1 /* 2 * Copyright 2019 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.car.settings.system; 18 19 import android.app.ActivityManager; 20 import android.app.ProgressDialog; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.os.AsyncTask; 25 import android.os.Bundle; 26 import android.provider.Settings; 27 import android.service.oemlock.OemLockManager; 28 import android.service.persistentdata.PersistentDataBlockManager; 29 30 import androidx.preference.PreferenceManager; 31 32 import com.android.car.settings.R; 33 import com.android.car.settings.common.SettingsFragment; 34 import com.android.car.ui.toolbar.MenuItem; 35 36 import java.util.Collections; 37 import java.util.List; 38 39 /** 40 * Presents the user with a final warning before issuing the request to reset the head unit to its 41 * default "factory" state. 42 */ 43 public class MasterClearConfirmFragment extends SettingsFragment { 44 45 private MenuItem mClearConfirmButton; 46 47 private MenuItem.OnClickListener mFinalClickListener = i -> { 48 if (ActivityManager.isUserAMonkey()) { 49 return; 50 } 51 52 PersistentDataBlockManager pdbManager = 53 (PersistentDataBlockManager) requireContext().getSystemService( 54 Context.PERSISTENT_DATA_BLOCK_SERVICE); 55 OemLockManager oemLockManager = (OemLockManager) requireContext().getSystemService( 56 Context.OEM_LOCK_SERVICE); 57 if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() 58 && isDeviceProvisioned()) { 59 // If OEM unlock is allowed, the persistent data block will be wiped during the factory 60 // reset process. If disabled, it will be wiped here, unless the device is still being 61 // provisioned, in which case the persistent data block will be preserved. 62 new AsyncTask<Void, Void, Void>() { 63 private ProgressDialog mProgressDialog; 64 65 @Override 66 protected Void doInBackground(Void... params) { 67 pdbManager.wipe(); 68 return null; 69 } 70 71 @Override 72 protected void onPostExecute(Void aVoid) { 73 mProgressDialog.hide(); 74 if (getActivity() != null) { 75 resetEverything(); 76 } 77 } 78 79 @Override 80 protected void onPreExecute() { 81 mProgressDialog = getProgressDialog(); 82 mProgressDialog.show(); 83 } 84 }.execute(); 85 } else { 86 resetEverything(); 87 } 88 }; 89 90 @Override getPreferenceScreenResId()91 protected int getPreferenceScreenResId() { 92 return R.xml.master_clear_confirm_fragment; 93 } 94 95 @Override getToolbarMenuItems()96 protected List<MenuItem> getToolbarMenuItems() { 97 return Collections.singletonList(mClearConfirmButton); 98 } 99 100 @Override onCreate(Bundle savedInstanceState)101 public void onCreate(Bundle savedInstanceState) { 102 super.onCreate(savedInstanceState); 103 104 mClearConfirmButton = new MenuItem.Builder(getContext()) 105 .setTitle(R.string.master_clear_confirm_button_text) 106 .setOnClickListener(mFinalClickListener) 107 .build(); 108 } 109 isDeviceProvisioned()110 private boolean isDeviceProvisioned() { 111 return Settings.Global.getInt(requireContext().getContentResolver(), 112 Settings.Global.DEVICE_PROVISIONED, 0) != 0; 113 } 114 getProgressDialog()115 private ProgressDialog getProgressDialog() { 116 ProgressDialog progressDialog = new ProgressDialog(requireContext()); 117 progressDialog.setIndeterminate(true); 118 progressDialog.setCancelable(false); 119 progressDialog.setTitle(requireContext().getString(R.string.master_clear_progress_title)); 120 progressDialog.setMessage(requireContext().getString(R.string.master_clear_progress_text)); 121 return progressDialog; 122 } 123 resetEverything()124 private void resetEverything() { 125 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET); 126 intent.setPackage("android"); 127 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 128 intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm"); 129 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, shouldResetEsim()); 130 requireActivity().sendBroadcast(intent); 131 } 132 shouldResetEsim()133 private boolean shouldResetEsim() { 134 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences( 135 requireContext()); 136 return sharedPreferences.getBoolean( 137 requireContext().getString(R.string.pk_master_clear_reset_esim), false); 138 } 139 } 140