summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java
blob: 4261b763ab98276335d5e8c8c5ad0823c09b3c4b (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
 * 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.phonelookup.cp2;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.DeletedContacts;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.ArraySet;
import android.telecom.Call;
import android.text.TextUtils;
import com.android.dialer.DialerPhoneNumber;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.inject.ApplicationContext;
import com.android.dialer.phonelookup.PhoneLookup;
import com.android.dialer.phonelookup.PhoneLookupInfo;
import com.android.dialer.phonelookup.PhoneLookupInfo.Cp2Info;
import com.android.dialer.phonelookup.PhoneLookupInfo.Cp2Info.Cp2ContactInfo;
import com.android.dialer.phonenumberproto.DialerPhoneNumberUtil;
import com.android.dialer.storage.Unencrypted;
import com.android.dialer.telecom.TelecomCallUtil;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject;

/** PhoneLookup implementation for local contacts. */
public final class Cp2PhoneLookup implements PhoneLookup {

  private static final String PREF_LAST_TIMESTAMP_PROCESSED =
      "cp2PhoneLookupLastTimestampProcessed";

  private static final String[] CP2_INFO_PROJECTION =
      new String[] {
        Phone.DISPLAY_NAME_PRIMARY, // 0
        Phone.PHOTO_THUMBNAIL_URI, // 1
        Phone.PHOTO_ID, // 2
        Phone.TYPE, // 3
        Phone.LABEL, // 4
        Phone.NORMALIZED_NUMBER, // 5
        Phone.NUMBER, // 6
        Phone.CONTACT_ID, // 7
        Phone.LOOKUP_KEY // 8
      };

  private static final int CP2_INFO_NAME_INDEX = 0;
  private static final int CP2_INFO_PHOTO_URI_INDEX = 1;
  private static final int CP2_INFO_PHOTO_ID_INDEX = 2;
  private static final int CP2_INFO_TYPE_INDEX = 3;
  private static final int CP2_INFO_LABEL_INDEX = 4;
  private static final int CP2_INFO_NORMALIZED_NUMBER_INDEX = 5;
  private static final int CP2_INFO_NUMBER_INDEX = 6;
  private static final int CP2_INFO_CONTACT_ID_INDEX = 7;
  private static final int CP2_INFO_LOOKUP_KEY_INDEX = 8;

  private final Context appContext;
  private final SharedPreferences sharedPreferences;
  private final ListeningExecutorService backgroundExecutorService;

  @Nullable private Long currentLastTimestampProcessed;

  @Inject
  Cp2PhoneLookup(
      @ApplicationContext Context appContext,
      @Unencrypted SharedPreferences sharedPreferences,
      @BackgroundExecutor ListeningExecutorService backgroundExecutorService) {
    this.appContext = appContext;
    this.sharedPreferences = sharedPreferences;
    this.backgroundExecutorService = backgroundExecutorService;
  }

  @Override
  public ListenableFuture<PhoneLookupInfo> lookup(Call call) {
    return backgroundExecutorService.submit(() -> lookupInternal(call));
  }

  private PhoneLookupInfo lookupInternal(Call call) {
    String rawNumber = TelecomCallUtil.getNumber(call);
    if (TextUtils.isEmpty(rawNumber)) {
      return PhoneLookupInfo.getDefaultInstance();
    }
    Optional<String> e164 = TelecomCallUtil.getE164Number(appContext, call);
    Set<Cp2ContactInfo> cp2ContactInfos = new ArraySet<>();
    try (Cursor cursor =
        e164.isPresent()
            ? queryPhoneTableBasedOnE164(CP2_INFO_PROJECTION, ImmutableSet.of(e164.get()))
            : queryPhoneTableBasedOnRawNumber(CP2_INFO_PROJECTION, ImmutableSet.of(rawNumber))) {
      if (cursor == null) {
        LogUtil.w("Cp2PhoneLookup.lookupInternal", "null cursor");
        return PhoneLookupInfo.getDefaultInstance();
      }
      while (cursor.moveToNext()) {
        cp2ContactInfos.add(buildCp2ContactInfoFromPhoneCursor(appContext, cursor));
      }
    }
    return PhoneLookupInfo.newBuilder()
        .setCp2Info(Cp2Info.newBuilder().addAllCp2ContactInfo(cp2ContactInfos))
        .build();
  }

  @Override
  public ListenableFuture<Boolean> isDirty(ImmutableSet<DialerPhoneNumber> phoneNumbers) {
    return backgroundExecutorService.submit(() -> isDirtyInternal(phoneNumbers));
  }

  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);
  }

  /**
   * Returns set of contact ids that correspond to {@code dialerPhoneNumbers} if the contact exists.
   */
  private Set<Long> queryPhoneTableForContactIds(
      ImmutableSet<DialerPhoneNumber> dialerPhoneNumbers) {
    Set<Long> contactIds = new ArraySet<>();

    PartitionedNumbers partitionedNumbers = new PartitionedNumbers(dialerPhoneNumbers);

    // First use the E164 numbers to query the NORMALIZED_NUMBER column.
    contactIds.addAll(
        queryPhoneTableForContactIdsBasedOnE164(partitionedNumbers.validE164Numbers()));

    // Then run a separate query using the NUMBER column to handle numbers that can't be formatted.
    contactIds.addAll(
        queryPhoneTableForContactIdsBasedOnRawNumber(partitionedNumbers.unformattableNumbers()));

    return contactIds;
  }

  private Set<Long> queryPhoneTableForContactIdsBasedOnE164(Set<String> validE164Numbers) {
    Set<Long> contactIds = new ArraySet<>();
    if (validE164Numbers.isEmpty()) {
      return contactIds;
    }
    try (Cursor cursor =
        queryPhoneTableBasedOnE164(new String[] {Phone.CONTACT_ID}, validE164Numbers)) {
      if (cursor == null) {
        LogUtil.w("Cp2PhoneLookup.queryPhoneTableForContactIdsBasedOnE164", "null cursor");
        return contactIds;
      }
      while (cursor.moveToNext()) {
        contactIds.add(cursor.getLong(0 /* columnIndex */));
      }
    }
    return contactIds;
  }

  private Set<Long> queryPhoneTableForContactIdsBasedOnRawNumber(Set<String> unformattableNumbers) {
    Set<Long> contactIds = new ArraySet<>();
    if (unformattableNumbers.isEmpty()) {
      return contactIds;
    }
    try (Cursor cursor =
        queryPhoneTableBasedOnRawNumber(new String[] {Phone.CONTACT_ID}, unformattableNumbers)) {
      if (cursor == null) {
        LogUtil.w("Cp2PhoneLookup.queryPhoneTableForContactIdsBasedOnE164", "null cursor");
        return contactIds;
      }
      while (cursor.moveToNext()) {
        contactIds.add(cursor.getLong(0 /* columnIndex */));
      }
    }
    return contactIds;
  }

  /** Returns true if any contacts were modified after {@code lastModified}. */
  private boolean contactsUpdated(Set<Long> contactIds, long lastModified) {
    try (Cursor cursor = queryContactsTableForContacts(contactIds, lastModified)) {
      return cursor.getCount() > 0;
    }
  }

  private Cursor queryContactsTableForContacts(Set<Long> contactIds, long lastModified) {
    // Filter to after last modified time based only on contacts we care about
    String where =
        Contacts.CONTACT_LAST_UPDATED_TIMESTAMP
            + " > ?"
            + " AND "
            + Contacts._ID
            + " IN ("
            + questionMarks(contactIds.size())
            + ")";

    String[] args = new String[contactIds.size() + 1];
    args[0] = Long.toString(lastModified);
    int i = 1;
    for (Long contactId : contactIds) {
      args[i++] = Long.toString(contactId);
    }

    return appContext
        .getContentResolver()
        .query(
            Contacts.CONTENT_URI,
            new String[] {Contacts._ID, Contacts.CONTACT_LAST_UPDATED_TIMESTAMP},
            where,
            args,
            null);
  }

  /** Returns true if any contacts were deleted after {@code lastModified}. */
  private boolean contactsDeleted(long lastModified) {
    try (Cursor cursor =
        appContext
            .getContentResolver()
            .query(
                DeletedContacts.CONTENT_URI,
                new String[] {DeletedContacts.CONTACT_DELETED_TIMESTAMP},
                DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?",
                new String[] {Long.toString(lastModified)},
                null)) {
      if (cursor == null) {
        LogUtil.w("Cp2PhoneLookup.contactsDeleted", "null cursor");
        return false;
      }
      return cursor.getCount() > 0;
    }
  }

  @Override
  public ListenableFuture<ImmutableMap<DialerPhoneNumber, PhoneLookupInfo>>
      getMostRecentPhoneLookupInfo(
          ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap) {
    return backgroundExecutorService.submit(
        () -> 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;
    long lastModified = sharedPreferences.getLong(PREF_LAST_TIMESTAMP_PROCESSED, 0L);

    // Build a set of each DialerPhoneNumber that was associated with a contact, and is no longer
    // associated with that same contact.
    Set<DialerPhoneNumber> deletedPhoneNumbers =
        getDeletedPhoneNumbers(existingInfoMap, lastModified);

    // For each DialerPhoneNumber that was associated with a contact or added to a contact,
    // build a map of those DialerPhoneNumbers to a set Cp2ContactInfos, where each Cp2ContactInfo
    // represents a contact.
    Map<DialerPhoneNumber, Set<Cp2ContactInfo>> updatedContacts =
        buildMapForUpdatedOrAddedContacts(existingInfoMap, lastModified, deletedPhoneNumbers);

    // Start build a new map of updated info. This will replace existing info.
    ImmutableMap.Builder<DialerPhoneNumber, PhoneLookupInfo> newInfoMapBuilder =
        ImmutableMap.builder();

    // For each DialerPhoneNumber in existing info...
    for (Entry<DialerPhoneNumber, PhoneLookupInfo> entry : existingInfoMap.entrySet()) {
      DialerPhoneNumber dialerPhoneNumber = entry.getKey();
      PhoneLookupInfo existingInfo = entry.getValue();

      // Build off the existing info
      PhoneLookupInfo.Builder infoBuilder = PhoneLookupInfo.newBuilder(existingInfo);

      // If the contact was updated, replace the Cp2ContactInfo list
      if (updatedContacts.containsKey(dialerPhoneNumber)) {
        infoBuilder.setCp2Info(
            Cp2Info.newBuilder().addAllCp2ContactInfo(updatedContacts.get(dialerPhoneNumber)));

        // If it was deleted and not added to a new contact, clear all the CP2 information.
      } else if (deletedPhoneNumbers.contains(dialerPhoneNumber)) {
        infoBuilder.clearCp2Info();
      }

      // If the DialerPhoneNumber didn't change, add the unchanged existing info.
      newInfoMapBuilder.put(dialerPhoneNumber, infoBuilder.build());
    }
    return newInfoMapBuilder.build();
  }

  @Override
  public ListenableFuture<Void> onSuccessfulBulkUpdate() {
    return backgroundExecutorService.submit(
        () -> {
          if (currentLastTimestampProcessed != null) {
            sharedPreferences
                .edit()
                .putLong(PREF_LAST_TIMESTAMP_PROCESSED, currentLastTimestampProcessed)
                .apply();
          }
          return null;
        });
  }

  /**
   * 1. get all contact ids. if the id is unset, add the number to the list of contacts to look up.
   * 2. reduce our list of contact ids to those that were updated after lastModified. 3. Now we have
   * the smallest set of dialer phone numbers to query cp2 against. 4. build and return the map of
   * dialerphonenumbers to their new Cp2ContactInfo
   *
   * @return Map of {@link DialerPhoneNumber} to {@link PhoneLookupInfo} with updated {@link
   *     Cp2ContactInfo}.
   */
  private Map<DialerPhoneNumber, Set<Cp2ContactInfo>> buildMapForUpdatedOrAddedContacts(
      ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap,
      long lastModified,
      Set<DialerPhoneNumber> deletedPhoneNumbers) {

    // Start building a set of DialerPhoneNumbers that we want to update.
    Set<DialerPhoneNumber> updatedNumbers = new ArraySet<>();

    Set<Long> contactIds = new ArraySet<>();
    for (Entry<DialerPhoneNumber, PhoneLookupInfo> entry : existingInfoMap.entrySet()) {
      DialerPhoneNumber dialerPhoneNumber = entry.getKey();
      PhoneLookupInfo existingInfo = entry.getValue();

      // If the number was deleted, we need to check if it was added to a new contact.
      if (deletedPhoneNumbers.contains(dialerPhoneNumber)) {
        updatedNumbers.add(dialerPhoneNumber);
        continue;
      }

      /// When the PhoneLookupHistory contains no information for a number, because for example the
      // user just upgraded to the new UI, or cleared data, we need to check for updated info.
      if (existingInfo.getCp2Info().getCp2ContactInfoCount() == 0) {
        updatedNumbers.add(dialerPhoneNumber);
      } else {
        // For each Cp2ContactInfo for each existing DialerPhoneNumber...
        // Store the contact id if it exist, else automatically add the DialerPhoneNumber to our
        // set of DialerPhoneNumbers we want to update.
        for (Cp2ContactInfo cp2ContactInfo : existingInfo.getCp2Info().getCp2ContactInfoList()) {
          long existingContactId = cp2ContactInfo.getContactId();
          if (existingContactId == 0) {
            // If the number doesn't have a contact id, for various reasons, we need to look up the
            // number to check if any exists. The various reasons this might happen are:
            //  - An existing contact that wasn't in the call log is now in the call log.
            //  - A number was in the call log before but has now been added to a contact.
            //  - A number is in the call log, but isn't associated with any contact.
            updatedNumbers.add(dialerPhoneNumber);
          } else {
            contactIds.add(cp2ContactInfo.getContactId());
          }
        }
      }
    }

    // Query the contacts table and get those that whose Contacts.CONTACT_LAST_UPDATED_TIMESTAMP is
    // after lastModified, such that Contacts._ID is in our set of contact IDs we build above.
    if (!contactIds.isEmpty()) {
      try (Cursor cursor = queryContactsTableForContacts(contactIds, lastModified)) {
        int contactIdIndex = cursor.getColumnIndex(Contacts._ID);
        int lastUpdatedIndex = cursor.getColumnIndex(Contacts.CONTACT_LAST_UPDATED_TIMESTAMP);
        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
          // Find the DialerPhoneNumber for each contact id and add it to our updated numbers set.
          // These, along with our number not associated with any Cp2ContactInfo need to be updated.
          long contactId = cursor.getLong(contactIdIndex);
          updatedNumbers.addAll(
              findDialerPhoneNumbersContainingContactId(existingInfoMap, contactId));
          long lastUpdatedTimestamp = cursor.getLong(lastUpdatedIndex);
          if (currentLastTimestampProcessed == null
              || currentLastTimestampProcessed < lastUpdatedTimestamp) {
            currentLastTimestampProcessed = lastUpdatedTimestamp;
          }
        }
      }
    }

    if (updatedNumbers.isEmpty()) {
      return new ArrayMap<>();
    }

    Map<DialerPhoneNumber, Set<Cp2ContactInfo>> map = new ArrayMap<>();

    // Divide the numbers into those we can format to E164 and those we can't. Then run separate
    // queries against the contacts table using the NORMALIZED_NUMBER and NUMBER columns.
    // TODO(zachh): These queries are inefficient without a lastModified column to filter on.
    PartitionedNumbers partitionedNumbers = new PartitionedNumbers(updatedNumbers);
    if (!partitionedNumbers.validE164Numbers().isEmpty()) {
      try (Cursor cursor =
          queryPhoneTableBasedOnE164(CP2_INFO_PROJECTION, partitionedNumbers.validE164Numbers())) {
        if (cursor == null) {
          LogUtil.w("Cp2PhoneLookup.buildMapForUpdatedOrAddedContacts", "null cursor");
        } else {
          while (cursor.moveToNext()) {
            String e164Number = cursor.getString(CP2_INFO_NORMALIZED_NUMBER_INDEX);
            Set<DialerPhoneNumber> dialerPhoneNumbers =
                partitionedNumbers.dialerPhoneNumbersForE164(e164Number);
            Cp2ContactInfo info = buildCp2ContactInfoFromPhoneCursor(appContext, cursor);
            addInfo(map, dialerPhoneNumbers, info);
          }
        }
      }
    }
    if (!partitionedNumbers.unformattableNumbers().isEmpty()) {
      try (Cursor cursor =
          queryPhoneTableBasedOnRawNumber(
              CP2_INFO_PROJECTION, partitionedNumbers.unformattableNumbers())) {
        if (cursor == null) {
          LogUtil.w("Cp2PhoneLookup.buildMapForUpdatedOrAddedContacts", "null cursor");
        } else {
          while (cursor.moveToNext()) {
            String unformattableNumber = cursor.getString(CP2_INFO_NUMBER_INDEX);
            Set<DialerPhoneNumber> dialerPhoneNumbers =
                partitionedNumbers.dialerPhoneNumbersForUnformattable(unformattableNumber);
            Cp2ContactInfo info = buildCp2ContactInfoFromPhoneCursor(appContext, cursor);
            addInfo(map, dialerPhoneNumbers, info);
          }
        }
      }
    }
    return map;
  }

  /**
   * Adds the {@code cp2ContactInfo} to the entries for all specified {@code dialerPhoneNumbers} in
   * the {@code map}.
   */
  private static void addInfo(
      Map<DialerPhoneNumber, Set<Cp2ContactInfo>> map,
      Set<DialerPhoneNumber> dialerPhoneNumbers,
      Cp2ContactInfo cp2ContactInfo) {
    for (DialerPhoneNumber dialerPhoneNumber : dialerPhoneNumbers) {
      if (map.containsKey(dialerPhoneNumber)) {
        map.get(dialerPhoneNumber).add(cp2ContactInfo);
      } else {
        Set<Cp2ContactInfo> cp2ContactInfos = new ArraySet<>();
        cp2ContactInfos.add(cp2ContactInfo);
        map.put(dialerPhoneNumber, cp2ContactInfos);
      }
    }
  }

  private Cursor queryPhoneTableBasedOnE164(String[] projection, Set<String> validE164Numbers) {
    return appContext
        .getContentResolver()
        .query(
            Phone.CONTENT_URI,
            projection,
            Phone.NORMALIZED_NUMBER + " IN (" + questionMarks(validE164Numbers.size()) + ")",
            validE164Numbers.toArray(new String[validE164Numbers.size()]),
            null);
  }

  private Cursor queryPhoneTableBasedOnRawNumber(
      String[] projection, Set<String> unformattableNumbers) {
    return appContext
        .getContentResolver()
        .query(
            Phone.CONTENT_URI,
            projection,
            Phone.NUMBER + " IN (" + questionMarks(unformattableNumbers.size()) + ")",
            unformattableNumbers.toArray(new String[unformattableNumbers.size()]),
            null);
  }

  /**
   * @param cursor with projection {@link #CP2_INFO_PROJECTION}.
   * @return new {@link Cp2ContactInfo} based on current row of {@code cursor}.
   */
  private static Cp2ContactInfo buildCp2ContactInfoFromPhoneCursor(
      Context appContext, Cursor cursor) {
    String displayName = cursor.getString(CP2_INFO_NAME_INDEX);
    String photoUri = cursor.getString(CP2_INFO_PHOTO_URI_INDEX);
    int photoId = cursor.getInt(CP2_INFO_PHOTO_ID_INDEX);
    int type = cursor.getInt(CP2_INFO_TYPE_INDEX);
    String label = cursor.getString(CP2_INFO_LABEL_INDEX);
    int contactId = cursor.getInt(CP2_INFO_CONTACT_ID_INDEX);
    String lookupKey = cursor.getString(CP2_INFO_LOOKUP_KEY_INDEX);

    Cp2ContactInfo.Builder infoBuilder = Cp2ContactInfo.newBuilder();
    if (!TextUtils.isEmpty(displayName)) {
      infoBuilder.setName(displayName);
    }
    if (!TextUtils.isEmpty(photoUri)) {
      infoBuilder.setPhotoUri(photoUri);
    }
    if (photoId > 0) {
      infoBuilder.setPhotoId(photoId);
    }

    // Phone.getTypeLabel returns "Custom" if given (0, null) which is not of any use. Just
    // omit setting the label if there's no information for it.
    if (type != 0 || !TextUtils.isEmpty(label)) {
      infoBuilder.setLabel(Phone.getTypeLabel(appContext.getResources(), type, label).toString());
    }
    infoBuilder.setContactId(contactId);
    if (!TextUtils.isEmpty(lookupKey)) {
      infoBuilder.setLookupUri(Contacts.getLookupUri(contactId, lookupKey).toString());
    }
    return infoBuilder.build();
  }

  /** Returns set of DialerPhoneNumbers that were associated with now deleted contacts. */
  private Set<DialerPhoneNumber> getDeletedPhoneNumbers(
      ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap, long lastModified) {
    // Build set of all contact IDs from our existing data. We're going to use this set to query
    // against the DeletedContacts table and see if any of them were deleted.
    Set<Long> contactIds = findContactIdsIn(existingInfoMap);

    // Start building a set of DialerPhoneNumbers that were associated with now deleted contacts.
    try (Cursor cursor = queryDeletedContacts(contactIds, lastModified)) {
      // We now have a cursor/list of contact IDs that were associated with deleted contacts.
      return findDeletedPhoneNumbersIn(existingInfoMap, cursor);
    }
  }

  private Set<Long> findContactIdsIn(ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> map) {
    Set<Long> contactIds = new ArraySet<>();
    for (PhoneLookupInfo info : map.values()) {
      for (Cp2ContactInfo cp2ContactInfo : info.getCp2Info().getCp2ContactInfoList()) {
        contactIds.add(cp2ContactInfo.getContactId());
      }
    }
    return contactIds;
  }

  private Cursor queryDeletedContacts(Set<Long> contactIds, long lastModified) {
    String where =
        DeletedContacts.CONTACT_DELETED_TIMESTAMP
            + " > ?"
            + " AND "
            + DeletedContacts.CONTACT_ID
            + " IN ("
            + questionMarks(contactIds.size())
            + ")";
    String[] args = new String[contactIds.size() + 1];
    args[0] = Long.toString(lastModified);
    int i = 1;
    for (Long contactId : contactIds) {
      args[i++] = Long.toString(contactId);
    }

    return appContext
        .getContentResolver()
        .query(
            DeletedContacts.CONTENT_URI,
            new String[] {DeletedContacts.CONTACT_ID, DeletedContacts.CONTACT_DELETED_TIMESTAMP},
            where,
            args,
            null);
  }

  /** Returns set of DialerPhoneNumbers that are associated with deleted contact IDs. */
  private Set<DialerPhoneNumber> findDeletedPhoneNumbersIn(
      ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap, Cursor cursor) {
    int contactIdIndex = cursor.getColumnIndexOrThrow(DeletedContacts.CONTACT_ID);
    int deletedTimeIndex = cursor.getColumnIndexOrThrow(DeletedContacts.CONTACT_DELETED_TIMESTAMP);
    Set<DialerPhoneNumber> deletedPhoneNumbers = new ArraySet<>();
    cursor.moveToPosition(-1);
    while (cursor.moveToNext()) {
      long contactId = cursor.getLong(contactIdIndex);
      deletedPhoneNumbers.addAll(
          findDialerPhoneNumbersContainingContactId(existingInfoMap, contactId));
      long deletedTime = cursor.getLong(deletedTimeIndex);
      if (currentLastTimestampProcessed == null || currentLastTimestampProcessed < deletedTime) {
        // TODO(zachh): There's a problem here if a contact for a new row is deleted?
        currentLastTimestampProcessed = deletedTime;
      }
    }
    return deletedPhoneNumbers;
  }

  private static Set<DialerPhoneNumber> findDialerPhoneNumbersContainingContactId(
      ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap, long contactId) {
    Set<DialerPhoneNumber> matches = new ArraySet<>();
    for (Entry<DialerPhoneNumber, PhoneLookupInfo> entry : existingInfoMap.entrySet()) {
      for (Cp2ContactInfo cp2ContactInfo : entry.getValue().getCp2Info().getCp2ContactInfoList()) {
        if (cp2ContactInfo.getContactId() == contactId) {
          matches.add(entry.getKey());
        }
      }
    }
    Assert.checkArgument(
        matches.size() > 0, "Couldn't find DialerPhoneNumber for contact ID: " + contactId);
    return matches;
  }

  private static String questionMarks(int count) {
    StringBuilder where = new StringBuilder();
    for (int i = 0; i < count; i++) {
      if (i != 0) {
        where.append(", ");
      }
      where.append("?");
    }
    return where.toString();
  }

  /**
   * Divides a set of {@link DialerPhoneNumber DialerPhoneNumbers} by those that can be formatted to
   * E164 and those that cannot.
   */
  private static class PartitionedNumbers {
    private Map<String, Set<DialerPhoneNumber>> e164NumbersToDialerPhoneNumbers = new ArrayMap<>();
    private Map<String, Set<DialerPhoneNumber>> unformattableNumbersToDialerPhoneNumbers =
        new ArrayMap<>();

    PartitionedNumbers(Set<DialerPhoneNumber> dialerPhoneNumbers) {
      DialerPhoneNumberUtil dialerPhoneNumberUtil =
          new DialerPhoneNumberUtil(PhoneNumberUtil.getInstance());
      for (DialerPhoneNumber dialerPhoneNumber : dialerPhoneNumbers) {
        Optional<String> e164 = dialerPhoneNumberUtil.formatToE164(dialerPhoneNumber);
        if (e164.isPresent()) {
          String validE164 = e164.get();
          Set<DialerPhoneNumber> currentNumbers = e164NumbersToDialerPhoneNumbers.get(validE164);
          if (currentNumbers == null) {
            currentNumbers = new ArraySet<>();
            e164NumbersToDialerPhoneNumbers.put(validE164, currentNumbers);
          }
          currentNumbers.add(dialerPhoneNumber);
        } else {
          String unformattableNumber = dialerPhoneNumber.getRawInput().getNumber();
          Set<DialerPhoneNumber> currentNumbers =
              unformattableNumbersToDialerPhoneNumbers.get(unformattableNumber);
          if (currentNumbers == null) {
            currentNumbers = new ArraySet<>();
            unformattableNumbersToDialerPhoneNumbers.put(unformattableNumber, currentNumbers);
          }
          currentNumbers.add(dialerPhoneNumber);
        }
      }
    }

    Set<String> unformattableNumbers() {
      return unformattableNumbersToDialerPhoneNumbers.keySet();
    }

    Set<String> validE164Numbers() {
      return e164NumbersToDialerPhoneNumbers.keySet();
    }

    Set<DialerPhoneNumber> dialerPhoneNumbersForE164(String e164) {
      return e164NumbersToDialerPhoneNumbers.get(e164);
    }

    Set<DialerPhoneNumber> dialerPhoneNumbersForUnformattable(String unformattableNumber) {
      return unformattableNumbersToDialerPhoneNumbers.get(unformattableNumber);
    }
  }
}