blob: 9604a3c81e9677c3423f9cfde552b5f548e40501 [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;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010029import android.content.res.TypedArray;
Joe Onoratob51886d2010-11-08 18:25:51 -080030import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080032import android.os.Environment;
Amith Yamasani4339a472014-10-22 13:51:53 -070033import android.os.Process;
Ben Komalo2a321922011-09-07 16:42:34 -070034import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010035import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070036import android.os.UserManager;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070037import android.preference.Preference;
Joe Onoratob51886d2010-11-08 18:25:51 -080038import android.util.Log;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010039import android.util.SparseArray;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040import android.view.LayoutInflater;
41import android.view.View;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070042import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070044import android.widget.CheckBox;
Joe Onoratob51886d2010-11-08 18:25:51 -080045import android.widget.LinearLayout;
46import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010048import java.util.List;
49
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050/**
51 * Confirm and execute a reset of the device to a clean "just out of the box"
52 * state. Multiple confirmations are required: first, a general "are you sure
53 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
54 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
55 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
56 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070057 *
58 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080059 */
Amith Yamasanib14e1e02010-11-02 09:52:29 -070060public class MasterClear extends Fragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080061 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062
63 private static final int KEYGUARD_REQUEST = 55;
64
Amith Yamasanib14e1e02010-11-02 09:52:29 -070065 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066
Amith Yamasanib14e1e02010-11-02 09:52:29 -070067 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070069 private View mExternalStorageContainer;
70 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080071
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070073 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080074 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070075 * @param request the request code to be returned once confirmation finishes
76 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 */
Jim Miller2deec7e2010-04-13 17:43:36 -070078 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070079 Resources res = getActivity().getResources();
Stuart Scottbe903412014-07-24 19:22:06 -070080 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(request,
81 null, res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 }
83
84 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070085 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 super.onActivityResult(requestCode, resultCode, data);
87
Julia Reynolds2c539332014-06-11 12:56:02 -040088 if (requestCode != KEYGUARD_REQUEST) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 return;
90 }
91
92 // If the user entered a valid keyguard trace, present the final
93 // confirmation prompt; otherwise, go back to the initial state.
94 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070095 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 } else {
97 establishInitialState();
98 }
99 }
100
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700101 private void showFinalConfirmation() {
Stuart Scottbe903412014-07-24 19:22:06 -0700102 Bundle args = new Bundle();
103 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
104 ((SettingsActivity) getActivity()).startPreferencePanel(MasterClearConfirm.class.getName(),
105 args, R.string.master_clear_confirm_title, null, null, 0);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700106 }
107
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800108 /**
109 * If the user clicks to begin the reset sequence, we next require a
110 * keyguard confirmation if the user has currently enabled one. If there
111 * is no keyguard available, we simply go to the final confirmation prompt.
112 */
Ben Komalo2a321922011-09-07 16:42:34 -0700113 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700114
115 public void onClick(View v) {
116 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
117 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700120 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800121
122 /**
123 * In its initial state, the activity presents a button for the user to
124 * click in order to initiate a confirmation sequence. This method is
125 * called from various other points in the code to reset the activity to
126 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800127 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800128 * <p>Reinflating views from resources is expensive and prevents us from
129 * caching widget pointers, so we use a single-inflate pattern: we lazy-
130 * inflate each view, caching all of the widget pointers we'll need at the
131 * time, then simply reuse the inflated views directly whenever we need
132 * to change contents.
133 */
134 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700135 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
136 mInitiateButton.setOnClickListener(mInitiateListener);
137 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
138 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139
Kenny Root3785e392011-01-18 15:14:32 -0800140 /*
141 * If the external storage is emulated, it will be erased with a factory
142 * reset at any rate. There is no need to have a separate option until
143 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700144 * others. Likewise, if it's non-removable storage, it could potentially have been
145 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800146 */
Ben Komalo2a321922011-09-07 16:42:34 -0700147 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
148 if (isExtStorageEmulated
149 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800150 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700151
Kenny Root3785e392011-01-18 15:14:32 -0800152 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
153 externalOption.setVisibility(View.GONE);
154
155 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
156 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700157
158 // If it's not emulated, it is on a separate partition but it means we're doing
159 // a force wipe due to encryption.
160 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800161 } else {
162 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
163
164 @Override
165 public void onClick(View v) {
166 mExternalStorage.toggle();
167 }
168 });
169 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800170
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100171 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
172 loadAccountList(um);
Joe Onoratob51886d2010-11-08 18:25:51 -0800173 }
174
Ben Komalo2a321922011-09-07 16:42:34 -0700175 private boolean isExtStorageEncrypted() {
176 String state = SystemProperties.get("vold.decrypt");
177 return !"".equals(state);
178 }
179
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100180 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800181 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
182 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700183 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800184
185 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100186 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
187 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800188
189 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800190
191 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
192 Context.LAYOUT_INFLATER_SERVICE);
193
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100194 int accountsCount = 0;
195 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
196 final UserInfo userInfo = profiles.get(profileIndex);
197 final int profileId = userInfo.id;
198 final UserHandle userHandle = new UserHandle(profileId);
199 Account[] accounts = mgr.getAccountsAsUser(profileId);
200 final int N = accounts.length;
201 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800202 continue;
203 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100204 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800205
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100206 AuthenticatorDescription[] descs = AccountManager.get(context)
207 .getAuthenticatorTypesAsUser(profileId);
208 final int M = descs.length;
209
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +0000210 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100211 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
212 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
213 : R.string.category_personal);
214 contents.addView(titleView);
215
216 for (int i = 0; i < N; i++) {
217 Account account = accounts[i];
218 AuthenticatorDescription desc = null;
219 for (int j = 0; j < M; j++) {
220 if (account.type.equals(descs[j].type)) {
221 desc = descs[j];
222 break;
223 }
224 }
225 if (desc == null) {
226 Log.w(TAG, "No descriptor for account name=" + account.name
227 + " type=" + account.type);
228 continue;
229 }
230 Drawable icon = null;
231 try {
232 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000233 Context authContext = context.createPackageContextAsUser(desc.packageName,
234 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100235 icon = context.getPackageManager().getUserBadgedIcon(
236 authContext.getDrawable(desc.iconId), userHandle);
237 }
238 } catch (PackageManager.NameNotFoundException e) {
239 Log.w(TAG, "No icon for account type " + desc.type);
240 }
241
242 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
243 contents, false);
244 child.setText(account.name);
245 if (icon != null) {
246 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
247 }
248 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800249 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800250 }
251
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100252 if (accountsCount > 0) {
253 accountsLabel.setVisibility(View.VISIBLE);
254 contents.setVisibility(View.VISIBLE);
255 }
256 // Checking for all other users and their profiles if any.
257 View otherUsers = mContentView.findViewById(R.id.other_users_present);
258 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
259 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800260 }
261
262 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700263 public View onCreateView(LayoutInflater inflater, ViewGroup container,
264 Bundle savedInstanceState) {
Amith Yamasani4339a472014-10-22 13:51:53 -0700265 if (!Process.myUserHandle().isOwner()
266 || UserManager.get(getActivity()).hasUserRestriction(
Julia Reynolds2c539332014-06-11 12:56:02 -0400267 UserManager.DISALLOW_FACTORY_RESET)) {
268 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
269 }
270
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700271 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800272
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800273 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700274 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800275 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800276}