/*
* 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.calllog.datasources.phonelookup;
import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.RemoteException;
import android.support.annotation.MainThread;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import com.android.dialer.DialerPhoneNumber;
import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog;
import com.android.dialer.calllog.datasources.CallLogDataSource;
import com.android.dialer.calllog.datasources.CallLogMutations;
import com.android.dialer.calllog.datasources.util.RowCombiner;
import com.android.dialer.calllogutils.NumberAttributesBuilder;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.common.concurrent.Annotations.LightweightExecutor;
import com.android.dialer.inject.ApplicationContext;
import com.android.dialer.phonelookup.PhoneLookup;
import com.android.dialer.phonelookup.PhoneLookupInfo;
import com.android.dialer.phonelookup.composite.CompositePhoneLookup;
import com.android.dialer.phonelookup.database.PhoneLookupHistoryDatabaseHelper;
import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract;
import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract.PhoneLookupHistory;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.inject.Inject;
/**
* Responsible for maintaining the columns in the annotated call log which are derived from phone
* numbers.
*/
public final class PhoneLookupDataSource implements CallLogDataSource {
private final Context appContext;
private final CompositePhoneLookup compositePhoneLookup;
private final ListeningExecutorService backgroundExecutorService;
private final ListeningExecutorService lightweightExecutorService;
/**
* Keyed by normalized number (the primary key for PhoneLookupHistory).
*
*
This is state saved between the {@link CallLogDataSource#fill(CallLogMutations)} and {@link
* CallLogDataSource#onSuccessfulFill()} operations.
*/
private final Map phoneLookupHistoryRowsToUpdate = new ArrayMap<>();
/**
* Normalized numbers (the primary key for PhoneLookupHistory) which should be deleted from
* PhoneLookupHistory.
*
* This is state saved between the {@link CallLogDataSource#fill(CallLogMutations)} and {@link
* CallLogDataSource#onSuccessfulFill()} operations.
*/
private final Set phoneLookupHistoryRowsToDelete = new ArraySet<>();
private final PhoneLookupHistoryDatabaseHelper phoneLookupHistoryDatabaseHelper;
@Inject
PhoneLookupDataSource(
@ApplicationContext Context appContext,
CompositePhoneLookup compositePhoneLookup,
@BackgroundExecutor ListeningExecutorService backgroundExecutorService,
@LightweightExecutor ListeningExecutorService lightweightExecutorService,
PhoneLookupHistoryDatabaseHelper phoneLookupHistoryDatabaseHelper) {
this.appContext = appContext;
this.compositePhoneLookup = compositePhoneLookup;
this.backgroundExecutorService = backgroundExecutorService;
this.lightweightExecutorService = lightweightExecutorService;
this.phoneLookupHistoryDatabaseHelper = phoneLookupHistoryDatabaseHelper;
}
@Override
public ListenableFuture isDirty() {
ListenableFuture> phoneNumbers =
backgroundExecutorService.submit(
() -> queryDistinctDialerPhoneNumbersFromAnnotatedCallLog(appContext));
return Futures.transformAsync(
phoneNumbers, compositePhoneLookup::isDirty, lightweightExecutorService);
}
/**
* {@inheritDoc}
*
* This method uses the following algorithm:
*
*
* - Finds the phone numbers of interest by taking the union of the distinct
* DialerPhoneNumbers from the AnnotatedCallLog and the pending inserts provided in {@code
* mutations}
*
- Uses them to fetch the current information from PhoneLookupHistory, in order to construct
* a map from DialerPhoneNumber to PhoneLookupInfo
*
* - If no PhoneLookupInfo is found (e.g. app data was cleared?) an empty value is used.
*
* - Looks through the provided set of mutations
*
- For inserts, uses the contents of PhoneLookupHistory to populate the fields of the
* provided mutations. (Note that at this point, data may not be fully up-to-date, but the
* next steps will take care of that.)
*
- Uses all of the numbers from AnnotatedCallLog to invoke (composite) {@link
* PhoneLookup#getMostRecentInfo(ImmutableMap)}
*
- Looks through the results of getMostRecentInfo
*
* - For each number, checks if the original PhoneLookupInfo differs from the new one
*
- If so, it applies the update to the mutations and (in onSuccessfulFill) writes the
* new value back to the PhoneLookupHistory.
*
*
*/
@Override
public ListenableFuture fill(CallLogMutations mutations) {
LogUtil.v(
"PhoneLookupDataSource.fill",
"processing mutations (inserts: %d, updates: %d, deletes: %d)",
mutations.getInserts().size(),
mutations.getUpdates().size(),
mutations.getDeletes().size());
// Clear state saved since the last call to fill. This is necessary in case fill is called but
// onSuccessfulFill is not called during a previous flow.
phoneLookupHistoryRowsToUpdate.clear();
phoneLookupHistoryRowsToDelete.clear();
// First query information from annotated call log (and include pending inserts).
ListenableFuture