summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/phonelookup/PhoneLookupSelector.java
blob: af8b849e5ec1d6dc4a213e9254478895c1901727 (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
/*
 * 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;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.android.dialer.phonelookup.PhoneLookupInfo.Cp2Info.Cp2ContactInfo;

/**
 * Prioritizes information from a {@link PhoneLookupInfo}.
 *
 * <p>For example, a single {@link PhoneLookupInfo} may contain different name information from many
 * different {@link PhoneLookup} sources. This class defines the rules for deciding which name
 * should be selected for display to the user, by prioritizing the data from some {@link PhoneLookup
 * PhoneLookups} over others.
 *
 * <p>Note that the logic in this class may be highly coupled with the logic in {@code
 * CompositePhoneLookup}, because {@code CompositePhoneLookup} may also include prioritization logic
 * for short-circuiting low-priority {@link PhoneLookup PhoneLookups}.
 */
public final class PhoneLookupSelector {

  /**
   * Select the name associated with this number. Examples of this are a local contact's name or a
   * business name received from caller ID.
   */
  @NonNull
  public static String selectName(PhoneLookupInfo phoneLookupInfo) {
    Cp2ContactInfo firstLocalContact = firstLocalContact(phoneLookupInfo);
    if (firstLocalContact != null) {
      String name = firstLocalContact.getName();
      if (!name.isEmpty()) {
        return firstLocalContact.getName();
      }
    }
    return "";
  }

  /** Select the photo URI associated with this number. */
  @NonNull
  public static String selectPhotoUri(PhoneLookupInfo phoneLookupInfo) {
    Cp2ContactInfo firstLocalContact = firstLocalContact(phoneLookupInfo);
    if (firstLocalContact != null) {
      String photoUri = firstLocalContact.getPhotoUri();
      if (!photoUri.isEmpty()) {
        return photoUri;
      }
    }
    return "";
  }

  /** Select the photo ID associated with this number, or 0 if there is none. */
  public static long selectPhotoId(PhoneLookupInfo phoneLookupInfo) {
    Cp2ContactInfo firstLocalContact = firstLocalContact(phoneLookupInfo);
    if (firstLocalContact != null) {
      long photoId = firstLocalContact.getPhotoId();
      if (photoId > 0) {
        return photoId;
      }
    }
    return 0;
  }

  /** Select the lookup URI associated with this number. */
  @NonNull
  public static String selectLookupUri(PhoneLookupInfo phoneLookupInfo) {
    Cp2ContactInfo firstLocalContact = firstLocalContact(phoneLookupInfo);
    if (firstLocalContact != null) {
      String lookupUri = firstLocalContact.getLookupUri();
      if (!lookupUri.isEmpty()) {
        return lookupUri;
      }
    }
    return "";
  }

  /**
   * A localized string representing the number type such as "Home" or "Mobile", or a custom value
   * set by the user.
   */
  @NonNull
  public static String selectNumberLabel(PhoneLookupInfo phoneLookupInfo) {
    Cp2ContactInfo firstLocalContact = firstLocalContact(phoneLookupInfo);
    if (firstLocalContact != null) {
      String label = firstLocalContact.getLabel();
      if (!label.isEmpty()) {
        return label;
      }
    }
    return "";
  }

  /**
   * Arbitrarily select the first contact. In the future, it may make sense to display contact
   * information from all contacts with the same number (for example show the name as "Mom, Dad" or
   * show a synthesized photo containing photos of both "Mom" and "Dad").
   */
  @Nullable
  private static Cp2ContactInfo firstLocalContact(PhoneLookupInfo phoneLookupInfo) {
    if (phoneLookupInfo.getCp2Info().getCp2ContactInfoCount() > 0) {
      return phoneLookupInfo.getCp2Info().getCp2ContactInfo(0);
    }
    return null;
  }
}