• 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      * Get the work profile of current user, if any.
41      *
42      * @param context the {@code Context} to retrieve system services
43      *
44      * @return the work profile of current user, or {@code null} if none
45      */
46     @Nullable
getWorkProfile(@onNull Context context)47     public static UserHandle getWorkProfile(@NonNull Context context) {
48         UserManager userManager = context.getSystemService(UserManager.class);
49         List<UserHandle> profiles = userManager.getUserProfiles();
50         UserHandle user = Process.myUserHandle();
51 
52         int profilesSize = profiles.size();
53         for (int i = 0; i < profilesSize; i++) {
54             UserHandle profile = profiles.get(i);
55 
56             if (Objects.equals(profile, user)) {
57                 continue;
58             }
59             if (!userManager.isManagedProfile(profile.getIdentifier())) {
60                 continue;
61             }
62             return profile;
63         }
64         return null;
65     }
66 
67     /**
68      * Get the private profile of current user, if any.
69      *
70      * @param context the {@code Context} to retrieve system services
71      *
72      * @return the private profile of current user, or {@code null} if none
73      */
74     @Nullable
getPrivateProfile(@onNull Context context)75     public static UserHandle getPrivateProfile(@NonNull Context context) {
76         if (!SdkLevel.isAtLeastV()) {
77             return null;
78         }
79 
80         List<UserHandle> profiles = context.getSystemService(UserManager.class).getUserProfiles();
81         UserHandle user = Process.myUserHandle();
82 
83         int profilesSize = profiles.size();
84         for (int i = 0; i < profilesSize; i++) {
85             UserHandle profile = profiles.get(i);
86 
87             if (Objects.equals(profile, user)) {
88                 continue;
89             }
90             if (isPrivateProfile(profile, context)) {
91                 return profile;
92             }
93         }
94         return null;
95     }
96 
isPrivateProfile(@onNull UserHandle userHandle, @NonNull Context context)97     private static boolean isPrivateProfile(@NonNull UserHandle userHandle,
98             @NonNull Context context) {
99         if (!SdkLevel.isAtLeastV() || !android.os.Flags.allowPrivateProfile()) {
100             return false;
101         }
102         Context userContext = context.createContextAsUser(userHandle, /* flags= */ 0);
103         return userContext.getSystemService(UserManager.class).isPrivateProfile();
104     }
105 
106     /**
107      * Create a context for a user.
108      *
109      * @param context The context to clone
110      * @param user The user the new context should be for
111      *
112      * @return The context for the new user
113      */
114     @NonNull
getUserContext(@onNull Context context, @NonNull UserHandle user)115     public static Context getUserContext(@NonNull Context context, @NonNull UserHandle user) {
116         if (Process.myUserHandle().equals(user)) {
117             return context;
118         } else {
119             return context.createContextAsUser(user, 0);
120         }
121     }
122 }
123