blob: fd4a41179a134fe56afeaa0725cec1809a62fa7c [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;
26import android.os.ICheckinService;
27import android.os.ServiceManager;
28import android.os.SystemProperties;
29import android.text.TextUtils;
30import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.widget.Button;
34
35/**
36 * Confirm and execute a reset of the device to a clean "just out of the box"
37 * state. Multiple confirmations are required: first, a general "are you sure
38 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
39 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
40 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
41 * locked, et cetera, then the confirmation sequence is abandoned.
42 */
43public class MasterClear extends Activity {
44
45 private static final int KEYGUARD_REQUEST = 55;
46
47 private LayoutInflater mInflater;
48 private LockPatternUtils mLockUtils;
49
50 private View mInitialView;
51 private Button mInitiateButton;
52
53 private View mFinalView;
54 private Button mFinalButton;
55
56 /**
57 * The user has gone through the multiple confirmation, so now we go ahead
58 * and invoke the Checkin Service to reset the device to its factory-default
59 * state (rebooting in the process).
60 */
61 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
62 public void onClick(View v) {
Ying Wang50cb76f2010-01-04 12:04:25 -080063
Ying Wanga7188322010-01-04 18:45:10 -080064 if (Utils.isMonkeyRunning()) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065 return;
66 }
Ying Wang50cb76f2010-01-04 12:04:25 -080067
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 ICheckinService service =
69 ICheckinService.Stub.asInterface(ServiceManager.getService("checkin"));
70 if (service != null) {
71 try {
72 // This RPC should never return
73 service.masterClear();
74 } catch (android.os.RemoteException e) {
75 // Intentionally blank - there's nothing we can do here
76 Log.w("MasterClear", "Unable to invoke ICheckinService.masterClear()");
77 }
78 } else {
79 Log.w("MasterClear", "Unable to locate ICheckinService");
80 }
81
82 /* If we reach this point, the master clear didn't happen -- the
83 * service might have been unregistered with the ServiceManager,
84 * the RPC might have thrown an exception, or for some reason
85 * the implementation of masterClear() may have returned instead
86 * of resetting the device.
87 */
88 new AlertDialog.Builder(MasterClear.this)
89 .setMessage(getText(R.string.master_clear_failed))
90 .setPositiveButton(getText(android.R.string.ok), null)
91 .show();
92 }
93 };
94
95 /**
96 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
97 * component as a subactivity
98 */
99 private void runKeyguardConfirmation() {
100 final Intent intent = new Intent();
101 intent.setClassName("com.android.settings",
102 "com.android.settings.ConfirmLockPattern");
103 // supply header and footer text in the intent
104 intent.putExtra(ConfirmLockPattern.HEADER_TEXT,
105 getText(R.string.master_clear_gesture_prompt));
106 intent.putExtra(ConfirmLockPattern.FOOTER_TEXT,
107 getText(R.string.master_clear_gesture_explanation));
108 startActivityForResult(intent, KEYGUARD_REQUEST);
109 }
110
111 @Override
112 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
113 super.onActivityResult(requestCode, resultCode, data);
114
115 if (requestCode != KEYGUARD_REQUEST) {
116 return;
117 }
118
119 // If the user entered a valid keyguard trace, present the final
120 // confirmation prompt; otherwise, go back to the initial state.
121 if (resultCode == Activity.RESULT_OK) {
122 establishFinalConfirmationState();
123 } else {
124 establishInitialState();
125 }
126 }
127
128 /**
129 * If the user clicks to begin the reset sequence, we next require a
130 * keyguard confirmation if the user has currently enabled one. If there
131 * is no keyguard available, we simply go to the final confirmation prompt.
132 */
133 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
134 public void onClick(View v) {
135 if (mLockUtils.isLockPatternEnabled()) {
136 runKeyguardConfirmation();
137 } else {
138 establishFinalConfirmationState();
139 }
140 }
141 };
142
143 /**
144 * Configure the UI for the final confirmation interaction
145 */
146 private void establishFinalConfirmationState() {
147 if (mFinalView == null) {
148 mFinalView = mInflater.inflate(R.layout.master_clear_final, null);
149 mFinalButton =
150 (Button) mFinalView.findViewById(R.id.execute_master_clear);
151 mFinalButton.setOnClickListener(mFinalClickListener);
152 }
153
154 setContentView(mFinalView);
155 }
156
157 /**
158 * In its initial state, the activity presents a button for the user to
159 * click in order to initiate a confirmation sequence. This method is
160 * called from various other points in the code to reset the activity to
161 * this base state.
162 *
163 * <p>Reinflating views from resources is expensive and prevents us from
164 * caching widget pointers, so we use a single-inflate pattern: we lazy-
165 * inflate each view, caching all of the widget pointers we'll need at the
166 * time, then simply reuse the inflated views directly whenever we need
167 * to change contents.
168 */
169 private void establishInitialState() {
170 if (mInitialView == null) {
171 mInitialView = mInflater.inflate(R.layout.master_clear_primary, null);
172 mInitiateButton =
173 (Button) mInitialView.findViewById(R.id.initiate_master_clear);
174 mInitiateButton.setOnClickListener(mInitiateListener);
175 }
176
177 setContentView(mInitialView);
178 }
179
180 @Override
181 protected void onCreate(Bundle savedState) {
182 super.onCreate(savedState);
183
184 mInitialView = null;
185 mFinalView = null;
186 mInflater = LayoutInflater.from(this);
187 mLockUtils = new LockPatternUtils(getContentResolver());
188
189 establishInitialState();
190 }
191
192 /** Abandon all progress through the confirmation sequence by returning
193 * to the initial view any time the activity is interrupted (e.g. by
194 * idle timeout).
195 */
196 @Override
197 public void onPause() {
198 super.onPause();
199
200 establishInitialState();
201 }
202
203}