blob: e653d90b375ea90e593cc06bf250b3e2e27d17c8 [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
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070019import com.android.internal.os.storage.ExternalStorageFormatter;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080020import com.android.internal.widget.LockPatternUtils;
21
22import android.app.Activity;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.content.Intent;
24import android.os.Bundle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070028import android.widget.CheckBox;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029
30/**
31 * Confirm and execute a reset of the device to a clean "just out of the box"
32 * state. Multiple confirmations are required: first, a general "are you sure
33 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
34 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
35 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
36 * locked, et cetera, then the confirmation sequence is abandoned.
37 */
38public class MasterClear extends Activity {
39
40 private static final int KEYGUARD_REQUEST = 55;
41
42 private LayoutInflater mInflater;
43 private LockPatternUtils mLockUtils;
44
45 private View mInitialView;
46 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070047 private View mExternalStorageContainer;
48 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049
50 private View mFinalView;
51 private Button mFinalButton;
52
Jim Miller47d380f2010-01-20 13:37:14 -080053 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054 * The user has gone through the multiple confirmation, so now we go ahead
55 * and invoke the Checkin Service to reset the device to its factory-default
56 * state (rebooting in the process).
57 */
58 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
59 public void onClick(View v) {
Ying Wanga7188322010-01-04 18:45:10 -080060 if (Utils.isMonkeyRunning()) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080061 return;
62 }
Ying Wang50cb76f2010-01-04 12:04:25 -080063
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070064 if (mExternalStorage.isChecked()) {
65 Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
66 intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
67 startService(intent);
68 } else {
69 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
70 // Intent handling is asynchronous -- assume it will happen soon.
71 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 }
73 };
74
75 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070076 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070078 * @param request the request code to be returned once confirmation finishes
79 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 */
Jim Miller2deec7e2010-04-13 17:43:36 -070081 private boolean runKeyguardConfirmation(int request) {
82 return new ChooseLockSettingsHelper(this)
83 .launchConfirmationActivity(request,
84 getText(R.string.master_clear_gesture_prompt),
85 getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 }
87
88 @Override
89 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
90 super.onActivityResult(requestCode, resultCode, data);
91
92 if (requestCode != KEYGUARD_REQUEST) {
93 return;
94 }
95
96 // If the user entered a valid keyguard trace, present the final
97 // confirmation prompt; otherwise, go back to the initial state.
98 if (resultCode == Activity.RESULT_OK) {
99 establishFinalConfirmationState();
Jim Miller2deec7e2010-04-13 17:43:36 -0700100 } else if (resultCode == Activity.RESULT_CANCELED) {
101 finish();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102 } else {
103 establishInitialState();
104 }
105 }
106
107 /**
108 * If the user clicks to begin the reset sequence, we next require a
109 * keyguard confirmation if the user has currently enabled one. If there
110 * is no keyguard available, we simply go to the final confirmation prompt.
111 */
112 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
113 public void onClick(View v) {
Jim Miller2deec7e2010-04-13 17:43:36 -0700114 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 establishFinalConfirmationState();
116 }
117 }
118 };
119
120 /**
121 * Configure the UI for the final confirmation interaction
122 */
123 private void establishFinalConfirmationState() {
124 if (mFinalView == null) {
125 mFinalView = mInflater.inflate(R.layout.master_clear_final, null);
126 mFinalButton =
127 (Button) mFinalView.findViewById(R.id.execute_master_clear);
128 mFinalButton.setOnClickListener(mFinalClickListener);
129 }
130
131 setContentView(mFinalView);
132 }
133
134 /**
135 * In its initial state, the activity presents a button for the user to
136 * click in order to initiate a confirmation sequence. This method is
137 * called from various other points in the code to reset the activity to
138 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800139 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800140 * <p>Reinflating views from resources is expensive and prevents us from
141 * caching widget pointers, so we use a single-inflate pattern: we lazy-
142 * inflate each view, caching all of the widget pointers we'll need at the
143 * time, then simply reuse the inflated views directly whenever we need
144 * to change contents.
145 */
146 private void establishInitialState() {
147 if (mInitialView == null) {
148 mInitialView = mInflater.inflate(R.layout.master_clear_primary, null);
149 mInitiateButton =
150 (Button) mInitialView.findViewById(R.id.initiate_master_clear);
151 mInitiateButton.setOnClickListener(mInitiateListener);
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700152 mExternalStorageContainer =
153 mInitialView.findViewById(R.id.erase_external_container);
154 mExternalStorage =
155 (CheckBox) mInitialView.findViewById(R.id.erase_external);
156 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
157 @Override
158 public void onClick(View v) {
159 mExternalStorage.toggle();
160 }
161 });
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800162 }
163
164 setContentView(mInitialView);
165 }
166
167 @Override
168 protected void onCreate(Bundle savedState) {
169 super.onCreate(savedState);
170
171 mInitialView = null;
172 mFinalView = null;
173 mInflater = LayoutInflater.from(this);
Jim Miller47d380f2010-01-20 13:37:14 -0800174 mLockUtils = new LockPatternUtils(this);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800175
176 establishInitialState();
177 }
178
179 /** Abandon all progress through the confirmation sequence by returning
180 * to the initial view any time the activity is interrupted (e.g. by
181 * idle timeout).
182 */
183 @Override
184 public void onPause() {
185 super.onPause();
186
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700187 if (!isFinishing()) {
188 establishInitialState();
189 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800190 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800191}