blob: 1b6468fd116a9134a2bdfec619b057cfe016c236 [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;
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;
Amith Yamasani4339a472014-10-22 13:51:53 -070032import android.os.Process;
Ben Komalo2a321922011-09-07 16:42:34 -070033import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010034import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070035import android.os.UserManager;
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;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070039import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070041import android.widget.CheckBox;
Joe Onoratob51886d2010-11-08 18:25:51 -080042import android.widget.LinearLayout;
43import android.widget.TextView;
Chris Wren8a963ba2015-03-20 10:29:14 -040044import com.android.internal.logging.MetricsLogger;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010046import java.util.List;
47
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048/**
49 * Confirm and execute a reset of the device to a clean "just out of the box"
50 * state. Multiple confirmations are required: first, a general "are you sure
51 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
52 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
53 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
54 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070055 *
56 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057 */
Chris Wren8a963ba2015-03-20 10:29:14 -040058public class MasterClear extends InstrumentedFragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080059 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060
61 private static final int KEYGUARD_REQUEST = 55;
62
Amith Yamasanib14e1e02010-11-02 09:52:29 -070063 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064
Amith Yamasanib14e1e02010-11-02 09:52:29 -070065 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070067 private View mExternalStorageContainer;
68 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070071 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070073 * @param request the request code to be returned once confirmation finishes
74 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075 */
Jim Miller2deec7e2010-04-13 17:43:36 -070076 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070077 Resources res = getActivity().getResources();
Stuart Scottbe903412014-07-24 19:22:06 -070078 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(request,
79 null, res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 }
81
82 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070083 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 super.onActivityResult(requestCode, resultCode, data);
85
Julia Reynolds2c539332014-06-11 12:56:02 -040086 if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080087 return;
88 }
89
90 // If the user entered a valid keyguard trace, present the final
91 // confirmation prompt; otherwise, go back to the initial state.
92 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070093 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 } else {
95 establishInitialState();
96 }
97 }
98
Amith Yamasanib14e1e02010-11-02 09:52:29 -070099 private void showFinalConfirmation() {
Stuart Scottbe903412014-07-24 19:22:06 -0700100 Bundle args = new Bundle();
101 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
102 ((SettingsActivity) getActivity()).startPreferencePanel(MasterClearConfirm.class.getName(),
103 args, R.string.master_clear_confirm_title, null, null, 0);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700104 }
105
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106 /**
107 * If the user clicks to begin the reset sequence, we next require a
108 * keyguard confirmation if the user has currently enabled one. If there
109 * is no keyguard available, we simply go to the final confirmation prompt.
110 */
Ben Komalo2a321922011-09-07 16:42:34 -0700111 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700112
113 public void onClick(View v) {
114 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
115 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800116 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700118 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119
120 /**
121 * In its initial state, the activity presents a button for the user to
122 * click in order to initiate a confirmation sequence. This method is
123 * called from various other points in the code to reset the activity to
124 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800125 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800126 * <p>Reinflating views from resources is expensive and prevents us from
127 * caching widget pointers, so we use a single-inflate pattern: we lazy-
128 * inflate each view, caching all of the widget pointers we'll need at the
129 * time, then simply reuse the inflated views directly whenever we need
130 * to change contents.
131 */
132 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700133 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
134 mInitiateButton.setOnClickListener(mInitiateListener);
135 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
136 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800137
Kenny Root3785e392011-01-18 15:14:32 -0800138 /*
139 * If the external storage is emulated, it will be erased with a factory
140 * reset at any rate. There is no need to have a separate option until
141 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700142 * others. Likewise, if it's non-removable storage, it could potentially have been
143 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800144 */
Ben Komalo2a321922011-09-07 16:42:34 -0700145 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
146 if (isExtStorageEmulated
147 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800148 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700149
Kenny Root3785e392011-01-18 15:14:32 -0800150 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
151 externalOption.setVisibility(View.GONE);
152
153 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
154 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700155
156 // If it's not emulated, it is on a separate partition but it means we're doing
157 // a force wipe due to encryption.
158 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800159 } else {
160 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
161
162 @Override
163 public void onClick(View v) {
164 mExternalStorage.toggle();
165 }
166 });
167 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800168
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100169 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
170 loadAccountList(um);
Joe Onoratob51886d2010-11-08 18:25:51 -0800171 }
172
Ben Komalo2a321922011-09-07 16:42:34 -0700173 private boolean isExtStorageEncrypted() {
174 String state = SystemProperties.get("vold.decrypt");
175 return !"".equals(state);
176 }
177
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100178 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800179 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
180 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700181 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800182
183 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100184 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
185 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800186
187 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800188
189 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
190 Context.LAYOUT_INFLATER_SERVICE);
191
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100192 int accountsCount = 0;
193 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
194 final UserInfo userInfo = profiles.get(profileIndex);
195 final int profileId = userInfo.id;
196 final UserHandle userHandle = new UserHandle(profileId);
197 Account[] accounts = mgr.getAccountsAsUser(profileId);
198 final int N = accounts.length;
199 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800200 continue;
201 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100202 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800203
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100204 AuthenticatorDescription[] descs = AccountManager.get(context)
205 .getAuthenticatorTypesAsUser(profileId);
206 final int M = descs.length;
207
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +0000208 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100209 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
210 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
211 : R.string.category_personal);
212 contents.addView(titleView);
213
214 for (int i = 0; i < N; i++) {
215 Account account = accounts[i];
216 AuthenticatorDescription desc = null;
217 for (int j = 0; j < M; j++) {
218 if (account.type.equals(descs[j].type)) {
219 desc = descs[j];
220 break;
221 }
222 }
223 if (desc == null) {
224 Log.w(TAG, "No descriptor for account name=" + account.name
225 + " type=" + account.type);
226 continue;
227 }
228 Drawable icon = null;
229 try {
230 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000231 Context authContext = context.createPackageContextAsUser(desc.packageName,
232 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100233 icon = context.getPackageManager().getUserBadgedIcon(
234 authContext.getDrawable(desc.iconId), userHandle);
235 }
236 } catch (PackageManager.NameNotFoundException e) {
237 Log.w(TAG, "No icon for account type " + desc.type);
238 }
239
240 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
241 contents, false);
242 child.setText(account.name);
243 if (icon != null) {
244 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
245 }
246 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800247 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800248 }
249
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100250 if (accountsCount > 0) {
251 accountsLabel.setVisibility(View.VISIBLE);
252 contents.setVisibility(View.VISIBLE);
253 }
254 // Checking for all other users and their profiles if any.
255 View otherUsers = mContentView.findViewById(R.id.other_users_present);
256 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
257 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800258 }
259
260 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700261 public View onCreateView(LayoutInflater inflater, ViewGroup container,
262 Bundle savedInstanceState) {
Amith Yamasani4339a472014-10-22 13:51:53 -0700263 if (!Process.myUserHandle().isOwner()
264 || UserManager.get(getActivity()).hasUserRestriction(
Julia Reynolds2c539332014-06-11 12:56:02 -0400265 UserManager.DISALLOW_FACTORY_RESET)) {
266 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
267 }
268
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700269 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800270
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800271 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700272 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800273 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400274
275 @Override
276 protected int getMetricsCategory() {
277 return MetricsLogger.MASTER_CLEAR;
278 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800279}