blob: 29a92b106b8c265571654e0d93a141aa73da6c1f [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
Joe Onoratob51886d2010-11-08 18:25:51 -080021import android.accounts.Account;
22import android.accounts.AccountManager;
23import android.accounts.AuthenticatorDescription;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.app.Activity;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070025import android.app.Fragment;
Joe Onoratob51886d2010-11-08 18:25:51 -080026import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080028import android.content.pm.PackageManager;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070029import android.content.res.Resources;
Joe Onoratob51886d2010-11-08 18:25:51 -080030import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080032import android.os.Environment;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070033import android.preference.Preference;
34import android.preference.PreferenceActivity;
Joe Onoratob51886d2010-11-08 18:25:51 -080035import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036import android.view.LayoutInflater;
37import android.view.View;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070038import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070040import android.widget.CheckBox;
Joe Onoratob51886d2010-11-08 18:25:51 -080041import android.widget.LinearLayout;
42import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043
44/**
45 * Confirm and execute a reset of the device to a clean "just out of the box"
46 * state. Multiple confirmations are required: first, a general "are you sure
47 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
48 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
49 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
50 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070051 *
52 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053 */
Amith Yamasanib14e1e02010-11-02 09:52:29 -070054public class MasterClear extends Fragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080055 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056
57 private static final int KEYGUARD_REQUEST = 55;
58
Amith Yamasanib14e1e02010-11-02 09:52:29 -070059 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060
Amith Yamasanib14e1e02010-11-02 09:52:29 -070061 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070063 private View mExternalStorageContainer;
64 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070067 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070069 * @param request the request code to be returned once confirmation finishes
70 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080071 */
Jim Miller2deec7e2010-04-13 17:43:36 -070072 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070073 Resources res = getActivity().getResources();
74 return new ChooseLockSettingsHelper(getActivity(), this)
Jim Miller2deec7e2010-04-13 17:43:36 -070075 .launchConfirmationActivity(request,
Amith Yamasanib14e1e02010-11-02 09:52:29 -070076 res.getText(R.string.master_clear_gesture_prompt),
77 res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080078 }
79
80 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070081 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 super.onActivityResult(requestCode, resultCode, data);
83
84 if (requestCode != KEYGUARD_REQUEST) {
85 return;
86 }
87
88 // If the user entered a valid keyguard trace, present the final
89 // confirmation prompt; otherwise, go back to the initial state.
90 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070091 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 } else {
93 establishInitialState();
94 }
95 }
96
Amith Yamasanib14e1e02010-11-02 09:52:29 -070097 private void showFinalConfirmation() {
98 Preference preference = new Preference(getActivity());
99 preference.setFragment(MasterClearConfirm.class.getName());
100 preference.setTitle(R.string.master_clear_confirm_title);
101 preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
102 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(null, preference);
103 }
104
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 /**
106 * If the user clicks to begin the reset sequence, we next require a
107 * keyguard confirmation if the user has currently enabled one. If there
108 * is no keyguard available, we simply go to the final confirmation prompt.
109 */
110 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700111
112 public void onClick(View v) {
113 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
114 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800116 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700117 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118
119 /**
120 * In its initial state, the activity presents a button for the user to
121 * click in order to initiate a confirmation sequence. This method is
122 * called from various other points in the code to reset the activity to
123 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800124 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 * <p>Reinflating views from resources is expensive and prevents us from
126 * caching widget pointers, so we use a single-inflate pattern: we lazy-
127 * inflate each view, caching all of the widget pointers we'll need at the
128 * time, then simply reuse the inflated views directly whenever we need
129 * to change contents.
130 */
131 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700132 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
133 mInitiateButton.setOnClickListener(mInitiateListener);
134 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
135 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136
Kenny Root3785e392011-01-18 15:14:32 -0800137 /*
138 * If the external storage is emulated, it will be erased with a factory
139 * reset at any rate. There is no need to have a separate option until
140 * we have a factory reset that only erases some directories and not
141 * others.
142 */
143 if (Environment.isExternalStorageEmulated()) {
144 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700145
Kenny Root3785e392011-01-18 15:14:32 -0800146 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
147 externalOption.setVisibility(View.GONE);
148
149 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
150 externalAlsoErased.setVisibility(View.VISIBLE);
151 } else {
152 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
153
154 @Override
155 public void onClick(View v) {
156 mExternalStorage.toggle();
157 }
158 });
159 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800160
161 loadAccountList();
162 }
163
164 private void loadAccountList() {
165 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
166 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
167
168 Context context = getActivity();
169
170 AccountManager mgr = AccountManager.get(context);
171 Account[] accounts = mgr.getAccounts();
172 final int N = accounts.length;
173 if (N == 0) {
174 accountsLabel.setVisibility(View.GONE);
175 contents.setVisibility(View.GONE);
176 return;
177 }
178
179 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
180 Context.LAYOUT_INFLATER_SERVICE);
181
182 AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypes();
183 final int M = descs.length;
184
185 for (int i=0; i<N; i++) {
186 Account account = accounts[i];
187 AuthenticatorDescription desc = null;
188 for (int j=0; j<M; j++) {
189 if (account.type.equals(descs[j].type)) {
190 desc = descs[j];
191 break;
192 }
193 }
194 if (desc == null) {
195 Log.w(TAG, "No descriptor for account name=" + account.name
196 + " type=" + account.type);
197 continue;
198 }
Amith Yamasani07d6fd22011-07-15 18:37:16 -0700199 Drawable icon = null;
Joe Onoratob51886d2010-11-08 18:25:51 -0800200 try {
Amith Yamasani07d6fd22011-07-15 18:37:16 -0700201 if (desc.iconId != 0) {
202 Context authContext = context.createPackageContext(desc.packageName, 0);
203 icon = authContext.getResources().getDrawable(desc.iconId);
204 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800205 } catch (PackageManager.NameNotFoundException e) {
206 Log.w(TAG, "No icon for account type " + desc.type);
Joe Onoratob51886d2010-11-08 18:25:51 -0800207 }
208
209 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
210 contents, false);
211 child.setText(account.name);
212 if (icon != null) {
213 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
214 }
215 contents.addView(child);
216 }
217
218 accountsLabel.setVisibility(View.VISIBLE);
219 contents.setVisibility(View.VISIBLE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800220 }
221
222 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700223 public View onCreateView(LayoutInflater inflater, ViewGroup container,
224 Bundle savedInstanceState) {
225 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800226
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800227 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700228 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800229 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800230}