The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame^] | 19 | import com.android.internal.os.storage.ExternalStorageFormatter; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 20 | import com.android.internal.widget.LockPatternUtils; |
| 21 | |
| 22 | import android.app.Activity; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.os.Bundle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.widget.Button; |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame^] | 28 | import android.widget.CheckBox; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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; |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame^] | 47 | private View mExternalStorageContainer; |
| 48 | private CheckBox mExternalStorage; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 49 | |
| 50 | private View mFinalView; |
| 51 | private Button mFinalButton; |
| 52 | |
Jim Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 53 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 60 | if (Utils.isMonkeyRunning()) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 61 | return; |
| 62 | } |
Ying Wang | 50cb76f | 2010-01-04 12:04:25 -0800 | [diff] [blame] | 63 | |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame^] | 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 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 72 | } |
| 73 | }; |
| 74 | |
| 75 | /** |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 76 | * Keyguard validation is run using the standard {@link ConfirmLockPattern} |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 77 | * component as a subactivity |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 78 | * @param request the request code to be returned once confirmation finishes |
| 79 | * @return true if confirmation launched |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 80 | */ |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 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)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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(); |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 100 | } else if (resultCode == Activity.RESULT_CANCELED) { |
| 101 | finish(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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) { |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 114 | if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 139 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 | */ |
| 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); |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame^] | 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 | }); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | setContentView(mInitialView); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | protected void onCreate(Bundle savedState) { |
| 169 | super.onCreate(savedState); |
| 170 | |
| 171 | mInitialView = null; |
| 172 | mFinalView = null; |
| 173 | mInflater = LayoutInflater.from(this); |
Jim Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 174 | mLockUtils = new LockPatternUtils(this); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 |
| 184 | public void onPause() { |
| 185 | super.onPause(); |
| 186 | |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame^] | 187 | if (!isFinishing()) { |
| 188 | establishInitialState(); |
| 189 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 190 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 191 | } |