• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.settings.applications.credentials;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 
25 import com.android.settings.Utils;
26 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
27 
28 import java.util.List;
29 
30 /** Handles user related utils. */
31 public class UserUtils {
32     public static final String EXTRA_IS_WORK_PROFILE = "isWorkProfile";
33     public static final String EXTRA_IS_PRIVATE_SPACE = "isPrivateSpace";
34 
35     /**
36      * Returns the managed profile of the current user or {@code null} if none is found or a profile
37      * exists but it is disabled.
38      */
39     @Nullable
getManagedProfile(UserManager userManager)40     public static UserHandle getManagedProfile(UserManager userManager) {
41         final List<UserHandle> userProfiles = userManager.getUserProfiles();
42         for (UserHandle profile : userProfiles) {
43             final UserInfo userInfo = userManager.getUserInfo(profile.getIdentifier());
44             if (userInfo.isManagedProfile()) {
45                 return profile;
46             }
47         }
48         return null;
49     }
50 
getUser(boolean isWorkProfile, boolean isPrivateSpace, Context context)51     public static int getUser(boolean isWorkProfile, boolean isPrivateSpace, Context context) {
52         int profileType = ProfileSelectFragment.ProfileType.PERSONAL;
53         if (isWorkProfile) {
54             profileType = ProfileSelectFragment.ProfileType.WORK;
55         } else if (isPrivateSpace) {
56             profileType = ProfileSelectFragment.ProfileType.PRIVATE;
57         }
58         // Get the user information of the tab we are trying to populate as the tab being populated
59         // might be different from the current userId
60         UserHandle userHandle = Utils.getProfileOfType(UserManager.get(context), profileType);
61         if (userHandle != null) {
62             return userHandle.getIdentifier();
63         }
64         return UserHandle.myUserId();
65     }
66 }
67