blob: 5fd4f46a3aa015880bc4b05976c194993de4db1c [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());
121 ((SettingsActivity) getActivity()).startPreferencePanel(MasterClearConfirm.class.getName(),
122 args, R.string.master_clear_confirm_title, null, null, 0);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700123 }
124
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 /**
126 * If the user clicks to begin the reset sequence, we next require a
127 * keyguard confirmation if the user has currently enabled one. If there
128 * is no keyguard available, we simply go to the final confirmation prompt.
129 */
Ben Komalo2a321922011-09-07 16:42:34 -0700130 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700131
132 public void onClick(View v) {
Christine Franksd7713c92017-01-20 11:24:14 -0800133 if ( Utils.isCarrierDemoUser(v.getContext())) {
134 // Require the carrier password before displaying the final confirmation.
135 final FragmentManager fm = getChildFragmentManager();
136 if (fm != null && !fm.isDestroyed()) {
137 new CarrierDemoPasswordDialogFragment().show(fm, null /* tag */);
138 }
139 } else if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700140 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800141 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700143 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800144
Christine Franksd7713c92017-01-20 11:24:14 -0800145 @Override
146 public void onPasswordVerified() {
147 showFinalConfirmation();
148 }
149
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 /**
151 * In its initial state, the activity presents a button for the user to
152 * click in order to initiate a confirmation sequence. This method is
153 * called from various other points in the code to reset the activity to
154 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800155 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800156 * <p>Reinflating views from resources is expensive and prevents us from
157 * caching widget pointers, so we use a single-inflate pattern: we lazy-
158 * inflate each view, caching all of the widget pointers we'll need at the
159 * time, then simply reuse the inflated views directly whenever we need
160 * to change contents.
161 */
162 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700163 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
164 mInitiateButton.setOnClickListener(mInitiateListener);
165 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
166 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800167 mScrollView = (ScrollView) mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800168
Kenny Root3785e392011-01-18 15:14:32 -0800169 /*
170 * If the external storage is emulated, it will be erased with a factory
171 * reset at any rate. There is no need to have a separate option until
172 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700173 * others. Likewise, if it's non-removable storage, it could potentially have been
174 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800175 */
Ben Komalo2a321922011-09-07 16:42:34 -0700176 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
177 if (isExtStorageEmulated
178 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800179 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700180
Kenny Root3785e392011-01-18 15:14:32 -0800181 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
182 externalOption.setVisibility(View.GONE);
183
184 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
185 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700186
187 // If it's not emulated, it is on a separate partition but it means we're doing
188 // a force wipe due to encryption.
189 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800190 } else {
191 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
192
193 @Override
194 public void onClick(View v) {
195 mExternalStorage.toggle();
196 }
197 });
198 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800199
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100200 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
201 loadAccountList(um);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400202 StringBuffer contentDescription = new StringBuffer();
203 View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
204 getContentDescription(masterClearContainer, contentDescription);
205 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800206
207 // Set the status of initiateButton based on scrollview
208 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
209 @Override
210 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
211 int oldScrollY) {
212 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
213 mInitiateButton.setEnabled(true);
214 }
215 }
216 });
217
218 // Set the initial state of the initiateButton
219 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
220 }
221
222 @VisibleForTesting
223 boolean hasReachedBottom(final ScrollView scrollView) {
224 if (scrollView.getChildCount() < 1) {
225 return true;
226 }
227
228 final View view = scrollView.getChildAt(0);
229 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
230
231 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400232 }
233
234 private void getContentDescription(View v, StringBuffer description) {
Jason Monkfda77412016-05-06 14:28:09 -0400235 if (v.getVisibility() != View.VISIBLE) {
236 return;
237 }
Julia Reynoldsce25af42015-07-08 16:56:31 -0400238 if (v instanceof ViewGroup) {
239 ViewGroup vGroup = (ViewGroup) v;
240 for (int i = 0; i < vGroup.getChildCount(); i++) {
241 View nextChild = vGroup.getChildAt(i);
242 getContentDescription(nextChild, description);
243 }
244 } else if (v instanceof TextView) {
245 TextView vText = (TextView) v;
246 description.append(vText.getText());
247 description.append(","); // Allow Talkback to pause between sections.
248 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800249 }
250
Ben Komalo2a321922011-09-07 16:42:34 -0700251 private boolean isExtStorageEncrypted() {
252 String state = SystemProperties.get("vold.decrypt");
253 return !"".equals(state);
254 }
255
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100256 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800257 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
258 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700259 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800260
261 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100262 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
263 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800264
265 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800266
267 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
268 Context.LAYOUT_INFLATER_SERVICE);
269
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100270 int accountsCount = 0;
271 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
272 final UserInfo userInfo = profiles.get(profileIndex);
273 final int profileId = userInfo.id;
274 final UserHandle userHandle = new UserHandle(profileId);
275 Account[] accounts = mgr.getAccountsAsUser(profileId);
276 final int N = accounts.length;
277 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800278 continue;
279 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100280 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800281
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100282 AuthenticatorDescription[] descs = AccountManager.get(context)
283 .getAuthenticatorTypesAsUser(profileId);
284 final int M = descs.length;
285
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +0000286 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100287 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
288 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
289 : R.string.category_personal);
290 contents.addView(titleView);
291
292 for (int i = 0; i < N; i++) {
293 Account account = accounts[i];
294 AuthenticatorDescription desc = null;
295 for (int j = 0; j < M; j++) {
296 if (account.type.equals(descs[j].type)) {
297 desc = descs[j];
298 break;
299 }
300 }
301 if (desc == null) {
302 Log.w(TAG, "No descriptor for account name=" + account.name
303 + " type=" + account.type);
304 continue;
305 }
306 Drawable icon = null;
307 try {
308 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000309 Context authContext = context.createPackageContextAsUser(desc.packageName,
310 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100311 icon = context.getPackageManager().getUserBadgedIcon(
312 authContext.getDrawable(desc.iconId), userHandle);
313 }
314 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100315 Log.w(TAG, "Bad package name for account type " + desc.type);
316 } catch (Resources.NotFoundException e) {
317 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
318 }
319 if (icon == null) {
320 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100321 }
322
Fan Zhang6b2bb392016-09-28 09:07:44 -0700323 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
324 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
325 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100326 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800327 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800328 }
329
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100330 if (accountsCount > 0) {
331 accountsLabel.setVisibility(View.VISIBLE);
332 contents.setVisibility(View.VISIBLE);
333 }
334 // Checking for all other users and their profiles if any.
335 View otherUsers = mContentView.findViewById(R.id.other_users_present);
336 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
337 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800338 }
339
340 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700341 public View onCreateView(LayoutInflater inflater, ViewGroup container,
342 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800343 final Context context = getContext();
344 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
345 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
346 final UserManager um = UserManager.get(context);
347 final boolean disallow = !um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction(
348 context, UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
349 if (disallow && !Utils.isCarrierDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400350 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000351 } else if (admin != null) {
352 View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
353 ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
354 view.setVisibility(View.VISIBLE);
355 return view;
Julia Reynolds2c539332014-06-11 12:56:02 -0400356 }
357
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700358 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800359
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800360 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700361 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800362 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400363
364 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700365 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500366 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400367 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800368}