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_TYPE = "type";
29    static final String EXTRA_KEY_PASSWORD = "password";
30
31    private LockPatternUtils mLockPatternUtils;
32    private Activity mActivity;
33    private Fragment mFragment;
34
35    public ChooseLockSettingsHelper(Activity activity) {
36        mActivity = activity;
37        mLockPatternUtils = new LockPatternUtils(activity);
38    }
39
40    public ChooseLockSettingsHelper(Activity activity, Fragment fragment) {
41        this(activity);
42        mFragment = fragment;
43    }
44
45    public LockPatternUtils utils() {
46        return mLockPatternUtils;
47    }
48
49    /**
50     * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
51     * @param message optional message to display about the action about to be done
52     * @param details optional detail message to display
53     * @return true if one exists and we launched an activity to confirm it
54     * @see #onActivityResult(int, int, android.content.Intent)
55     */
56    boolean launchConfirmationActivity(int request, CharSequence message, CharSequence details) {
57        return launchConfirmationActivity(request, message, details, false);
58    }
59
60    /**
61     * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
62     * @param message optional message to display about the action about to be done
63     * @param details optional detail message to display
64     * @param returnCredentials if true, put credentials into intent. Note that if this is true,
65                                this can only be called internally.
66     * @return true if one exists and we launched an activity to confirm it
67     * @see #onActivityResult(int, int, android.content.Intent)
68     */
69    boolean launchConfirmationActivity(int request, CharSequence message, CharSequence details,
70                                       boolean returnCredentials) {
71        boolean launched = false;
72        switch (mLockPatternUtils.getKeyguardStoredPasswordQuality()) {
73            case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
74                launched = confirmPattern(request, message, details, returnCredentials);
75                break;
76            case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
77            case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
78            case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
79            case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
80            case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
81                // TODO: update UI layout for ConfirmPassword to show message and details
82                launched = confirmPassword(request, message, returnCredentials);
83                break;
84        }
85        return launched;
86    }
87
88    /**
89     * Launch screen to confirm the existing lock pattern.
90     * @param message shown in header of ConfirmLockPattern if not null
91     * @param details shown in footer of ConfirmLockPattern if not null
92     * @param returnCredentials if true, put credentials into intent.
93     * @see #onActivityResult(int, int, android.content.Intent)
94     * @return true if we launched an activity to confirm pattern
95     */
96    private boolean confirmPattern(int request, CharSequence message,
97                                   CharSequence details, boolean returnCredentials) {
98        if (!mLockPatternUtils.isLockPatternEnabled() || !mLockPatternUtils.savedPatternExists()) {
99            return false;
100        }
101        final Intent intent = new Intent();
102        // supply header and footer text in the intent
103        intent.putExtra(ConfirmLockPattern.HEADER_TEXT, message);
104        intent.putExtra(ConfirmLockPattern.FOOTER_TEXT, details);
105        intent.setClassName("com.android.settings",
106                            returnCredentials
107                            ? ConfirmLockPattern.InternalActivity.class.getName()
108                            : ConfirmLockPattern.class.getName());
109        if (mFragment != null) {
110            mFragment.startActivityForResult(intent, request);
111        } else {
112            mActivity.startActivityForResult(intent, request);
113        }
114        return true;
115    }
116
117    /**
118     * Launch screen to confirm the existing lock password.
119     * @param message shown in header of ConfirmLockPassword if not null
120     * @param returnCredentials if true, put credentials into intent.
121     * @see #onActivityResult(int, int, android.content.Intent)
122     * @return true if we launched an activity to confirm password
123     */
124    private boolean confirmPassword(int request, CharSequence message,
125            boolean returnCredentials) {
126        if (!mLockPatternUtils.isLockPasswordEnabled()) return false;
127        final Intent intent = new Intent();
128        // supply header text in the intent
129        intent.putExtra(ConfirmLockPattern.HEADER_TEXT, message);
130        intent.setClassName("com.android.settings",
131                            returnCredentials
132                            ? ConfirmLockPassword.InternalActivity.class.getName()
133                            : ConfirmLockPassword.class.getName());
134        if (mFragment != null) {
135            mFragment.startActivityForResult(intent, request);
136        } else {
137            mActivity.startActivityForResult(intent, request);
138        }
139        return true;
140    }
141
142
143}
144