diff options
-rw-r--r-- | src/com/android/providers/contacts/ContactsProvider2.java | 31 | ||||
-rw-r--r-- | tests/src/com/android/providers/contacts/ContactsProvider2Test.java | 24 |
2 files changed, 40 insertions, 15 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index e853833..130134b 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -6530,14 +6530,16 @@ public class ContactsProvider2 extends AbstractContactsProvider if (VERBOSE_LOGGING) { Log.v(TAG, "queryPhoneLookupEnterprise: corp query URI=" + remoteUri); } - final Cursor corp = getContext().getContentResolver().query(remoteUri, projection, + // Note in order to re-write the cursor correctly, we need all columns from the corp cp2. + final Cursor corp = getContext().getContentResolver().query(remoteUri, null, /* selection */ null, /* args */ null, /* order */ null, /* cancellationsignal*/ null); try { if (VERBOSE_LOGGING) { MoreDatabaseUtils.dumpCursor(TAG, "corp raw", corp); } - final Cursor rewritten = rewriteCorpPhoneLookup(corp); + final Cursor rewritten = rewriteCorpPhoneLookup( + (projection != null ? projection : corp.getColumnNames()), corp); if (VERBOSE_LOGGING) { MoreDatabaseUtils.dumpCursor(TAG, "corp rewritten", rewritten); } @@ -6552,18 +6554,20 @@ public class ContactsProvider2 extends AbstractContactsProvider * Rewrite a cursor from the corp profile for {@link PhoneLookup#ENTERPRISE_CONTENT_FILTER_URI}. */ @VisibleForTesting - static Cursor rewriteCorpPhoneLookup(Cursor original) { - final String[] columns = original.getColumnNames(); - final MatrixCursor ret = new MatrixCursor(columns); + static Cursor rewriteCorpPhoneLookup(String[] outputProjection, Cursor original) { + final MatrixCursor ret = new MatrixCursor(outputProjection); original.moveToPosition(-1); while (original.moveToNext()) { + // Note PhoneLookup._ID is a contact ID, not a data ID. final int contactId = original.getInt(original.getColumnIndex(PhoneLookup._ID)); final MatrixCursor.RowBuilder builder = ret.newRow(); - for (int i = 0; i < columns.length; i++) { - switch (columns[i]) { + for (int i = 0; i < outputProjection.length; i++) { + final String outputColumnName = outputProjection[i]; + final int originalColumnIndex = original.getColumnIndex(outputColumnName); + switch (outputColumnName) { // Set artificial photo URLs using Contacts.CORP_CONTENT_URI. case PhoneLookup.PHOTO_THUMBNAIL_URI: builder.add(getCorpThumbnailUri(contactId, original)); @@ -6572,7 +6576,8 @@ public class ContactsProvider2 extends AbstractContactsProvider builder.add(getCorpDisplayPhotoUri(contactId, original)); break; case PhoneLookup._ID: - builder.add(original.getLong(i) + Contacts.ENTERPRISE_CONTACT_ID_BASE); + builder.add(original.getLong(originalColumnIndex) + + Contacts.ENTERPRISE_CONTACT_ID_BASE); break; // These columns are set to null. @@ -6584,21 +6589,21 @@ public class ContactsProvider2 extends AbstractContactsProvider break; default: // Copy the original value. - switch (original.getType(i)) { + switch (original.getType(originalColumnIndex)) { case Cursor.FIELD_TYPE_NULL: builder.add(null); break; case Cursor.FIELD_TYPE_INTEGER: - builder.add(original.getLong(i)); + builder.add(original.getLong(originalColumnIndex)); break; case Cursor.FIELD_TYPE_FLOAT: - builder.add(original.getFloat(i)); + builder.add(original.getFloat(originalColumnIndex)); break; case Cursor.FIELD_TYPE_STRING: - builder.add(original.getString(i)); + builder.add(original.getString(originalColumnIndex)); break; case Cursor.FIELD_TYPE_BLOB: - builder.add(original.getBlob(i)); + builder.add(original.getBlob(originalColumnIndex)); break; } } diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java index 94997c6..b0fb3e9 100644 --- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java +++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java @@ -1940,7 +1940,7 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { }); // First, convert and make sure it returns an empty cursor. - Cursor rewritten = ContactsProvider2.rewriteCorpPhoneLookup(c); + Cursor rewritten = ContactsProvider2.rewriteCorpPhoneLookup(c.getColumnNames(), c); assertEquals(0, rewritten.getCount()); assertEquals(19, rewritten.getColumnCount()); @@ -1987,8 +1987,9 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { "label", // PhoneLookup.LABEL, "+1234", // PhoneLookup.NORMALIZED_NUMBER }); - rewritten = ContactsProvider2.rewriteCorpPhoneLookup(c); + rewritten = ContactsProvider2.rewriteCorpPhoneLookup(c.getColumnNames(), c); assertEquals(2, rewritten.getCount()); + assertEquals(19, rewritten.getColumnCount()); rewritten.moveToPosition(0); int column = 0; @@ -2036,6 +2037,25 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { assertEquals(1, rewritten.getInt(column++)); assertEquals("label", rewritten.getString(column++)); assertEquals("+1234", rewritten.getString(column++)); + + // Use a narower projection. + rewritten = ContactsProvider2.rewriteCorpPhoneLookup( + new String[] {PhoneLookup.PHOTO_URI, PhoneLookup.PHOTO_THUMBNAIL_URI}, c); + assertEquals(2, rewritten.getCount()); + assertEquals(2, rewritten.getColumnCount()); + + rewritten.moveToPosition(0); + column = 0; + assertEquals(null, rewritten.getString(column++)); + assertEquals(null, rewritten.getString(column++)); + + + rewritten.moveToNext(); + column = 0; + assertEquals("content://com.android.contacts/contacts_corp/10/display_photo", + rewritten.getString(column++)); + assertEquals("content://com.android.contacts/contacts_corp/10/photo", + rewritten.getString(column++)); } public void testPhoneUpdate() { |