• 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.packageinstaller.role.utils;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.Process;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 
28 import java.util.List;
29 import java.util.Objects;
30 
31 /**
32  * Utility methods about user.
33  */
34 public class UserUtils {
35 
UserUtils()36     private UserUtils() {}
37 
38     /**
39      * Check whether a user is a work profile.
40      *
41      * @param user the user to check
42      * @param context the {@code Context} to retrieve system services
43      *
44      * @return whether the user is a work profile
45      */
isWorkProfile(@onNull UserHandle user, @NonNull Context context)46     public static boolean isWorkProfile(@NonNull UserHandle user, @NonNull Context context) {
47         UserManager userManager = context.getSystemService(UserManager.class);
48         return userManager.isManagedProfile(user.getIdentifier());
49     }
50 
51     /**
52      * Get the work profile of current user, if any.
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
57      */
58     @Nullable
getWorkProfile(@onNull Context context)59     public static UserHandle getWorkProfile(@NonNull Context context) {
60         UserManager userManager = context.getSystemService(UserManager.class);
61         List<UserHandle> profiles = userManager.getUserProfiles();
62         UserHandle user = Process.myUserHandle();
63 
64         int profilesSize = profiles.size();
65         for (int i = 0; i < profilesSize; i++) {
66             UserHandle profile = profiles.get(i);
67 
68             if (Objects.equals(profile, user)) {
69                 continue;
70             }
71             if (!userManager.isManagedProfile(profile.getIdentifier())) {
72                 continue;
73             }
74             return profile;
75         }
76         return null;
77     }
78 
79     /**
80      * Create a context for a user.
81      *
82      * @param context The context to clone
83      * @param user The user the new context should be for
84      *
85      * @return The context for the new user
86      */
87     @NonNull
getUserContext(@onNull Context context, @NonNull UserHandle user)88     public static Context getUserContext(@NonNull Context context, @NonNull UserHandle user) {
89         if (Process.myUserHandle().equals(user)) {
90             return context;
91         } else {
92             try {
93                 return context.createPackageContextAsUser(context.getPackageName(), 0, user);
94             } catch (PackageManager.NameNotFoundException doesNotHappen) {
95                 throw new IllegalStateException(doesNotHappen);
96             }
97         }
98     }
99 }
100