• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 package com.android.dialer.phonelookup.cp2;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.provider.ContactsContract.CommonDataKinds.Phone;
22 import android.provider.ContactsContract.Contacts;
23 import android.provider.ContactsContract.PhoneLookup;
24 import android.text.TextUtils;
25 import com.android.dialer.phonelookup.PhoneLookupInfo.Cp2Info.Cp2ContactInfo;
26 
27 /**
28  * A class providing projection-related functionality for {@link
29  * com.android.dialer.phonelookup.PhoneLookup} implementations for ContactsProvider2 (CP2).
30  */
31 final class Cp2Projections {
32 
33   // Projection for performing lookups using the PHONE table
34   private static final String[] PHONE_PROJECTION =
35       new String[] {
36         Phone.DISPLAY_NAME_PRIMARY, // 0
37         Phone.PHOTO_THUMBNAIL_URI, // 1
38         Phone.PHOTO_URI, // 2
39         Phone.PHOTO_ID, // 3
40         Phone.TYPE, // 4
41         Phone.LABEL, // 5
42         Phone.NORMALIZED_NUMBER, // 6
43         Phone.CONTACT_ID, // 7
44         Phone.LOOKUP_KEY // 8
45       };
46 
47   // Projection for performing lookups using the PHONE_LOOKUP table
48   private static final String[] PHONE_LOOKUP_PROJECTION =
49       new String[] {
50         PhoneLookup.DISPLAY_NAME_PRIMARY, // 0
51         PhoneLookup.PHOTO_THUMBNAIL_URI, // 1
52         PhoneLookup.PHOTO_URI, // 2
53         PhoneLookup.PHOTO_ID, // 3
54         PhoneLookup.TYPE, // 4
55         PhoneLookup.LABEL, // 5
56         PhoneLookup.NORMALIZED_NUMBER, // 6
57         PhoneLookup.CONTACT_ID, // 7
58         PhoneLookup.LOOKUP_KEY // 8
59       };
60 
61   // The following indexes should match both PHONE_PROJECTION and PHONE_LOOKUP_PROJECTION above.
62   private static final int CP2_INFO_NAME_INDEX = 0;
63   private static final int CP2_INFO_PHOTO_THUMBNAIL_URI_INDEX = 1;
64   private static final int CP2_INFO_PHOTO_URI_INDEX = 2;
65   private static final int CP2_INFO_PHOTO_ID_INDEX = 3;
66   private static final int CP2_INFO_TYPE_INDEX = 4;
67   private static final int CP2_INFO_LABEL_INDEX = 5;
68   private static final int CP2_INFO_NORMALIZED_NUMBER_INDEX = 6;
69   private static final int CP2_INFO_CONTACT_ID_INDEX = 7;
70   private static final int CP2_INFO_LOOKUP_KEY_INDEX = 8;
71 
Cp2Projections()72   private Cp2Projections() {}
73 
getProjectionForPhoneTable()74   static String[] getProjectionForPhoneTable() {
75     return PHONE_PROJECTION;
76   }
77 
getProjectionForPhoneLookupTable()78   static String[] getProjectionForPhoneLookupTable() {
79     return PHONE_LOOKUP_PROJECTION;
80   }
81 
82   /**
83    * Builds a {@link Cp2ContactInfo} based on the current row of {@code cursor}, of which the
84    * projection is either {@link #PHONE_PROJECTION} or {@link #PHONE_LOOKUP_PROJECTION}.
85    */
buildCp2ContactInfoFromCursor(Context appContext, Cursor cursor)86   static Cp2ContactInfo buildCp2ContactInfoFromCursor(Context appContext, Cursor cursor) {
87     String displayName = cursor.getString(CP2_INFO_NAME_INDEX);
88     String photoThumbnailUri = cursor.getString(CP2_INFO_PHOTO_THUMBNAIL_URI_INDEX);
89     String photoUri = cursor.getString(CP2_INFO_PHOTO_URI_INDEX);
90     int photoId = cursor.getInt(CP2_INFO_PHOTO_ID_INDEX);
91     int type = cursor.getInt(CP2_INFO_TYPE_INDEX);
92     String label = cursor.getString(CP2_INFO_LABEL_INDEX);
93     int contactId = cursor.getInt(CP2_INFO_CONTACT_ID_INDEX);
94     String lookupKey = cursor.getString(CP2_INFO_LOOKUP_KEY_INDEX);
95 
96     Cp2ContactInfo.Builder infoBuilder = Cp2ContactInfo.newBuilder();
97     if (!TextUtils.isEmpty(displayName)) {
98       infoBuilder.setName(displayName);
99     }
100     if (!TextUtils.isEmpty(photoThumbnailUri)) {
101       infoBuilder.setPhotoThumbnailUri(photoThumbnailUri);
102     }
103     if (!TextUtils.isEmpty(photoUri)) {
104       infoBuilder.setPhotoUri(photoUri);
105     }
106     if (photoId > 0) {
107       infoBuilder.setPhotoId(photoId);
108     }
109 
110     // Phone.getTypeLabel returns "Custom" if given (0, null) which is not of any use. Just
111     // omit setting the label if there's no information for it.
112     if (type != 0 || !TextUtils.isEmpty(label)) {
113       infoBuilder.setLabel(Phone.getTypeLabel(appContext.getResources(), type, label).toString());
114     }
115     infoBuilder.setContactId(contactId);
116     if (!TextUtils.isEmpty(lookupKey)) {
117       infoBuilder.setLookupUri(Contacts.getLookupUri(contactId, lookupKey).toString());
118     }
119     return infoBuilder.build();
120   }
121 
122   /** Returns the normalized number in the current row of {@code cursor}. */
getNormalizedNumberFromCursor(Cursor cursor)123   static String getNormalizedNumberFromCursor(Cursor cursor) {
124     return cursor.getString(CP2_INFO_NORMALIZED_NUMBER_INDEX);
125   }
126 }
127