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