blob: 262aca37e784b881c04750db6336fd262996fa29 [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
Joe Onoratob51886d2010-11-08 18:25:51 -080019import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.accounts.AuthenticatorDescription;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080022import android.app.Activity;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070023import android.app.Fragment;
Joe Onoratob51886d2010-11-08 18:25:51 -080024import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080026import android.content.pm.PackageManager;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070027import android.content.res.Resources;
Joe Onoratob51886d2010-11-08 18:25:51 -080028import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080030import android.os.Environment;
Ben Komalo2a321922011-09-07 16:42:34 -070031import android.os.SystemProperties;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070032import android.os.UserManager;
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;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070058 private static final int PIN_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080059
Amith Yamasanib14e1e02010-11-02 09:52:29 -070060 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080061
Amith Yamasanib14e1e02010-11-02 09:52:29 -070062 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070064 private View mExternalStorageContainer;
65 private CheckBox mExternalStorage;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070066 private boolean mPinConfirmed;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070069 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070071 * @param request the request code to be returned once confirmation finishes
72 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073 */
Jim Miller2deec7e2010-04-13 17:43:36 -070074 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070075 Resources res = getActivity().getResources();
76 return new ChooseLockSettingsHelper(getActivity(), this)
Jim Miller2deec7e2010-04-13 17:43:36 -070077 .launchConfirmationActivity(request,
Amith Yamasanib14e1e02010-11-02 09:52:29 -070078 res.getText(R.string.master_clear_gesture_prompt),
79 res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 }
81
Amith Yamasani7eedcf42013-07-02 14:15:29 -070082 private boolean runRestrictionsChallenge() {
Amith Yamasani14912662013-09-05 12:40:31 -070083 if (UserManager.get(getActivity()).hasRestrictionsChallenge()) {
Amith Yamasani7eedcf42013-07-02 14:15:29 -070084 startActivityForResult(
Amith Yamasani14912662013-09-05 12:40:31 -070085 new Intent(Intent.ACTION_RESTRICTIONS_CHALLENGE), PIN_REQUEST);
Amith Yamasani7eedcf42013-07-02 14:15:29 -070086 return true;
87 }
88 return false;
89 }
90
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070092 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093 super.onActivityResult(requestCode, resultCode, data);
94
Amith Yamasani7eedcf42013-07-02 14:15:29 -070095 if (requestCode == PIN_REQUEST) {
96 if (resultCode == Activity.RESULT_OK) {
97 mPinConfirmed = true;
98 }
99 return;
100 } else if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 return;
102 }
103
104 // If the user entered a valid keyguard trace, present the final
105 // confirmation prompt; otherwise, go back to the initial state.
106 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700107 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800108 } else {
109 establishInitialState();
110 }
111 }
112
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700113 private void showFinalConfirmation() {
114 Preference preference = new Preference(getActivity());
115 preference.setFragment(MasterClearConfirm.class.getName());
116 preference.setTitle(R.string.master_clear_confirm_title);
117 preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
118 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(null, preference);
119 }
120
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800121 /**
122 * If the user clicks to begin the reset sequence, we next require a
123 * keyguard confirmation if the user has currently enabled one. If there
124 * is no keyguard available, we simply go to the final confirmation prompt.
125 */
Ben Komalo2a321922011-09-07 16:42:34 -0700126 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700127
128 public void onClick(View v) {
Amith Yamasani7eedcf42013-07-02 14:15:29 -0700129 mPinConfirmed = false;
130 if (runRestrictionsChallenge()) {
131 return;
132 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700133 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
134 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700137 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138
139 /**
140 * In its initial state, the activity presents a button for the user to
141 * click in order to initiate a confirmation sequence. This method is
142 * called from various other points in the code to reset the activity to
143 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800144 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800145 * <p>Reinflating views from resources is expensive and prevents us from
146 * caching widget pointers, so we use a single-inflate pattern: we lazy-
147 * inflate each view, caching all of the widget pointers we'll need at the
148 * time, then simply reuse the inflated views directly whenever we need
149 * to change contents.
150 */
151 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700152 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
153 mInitiateButton.setOnClickListener(mInitiateListener);
154 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
155 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800156
Kenny Root3785e392011-01-18 15:14:32 -0800157 /*
158 * If the external storage is emulated, it will be erased with a factory
159 * reset at any rate. There is no need to have a separate option until
160 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700161 * others. Likewise, if it's non-removable storage, it could potentially have been
162 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800163 */
Ben Komalo2a321922011-09-07 16:42:34 -0700164 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
165 if (isExtStorageEmulated
166 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800167 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700168
Kenny Root3785e392011-01-18 15:14:32 -0800169 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
170 externalOption.setVisibility(View.GONE);
171
172 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
173 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700174
175 // If it's not emulated, it is on a separate partition but it means we're doing
176 // a force wipe due to encryption.
177 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800178 } else {
179 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
180
181 @Override
182 public void onClick(View v) {
183 mExternalStorage.toggle();
184 }
185 });
186 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800187
188 loadAccountList();
189 }
190
Ben Komalo2a321922011-09-07 16:42:34 -0700191 private boolean isExtStorageEncrypted() {
192 String state = SystemProperties.get("vold.decrypt");
193 return !"".equals(state);
194 }
195
Joe Onoratob51886d2010-11-08 18:25:51 -0800196 private void loadAccountList() {
197 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
198 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700199 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800200
201 Context context = getActivity();
202
203 AccountManager mgr = AccountManager.get(context);
204 Account[] accounts = mgr.getAccounts();
205 final int N = accounts.length;
206 if (N == 0) {
207 accountsLabel.setVisibility(View.GONE);
208 contents.setVisibility(View.GONE);
209 return;
210 }
211
212 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
213 Context.LAYOUT_INFLATER_SERVICE);
214
215 AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypes();
216 final int M = descs.length;
217
218 for (int i=0; i<N; i++) {
219 Account account = accounts[i];
220 AuthenticatorDescription desc = null;
221 for (int j=0; j<M; j++) {
222 if (account.type.equals(descs[j].type)) {
223 desc = descs[j];
224 break;
225 }
226 }
227 if (desc == null) {
228 Log.w(TAG, "No descriptor for account name=" + account.name
229 + " type=" + account.type);
230 continue;
231 }
Amith Yamasani07d6fd22011-07-15 18:37:16 -0700232 Drawable icon = null;
Joe Onoratob51886d2010-11-08 18:25:51 -0800233 try {
Amith Yamasani07d6fd22011-07-15 18:37:16 -0700234 if (desc.iconId != 0) {
235 Context authContext = context.createPackageContext(desc.packageName, 0);
236 icon = authContext.getResources().getDrawable(desc.iconId);
237 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800238 } catch (PackageManager.NameNotFoundException e) {
239 Log.w(TAG, "No icon for account type " + desc.type);
Joe Onoratob51886d2010-11-08 18:25:51 -0800240 }
241
242 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
243 contents, false);
244 child.setText(account.name);
245 if (icon != null) {
246 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
247 }
248 contents.addView(child);
249 }
250
251 accountsLabel.setVisibility(View.VISIBLE);
252 contents.setVisibility(View.VISIBLE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800253 }
254
255 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700256 public View onCreateView(LayoutInflater inflater, ViewGroup container,
257 Bundle savedInstanceState) {
258 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800259
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800260 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700261 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800262 }
Amith Yamasani7eedcf42013-07-02 14:15:29 -0700263
264 @Override
265 public void onResume() {
266 super.onResume();
267
268 // If this is the second step after restrictions pin challenge
269 if (mPinConfirmed) {
270 mPinConfirmed = false;
271 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
272 showFinalConfirmation();
273 }
274 }
275 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800276}