The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 19 | import android.accounts.Account; |
| 20 | import android.accounts.AccountManager; |
| 21 | import android.accounts.AuthenticatorDescription; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 23 | import android.app.Fragment; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 24 | import android.content.Context; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 29 | import android.os.Bundle; |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 30 | import android.os.Environment; |
Amith Yamasani | 4339a47 | 2014-10-22 13:51:53 -0700 | [diff] [blame^] | 31 | import android.os.Process; |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 32 | import android.os.SystemProperties; |
Amith Yamasani | 7eedcf4 | 2013-07-02 14:15:29 -0700 | [diff] [blame] | 33 | import android.os.UserManager; |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 34 | import android.preference.Preference; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 35 | import android.util.Log; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 36 | import android.view.LayoutInflater; |
| 37 | import android.view.View; |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 38 | import android.view.ViewGroup; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 39 | import android.widget.Button; |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame] | 40 | import android.widget.CheckBox; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 41 | import android.widget.LinearLayout; |
| 42 | import android.widget.TextView; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Confirm and execute a reset of the device to a clean "just out of the box" |
| 46 | * state. Multiple confirmations are required: first, a general "are you sure |
| 47 | * you want to do this?" prompt, followed by a keyguard pattern trace if the user |
| 48 | * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING |
| 49 | * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is |
| 50 | * locked, et cetera, then the confirmation sequence is abandoned. |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 51 | * |
| 52 | * This is the initial screen. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 53 | */ |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 54 | public class MasterClear extends Fragment { |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 55 | private static final String TAG = "MasterClear"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 56 | |
| 57 | private static final int KEYGUARD_REQUEST = 55; |
| 58 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 59 | static final String ERASE_EXTERNAL_EXTRA = "erase_sd"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 60 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 61 | private View mContentView; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 62 | private Button mInitiateButton; |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame] | 63 | private View mExternalStorageContainer; |
| 64 | private CheckBox mExternalStorage; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 66 | /** |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 67 | * Keyguard validation is run using the standard {@link ConfirmLockPattern} |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 68 | * component as a subactivity |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 69 | * @param request the request code to be returned once confirmation finishes |
| 70 | * @return true if confirmation launched |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 71 | */ |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 72 | private boolean runKeyguardConfirmation(int request) { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 73 | Resources res = getActivity().getResources(); |
| 74 | return new ChooseLockSettingsHelper(getActivity(), this) |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 75 | .launchConfirmationActivity(request, |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 76 | res.getText(R.string.master_clear_gesture_prompt), |
| 77 | res.getText(R.string.master_clear_gesture_explanation)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 81 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 82 | super.onActivityResult(requestCode, resultCode, data); |
| 83 | |
Julia Reynolds | 2c53933 | 2014-06-11 12:56:02 -0400 | [diff] [blame] | 84 | if (requestCode != KEYGUARD_REQUEST) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
| 88 | // If the user entered a valid keyguard trace, present the final |
| 89 | // confirmation prompt; otherwise, go back to the initial state. |
| 90 | if (resultCode == Activity.RESULT_OK) { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 91 | showFinalConfirmation(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 92 | } else { |
| 93 | establishInitialState(); |
| 94 | } |
| 95 | } |
| 96 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 97 | private void showFinalConfirmation() { |
| 98 | Preference preference = new Preference(getActivity()); |
| 99 | preference.setFragment(MasterClearConfirm.class.getName()); |
| 100 | preference.setTitle(R.string.master_clear_confirm_title); |
| 101 | preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked()); |
Fabrice Di Meglio | 263bcc8 | 2014-01-17 19:17:58 -0800 | [diff] [blame] | 102 | ((SettingsActivity) getActivity()).onPreferenceStartFragment(null, preference); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 103 | } |
| 104 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 105 | /** |
| 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. |
| 109 | */ |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 110 | private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 111 | |
| 112 | public void onClick(View v) { |
| 113 | if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { |
| 114 | showFinalConfirmation(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 115 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 116 | } |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 117 | }; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
| 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 |
| 123 | * this base state. |
Jim Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 124 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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. |
| 130 | */ |
| 131 | private void establishInitialState() { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 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); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 137 | /* |
| 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 |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 141 | * others. Likewise, if it's non-removable storage, it could potentially have been |
| 142 | * encrypted, and will also need to be wiped. |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 143 | */ |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 144 | boolean isExtStorageEmulated = Environment.isExternalStorageEmulated(); |
| 145 | if (isExtStorageEmulated |
| 146 | || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) { |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 147 | mExternalStorageContainer.setVisibility(View.GONE); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 148 | |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 149 | final View externalOption = mContentView.findViewById(R.id.erase_external_option_text); |
| 150 | externalOption.setVisibility(View.GONE); |
| 151 | |
| 152 | final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external); |
| 153 | externalAlsoErased.setVisibility(View.VISIBLE); |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 154 | |
| 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); |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 158 | } else { |
| 159 | mExternalStorageContainer.setOnClickListener(new View.OnClickListener() { |
| 160 | |
| 161 | @Override |
| 162 | public void onClick(View v) { |
| 163 | mExternalStorage.toggle(); |
| 164 | } |
| 165 | }); |
| 166 | } |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 167 | |
| 168 | loadAccountList(); |
| 169 | } |
| 170 | |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 171 | private boolean isExtStorageEncrypted() { |
| 172 | String state = SystemProperties.get("vold.decrypt"); |
| 173 | return !"".equals(state); |
| 174 | } |
| 175 | |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 176 | private void loadAccountList() { |
| 177 | View accountsLabel = mContentView.findViewById(R.id.accounts_label); |
| 178 | LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts); |
Amith Yamasani | 3f45de5 | 2011-09-22 14:34:17 -0700 | [diff] [blame] | 179 | contents.removeAllViews(); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 180 | |
| 181 | Context context = getActivity(); |
| 182 | |
| 183 | AccountManager mgr = AccountManager.get(context); |
| 184 | Account[] accounts = mgr.getAccounts(); |
| 185 | final int N = accounts.length; |
| 186 | if (N == 0) { |
| 187 | accountsLabel.setVisibility(View.GONE); |
| 188 | contents.setVisibility(View.GONE); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | LayoutInflater inflater = (LayoutInflater)context.getSystemService( |
| 193 | Context.LAYOUT_INFLATER_SERVICE); |
| 194 | |
| 195 | AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypes(); |
| 196 | final int M = descs.length; |
| 197 | |
| 198 | for (int i=0; i<N; i++) { |
| 199 | Account account = accounts[i]; |
| 200 | AuthenticatorDescription desc = null; |
| 201 | for (int j=0; j<M; j++) { |
| 202 | if (account.type.equals(descs[j].type)) { |
| 203 | desc = descs[j]; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | if (desc == null) { |
| 208 | Log.w(TAG, "No descriptor for account name=" + account.name |
| 209 | + " type=" + account.type); |
| 210 | continue; |
| 211 | } |
Amith Yamasani | 07d6fd2 | 2011-07-15 18:37:16 -0700 | [diff] [blame] | 212 | Drawable icon = null; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 213 | try { |
Amith Yamasani | 07d6fd2 | 2011-07-15 18:37:16 -0700 | [diff] [blame] | 214 | if (desc.iconId != 0) { |
| 215 | Context authContext = context.createPackageContext(desc.packageName, 0); |
| 216 | icon = authContext.getResources().getDrawable(desc.iconId); |
| 217 | } |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 218 | } catch (PackageManager.NameNotFoundException e) { |
| 219 | Log.w(TAG, "No icon for account type " + desc.type); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TextView child = (TextView)inflater.inflate(R.layout.master_clear_account, |
| 223 | contents, false); |
| 224 | child.setText(account.name); |
| 225 | if (icon != null) { |
| 226 | child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); |
| 227 | } |
| 228 | contents.addView(child); |
| 229 | } |
| 230 | |
| 231 | accountsLabel.setVisibility(View.VISIBLE); |
| 232 | contents.setVisibility(View.VISIBLE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 236 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 237 | Bundle savedInstanceState) { |
Amith Yamasani | 4339a47 | 2014-10-22 13:51:53 -0700 | [diff] [blame^] | 238 | if (!Process.myUserHandle().isOwner() |
| 239 | || UserManager.get(getActivity()).hasUserRestriction( |
Julia Reynolds | 2c53933 | 2014-06-11 12:56:02 -0400 | [diff] [blame] | 240 | UserManager.DISALLOW_FACTORY_RESET)) { |
| 241 | return inflater.inflate(R.layout.master_clear_disallowed_screen, null); |
| 242 | } |
| 243 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 244 | mContentView = inflater.inflate(R.layout.master_clear, null); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 245 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 246 | establishInitialState(); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 247 | return mContentView; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 248 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 249 | } |