blob: 4de0e448edba3fad23ed7daf3044aaed37e67f96 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2008 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.AlertDialog;
23import android.content.Context;
24import android.content.Intent;
25import android.os.Bundle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.os.ServiceManager;
27import android.os.SystemProperties;
28import android.text.TextUtils;
29import android.util.Log;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.widget.Button;
33
34/**
35 * Confirm and execute a reset of the device to a clean "just out of the box"
36 * state. Multiple confirmations are required: first, a general "are you sure
37 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
38 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
39 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
40 * locked, et cetera, then the confirmation sequence is abandoned.
41 */
42public class MasterClear extends Activity {
43
44 private static final int KEYGUARD_REQUEST = 55;
45
46 private LayoutInflater mInflater;
47 private LockPatternUtils mLockUtils;
48
49 private View mInitialView;
50 private Button mInitiateButton;
51
52 private View mFinalView;
53 private Button mFinalButton;
54
Jim Miller47d380f2010-01-20 13:37:14 -080055 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056 * The user has gone through the multiple confirmation, so now we go ahead
57 * and invoke the Checkin Service to reset the device to its factory-default
58 * state (rebooting in the process).
59 */
60 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
61 public void onClick(View v) {
Ying Wanga7188322010-01-04 18:45:10 -080062 if (Utils.isMonkeyRunning()) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063 return;
64 }
Ying Wang50cb76f2010-01-04 12:04:25 -080065
Dan Egnor3352d102010-02-10 19:29:47 -080066 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
67 // Intent handling is asynchronous -- assume it will happen soon.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 }
69 };
70
71 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070072 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070074 * @param request the request code to be returned once confirmation finishes
75 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076 */
Jim Miller2deec7e2010-04-13 17:43:36 -070077 private boolean runKeyguardConfirmation(int request) {
78 return new ChooseLockSettingsHelper(this)
79 .launchConfirmationActivity(request,
80 getText(R.string.master_clear_gesture_prompt),
81 getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 }
83
84 @Override
85 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
86 super.onActivityResult(requestCode, resultCode, data);
87
88 if (requestCode != KEYGUARD_REQUEST) {
89 return;
90 }
91
92 // If the user entered a valid keyguard trace, present the final
93 // confirmation prompt; otherwise, go back to the initial state.
94 if (resultCode == Activity.RESULT_OK) {
95 establishFinalConfirmationState();
Jim Miller2deec7e2010-04-13 17:43:36 -070096 } else if (resultCode == Activity.RESULT_CANCELED) {
97 finish();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098 } else {
99 establishInitialState();
100 }
101 }
102
103 /**
104 * If the user clicks to begin the reset sequence, we next require a
105 * keyguard confirmation if the user has currently enabled one. If there
106 * is no keyguard available, we simply go to the final confirmation prompt.
107 */
108 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
109 public void onClick(View v) {
Jim Miller2deec7e2010-04-13 17:43:36 -0700110 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111 establishFinalConfirmationState();
112 }
113 }
114 };
115
116 /**
117 * Configure the UI for the final confirmation interaction
118 */
119 private void establishFinalConfirmationState() {
120 if (mFinalView == null) {
121 mFinalView = mInflater.inflate(R.layout.master_clear_final, null);
122 mFinalButton =
123 (Button) mFinalView.findViewById(R.id.execute_master_clear);
124 mFinalButton.setOnClickListener(mFinalClickListener);
125 }
126
127 setContentView(mFinalView);
128 }
129
130 /**
131 * In its initial state, the activity presents a button for the user to
132 * click in order to initiate a confirmation sequence. This method is
133 * called from various other points in the code to reset the activity to
134 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800135 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136 * <p>Reinflating views from resources is expensive and prevents us from
137 * caching widget pointers, so we use a single-inflate pattern: we lazy-
138 * inflate each view, caching all of the widget pointers we'll need at the
139 * time, then simply reuse the inflated views directly whenever we need
140 * to change contents.
141 */
142 private void establishInitialState() {
143 if (mInitialView == null) {
144 mInitialView = mInflater.inflate(R.layout.master_clear_primary, null);
145 mInitiateButton =
146 (Button) mInitialView.findViewById(R.id.initiate_master_clear);
147 mInitiateButton.setOnClickListener(mInitiateListener);
148 }
149
150 setContentView(mInitialView);
151 }
152
153 @Override
154 protected void onCreate(Bundle savedState) {
155 super.onCreate(savedState);
156
157 mInitialView = null;
158 mFinalView = null;
159 mInflater = LayoutInflater.from(this);
Jim Miller47d380f2010-01-20 13:37:14 -0800160 mLockUtils = new LockPatternUtils(this);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800161
162 establishInitialState();
163 }
164
165 /** Abandon all progress through the confirmation sequence by returning
166 * to the initial view any time the activity is interrupted (e.g. by
167 * idle timeout).
168 */
169 @Override
170 public void onPause() {
171 super.onPause();
172
173 establishInitialState();
174 }
175
176}