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 | |
| 19 | import com.android.internal.widget.LockPatternUtils; |
| 20 | |
| 21 | import android.app.Activity; |
| 22 | import android.app.AlertDialog; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.os.Bundle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 26 | import android.os.ServiceManager; |
| 27 | import android.os.SystemProperties; |
| 28 | import android.text.TextUtils; |
| 29 | import android.util.Log; |
| 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
| 32 | import 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 | */ |
| 42 | public 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 Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 55 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 56 | * 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 Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 62 | if (Utils.isMonkeyRunning()) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 63 | return; |
| 64 | } |
Ying Wang | 50cb76f | 2010-01-04 12:04:25 -0800 | [diff] [blame] | 65 | |
Dan Egnor | 3352d10 | 2010-02-10 19:29:47 -0800 | [diff] [blame] | 66 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 67 | // Intent handling is asynchronous -- assume it will happen soon. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | /** |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame^] | 72 | * Keyguard validation is run using the standard {@link ConfirmLockPattern} |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 73 | * component as a subactivity |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame^] | 74 | * @param request the request code to be returned once confirmation finishes |
| 75 | * @return true if confirmation launched |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 76 | */ |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame^] | 77 | private boolean runKeyguardConfirmation(int request) { |
| 78 | return new ChooseLockSettingsHelper(this) |
| 79 | .launchConfirmationActivity(request, |
| 80 | getText(R.string.master_clear_gesture_prompt), |
| 81 | getText(R.string.master_clear_gesture_explanation)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 86 | super.onActivityResult(requestCode, resultCode, data); |
| 87 | |
| 88 | if (requestCode != KEYGUARD_REQUEST) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // If the user entered a valid keyguard trace, present the final |
| 93 | // confirmation prompt; otherwise, go back to the initial state. |
| 94 | if (resultCode == Activity.RESULT_OK) { |
| 95 | establishFinalConfirmationState(); |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame^] | 96 | } else if (resultCode == Activity.RESULT_CANCELED) { |
| 97 | finish(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 98 | } else { |
| 99 | establishInitialState(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * If the user clicks to begin the reset sequence, we next require a |
| 105 | * keyguard confirmation if the user has currently enabled one. If there |
| 106 | * is no keyguard available, we simply go to the final confirmation prompt. |
| 107 | */ |
| 108 | private Button.OnClickListener mInitiateListener = new Button.OnClickListener() { |
| 109 | public void onClick(View v) { |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame^] | 110 | if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 111 | establishFinalConfirmationState(); |
| 112 | } |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * Configure the UI for the final confirmation interaction |
| 118 | */ |
| 119 | private void establishFinalConfirmationState() { |
| 120 | if (mFinalView == null) { |
| 121 | mFinalView = mInflater.inflate(R.layout.master_clear_final, null); |
| 122 | mFinalButton = |
| 123 | (Button) mFinalView.findViewById(R.id.execute_master_clear); |
| 124 | mFinalButton.setOnClickListener(mFinalClickListener); |
| 125 | } |
| 126 | |
| 127 | setContentView(mFinalView); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * In its initial state, the activity presents a button for the user to |
| 132 | * click in order to initiate a confirmation sequence. This method is |
| 133 | * called from various other points in the code to reset the activity to |
| 134 | * this base state. |
Jim Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 135 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | * <p>Reinflating views from resources is expensive and prevents us from |
| 137 | * caching widget pointers, so we use a single-inflate pattern: we lazy- |
| 138 | * inflate each view, caching all of the widget pointers we'll need at the |
| 139 | * time, then simply reuse the inflated views directly whenever we need |
| 140 | * to change contents. |
| 141 | */ |
| 142 | private void establishInitialState() { |
| 143 | if (mInitialView == null) { |
| 144 | mInitialView = mInflater.inflate(R.layout.master_clear_primary, null); |
| 145 | mInitiateButton = |
| 146 | (Button) mInitialView.findViewById(R.id.initiate_master_clear); |
| 147 | mInitiateButton.setOnClickListener(mInitiateListener); |
| 148 | } |
| 149 | |
| 150 | setContentView(mInitialView); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | protected void onCreate(Bundle savedState) { |
| 155 | super.onCreate(savedState); |
| 156 | |
| 157 | mInitialView = null; |
| 158 | mFinalView = null; |
| 159 | mInflater = LayoutInflater.from(this); |
Jim Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 160 | mLockUtils = new LockPatternUtils(this); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 161 | |
| 162 | establishInitialState(); |
| 163 | } |
| 164 | |
| 165 | /** Abandon all progress through the confirmation sequence by returning |
| 166 | * to the initial view any time the activity is interrupted (e.g. by |
| 167 | * idle timeout). |
| 168 | */ |
| 169 | @Override |
| 170 | public void onPause() { |
| 171 | super.onPause(); |
| 172 | |
| 173 | establishInitialState(); |
| 174 | } |
| 175 | |
| 176 | } |