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