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