/*
* 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.net.Uri;
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.common.LogUtil;
import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.common.concurrent.Annotations.LightweightExecutor;
import com.android.dialer.phonelookup.PhoneLookup;
import com.android.dialer.phonelookup.PhoneLookupInfo;
import com.android.dialer.phonelookup.PhoneLookupSelector;
import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract;
import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract.PhoneLookupHistory;
import com.android.dialer.phonenumberproto.DialerPhoneNumberUtil;
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.i18n.phonenumbers.PhoneNumberUtil;
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 PhoneLookup phoneLookup;
private final ListeningExecutorService backgroundExecutorService;
private final ListeningExecutorService lightweightExecutorService;
/**
* Keyed by normalized number (the primary key for PhoneLookupHistory).
*
*
This is state saved between the {@link #fill(Context, CallLogMutations)} and {@link
* #onSuccessfulFill(Context)} 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 #fill(Context, CallLogMutations)} and {@link
* #onSuccessfulFill(Context)} operations.
*/
private final Set phoneLookupHistoryRowsToDelete = new ArraySet<>();
@Inject
PhoneLookupDataSource(
PhoneLookup phoneLookup,
@BackgroundExecutor ListeningExecutorService backgroundExecutorService,
@LightweightExecutor ListeningExecutorService lightweightExecutorService) {
this.phoneLookup = phoneLookup;
this.backgroundExecutorService = backgroundExecutorService;
this.lightweightExecutorService = lightweightExecutorService;
}
@Override
public ListenableFuture isDirty(Context appContext) {
ListenableFuture> phoneNumbers =
backgroundExecutorService.submit(
() -> queryDistinctDialerPhoneNumbersFromAnnotatedCallLog(appContext));
return Futures.transformAsync(phoneNumbers, phoneLookup::isDirty, lightweightExecutorService);
}
/**
* {@inheritDoc}
*
* This method uses the following algorithm:
*
*
* - Selects the distinct DialerPhoneNumbers from the AnnotatedCallLog
*
- 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#getMostRecentPhoneLookupInfo(ImmutableMap)}
*
- Looks through the results of getMostRecentPhoneLookupInfo
*
* - 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(Context appContext, CallLogMutations mutations) {
// 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.
ListenableFuture