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);
292 if (!launchedConfirmationActivity) {
293 updateStage(Stage.Introduction);
296 updateStage(Stage.Introduction);
299 // restore from previous state
300 final String patternString = savedInstanceState.getString(KEY_PATTERN_CHOICE);
301 if (patternString != null) {
302 mChosenPattern = LockPatternUtils.stringToPattern(patternString);
304 updateStage(Stage.values()[savedInstanceState.getInt(KEY_UI_STAGE)]);
309 * Keep all "find view" related stuff confined to this function since in
310 * case someone needs to subclass and customize.
312 protected void setupViews() {
313 setContentView(R.layout.choose_lock_pattern);
315 mHeaderText = (TextView) findViewById(R.id.headerText);
317 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
318 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
319 mLockPatternView.setTactileFeedbackEnabled(
320 mChooseLockSettingsHelper.utils().isTactileFeedbackEnabled());
322 mFooterText = (TextView) findViewById(R.id.footerText);
324 mFooterLeftButton = (TextView) findViewById(R.id.footerLeftButton);
325 mFooterRightButton = (TextView) findViewById(R.id.footerRightButton);
327 mFooterLeftButton.setOnClickListener(this);
328 mFooterRightButton.setOnClickListener(this);
331 public void onClick(View v) {
332 if (v == mFooterLeftButton) {
333 if (mUiStage.leftMode == LeftButtonMode.Retry) {
334 mChosenPattern = null;
335 mLockPatternView.clearPattern();
336 updateStage(Stage.Introduction);
337 } else if (mUiStage.leftMode == LeftButtonMode.Cancel) {
338 // They are canceling the entire wizard
339 setResult(RESULT_FINISHED);
342 throw new IllegalStateException("left footer button pressed, but stage of " +
343 mUiStage + " doesn't make sense");
345 } else if (v == mFooterRightButton) {
347 if (mUiStage.rightMode == RightButtonMode.Continue) {
348 if (mUiStage != Stage.FirstChoiceValid) {
349 throw new IllegalStateException("expected ui stage " + Stage.FirstChoiceValid
350 + " when button is " + RightButtonMode.Continue);
352 updateStage(Stage.NeedToConfirm);
353 } else if (mUiStage.rightMode == RightButtonMode.Confirm) {
354 if (mUiStage != Stage.ChoiceConfirmed) {
355 throw new IllegalStateException("expected ui stage " + Stage.ChoiceConfirmed
356 + " when button is " + RightButtonMode.Confirm);
358 saveChosenPatternAndFinish();
359 } else if (mUiStage.rightMode == RightButtonMode.Ok) {
360 if (mUiStage != Stage.HelpScreen) {
361 throw new IllegalStateException("Help screen is only mode with ok button, but " +
362 "stage is " + mUiStage);
364 mLockPatternView.clearPattern();
365 mLockPatternView.setDisplayMode(DisplayMode.Correct);
366 updateStage(Stage.Introduction);
372 public boolean onKeyDown(int keyCode, KeyEvent event) {
373 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
374 if (mUiStage == Stage.HelpScreen) {
375 updateStage(Stage.Introduction);
379 if (keyCode == KeyEvent.KEYCODE_MENU && mUiStage == Stage.Introduction) {
380 updateStage(Stage.HelpScreen);
384 return super.onKeyDown(keyCode, event);
388 protected void onSaveInstanceState(Bundle outState) {
389 super.onSaveInstanceState(outState);
391 outState.putInt(KEY_UI_STAGE, mUiStage.ordinal());
392 if (mChosenPattern != null) {
393 outState.putString(KEY_PATTERN_CHOICE, LockPatternUtils.patternToString(mChosenPattern));
399 * Updates the messages and buttons appropriate to what stage the user
400 * is at in choosing a view. This doesn't handle clearing out the pattern;
401 * the pattern is expected to be in the right state.
404 protected void updateStage(Stage stage) {
408 // header text, footer text, visibility and
409 // enabled state all known from the stage
410 if (stage == Stage.ChoiceTooShort) {
412 getResources().getString(
414 LockPatternUtils.MIN_LOCK_PATTERN_SIZE));
416 mHeaderText.setText(stage.headerMessage);
418 if (stage.footerMessage == ID_EMPTY_MESSAGE) {
419 mFooterText.setText("");
421 mFooterText.setText(stage.footerMessage);
424 if (stage.leftMode == LeftButtonMode.Gone) {
425 mFooterLeftButton.setVisibility(View.GONE);
427 mFooterLeftButton.setVisibility(View.VISIBLE);
428 mFooterLeftButton.setText(stage.leftMode.text);
429 mFooterLeftButton.setEnabled(stage.leftMode.enabled);
432 mFooterRightButton.setText(stage.rightMode.text);
433 mFooterRightButton.setEnabled(stage.rightMode.enabled);
435 // same for whether the patten is enabled
436 if (stage.patternEnabled) {
437 mLockPatternView.enableInput();
439 mLockPatternView.disableInput();
442 // the rest of the stuff varies enough that it is easier just to handle
443 // on a case by case basis.
444 mLockPatternView.setDisplayMode(DisplayMode.Correct);
448 mLockPatternView.clearPattern();
451 mLockPatternView.setPattern(DisplayMode.Animate, mAnimatePattern);
454 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
455 postClearPatternRunnable();
457 case FirstChoiceValid:
460 mLockPatternView.clearPattern();
463 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
464 postClearPatternRunnable();
466 case ChoiceConfirmed:
472 // clear the wrong pattern unless they have started a new one
474 private void postClearPatternRunnable() {
475 mLockPatternView.removeCallbacks(mClearPatternRunnable);
476 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
479 private void saveChosenPatternAndFinish() {
480 LockPatternUtils utils = mChooseLockSettingsHelper.utils();
481 final boolean lockVirgin = !utils.isPatternEverChosen();
483 utils.saveLockPattern(mChosenPattern);
484 utils.setLockPatternEnabled(true);
487 utils.setVisiblePatternEnabled(true);
488 utils.setTactileFeedbackEnabled(false);
491 setResult(RESULT_FINISHED);