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     * @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     */
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        // TODO DevicePolicyManager is not mockable -- the constructor is private.
65        // Test it somehow.
66        if (getDevicePolicyManager(context)
67                .getCrossProfileCallerIdDisabled(new UserHandle(myUser))) {
68            if (VERBOSE_LOGGING) {
69                Log.v(TAG, "Enterprise caller-id disabled.");
70            }
71            return -1;
72        }
73
74        // Check each user.
75        for (UserInfo ui : um.getUsers()) {
76            if (!ui.isManagedProfile()) {
77                continue; // Not a managed user.
78            }
79            final UserInfo parent = um.getProfileParent(ui.id);
80            if (parent == null) {
81                continue; // No parent.
82            }
83            // Check if it's linked to the current user.
84            if (parent.id == myUser) {
85                if (VERBOSE_LOGGING) {
86                    Log.v(TAG, "Corp user=" + ui.id);
87                }
88                return ui.id;
89            }
90        }
91        if (VERBOSE_LOGGING) {
92            Log.v(TAG, "Corp user not found.");
93        }
94        return -1;
95    }
96}
97