blob: d6434680390073285ac6a93cc14cd02cae4bdacf [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;
jackqdyulei0b3edc72016-12-13 17:07:08 -080034import android.support.annotation.VisibleForTesting;
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;
jackqdyulei0b3edc72016-12-13 17:07:08 -080038import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070039import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080040import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070042import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070043import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080044import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080045import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080046import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040047
Tamas Berghammer265d3c22016-06-22 15:34:45 +010048import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000049import com.android.settingslib.RestrictedLockUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010051import java.util.List;
52
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000053import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
54
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055/**
56 * Confirm and execute a reset of the device to a clean "just out of the box"
57 * state. Multiple confirmations are required: first, a general "are you sure
58 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
59 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
60 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
61 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070062 *
63 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064 */
Udam Saini0708d9e2016-03-28 16:35:13 -070065public class MasterClear extends OptionsMenuFragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080066 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067
68 private static final int KEYGUARD_REQUEST = 55;
69
Amith Yamasanib14e1e02010-11-02 09:52:29 -070070 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080071
Amith Yamasanib14e1e02010-11-02 09:52:29 -070072 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070074 private View mExternalStorageContainer;
75 private CheckBox mExternalStorage;
jackqdyulei0b3edc72016-12-13 17:07:08 -080076 private ScrollView mScrollView;
77
78 private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
79 @Override
80 public void onGlobalLayout() {
81 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
82 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
83 }
84 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080085
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070087 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070089 * @param request the request code to be returned once confirmation finishes
90 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 */
Jim Miller2deec7e2010-04-13 17:43:36 -070092 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070093 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -070094 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
95 request, res.getText(R.string.master_clear_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 }
97
98 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070099 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 super.onActivityResult(requestCode, resultCode, data);
101
Julia Reynolds2c539332014-06-11 12:56:02 -0400102 if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800103 return;
104 }
105
106 // If the user entered a valid keyguard trace, present the final
107 // confirmation prompt; otherwise, go back to the initial state.
108 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700109 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800110 } else {
111 establishInitialState();
112 }
113 }
114
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700115 private void showFinalConfirmation() {
Stuart Scottbe903412014-07-24 19:22:06 -0700116 Bundle args = new Bundle();
117 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
118 ((SettingsActivity) getActivity()).startPreferencePanel(MasterClearConfirm.class.getName(),
119 args, R.string.master_clear_confirm_title, null, null, 0);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700120 }
121
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800122 /**
123 * If the user clicks to begin the reset sequence, we next require a
124 * keyguard confirmation if the user has currently enabled one. If there
125 * is no keyguard available, we simply go to the final confirmation prompt.
126 */
Ben Komalo2a321922011-09-07 16:42:34 -0700127 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700128
129 public void onClick(View v) {
130 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
131 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800132 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700134 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135
136 /**
137 * In its initial state, the activity presents a button for the user to
138 * click in order to initiate a confirmation sequence. This method is
139 * called from various other points in the code to reset the activity to
140 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800141 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 * <p>Reinflating views from resources is expensive and prevents us from
143 * caching widget pointers, so we use a single-inflate pattern: we lazy-
144 * inflate each view, caching all of the widget pointers we'll need at the
145 * time, then simply reuse the inflated views directly whenever we need
146 * to change contents.
147 */
148 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700149 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
150 mInitiateButton.setOnClickListener(mInitiateListener);
151 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
152 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800153 mScrollView = (ScrollView) mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800154
Kenny Root3785e392011-01-18 15:14:32 -0800155 /*
156 * If the external storage is emulated, it will be erased with a factory
157 * reset at any rate. There is no need to have a separate option until
158 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700159 * others. Likewise, if it's non-removable storage, it could potentially have been
160 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800161 */
Ben Komalo2a321922011-09-07 16:42:34 -0700162 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
163 if (isExtStorageEmulated
164 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800165 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700166
Kenny Root3785e392011-01-18 15:14:32 -0800167 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
168 externalOption.setVisibility(View.GONE);
169
170 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
171 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700172
173 // If it's not emulated, it is on a separate partition but it means we're doing
174 // a force wipe due to encryption.
175 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800176 } else {
177 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
178
179 @Override
180 public void onClick(View v) {
181 mExternalStorage.toggle();
182 }
183 });
184 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800185
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100186 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
187 loadAccountList(um);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400188 StringBuffer contentDescription = new StringBuffer();
189 View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
190 getContentDescription(masterClearContainer, contentDescription);
191 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800192
193 // Set the status of initiateButton based on scrollview
194 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
195 @Override
196 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
197 int oldScrollY) {
198 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
199 mInitiateButton.setEnabled(true);
200 }
201 }
202 });
203
204 // Set the initial state of the initiateButton
205 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
206 }
207
208 @VisibleForTesting
209 boolean hasReachedBottom(final ScrollView scrollView) {
210 if (scrollView.getChildCount() < 1) {
211 return true;
212 }
213
214 final View view = scrollView.getChildAt(0);
215 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
216
217 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400218 }
219
220 private void getContentDescription(View v, StringBuffer description) {
Jason Monkfda77412016-05-06 14:28:09 -0400221 if (v.getVisibility() != View.VISIBLE) {
222 return;
223 }
Julia Reynoldsce25af42015-07-08 16:56:31 -0400224 if (v instanceof ViewGroup) {
225 ViewGroup vGroup = (ViewGroup) v;
226 for (int i = 0; i < vGroup.getChildCount(); i++) {
227 View nextChild = vGroup.getChildAt(i);
228 getContentDescription(nextChild, description);
229 }
230 } else if (v instanceof TextView) {
231 TextView vText = (TextView) v;
232 description.append(vText.getText());
233 description.append(","); // Allow Talkback to pause between sections.
234 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800235 }
236
Ben Komalo2a321922011-09-07 16:42:34 -0700237 private boolean isExtStorageEncrypted() {
238 String state = SystemProperties.get("vold.decrypt");
239 return !"".equals(state);
240 }
241
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100242 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800243 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
244 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700245 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800246
247 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100248 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
249 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800250
251 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800252
253 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
254 Context.LAYOUT_INFLATER_SERVICE);
255
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100256 int accountsCount = 0;
257 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
258 final UserInfo userInfo = profiles.get(profileIndex);
259 final int profileId = userInfo.id;
260 final UserHandle userHandle = new UserHandle(profileId);
261 Account[] accounts = mgr.getAccountsAsUser(profileId);
262 final int N = accounts.length;
263 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800264 continue;
265 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100266 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800267
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100268 AuthenticatorDescription[] descs = AccountManager.get(context)
269 .getAuthenticatorTypesAsUser(profileId);
270 final int M = descs.length;
271
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +0000272 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100273 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
274 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
275 : R.string.category_personal);
276 contents.addView(titleView);
277
278 for (int i = 0; i < N; i++) {
279 Account account = accounts[i];
280 AuthenticatorDescription desc = null;
281 for (int j = 0; j < M; j++) {
282 if (account.type.equals(descs[j].type)) {
283 desc = descs[j];
284 break;
285 }
286 }
287 if (desc == null) {
288 Log.w(TAG, "No descriptor for account name=" + account.name
289 + " type=" + account.type);
290 continue;
291 }
292 Drawable icon = null;
293 try {
294 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000295 Context authContext = context.createPackageContextAsUser(desc.packageName,
296 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100297 icon = context.getPackageManager().getUserBadgedIcon(
298 authContext.getDrawable(desc.iconId), userHandle);
299 }
300 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100301 Log.w(TAG, "Bad package name for account type " + desc.type);
302 } catch (Resources.NotFoundException e) {
303 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
304 }
305 if (icon == null) {
306 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100307 }
308
Fan Zhang6b2bb392016-09-28 09:07:44 -0700309 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
310 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
311 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100312 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800313 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800314 }
315
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100316 if (accountsCount > 0) {
317 accountsLabel.setVisibility(View.VISIBLE);
318 contents.setVisibility(View.VISIBLE);
319 }
320 // Checking for all other users and their profiles if any.
321 View otherUsers = mContentView.findViewById(R.id.other_users_present);
322 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
323 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800324 }
325
326 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700327 public View onCreateView(LayoutInflater inflater, ViewGroup container,
328 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800329 final Context context = getContext();
330 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
331 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
332 final UserManager um = UserManager.get(context);
333 final boolean disallow = !um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction(
334 context, UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
335 if (disallow && !Utils.isCarrierDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400336 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000337 } else if (admin != null) {
338 View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
339 ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
340 view.setVisibility(View.VISIBLE);
341 return view;
Julia Reynolds2c539332014-06-11 12:56:02 -0400342 }
343
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700344 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800345
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800346 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700347 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800348 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400349
350 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700351 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500352 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400353 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800354}