blob: 8483114ebc9f679b363d4aebc42b8ad76bcf53f2 [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;
Joe Onoratob51886d2010-11-08 18:25:51 -080023import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080025import android.content.pm.PackageManager;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010026import android.content.pm.UserInfo;
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;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010032import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070033import android.os.UserManager;
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;
Fan Zhang6b2bb392016-09-28 09:07:44 -070040import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080041import android.widget.LinearLayout;
42import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040043
Chris Wren9d1bfd12016-01-26 18:04:01 -050044import com.android.internal.logging.MetricsProto.MetricsEvent;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000045import com.android.settingslib.RestrictedLockUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080046
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010047import java.util.List;
48
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000049import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
50
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051/**
52 * Confirm and execute a reset of the device to a clean "just out of the box"
53 * state. Multiple confirmations are required: first, a general "are you sure
54 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
55 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
56 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
57 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070058 *
59 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060 */
Udam Saini0708d9e2016-03-28 16:35:13 -070061public class MasterClear extends OptionsMenuFragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080062 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063
64 private static final int KEYGUARD_REQUEST = 55;
65
Amith Yamasanib14e1e02010-11-02 09:52:29 -070066 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067
Amith Yamasanib14e1e02010-11-02 09:52:29 -070068 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070070 private View mExternalStorageContainer;
71 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070074 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070076 * @param request the request code to be returned once confirmation finishes
77 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080078 */
Jim Miller2deec7e2010-04-13 17:43:36 -070079 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070080 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -070081 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
82 request, res.getText(R.string.master_clear_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080083 }
84
85 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070086 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080087 super.onActivityResult(requestCode, resultCode, data);
88
Julia Reynolds2c539332014-06-11 12:56:02 -040089 if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 return;
91 }
92
93 // If the user entered a valid keyguard trace, present the final
94 // confirmation prompt; otherwise, go back to the initial state.
95 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070096 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 } else {
98 establishInitialState();
99 }
100 }
101
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700102 private void showFinalConfirmation() {
Stuart Scottbe903412014-07-24 19:22:06 -0700103 Bundle args = new Bundle();
104 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
105 ((SettingsActivity) getActivity()).startPreferencePanel(MasterClearConfirm.class.getName(),
106 args, R.string.master_clear_confirm_title, null, null, 0);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700107 }
108
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800109 /**
110 * If the user clicks to begin the reset sequence, we next require a
111 * keyguard confirmation if the user has currently enabled one. If there
112 * is no keyguard available, we simply go to the final confirmation prompt.
113 */
Ben Komalo2a321922011-09-07 16:42:34 -0700114 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700115
116 public void onClick(View v) {
117 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
118 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700121 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800122
123 /**
124 * In its initial state, the activity presents a button for the user to
125 * click in order to initiate a confirmation sequence. This method is
126 * called from various other points in the code to reset the activity to
127 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800128 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 * <p>Reinflating views from resources is expensive and prevents us from
130 * caching widget pointers, so we use a single-inflate pattern: we lazy-
131 * inflate each view, caching all of the widget pointers we'll need at the
132 * time, then simply reuse the inflated views directly whenever we need
133 * to change contents.
134 */
135 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700136 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
137 mInitiateButton.setOnClickListener(mInitiateListener);
138 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
139 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800140
Kenny Root3785e392011-01-18 15:14:32 -0800141 /*
142 * If the external storage is emulated, it will be erased with a factory
143 * reset at any rate. There is no need to have a separate option until
144 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700145 * others. Likewise, if it's non-removable storage, it could potentially have been
146 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800147 */
Ben Komalo2a321922011-09-07 16:42:34 -0700148 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
149 if (isExtStorageEmulated
150 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800151 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700152
Kenny Root3785e392011-01-18 15:14:32 -0800153 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
154 externalOption.setVisibility(View.GONE);
155
156 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
157 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700158
159 // If it's not emulated, it is on a separate partition but it means we're doing
160 // a force wipe due to encryption.
161 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800162 } else {
163 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
164
165 @Override
166 public void onClick(View v) {
167 mExternalStorage.toggle();
168 }
169 });
170 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800171
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100172 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
173 loadAccountList(um);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400174 StringBuffer contentDescription = new StringBuffer();
175 View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
176 getContentDescription(masterClearContainer, contentDescription);
177 masterClearContainer.setContentDescription(contentDescription);
178 }
179
180 private void getContentDescription(View v, StringBuffer description) {
Jason Monkfda77412016-05-06 14:28:09 -0400181 if (v.getVisibility() != View.VISIBLE) {
182 return;
183 }
Julia Reynoldsce25af42015-07-08 16:56:31 -0400184 if (v instanceof ViewGroup) {
185 ViewGroup vGroup = (ViewGroup) v;
186 for (int i = 0; i < vGroup.getChildCount(); i++) {
187 View nextChild = vGroup.getChildAt(i);
188 getContentDescription(nextChild, description);
189 }
190 } else if (v instanceof TextView) {
191 TextView vText = (TextView) v;
192 description.append(vText.getText());
193 description.append(","); // Allow Talkback to pause between sections.
194 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800195 }
196
Ben Komalo2a321922011-09-07 16:42:34 -0700197 private boolean isExtStorageEncrypted() {
198 String state = SystemProperties.get("vold.decrypt");
199 return !"".equals(state);
200 }
201
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100202 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800203 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
204 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700205 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800206
207 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100208 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
209 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800210
211 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800212
213 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
214 Context.LAYOUT_INFLATER_SERVICE);
215
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100216 int accountsCount = 0;
217 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
218 final UserInfo userInfo = profiles.get(profileIndex);
219 final int profileId = userInfo.id;
220 final UserHandle userHandle = new UserHandle(profileId);
221 Account[] accounts = mgr.getAccountsAsUser(profileId);
222 final int N = accounts.length;
223 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800224 continue;
225 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100226 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800227
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100228 AuthenticatorDescription[] descs = AccountManager.get(context)
229 .getAuthenticatorTypesAsUser(profileId);
230 final int M = descs.length;
231
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +0000232 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100233 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
234 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
235 : R.string.category_personal);
236 contents.addView(titleView);
237
238 for (int i = 0; i < N; i++) {
239 Account account = accounts[i];
240 AuthenticatorDescription desc = null;
241 for (int j = 0; j < M; j++) {
242 if (account.type.equals(descs[j].type)) {
243 desc = descs[j];
244 break;
245 }
246 }
247 if (desc == null) {
248 Log.w(TAG, "No descriptor for account name=" + account.name
249 + " type=" + account.type);
250 continue;
251 }
252 Drawable icon = null;
253 try {
254 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000255 Context authContext = context.createPackageContextAsUser(desc.packageName,
256 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100257 icon = context.getPackageManager().getUserBadgedIcon(
258 authContext.getDrawable(desc.iconId), userHandle);
259 }
260 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100261 Log.w(TAG, "Bad package name for account type " + desc.type);
262 } catch (Resources.NotFoundException e) {
263 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
264 }
265 if (icon == null) {
266 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100267 }
268
Fan Zhang6b2bb392016-09-28 09:07:44 -0700269 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
270 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
271 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100272 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800273 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800274 }
275
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100276 if (accountsCount > 0) {
277 accountsLabel.setVisibility(View.VISIBLE);
278 contents.setVisibility(View.VISIBLE);
279 }
280 // Checking for all other users and their profiles if any.
281 View otherUsers = mContentView.findViewById(R.id.other_users_present);
282 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
283 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800284 }
285
286 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700287 public View onCreateView(LayoutInflater inflater, ViewGroup container,
288 Bundle savedInstanceState) {
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000289 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
290 getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Xiaohui Chen44879a32015-07-22 13:53:22 -0700291 final UserManager um = UserManager.get(getActivity());
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000292 if (!um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
293 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400294 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000295 } else if (admin != null) {
296 View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
297 ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
298 view.setVisibility(View.VISIBLE);
299 return view;
Julia Reynolds2c539332014-06-11 12:56:02 -0400300 }
301
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700302 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800303
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800304 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700305 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800306 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400307
308 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700309 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500310 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400311 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800312}