FileDocCategorySizeDatePackage
SimContacts.javaAPI DocAndroid 1.5 API11131Wed May 06 22:42:46 BST 2009com.android.phone

SimContacts

public class SimContacts extends ADNList
SIM Address Book UI for the Phone app.

Fields Summary
private static final int
MENU_IMPORT_ONE
private static final int
MENU_IMPORT_ALL
private android.app.ProgressDialog
mProgressDialog
Constructors Summary
Methods Summary
private voidimportOne(int position)

        if (mCursor.moveToPosition(position)) {
            String name = mCursor.getString(NAME_COLUMN);
            String number = mCursor.getString(NUMBER_COLUMN);
            Object[] parsed = new Object[2];
            Uri personUrl = parseName(name, parsed);

            Intent intent;
            if (personUrl == null) {
                // Add a new contact
                intent = new Intent(Contacts.Intents.Insert.ACTION,
                        Contacts.People.CONTENT_URI);
                intent.putExtra(Contacts.Intents.Insert.NAME, (String)parsed[0]);
                intent.putExtra(Contacts.Intents.Insert.PHONE, number);
                intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, ((Integer)parsed[1]).intValue());
            } else {
                // Add the number to an existing contact
                intent = new Intent(Intent.ACTION_EDIT, personUrl);
                intent.putExtra(Contacts.Intents.Insert.PHONE, number);
                intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, ((Integer)parsed[1]).intValue());
            }
            startActivity(intent);
        }
    
protected android.widget.CursorAdapternewAdapter()

        return new SimpleCursorAdapter(this, R.layout.sim_import_list_entry, mCursor,
                new String[] { "name" }, new int[] { android.R.id.text1 });
    
public booleanonContextItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case MENU_IMPORT_ONE:
                ContextMenu.ContextMenuInfo menuInfo = item.getMenuInfo();
                if (menuInfo instanceof AdapterView.AdapterContextMenuInfo) {
                    int position = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
                    importOne(position);
                    return true;
                }
        }
        return super.onContextItemSelected(item);
    
protected voidonCreate(android.os.Bundle icicle)


    
    
        
        super.onCreate(icicle);
        registerForContextMenu(getListView());
    
public voidonCreateContextMenu(android.view.ContextMenu menu, android.view.View v, ContextMenu.ContextMenuInfo menuInfo)

        if (menuInfo instanceof AdapterView.AdapterContextMenuInfo) {
            AdapterView.AdapterContextMenuInfo itemInfo =
                    (AdapterView.AdapterContextMenuInfo) menuInfo;
            TextView textView = (TextView) itemInfo.targetView.findViewById(android.R.id.text1);
            if (textView != null) {
                menu.setHeaderTitle(textView.getText());
            }
            menu.add(0, MENU_IMPORT_ONE, 0, R.string.importSimEntry);
        }
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_IMPORT_ALL, 0, R.string.importAllSimEntries);
        return true;
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)

        switch (keyCode) {
            case KeyEvent.KEYCODE_CALL: {
                if (mCursor != null && mCursor.moveToPosition(getSelectedItemPosition())) {
                    String number = mCursor.getString(NUMBER_COLUMN);
                    if (number == null || !TextUtils.isGraphic(number)) {
                        // There is no number entered.
                        //TODO play error sound or something...
                        return true;
                    }
                    Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
                            Uri.fromParts("tel", number, null));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                          | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    startActivity(intent);
                    finish();
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    
public voidonListItemClick(android.widget.ListView l, android.view.View v, int position, long id)

        importOne(position);
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case MENU_IMPORT_ALL:
                CharSequence title = getString(R.string.importAllSimEntries);
                CharSequence message = getString(R.string.importingSimContacts); 

                ImportAllThread thread = new ImportAllThread();

                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setTitle(title);
                mProgressDialog.setMessage(message);
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setButton(getString(R.string.cancel), thread);
                mProgressDialog.setProgress(0);
                mProgressDialog.setMax(mCursor.getCount());
                mProgressDialog.show();
                
                thread.start();
                
                return true;
        }
        return super.onOptionsItemSelected(item);
    
private android.net.UriparseName(java.lang.String name, java.lang.Object[] parsed)
Parse the name looking for /W /H /M or /O at the end, signifying the type.

param
name The name from the SIM card
param
parsed slot 0 is filled in with the name, and slot 1 is filled in with the type

        // default to TYPE_MOBILE so you can send SMSs to the numbers
        int type = Contacts.PhonesColumns.TYPE_MOBILE;

        // Look for /W /H /M or /O at the end of the name signifying the type
        int nameLen = name.length();
        if (nameLen - 2 >= 0 && name.charAt(nameLen - 2) == '/") {
            char c = Character.toUpperCase(name.charAt(nameLen - 1));
            if (c == 'W") {
                type = Contacts.PhonesColumns.TYPE_WORK;
            } else if (c == 'M") {
                type = Contacts.PhonesColumns.TYPE_MOBILE;
            } else if (c == 'H") {
                type = Contacts.PhonesColumns.TYPE_HOME;
            } else if (c == 'O") {
                type = Contacts.PhonesColumns.TYPE_MOBILE;
            }
            name = name.substring(0, nameLen - 2);
        }
        parsed[0] = name;
        parsed[1] = type;

        StringBuilder where = new StringBuilder(Contacts.People.NAME);
        where.append('=");
        DatabaseUtils.appendEscapedSQLString(where, name);
        Uri url = null;
        Cursor c = getContentResolver().query(Contacts.People.CONTENT_URI,
                new String[] {Contacts.People._ID},
                where.toString(), null, null);
        if (c != null) {
            if (c.moveToFirst()) {
                url = ContentUris.withAppendedId(Contacts.People.CONTENT_URI, c.getLong(0));
            }
            c.deactivate();
        }
        return url;
    
protected android.net.UriresolveIntent()

        Intent intent = getIntent();
        intent.setData(Uri.parse("content://sim/adn"));
        if (Intent.ACTION_PICK.equals(intent.getAction())) {
            // "index" is 1-based
            mInitialSelection = intent.getIntExtra("index", 0) - 1;
        }
        return intent.getData();