2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.annotation.Nullable;
20 import android.app.Activity;
21 import android.app.Fragment;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Intent;
24 import android.os.UserHandle;
26 import com.android.internal.widget.LockPatternUtils;
28 public final class ChooseLockSettingsHelper {
30 static final String EXTRA_KEY_TYPE = "type";
31 static final String EXTRA_KEY_PASSWORD = "password";
32 public static final String EXTRA_KEY_HAS_CHALLENGE = "has_challenge";
33 public static final String EXTRA_KEY_CHALLENGE = "challenge";
34 public static final String EXTRA_KEY_CHALLENGE_TOKEN = "hw_auth_token";
35 public static final String EXTRA_KEY_FOR_FINGERPRINT = "for_fingerprint";
38 private LockPatternUtils mLockPatternUtils;
39 private Activity mActivity;
40 private Fragment mFragment;
42 public ChooseLockSettingsHelper(Activity activity) {
44 mLockPatternUtils = new LockPatternUtils(activity);
47 public ChooseLockSettingsHelper(Activity activity, Fragment fragment) {
52 public LockPatternUtils utils() {
53 return mLockPatternUtils;
57 * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
59 * @param title title of the confirmation screen; shown in the action bar
60 * @return true if one exists and we launched an activity to confirm it
61 * @see Activity#onActivityResult(int, int, android.content.Intent)
63 boolean launchConfirmationActivity(int request, CharSequence title) {
64 return launchConfirmationActivity(request, title, null, null, false, false);
68 * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
70 * @param title title of the confirmation screen; shown in the action bar
71 * @param returnCredentials if true, put credentials into intent. Note that if this is true,
72 * this can only be called internally.
73 * @return true if one exists and we launched an activity to confirm it
74 * @see Activity#onActivityResult(int, int, android.content.Intent)
76 boolean launchConfirmationActivity(int request, CharSequence title, boolean returnCredentials) {
77 return launchConfirmationActivity(request, title, null, null, returnCredentials, false);
81 * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
83 * @param title title of the confirmation screen; shown in the action bar
84 * @param header header of the confirmation screen; shown as large text
85 * @param description description of the confirmation screen
86 * @param returnCredentials if true, put credentials into intent. Note that if this is true,
87 * this can only be called internally.
88 * @param external specifies whether this activity is launched externally, meaning that it will
89 * get a dark theme and allow fingerprint authentication
90 * @return true if one exists and we launched an activity to confirm it
91 * @see Activity#onActivityResult(int, int, android.content.Intent)
93 boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
94 @Nullable CharSequence header, @Nullable CharSequence description,
95 boolean returnCredentials, boolean external) {
96 return launchConfirmationActivity(request, title, header, description,
97 returnCredentials, external, false, 0);
101 * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
102 * @param message optional message to display about the action about to be done
103 * @param details optional detail message to display
104 * @param challenge a challenge to be verified against the device credential.
105 * This method can only be called internally.
106 * @return true if one exists and we launched an activity to confirm it
107 * @see #onActivityResult(int, int, android.content.Intent)
109 public boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
110 @Nullable CharSequence header, @Nullable CharSequence description,
112 return launchConfirmationActivity(request, title, header, description,
113 false, false, true, challenge);
116 private boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
117 @Nullable CharSequence header, @Nullable CharSequence description,
118 boolean returnCredentials, boolean external, boolean hasChallenge,
120 boolean launched = false;
122 int effectiveUserId = Utils.getEffectiveUserId(mActivity);
124 switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(effectiveUserId)) {
125 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
126 launched = launchConfirmationActivity(request, title, header, description,
127 returnCredentials || hasChallenge
128 ? ConfirmLockPattern.InternalActivity.class
129 : ConfirmLockPattern.class, external,
130 hasChallenge, challenge);
132 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
133 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
134 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
135 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
136 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
137 launched = launchConfirmationActivity(request, title, header, description,
138 returnCredentials || hasChallenge
139 ? ConfirmLockPassword.InternalActivity.class
140 : ConfirmLockPassword.class, external,
141 hasChallenge, challenge);
147 private boolean launchConfirmationActivity(int request, CharSequence title, CharSequence header,
148 CharSequence message, Class<?> activityClass, boolean external, boolean hasChallenge,
150 final Intent intent = new Intent();
151 intent.putExtra(ConfirmDeviceCredentialBaseFragment.TITLE_TEXT, title);
152 intent.putExtra(ConfirmDeviceCredentialBaseFragment.HEADER_TEXT, header);
153 intent.putExtra(ConfirmDeviceCredentialBaseFragment.DETAILS_TEXT, message);
154 intent.putExtra(ConfirmDeviceCredentialBaseFragment.ALLOW_FP_AUTHENTICATION, external);
155 intent.putExtra(ConfirmDeviceCredentialBaseFragment.DARK_THEME, external);
156 intent.putExtra(ConfirmDeviceCredentialBaseFragment.SHOW_CANCEL_BUTTON, external);
157 intent.putExtra(ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, external);
158 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, hasChallenge);
159 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
160 intent.setClassName(ConfirmDeviceCredentialBaseFragment.PACKAGE, activityClass.getName());
162 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
163 if (mFragment != null) {
164 mFragment.startActivity(intent);
166 mActivity.startActivity(intent);
169 if (mFragment != null) {
170 mFragment.startActivityForResult(intent, request);
172 mActivity.startActivityForResult(intent, request);