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 | /** |
| 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 Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 138 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 139 | * <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 Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 163 | mLockUtils = new LockPatternUtils(this); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 164 | |
| 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 | } |