diff options
author | zachh <zachh@google.com> | 2017-12-20 10:57:38 -0800 |
---|---|---|
committer | Eric Erfanian <erfanian@google.com> | 2017-12-22 08:53:44 -0800 |
commit | 0fab10eb28ab1e246f572302ad0e12508a196b34 (patch) | |
tree | 6fb4d96f0c6e2d9e31ab92fb7a3da5d50384a26d /java | |
parent | 48609d015f06e262796b7ce8a088243199940a8a (diff) |
Added copySubMessage method to PhoneLookup interface.
The existing way that protos are merged in CompositePhoneLookup is not correct because foo_submessage from BarDataSource may incorrectly contribute old information to the merged message.
The new copySubMessage method makes it so that each PhoneLookup is responsible for defining which submessage it is responsible for and prevents the problem.
Test: unit
PiperOrigin-RevId: 179707015
Change-Id: I566305cf64c46c698f14812d9241d166ac75a6d3
Diffstat (limited to 'java')
4 files changed, 44 insertions, 2 deletions
diff --git a/java/com/android/dialer/function/BiConsumer.java b/java/com/android/dialer/function/BiConsumer.java new file mode 100644 index 000000000..50052210e --- /dev/null +++ b/java/com/android/dialer/function/BiConsumer.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.function; + +/** Functional interface for consuming two generic values. */ +public interface BiConsumer<T, U> { + + /** Consumes a value. */ + void accept(T t, U u); +} diff --git a/java/com/android/dialer/phonelookup/PhoneLookup.java b/java/com/android/dialer/phonelookup/PhoneLookup.java index eeab4dadd..bb14c1ff6 100644 --- a/java/com/android/dialer/phonelookup/PhoneLookup.java +++ b/java/com/android/dialer/phonelookup/PhoneLookup.java @@ -62,6 +62,12 @@ public interface PhoneLookup { ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap); /** + * Populates the sub-message that this {@link PhoneLookup} is responsible for by copying the + * sub-message value from {@code source} to {@code destination} + */ + void copySubMessage(PhoneLookupInfo.Builder destination, PhoneLookupInfo source); + + /** * Called when the results of the {@link #getMostRecentPhoneLookupInfo(ImmutableMap)} have been * applied by the caller. * diff --git a/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java b/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java index ee2244615..bb7856fff 100644 --- a/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java +++ b/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java @@ -112,14 +112,15 @@ public final class CompositePhoneLookup implements PhoneLookup { ImmutableMap.builder(); for (DialerPhoneNumber dialerPhoneNumber : existingInfoMap.keySet()) { PhoneLookupInfo.Builder combinedInfo = PhoneLookupInfo.newBuilder(); - for (ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> map : allMaps) { + for (int i = 0; i < allMaps.size(); i++) { + ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> map = allMaps.get(i); PhoneLookupInfo subInfo = map.get(dialerPhoneNumber); if (subInfo == null) { throw new IllegalStateException( "A sublookup didn't return an info for number: " + LogUtil.sanitizePhoneNumber(dialerPhoneNumber.getRawInput().getNumber())); } - combinedInfo.mergeFrom(subInfo); + phoneLookups.get(i).copySubMessage(combinedInfo, subInfo); } combinedMap.put(dialerPhoneNumber, combinedInfo.build()); } @@ -129,6 +130,11 @@ public final class CompositePhoneLookup implements PhoneLookup { } @Override + public void copySubMessage(PhoneLookupInfo.Builder destination, PhoneLookupInfo source) { + throw new UnsupportedOperationException(); + } + + @Override public ListenableFuture<Void> onSuccessfulBulkUpdate() { List<ListenableFuture<Void>> futures = new ArrayList<>(); for (PhoneLookup phoneLookup : phoneLookups) { diff --git a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java index b31d0e72e..4261b763a 100644 --- a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java +++ b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java @@ -131,6 +131,7 @@ public final class Cp2PhoneLookup implements PhoneLookup { private boolean isDirtyInternal(ImmutableSet<DialerPhoneNumber> phoneNumbers) { long lastModified = sharedPreferences.getLong(PREF_LAST_TIMESTAMP_PROCESSED, 0L); + // TODO(zachh): If a number got disassociated with a contact; the contactIds will be empty. return contactsUpdated(queryPhoneTableForContactIds(phoneNumbers), lastModified) || contactsDeleted(lastModified); } @@ -253,6 +254,11 @@ public final class Cp2PhoneLookup implements PhoneLookup { () -> getMostRecentPhoneLookupInfoInternal(existingInfoMap)); } + @Override + public void copySubMessage(PhoneLookupInfo.Builder destination, PhoneLookupInfo source) { + destination.setCp2Info(source.getCp2Info()); + } + private ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> getMostRecentPhoneLookupInfoInternal( ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap) { currentLastTimestampProcessed = null; |