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 android.annotation.UserIdInt;
19 import android.content.Context;
20 import com.android.settings.AccessiblePreferenceCategory;
21 import com.android.settingslib.RestrictedLockUtils;
22 import com.android.settingslib.RestrictedPreference;
23 import java.util.ArrayList;
24 
25 public class AccountRestrictionHelper {
26 
27     private final Context mContext;
28 
AccountRestrictionHelper(Context context)29     public AccountRestrictionHelper(Context context) {
30         mContext = context;
31     }
32 
33     /**
34      * Configure the UI of the preference by checking user restriction.
35      * @param preference The preference we are configuring.
36      * @param userRestriction The user restriction related to the preference.
37      * @param userId The user that we retrieve user restriction of.
38      */
enforceRestrictionOnPreference(RestrictedPreference preference, String userRestriction, @UserIdInt int userId)39     public void enforceRestrictionOnPreference(RestrictedPreference preference,
40         String userRestriction, @UserIdInt int userId) {
41         if (preference == null) {
42             return;
43         }
44         if (hasBaseUserRestriction(userRestriction, userId)) {
45             preference.setEnabled(false);
46         } else {
47             preference.checkRestrictionAndSetDisabled(userRestriction, userId);
48         }
49     }
50 
hasBaseUserRestriction(String userRestriction, @UserIdInt int userId)51     public boolean hasBaseUserRestriction(String userRestriction, @UserIdInt int userId) {
52         return RestrictedLockUtils.hasBaseUserRestriction(mContext, userRestriction, userId);
53     }
54 
createAccessiblePreferenceCategory(Context context)55     public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) {
56         return new AccessiblePreferenceCategory(context);
57     }
58 
59     /**
60      * Checks if the account should be shown based on the required authorities for the account type
61      * @param authorities given authority that is passed as activity extra
62      * @param auths list of authorities for particular account type
63      * @return true if the activity has the required authority to show the account
64      */
showAccount(String[] authorities, ArrayList<String> auths)65     public static boolean showAccount(String[] authorities, ArrayList<String> auths) {
66         boolean showAccount = true;
67         if (authorities != null && auths != null) {
68             showAccount = false;
69             for (String requestedAuthority : authorities) {
70                 if (auths.contains(requestedAuthority)) {
71                     return true;
72                 }
73             }
74         }
75         return showAccount;
76     }
77 }
78