2 * Copyright (C) 2007 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 com.google.android.collect.Lists;
21 import com.android.internal.widget.LinearLayoutWithDefaultTouchRecepient;
22 import com.android.internal.widget.LockPatternUtils;
23 import com.android.internal.widget.LockPatternView;
24 import com.android.internal.widget.LockPatternView.Cell;
25 import static com.android.internal.widget.LockPatternView.DisplayMode;
27 import android.app.Activity;
28 import android.content.Intent;
29 import android.os.Bundle;
30 import android.view.KeyEvent;
31 import android.view.View;
32 import android.view.Window;
33 import android.widget.TextView;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
40 * If the user has a lock pattern set already, makes them confirm the existing one.
42 * Then, prompts the user to choose a lock pattern:
43 * - prompts for initial pattern
44 * - asks for confirmation / restart
45 * - saves chosen password when confirmed
47 public class ChooseLockPattern extends Activity implements View.OnClickListener{
49 * Used by the choose lock pattern wizard to indicate the wizard is
50 * finished, and each activity in the wizard should finish.
52 * Previously, each activity in the wizard would finish itself after
53 * starting the next activity. However, this leads to broken 'Back'
54 * behavior. So, now an activity does not finish itself until it gets this
57 static final int RESULT_FINISHED = RESULT_FIRST_USER;
59 public static final int CONFIRM_EXISTING_REQUEST = 55;
61 // how long after a confirmation message is shown before moving on
62 static final int INFORMATION_MSG_TIMEOUT_MS = 3000;
64 // how long we wait to clear a wrong pattern
65 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 2000;
67 private static final int ID_EMPTY_MESSAGE = -1;
69 protected TextView mHeaderText;
70 protected LockPatternView mLockPatternView;
71 protected TextView mFooterText;
72 private TextView mFooterLeftButton;
73 private TextView mFooterRightButton;
74 protected List<LockPatternView.Cell> mChosenPattern = null;
77 * The patten used during the help screen to show how to draw a pattern.
79 private final List<LockPatternView.Cell> mAnimatePattern =
80 Collections.unmodifiableList(Lists.newArrayList(
81 LockPatternView.Cell.of(0, 0),
82 LockPatternView.Cell.of(0, 1),
83 LockPatternView.Cell.of(1, 1),
84 LockPatternView.Cell.of(2, 1)
88 protected void onActivityResult(int requestCode, int resultCode,
90 super.onActivityResult(requestCode, resultCode, data);
91 switch (requestCode) {
92 case CONFIRM_EXISTING_REQUEST:
93 if (resultCode != Activity.RESULT_OK) {
94 setResult(RESULT_FINISHED);
97 updateStage(Stage.Introduction);
103 * The pattern listener that responds according to a user choosing a new
106 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener = new LockPatternView.OnPatternListener() {
108 public void onPatternStart() {
109 mLockPatternView.removeCallbacks(mClearPatternRunnable);
113 public void onPatternCleared() {
114 mLockPatternView.removeCallbacks(mClearPatternRunnable);
117 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
118 if (mUiStage == Stage.NeedToConfirm || mUiStage == Stage.ConfirmWrong) {
119 if (mChosenPattern == null) throw new IllegalStateException("null chosen pattern in stage 'need to confirm");
120 if (mChosenPattern.equals(pattern)) {
121 updateStage(Stage.ChoiceConfirmed);
123 updateStage(Stage.ConfirmWrong);
125 } else if (mUiStage == Stage.Introduction || mUiStage == Stage.ChoiceTooShort){
126 if (pattern.size() < LockPatternUtils.MIN_LOCK_PATTERN_SIZE) {
127 updateStage(Stage.ChoiceTooShort);
129 mChosenPattern = new ArrayList<LockPatternView.Cell>(pattern);
130 updateStage(Stage.FirstChoiceValid);
133 throw new IllegalStateException("Unexpected stage " + mUiStage + " when "
134 + "entering the pattern.");
138 public void onPatternCellAdded(List<Cell> pattern) {
142 private void patternInProgress() {
143 mHeaderText.setText(R.string.lockpattern_recording_inprogress);
144 mFooterText.setText("");
145 mFooterLeftButton.setEnabled(false);
146 mFooterRightButton.setEnabled(false);
152 * The states of the left footer button.
154 enum LeftButtonMode {
155 Cancel(R.string.cancel, true),
156 CancelDisabled(R.string.cancel, false),
157 Retry(R.string.lockpattern_retry_button_text, true),
158 RetryDisabled(R.string.lockpattern_retry_button_text, false),
159 Gone(ID_EMPTY_MESSAGE, false);
163 * @param text The displayed text for this mode.
164 * @param enabled Whether the button should be enabled.
166 LeftButtonMode(int text, boolean enabled) {
168 this.enabled = enabled;
172 final boolean enabled;
176 * The states of the right button.
178 enum RightButtonMode {
179 Continue(R.string.lockpattern_continue_button_text, true),
180 ContinueDisabled(R.string.lockpattern_continue_button_text, false),
181 Confirm(R.string.lockpattern_confirm_button_text, true),
182 ConfirmDisabled(R.string.lockpattern_confirm_button_text, false),
183 Ok(android.R.string.ok, true);
186 * @param text The displayed text for this mode.
187 * @param enabled Whether the button should be enabled.
189 RightButtonMode(int text, boolean enabled) {
191 this.enabled = enabled;
195 final boolean enabled;
199 * Keep track internally of where the user is in choosing a pattern.
201 protected enum Stage {
204 R.string.lockpattern_recording_intro_header,
205 LeftButtonMode.Cancel, RightButtonMode.ContinueDisabled,
206 R.string.lockpattern_recording_intro_footer, true),
208 R.string.lockpattern_settings_help_how_to_record,
209 LeftButtonMode.Gone, RightButtonMode.Ok, ID_EMPTY_MESSAGE, false),
211 R.string.lockpattern_recording_incorrect_too_short,
212 LeftButtonMode.Retry, RightButtonMode.ContinueDisabled,
213 ID_EMPTY_MESSAGE, true),
215 R.string.lockpattern_pattern_entered_header,
216 LeftButtonMode.Retry, RightButtonMode.Continue, ID_EMPTY_MESSAGE, false),
218 R.string.lockpattern_need_to_confirm,
219 LeftButtonMode.CancelDisabled, RightButtonMode.ConfirmDisabled,
220 ID_EMPTY_MESSAGE, true),
222 R.string.lockpattern_need_to_unlock_wrong,
223 LeftButtonMode.Cancel, RightButtonMode.ConfirmDisabled,
224 ID_EMPTY_MESSAGE, true),
226 R.string.lockpattern_pattern_confirmed_header,
227 LeftButtonMode.Cancel, RightButtonMode.Confirm, ID_EMPTY_MESSAGE, false);
231 * @param headerMessage The message displayed at the top.
232 * @param leftMode The mode of the left button.
233 * @param rightMode The mode of the right button.
234 * @param footerMessage The footer message.
235 * @param patternEnabled Whether the pattern widget is enabled.
237 Stage(int headerMessage,
238 LeftButtonMode leftMode,
239 RightButtonMode rightMode,
240 int footerMessage, boolean patternEnabled) {
241 this.headerMessage = headerMessage;
242 this.leftMode = leftMode;
243 this.rightMode = rightMode;
244 this.footerMessage = footerMessage;
245 this.patternEnabled = patternEnabled;
248 final int headerMessage;
249 final LeftButtonMode leftMode;
250 final RightButtonMode rightMode;
251 final int footerMessage;
252 final boolean patternEnabled;
255 private Stage mUiStage = Stage.Introduction;
257 private Runnable mClearPatternRunnable = new Runnable() {
259 mLockPatternView.clearPattern();
263 private ChooseLockSettingsHelper mChooseLockSettingsHelper;
265 private static final String KEY_UI_STAGE = "uiStage";
266 private static final String KEY_PATTERN_CHOICE = "chosenPattern";
269 protected void onCreate(Bundle savedInstanceState) {
270 super.onCreate(savedInstanceState);
271 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this);
272 requestWindowFeature(Window.FEATURE_NO_TITLE);
276 // make it so unhandled touch events within the unlock screen go to the
277 // lock pattern view.
278 final LinearLayoutWithDefaultTouchRecepient topLayout
279 = (LinearLayoutWithDefaultTouchRecepient) findViewById(
281 topLayout.setDefaultTouchRecepient(mLockPatternView);
283 final boolean confirmCredentials = getIntent().getBooleanExtra("confirm_credentials", true);
285 if (savedInstanceState == null) {
286 if (confirmCredentials) {
287 // first launch. As a security measure, we're in NeedToConfirm mode until we know
288 // there isn't an existing password or the user confirms their password.
289 updateStage(Stage.NeedToConfirm);
290 boolean launchedConfirmationActivity =
291 mChooseLockSettingsHelper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST,
293 if (!launchedConfirmationActivity) {
294 updateStage(Stage.Introduction);
297 updateStage(Stage.Introduction);
300 // restore from previous state
301 final String patternString = savedInstanceState.getString(KEY_PATTERN_CHOICE);
302 if (patternString != null) {
303 mChosenPattern = LockPatternUtils.stringToPattern(patternString);
305 updateStage(Stage.values()[savedInstanceState.getInt(KEY_UI_STAGE)]);
310 * Keep all "find view" related stuff confined to this function since in
311 * case someone needs to subclass and customize.
313 protected void setupViews() {
314 setContentView(R.layout.choose_lock_pattern);
316 mHeaderText = (TextView) findViewById(R.id.headerText);
318 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
319 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
320 mLockPatternView.setTactileFeedbackEnabled(
321 mChooseLockSettingsHelper.utils().isTactileFeedbackEnabled());
323 mFooterText = (TextView) findViewById(R.id.footerText);
325 mFooterLeftButton = (TextView) findViewById(R.id.footerLeftButton);
326 mFooterRightButton = (TextView) findViewById(R.id.footerRightButton);
328 mFooterLeftButton.setOnClickListener(this);
329 mFooterRightButton.setOnClickListener(this);
332 public void onClick(View v) {
333 if (v == mFooterLeftButton) {
334 if (mUiStage.leftMode == LeftButtonMode.Retry) {
335 mChosenPattern = null;
336 mLockPatternView.clearPattern();
337 updateStage(Stage.Introduction);
338 } else if (mUiStage.leftMode == LeftButtonMode.Cancel) {
339 // They are canceling the entire wizard
340 setResult(RESULT_FINISHED);
343 throw new IllegalStateException("left footer button pressed, but stage of " +
344 mUiStage + " doesn't make sense");
346 } else if (v == mFooterRightButton) {
348 if (mUiStage.rightMode == RightButtonMode.Continue) {
349 if (mUiStage != Stage.FirstChoiceValid) {
350 throw new IllegalStateException("expected ui stage " + Stage.FirstChoiceValid
351 + " when button is " + RightButtonMode.Continue);
353 updateStage(Stage.NeedToConfirm);
354 } else if (mUiStage.rightMode == RightButtonMode.Confirm) {
355 if (mUiStage != Stage.ChoiceConfirmed) {
356 throw new IllegalStateException("expected ui stage " + Stage.ChoiceConfirmed
357 + " when button is " + RightButtonMode.Confirm);
359 saveChosenPatternAndFinish();
360 } else if (mUiStage.rightMode == RightButtonMode.Ok) {
361 if (mUiStage != Stage.HelpScreen) {
362 throw new IllegalStateException("Help screen is only mode with ok button, but " +
363 "stage is " + mUiStage);
365 mLockPatternView.clearPattern();
366 mLockPatternView.setDisplayMode(DisplayMode.Correct);
367 updateStage(Stage.Introduction);
373 public boolean onKeyDown(int keyCode, KeyEvent event) {
374 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
375 if (mUiStage == Stage.HelpScreen) {
376 updateStage(Stage.Introduction);
380 if (keyCode == KeyEvent.KEYCODE_MENU && mUiStage == Stage.Introduction) {
381 updateStage(Stage.HelpScreen);
385 return super.onKeyDown(keyCode, event);
389 protected void onSaveInstanceState(Bundle outState) {
390 super.onSaveInstanceState(outState);
392 outState.putInt(KEY_UI_STAGE, mUiStage.ordinal());
393 if (mChosenPattern != null) {
394 outState.putString(KEY_PATTERN_CHOICE, LockPatternUtils.patternToString(mChosenPattern));
400 * Updates the messages and buttons appropriate to what stage the user
401 * is at in choosing a view. This doesn't handle clearing out the pattern;
402 * the pattern is expected to be in the right state.
405 protected void updateStage(Stage stage) {
409 // header text, footer text, visibility and
410 // enabled state all known from the stage
411 if (stage == Stage.ChoiceTooShort) {
413 getResources().getString(
415 LockPatternUtils.MIN_LOCK_PATTERN_SIZE));
417 mHeaderText.setText(stage.headerMessage);
419 if (stage.footerMessage == ID_EMPTY_MESSAGE) {
420 mFooterText.setText("");
422 mFooterText.setText(stage.footerMessage);
425 if (stage.leftMode == LeftButtonMode.Gone) {
426 mFooterLeftButton.setVisibility(View.GONE);
428 mFooterLeftButton.setVisibility(View.VISIBLE);
429 mFooterLeftButton.setText(stage.leftMode.text);
430 mFooterLeftButton.setEnabled(stage.leftMode.enabled);
433 mFooterRightButton.setText(stage.rightMode.text);
434 mFooterRightButton.setEnabled(stage.rightMode.enabled);
436 // same for whether the patten is enabled
437 if (stage.patternEnabled) {
438 mLockPatternView.enableInput();
440 mLockPatternView.disableInput();
443 // the rest of the stuff varies enough that it is easier just to handle
444 // on a case by case basis.
445 mLockPatternView.setDisplayMode(DisplayMode.Correct);
449 mLockPatternView.clearPattern();
452 mLockPatternView.setPattern(DisplayMode.Animate, mAnimatePattern);
455 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
456 postClearPatternRunnable();
458 case FirstChoiceValid:
461 mLockPatternView.clearPattern();
464 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
465 postClearPatternRunnable();
467 case ChoiceConfirmed:
473 // clear the wrong pattern unless they have started a new one
475 private void postClearPatternRunnable() {
476 mLockPatternView.removeCallbacks(mClearPatternRunnable);
477 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
480 private void saveChosenPatternAndFinish() {
481 LockPatternUtils utils = mChooseLockSettingsHelper.utils();
482 final boolean lockVirgin = !utils.isPatternEverChosen();
484 utils.saveLockPattern(mChosenPattern);
485 utils.setLockPatternEnabled(true);
488 utils.setVisiblePatternEnabled(true);
489 utils.setTactileFeedbackEnabled(false);
492 setResult(RESULT_FINISHED);