blob: fa9410d2db89f93702d19a4621f4c10c6d3d77e0 [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;
Christine Franksd7713c92017-01-20 11:24:14 -080023import android.app.FragmentManager;
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;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010027import android.content.pm.UserInfo;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070028import android.content.res.Resources;
Joe Onoratob51886d2010-11-08 18:25:51 -080029import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080031import android.os.Environment;
Ben Komalo2a321922011-09-07 16:42:34 -070032import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010033import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070034import android.os.UserManager;
jackqdyulei0b3edc72016-12-13 17:07:08 -080035import android.support.annotation.VisibleForTesting;
Joe Onoratob51886d2010-11-08 18:25:51 -080036import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037import android.view.LayoutInflater;
38import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080039import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070040import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080041import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070043import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070044import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080045import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080046import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080047import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040048
Tamas Berghammer265d3c22016-06-22 15:34:45 +010049import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Christine Franksd7713c92017-01-20 11:24:14 -080050import com.android.settings.widget.CarrierDemoPasswordDialogFragment;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000051import com.android.settingslib.RestrictedLockUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010053import java.util.List;
54
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000055import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
56
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057/**
58 * Confirm and execute a reset of the device to a clean "just out of the box"
59 * state. Multiple confirmations are required: first, a general "are you sure
60 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
61 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
62 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
63 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070064 *
65 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066 */
Christine Franksd7713c92017-01-20 11:24:14 -080067public class MasterClear extends OptionsMenuFragment
68 implements CarrierDemoPasswordDialogFragment.Callback {
Joe Onoratob51886d2010-11-08 18:25:51 -080069 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070
71 private static final int KEYGUARD_REQUEST = 55;
72
Amith Yamasanib14e1e02010-11-02 09:52:29 -070073 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080074
Amith Yamasanib14e1e02010-11-02 09:52:29 -070075 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070077 private View mExternalStorageContainer;
78 private CheckBox mExternalStorage;
jackqdyulei0b3edc72016-12-13 17:07:08 -080079 private ScrollView mScrollView;
80
81 private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
82 @Override
83 public void onGlobalLayout() {
84 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
85 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
86 }
87 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070090 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070092 * @param request the request code to be returned once confirmation finishes
93 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 */
Jim Miller2deec7e2010-04-13 17:43:36 -070095 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070096 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -070097 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
98 request, res.getText(R.string.master_clear_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099 }
100
101 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700102 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800103 super.onActivityResult(requestCode, resultCode, data);
104
Julia Reynolds2c539332014-06-11 12:56:02 -0400105 if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106 return;
107 }
108
109 // If the user entered a valid keyguard trace, present the final
110 // confirmation prompt; otherwise, go back to the initial state.
111 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700112 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 } else {
114 establishInitialState();
115 }
116 }
117
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700118 private void showFinalConfirmation() {
Stuart Scottbe903412014-07-24 19:22:06 -0700119 Bundle args = new Bundle();
120 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Fan Zhangc6ca3142017-02-14 15:02:35 -0800121 ((SettingsActivity) getActivity()).startPreferencePanel(
122 this, MasterClearConfirm.class.getName(),
Stuart Scottbe903412014-07-24 19:22:06 -0700123 args, R.string.master_clear_confirm_title, null, null, 0);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700124 }
125
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800126 /**
127 * If the user clicks to begin the reset sequence, we next require a
128 * keyguard confirmation if the user has currently enabled one. If there
129 * is no keyguard available, we simply go to the final confirmation prompt.
130 */
Ben Komalo2a321922011-09-07 16:42:34 -0700131 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700132
133 public void onClick(View v) {
Christine Franksd7713c92017-01-20 11:24:14 -0800134 if ( Utils.isCarrierDemoUser(v.getContext())) {
135 // Require the carrier password before displaying the final confirmation.
136 final FragmentManager fm = getChildFragmentManager();
137 if (fm != null && !fm.isDestroyed()) {
138 new CarrierDemoPasswordDialogFragment().show(fm, null /* tag */);
139 }
140 } else if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700141 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800143 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700144 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800145
Christine Franksd7713c92017-01-20 11:24:14 -0800146 @Override
147 public void onPasswordVerified() {
148 showFinalConfirmation();
149 }
150
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800151 /**
152 * In its initial state, the activity presents a button for the user to
153 * click in order to initiate a confirmation sequence. This method is
154 * called from various other points in the code to reset the activity to
155 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800156 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800157 * <p>Reinflating views from resources is expensive and prevents us from
158 * caching widget pointers, so we use a single-inflate pattern: we lazy-
159 * inflate each view, caching all of the widget pointers we'll need at the
160 * time, then simply reuse the inflated views directly whenever we need
161 * to change contents.
162 */
163 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700164 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
165 mInitiateButton.setOnClickListener(mInitiateListener);
166 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
167 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800168 mScrollView = (ScrollView) mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800169
Kenny Root3785e392011-01-18 15:14:32 -0800170 /*
171 * If the external storage is emulated, it will be erased with a factory
172 * reset at any rate. There is no need to have a separate option until
173 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700174 * others. Likewise, if it's non-removable storage, it could potentially have been
175 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800176 */
Ben Komalo2a321922011-09-07 16:42:34 -0700177 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
178 if (isExtStorageEmulated
179 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800180 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700181
Kenny Root3785e392011-01-18 15:14:32 -0800182 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
183 externalOption.setVisibility(View.GONE);
184
185 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
186 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700187
188 // If it's not emulated, it is on a separate partition but it means we're doing
189 // a force wipe due to encryption.
190 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800191 } else {
192 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
193
194 @Override
195 public void onClick(View v) {
196 mExternalStorage.toggle();
197 }
198 });
199 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800200
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100201 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
202 loadAccountList(um);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400203 StringBuffer contentDescription = new StringBuffer();
204 View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
205 getContentDescription(masterClearContainer, contentDescription);
206 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800207
208 // Set the status of initiateButton based on scrollview
209 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
210 @Override
211 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
212 int oldScrollY) {
213 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
214 mInitiateButton.setEnabled(true);
215 }
216 }
217 });
218
219 // Set the initial state of the initiateButton
220 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
221 }
222
223 @VisibleForTesting
224 boolean hasReachedBottom(final ScrollView scrollView) {
225 if (scrollView.getChildCount() < 1) {
226 return true;
227 }
228
229 final View view = scrollView.getChildAt(0);
230 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
231
232 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400233 }
234
235 private void getContentDescription(View v, StringBuffer description) {
Jason Monkfda77412016-05-06 14:28:09 -0400236 if (v.getVisibility() != View.VISIBLE) {
237 return;
238 }
Julia Reynoldsce25af42015-07-08 16:56:31 -0400239 if (v instanceof ViewGroup) {
240 ViewGroup vGroup = (ViewGroup) v;
241 for (int i = 0; i < vGroup.getChildCount(); i++) {
242 View nextChild = vGroup.getChildAt(i);
243 getContentDescription(nextChild, description);
244 }
245 } else if (v instanceof TextView) {
246 TextView vText = (TextView) v;
247 description.append(vText.getText());
248 description.append(","); // Allow Talkback to pause between sections.
249 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800250 }
251
Ben Komalo2a321922011-09-07 16:42:34 -0700252 private boolean isExtStorageEncrypted() {
253 String state = SystemProperties.get("vold.decrypt");
254 return !"".equals(state);
255 }
256
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100257 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800258 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
259 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700260 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800261
262 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100263 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
264 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800265
266 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800267
268 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
269 Context.LAYOUT_INFLATER_SERVICE);
270
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100271 int accountsCount = 0;
272 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
273 final UserInfo userInfo = profiles.get(profileIndex);
274 final int profileId = userInfo.id;
275 final UserHandle userHandle = new UserHandle(profileId);
276 Account[] accounts = mgr.getAccountsAsUser(profileId);
277 final int N = accounts.length;
278 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800279 continue;
280 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100281 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800282
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100283 AuthenticatorDescription[] descs = AccountManager.get(context)
284 .getAuthenticatorTypesAsUser(profileId);
285 final int M = descs.length;
286
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +0000287 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100288 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
289 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
290 : R.string.category_personal);
291 contents.addView(titleView);
292
293 for (int i = 0; i < N; i++) {
294 Account account = accounts[i];
295 AuthenticatorDescription desc = null;
296 for (int j = 0; j < M; j++) {
297 if (account.type.equals(descs[j].type)) {
298 desc = descs[j];
299 break;
300 }
301 }
302 if (desc == null) {
303 Log.w(TAG, "No descriptor for account name=" + account.name
304 + " type=" + account.type);
305 continue;
306 }
307 Drawable icon = null;
308 try {
309 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000310 Context authContext = context.createPackageContextAsUser(desc.packageName,
311 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100312 icon = context.getPackageManager().getUserBadgedIcon(
313 authContext.getDrawable(desc.iconId), userHandle);
314 }
315 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100316 Log.w(TAG, "Bad package name for account type " + desc.type);
317 } catch (Resources.NotFoundException e) {
318 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
319 }
320 if (icon == null) {
321 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100322 }
323
Fan Zhang6b2bb392016-09-28 09:07:44 -0700324 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
325 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
326 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100327 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800328 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800329 }
330
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100331 if (accountsCount > 0) {
332 accountsLabel.setVisibility(View.VISIBLE);
333 contents.setVisibility(View.VISIBLE);
334 }
335 // Checking for all other users and their profiles if any.
336 View otherUsers = mContentView.findViewById(R.id.other_users_present);
337 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
338 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800339 }
340
341 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700342 public View onCreateView(LayoutInflater inflater, ViewGroup container,
343 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800344 final Context context = getContext();
345 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
346 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
347 final UserManager um = UserManager.get(context);
348 final boolean disallow = !um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction(
349 context, UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
350 if (disallow && !Utils.isCarrierDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400351 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000352 } else if (admin != null) {
353 View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
354 ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
355 view.setVisibility(View.VISIBLE);
356 return view;
Julia Reynolds2c539332014-06-11 12:56:02 -0400357 }
358
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700359 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800360
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800361 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700362 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800363 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400364
365 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700366 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500367 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400368 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800369}