• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings;
18 
19 import com.android.internal.os.storage.ExternalStorageFormatter;
20 import com.android.internal.widget.LockPatternUtils;
21 
22 import android.app.Activity;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.widget.Button;
28 import android.widget.CheckBox;
29 
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  */
38 public 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;
47     private View mExternalStorageContainer;
48     private CheckBox mExternalStorage;
49 
50     private View mFinalView;
51     private Button mFinalButton;
52 
53     /**
54      * 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) {
60                 if (Utils.isMonkeyRunning()) {
61                     return;
62                 }
63 
64                 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                 }
72             }
73         };
74 
75     /**
76      * Keyguard validation is run using the standard {@link ConfirmLockPattern}
77      * component as a subactivity
78      * @param request the request code to be returned once confirmation finishes
79      * @return true if confirmation launched
80      */
runKeyguardConfirmation(int request)81     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));
86     }
87 
88     @Override
onActivityResult(int requestCode, int resultCode, Intent data)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 if (resultCode == Activity.RESULT_CANCELED) {
101             finish();
102         } 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) {
114                 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
115                     establishFinalConfirmationState();
116                 }
117             }
118         };
119 
120     /**
121      * Configure the UI for the final confirmation interaction
122      */
establishFinalConfirmationState()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.
139      *
140      * <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      */
establishInitialState()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             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             });
162         }
163 
164         setContentView(mInitialView);
165     }
166 
167     @Override
onCreate(Bundle savedState)168     protected void onCreate(Bundle savedState) {
169         super.onCreate(savedState);
170 
171         mInitialView = null;
172         mFinalView = null;
173         mInflater = LayoutInflater.from(this);
174         mLockUtils = new LockPatternUtils(this);
175 
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
onPause()184     public void onPause() {
185         super.onPause();
186 
187         if (!isFinishing()) {
188             establishInitialState();
189         }
190     }
191 }
192