blob: a2ef7f4a604c35cf56dae580a6d684e6f084a5cd [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
Amith Yamasanib14e1e02010-11-02 09:52:29 -070019import com.android.settings.R;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080020
21import android.app.Activity;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070022import android.app.Fragment;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.content.Intent;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070024import android.content.res.Resources;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.os.Bundle;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070026import android.preference.Preference;
27import android.preference.PreferenceActivity;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.view.LayoutInflater;
29import android.view.View;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070030import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070032import android.widget.CheckBox;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033
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.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070041 *
42 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043 */
Amith Yamasanib14e1e02010-11-02 09:52:29 -070044public class MasterClear extends Fragment {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045
46 private static final int KEYGUARD_REQUEST = 55;
47
Amith Yamasanib14e1e02010-11-02 09:52:29 -070048 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049
Amith Yamasanib14e1e02010-11-02 09:52:29 -070050 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070052 private View mExternalStorageContainer;
53 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070056 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070058 * @param request the request code to be returned once confirmation finishes
59 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060 */
Jim Miller2deec7e2010-04-13 17:43:36 -070061 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070062 Resources res = getActivity().getResources();
63 return new ChooseLockSettingsHelper(getActivity(), this)
Jim Miller2deec7e2010-04-13 17:43:36 -070064 .launchConfirmationActivity(request,
Amith Yamasanib14e1e02010-11-02 09:52:29 -070065 res.getText(R.string.master_clear_gesture_prompt),
66 res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067 }
68
69 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070070 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080071 super.onActivityResult(requestCode, resultCode, data);
72
73 if (requestCode != KEYGUARD_REQUEST) {
74 return;
75 }
76
77 // If the user entered a valid keyguard trace, present the final
78 // confirmation prompt; otherwise, go back to the initial state.
79 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070080 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 } else {
82 establishInitialState();
83 }
84 }
85
Amith Yamasanib14e1e02010-11-02 09:52:29 -070086 private void showFinalConfirmation() {
87 Preference preference = new Preference(getActivity());
88 preference.setFragment(MasterClearConfirm.class.getName());
89 preference.setTitle(R.string.master_clear_confirm_title);
90 preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
91 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(null, preference);
92 }
93
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 /**
95 * If the user clicks to begin the reset sequence, we next require a
96 * keyguard confirmation if the user has currently enabled one. If there
97 * is no keyguard available, we simply go to the final confirmation prompt.
98 */
99 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700100
101 public void onClick(View v) {
102 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
103 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800104 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700106 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107
108 /**
109 * In its initial state, the activity presents a button for the user to
110 * click in order to initiate a confirmation sequence. This method is
111 * called from various other points in the code to reset the activity to
112 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800113 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 * <p>Reinflating views from resources is expensive and prevents us from
115 * caching widget pointers, so we use a single-inflate pattern: we lazy-
116 * inflate each view, caching all of the widget pointers we'll need at the
117 * time, then simply reuse the inflated views directly whenever we need
118 * to change contents.
119 */
120 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700121 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
122 mInitiateButton.setOnClickListener(mInitiateListener);
123 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
124 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700126 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
127
128 @Override
129 public void onClick(View v) {
130 mExternalStorage.toggle();
131 }
132 });
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 }
134
135 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700136 public View onCreateView(LayoutInflater inflater, ViewGroup container,
137 Bundle savedInstanceState) {
138 mContentView = inflater.inflate(R.layout.master_clear, null);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700140 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800141 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142}