2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AuthenticatorDescription;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.content.pm.UserInfo;
27 import android.content.res.Resources;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
30 import android.os.Environment;
31 import android.os.SystemProperties;
32 import android.os.UserHandle;
33 import android.os.UserManager;
34 import android.util.Log;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.Button;
39 import android.widget.CheckBox;
40 import android.widget.LinearLayout;
41 import android.widget.TextView;
43 import com.android.internal.logging.MetricsProto.MetricsEvent;
45 import java.util.List;
48 * Confirm and execute a reset of the device to a clean "just out of the box"
49 * state. Multiple confirmations are required: first, a general "are you sure
50 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
51 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
52 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
53 * locked, et cetera, then the confirmation sequence is abandoned.
55 * This is the initial screen.
57 public class MasterClear extends InstrumentedFragment {
58 private static final String TAG = "MasterClear";
60 private static final int KEYGUARD_REQUEST = 55;
62 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
64 private View mContentView;
65 private Button mInitiateButton;
66 private View mExternalStorageContainer;
67 private CheckBox mExternalStorage;
70 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
71 * component as a subactivity
72 * @param request the request code to be returned once confirmation finishes
73 * @return true if confirmation launched
75 private boolean runKeyguardConfirmation(int request) {
76 Resources res = getActivity().getResources();
77 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
78 request, res.getText(R.string.master_clear_title));
82 public void onActivityResult(int requestCode, int resultCode, Intent data) {
83 super.onActivityResult(requestCode, resultCode, data);
85 if (requestCode != KEYGUARD_REQUEST) {
89 // If the user entered a valid keyguard trace, present the final
90 // confirmation prompt; otherwise, go back to the initial state.
91 if (resultCode == Activity.RESULT_OK) {
92 showFinalConfirmation();
94 establishInitialState();
98 private void showFinalConfirmation() {
99 Bundle args = new Bundle();
100 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
101 ((SettingsActivity) getActivity()).startPreferencePanel(MasterClearConfirm.class.getName(),
102 args, R.string.master_clear_confirm_title, null, null, 0);
106 * If the user clicks to begin the reset sequence, we next require a
107 * keyguard confirmation if the user has currently enabled one. If there
108 * is no keyguard available, we simply go to the final confirmation prompt.
110 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
112 public void onClick(View v) {
113 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
114 showFinalConfirmation();
120 * In its initial state, the activity presents a button for the user to
121 * click in order to initiate a confirmation sequence. This method is
122 * called from various other points in the code to reset the activity to
125 * <p>Reinflating views from resources is expensive and prevents us from
126 * caching widget pointers, so we use a single-inflate pattern: we lazy-
127 * inflate each view, caching all of the widget pointers we'll need at the
128 * time, then simply reuse the inflated views directly whenever we need
129 * to change contents.
131 private void establishInitialState() {
132 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
133 mInitiateButton.setOnClickListener(mInitiateListener);
134 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
135 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
138 * If the external storage is emulated, it will be erased with a factory
139 * reset at any rate. There is no need to have a separate option until
140 * we have a factory reset that only erases some directories and not
141 * others. Likewise, if it's non-removable storage, it could potentially have been
142 * encrypted, and will also need to be wiped.
144 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
145 if (isExtStorageEmulated
146 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
147 mExternalStorageContainer.setVisibility(View.GONE);
149 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
150 externalOption.setVisibility(View.GONE);
152 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
153 externalAlsoErased.setVisibility(View.VISIBLE);
155 // If it's not emulated, it is on a separate partition but it means we're doing
156 // a force wipe due to encryption.
157 mExternalStorage.setChecked(!isExtStorageEmulated);
159 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
162 public void onClick(View v) {
163 mExternalStorage.toggle();
168 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
170 StringBuffer contentDescription = new StringBuffer();
171 View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
172 getContentDescription(masterClearContainer, contentDescription);
173 masterClearContainer.setContentDescription(contentDescription);
176 private void getContentDescription(View v, StringBuffer description) {
177 if (v instanceof ViewGroup) {
178 ViewGroup vGroup = (ViewGroup) v;
179 for (int i = 0; i < vGroup.getChildCount(); i++) {
180 View nextChild = vGroup.getChildAt(i);
181 getContentDescription(nextChild, description);
183 } else if (v instanceof TextView) {
184 TextView vText = (TextView) v;
185 description.append(vText.getText());
186 description.append(","); // Allow Talkback to pause between sections.
190 private boolean isExtStorageEncrypted() {
191 String state = SystemProperties.get("vold.decrypt");
192 return !"".equals(state);
195 private void loadAccountList(final UserManager um) {
196 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
197 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
198 contents.removeAllViews();
200 Context context = getActivity();
201 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
202 final int profilesSize = profiles.size();
204 AccountManager mgr = AccountManager.get(context);
206 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
207 Context.LAYOUT_INFLATER_SERVICE);
209 int accountsCount = 0;
210 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
211 final UserInfo userInfo = profiles.get(profileIndex);
212 final int profileId = userInfo.id;
213 final UserHandle userHandle = new UserHandle(profileId);
214 Account[] accounts = mgr.getAccountsAsUser(profileId);
215 final int N = accounts.length;
221 AuthenticatorDescription[] descs = AccountManager.get(context)
222 .getAuthenticatorTypesAsUser(profileId);
223 final int M = descs.length;
225 View titleView = Utils.inflateCategoryHeader(inflater, contents);
226 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
227 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
228 : R.string.category_personal);
229 contents.addView(titleView);
231 for (int i = 0; i < N; i++) {
232 Account account = accounts[i];
233 AuthenticatorDescription desc = null;
234 for (int j = 0; j < M; j++) {
235 if (account.type.equals(descs[j].type)) {
241 Log.w(TAG, "No descriptor for account name=" + account.name
242 + " type=" + account.type);
245 Drawable icon = null;
247 if (desc.iconId != 0) {
248 Context authContext = context.createPackageContextAsUser(desc.packageName,
250 icon = context.getPackageManager().getUserBadgedIcon(
251 authContext.getDrawable(desc.iconId), userHandle);
253 } catch (PackageManager.NameNotFoundException e) {
254 Log.w(TAG, "Bad package name for account type " + desc.type);
255 } catch (Resources.NotFoundException e) {
256 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
259 icon = context.getPackageManager().getDefaultActivityIcon();
262 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
264 child.setText(account.name);
265 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
266 contents.addView(child);
270 if (accountsCount > 0) {
271 accountsLabel.setVisibility(View.VISIBLE);
272 contents.setVisibility(View.VISIBLE);
274 // Checking for all other users and their profiles if any.
275 View otherUsers = mContentView.findViewById(R.id.other_users_present);
276 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
277 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
281 public View onCreateView(LayoutInflater inflater, ViewGroup container,
282 Bundle savedInstanceState) {
283 final UserManager um = UserManager.get(getActivity());
284 if (!um.isAdminUser()
285 || um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
286 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
289 mContentView = inflater.inflate(R.layout.master_clear, null);
291 establishInitialState();
296 protected int getMetricsCategory() {
297 return MetricsEvent.MASTER_CLEAR;