Group添加联系人时,Contacts默认设计不会过滤已分组的联系人。之前看到小米,oppo都做过过滤,一直懒得改。
最近客户要求group添加成员时,不显示已分组的联系人,故记录一下实现过程。
public void pickMembers() { Intent intent = new Intent(MultiPickContactActivity.ACTION_MULTI_PICK); intent.putExtra(MultiPickContactActivity.IS_CONTACT,true); //add by antoon, for group pick contacts intent.putExtra(MultiPickContactActivity.IS_GROUP_MEMBER_PICK,true);//增加标记,表示Contacts多选界面进行的是group pick member //end add intent.setClass(mActivity, MultiPickContactActivity.class); ContactListFilter filter = new ContactListFilter(ContactListFilter.FILTER_TYPE_ACCOUNT, PhoneAccountType.ACCOUNT_TYPE, SimContactsConstants.PHONE_NAME, null, null); intent.putExtra(AccountFilterActivity.KEY_EXTRA_CONTACT_LIST_FILTER, filter); mActivity.startActivityForResult(intent, CODE_PICK_MEMBER); }
MultiPickContactActivity处理
//add by antoon, for group pick contacts public static final String IS_GROUP_MEMBER_PICK ="is_group_pick_contact"; public void onCreate(Bundle savedInstanceState) { ...... else if (ACTION_MULTI_PICK.equals(action)) { if (!isContact) { mMode = MODE_DEFAULT_PHONE; } else { mMode = MODE_DEFAULT_CONTACT; //add by antoon, for group pick contacts if(getIntent().getBooleanExtra(IS_GROUP_MEMBER_PICK, false)) setTitle(R.string.ungrouped_contacts); //end add } } ...... }
public void startQuery() { Uri uri = getUriToQuery(); ContactListFilter filter = (ContactListFilter) getIntent().getExtra( AccountFilterActivity.KEY_EXTRA_CONTACT_LIST_FILTER); if (filter != null) { if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT) { // We should exclude the invisiable contacts. uri = uri.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, filter.accountName).appendQueryParameter(RawContacts.ACCOUNT_TYPE, filter.accountType) .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, ContactsContract.Directory.DEFAULT+"").build(); //add by antoon, for group pick contacts if(getIntent().getBooleanExtra(IS_GROUP_MEMBER_PICK, false)){ uri = uri.buildUpon().appendQueryParameter(IS_GROUP_MEMBER_PICK, "true").build();//传递查询条件参数给ContactsProvider2使用 } //end add } else if (filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) { // Do not query sim contacts in airplane mode. if (!isShowSIM()) { uri = uri.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_TYPE, SimAccountType.ACCOUNT_TYPE) .appendQueryParameter(DefaultContactListAdapter.WITHOUT_SIM_FLAG, "true").build(); } } } String[] projection = getProjectionForQuery(); String selection = getSelectionForQuery(); String[] selectionArgs = getSelectionArgsForQuery(); mQueryHandler.startQuery(QUERY_TOKEN, null, uri, projection, selection, selectionArgs, getSortOrder(projection)); }
最关键是在ContactsProvider2中增加数据库查询判断条件。
protected Cursor queryLocal(final Uri uri, final String[] projection, String selection, String[] selectionArgs, String sortOrder, final long directoryId, final CancellationSignal cancellationSignal) { final SQLiteDatabase db = mDbHelper.get().getReadableDatabase(); SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); ...... final int match = sUriMatcher.match(uri); switch (match) { case SYNCSTATE: case PROFILE_SYNCSTATE: return mDbHelper.get().getSyncState().query(db, projection, selection, selectionArgs, sortOrder); case CONTACTS: { setTablesAndProjectionMapForContacts(qb, uri, projection); appendLocalDirectoryAndAccountSelectionIfNeeded(qb, directoryId, uri);//设置查询条件,where字句 break; }
private void appendLocalDirectoryAndAccountSelectionIfNeeded(SQLiteQueryBuilder qb, long directoryId, Uri uri) { final StringBuilder sb = new StringBuilder(); if (directoryId == Directory.DEFAULT) { sb.append("(" + Contacts._ID + " IN " + Tables.DEFAULT_DIRECTORY + ")"); } else if (directoryId == Directory.LOCAL_INVISIBLE){ sb.append("(" + Contacts._ID + " NOT IN " + Tables.DEFAULT_DIRECTORY + ")"); } else { sb.append("(1)"); } ...... //add by nxd 2015-5-18, for local group pick contacts String isGroupMemPick = getQueryParameter(uri, IS_GROUP_MEMBER_PICK ); if("true".equals(isGroupMemPick)){ sb.append(" AND (" + Contacts.NAME_RAW_CONTACT_ID + " NOT IN (" + "SELECT " + Data.RAW_CONTACT_ID + " FROM " + Tables.DATA + " WHERE " + Data.MIMETYPE + "_id =("
+ "SELECT _id FROM mimetypes "
+ "WHERE mimetype='vnd.android.cursor.item/local-groups')))");// mimetype_id=local-groups对应id,说明已分组
} //end add qb.appendWhere(sb.toString()); }