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