diff options
author | blong <blong@codeaurora.org> | 2014-07-24 17:18:35 +0800 |
---|---|---|
committer | Linux Build Service Account <lnxbuild@localhost> | 2015-08-24 01:31:48 -0600 |
commit | e52bc7e9db5534767f3b44f70ad4a6d25089347c (patch) | |
tree | d134e5686eed43990f269d8453931d31fd8313a5 /src | |
parent | dab20159eff14d09c82c86c839cc0a404a350659 (diff) | |
download | packages_providers_ContactsProvider-e52bc7e9db5534767f3b44f70ad4a6d25089347c.zip packages_providers_ContactsProvider-e52bc7e9db5534767f3b44f70ad4a6d25089347c.tar.gz packages_providers_ContactsProvider-e52bc7e9db5534767f3b44f70ad4a6d25089347c.tar.bz2 |
Add supports for SIM and Phone account
- Add a flag to filter SIM contacts
- Use the Phone account as default account
Change-Id: If2219a13b0c915504375d38f9a0bcbf274250ccb
Diffstat (limited to 'src')
4 files changed, 165 insertions, 24 deletions
diff --git a/src/com/android/providers/contacts/AccountWithDataSet.java b/src/com/android/providers/contacts/AccountWithDataSet.java index bfae112..a654f50 100644 --- a/src/com/android/providers/contacts/AccountWithDataSet.java +++ b/src/com/android/providers/contacts/AccountWithDataSet.java @@ -25,7 +25,11 @@ import com.google.common.base.Objects; * Account information that includes the data set, if any. */ public class AccountWithDataSet { - public static final AccountWithDataSet LOCAL = new AccountWithDataSet(null, null, null); + public static final String PHONE_NAME = "PHONE"; + public static final String ACCOUNT_TYPE_PHONE = "com.android.localphone"; + public static final String ACCOUNT_TYPE_SIM = "com.android.sim"; + public static final AccountWithDataSet LOCAL = new AccountWithDataSet( + PHONE_NAME, ACCOUNT_TYPE_PHONE, null); private final String mAccountName; private final String mAccountType; @@ -62,7 +66,7 @@ public class AccountWithDataSet { } public boolean isLocalAccount() { - return (mAccountName == null) && (mAccountType == null); + return (PHONE_NAME.equals(mAccountName)) && (ACCOUNT_TYPE_PHONE.equals(mAccountType)); } @Override diff --git a/src/com/android/providers/contacts/ContactsDatabaseHelper.java b/src/com/android/providers/contacts/ContactsDatabaseHelper.java index aac37ba..1d43fbc 100644 --- a/src/com/android/providers/contacts/ContactsDatabaseHelper.java +++ b/src/com/android/providers/contacts/ContactsDatabaseHelper.java @@ -335,9 +335,8 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { "(SELECT " + AccountsColumns._ID + " FROM " + Tables.ACCOUNTS + " WHERE " + - AccountsColumns.ACCOUNT_NAME + " IS NULL AND " + - AccountsColumns.ACCOUNT_TYPE + " IS NULL AND " + - AccountsColumns.DATA_SET + " IS NULL)"; + AccountsColumns.ACCOUNT_TYPE + "='" + + AccountWithDataSet.ACCOUNT_TYPE_PHONE + "')"; final String RAW_CONTACT_IS_LOCAL = RawContactsColumns.CONCRETE_ACCOUNT_ID + "=" + LOCAL_ACCOUNT_ID; @@ -351,8 +350,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { "SELECT " + "MAX((SELECT (CASE WHEN " + "(CASE" + - " WHEN " + RAW_CONTACT_IS_LOCAL + - " THEN 1 " + " WHEN " + ZERO_GROUP_MEMBERSHIPS + " THEN " + Settings.UNGROUPED_VISIBLE + " ELSE MAX(" + Groups.GROUP_VISIBLE + ")" + @@ -2022,13 +2019,18 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { String contactsSelect = "SELECT " + ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID + "," + contactsColumns + ", " + + AccountsColumns.ACCOUNT_NAME + ", " + + AccountsColumns.ACCOUNT_TYPE + ", " + buildDisplayPhotoUriAlias(ContactsColumns.CONCRETE_ID, Contacts.PHOTO_URI) + ", " + buildThumbnailPhotoUriAlias(ContactsColumns.CONCRETE_ID, Contacts.PHOTO_THUMBNAIL_URI) + ", " + dbForProfile() + " AS " + Contacts.IS_USER_PROFILE + " FROM " + Tables.CONTACTS + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON(" - + Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")"; + + Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")" + + " JOIN " + Tables.ACCOUNTS + " AS name_accounts ON(" + + "name_raw_contact." + RawContactsColumns.ACCOUNT_ID + "=name_accounts." + + AccountsColumns._ID + ")"; db.execSQL("CREATE VIEW " + Views.CONTACTS + " AS " + contactsSelect); diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index 7dc7e7e..94bf413 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -300,6 +300,7 @@ public class ContactsProvider2 extends AbstractContactsProvider private static final String FREQUENT_ORDER_BY = DataUsageStatColumns.TIMES_USED + " DESC," + Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; + private static String WITHOUT_SIM_FLAG = "no_sim"; private static final int CONTACTS = 1000; private static final int CONTACTS_ID = 1001; @@ -771,6 +772,8 @@ public class ContactsProvider2 extends AbstractContactsProvider .add(Contacts.IS_USER_PROFILE) .addAll(sContactsColumns) .addAll(sContactsPresenceColumns) + .add(RawContacts.ACCOUNT_TYPE) + .add(RawContacts.ACCOUNT_NAME) .build(); /** Contains just the contacts columns */ @@ -917,6 +920,7 @@ public class ContactsProvider2 extends AbstractContactsProvider .addAll(sDataColumns) .addAll(sDataPresenceColumns) .addAll(sContactsColumns) + .addAll(sRawContactColumns) .addAll(sContactPresenceColumns) .addAll(sDataUsageColumns) .build(); @@ -2751,7 +2755,7 @@ public class ContactsProvider2 extends AbstractContactsProvider final long rawContactId = db.insert(Tables.RAW_CONTACTS, RawContacts.CONTACT_ID, values); final int aggregationMode = getIntValue(values, RawContacts.AGGREGATION_MODE, - RawContacts.AGGREGATION_MODE_DEFAULT); + RawContacts.AGGREGATION_MODE_SUSPENDED); mAggregator.get().markNewForAggregation(rawContactId, aggregationMode); // Trigger creation of a Contact based on this RawContact at the end of transaction. @@ -7322,6 +7326,58 @@ public class ContactsProvider2 extends AbstractContactsProvider StringBuilder sb = new StringBuilder(); sb.append(Views.CONTACTS); + /* Do not show contacts when SIM card is disabled for CONTACTS_FILTER */ + StringBuilder sbWhere = new StringBuilder(); + String withoutSim = getQueryParameter(uri, WITHOUT_SIM_FLAG ); + if ("true".equals(withoutSim)) { + final long[] accountId = getAccountIdWithoutSim(uri); + if (accountId == null) { + // No such account. + sbWhere.setLength(0); + sbWhere.append("(1=2)"); + } else { + if (accountId.length > 0) { + sbWhere.append(" (" + Contacts._ID + " not IN (" + "SELECT " + + RawContacts.CONTACT_ID + " FROM " + + Tables.RAW_CONTACTS + " WHERE " + + RawContacts.CONTACT_ID + " not NULL AND ( "); + for (int i = 0; i < accountId.length; i++) { + sbWhere.append(RawContactsColumns.ACCOUNT_ID + "=" + + accountId[i]); + if (i != accountId.length - 1) { + sbWhere.append(" OR "); + } + } + sbWhere.append(")))"); + } + } + } else { + final AccountWithDataSet accountWithDataSet = getAccountWithDataSetFromUri(uri); + // Accounts are valid by only checking one parameter, since we've + // already ruled out partial accounts. + final boolean validAccount = !TextUtils.isEmpty(accountWithDataSet.getAccountName()); + if (validAccount) { + final Long accountId = mDbHelper.get().getAccountIdOrNull(accountWithDataSet); + if (accountId != null) { + sbWhere.append(" INNER JOIN (SELECT " + + RawContacts.CONTACT_ID + + " AS raw_contact_contact_id FROM " + + Tables.RAW_CONTACTS + " WHERE " + + RawContactsColumns.ACCOUNT_ID + " = " + + accountId + + ") ON raw_contact_contact_id = " + Contacts._ID); + } + } + } + + if (!TextUtils.isEmpty(sbWhere.toString())) { + if ("true".equals(withoutSim)) { + qb.appendWhere(sbWhere.toString()); + } else { + sb.append(sbWhere.toString()); + } + } + if (filter != null) { filter = filter.trim(); } @@ -7746,22 +7802,50 @@ public class ContactsProvider2 extends AbstractContactsProvider sb.append("(1)"); } - final AccountWithDataSet accountWithDataSet = getAccountWithDataSetFromUri(uri); - // Accounts are valid by only checking one parameter, since we've - // already ruled out partial accounts. - final boolean validAccount = !TextUtils.isEmpty(accountWithDataSet.getAccountName()); - if (validAccount) { - final Long accountId = mDbHelper.get().getAccountIdOrNull(accountWithDataSet); + String withoutSim = getQueryParameter(uri, WITHOUT_SIM_FLAG ); + if ("true".equals(withoutSim)) { + final long[] accountId = getAccountIdWithoutSim(uri); + if (accountId == null) { // No such account. sb.setLength(0); sb.append("(1=2)"); } else { - sb.append( - " AND (" + Contacts._ID + " IN (" + - "SELECT " + RawContacts.CONTACT_ID + " FROM " + Tables.RAW_CONTACTS + - " WHERE " + RawContactsColumns.ACCOUNT_ID + "=" + accountId.toString() + - "))"); + if (accountId.length > 0) { + sb.append( + " AND (" + Contacts._ID + " not IN (" + + "SELECT " + RawContacts.CONTACT_ID + " FROM " + + Tables.RAW_CONTACTS + + " WHERE " + RawContacts.CONTACT_ID + " not NULL AND ( "); + for (int i = 0; i < accountId.length; i++) { + sb.append(RawContactsColumns.ACCOUNT_ID + "=" + + accountId[i]); + if (i != accountId.length - 1) { + sb.append(" or "); + } + } + sb.append(")))"); + } + } + } + else { + final AccountWithDataSet accountWithDataSet = getAccountWithDataSetFromUri(uri); + // Accounts are valid by only checking one parameter, since we've + // already ruled out partial accounts. + final boolean validAccount = !TextUtils.isEmpty(accountWithDataSet.getAccountName()); + if (validAccount) { + final Long accountId = mDbHelper.get().getAccountIdOrNull(accountWithDataSet); + if (accountId == null) { + // No such account. + sb.setLength(0); + sb.append("(1=2)"); + } else { + sb.append( + " AND (" + Contacts._ID + " IN (" + + "SELECT " + RawContacts.CONTACT_ID + " FROM " + Tables.RAW_CONTACTS + + " WHERE " + RawContactsColumns.ACCOUNT_ID + "=" + accountId.toString() + + "))"); + } } } qb.appendWhere(sb.toString()); @@ -7811,6 +7895,56 @@ public class ContactsProvider2 extends AbstractContactsProvider } } + private long[] getAccountIdWithoutSim(Uri uri) { + final String accountType = getQueryParameter(uri, RawContacts.ACCOUNT_TYPE); + final String accountName = getQueryParameter(uri, RawContacts.ACCOUNT_NAME); + Cursor c = null; + SQLiteDatabase db = mContactsHelper.getWritableDatabase(); + long[] accountId = null; + try { + if (null != accountType) { + c = db.query(Tables.ACCOUNTS, + new String[] { AccountsColumns._ID }, + AccountsColumns.ACCOUNT_TYPE + "=?", + new String[] { String.valueOf(accountType) }, null, + null, null); + } else if (!TextUtils.isEmpty(accountName)) { + String[] names = accountName.split(","); + int nameCount = names.length; + String where = AccountsColumns.ACCOUNT_NAME + "=?"; + StringBuilder selection = new StringBuilder(); + String[] selectionArgs = new String[nameCount]; + for (int i = 0; i < nameCount; i++) { + selection.append(where); + if (i != nameCount - 1) { + selection.append(" OR "); + } + selectionArgs[i] = names[i]; + } + c = db.query(Tables.ACCOUNTS, + new String[] { AccountsColumns._ID }, + selection.toString(), + selectionArgs, null, + null, null); + } + + if (c != null) { + accountId = new long[c.getCount()]; + + for (int i = 0; i < c.getCount(); i++) { + if (c.moveToNext()) { + accountId[c.getPosition()] = c.getInt(0); + } + } + } + } finally { + if (c != null) { + c.close(); + } + } + return accountId; + } + private AccountWithDataSet getAccountWithDataSetFromUri(Uri uri) { final String accountName = getQueryParameter(uri, RawContacts.ACCOUNT_NAME); final String accountType = getQueryParameter(uri, RawContacts.ACCOUNT_TYPE); diff --git a/src/com/android/providers/contacts/LegacyApiSupport.java b/src/com/android/providers/contacts/LegacyApiSupport.java index 598a4a0..060f87e 100644 --- a/src/com/android/providers/contacts/LegacyApiSupport.java +++ b/src/com/android/providers/contacts/LegacyApiSupport.java @@ -1937,8 +1937,7 @@ public class LegacyApiSupport { sb.append(" AND " + RawContacts.ACCOUNT_TYPE + "="); DatabaseUtils.appendEscapedSQLString(sb, mAccount.type); } else { - sb.append(RawContacts.ACCOUNT_NAME + " IS NULL" + - " AND " + RawContacts.ACCOUNT_TYPE + " IS NULL"); + sb.append("1=1"); } } @@ -1955,8 +1954,10 @@ public class LegacyApiSupport { sb.append(" AND " + Groups.ACCOUNT_TYPE + "="); DatabaseUtils.appendEscapedSQLString(sb, mAccount.type); } else { - sb.append(Groups.ACCOUNT_NAME + " IS NULL" + - " AND " + Groups.ACCOUNT_TYPE + " IS NULL"); + sb.append(Groups.ACCOUNT_NAME + " = '" + + AccountWithDataSet.LOCAL.getAccountName() + "' AND " + + Groups.ACCOUNT_TYPE + " = '" + + AccountWithDataSet.LOCAL.getAccountType() + "'"); } } |