From e1ec439563705d47f699a565c35eff4bc0dcbf61 Mon Sep 17 00:00:00 2001 From: zachh Date: Fri, 22 Dec 2017 16:59:32 -0800 Subject: Parameterized PhoneLookup with submessage type. This allows indvidual PhoneLookups to define and deal mostly with their own submessage type (with the exception of trivial setter and getter methods for converting from/to PhoneLookupInfo). This also simplifies the FakePhoneLookup and tests which use it a bit, I think. Bug: 34672501 Test: unit PiperOrigin-RevId: 179976215 Change-Id: I2db1fc85771621be2f2afcd6af114d82680e30d0 --- .../dialer/phonelookup/cp2/Cp2PhoneLookup.java | 77 +++++++++++----------- 1 file changed, 38 insertions(+), 39 deletions(-) (limited to 'java/com/android/dialer/phonelookup/cp2') diff --git a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java index a477e035c..60c934ace 100644 --- a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java +++ b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java @@ -53,7 +53,7 @@ import java.util.Set; import javax.inject.Inject; /** PhoneLookup implementation for local contacts. */ -public final class Cp2PhoneLookup implements PhoneLookup { +public final class Cp2PhoneLookup implements PhoneLookup { private static final String PREF_LAST_TIMESTAMP_PROCESSED = "cp2PhoneLookupLastTimestampProcessed"; @@ -98,14 +98,14 @@ public final class Cp2PhoneLookup implements PhoneLookup { } @Override - public ListenableFuture lookup(Call call) { + public ListenableFuture lookup(Call call) { return backgroundExecutorService.submit(() -> lookupInternal(call)); } - private PhoneLookupInfo lookupInternal(Call call) { + private Cp2Info lookupInternal(Call call) { String rawNumber = TelecomCallUtil.getNumber(call); if (TextUtils.isEmpty(rawNumber)) { - return PhoneLookupInfo.getDefaultInstance(); + return Cp2Info.getDefaultInstance(); } Optional e164 = TelecomCallUtil.getE164Number(appContext, call); Set cp2ContactInfos = new ArraySet<>(); @@ -115,15 +115,13 @@ public final class Cp2PhoneLookup implements PhoneLookup { : queryPhoneTableBasedOnRawNumber(CP2_INFO_PROJECTION, ImmutableSet.of(rawNumber))) { if (cursor == null) { LogUtil.w("Cp2PhoneLookup.lookupInternal", "null cursor"); - return PhoneLookupInfo.getDefaultInstance(); + return Cp2Info.getDefaultInstance(); } while (cursor.moveToNext()) { cp2ContactInfos.add(buildCp2ContactInfoFromPhoneCursor(appContext, cursor)); } } - return PhoneLookupInfo.newBuilder() - .setCp2Info(Cp2Info.newBuilder().addAllCp2ContactInfo(cp2ContactInfos)) - .build(); + return Cp2Info.newBuilder().addAllCp2ContactInfo(cp2ContactInfos).build(); } @Override @@ -323,20 +321,23 @@ public final class Cp2PhoneLookup implements PhoneLookup { } @Override - public ListenableFuture> - getMostRecentPhoneLookupInfo( - ImmutableMap existingInfoMap) { - return backgroundExecutorService.submit( - () -> getMostRecentPhoneLookupInfoInternal(existingInfoMap)); + public ListenableFuture> getMostRecentInfo( + ImmutableMap existingInfoMap) { + return backgroundExecutorService.submit(() -> getMostRecentInfoInternal(existingInfoMap)); + } + + @Override + public void setSubMessage(PhoneLookupInfo.Builder destination, Cp2Info subMessage) { + destination.setCp2Info(subMessage); } @Override - public void copySubMessage(PhoneLookupInfo.Builder destination, PhoneLookupInfo source) { - destination.setCp2Info(source.getCp2Info()); + public Cp2Info getSubMessage(PhoneLookupInfo phoneLookupInfo) { + return phoneLookupInfo.getCp2Info(); } - private ImmutableMap getMostRecentPhoneLookupInfoInternal( - ImmutableMap existingInfoMap) { + private ImmutableMap getMostRecentInfoInternal( + ImmutableMap existingInfoMap) { currentLastTimestampProcessed = null; long lastModified = sharedPreferences.getLong(PREF_LAST_TIMESTAMP_PROCESSED, 0L); @@ -352,25 +353,23 @@ public final class Cp2PhoneLookup implements PhoneLookup { buildMapForUpdatedOrAddedContacts(existingInfoMap, lastModified, deletedPhoneNumbers); // Start build a new map of updated info. This will replace existing info. - ImmutableMap.Builder newInfoMapBuilder = - ImmutableMap.builder(); + ImmutableMap.Builder newInfoMapBuilder = ImmutableMap.builder(); // For each DialerPhoneNumber in existing info... - for (Entry entry : existingInfoMap.entrySet()) { + for (Entry entry : existingInfoMap.entrySet()) { DialerPhoneNumber dialerPhoneNumber = entry.getKey(); - PhoneLookupInfo existingInfo = entry.getValue(); + Cp2Info existingInfo = entry.getValue(); // Build off the existing info - PhoneLookupInfo.Builder infoBuilder = PhoneLookupInfo.newBuilder(existingInfo); + Cp2Info.Builder infoBuilder = Cp2Info.newBuilder(existingInfo); // If the contact was updated, replace the Cp2ContactInfo list if (updatedContacts.containsKey(dialerPhoneNumber)) { - infoBuilder.setCp2Info( - Cp2Info.newBuilder().addAllCp2ContactInfo(updatedContacts.get(dialerPhoneNumber))); + infoBuilder.clear().addAllCp2ContactInfo(updatedContacts.get(dialerPhoneNumber)); // If it was deleted and not added to a new contact, clear all the CP2 information. } else if (deletedPhoneNumbers.contains(dialerPhoneNumber)) { - infoBuilder.clearCp2Info(); + infoBuilder.clear(); } // If the DialerPhoneNumber didn't change, add the unchanged existing info. @@ -399,11 +398,11 @@ public final class Cp2PhoneLookup implements PhoneLookup { * the smallest set of dialer phone numbers to query cp2 against. 4. build and return the map of * dialerphonenumbers to their new Cp2ContactInfo * - * @return Map of {@link DialerPhoneNumber} to {@link PhoneLookupInfo} with updated {@link + * @return Map of {@link DialerPhoneNumber} to {@link Cp2Info} with updated {@link * Cp2ContactInfo}. */ private Map> buildMapForUpdatedOrAddedContacts( - ImmutableMap existingInfoMap, + ImmutableMap existingInfoMap, long lastModified, Set deletedPhoneNumbers) { @@ -411,9 +410,9 @@ public final class Cp2PhoneLookup implements PhoneLookup { Set updatedNumbers = new ArraySet<>(); Set contactIds = new ArraySet<>(); - for (Entry entry : existingInfoMap.entrySet()) { + for (Entry entry : existingInfoMap.entrySet()) { DialerPhoneNumber dialerPhoneNumber = entry.getKey(); - PhoneLookupInfo existingInfo = entry.getValue(); + Cp2Info existingInfo = entry.getValue(); // If the number was deleted, we need to check if it was added to a new contact. if (deletedPhoneNumbers.contains(dialerPhoneNumber)) { @@ -423,13 +422,13 @@ public final class Cp2PhoneLookup implements PhoneLookup { /// When the PhoneLookupHistory contains no information for a number, because for example the // user just upgraded to the new UI, or cleared data, we need to check for updated info. - if (existingInfo.getCp2Info().getCp2ContactInfoCount() == 0) { + if (existingInfo.getCp2ContactInfoCount() == 0) { updatedNumbers.add(dialerPhoneNumber); } else { // For each Cp2ContactInfo for each existing DialerPhoneNumber... // Store the contact id if it exist, else automatically add the DialerPhoneNumber to our // set of DialerPhoneNumbers we want to update. - for (Cp2ContactInfo cp2ContactInfo : existingInfo.getCp2Info().getCp2ContactInfoList()) { + for (Cp2ContactInfo cp2ContactInfo : existingInfo.getCp2ContactInfoList()) { long existingContactId = cp2ContactInfo.getContactId(); if (existingContactId == 0) { // If the number doesn't have a contact id, for various reasons, we need to look up the @@ -609,7 +608,7 @@ public final class Cp2PhoneLookup implements PhoneLookup { /** Returns set of DialerPhoneNumbers that were associated with now deleted contacts. */ private Set getDeletedPhoneNumbers( - ImmutableMap existingInfoMap, long lastModified) { + ImmutableMap existingInfoMap, long lastModified) { // Build set of all contact IDs from our existing data. We're going to use this set to query // against the DeletedContacts table and see if any of them were deleted. Set contactIds = findContactIdsIn(existingInfoMap); @@ -621,10 +620,10 @@ public final class Cp2PhoneLookup implements PhoneLookup { } } - private Set findContactIdsIn(ImmutableMap map) { + private Set findContactIdsIn(ImmutableMap map) { Set contactIds = new ArraySet<>(); - for (PhoneLookupInfo info : map.values()) { - for (Cp2ContactInfo cp2ContactInfo : info.getCp2Info().getCp2ContactInfoList()) { + for (Cp2Info info : map.values()) { + for (Cp2ContactInfo cp2ContactInfo : info.getCp2ContactInfoList()) { contactIds.add(cp2ContactInfo.getContactId()); } } @@ -659,7 +658,7 @@ public final class Cp2PhoneLookup implements PhoneLookup { /** Returns set of DialerPhoneNumbers that are associated with deleted contact IDs. */ private Set findDeletedPhoneNumbersIn( - ImmutableMap existingInfoMap, Cursor cursor) { + ImmutableMap existingInfoMap, Cursor cursor) { int contactIdIndex = cursor.getColumnIndexOrThrow(DeletedContacts.CONTACT_ID); int deletedTimeIndex = cursor.getColumnIndexOrThrow(DeletedContacts.CONTACT_DELETED_TIMESTAMP); Set deletedPhoneNumbers = new ArraySet<>(); @@ -678,10 +677,10 @@ public final class Cp2PhoneLookup implements PhoneLookup { } private static Set findDialerPhoneNumbersContainingContactId( - ImmutableMap existingInfoMap, long contactId) { + ImmutableMap existingInfoMap, long contactId) { Set matches = new ArraySet<>(); - for (Entry entry : existingInfoMap.entrySet()) { - for (Cp2ContactInfo cp2ContactInfo : entry.getValue().getCp2Info().getCp2ContactInfoList()) { + for (Entry entry : existingInfoMap.entrySet()) { + for (Cp2ContactInfo cp2ContactInfo : entry.getValue().getCp2ContactInfoList()) { if (cp2ContactInfo.getContactId() == contactId) { matches.add(entry.getKey()); } -- cgit v1.2.3