blob: 05b27aa6728396ade17bba646b2327f9c2e155e5 [file] [log] [blame]
Doris Lingbfac31b2016-11-10 15:12:52 -08001/*
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 */
16package com.android.settings.accounts;
17
18import android.annotation.UserIdInt;
19import android.content.Context;
Fan Zhangc7162cd2018-06-18 15:21:41 -070020
Doris Lingbfac31b2016-11-10 15:12:52 -080021import com.android.settings.AccessiblePreferenceCategory;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070022import com.android.settingslib.RestrictedLockUtilsInternal;
Doris Lingbfac31b2016-11-10 15:12:52 -080023import com.android.settingslib.RestrictedPreference;
Fan Zhangc7162cd2018-06-18 15:21:41 -070024
Doris Ling20d4b042016-11-22 16:37:06 -080025import java.util.ArrayList;
Doris Lingbfac31b2016-11-10 15:12:52 -080026
27public class AccountRestrictionHelper {
28
29 private final Context mContext;
30
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 */
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
53 public boolean hasBaseUserRestriction(String userRestriction, @UserIdInt int userId) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070054 return RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, userRestriction,
55 userId);
Doris Lingbfac31b2016-11-10 15:12:52 -080056 }
57
58 public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) {
59 return new AccessiblePreferenceCategory(context);
60 }
61
Doris Ling20d4b042016-11-22 16:37:06 -080062 /**
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 */
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 }
Doris Lingbfac31b2016-11-10 15:12:52 -080080}