1 /* 2 * Copyright (C) 2014 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 package com.android.providers.contacts.util; 17 18 import com.android.providers.contacts.ContactsProvider2; 19 20 import android.annotation.SuppressLint; 21 import android.app.admin.DevicePolicyManager; 22 import android.content.Context; 23 import android.content.pm.UserInfo; 24 import android.content.pm.UserProperties; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.util.Log; 28 29 public final class UserUtils { 30 public static final String TAG = ContactsProvider2.TAG; 31 32 public static final boolean VERBOSE_LOGGING = Log.isLoggable(TAG, Log.VERBOSE); 33 UserUtils()34 private UserUtils() { 35 } 36 getUserManager(Context context)37 public static UserManager getUserManager(Context context) { 38 return (UserManager) context.getSystemService(Context.USER_SERVICE); 39 } 40 getDevicePolicyManager(Context context)41 private static DevicePolicyManager getDevicePolicyManager(Context context) { 42 return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); 43 } 44 getCurrentUserHandle(Context context)45 public static int getCurrentUserHandle(Context context) { 46 return getUserManager(context).getProcessUserId(); 47 } 48 49 /** 50 * @param context Context 51 * @return {@link UserInfo} of the corp user that is linked to the current user, 52 * if any. If there's no such user or cross-user contacts access is 53 * disallowed by policy, returns null. 54 */ getCorpUserInfo(Context context)55 private static UserInfo getCorpUserInfo(Context context) { 56 final UserManager um = getUserManager(context); 57 final int myUser = um.getProcessUserId(); 58 59 // Check each user. 60 for (UserInfo ui : um.getUsers()) { 61 if (!ui.isManagedProfile()) { 62 continue; // Not a managed user. 63 } 64 final UserInfo parent = um.getProfileParent(ui.id); 65 if (parent == null) { 66 continue; // No parent. 67 } 68 // Check if it's linked to the current user. 69 if (parent.id == myUser) { 70 return ui; 71 } 72 } 73 return null; 74 } 75 76 /** 77 * @return the user ID of the corp user that is linked to the current user, 78 * if any. If there's no such user returns -1. 79 */ getCorpUserId(Context context)80 public static int getCorpUserId(Context context) { 81 final UserInfo ui = getCorpUserInfo(context); 82 return ui == null ? -1 : ui.id; 83 } 84 85 @SuppressLint("AndroidFrameworkRequiresPermission") shouldUseParentsContacts(Context context)86 public static boolean shouldUseParentsContacts(Context context) { 87 try { 88 final UserManager userManager = getUserManager(context); 89 final UserProperties userProperties = userManager.getUserProperties(context.getUser()); 90 return userProperties.getUseParentsContacts(); 91 } catch (IllegalArgumentException e) { 92 Log.w(TAG, "Trying to fetch user properties for non-existing/partial user " 93 + context.getUser()); 94 return false; 95 } 96 } 97 98 @SuppressLint("AndroidFrameworkRequiresPermission") shouldUseParentsContacts(Context context, UserHandle userHandle)99 public static boolean shouldUseParentsContacts(Context context, UserHandle userHandle) { 100 try { 101 final UserManager userManager = getUserManager(context); 102 final UserProperties userProperties = userManager.getUserProperties(userHandle); 103 return userProperties.getUseParentsContacts(); 104 } catch (IllegalArgumentException e) { 105 Log.w(TAG, "Trying to fetch user properties for non-existing/partial user " 106 + userHandle); 107 return false; 108 } 109 } 110 111 /** 112 * Checks if the input profile user is the parent of the other user 113 * @return True if user1 is the parent profile of user2, false otherwise 114 */ 115 @SuppressLint("AndroidFrameworkRequiresPermission") isParentUser(Context context, UserHandle user1, UserHandle user2)116 public static boolean isParentUser(Context context, UserHandle user1, UserHandle user2) { 117 if (user1 == null || user2 == null) return false; 118 final UserManager userManager = getUserManager(context); 119 UserInfo parentUserInfo = userManager.getProfileParent(user2.getIdentifier()); 120 return parentUserInfo != null 121 && parentUserInfo.getUserHandle() != null 122 && parentUserInfo.getUserHandle().equals(user1); 123 } 124 125 @SuppressLint("AndroidFrameworkRequiresPermission") getProfileParentUser(Context context)126 public static UserInfo getProfileParentUser(Context context) { 127 final UserManager userManager = getUserManager(context); 128 return userManager.getProfileParent(context.getUserId()); 129 } 130 } 131