Home | History | Annotate | Download | only in common
      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.support.annotation.IntDef;
     21 import com.android.dialer.common.cp2.DirectoryCompat;
     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    */
     60   public static @UserType long determineUserType(Long directoryId, Long contactId) {
     61     // First check directory id
     62     if (directoryId != null) {
     63       return DirectoryCompat.isEnterpriseDirectoryId(directoryId)
     64           ? USER_TYPE_WORK
     65           : USER_TYPE_CURRENT;
     66     }
     67     // Only check contact id if directory id is null
     68     if (contactId != null && contactId != 0L && Contacts.isEnterpriseContactId(contactId)) {
     69       return USER_TYPE_WORK;
     70     } else {
     71       return USER_TYPE_CURRENT;
     72     }
     73   }
     74 
     75   /**
     76    * UserType indicates the user type of the contact. If the contact is from Work User (Work Profile
     77    * in Android Multi-User System), it's {@link #USER_TYPE_WORK}, otherwise, {@link
     78    * #USER_TYPE_CURRENT}. Please note that current user can be in work profile, where the dialer is
     79    * running inside Work Profile.
     80    */
     81   @Retention(RetentionPolicy.SOURCE)
     82   // TODO: Switch to @LongDef when @LongDef is available in the support library
     83   @IntDef({(int) USER_TYPE_CURRENT, (int) USER_TYPE_WORK})
     84   public @interface UserType {}
     85 }
     86