FileDocCategorySizeDatePackage
ManageSimMessages.javaAPI DocAndroid 1.5 API11686Wed May 06 22:42:46 BST 2009com.android.mms.ui

ManageSimMessages

public class ManageSimMessages extends android.app.Activity implements View.OnCreateContextMenuListener
Displays a list of the SMS messages stored on the SIM.

Fields Summary
private static final android.net.Uri
SIM_URI
private static final String
TAG
private static final int
MENU_COPY_TO_PHONE_MEMORY
private static final int
MENU_DELETE_FROM_SIM
private static final int
MENU_VIEW
private static final int
OPTION_MENU_DELETE_ALL
private static final int
SHOW_LIST
private static final int
SHOW_EMPTY
private static final int
SHOW_BUSY
private int
mState
private android.content.ContentResolver
mContentResolver
private android.database.Cursor
mCursor
private android.widget.ListView
mSimList
private android.widget.TextView
mMessage
private MessageListAdapter
mListAdapter
private android.content.AsyncQueryHandler
mQueryHandler
public static final int
SIM_FULL_NOTIFICATION_ID
private final android.database.ContentObserver
simChangeObserver
Constructors Summary
Methods Summary
private voidconfirmDeleteDialog(android.content.DialogInterface.OnClickListener listener, int messageId)

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.confirm_dialog_title);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setCancelable(true);
        builder.setPositiveButton(R.string.yes, listener);
        builder.setNegativeButton(R.string.no, null);
        builder.setMessage(messageId);

        builder.show();
    
private voidcopyToPhoneMemory(android.database.Cursor cursor)

        String address = cursor.getString(
                cursor.getColumnIndexOrThrow("address"));
        String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
        Long date = cursor.getLong(cursor.getColumnIndexOrThrow("date"));

        try {
            if (isIncomingMessage(cursor)) {
                Sms.Inbox.addMessage(mContentResolver, address, body, null, date, true);
            } else {
                Sms.Sent.addMessage(mContentResolver, address, body, null, date);
            }
        } catch (SQLiteException e) {
            SqliteWrapper.checkSQLiteException(this, e);
        }
    
private voiddeleteAllFromSim()

        Cursor cursor = (Cursor) mListAdapter.getCursor();

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int count = cursor.getCount();

                for (int i = 0; i < count; ++i) {
                    deleteFromSim(cursor);
                    cursor.moveToNext();
                }
            }
        }
    
private voiddeleteFromSim(android.database.Cursor cursor)

        String messageIndexString =
                cursor.getString(cursor.getColumnIndexOrThrow("index_on_sim"));
        Uri simUri = SIM_URI.buildUpon().appendPath(messageIndexString).build();

        SqliteWrapper.delete(this, mContentResolver, simUri, null, null);
    
private voidinit()

        MessagingNotification.cancelNotification(getApplicationContext(),
                SIM_FULL_NOTIFICATION_ID);

        updateState(SHOW_BUSY);
        startQuery();
    
private booleanisIncomingMessage(android.database.Cursor cursor)

        int messageStatus = cursor.getInt(
                cursor.getColumnIndexOrThrow("status"));

        return (messageStatus == SmsManager.STATUS_ON_SIM_READ) ||
               (messageStatus == SmsManager.STATUS_ON_SIM_UNREAD);
    
public booleanonContextItemSelected(android.view.MenuItem item)

        AdapterView.AdapterContextMenuInfo info;
        try {
             info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        } catch (ClassCastException exception) {
            Log.e(TAG, "Bad menuInfo.", exception);
            return false;
        }

        final Cursor cursor = (Cursor) mListAdapter.getItem(info.position);

        switch (item.getItemId()) {
            case MENU_COPY_TO_PHONE_MEMORY:
                copyToPhoneMemory(cursor);
                return true;
            case MENU_DELETE_FROM_SIM:
                confirmDeleteDialog(new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        updateState(SHOW_BUSY);
                        deleteFromSim(cursor);
                    }
                }, R.string.confirm_delete_SIM_message);
                return true;
            case MENU_VIEW:
                viewMessage(cursor);
                return true;
        }
        return super.onContextItemSelected(item);
    
protected voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        mContentResolver = getContentResolver();
        mQueryHandler = new QueryHandler(mContentResolver, this);
        setContentView(R.layout.sim_list);
        mSimList = (ListView) findViewById(R.id.messages);
        mMessage = (TextView) findViewById(R.id.empty_message);

        init();
    
public voidonCreateContextMenu(android.view.ContextMenu menu, android.view.View v, ContextMenu.ContextMenuInfo menuInfo)

        menu.add(0, MENU_COPY_TO_PHONE_MEMORY, 0,
                 R.string.sim_copy_to_phone_memory);
        menu.add(0, MENU_DELETE_FROM_SIM, 0, R.string.sim_delete);

        // TODO: Enable this once viewMessage is written.
        // menu.add(0, MENU_VIEW, 0, R.string.sim_view);
    
protected voidonNewIntent(android.content.Intent intent)

        setIntent(intent);

        init();
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case OPTION_MENU_DELETE_ALL:
                confirmDeleteDialog(new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        updateState(SHOW_BUSY);
                        deleteAllFromSim();
                    }
                }, R.string.confirm_delete_all_SIM_messages);
                break;
        }

        return true;
    
public voidonPause()

        super.onPause();
        mContentResolver.unregisterContentObserver(simChangeObserver);
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        menu.clear();

        if ((null != mCursor) && (mCursor.getCount() > 0) && mState == SHOW_LIST) {
            menu.add(0, OPTION_MENU_DELETE_ALL, 0, R.string.menu_delete_messages).setIcon(
                    android.R.drawable.ic_menu_delete);
        }

        return true;
    
public voidonResume()

        super.onResume();
        registerSimChangeObserver();
    
private voidrefreshMessageList()

        updateState(SHOW_BUSY);
        if (mCursor != null) {
            stopManagingCursor(mCursor);
            mCursor.close();
        }
        startQuery();
    
private voidregisterSimChangeObserver()

        mContentResolver.registerContentObserver(
                SIM_URI, true, simChangeObserver);
    
private voidstartQuery()

        try {
            mQueryHandler.startQuery(0, null, SIM_URI, null, null, null, null);
        } catch (SQLiteException e) {
            SqliteWrapper.checkSQLiteException(this, e);
        }
    
private voidupdateState(int state)

        if (mState == state) {
            return;
        }

        mState = state;
        switch (state) {
            case SHOW_LIST:
                mSimList.setVisibility(View.VISIBLE);
                mMessage.setVisibility(View.GONE);
                setTitle(getString(R.string.sim_manage_messages_title));
                setProgressBarIndeterminateVisibility(false);
                break;
            case SHOW_EMPTY:
                mSimList.setVisibility(View.GONE);
                mMessage.setVisibility(View.VISIBLE);
                setTitle(getString(R.string.sim_manage_messages_title));
                setProgressBarIndeterminateVisibility(false);
                break;
            case SHOW_BUSY:
                mSimList.setVisibility(View.GONE);
                mMessage.setVisibility(View.GONE);
                setTitle(getString(R.string.refreshing));
                setProgressBarIndeterminateVisibility(true);
                break;
            default:
                Log.e(TAG, "Invalid State");
        }
    
private voidviewMessage(android.database.Cursor cursor)

        // TODO: Add this.