Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 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; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 20 | |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 21 | import com.android.settings.AccessiblePreferenceCategory; |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 22 | import com.android.settingslib.RestrictedLockUtilsInternal; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 23 | import com.android.settingslib.RestrictedPreference; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 24 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 25 | import java.util.ArrayList; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 26 | |
| 27 | public 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. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 54 | return RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, userRestriction, |
| 55 | userId); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) { |
| 59 | return new AccessiblePreferenceCategory(context); |
| 60 | } |
| 61 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 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 | */ |
| 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 Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 80 | } |