1bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling/*
2bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * Copyright (C) 2016 The Android Open Source Project
3bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling *
4bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * Licensed under the Apache License, Version 2.0 (the "License");
5bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * you may not use this file except in compliance with the License.
6bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * You may obtain a copy of the License at
7bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling *
8bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling *      http://www.apache.org/licenses/LICENSE-2.0
9bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling *
10bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * Unless required by applicable law or agreed to in writing, software
11bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * distributed under the License is distributed on an "AS IS" BASIS,
12bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * See the License for the specific language governing permissions and
14bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling * limitations under the License.
15bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling */
16bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingpackage com.android.settings.accounts;
17bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
18bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingimport android.annotation.UserIdInt;
19bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingimport android.content.Context;
20bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingimport com.android.settings.AccessiblePreferenceCategory;
21bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingimport com.android.settingslib.RestrictedLockUtils;
22bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingimport com.android.settingslib.RestrictedPreference;
2320d4b041f7870796f4f186729add8cd1b42f2dddDoris Lingimport java.util.ArrayList;
24bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
25bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Lingpublic class AccountRestrictionHelper {
26bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
27bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    private final Context mContext;
28bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
29bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    public AccountRestrictionHelper(Context context) {
30bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        mContext = context;
31bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    }
32bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
33bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    /**
34bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling     * Configure the UI of the preference by checking user restriction.
35bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling     * @param preference The preference we are configuring.
36bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling     * @param userRestriction The user restriction related to the preference.
37bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling     * @param userId The user that we retrieve user restriction of.
38bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling     */
39bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    public void enforceRestrictionOnPreference(RestrictedPreference preference,
40bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        String userRestriction, @UserIdInt int userId) {
41bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        if (preference == null) {
42bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling            return;
43bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        }
44bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        if (hasBaseUserRestriction(userRestriction, userId)) {
45bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling            preference.setEnabled(false);
46bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        } else {
47bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling            preference.checkRestrictionAndSetDisabled(userRestriction, userId);
48bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        }
49bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    }
50bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
51bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    public boolean hasBaseUserRestriction(String userRestriction, @UserIdInt int userId) {
52bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        return RestrictedLockUtils.hasBaseUserRestriction(mContext, userRestriction, userId);
53bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    }
54bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
55bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) {
56bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling        return new AccessiblePreferenceCategory(context);
57bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling    }
58bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling
5920d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling    /**
6020d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling     * Checks if the account should be shown based on the required authorities for the account type
6120d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling     * @param authorities given authority that is passed as activity extra
6220d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling     * @param auths list of authorities for particular account type
6320d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling     * @return true if the activity has the required authority to show the account
6420d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling     */
6520d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling    public static boolean showAccount(String[] authorities, ArrayList<String> auths) {
6620d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling        boolean showAccount = true;
6720d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling        if (authorities != null && auths != null) {
6820d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling            showAccount = false;
6920d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling            for (String requestedAuthority : authorities) {
7020d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling                if (auths.contains(requestedAuthority)) {
7120d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling                    return true;
7220d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling                }
7320d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling            }
7420d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling        }
7520d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling        return showAccount;
7620d4b041f7870796f4f186729add8cd1b42f2dddDoris Ling    }
77bfac31b517e1fa2f217fe932192ecd0c57b33525Doris Ling}
78