blob: 7e408d1d770b8dee6499f077940e5ec6bb89636f [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 /**
72 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
73 * component as a subactivity
74 */
75 private void runKeyguardConfirmation() {
76 final Intent intent = new Intent();
77 intent.setClassName("com.android.settings",
78 "com.android.settings.ConfirmLockPattern");
79 // supply header and footer text in the intent
80 intent.putExtra(ConfirmLockPattern.HEADER_TEXT,
81 getText(R.string.master_clear_gesture_prompt));
82 intent.putExtra(ConfirmLockPattern.FOOTER_TEXT,
83 getText(R.string.master_clear_gesture_explanation));
84 startActivityForResult(intent, KEYGUARD_REQUEST);
85 }
86
87 @Override
88 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
89 super.onActivityResult(requestCode, resultCode, data);
90
91 if (requestCode != KEYGUARD_REQUEST) {
92 return;
93 }
94
95 // If the user entered a valid keyguard trace, present the final
96 // confirmation prompt; otherwise, go back to the initial state.
97 if (resultCode == Activity.RESULT_OK) {
98 establishFinalConfirmationState();
99 } else {
100 establishInitialState();
101 }
102 }
103
104 /**
105 * If the user clicks to begin the reset sequence, we next require a
106 * keyguard confirmation if the user has currently enabled one. If there
107 * is no keyguard available, we simply go to the final confirmation prompt.
108 */
109 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
110 public void onClick(View v) {
111 if (mLockUtils.isLockPatternEnabled()) {
112 runKeyguardConfirmation();
113 } else {
114 establishFinalConfirmationState();
115 }
116 }
117 };
118
119 /**
120 * Configure the UI for the final confirmation interaction
121 */
122 private void establishFinalConfirmationState() {
123 if (mFinalView == null) {
124 mFinalView = mInflater.inflate(R.layout.master_clear_final, null);
125 mFinalButton =
126 (Button) mFinalView.findViewById(R.id.execute_master_clear);
127 mFinalButton.setOnClickListener(mFinalClickListener);
128 }
129
130 setContentView(mFinalView);
131 }
132
133 /**
134 * In its initial state, the activity presents a button for the user to
135 * click in order to initiate a confirmation sequence. This method is
136 * called from various other points in the code to reset the activity to
137 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800138 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 * <p>Reinflating views from resources is expensive and prevents us from
140 * caching widget pointers, so we use a single-inflate pattern: we lazy-
141 * inflate each view, caching all of the widget pointers we'll need at the
142 * time, then simply reuse the inflated views directly whenever we need
143 * to change contents.
144 */
145 private void establishInitialState() {
146 if (mInitialView == null) {
147 mInitialView = mInflater.inflate(R.layout.master_clear_primary, null);
148 mInitiateButton =
149 (Button) mInitialView.findViewById(R.id.initiate_master_clear);
150 mInitiateButton.setOnClickListener(mInitiateListener);
151 }
152
153 setContentView(mInitialView);
154 }
155
156 @Override
157 protected void onCreate(Bundle savedState) {
158 super.onCreate(savedState);
159
160 mInitialView = null;
161 mFinalView = null;
162 mInflater = LayoutInflater.from(this);
Jim Miller47d380f2010-01-20 13:37:14 -0800163 mLockUtils = new LockPatternUtils(this);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800164
165 establishInitialState();
166 }
167
168 /** Abandon all progress through the confirmation sequence by returning
169 * to the initial view any time the activity is interrupted (e.g. by
170 * idle timeout).
171 */
172 @Override
173 public void onPause() {
174 super.onPause();
175
176 establishInitialState();
177 }
178
179}