summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/calllog/ui/RealtimeRowProcessor.java
blob: c5148d93eb8b93a6175de751eb26d1ab013e1371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (C) 2018 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.ui;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.support.annotation.MainThread;
import android.support.annotation.VisibleForTesting;
import android.util.ArrayMap;
import com.android.dialer.DialerPhoneNumber;
import com.android.dialer.calllog.model.CoalescedRow;
import com.android.dialer.calllogutils.NumberAttributesConverter;
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.Ui;
import com.android.dialer.common.concurrent.ThreadUtil;
import com.android.dialer.inject.ApplicationContext;
import com.android.dialer.phonelookup.PhoneLookupInfo;
import com.android.dialer.phonelookup.composite.CompositePhoneLookup;
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.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;

/**
 * Does work necessary to update a {@link CoalescedRow} when it is requested to be displayed.
 *
 * <p>In most cases this is a no-op as most AnnotatedCallLog rows can be displayed immediately
 * as-is. However, there are certain times that a row from the AnnotatedCallLog cannot be displayed
 * without further work being performed.
 *
 * <p>For example, when there are many invalid numbers in the call log, we cannot efficiently update
 * the CP2 information for all of them at once, and so information for those rows must be retrieved
 * at display time.
 *
 * <p>This class also updates {@link PhoneLookupHistory} with the results that it fetches.
 */
public final class RealtimeRowProcessor {

  /*
   * The time to wait between writing batches of records to PhoneLookupHistory.
   */
  @VisibleForTesting static final long BATCH_WAIT_MILLIS = TimeUnit.SECONDS.toMillis(3);

  private final Context appContext;
  private final CompositePhoneLookup compositePhoneLookup;
  private final ListeningExecutorService uiExecutor;
  private final ListeningExecutorService backgroundExecutor;

  private final Map<DialerPhoneNumber, PhoneLookupInfo> cache = new ArrayMap<>();

  private final Map<DialerPhoneNumber, PhoneLookupInfo> queuedPhoneLookupHistoryWrites =
      new LinkedHashMap<>(); // Keep the order so the most recent looked up value always wins
  private final Runnable writePhoneLookupHistoryRunnable = this::writePhoneLookupHistory;

  @Inject
  RealtimeRowProcessor(
      @ApplicationContext Context appContext,
      @Ui ListeningExecutorService uiExecutor,
      @BackgroundExecutor ListeningExecutorService backgroundExecutor,
      CompositePhoneLookup compositePhoneLookup) {
    this.appContext = appContext;
    this.uiExecutor = uiExecutor;
    this.backgroundExecutor = backgroundExecutor;
    this.compositePhoneLookup = compositePhoneLookup;
  }

  /**
   * Converts a {@link CoalescedRow} to a future which is the result of performing additional work
   * on the row. May simply return the original row if no modifications were necessary.
   */
  @MainThread
  ListenableFuture<CoalescedRow> applyRealtimeProcessing(final CoalescedRow row) {
    // Cp2DefaultDirectoryPhoneLookup can not always efficiently process all rows.
    if (!row.getNumberAttributes().getIsCp2InfoIncomplete()) {
      return Futures.immediateFuture(row);
    }

    PhoneLookupInfo cachedPhoneLookupInfo = cache.get(row.getNumber());
    if (cachedPhoneLookupInfo != null) {
      return Futures.immediateFuture(applyPhoneLookupInfoToRow(cachedPhoneLookupInfo, row));
    }

    ListenableFuture<PhoneLookupInfo> phoneLookupInfoFuture =
        compositePhoneLookup.lookup(row.getNumber());
    return Futures.transform(
        phoneLookupInfoFuture,
        phoneLookupInfo -> {
          queuePhoneLookupHistoryWrite(row.getNumber(), phoneLookupInfo);
          cache.put(row.getNumber(), phoneLookupInfo);
          return applyPhoneLookupInfoToRow(phoneLookupInfo, row);
        },
        uiExecutor /* ensures the cache is updated on a single thread */);
  }

  /** Clears the internal cache. */
  @MainThread
  public void clearCache() {
    Assert.isMainThread();
    cache.clear();
  }

  @MainThread
  private void queuePhoneLookupHistoryWrite(
      DialerPhoneNumber dialerPhoneNumber, PhoneLookupInfo phoneLookupInfo) {
    Assert.isMainThread();
    queuedPhoneLookupHistoryWrites.put(dialerPhoneNumber, phoneLookupInfo);
    ThreadUtil.getUiThreadHandler().removeCallbacks(writePhoneLookupHistoryRunnable);
    ThreadUtil.getUiThreadHandler().postDelayed(writePhoneLookupHistoryRunnable, BATCH_WAIT_MILLIS);
  }

  @MainThread
  private void writePhoneLookupHistory() {
    Assert.isMainThread();

    // Copy the batch to a new collection that be safely processed on a background thread.
    ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> currentBatch =
        ImmutableMap.copyOf(queuedPhoneLookupHistoryWrites);

    // Clear the queue, handing responsibility for its items to the background task.
    queuedPhoneLookupHistoryWrites.clear();

    // Returns the number of rows updated.
    ListenableFuture<Integer> applyBatchFuture =
        backgroundExecutor.submit(
            () -> {
              ArrayList<ContentProviderOperation> operations = new ArrayList<>();
              long currentTimestamp = System.currentTimeMillis();
              for (Entry<DialerPhoneNumber, PhoneLookupInfo> entry : currentBatch.entrySet()) {
                DialerPhoneNumber dialerPhoneNumber = entry.getKey();
                PhoneLookupInfo phoneLookupInfo = entry.getValue();

                // Note: Multiple DialerPhoneNumbers can map to the same normalized number but we
                // just write them all and the value for the last one will arbitrarily win.
                // Note: This loses country info when number is not valid.
                String normalizedNumber = dialerPhoneNumber.getNormalizedNumber();

                ContentValues contentValues = new ContentValues();
                contentValues.put(
                    PhoneLookupHistory.PHONE_LOOKUP_INFO, phoneLookupInfo.toByteArray());
                contentValues.put(PhoneLookupHistory.LAST_MODIFIED, currentTimestamp);
                operations.add(
                    ContentProviderOperation.newUpdate(
                            PhoneLookupHistory.contentUriForNumber(normalizedNumber))
                        .withValues(contentValues)
                        .build());
              }
              return Assert.isNotNull(
                      appContext
                          .getContentResolver()
                          .applyBatch(PhoneLookupHistoryContract.AUTHORITY, operations))
                  .length;
            });

    Futures.addCallback(
        applyBatchFuture,
        new FutureCallback<Integer>() {
          @Override
          public void onSuccess(Integer rowsAffected) {
            LogUtil.i(
                "RealtimeRowProcessor.onSuccess",
                "wrote %d rows to PhoneLookupHistory",
                rowsAffected);
          }

          @Override
          public void onFailure(Throwable throwable) {
            throw new RuntimeException(throwable);
          }
        },
        uiExecutor);
  }

  private CoalescedRow applyPhoneLookupInfoToRow(
      PhoneLookupInfo phoneLookupInfo, CoalescedRow row) {
    // Force the "cp2_info_incomplete" value to the original value so that it is not used when
    // comparing the original row to the updated row.
    // TODO(linyuh): Improve the comparison instead.
    return row.toBuilder()
        .setNumberAttributes(
            NumberAttributesConverter.fromPhoneLookupInfo(phoneLookupInfo)
                .setIsCp2InfoIncomplete(row.getNumberAttributes().getIsCp2InfoIncomplete())
                .build())
        .build();
  }
}