1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 package com.android.incallui;
17 
18 import android.content.ContentValues;
19 import android.content.Context;
20 import android.support.annotation.Nullable;
21 import android.telecom.Call;
22 import com.android.dialer.calllog.config.CallLogConfigComponent;
23 import com.android.dialer.common.Assert;
24 import com.android.dialer.common.LogUtil;
25 import com.android.dialer.common.concurrent.DialerExecutorComponent;
26 import com.android.dialer.phonelookup.PhoneLookupComponent;
27 import com.android.dialer.phonelookup.PhoneLookupInfo;
28 import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract.PhoneLookupHistory;
29 import com.android.dialer.telecom.TelecomCallUtil;
30 import com.google.common.base.Optional;
31 import com.google.common.util.concurrent.FutureCallback;
32 import com.google.common.util.concurrent.Futures;
33 import com.google.common.util.concurrent.ListenableFuture;
34 
35 /**
36  * Fetches the current {@link PhoneLookupInfo} for the provided call and writes it to the
37  * PhoneLookupHistory.
38  */
39 final class PhoneLookupHistoryRecorder {
40 
41   /**
42    * If the call log framework is enabled, fetches the current {@link PhoneLookupInfo} for the
43    * provided call and writes it to the PhoneLookupHistory. Otherwise does nothing.
44    */
recordPhoneLookupInfo(Context appContext, Call call)45   static void recordPhoneLookupInfo(Context appContext, Call call) {
46     if (!CallLogConfigComponent.get(appContext).callLogConfig().isCallLogFrameworkEnabled()) {
47       return;
48     }
49 
50     ListenableFuture<PhoneLookupInfo> infoFuture =
51         PhoneLookupComponent.get(appContext).compositePhoneLookup().lookup(call);
52 
53     Futures.addCallback(
54         infoFuture,
55         new FutureCallback<PhoneLookupInfo>() {
56           @Override
57           public void onSuccess(@Nullable PhoneLookupInfo result) {
58             Assert.checkArgument(result != null);
59             Optional<String> normalizedNumber =
60                 TelecomCallUtil.getNormalizedNumber(appContext, call);
61             if (!normalizedNumber.isPresent()) {
62               LogUtil.w("PhoneLookupHistoryRecorder.onSuccess", "couldn't get a number");
63               return;
64             }
65             ContentValues contentValues = new ContentValues();
66             contentValues.put(PhoneLookupHistory.PHONE_LOOKUP_INFO, result.toByteArray());
67             contentValues.put(PhoneLookupHistory.LAST_MODIFIED, System.currentTimeMillis());
68             appContext
69                 .getContentResolver()
70                 .update(
71                     PhoneLookupHistory.contentUriForNumber(normalizedNumber.get()),
72                     contentValues,
73                     null,
74                     null);
75           }
76 
77           @Override
78           public void onFailure(Throwable t) {
79             // TODO(zachh): Consider how to best handle this; take measures to repair call log?
80             LogUtil.w(
81                 "PhoneLookupHistoryRecorder.onFailure", "could not write PhoneLookupHistory", t);
82           }
83         },
84         DialerExecutorComponent.get(appContext).backgroundExecutor());
85   }
86 }
87