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