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 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.accounts.AuthenticatorDescription; 22 import android.app.Activity; 23 import android.app.FragmentManager; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.pm.PackageManager; 27 import android.content.pm.UserInfo; 28 import android.content.res.Resources; 29 import android.graphics.drawable.Drawable; 30 import android.os.Bundle; 31 import android.os.Environment; 32 import android.os.SystemProperties; 33 import android.os.UserHandle; 34 import android.os.UserManager; 35 import android.support.annotation.VisibleForTesting; 36 import android.util.Log; 37 import android.view.LayoutInflater; 38 import android.view.View; 39 import android.view.View.OnScrollChangeListener; 40 import android.view.ViewGroup; 41 import android.view.ViewTreeObserver.OnGlobalLayoutListener; 42 import android.widget.Button; 43 import android.widget.CheckBox; 44 import android.widget.ImageView; 45 import android.widget.LinearLayout; 46 import android.widget.ScrollView; 47 import android.widget.TextView; 48 49 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 50 import com.android.settings.widget.CarrierDemoPasswordDialogFragment; 51 import com.android.settingslib.RestrictedLockUtils; 52 53 import java.util.List; 54 55 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 56 57 /** 58 * Confirm and execute a reset of the device to a clean "just out of the box" 59 * state. Multiple confirmations are required: first, a general "are you sure 60 * you want to do this?" prompt, followed by a keyguard pattern trace if the user 61 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING 62 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is 63 * locked, et cetera, then the confirmation sequence is abandoned. 64 * 65 * This is the initial screen. 66 */ 67 public class MasterClear extends OptionsMenuFragment 68 implements CarrierDemoPasswordDialogFragment.Callback { 69 private static final String TAG = "MasterClear"; 70 71 private static final int KEYGUARD_REQUEST = 55; 72 73 static final String ERASE_EXTERNAL_EXTRA = "erase_sd"; 74 75 private View mContentView; 76 private Button mInitiateButton; 77 private View mExternalStorageContainer; 78 private CheckBox mExternalStorage; 79 private ScrollView mScrollView; 80 81 private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() { 82 @Override 83 public void onGlobalLayout() { 84 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener); 85 mInitiateButton.setEnabled(hasReachedBottom(mScrollView)); 86 } 87 }; 88 89 /** 90 * Keyguard validation is run using the standard {@link ConfirmLockPattern} 91 * component as a subactivity 92 * @param request the request code to be returned once confirmation finishes 93 * @return true if confirmation launched 94 */ runKeyguardConfirmation(int request)95 private boolean runKeyguardConfirmation(int request) { 96 Resources res = getActivity().getResources(); 97 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity( 98 request, res.getText(R.string.master_clear_title)); 99 } 100 101 @Override onActivityResult(int requestCode, int resultCode, Intent data)102 public void onActivityResult(int requestCode, int resultCode, Intent data) { 103 super.onActivityResult(requestCode, resultCode, data); 104 105 if (requestCode != KEYGUARD_REQUEST) { 106 return; 107 } 108 109 // If the user entered a valid keyguard trace, present the final 110 // confirmation prompt; otherwise, go back to the initial state. 111 if (resultCode == Activity.RESULT_OK) { 112 showFinalConfirmation(); 113 } else { 114 establishInitialState(); 115 } 116 } 117 showFinalConfirmation()118 private void showFinalConfirmation() { 119 Bundle args = new Bundle(); 120 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked()); 121 ((SettingsActivity) getActivity()).startPreferencePanel( 122 this, MasterClearConfirm.class.getName(), 123 args, R.string.master_clear_confirm_title, null, null, 0); 124 } 125 126 /** 127 * If the user clicks to begin the reset sequence, we next require a 128 * keyguard confirmation if the user has currently enabled one. If there 129 * is no keyguard available, we simply go to the final confirmation prompt. 130 */ 131 private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() { 132 133 public void onClick(View v) { 134 if ( Utils.isCarrierDemoUser(v.getContext())) { 135 // Require the carrier password before displaying the final confirmation. 136 final FragmentManager fm = getChildFragmentManager(); 137 if (fm != null && !fm.isDestroyed()) { 138 new CarrierDemoPasswordDialogFragment().show(fm, null /* tag */); 139 } 140 } else if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { 141 showFinalConfirmation(); 142 } 143 } 144 }; 145 146 @Override onPasswordVerified()147 public void onPasswordVerified() { 148 showFinalConfirmation(); 149 } 150 151 /** 152 * In its initial state, the activity presents a button for the user to 153 * click in order to initiate a confirmation sequence. This method is 154 * called from various other points in the code to reset the activity to 155 * this base state. 156 * 157 * <p>Reinflating views from resources is expensive and prevents us from 158 * caching widget pointers, so we use a single-inflate pattern: we lazy- 159 * inflate each view, caching all of the widget pointers we'll need at the 160 * time, then simply reuse the inflated views directly whenever we need 161 * to change contents. 162 */ establishInitialState()163 private void establishInitialState() { 164 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear); 165 mInitiateButton.setOnClickListener(mInitiateListener); 166 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container); 167 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external); 168 mScrollView = (ScrollView) mContentView.findViewById(R.id.master_clear_scrollview); 169 170 /* 171 * If the external storage is emulated, it will be erased with a factory 172 * reset at any rate. There is no need to have a separate option until 173 * we have a factory reset that only erases some directories and not 174 * others. Likewise, if it's non-removable storage, it could potentially have been 175 * encrypted, and will also need to be wiped. 176 */ 177 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated(); 178 if (isExtStorageEmulated 179 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) { 180 mExternalStorageContainer.setVisibility(View.GONE); 181 182 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text); 183 externalOption.setVisibility(View.GONE); 184 185 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external); 186 externalAlsoErased.setVisibility(View.VISIBLE); 187 188 // If it's not emulated, it is on a separate partition but it means we're doing 189 // a force wipe due to encryption. 190 mExternalStorage.setChecked(!isExtStorageEmulated); 191 } else { 192 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() { 193 194 @Override 195 public void onClick(View v) { 196 mExternalStorage.toggle(); 197 } 198 }); 199 } 200 201 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); 202 loadAccountList(um); 203 StringBuffer contentDescription = new StringBuffer(); 204 View masterClearContainer = mContentView.findViewById(R.id.master_clear_container); 205 getContentDescription(masterClearContainer, contentDescription); 206 masterClearContainer.setContentDescription(contentDescription); 207 208 // Set the status of initiateButton based on scrollview 209 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() { 210 @Override 211 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, 212 int oldScrollY) { 213 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) { 214 mInitiateButton.setEnabled(true); 215 } 216 } 217 }); 218 219 // Set the initial state of the initiateButton 220 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener); 221 } 222 223 @VisibleForTesting hasReachedBottom(final ScrollView scrollView)224 boolean hasReachedBottom(final ScrollView scrollView) { 225 if (scrollView.getChildCount() < 1) { 226 return true; 227 } 228 229 final View view = scrollView.getChildAt(0); 230 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()); 231 232 return diff <= 0; 233 } 234 getContentDescription(View v, StringBuffer description)235 private void getContentDescription(View v, StringBuffer description) { 236 if (v.getVisibility() != View.VISIBLE) { 237 return; 238 } 239 if (v instanceof ViewGroup) { 240 ViewGroup vGroup = (ViewGroup) v; 241 for (int i = 0; i < vGroup.getChildCount(); i++) { 242 View nextChild = vGroup.getChildAt(i); 243 getContentDescription(nextChild, description); 244 } 245 } else if (v instanceof TextView) { 246 TextView vText = (TextView) v; 247 description.append(vText.getText()); 248 description.append(","); // Allow Talkback to pause between sections. 249 } 250 } 251 isExtStorageEncrypted()252 private boolean isExtStorageEncrypted() { 253 String state = SystemProperties.get("vold.decrypt"); 254 return !"".equals(state); 255 } 256 loadAccountList(final UserManager um)257 private void loadAccountList(final UserManager um) { 258 View accountsLabel = mContentView.findViewById(R.id.accounts_label); 259 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts); 260 contents.removeAllViews(); 261 262 Context context = getActivity(); 263 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId()); 264 final int profilesSize = profiles.size(); 265 266 AccountManager mgr = AccountManager.get(context); 267 268 LayoutInflater inflater = (LayoutInflater)context.getSystemService( 269 Context.LAYOUT_INFLATER_SERVICE); 270 271 int accountsCount = 0; 272 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) { 273 final UserInfo userInfo = profiles.get(profileIndex); 274 final int profileId = userInfo.id; 275 final UserHandle userHandle = new UserHandle(profileId); 276 Account[] accounts = mgr.getAccountsAsUser(profileId); 277 final int N = accounts.length; 278 if (N == 0) { 279 continue; 280 } 281 accountsCount += N; 282 283 AuthenticatorDescription[] descs = AccountManager.get(context) 284 .getAuthenticatorTypesAsUser(profileId); 285 final int M = descs.length; 286 287 if (profilesSize > 1) { 288 View titleView = Utils.inflateCategoryHeader(inflater, contents); 289 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title); 290 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work 291 : R.string.category_personal); 292 contents.addView(titleView); 293 } 294 295 for (int i = 0; i < N; i++) { 296 Account account = accounts[i]; 297 AuthenticatorDescription desc = null; 298 for (int j = 0; j < M; j++) { 299 if (account.type.equals(descs[j].type)) { 300 desc = descs[j]; 301 break; 302 } 303 } 304 if (desc == null) { 305 Log.w(TAG, "No descriptor for account name=" + account.name 306 + " type=" + account.type); 307 continue; 308 } 309 Drawable icon = null; 310 try { 311 if (desc.iconId != 0) { 312 Context authContext = context.createPackageContextAsUser(desc.packageName, 313 0, userHandle); 314 icon = context.getPackageManager().getUserBadgedIcon( 315 authContext.getDrawable(desc.iconId), userHandle); 316 } 317 } catch (PackageManager.NameNotFoundException e) { 318 Log.w(TAG, "Bad package name for account type " + desc.type); 319 } catch (Resources.NotFoundException e) { 320 Log.w(TAG, "Invalid icon id for account type " + desc.type, e); 321 } 322 if (icon == null) { 323 icon = context.getPackageManager().getDefaultActivityIcon(); 324 } 325 326 View child = inflater.inflate(R.layout.master_clear_account, contents, false); 327 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon); 328 ((TextView) child.findViewById(android.R.id.title)).setText(account.name); 329 contents.addView(child); 330 } 331 } 332 333 if (accountsCount > 0) { 334 accountsLabel.setVisibility(View.VISIBLE); 335 contents.setVisibility(View.VISIBLE); 336 } 337 // Checking for all other users and their profiles if any. 338 View otherUsers = mContentView.findViewById(R.id.other_users_present); 339 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0; 340 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE); 341 } 342 343 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)344 public View onCreateView(LayoutInflater inflater, ViewGroup container, 345 Bundle savedInstanceState) { 346 final Context context = getContext(); 347 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context, 348 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId()); 349 final UserManager um = UserManager.get(context); 350 final boolean disallow = !um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction( 351 context, UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId()); 352 if (disallow && !Utils.isCarrierDemoUser(context)) { 353 return inflater.inflate(R.layout.master_clear_disallowed_screen, null); 354 } else if (admin != null) { 355 View view = inflater.inflate(R.layout.admin_support_details_empty_view, null); 356 ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false); 357 view.setVisibility(View.VISIBLE); 358 return view; 359 } 360 361 mContentView = inflater.inflate(R.layout.master_clear, null); 362 363 establishInitialState(); 364 return mContentView; 365 } 366 367 @Override getMetricsCategory()368 public int getMetricsCategory() { 369 return MetricsEvent.MASTER_CLEAR; 370 } 371 } 372