From 609aa137f7436ba033f7d077c1328bb4d26f447b Mon Sep 17 00:00:00 2001 From: Richard MacGregor Date: Mon, 25 Apr 2016 10:17:31 -0700 Subject: Modify "(1/2) Call Lookup: Update call log handler to query usernames" This paritally reverts commit 22eeaf50aa43112156d11b665857cc867d21ae63. Then modifies it to not use isPhoneNumber, which was causing issues when InCallApi usernames had digits. Ticket CD-460 Change-Id: I751fa70a18ab2752ff7a3adfceda53b041051a8d --- .../providers/contacts/ContactsDatabaseHelper.java | 32 +++++++-- .../providers/contacts/ContactsProvider2.java | 75 +++++++++++++--------- 2 files changed, 69 insertions(+), 38 deletions(-) diff --git a/src/com/android/providers/contacts/ContactsDatabaseHelper.java b/src/com/android/providers/contacts/ContactsDatabaseHelper.java index 143f6b61..89351494 100644 --- a/src/com/android/providers/contacts/ContactsDatabaseHelper.java +++ b/src/com/android/providers/contacts/ContactsDatabaseHelper.java @@ -5255,8 +5255,12 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { public void buildDataLookupAndContactQuery(SQLiteQueryBuilder qb, String data) { StringBuilder sb = new StringBuilder(); - buildDataQuery(sb, data); + buildDataQueryTables(sb); qb.setTables(sb.toString()); + + sb = new StringBuilder(); + buildDataQuerySelection(sb, data); + qb.appendWhere(sb.toString()); } /** @@ -5336,11 +5340,27 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { } private void buildDataQuery(StringBuilder sb, String lookupData) { - // Todo: make more performant - sb.append(Tables.RAW_CONTACTS + - " JOIN " + Views.DATA + " data_view ON (data_view.raw_contact_id = " - + Tables.RAW_CONTACTS + "._id) WHERE data1 = "); - DatabaseUtils.appendEscapedSQLString(sb, lookupData); + sb.append(Tables.RAW_CONTACTS); + sb.append(" JOIN " + Views.CONTACTS + " contacts_view" + + " ON (contacts_view._id = raw_contacts.contact_id)"); + sb.append(" JOIN " + Views.DATA + " data_view" + + " ON (data_view.raw_contact_id = " + Tables.RAW_CONTACTS + "._id)"); + } + + private void buildDataQueryTables(StringBuilder sb) { + sb.append(Tables.RAW_CONTACTS); + sb.append(" JOIN " + Views.CONTACTS + " contacts_view" + + " ON (contacts_view._id = raw_contacts.contact_id)"); + sb.append(", (SELECT data_id, normalized_number, length(normalized_number) as len " + + " FROM phone_lookup) AS lookup, " + Tables.DATA); + } + + private void buildDataQuerySelection(StringBuilder sb, String lookupData) { + sb.append("data.raw_contact_id=raw_contacts._id"); + if (!TextUtils.isEmpty(lookupData)) { + sb.append(" AND data.data1 = "); + DatabaseUtils.appendEscapedSQLString(sb, lookupData); + } } private void appendPhoneLookupSelection(StringBuilder sb, String number, String numberE164) { diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index 870b912d..7c1f543d 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -213,6 +213,14 @@ public class ContactsProvider2 extends AbstractContactsProvider private static final String WRITE_PERMISSION = "android.permission.WRITE_CONTACTS"; private static final String INTERACT_ACROSS_USERS = "android.permission.INTERACT_ACROSS_USERS"; + /** + * If this boolean parameter is set to true, then the appended query is treated as a + * InCallApi plugin contact ID and the lookup will be performed against InCallApi contacts in + * the user's contacts. + */ + // TODO: Move this to a more central place + private static final String QUERY_PARAMETER_INCALLAPI_ID = "incallapi_contactid"; + /* package */ static final String UPDATE_TIMES_CONTACTED_CONTACTS_TABLE = "UPDATE " + Tables.CONTACTS + " SET " + Contacts.TIMES_CONTACTED + "=" + " ifnull(" + Contacts.TIMES_CONTACTED + ",0)+1" + @@ -6361,7 +6369,9 @@ public class ContactsProvider2 extends AbstractContactsProvider // Phone lookup cannot be combined with a selection selection = null; selectionArgs = null; - if (uri.getBooleanQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, false)) { + final boolean isSipAddress = uri.getBooleanQueryParameter( + PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, false); + if (isSipAddress) { if (TextUtils.isEmpty(sortOrder)) { // Default the sort order to something reasonable so we get consistent // results when callers don't request an ordering @@ -6383,33 +6393,32 @@ public class ContactsProvider2 extends AbstractContactsProvider String number = uri.getPathSegments().size() > 1 ? uri.getLastPathSegment() : ""; - - boolean isPhoneNumber = isPhoneNumber(number); - - String[] projectionWithNumber; - if (isPhoneNumber) { - String numberE164 = PhoneNumberUtils.formatNumberToE164( - number, mDbHelper.get().getCurrentCountryIso()); - String normalizedNumber = PhoneNumberUtils.normalizeNumber(number); + String numberE164 = PhoneNumberUtils.formatNumberToE164( + number, mDbHelper.get().getCurrentCountryIso()); + String normalizedNumber = PhoneNumberUtils.normalizeNumber(number); + + // Change query based on call type... + final boolean isInCallPluginContactId = uri.getBooleanQueryParameter( + QUERY_PARAMETER_INCALLAPI_ID, false); + if (isInCallPluginContactId) { + // InCallApi contact id + mDbHelper.get().buildDataLookupAndContactQuery(qb, number); + } else { + // Regular phone number mDbHelper.get().buildPhoneLookupAndContactQuery( qb, normalizedNumber, numberE164); - qb.setProjectionMap(sPhoneLookupProjectionMap); - - // removeNonStarMatchesFromCursor() requires the cursor to contain - // PhoneLookup.NUMBER. Therefore, if the projection explicitly omits it, extend - // the projection. - projectionWithNumber = projection; - if (projection != null - && !ArrayUtils.contains(projection,PhoneLookup.NUMBER)) { - projectionWithNumber = ArrayUtils.appendElement( - String.class, projection, PhoneLookup.NUMBER); - } - } else { - mDbHelper.get().buildDataLookupAndContactQuery(qb, number); - projectionWithNumber = new String[0]; - sortOrder = null; } - + qb.setProjectionMap(sPhoneLookupProjectionMap); + + // removeNonStarMatchesFromCursor() requires the cursor to contain + // PhoneLookup.NUMBER. Therefore, if the projection explicitly omits it, extend + // the projection. + String[] projectionWithNumber = projection; + if (projection != null + && !ArrayUtils.contains(projection,PhoneLookup.NUMBER)) { + projectionWithNumber = ArrayUtils.appendElement( + String.class, projection, PhoneLookup.NUMBER); + } // Peek at the results of the first query (which attempts to use fully // normalized and internationalized numbers for comparison). If no results @@ -6422,12 +6431,8 @@ public class ContactsProvider2 extends AbstractContactsProvider try { if (cursor.getCount() > 0) { foundResult = true; - if (isPhoneNumber) { - return PhoneLookupWithStarPrefix - .removeNonStarMatchesFromCursor(number, cursor); - } else { - return cursor; - } + return PhoneLookupWithStarPrefix + .removeNonStarMatchesFromCursor(number, cursor); } // Use the fall-back lookup method. @@ -6908,11 +6913,17 @@ public class ContactsProvider2 extends AbstractContactsProvider final String phoneNumber = Uri.decode(uri.getLastPathSegment()); final boolean isSipAddress = uri.getBooleanQueryParameter( PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, false); + // Only allow inCallApiContact specification if not SIP address + final boolean isInCallPluginContactId = uri.getBooleanQueryParameter( + QUERY_PARAMETER_INCALLAPI_ID, false) && !isSipAddress; final Uri localUri = PhoneLookup.CONTENT_FILTER_URI .buildUpon() .appendPath(phoneNumber) .appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, - String.valueOf(isSipAddress)).build(); + String.valueOf(isSipAddress)) + .appendQueryParameter(QUERY_PARAMETER_INCALLAPI_ID, + String.valueOf(isInCallPluginContactId)) + .build(); return queryEnterpriseIfNecessary(localUri, projection, null, null, null, isSipAddress ? Data.CONTACT_ID : PhoneLookup._ID); } -- cgit v1.2.3