• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
17 package com.android.role.controller.util;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.Flags;
22 import android.os.Process;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 
26 import androidx.annotation.NonNull;
27 
28 import com.android.modules.utils.build.SdkLevel;
29 
30 /** Utility class to deal with Android users. */
31 public final class UserUtils {
32 
UserUtils()33     private UserUtils() {}
34 
35     /**
36      * Check whether a user is a profile.
37      *
38      * @param user    the user to check
39      * @param context the {@code Context} to retrieve system services
40      * @return whether the user is a profile
41      */
isProfile(@onNull UserHandle user, @NonNull Context context)42     public static boolean isProfile(@NonNull UserHandle user, @NonNull Context context) {
43         if (SdkLevel.isAtLeastV()) {
44             Context userContext = getUserContext(context, user);
45             UserManager userUserManager = userContext.getSystemService(UserManager.class);
46             return userUserManager.isProfile();
47         } else {
48             return isManagedProfile(user, context) || isCloneProfile(user, context);
49         }
50     }
51 
52     /**
53      * Check whether a user is a managed profile.
54      *
55      * @param user    the user to check
56      * @param context the {@code Context} to retrieve system services
57      * @return whether the user is a managed profile
58      */
isManagedProfile(@onNull UserHandle user, @NonNull Context context)59     public static boolean isManagedProfile(@NonNull UserHandle user, @NonNull Context context) {
60         Context userContext = getUserContext(context, user);
61         UserManager userUserManager = userContext.getSystemService(UserManager.class);
62         return userUserManager.isManagedProfile(user.getIdentifier());
63     }
64 
65     /**
66      * Check whether a user is a clone profile.
67      *
68      * @param user    the user to check
69      * @param context the {@code Context} to retrieve system services
70      * @return whether the user is a clone profile
71      */
isCloneProfile(@onNull UserHandle user, @NonNull Context context)72     public static boolean isCloneProfile(@NonNull UserHandle user, @NonNull Context context) {
73         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
74             return false;
75         }
76         Context userContext = getUserContext(context, user);
77         UserManager userUserManager = userContext.getSystemService(UserManager.class);
78         return userUserManager.isCloneProfile();
79     }
80 
81     /**
82      * Check whether a user is a private profile.
83      *
84      * @param user    the user to check
85      * @param context the {@code Context} to retrieve system services
86      * @return whether the user is a private profile. Private profiles are
87      * allowed from Android V+ only, so this method will return false on Sdk levels below that.
88      */
isPrivateProfile(@onNull UserHandle user, @NonNull Context context)89     public static boolean isPrivateProfile(@NonNull UserHandle user, @NonNull Context context) {
90         if (!SdkLevel.isAtLeastV() || !Flags.allowPrivateProfile()) {
91             return false;
92         }
93         Context userContext = getUserContext(context, user);
94         UserManager userUserManager = userContext.getSystemService(UserManager.class);
95         return userUserManager.isPrivateProfile();
96     }
97 
98     /**
99      * Create a context for a user.
100      *
101      * @param context The context to clone
102      * @param user The user the new context should be for
103      *
104      * @return The context for the new user
105      */
106     @NonNull
getUserContext(@onNull Context context, @NonNull UserHandle user)107     public static Context getUserContext(@NonNull Context context, @NonNull UserHandle user) {
108         if (Process.myUserHandle().equals(user)) {
109             return context;
110         } else {
111             return context.createContextAsUser(user, 0);
112         }
113     }
114 }
115