blob: eaf29c6e7ecfa6aac9a2295ec641f467924ab0d8 [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;
Joe Onoratob51886d2010-11-08 18:25:51 -080034import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035import android.view.LayoutInflater;
36import android.view.View;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070037import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070039import android.widget.CheckBox;
Joe Onoratob51886d2010-11-08 18:25:51 -080040import android.widget.LinearLayout;
41import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042
43/**
44 * Confirm and execute a reset of the device to a clean "just out of the box"
45 * state. Multiple confirmations are required: first, a general "are you sure
46 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
47 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
48 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
49 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070050 *
51 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052 */
Amith Yamasanib14e1e02010-11-02 09:52:29 -070053public class MasterClear extends Fragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080054 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055
56 private static final int KEYGUARD_REQUEST = 55;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070057 private static final int PIN_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080058
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;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070065 private boolean mPinConfirmed;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070068 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070070 * @param request the request code to be returned once confirmation finishes
71 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 */
Jim Miller2deec7e2010-04-13 17:43:36 -070073 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070074 Resources res = getActivity().getResources();
75 return new ChooseLockSettingsHelper(getActivity(), this)
Jim Miller2deec7e2010-04-13 17:43:36 -070076 .launchConfirmationActivity(request,
Amith Yamasanib14e1e02010-11-02 09:52:29 -070077 res.getText(R.string.master_clear_gesture_prompt),
78 res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080079 }
80
Amith Yamasani7eedcf42013-07-02 14:15:29 -070081 private boolean runRestrictionsChallenge() {
Amith Yamasani14912662013-09-05 12:40:31 -070082 if (UserManager.get(getActivity()).hasRestrictionsChallenge()) {
Amith Yamasani7eedcf42013-07-02 14:15:29 -070083 startActivityForResult(
Amith Yamasani14912662013-09-05 12:40:31 -070084 new Intent(Intent.ACTION_RESTRICTIONS_CHALLENGE), PIN_REQUEST);
Amith Yamasani7eedcf42013-07-02 14:15:29 -070085 return true;
86 }
87 return false;
88 }
89
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070091 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 super.onActivityResult(requestCode, resultCode, data);
93
Amith Yamasani7eedcf42013-07-02 14:15:29 -070094 if (requestCode == PIN_REQUEST) {
95 if (resultCode == Activity.RESULT_OK) {
96 mPinConfirmed = true;
97 }
98 return;
99 } else if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 return;
101 }
102
103 // If the user entered a valid keyguard trace, present the final
104 // confirmation prompt; otherwise, go back to the initial state.
105 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700106 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 } else {
108 establishInitialState();
109 }
110 }
111
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700112 private void showFinalConfirmation() {
113 Preference preference = new Preference(getActivity());
114 preference.setFragment(MasterClearConfirm.class.getName());
115 preference.setTitle(R.string.master_clear_confirm_title);
116 preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117 ((SettingsActivity) getActivity()).onPreferenceStartFragment(null, preference);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700118 }
119
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120 /**
121 * If the user clicks to begin the reset sequence, we next require a
122 * keyguard confirmation if the user has currently enabled one. If there
123 * is no keyguard available, we simply go to the final confirmation prompt.
124 */
Ben Komalo2a321922011-09-07 16:42:34 -0700125 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700126
127 public void onClick(View v) {
Amith Yamasani7eedcf42013-07-02 14:15:29 -0700128 mPinConfirmed = false;
129 if (runRestrictionsChallenge()) {
130 return;
131 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700132 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
133 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700136 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800137
138 /**
139 * In its initial state, the activity presents a button for the user to
140 * click in order to initiate a confirmation sequence. This method is
141 * called from various other points in the code to reset the activity to
142 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800143 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800144 * <p>Reinflating views from resources is expensive and prevents us from
145 * caching widget pointers, so we use a single-inflate pattern: we lazy-
146 * inflate each view, caching all of the widget pointers we'll need at the
147 * time, then simply reuse the inflated views directly whenever we need
148 * to change contents.
149 */
150 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700151 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
152 mInitiateButton.setOnClickListener(mInitiateListener);
153 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
154 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800155
Kenny Root3785e392011-01-18 15:14:32 -0800156 /*
157 * If the external storage is emulated, it will be erased with a factory
158 * reset at any rate. There is no need to have a separate option until
159 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700160 * others. Likewise, if it's non-removable storage, it could potentially have been
161 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800162 */
Ben Komalo2a321922011-09-07 16:42:34 -0700163 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
164 if (isExtStorageEmulated
165 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800166 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700167
Kenny Root3785e392011-01-18 15:14:32 -0800168 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
169 externalOption.setVisibility(View.GONE);
170
171 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
172 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700173
174 // If it's not emulated, it is on a separate partition but it means we're doing
175 // a force wipe due to encryption.
176 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800177 } else {
178 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
179
180 @Override
181 public void onClick(View v) {
182 mExternalStorage.toggle();
183 }
184 });
185 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800186
187 loadAccountList();
188 }
189
Ben Komalo2a321922011-09-07 16:42:34 -0700190 private boolean isExtStorageEncrypted() {
191 String state = SystemProperties.get("vold.decrypt");
192 return !"".equals(state);
193 }
194
Joe Onoratob51886d2010-11-08 18:25:51 -0800195 private void loadAccountList() {
196 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
197 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700198 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800199
200 Context context = getActivity();
201
202 AccountManager mgr = AccountManager.get(context);
203 Account[] accounts = mgr.getAccounts();
204 final int N = accounts.length;
205 if (N == 0) {
206 accountsLabel.setVisibility(View.GONE);
207 contents.setVisibility(View.GONE);
208 return;
209 }
210
211 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
212 Context.LAYOUT_INFLATER_SERVICE);
213
214 AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypes();
215 final int M = descs.length;
216
217 for (int i=0; i<N; i++) {
218 Account account = accounts[i];
219 AuthenticatorDescription desc = null;
220 for (int j=0; j<M; j++) {
221 if (account.type.equals(descs[j].type)) {
222 desc = descs[j];
223 break;
224 }
225 }
226 if (desc == null) {
227 Log.w(TAG, "No descriptor for account name=" + account.name
228 + " type=" + account.type);
229 continue;
230 }
Amith Yamasani07d6fd22011-07-15 18:37:16 -0700231 Drawable icon = null;
Joe Onoratob51886d2010-11-08 18:25:51 -0800232 try {
Amith Yamasani07d6fd22011-07-15 18:37:16 -0700233 if (desc.iconId != 0) {
234 Context authContext = context.createPackageContext(desc.packageName, 0);
235 icon = authContext.getResources().getDrawable(desc.iconId);
236 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800237 } catch (PackageManager.NameNotFoundException e) {
238 Log.w(TAG, "No icon for account type " + desc.type);
Joe Onoratob51886d2010-11-08 18:25:51 -0800239 }
240
241 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
242 contents, false);
243 child.setText(account.name);
244 if (icon != null) {
245 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
246 }
247 contents.addView(child);
248 }
249
250 accountsLabel.setVisibility(View.VISIBLE);
251 contents.setVisibility(View.VISIBLE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800252 }
253
254 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700255 public View onCreateView(LayoutInflater inflater, ViewGroup container,
256 Bundle savedInstanceState) {
257 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800258
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800259 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700260 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800261 }
Amith Yamasani7eedcf42013-07-02 14:15:29 -0700262
263 @Override
264 public void onResume() {
265 super.onResume();
266
267 // If this is the second step after restrictions pin challenge
268 if (mPinConfirmed) {
269 mPinConfirmed = false;
270 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
271 showFinalConfirmation();
272 }
273 }
274 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800275}