diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-10-26 18:11:22 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-10-26 18:11:22 +0000 |
commit | 9b99b22391a92fe20a74076bf5e47d36eb9b4ab4 (patch) | |
tree | 510aa70e1d1421be75f8f5cea57a6fce51427cbc /java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java | |
parent | e5041c35b4c0ef31e5d242a8dd61d7441d490f44 (diff) | |
parent | 65c21801bf47cf24e2755445545bedabc0f393bf (diff) |
Merge changes I1fa267c0,Ia6812794,Ib5715d0f,I71d4c03b
* changes:
Cp2 Phonelookup isDirty implementation.
App latency test migration. Migrate the existing app latency test to a new solution from Project Fi team. The main change on this new solution is that the data collection part is done by Primes now.
Search fragment now closes after placing a call.
Display CDMA voice privacy icon on notification bar.
Diffstat (limited to 'java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java')
-rw-r--r-- | java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java | 125 |
1 files changed, 121 insertions, 4 deletions
diff --git a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java index 323ec7c65..a3d97c64e 100644 --- a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java +++ b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java @@ -16,21 +16,138 @@ package com.android.dialer.phonelookup.cp2; +import android.content.Context; +import android.database.Cursor; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.DeletedContacts; +import android.support.v4.util.ArraySet; import com.android.dialer.DialerPhoneNumber; +import com.android.dialer.common.concurrent.DialerExecutors; import com.android.dialer.phonelookup.PhoneLookup; import com.android.dialer.phonelookup.PhoneLookupInfo; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; +import java.util.Set; -/** TODO(calderwoodra) */ -final class Cp2PhoneLookup implements PhoneLookup { +/** PhoneLookup implementation for local contacts. */ +public final class Cp2PhoneLookup implements PhoneLookup { + + private final Context context; + + Cp2PhoneLookup(Context context) { + this.context = context; + } @Override public ListenableFuture<Boolean> isDirty( ImmutableSet<DialerPhoneNumber> phoneNumbers, long lastModified) { - // TODO(calderwoodra) - return null; + // TODO(calderwoodra): consider a different thread pool + return MoreExecutors.listeningDecorator(DialerExecutors.getLowPriorityThreadPool(context)) + .submit(() -> isDirtyInternal(phoneNumbers, lastModified)); + } + + private boolean isDirtyInternal(ImmutableSet<DialerPhoneNumber> phoneNumbers, long lastModified) { + return contactsUpdated(getContactIdsFromPhoneNumbers(phoneNumbers), lastModified) + || contactsDeleted(lastModified); + } + + /** Returns set of contact ids that correspond to {@code phoneNumbers} if the contact exists. */ + private Set<Long> getContactIdsFromPhoneNumbers(ImmutableSet<DialerPhoneNumber> phoneNumbers) { + Set<Long> contactIds = new ArraySet<>(); + try (Cursor cursor = + context + .getContentResolver() + .query( + Phone.CONTENT_URI, + new String[] {Phone.CONTACT_ID}, + columnInSetWhereStatement(Phone.NORMALIZED_NUMBER, phoneNumbers.size()), + contactIdsSelectionArgs(phoneNumbers), + null)) { + cursor.moveToPosition(-1); + while (cursor.moveToNext()) { + contactIds.add(cursor.getLong(0 /* columnIndex */)); + } + } + return contactIds; + } + + private static String[] contactIdsSelectionArgs(ImmutableSet<DialerPhoneNumber> phoneNumbers) { + String[] args = new String[phoneNumbers.size()]; + int i = 0; + for (DialerPhoneNumber phoneNumber : phoneNumbers) { + args[i++] = getNormalizedNumber(phoneNumber); + } + return args; + } + + private static String getNormalizedNumber(DialerPhoneNumber phoneNumber) { + // TODO(calderwoodra): implement normalization logic that matches contacts. + return phoneNumber.getRawInput().getNumber(); + } + + /** Returns true if any contacts were modified after {@code lastModified}. */ + private boolean contactsUpdated(Set<Long> contactIds, long lastModified) { + try (Cursor cursor = + context + .getContentResolver() + .query( + Contacts.CONTENT_URI, + new String[] {Contacts._ID}, + contactsIsDirtyWhereStatement(contactIds.size()), + contactsIsDirtySelectionArgs(lastModified, contactIds), + null)) { + return cursor.getCount() > 0; + } + } + + private static String contactsIsDirtyWhereStatement(int numberOfContactIds) { + StringBuilder where = new StringBuilder(); + // Filter to after last modified time + where.append(Contacts.CONTACT_LAST_UPDATED_TIMESTAMP).append(" > ?"); + + // Filter based only on contacts we care about + where.append(" AND ").append(columnInSetWhereStatement(Contacts._ID, numberOfContactIds)); + return where.toString(); + } + + private String[] contactsIsDirtySelectionArgs(long lastModified, Set<Long> contactIds) { + String[] args = new String[contactIds.size() + 1]; + args[0] = Long.toString(lastModified); + int i = 1; + for (Long contactId : contactIds) { + args[i++] = Long.toString(contactId); + } + return args; + } + + /** Returns true if any contacts were deleted after {@code lastModified}. */ + private boolean contactsDeleted(long lastModified) { + try (Cursor cursor = + context + .getContentResolver() + .query( + DeletedContacts.CONTENT_URI, + new String[] {DeletedContacts.CONTACT_DELETED_TIMESTAMP}, + DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?", + new String[] {Long.toString(lastModified)}, + null)) { + return cursor.getCount() > 0; + } + } + + private static String columnInSetWhereStatement(String columnName, int setSize) { + StringBuilder where = new StringBuilder(); + where.append(columnName).append(" IN ("); + for (int i = 0; i < setSize; i++) { + if (i != 0) { + where.append(", "); + } + where.append("?"); + } + return where.append(")").toString(); } @Override |