blob: 4cf05466c1e85bdcc75c8a21e5de4fff344de269 [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
Jim Miller47d380f2010-01-20 13:37:14 -080056 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057 * 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 Wanga7188322010-01-04 18:45:10 -080063 if (Utils.isMonkeyRunning()) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064 return;
65 }
Ying Wang50cb76f2010-01-04 12:04:25 -080066
Dan Egnor3352d102010-02-10 19:29:47 -080067 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
68 // Intent handling is asynchronous -- assume it will happen soon.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069 }
70 };
71
72 /**
73 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
74 * component as a subactivity
75 */
76 private void runKeyguardConfirmation() {
77 final Intent intent = new Intent();
78 intent.setClassName("com.android.settings",
79 "com.android.settings.ConfirmLockPattern");
80 // supply header and footer text in the intent
81 intent.putExtra(ConfirmLockPattern.HEADER_TEXT,
82 getText(R.string.master_clear_gesture_prompt));
83 intent.putExtra(ConfirmLockPattern.FOOTER_TEXT,
84 getText(R.string.master_clear_gesture_explanation));
85 startActivityForResult(intent, KEYGUARD_REQUEST);
86 }
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();
100 } else {
101 establishInitialState();
102 }
103 }
104
105 /**
106 * If the user clicks to begin the reset sequence, we next require a
107 * keyguard confirmation if the user has currently enabled one. If there
108 * is no keyguard available, we simply go to the final confirmation prompt.
109 */
110 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
111 public void onClick(View v) {
112 if (mLockUtils.isLockPatternEnabled()) {
113 runKeyguardConfirmation();
114 } else {
115 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);
152 }
153
154 setContentView(mInitialView);
155 }
156
157 @Override
158 protected void onCreate(Bundle savedState) {
159 super.onCreate(savedState);
160
161 mInitialView = null;
162 mFinalView = null;
163 mInflater = LayoutInflater.from(this);
Jim Miller47d380f2010-01-20 13:37:14 -0800164 mLockUtils = new LockPatternUtils(this);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800165
166 establishInitialState();
167 }
168
169 /** Abandon all progress through the confirmation sequence by returning
170 * to the initial view any time the activity is interrupted (e.g. by
171 * idle timeout).
172 */
173 @Override
174 public void onPause() {
175 super.onPause();
176
177 establishInitialState();
178 }
179
180}