• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.permissioncontroller.role.utils;
18 
19 import android.content.Context;
20 import android.os.Process;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 
27 import com.android.modules.utils.build.SdkLevel;
28 
29 import java.util.List;
30 import java.util.Objects;
31 
32 /**
33  * Utility methods about user.
34  */
35 public class UserUtils {
36 
UserUtils()37     private UserUtils() {}
38 
39     /**
40      * Returns the parent of a given user, or user if it has no parent (e.g. it is the primary
41      * user)
42      */
43     @NonNull
getProfileParentOrSelf(@onNull UserHandle user, @NonNull Context context)44     public static UserHandle getProfileParentOrSelf(@NonNull UserHandle user,
45             @NonNull Context context) {
46         return com.android.role.controller.util.UserUtils.getProfileParentOrSelf(user, context);
47     }
48 
49     /**
50      * Get the work profile of current user, if any.
51      * <p>
52      * If {@link Process#myUserHandle()} is the work profile, then {@code null} is returned
53      *
54      * @param context the {@code Context} to retrieve system services
55      *
56      * @return the work profile of current user, or {@code null} if none or if work profile is the
57      * current process user
58      */
59     @Nullable
getWorkProfile(@onNull Context context)60     public static UserHandle getWorkProfile(@NonNull Context context) {
61         UserManager userManager = context.getSystemService(UserManager.class);
62         List<UserHandle> profiles = userManager.getUserProfiles();
63         UserHandle user = Process.myUserHandle();
64 
65         int profilesSize = profiles.size();
66         for (int i = 0; i < profilesSize; i++) {
67             UserHandle profile = profiles.get(i);
68 
69             if (Objects.equals(profile, user)) {
70                 continue;
71             }
72             if (!userManager.isManagedProfile(profile.getIdentifier())) {
73                 continue;
74             }
75             return profile;
76         }
77         return null;
78     }
79 
80     /**
81      * Get the work profile of current profile-group, if any.
82      * <p>
83      * {@link Process#myUserHandle()} may be the work profile, and is a valid returned value
84      *
85      * @param context the {@code Context} to retrieve system services
86      *
87      * @return the work profile in the current profile-group, or {@code null} if none
88      */
89     @Nullable
getWorkProfileOrSelf(@onNull Context context)90     public static UserHandle getWorkProfileOrSelf(@NonNull Context context) {
91         UserManager userManager = context.getSystemService(UserManager.class);
92         List<UserHandle> profiles = userManager.getUserProfiles();
93         int profilesSize = profiles.size();
94         for (int i = 0; i < profilesSize; i++) {
95             UserHandle profile = profiles.get(i);
96             if (!userManager.isManagedProfile(profile.getIdentifier())) {
97                 continue;
98             }
99             return profile;
100         }
101         return null;
102     }
103 
104     /**
105      * Get the private profile of current user, if any.
106      *
107      * @param context the {@code Context} to retrieve system services
108      *
109      * @return the private profile of current user, or {@code null} if none
110      */
111     @Nullable
getPrivateProfile(@onNull Context context)112     public static UserHandle getPrivateProfile(@NonNull Context context) {
113         if (!SdkLevel.isAtLeastV()) {
114             return null;
115         }
116 
117         List<UserHandle> profiles = context.getSystemService(UserManager.class).getUserProfiles();
118         UserHandle user = Process.myUserHandle();
119 
120         int profilesSize = profiles.size();
121         for (int i = 0; i < profilesSize; i++) {
122             UserHandle profile = profiles.get(i);
123 
124             if (Objects.equals(profile, user)) {
125                 continue;
126             }
127             if (isPrivateProfile(profile, context)) {
128                 return profile;
129             }
130         }
131         return null;
132     }
133 
134     /**
135      * Returns whether the user is a private profile or not.
136      *
137      * @param userHandle the {@code UserHandle} to check is private profile
138      * @param context the {@code Context} to retrieve system services
139      */
isPrivateProfile(@onNull UserHandle userHandle, @NonNull Context context)140     private static boolean isPrivateProfile(@NonNull UserHandle userHandle,
141             @NonNull Context context) {
142         if (!SdkLevel.isAtLeastV()) {
143             return false;
144         }
145         Context userContext = context.createContextAsUser(userHandle, /* flags= */ 0);
146         return userContext.getSystemService(UserManager.class).isPrivateProfile();
147     }
148 
149     /**
150      * Create a context for a user.
151      *
152      * @param context The context to clone
153      * @param user The user the new context should be for
154      *
155      * @return The context for the new user
156      */
157     @NonNull
getUserContext(@onNull Context context, @NonNull UserHandle user)158     public static Context getUserContext(@NonNull Context context, @NonNull UserHandle user) {
159         if (Process.myUserHandle().equals(user)) {
160             return context;
161         } else {
162             return context.createContextAsUser(user, 0);
163         }
164     }
165 }
166