• 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.provider.ContactsContract.Contacts;
20 import android.provider.ContactsContract.Directory;
21 import android.support.annotation.IntDef;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 
25 public class ContactsUtils {
26 
27   // Telecomm related schemes are in CallUtil
28   public static final long USER_TYPE_CURRENT = 0;
29   public static final long USER_TYPE_WORK = 1;
30 
31   /**
32    * Determine UserType from directory id and contact id.
33    *
34    * <p>3 types of query
35    *
36    * <p>1. 2 profile query: content://com.android.contacts/phone_lookup_enterprise/1234567890
37    * personal and work contact are mixed into one cursor. no directory id. contact_id indicates if
38    * it's work contact
39    *
40    * <p>2. work local query:
41    * content://com.android.contacts/phone_lookup_enterprise/1234567890?directory=1000000000 either
42    * directory_id or contact_id is enough to identify work contact
43    *
44    * <p>3. work remote query:
45    * content://com.android.contacts/phone_lookup_enterprise/1234567890?directory=1000000003
46    * contact_id is random. only directory_id is available
47    *
48    * <p>Summary: If directory_id is not null, always use directory_id to identify work contact.
49    * (which is the case here) Otherwise, use contact_id.
50    *
51    * @param directoryId directory id of ContactsProvider query
52    * @param contactId contact id
53    * @return UserType indicates the user type of the contact. A directory id or contact id larger
54    *     than a thredshold indicates that the contact is stored in Work Profile, but not in current
55    *     user. It's a contract by ContactsProvider and check by Contacts.isEnterpriseDirectoryId and
56    *     Contacts.isEnterpriseContactId. Currently, only 2 kinds of users can be detected from the
57    *     directoryId and contactId as ContactsProvider can only access current and work user's
58    *     contacts
59    */
determineUserType(Long directoryId, Long contactId)60   public static @UserType long determineUserType(Long directoryId, Long contactId) {
61     // First check directory id
62     if (directoryId != null) {
63       return Directory.isEnterpriseDirectoryId(directoryId) ? USER_TYPE_WORK : USER_TYPE_CURRENT;
64     }
65     // Only check contact id if directory id is null
66     if (contactId != null && contactId != 0L && Contacts.isEnterpriseContactId(contactId)) {
67       return USER_TYPE_WORK;
68     } else {
69       return USER_TYPE_CURRENT;
70     }
71   }
72 
73   /**
74    * UserType indicates the user type of the contact. If the contact is from Work User (Work Profile
75    * in Android Multi-User System), it's {@link #USER_TYPE_WORK}, otherwise, {@link
76    * #USER_TYPE_CURRENT}. Please note that current user can be in work profile, where the dialer is
77    * running inside Work Profile.
78    */
79   @Retention(RetentionPolicy.SOURCE)
80   // TODO: Switch to @LongDef when @LongDef is available in the support library
81   @IntDef({(int) USER_TYPE_CURRENT, (int) USER_TYPE_WORK})
82   public @interface UserType {}
83 }
84