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