1edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam/*
2edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * Copyright (C) 2017 The Android Open Source Project
3edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam *
4edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
5edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * you may not use this file except in compliance with the License.
6edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * You may obtain a copy of the License at
7edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam *
8edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
9edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam *
10edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * Unless required by applicable law or agreed to in writing, software
11edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
12edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * See the License for the specific language governing permissions and
14edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * limitations under the License.
15edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam */
16edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
17edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lampackage com.android.settings.password;
18edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
19edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport android.app.admin.DevicePolicyManager;
20edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport android.content.Context;
21edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport android.os.UserHandle;
22edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport android.support.annotation.NonNull;
23edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport android.support.annotation.VisibleForTesting;
24edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
25edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport com.android.settings.R;
26edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
27edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport java.util.ArrayList;
28edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport java.util.List;
29edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
30edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam/**
31edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * A controller for ChooseLockGeneric, and other similar classes which shows a list of possible
32edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam * screen locks for the user to choose from.
33edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam */
34edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lampublic class ChooseLockGenericController {
35edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
36edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    private final Context mContext;
37edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    private final int mUserId;
38edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    private ManagedLockPasswordProvider mManagedPasswordProvider;
39edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    private DevicePolicyManager mDpm;
40edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
41edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public ChooseLockGenericController(Context context, int userId) {
42edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        this(
43edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                context,
44edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                userId,
45edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                context.getSystemService(DevicePolicyManager.class),
46edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                ManagedLockPasswordProvider.get(context, userId));
47edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
48edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
49edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    @VisibleForTesting
50edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    ChooseLockGenericController(
51edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            Context context,
52edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            int userId,
53edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            DevicePolicyManager dpm,
54edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            ManagedLockPasswordProvider managedLockPasswordProvider) {
55edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        mContext = context;
56edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        mUserId = userId;
57edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        mManagedPasswordProvider = managedLockPasswordProvider;
58edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        mDpm = dpm;
59edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
60edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
61edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    /**
62edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @return The higher quality of either the specified {@code quality} or the quality required
63edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *         by {@link DevicePolicyManager#getPasswordQuality}.
64edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     */
65edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public int upgradeQuality(int quality) {
66edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        // Compare min allowed password quality
67edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        return Math.max(quality, mDpm.getPasswordQuality(null, mUserId));
68edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
69edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
70edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    /**
71edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * Whether the given screen lock type should be visible in the given context.
72edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     */
73edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public boolean isScreenLockVisible(ScreenLockType type) {
74edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        switch (type) {
75edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case NONE:
76edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return !mContext.getResources().getBoolean(R.bool.config_hide_none_security_option);
77edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case SWIPE:
78edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return !mContext.getResources().getBoolean(R.bool.config_hide_swipe_security_option)
79edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                    // Swipe doesn't make sense for profiles.
80edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                    && mUserId == UserHandle.myUserId();
81edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case MANAGED:
82edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mManagedPasswordProvider.isManagedPasswordChoosable();
83edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        }
84edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        return true;
85edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
86edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
87edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    /**
88edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * Whether screen lock with {@code type} should be enabled.
89edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *
90edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @param type The screen lock type.
91edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @param quality The minimum required quality. This can either be requirement by device policy
92edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *                manager or because some flow only makes sense with secure lock screens.
93edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     */
94edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public boolean isScreenLockEnabled(ScreenLockType type, int quality) {
95edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        return type.maxQuality >= quality;
96edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
97edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
98edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    /**
99edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * Whether screen lock with {@code type} is disabled by device policy admin.
100edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *
101edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @param type The screen lock type.
102edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @param adminEnforcedQuality The minimum quality that the admin enforces.
103edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     */
104edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public boolean isScreenLockDisabledByAdmin(ScreenLockType type, int adminEnforcedQuality) {
105edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        boolean disabledByAdmin = type.maxQuality < adminEnforcedQuality;
106edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        if (type == ScreenLockType.MANAGED) {
107edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            disabledByAdmin = disabledByAdmin
108edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                    || !mManagedPasswordProvider.isManagedPasswordChoosable();
109edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        }
110edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        return disabledByAdmin;
111edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
112edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
113edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    /**
114edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * User friendly title for the given screen lock type.
115edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     */
116edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public CharSequence getTitle(ScreenLockType type) {
117edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        switch (type) {
118edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case NONE:
119edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mContext.getText(R.string.unlock_set_unlock_off_title);
120edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case SWIPE:
121edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mContext.getText(R.string.unlock_set_unlock_none_title);
122edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case PATTERN:
123edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mContext.getText(R.string.unlock_set_unlock_pattern_title);
124edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case PIN:
125edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mContext.getText(R.string.unlock_set_unlock_pin_title);
126edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case PASSWORD:
127edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mContext.getText(R.string.unlock_set_unlock_password_title);
128edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            case MANAGED:
129edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                return mManagedPasswordProvider.getPickerOptionTitle(false);
130edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        }
131edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        return null;
132edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
133edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
134edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    /**
135edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * Gets a list of screen locks that should be visible for the given quality. The returned list
136edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * is ordered in the natural order of the enum (the order those enums were defined).
137edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *
138edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @param quality The minimum quality required in the context of the current flow. This should
139edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *                be one of the constants defined in
140edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *                {@code DevicePolicyManager#PASSWORD_QUALITY_*}.
141edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     * @param includeDisabled Whether to include screen locks disabled by {@code quality}
142edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     *                        requirements in the returned list.
143edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam     */
144edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    @NonNull
145edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    public List<ScreenLockType> getVisibleScreenLockTypes(int quality, boolean includeDisabled) {
146edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        int upgradedQuality = upgradeQuality(quality);
147edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        List<ScreenLockType> locks = new ArrayList<>();
148edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        // EnumSet's iterator guarantees the natural order of the enums
149edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        for (ScreenLockType lock : ScreenLockType.values()) {
150edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            if (isScreenLockVisible(lock)) {
151edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                if (includeDisabled || isScreenLockEnabled(lock, upgradedQuality)) {
152edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                    locks.add(lock);
153edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam                }
154edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            }
155edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        }
156edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        return locks;
157edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam    }
158edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam}
159