blob: 38ad6081850061000fd84f9c1febf97a23d51a0d [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) {
63
64 // Those monkeys kept committing suicide, so we add this property
65 // to disable going through with the master clear
66 if (!TextUtils.isEmpty(SystemProperties.get("ro.monkey"))) {
67 return;
68 }
69
70 ICheckinService service =
71 ICheckinService.Stub.asInterface(ServiceManager.getService("checkin"));
72 if (service != null) {
73 try {
74 // This RPC should never return
75 service.masterClear();
76 } catch (android.os.RemoteException e) {
77 // Intentionally blank - there's nothing we can do here
78 Log.w("MasterClear", "Unable to invoke ICheckinService.masterClear()");
79 }
80 } else {
81 Log.w("MasterClear", "Unable to locate ICheckinService");
82 }
83
84 /* If we reach this point, the master clear didn't happen -- the
85 * service might have been unregistered with the ServiceManager,
86 * the RPC might have thrown an exception, or for some reason
87 * the implementation of masterClear() may have returned instead
88 * of resetting the device.
89 */
90 new AlertDialog.Builder(MasterClear.this)
91 .setMessage(getText(R.string.master_clear_failed))
92 .setPositiveButton(getText(android.R.string.ok), null)
93 .show();
94 }
95 };
96
97 /**
98 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
99 * component as a subactivity
100 */
101 private void runKeyguardConfirmation() {
102 final Intent intent = new Intent();
103 intent.setClassName("com.android.settings",
104 "com.android.settings.ConfirmLockPattern");
105 // supply header and footer text in the intent
106 intent.putExtra(ConfirmLockPattern.HEADER_TEXT,
107 getText(R.string.master_clear_gesture_prompt));
108 intent.putExtra(ConfirmLockPattern.FOOTER_TEXT,
109 getText(R.string.master_clear_gesture_explanation));
110 startActivityForResult(intent, KEYGUARD_REQUEST);
111 }
112
113 @Override
114 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
115 super.onActivityResult(requestCode, resultCode, data);
116
117 if (requestCode != KEYGUARD_REQUEST) {
118 return;
119 }
120
121 // If the user entered a valid keyguard trace, present the final
122 // confirmation prompt; otherwise, go back to the initial state.
123 if (resultCode == Activity.RESULT_OK) {
124 establishFinalConfirmationState();
125 } else {
126 establishInitialState();
127 }
128 }
129
130 /**
131 * If the user clicks to begin the reset sequence, we next require a
132 * keyguard confirmation if the user has currently enabled one. If there
133 * is no keyguard available, we simply go to the final confirmation prompt.
134 */
135 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
136 public void onClick(View v) {
137 if (mLockUtils.isLockPatternEnabled()) {
138 runKeyguardConfirmation();
139 } else {
140 establishFinalConfirmationState();
141 }
142 }
143 };
144
145 /**
146 * Configure the UI for the final confirmation interaction
147 */
148 private void establishFinalConfirmationState() {
149 if (mFinalView == null) {
150 mFinalView = mInflater.inflate(R.layout.master_clear_final, null);
151 mFinalButton =
152 (Button) mFinalView.findViewById(R.id.execute_master_clear);
153 mFinalButton.setOnClickListener(mFinalClickListener);
154 }
155
156 setContentView(mFinalView);
157 }
158
159 /**
160 * In its initial state, the activity presents a button for the user to
161 * click in order to initiate a confirmation sequence. This method is
162 * called from various other points in the code to reset the activity to
163 * this base state.
164 *
165 * <p>Reinflating views from resources is expensive and prevents us from
166 * caching widget pointers, so we use a single-inflate pattern: we lazy-
167 * inflate each view, caching all of the widget pointers we'll need at the
168 * time, then simply reuse the inflated views directly whenever we need
169 * to change contents.
170 */
171 private void establishInitialState() {
172 if (mInitialView == null) {
173 mInitialView = mInflater.inflate(R.layout.master_clear_primary, null);
174 mInitiateButton =
175 (Button) mInitialView.findViewById(R.id.initiate_master_clear);
176 mInitiateButton.setOnClickListener(mInitiateListener);
177 }
178
179 setContentView(mInitialView);
180 }
181
182 @Override
183 protected void onCreate(Bundle savedState) {
184 super.onCreate(savedState);
185
186 mInitialView = null;
187 mFinalView = null;
188 mInflater = LayoutInflater.from(this);
189 mLockUtils = new LockPatternUtils(getContentResolver());
190
191 establishInitialState();
192 }
193
194 /** Abandon all progress through the confirmation sequence by returning
195 * to the initial view any time the activity is interrupted (e.g. by
196 * idle timeout).
197 */
198 @Override
199 public void onPause() {
200 super.onPause();
201
202 establishInitialState();
203 }
204
205}