1 /*
2  * Copyright (C) 2016 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 package com.android.settings.accounts;
17 
18 import static android.os.UserManager.DISALLOW_REMOVE_MANAGED_PROFILE;
19 
20 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
21 
22 import android.annotation.UserIdInt;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.pm.UserInfo;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 
30 import com.android.settings.AccessiblePreferenceCategory;
31 import com.android.settingslib.RestrictedLockUtilsInternal;
32 import com.android.settingslib.RestrictedPreference;
33 
34 import java.util.ArrayList;
35 
36 public class AccountRestrictionHelper {
37 
38     private final Context mContext;
39 
AccountRestrictionHelper(Context context)40     public AccountRestrictionHelper(Context context) {
41         mContext = context;
42     }
43 
44     /**
45      * Configure the UI of the preference by checking user restriction.
46      * @param preference The preference we are configuring.
47      * @param userRestriction The user restriction related to the preference.
48      * @param userId The user that we retrieve user restriction of.
49      */
enforceRestrictionOnPreference(RestrictedPreference preference, String userRestriction, @UserIdInt int userId)50     public void enforceRestrictionOnPreference(RestrictedPreference preference,
51         String userRestriction, @UserIdInt int userId) {
52         if (preference == null) {
53             return;
54         }
55         if (hasBaseUserRestriction(userRestriction, userId)) {
56             if (userRestriction.equals(DISALLOW_REMOVE_MANAGED_PROFILE)
57                     && isOrganizationOwnedDevice()) {
58                 preference.setDisabledByAdmin(getEnforcedAdmin(userRestriction, userId));
59             } else {
60                 preference.setEnabled(false);
61             }
62         } else {
63             preference.checkRestrictionAndSetDisabled(userRestriction, userId);
64         }
65     }
66 
hasBaseUserRestriction(String userRestriction, @UserIdInt int userId)67     public boolean hasBaseUserRestriction(String userRestriction, @UserIdInt int userId) {
68         return RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, userRestriction,
69                 userId);
70     }
71 
isOrganizationOwnedDevice()72     private boolean isOrganizationOwnedDevice() {
73         final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
74                 Context.DEVICE_POLICY_SERVICE);
75         if (dpm == null) {
76             return false;
77         }
78         return dpm.isOrganizationOwnedDeviceWithManagedProfile();
79     }
80 
getEnforcedAdmin(String userRestriction, int userId)81     private EnforcedAdmin getEnforcedAdmin(String userRestriction, int userId) {
82         final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
83                 Context.DEVICE_POLICY_SERVICE);
84         if (dpm == null) {
85             return null;
86         }
87         final int managedUsedId = getManagedUserId(userId);
88         ComponentName adminComponent = dpm.getProfileOwnerAsUser(managedUsedId);
89         if (adminComponent != null) {
90             return new EnforcedAdmin(adminComponent, userRestriction,
91                     UserHandle.of(managedUsedId));
92         }
93         return null;
94     }
95 
getManagedUserId(int userId)96     private int getManagedUserId(int userId) {
97         final UserManager um = UserManager.get(mContext);
98         for (UserInfo ui : um.getProfiles(userId)) {
99             if (ui.id == userId || !ui.isManagedProfile()) {
100                 continue;
101             }
102             return ui.id;
103         }
104         return -1;
105     }
106 
createAccessiblePreferenceCategory(Context context)107     public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) {
108         return new AccessiblePreferenceCategory(context);
109     }
110 
111     /**
112      * Checks if the account should be shown based on the required authorities for the account type
113      * @param authorities given authority that is passed as activity extra
114      * @param auths list of authorities for particular account type
115      * @return true if the activity has the required authority to show the account
116      */
showAccount(String[] authorities, ArrayList<String> auths)117     public static boolean showAccount(String[] authorities, ArrayList<String> auths) {
118         boolean showAccount = true;
119         if (authorities != null && auths != null) {
120             showAccount = false;
121             for (String requestedAuthority : authorities) {
122                 if (auths.contains(requestedAuthority)) {
123                     return true;
124                 }
125             }
126         }
127         return showAccount;
128     }
129 }
130