1 /*
2  * Copyright (C) 2009 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.contacts;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.os.Build;
24 import android.provider.ContactsContract.CommonDataKinds.Im;
25 import android.provider.ContactsContract.DisplayPhoto;
26 import androidx.annotation.IntDef;
27 import android.text.TextUtils;
28 import android.util.Pair;
29 
30 import com.android.contacts.compat.ContactsCompat;
31 import com.android.contacts.compat.DirectoryCompat;
32 import com.android.contacts.model.dataitem.ImDataItem;
33 
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 
37 public class ContactsUtils {
38     private static final String TAG = "ContactsUtils";
39 
40     // Telecomm related schemes are in CallUtil
41     public static final String SCHEME_IMTO = "imto";
42     public static final String SCHEME_MAILTO = "mailto";
43     public static final String SCHEME_SMSTO = "smsto";
44 
45     private static final int DEFAULT_THUMBNAIL_SIZE = 96;
46 
47     private static int sThumbnailSize = -1;
48 
49     public static final boolean FLAG_N_FEATURE = Build.VERSION.SDK_INT >= 24;
50 
51     // TODO find a proper place for the canonical version of these
52     public interface ProviderNames {
53         String YAHOO = "Yahoo";
54         String GTALK = "GTalk";
55         String MSN = "MSN";
56         String ICQ = "ICQ";
57         String AIM = "AIM";
58         String XMPP = "XMPP";
59         String JABBER = "JABBER";
60         String SKYPE = "SKYPE";
61         String QQ = "QQ";
62     }
63 
64     /**
65      * This looks up the provider name defined in
66      * ProviderNames from the predefined IM protocol id.
67      * This is used for interacting with the IM application.
68      *
69      * @param protocol the protocol ID
70      * @return the provider name the IM app uses for the given protocol, or null if no
71      * provider is defined for the given protocol
72      * @hide
73      */
lookupProviderNameFromId(int protocol)74     public static String lookupProviderNameFromId(int protocol) {
75         switch (protocol) {
76             case Im.PROTOCOL_GOOGLE_TALK:
77                 return ProviderNames.GTALK;
78             case Im.PROTOCOL_AIM:
79                 return ProviderNames.AIM;
80             case Im.PROTOCOL_MSN:
81                 return ProviderNames.MSN;
82             case Im.PROTOCOL_YAHOO:
83                 return ProviderNames.YAHOO;
84             case Im.PROTOCOL_ICQ:
85                 return ProviderNames.ICQ;
86             case Im.PROTOCOL_JABBER:
87                 return ProviderNames.JABBER;
88             case Im.PROTOCOL_SKYPE:
89                 return ProviderNames.SKYPE;
90             case Im.PROTOCOL_QQ:
91                 return ProviderNames.QQ;
92         }
93         return null;
94     }
95 
96 
97     public static final long USER_TYPE_CURRENT = 0;
98     public static final long USER_TYPE_WORK = 1;
99 
100     /**
101      * UserType indicates the user type of the contact. If the contact is from Work User (Work
102      * Profile in Android Multi-User System), it's {@link #USER_TYPE_WORK}, otherwise,
103      * {@link #USER_TYPE_CURRENT}. Please note that current user can be in work profile, where the
104      * dialer is running inside Work Profile.
105      */
106     @Retention(RetentionPolicy.SOURCE)
107     // TODO: Switch to @LongDef once @LongDef is available in the support library
108     @IntDef({(int)USER_TYPE_CURRENT, (int)USER_TYPE_WORK})
109     public @interface UserType {}
110 
111     /**
112      * Test if the given {@link CharSequence} contains any graphic characters,
113      * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
114      */
isGraphic(CharSequence str)115     public static boolean isGraphic(CharSequence str) {
116         return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
117     }
118 
119     /**
120      * Returns true if two objects are considered equal.  Two null references are equal here.
121      */
areObjectsEqual(Object a, Object b)122     public static boolean areObjectsEqual(Object a, Object b) {
123         return a == b || (a != null && a.equals(b));
124     }
125 
126     /**
127      * Returns true if two {@link Intent}s are both null, or have the same action.
128      */
areIntentActionEqual(Intent a, Intent b)129     public static final boolean areIntentActionEqual(Intent a, Intent b) {
130         if (a == b) {
131             return true;
132         }
133         if (a == null || b == null) {
134             return false;
135         }
136         return TextUtils.equals(a.getAction(), b.getAction());
137     }
138 
139     /**
140      * Returns the size (width and height) of thumbnail pictures as configured in the provider. This
141      * can safely be called from the UI thread, as the provider can serve this without performing
142      * a database access
143      */
getThumbnailSize(Context context)144     public static int getThumbnailSize(Context context) {
145         if (sThumbnailSize == -1) {
146             final Cursor c = context.getContentResolver().query(
147                     DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
148                     new String[] { DisplayPhoto.THUMBNAIL_MAX_DIM }, null, null, null);
149             if (c != null) {
150                 try {
151                     if (c.moveToFirst()) {
152                         sThumbnailSize = c.getInt(0);
153                     }
154                 } finally {
155                     c.close();
156                 }
157             }
158         }
159         return sThumbnailSize != -1 ? sThumbnailSize : DEFAULT_THUMBNAIL_SIZE;
160     }
161 
getCustomImIntent(ImDataItem im, int protocol)162     private static Intent getCustomImIntent(ImDataItem im, int protocol) {
163         String host = im.getCustomProtocol();
164         final String data = im.getData();
165         if (TextUtils.isEmpty(data)) {
166             return null;
167         }
168         if (protocol != Im.PROTOCOL_CUSTOM) {
169             // Try bringing in a well-known host for specific protocols
170             host = ContactsUtils.lookupProviderNameFromId(protocol);
171         }
172         if (TextUtils.isEmpty(host)) {
173             return null;
174         }
175         final String authority = host.toLowerCase();
176         final Uri imUri = new Uri.Builder().scheme(SCHEME_IMTO).authority(
177                 authority).appendPath(data).build();
178         final Intent intent = new Intent(Intent.ACTION_SENDTO, imUri);
179         return intent;
180     }
181 
182     /**
183      * Returns the proper Intent for an ImDatItem. If available, a secondary intent is stored
184      * in the second Pair slot
185      */
buildImIntent(Context context, ImDataItem im)186     public static Pair<Intent, Intent> buildImIntent(Context context, ImDataItem im) {
187         Intent intent = null;
188         Intent secondaryIntent = null;
189         final boolean isEmail = im.isCreatedFromEmail();
190 
191         if (!isEmail && !im.isProtocolValid()) {
192             return new Pair<>(null, null);
193         }
194 
195         final String data = im.getData();
196         if (TextUtils.isEmpty(data)) {
197             return new Pair<>(null, null);
198         }
199 
200         final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
201 
202         if (protocol == Im.PROTOCOL_GOOGLE_TALK) {
203             final int chatCapability = im.getChatCapability();
204             if ((chatCapability & Im.CAPABILITY_HAS_CAMERA) != 0) {
205                 intent = new Intent(Intent.ACTION_SENDTO,
206                                 Uri.parse("xmpp:" + data + "?message"));
207                 secondaryIntent = new Intent(Intent.ACTION_SENDTO,
208                         Uri.parse("xmpp:" + data + "?call"));
209             } else if ((chatCapability & Im.CAPABILITY_HAS_VOICE) != 0) {
210                 // Allow Talking and Texting
211                 intent =
212                     new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message"));
213                 secondaryIntent =
214                     new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call"));
215             } else {
216                 intent =
217                     new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message"));
218             }
219         } else {
220             // Build an IM Intent
221             intent = getCustomImIntent(im, protocol);
222         }
223         return new Pair<>(intent, secondaryIntent);
224     }
225 
226     /**
227      * Determine UserType from directory id and contact id.
228      *
229      * 3 types of query
230      *
231      * 1. 2 profile query: content://com.android.contacts/phone_lookup_enterprise/1234567890
232      * personal and work contact are mixed into one cursor. no directory id. contact_id indicates if
233      * it's work contact
234      *
235      * 2. work local query:
236      * content://com.android.contacts/phone_lookup_enterprise/1234567890?directory=1000000000
237      * either directory_id or contact_id is enough to identify work contact
238      *
239      * 3. work remote query:
240      * content://com.android.contacts/phone_lookup_enterprise/1234567890?directory=1000000003
241      * contact_id is random. only directory_id is available
242      *
243      * Summary: If directory_id is not null, always use directory_id to identify work contact.
244      * (which is the case here) Otherwise, use contact_id.
245      *
246      * @param directoryId directory id of ContactsProvider query
247      * @param contactId contact id
248      * @return UserType indicates the user type of the contact. A directory id or contact id larger
249      *         than a thredshold indicates that the contact is stored in Work Profile, but not in
250      *         current user. It's a contract by ContactsProvider and check by
251      *         Contacts.isEnterpriseDirectoryId and Contacts.isEnterpriseContactId. Currently, only
252      *         2 kinds of users can be detected from the directoryId and contactId as
253      *         ContactsProvider can only access current and work user's contacts
254      */
determineUserType(Long directoryId, Long contactId)255     public static @UserType long determineUserType(Long directoryId, Long contactId) {
256         // First check directory id
257         if (directoryId != null) {
258             return DirectoryCompat.isEnterpriseDirectoryId(directoryId) ? USER_TYPE_WORK
259                     : USER_TYPE_CURRENT;
260         }
261         // Only check contact id if directory id is null
262         if (contactId != null && contactId != 0L
263                 && ContactsCompat.isEnterpriseContactId(contactId)) {
264             return USER_TYPE_WORK;
265         } else {
266             return USER_TYPE_CURRENT;
267         }
268 
269     }
270 }
271