UserUtils.java revision 42d5b79db882739368b163f4d5f61546d134cdbb
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 */ 16package com.android.providers.contacts.util; 17 18import com.android.providers.contacts.ContactsProvider2; 19 20import android.app.admin.DevicePolicyManager; 21import android.content.Context; 22import android.content.pm.UserInfo; 23import android.os.UserHandle; 24import android.os.UserManager; 25import android.util.Log; 26 27public final class UserUtils { 28 public static final String TAG = ContactsProvider2.TAG; 29 30 public static final boolean VERBOSE_LOGGING = Log.isLoggable(TAG, Log.VERBOSE); 31 32 private UserUtils() { 33 } 34 35 public static UserManager getUserManager(Context context) { 36 return (UserManager) context.getSystemService(Context.USER_SERVICE); 37 } 38 39 private static DevicePolicyManager getDevicePolicyManager(Context context) { 40 return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); 41 } 42 43 public static int getCurrentUserHandle(Context context) { 44 return getUserManager(context).getUserHandle(); 45 } 46 47 /** 48 * @param enforceCallerIdCheck True if we want to enforce cross profile 49 * caller-id device policy. 50 * @return the user ID of the corp user that is linked to the current user, 51 * if any. If there's no such user or cross-user contacts access is 52 * disallowed by policy, returns -1. 53 */ 54 public static int getCorpUserId(Context context, boolean enforceCallerIdCheck) { 55 final UserManager um = getUserManager(context); 56 if (um == null) { 57 Log.e(TAG, "No user manager service found"); 58 return -1; 59 } 60 61 final int myUser = um.getUserHandle(); 62 63 if (VERBOSE_LOGGING) { 64 Log.v(TAG, "getCorpUserId: myUser=" + myUser); 65 } 66 67 // Check each user. 68 for (UserInfo ui : um.getUsers()) { 69 if (!ui.isManagedProfile()) { 70 continue; // Not a managed user. 71 } 72 final UserInfo parent = um.getProfileParent(ui.id); 73 if (parent == null) { 74 continue; // No parent. 75 } 76 // Check if it's linked to the current user. 77 if (parent.id == myUser) { 78 // Check if profile is blocking calling id. 79 // TODO DevicePolicyManager is not mockable -- the constructor is private. 80 // Test it somehow. 81 if (enforceCallerIdCheck 82 && getDevicePolicyManager(context) 83 .getCrossProfileCallerIdDisabled(ui.getUserHandle())) { 84 if (VERBOSE_LOGGING) { 85 Log.v(TAG, "Enterprise caller-id disabled for user " + ui.id); 86 } 87 return -1; 88 } else { 89 if (VERBOSE_LOGGING) { 90 Log.v(TAG, "Corp user=" + ui.id); 91 } 92 return ui.id; 93 } 94 } 95 } 96 if (VERBOSE_LOGGING) { 97 Log.v(TAG, "Corp user not found."); 98 } 99 return -1; 100 } 101} 102