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      * @param context Context
49      * @return {@link UserInfo} of the corp user that is linked to the current user,
50      *         if any. If there's no such user or cross-user contacts access is
51      *         disallowed by policy, returns null.
52      */
getCorpUserInfo(Context context)53     private static UserInfo getCorpUserInfo(Context context) {
54         final UserManager um = getUserManager(context);
55         final int myUser = um.getUserHandle();
56 
57         // Check each user.
58         for (UserInfo ui : um.getUsers()) {
59             if (!ui.isManagedProfile()) {
60                 continue; // Not a managed user.
61             }
62             final UserInfo parent = um.getProfileParent(ui.id);
63             if (parent == null) {
64                 continue; // No parent.
65             }
66             // Check if it's linked to the current user.
67             if (parent.id == myUser) {
68                 return ui;
69             }
70         }
71         return null;
72     }
73 
74     /**
75      * @return the user ID of the corp user that is linked to the current user,
76      *         if any. If there's no such user returns -1.
77      */
getCorpUserId(Context context)78     public static int getCorpUserId(Context context) {
79         final UserInfo ui = getCorpUserInfo(context);
80         return ui == null ? -1 : ui.id;
81     }
82 }
83