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.security; 18 19 import android.app.Activity; 20 import android.app.admin.DevicePolicyManager; 21 import android.app.settings.SettingsEnums; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.content.res.Resources; 27 import android.os.BatteryManager; 28 import android.os.Bundle; 29 import android.os.UserHandle; 30 import android.os.storage.StorageManager; 31 import android.text.TextUtils; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.Button; 36 37 import androidx.appcompat.app.AlertDialog; 38 import androidx.preference.Preference; 39 40 import com.android.settings.CryptKeeperConfirm; 41 import com.android.settings.R; 42 import com.android.settings.SettingsActivity; 43 import com.android.settings.core.InstrumentedPreferenceFragment; 44 import com.android.settings.password.ChooseLockSettingsHelper; 45 import com.android.settings.password.ConfirmLockPattern; 46 47 public class CryptKeeperSettings extends InstrumentedPreferenceFragment { 48 private static final String TAG = "CryptKeeper"; 49 private static final String TYPE = "type"; 50 private static final String PASSWORD = "password"; 51 52 private static final int KEYGUARD_REQUEST = 55; 53 54 // Minimum battery charge level (in percent) to launch encryption. If the battery charge is 55 // lower than this, encryption should not be activated. 56 private static final int MIN_BATTERY_LEVEL = 80; 57 58 private View mContentView; 59 private Button mInitiateButton; 60 private View mPowerWarning; 61 private View mBatteryWarning; 62 private IntentFilter mIntentFilter; 63 64 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 65 @Override 66 public void onReceive(Context context, Intent intent) { 67 String action = intent.getAction(); 68 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 69 final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); 70 final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); 71 final int invalidCharger = intent.getIntExtra( 72 BatteryManager.EXTRA_INVALID_CHARGER, 0); 73 74 final boolean levelOk = level >= MIN_BATTERY_LEVEL; 75 final boolean pluggedOk = 76 ((plugged & BatteryManager.BATTERY_PLUGGED_ANY) != 0) && 77 invalidCharger == 0; 78 79 // Update UI elements based on power/battery status 80 mInitiateButton.setEnabled(levelOk && pluggedOk); 81 mPowerWarning.setVisibility(pluggedOk ? View.GONE : View.VISIBLE ); 82 mBatteryWarning.setVisibility(levelOk ? View.GONE : View.VISIBLE); 83 } 84 } 85 }; 86 87 /** 88 * If the user clicks to begin the reset sequence, we next require a 89 * keyguard confirmation if the user has currently enabled one. If there 90 * is no keyguard available, we prompt the user to set a password. 91 */ 92 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() { 93 @Override 94 public void onClick(View v) { 95 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { 96 // TODO replace (or follow) this dialog with an explicit launch into password UI 97 new AlertDialog.Builder(getActivity()) 98 .setTitle(R.string.crypt_keeper_dialog_need_password_title) 99 .setMessage(R.string.crypt_keeper_dialog_need_password_message) 100 .setPositiveButton(android.R.string.ok, null) 101 .create() 102 .show(); 103 } 104 } 105 }; 106 107 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState)108 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { 109 mContentView = inflater.inflate(R.layout.crypt_keeper_settings, null); 110 111 mIntentFilter = new IntentFilter(); 112 mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 113 114 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_encrypt); 115 mInitiateButton.setOnClickListener(mInitiateListener); 116 mInitiateButton.setEnabled(false); 117 118 mPowerWarning = mContentView.findViewById(R.id.warning_unplugged); 119 mBatteryWarning = mContentView.findViewById(R.id.warning_low_charge); 120 121 return mContentView; 122 } 123 124 @Override getMetricsCategory()125 public int getMetricsCategory() { 126 return SettingsEnums.CRYPT_KEEPER; 127 } 128 129 @Override onResume()130 public void onResume() { 131 super.onResume(); 132 getActivity().registerReceiver(mIntentReceiver, mIntentFilter); 133 } 134 135 @Override onPause()136 public void onPause() { 137 super.onPause(); 138 getActivity().unregisterReceiver(mIntentReceiver); 139 } 140 141 /** 142 * If encryption is already started, and this launched via a "start encryption" intent, 143 * then exit immediately - it's already up and running, so there's no point in "starting" it. 144 */ 145 @Override onActivityCreated(Bundle savedInstanceState)146 public void onActivityCreated(Bundle savedInstanceState) { 147 super.onActivityCreated(savedInstanceState); 148 Activity activity = getActivity(); 149 Intent intent = activity.getIntent(); 150 if (DevicePolicyManager.ACTION_START_ENCRYPTION.equals(intent.getAction())) { 151 DevicePolicyManager dpm = (DevicePolicyManager) 152 activity.getSystemService(Context.DEVICE_POLICY_SERVICE); 153 if (dpm != null) { 154 int status = dpm.getStorageEncryptionStatus(); 155 if (status != DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE) { 156 // There is nothing to do here, so simply finish() (which returns to caller) 157 activity.finish(); 158 } 159 } 160 } 161 activity.setTitle(R.string.crypt_keeper_encrypt_title); 162 } 163 164 /** 165 * Keyguard validation is run using the standard {@link ConfirmLockPattern} 166 * component as a subactivity 167 * @param request the request code to be returned once confirmation finishes 168 * @return true if confirmation launched 169 */ runKeyguardConfirmation(int request)170 private boolean runKeyguardConfirmation(int request) { 171 Resources res = getActivity().getResources(); 172 ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(getActivity(), this); 173 174 if (helper.utils().getKeyguardStoredPasswordQuality(UserHandle.myUserId()) 175 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { 176 showFinalConfirmation(StorageManager.CRYPT_TYPE_DEFAULT, "".getBytes()); 177 return true; 178 } 179 180 return helper.launchConfirmationActivity(request, 181 res.getText(R.string.crypt_keeper_encrypt_title), true); 182 } 183 184 @Override onActivityResult(int requestCode, int resultCode, Intent data)185 public void onActivityResult(int requestCode, int resultCode, Intent data) { 186 super.onActivityResult(requestCode, resultCode, data); 187 188 if (requestCode != KEYGUARD_REQUEST) { 189 return; 190 } 191 192 // If the user entered a valid keyguard trace, present the final 193 // confirmation prompt; otherwise, go back to the initial state. 194 if (resultCode == Activity.RESULT_OK && data != null) { 195 int type = data.getIntExtra(ChooseLockSettingsHelper.EXTRA_KEY_TYPE, -1); 196 byte[] password = data.getByteArrayExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD); 197 if (!(password == null || password.length == 0)) { 198 showFinalConfirmation(type, password); 199 } 200 } 201 } 202 showFinalConfirmation(int type, byte[] password)203 private void showFinalConfirmation(int type, byte[] password) { 204 Preference preference = new Preference(getPreferenceManager().getContext()); 205 preference.setFragment(CryptKeeperConfirm.class.getName()); 206 preference.setTitle(R.string.crypt_keeper_confirm_title); 207 addEncryptionInfoToPreference(preference, type, password); 208 ((SettingsActivity) getActivity()).onPreferenceStartFragment(null, preference); 209 } 210 addEncryptionInfoToPreference(Preference preference, int type, byte[] password)211 private void addEncryptionInfoToPreference(Preference preference, int type, byte[] password) { 212 Activity activity = getActivity(); 213 DevicePolicyManager dpm = (DevicePolicyManager) 214 activity.getSystemService(Context.DEVICE_POLICY_SERVICE); 215 if (dpm.getDoNotAskCredentialsOnBoot()) { 216 preference.getExtras().putInt(TYPE, StorageManager.CRYPT_TYPE_DEFAULT); 217 preference.getExtras().putByteArray(PASSWORD, "".getBytes()); 218 } else { 219 preference.getExtras().putInt(TYPE, type); 220 preference.getExtras().putByteArray(PASSWORD, password); 221 } 222 } 223 } 224