Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to exclude SIM contacts when using Intent.ACTION_PICK?

I need to pick contacts in my app and would like to exclude those which are stored in my SIM card. Is it possible with ACTION_PICK?

like image 271
Maksim Dmitriev Avatar asked Nov 08 '22 21:11

Maksim Dmitriev


1 Answers

No, it's not possible

Unfortunately, it's not possible for now.

To proof it, let's dive into source code of ContanctsListActivity.
Here's an onCreate() method of the Activity. In it, ContactApp reads Intent(ACTION_PICK) we passing to it and handles it respectively:

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mIconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
    mContactsPrefs = new ContactsPreferences(this);
    mPhotoLoader = new ContactPhotoLoader(this, R.drawable.ic_contact_list_picture);

    // Resolve the intent
    final Intent intent = getIntent();

    // Allow the title to be set to a custom String using an extra on the intent
    String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
    if (title != null) {
        setTitle(title);
    }

    String action = intent.getAction();
    String component = intent.getComponent().getClassName();

    // When we get a FILTER_CONTACTS_ACTION, it represents search in the context
    // of some other action. Let's retrieve the original action to provide proper
    // context for the search queries.
    if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
        mSearchMode = true;
        mShowSearchSnippets = true;
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mInitialFilter = extras.getString(UI.FILTER_TEXT_EXTRA_KEY);
            String originalAction =
                    extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
            if (originalAction != null) {
                action = originalAction;
            }
            String originalComponent =
                    extras.getString(ContactsSearchManager.ORIGINAL_COMPONENT_EXTRA_KEY);
            if (originalComponent != null) {
                component = originalComponent;
            }
        } else {
            mInitialFilter = null;
        }
    }

    Log.i(TAG, "Called with action: " + action);
    mMode = MODE_UNKNOWN;
    if (UI.LIST_DEFAULT.equals(action) || UI.FILTER_CONTACTS_ACTION.equals(action)) {
        ..... 
        else if (Intent.ACTION_PICK.equals(action)) {
        // XXX These should be showing the data from the URI given in
        // the Intent.
        final String type = intent.resolveType(this);
        if (Contacts.CONTENT_TYPE.equals(type)) {
            mMode = MODE_PICK_CONTACT;
        } else if (People.CONTENT_TYPE.equals(type)) {
            mMode = MODE_LEGACY_PICK_PERSON;
        } else if (Phone.CONTENT_TYPE.equals(type)) {
            mMode = MODE_PICK_PHONE;
        } else if (Phones.CONTENT_TYPE.equals(type)) {
            mMode = MODE_LEGACY_PICK_PHONE;
        } else if (StructuredPostal.CONTENT_TYPE.equals(type)) {
            mMode = MODE_PICK_POSTAL;
        } else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(type)) {
            mMode = MODE_LEGACY_PICK_POSTAL;
        }
       ....
       // VERY LONG IF WITH DIFFERENT MODE-SELECTION
       ....
    }
    .....
    if (mMode == MODE_JOIN_CONTACT) {
        setContentView(R.layout.contacts_list_content_join);
    } else if (mSearchMode) {
        setContentView(R.layout.contacts_search_content);
    } else if (mSearchResultsMode) {
        setContentView(R.layout.contacts_list_search_results);
    } else {
        setContentView(R.layout.contacts_list_content);
    }

    setupListView();
    ...
}

It's very long method (and I also suggest to check setupListView() method), but pretty straightforward. And, as you can see, there's no parameter you can pass to specify source of contacts you want to pick from. Only thing you can configure here is the particular mMode ContactsApp to use (MODE_PICK_CONTACT, MODE_PICK_PHONE, etc.) - but unfortunately number of possible modes is very limited by 6 and non of them suits you.

(If anyone needs to pass mMode to ContanctsListActivity - use intent's setType() method, for example: intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);)

Workaround

As a workaround - as tiny sunlight's suggested in comments - render non-SIM contacts within the app and pick the one you needed from there.
How to get all android contacts but without those which are on SIM - this link looks like most promising one explaining how to query cursor with all contacts, apart from SIM ones.

I hope, it helps

like image 100
Konstantin Loginov Avatar answered Nov 14 '22 21:11

Konstantin Loginov