1/*
2 * Copyright (C) 2010 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
17package com.android.settings;
18
19import com.android.internal.widget.LockPatternUtils;
20
21import android.app.Activity;
22import android.app.Fragment;
23import android.app.admin.DevicePolicyManager;
24import android.content.Intent;
25
26public final class ChooseLockSettingsHelper {
27
28    static final String EXTRA_KEY_PASSWORD = "password";
29
30    private LockPatternUtils mLockPatternUtils;
31    private Activity mActivity;
32    private Fragment mFragment;
33
34    public ChooseLockSettingsHelper(Activity activity) {
35        mActivity = activity;
36        mLockPatternUtils = new LockPatternUtils(activity);
37    }
38
39    public ChooseLockSettingsHelper(Activity activity, Fragment fragment) {
40        this(activity);
41        mFragment = fragment;
42    }
43
44    public LockPatternUtils utils() {
45        return mLockPatternUtils;
46    }
47
48    /**
49     * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
50     * @param message optional message to display about the action about to be done
51     * @param details optional detail message to display
52     * @return true if one exists and we launched an activity to confirm it
53     * @see #onActivityResult(int, int, android.content.Intent)
54     */
55    boolean launchConfirmationActivity(int request, CharSequence message, CharSequence details) {
56        boolean launched = false;
57        switch (mLockPatternUtils.getKeyguardStoredPasswordQuality()) {
58            case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
59                launched = confirmPattern(request, message, details);
60                break;
61            case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
62            case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
63            case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
64            case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
65                // TODO: update UI layout for ConfirmPassword to show message and details
66                launched = confirmPassword(request);
67                break;
68        }
69        return launched;
70    }
71
72    /**
73     * Launch screen to confirm the existing lock pattern.
74     * @param message shown in header of ConfirmLockPattern if not null
75     * @param details shown in footer of ConfirmLockPattern if not null
76     * @see #onActivityResult(int, int, android.content.Intent)
77     * @return true if we launched an activity to confirm pattern
78     */
79    private boolean confirmPattern(int request, CharSequence message, CharSequence details) {
80        if (!mLockPatternUtils.isLockPatternEnabled() || !mLockPatternUtils.savedPatternExists()) {
81            return false;
82        }
83        final Intent intent = new Intent();
84        // supply header and footer text in the intent
85        intent.putExtra(ConfirmLockPattern.HEADER_TEXT, message);
86        intent.putExtra(ConfirmLockPattern.FOOTER_TEXT, details);
87        intent.setClassName("com.android.settings", "com.android.settings.ConfirmLockPattern");
88        if (mFragment != null) {
89            mFragment.startActivityForResult(intent, request);
90        } else {
91            mActivity.startActivityForResult(intent, request);
92        }
93        return true;
94    }
95
96    /**
97     * Launch screen to confirm the existing lock password.
98     * @see #onActivityResult(int, int, android.content.Intent)
99     * @return true if we launched an activity to confirm password
100     */
101    private boolean confirmPassword(int request) {
102        if (!mLockPatternUtils.isLockPasswordEnabled()) return false;
103        final Intent intent = new Intent();
104        intent.setClassName("com.android.settings", "com.android.settings.ConfirmLockPassword");
105        if (mFragment != null) {
106            mFragment.startActivityForResult(intent, request);
107        } else {
108            mActivity.startActivityForResult(intent, request);
109        }
110        return true;
111    }
112
113
114}
115