Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.StatusBarManager; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 23 | import android.content.Intent; |
Paul Lawrence | 7ae20e3 | 2014-07-22 15:00:50 -0700 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Paul Lawrence | 7ae20e3 | 2014-07-22 15:00:50 -0700 | [diff] [blame] | 26 | import android.content.res.Resources.NotFoundException; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 27 | import android.media.AudioManager; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 28 | import android.os.AsyncTask; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 29 | import android.os.Bundle; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 30 | import android.os.Handler; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 31 | import android.os.IBinder; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 32 | import android.os.Message; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 33 | import android.os.PowerManager; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 35 | import android.os.ServiceManager; |
| 36 | import android.os.SystemProperties; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 38 | import android.os.storage.IMountService; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 39 | import android.os.storage.StorageManager; |
Vikram Aggarwal | 6ebbd30 | 2012-05-04 14:09:52 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
Tyler Gunn | 3e71b19 | 2014-09-10 15:21:33 -0700 | [diff] [blame] | 41 | import android.telecom.TelecomManager; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 42 | import android.telephony.TelephonyManager; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 43 | import android.text.Editable; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 44 | import android.text.TextUtils; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 45 | import android.text.TextWatcher; |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 46 | import android.text.format.DateUtils; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 47 | import android.util.Log; |
| 48 | import android.view.KeyEvent; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 49 | import android.view.MotionEvent; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 50 | import android.view.View; |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 51 | import android.view.WindowManager; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 52 | import android.view.View.OnClickListener; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 53 | import android.view.View.OnKeyListener; |
| 54 | import android.view.View.OnTouchListener; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 55 | import android.view.inputmethod.EditorInfo; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 56 | import android.view.inputmethod.InputMethodInfo; |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 57 | import android.view.inputmethod.InputMethodManager; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 58 | import android.view.inputmethod.InputMethodSubtype; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 59 | import android.widget.Button; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 60 | import android.widget.EditText; |
| 61 | import android.widget.ProgressBar; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 62 | import android.widget.TextView; |
| 63 | |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 64 | import com.android.internal.telephony.PhoneConstants; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 65 | import com.android.internal.widget.LockPatternUtils; |
| 66 | import com.android.internal.widget.LockPatternView; |
| 67 | import com.android.internal.widget.LockPatternView.Cell; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 68 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 69 | import static com.android.internal.widget.LockPatternView.DisplayMode; |
| 70 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 71 | import java.util.List; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Settings screens to show the UI flows for encrypting/decrypting the device. |
| 75 | * |
| 76 | * This may be started via adb for debugging the UI layout, without having to go through |
| 77 | * encryption flows everytime. It should be noted that starting the activity in this manner |
| 78 | * is only useful for verifying UI-correctness - the behavior will not be identical. |
| 79 | * <pre> |
| 80 | * $ adb shell pm enable com.android.settings/.CryptKeeper |
| 81 | * $ adb shell am start \ |
| 82 | * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \ |
| 83 | * -n com.android.settings/.CryptKeeper |
| 84 | * </pre> |
| 85 | */ |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 86 | public class CryptKeeper extends Activity implements TextView.OnEditorActionListener, |
| 87 | OnKeyListener, OnTouchListener, TextWatcher { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 88 | private static final String TAG = "CryptKeeper"; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 89 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 90 | private static final String DECRYPT_STATE = "trigger_restart_framework"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 91 | /** Message sent to us to indicate encryption update progress. */ |
| 92 | private static final int MESSAGE_UPDATE_PROGRESS = 1; |
| 93 | /** Message sent to us to cool-down (waste user's time between password attempts) */ |
| 94 | private static final int MESSAGE_COOLDOWN = 2; |
| 95 | /** Message sent to us to indicate alerting the user that we are waiting for password entry */ |
| 96 | private static final int MESSAGE_NOTIFY = 3; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 97 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 98 | // Constants used to control policy. |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 99 | private static final int MAX_FAILED_ATTEMPTS = 30; |
| 100 | private static final int COOL_DOWN_ATTEMPTS = 10; |
| 101 | private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds |
| 102 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 103 | // Intent action for launching the Emergency Dialer activity. |
| 104 | static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 105 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 106 | // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts |
| 107 | private static final String EXTRA_FORCE_VIEW = |
| 108 | "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW"; |
| 109 | private static final String FORCE_VIEW_PROGRESS = "progress"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 110 | private static final String FORCE_VIEW_ERROR = "error"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 111 | private static final String FORCE_VIEW_PASSWORD = "password"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 112 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 113 | /** When encryption is detected, this flag indicates whether or not we've checked for errors. */ |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 114 | private boolean mValidationComplete; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 115 | private boolean mValidationRequested; |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 116 | /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */ |
| 117 | private boolean mEncryptionGoneBad; |
Paul Lawrence | 87abbd3 | 2014-08-28 15:56:38 -0700 | [diff] [blame] | 118 | /** If gone bad, should we show encryption failed (false) or corrupt (true)*/ |
| 119 | private boolean mCorrupt; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 120 | /** A flag to indicate when the back event should be ignored */ |
| 121 | private boolean mIgnoreBack = false; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 122 | private int mCooldown; |
| 123 | PowerManager.WakeLock mWakeLock; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 124 | private EditText mPasswordEntry; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 125 | private LockPatternView mLockPatternView; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 126 | /** Number of calls to {@link #notifyUser()} to ignore before notifying. */ |
| 127 | private int mNotificationCountdown = 0; |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 128 | /** Number of calls to {@link #notifyUser()} before we release the wakelock */ |
| 129 | private int mReleaseWakeLockCountdown = 0; |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 130 | private int mStatusString = R.string.enter_password; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 131 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 132 | // how long we wait to clear a wrong pattern |
| 133 | private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500; |
| 134 | |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame] | 135 | // how long we wait to clear a right pattern |
| 136 | private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500; |
| 137 | |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 138 | // When the user enters a short pin/password, run this to show an error, |
| 139 | // but don't count it against attempts. |
| 140 | private final Runnable mFakeUnlockAttemptRunnable = new Runnable() { |
| 141 | public void run() { |
| 142 | handleBadAttempt(1 /* failedAttempt */); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | // TODO: this should be tuned to match minimum decryption timeout |
| 147 | private static final int FAKE_ATTEMPT_DELAY = 1000; |
| 148 | |
| 149 | private final Runnable mClearPatternRunnable = new Runnable() { |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 150 | @Override |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 151 | public void run() { |
| 152 | mLockPatternView.clearPattern(); |
| 153 | } |
| 154 | }; |
| 155 | |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Used to propagate state through configuration changes (e.g. screen rotation) |
| 158 | */ |
| 159 | private static class NonConfigurationInstanceState { |
| 160 | final PowerManager.WakeLock wakelock; |
| 161 | |
| 162 | NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) { |
| 163 | wakelock = _wakelock; |
| 164 | } |
| 165 | } |
| 166 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 167 | private class DecryptTask extends AsyncTask<String, Void, Integer> { |
Paul Lawrence | ced7db4 | 2014-09-18 11:23:40 -0700 | [diff] [blame] | 168 | private void hide(int id) { |
| 169 | View view = findViewById(id); |
| 170 | if (view != null) { |
| 171 | view.setVisibility(View.GONE); |
| 172 | } |
| 173 | } |
| 174 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 175 | @Override |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 176 | protected void onPreExecute() { |
| 177 | super.onPreExecute(); |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 178 | beginAttempt(); |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 182 | protected Integer doInBackground(String... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 183 | final IMountService service = getMountService(); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 184 | try { |
| 185 | return service.decryptStorage(params[0]); |
| 186 | } catch (Exception e) { |
| 187 | Log.e(TAG, "Error while decrypting...", e); |
| 188 | return -1; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | protected void onPostExecute(Integer failedAttempts) { |
| 194 | if (failedAttempts == 0) { |
Paul Lawrence | 89c7570 | 2014-07-11 07:34:44 -0700 | [diff] [blame] | 195 | // The password was entered successfully. Simply do nothing |
| 196 | // and wait for the service restart to switch to surfacefligner |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame] | 197 | if (mLockPatternView != null) { |
| 198 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 199 | mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS); |
| 200 | } |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 201 | final TextView status = (TextView) findViewById(R.id.status); |
| 202 | status.setText(R.string.starting_android); |
Paul Lawrence | ced7db4 | 2014-09-18 11:23:40 -0700 | [diff] [blame] | 203 | hide(R.id.passwordEntry); |
| 204 | hide(R.id.switch_ime_button); |
| 205 | hide(R.id.lockPattern); |
Paul Lawrence | ced7db4 | 2014-09-18 11:23:40 -0700 | [diff] [blame] | 206 | hide(R.id.owner_info); |
| 207 | hide(R.id.emergencyCallButton); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 208 | } else if (failedAttempts == MAX_FAILED_ATTEMPTS) { |
| 209 | // Factory reset the device. |
Jeff Sharkey | 1de688d | 2014-09-24 13:57:07 -0700 | [diff] [blame] | 210 | Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR); |
| 211 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 212 | intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS"); |
| 213 | sendBroadcast(intent); |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 214 | } else if (failedAttempts == -1) { |
| 215 | // Right password, but decryption failed. Tell user bad news ... |
| 216 | setContentView(R.layout.crypt_keeper_progress); |
| 217 | showFactoryReset(true); |
| 218 | return; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 219 | } else { |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 220 | handleBadAttempt(failedAttempts); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private void beginAttempt() { |
| 226 | final TextView status = (TextView) findViewById(R.id.status); |
| 227 | status.setText(R.string.checking_decryption); |
| 228 | } |
| 229 | |
| 230 | private void handleBadAttempt(Integer failedAttempts) { |
| 231 | // Wrong entry. Handle pattern case. |
| 232 | if (mLockPatternView != null) { |
| 233 | mLockPatternView.setDisplayMode(DisplayMode.Wrong); |
| 234 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 235 | mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS); |
| 236 | } |
| 237 | if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) { |
| 238 | mCooldown = COOL_DOWN_INTERVAL; |
| 239 | cooldown(); |
| 240 | } else { |
| 241 | final TextView status = (TextView) findViewById(R.id.status); |
| 242 | |
| 243 | int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts; |
| 244 | if (remainingAttempts < COOL_DOWN_ATTEMPTS) { |
| 245 | CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe); |
| 246 | CharSequence warning = TextUtils.expandTemplate(warningTemplate, |
| 247 | Integer.toString(remainingAttempts)); |
| 248 | status.setText(warning); |
| 249 | } else { |
| 250 | int passwordType = StorageManager.CRYPT_TYPE_PASSWORD; |
| 251 | try { |
| 252 | final IMountService service = getMountService(); |
| 253 | passwordType = service.getPasswordType(); |
| 254 | } catch (Exception e) { |
| 255 | Log.e(TAG, "Error calling mount service " + e); |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 256 | } |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 257 | |
| 258 | if (passwordType == StorageManager.CRYPT_TYPE_PIN) { |
| 259 | status.setText(R.string.cryptkeeper_wrong_pin); |
| 260 | } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) { |
| 261 | status.setText(R.string.cryptkeeper_wrong_pattern); |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 262 | } else { |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 263 | status.setText(R.string.cryptkeeper_wrong_password); |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 264 | } |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 265 | } |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 266 | |
| 267 | if (mLockPatternView != null) { |
| 268 | mLockPatternView.setDisplayMode(DisplayMode.Wrong); |
| 269 | mLockPatternView.setEnabled(true); |
| 270 | } |
| 271 | |
| 272 | // Reenable the password entry |
| 273 | if (mPasswordEntry != null) { |
| 274 | mPasswordEntry.setEnabled(true); |
| 275 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 276 | Context.INPUT_METHOD_SERVICE); |
| 277 | imm.showSoftInput(mPasswordEntry, 0); |
| 278 | setBackFunctionality(true); |
| 279 | } |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 280 | } |
| 281 | } |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 282 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 283 | private class ValidationTask extends AsyncTask<Void, Void, Boolean> { |
Paul Lawrence | 87abbd3 | 2014-08-28 15:56:38 -0700 | [diff] [blame] | 284 | int state; |
| 285 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 286 | @Override |
| 287 | protected Boolean doInBackground(Void... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 288 | final IMountService service = getMountService(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 289 | try { |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 290 | Log.d(TAG, "Validating encryption state."); |
Paul Lawrence | 87abbd3 | 2014-08-28 15:56:38 -0700 | [diff] [blame] | 291 | state = service.getEncryptionState(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 292 | if (state == IMountService.ENCRYPTION_STATE_NONE) { |
| 293 | Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption."); |
| 294 | return true; // Unexpected, but fine, I guess... |
| 295 | } |
| 296 | return state == IMountService.ENCRYPTION_STATE_OK; |
| 297 | } catch (RemoteException e) { |
| 298 | Log.w(TAG, "Unable to get encryption state properly"); |
| 299 | return true; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | protected void onPostExecute(Boolean result) { |
| 305 | mValidationComplete = true; |
| 306 | if (Boolean.FALSE.equals(result)) { |
| 307 | Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe."); |
| 308 | mEncryptionGoneBad = true; |
Paul Lawrence | 87abbd3 | 2014-08-28 15:56:38 -0700 | [diff] [blame] | 309 | mCorrupt = state == IMountService.ENCRYPTION_STATE_ERROR_CORRUPT; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 310 | } else { |
| 311 | Log.d(TAG, "Encryption state validated. Proceeding to configure UI"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 312 | } |
| 313 | setupUi(); |
| 314 | } |
| 315 | } |
| 316 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 317 | private final Handler mHandler = new Handler() { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 318 | @Override |
| 319 | public void handleMessage(Message msg) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 320 | switch (msg.what) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 321 | case MESSAGE_UPDATE_PROGRESS: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 322 | updateProgress(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 323 | break; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 324 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 325 | case MESSAGE_COOLDOWN: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 326 | cooldown(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 327 | break; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 328 | |
| 329 | case MESSAGE_NOTIFY: |
| 330 | notifyUser(); |
| 331 | break; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | }; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 335 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 336 | private AudioManager mAudioManager; |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 337 | /** The status bar where back/home/recent buttons are shown. */ |
| 338 | private StatusBarManager mStatusBar; |
| 339 | |
| 340 | /** All the widgets to disable in the status bar */ |
| 341 | final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND |
| 342 | | StatusBarManager.DISABLE_NOTIFICATION_ICONS |
| 343 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS |
| 344 | | StatusBarManager.DISABLE_SYSTEM_INFO |
| 345 | | StatusBarManager.DISABLE_HOME |
Alon Albert | 66d050b | 2013-09-30 17:08:12 -0700 | [diff] [blame] | 346 | | StatusBarManager.DISABLE_SEARCH |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 347 | | StatusBarManager.DISABLE_RECENT; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 348 | |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 349 | protected static final int MIN_LENGTH_BEFORE_REPORT = LockPatternUtils.MIN_LOCK_PATTERN_SIZE; |
| 350 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 351 | /** @return whether or not this Activity was started for debugging the UI only. */ |
| 352 | private boolean isDebugView() { |
| 353 | return getIntent().hasExtra(EXTRA_FORCE_VIEW); |
| 354 | } |
| 355 | |
| 356 | /** @return whether or not this Activity was started for debugging the specific UI view only. */ |
| 357 | private boolean isDebugView(String viewType /* non-nullable */) { |
| 358 | return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW)); |
| 359 | } |
| 360 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Notify the user that we are awaiting input. Currently this sends an audio alert. |
| 363 | */ |
| 364 | private void notifyUser() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 365 | if (mNotificationCountdown > 0) { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 366 | --mNotificationCountdown; |
| 367 | } else if (mAudioManager != null) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 368 | try { |
| 369 | // Play the standard keypress sound at full volume. This should be available on |
| 370 | // every device. We cannot play a ringtone here because media services aren't |
| 371 | // available yet. A DTMF-style tone is too soft to be noticed, and might not exist |
| 372 | // on tablet devices. The idea is to alert the user that something is needed: this |
| 373 | // does not have to be pleasing. |
| 374 | mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100); |
| 375 | } catch (Exception e) { |
| 376 | Log.w(TAG, "notifyUser: Exception while playing sound: " + e); |
| 377 | } |
| 378 | } |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 379 | // Notify the user again in 5 seconds. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 380 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 381 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000); |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 382 | |
| 383 | if (mWakeLock.isHeld()) { |
| 384 | if (mReleaseWakeLockCountdown > 0) { |
| 385 | --mReleaseWakeLockCountdown; |
| 386 | } else { |
| 387 | mWakeLock.release(); |
| 388 | } |
| 389 | } |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Ignore back events after the user has entered the decrypt screen and while the device is |
| 394 | * encrypting. |
| 395 | */ |
| 396 | @Override |
| 397 | public void onBackPressed() { |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 398 | // In the rare case that something pressed back even though we were disabled. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 399 | if (mIgnoreBack) |
| 400 | return; |
| 401 | super.onBackPressed(); |
| 402 | } |
| 403 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 404 | @Override |
| 405 | public void onCreate(Bundle savedInstanceState) { |
| 406 | super.onCreate(savedInstanceState); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 407 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 408 | // If we are not encrypted or encrypting, get out quickly. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 409 | final String state = SystemProperties.get("vold.decrypt"); |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 410 | if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 411 | // Disable the crypt keeper. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 412 | PackageManager pm = getPackageManager(); |
| 413 | ComponentName name = new ComponentName(this, CryptKeeper.class); |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 414 | pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 415 | PackageManager.DONT_KILL_APP); |
| 416 | // Typically CryptKeeper is launched as the home app. We didn't |
Dianne Hackborn | 644fa42 | 2011-10-18 13:38:03 -0700 | [diff] [blame] | 417 | // want to be running, so need to finish this activity. We can count |
| 418 | // on the activity manager re-launching the new home app upon finishing |
| 419 | // this one, since this will leave the activity stack empty. |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 420 | // NOTE: This is really grungy. I think it would be better for the |
| 421 | // activity manager to explicitly launch the crypt keeper instead of |
| 422 | // home in the situation where we need to decrypt the device |
| 423 | finish(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 424 | return; |
| 425 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 426 | |
Paul Lawrence | 7ae20e3 | 2014-07-22 15:00:50 -0700 | [diff] [blame] | 427 | try { |
| 428 | if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) { |
| 429 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 430 | } |
| 431 | } catch (NotFoundException e) { |
| 432 | } |
| 433 | |
Vikram Aggarwal | b96b35a | 2012-05-01 11:09:39 -0700 | [diff] [blame] | 434 | // Disable the status bar, but do NOT disable back because the user needs a way to go |
| 435 | // from keyboard settings and back to the password screen. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 436 | mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
| 437 | mStatusBar.disable(sWidgetsToDisable); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 438 | |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 439 | setAirplaneModeIfNecessary(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 440 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 441 | // Check for (and recover) retained instance data |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 442 | final Object lastInstance = getLastNonConfigurationInstance(); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 443 | if (lastInstance instanceof NonConfigurationInstanceState) { |
| 444 | NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance; |
| 445 | mWakeLock = retained.wakelock; |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 446 | Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 447 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 448 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 449 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 450 | /** |
| 451 | * Note, we defer the state check and screen setup to onStart() because this will be |
| 452 | * re-run if the user clicks the power button (sleeping/waking the screen), and this is |
| 453 | * especially important if we were to lose the wakelock for any reason. |
| 454 | */ |
| 455 | @Override |
| 456 | public void onStart() { |
| 457 | super.onStart(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 458 | setupUi(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Initializes the UI based on the current state of encryption. |
| 463 | * This is idempotent - calling repeatedly will simply re-initialize the UI. |
| 464 | */ |
| 465 | private void setupUi() { |
| 466 | if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) { |
| 467 | setContentView(R.layout.crypt_keeper_progress); |
Paul Lawrence | 87abbd3 | 2014-08-28 15:56:38 -0700 | [diff] [blame] | 468 | showFactoryReset(mCorrupt); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 472 | final String progress = SystemProperties.get("vold.encrypt_progress"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 473 | if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) { |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 474 | setContentView(R.layout.crypt_keeper_progress); |
| 475 | encryptionProgressInit(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 476 | } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) { |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 477 | new AsyncTask<Void, Void, Void>() { |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 478 | int passwordType = StorageManager.CRYPT_TYPE_PASSWORD; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 479 | String owner_info; |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame] | 480 | boolean pattern_visible; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 481 | |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 482 | @Override |
| 483 | public Void doInBackground(Void... v) { |
| 484 | try { |
| 485 | final IMountService service = getMountService(); |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 486 | passwordType = service.getPasswordType(); |
Elliott Hughes | f9d6d29 | 2014-10-02 13:29:58 -0700 | [diff] [blame] | 487 | owner_info = service.getField(StorageManager.OWNER_INFO_KEY); |
| 488 | pattern_visible = !("0".equals(service.getField(StorageManager.PATTERN_VISIBLE_KEY))); |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 489 | } catch (Exception e) { |
| 490 | Log.e(TAG, "Error calling mount service " + e); |
| 491 | } |
| 492 | |
| 493 | return null; |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public void onPostExecute(java.lang.Void v) { |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 498 | if(passwordType == StorageManager.CRYPT_TYPE_PIN) { |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 499 | setContentView(R.layout.crypt_keeper_pin_entry); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 500 | mStatusString = R.string.enter_pin; |
Paul Crowley | 529834d | 2014-10-21 10:12:47 +0100 | [diff] [blame] | 501 | } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) { |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 502 | setContentView(R.layout.crypt_keeper_pattern_entry); |
| 503 | setBackFunctionality(false); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 504 | mStatusString = R.string.enter_pattern; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 505 | } else { |
| 506 | setContentView(R.layout.crypt_keeper_password_entry); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 507 | mStatusString = R.string.enter_password; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 508 | } |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 509 | final TextView status = (TextView) findViewById(R.id.status); |
| 510 | status.setText(mStatusString); |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 511 | |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 512 | final TextView ownerInfo = (TextView) findViewById(R.id.owner_info); |
| 513 | ownerInfo.setText(owner_info); |
| 514 | ownerInfo.setSelected(true); // Required for marquee'ing to work |
| 515 | |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 516 | passwordEntryInit(); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 517 | |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame] | 518 | if (mLockPatternView != null) { |
| 519 | mLockPatternView.setInStealthMode(!pattern_visible); |
| 520 | } |
| 521 | |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 522 | if (mCooldown > 0) { |
| 523 | setBackFunctionality(false); |
| 524 | cooldown(); // in case we are cooling down and coming back from emergency dialler |
| 525 | } |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 526 | } |
| 527 | }.execute(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 528 | } else if (!mValidationRequested) { |
| 529 | // We're supposed to be encrypted, but no validation has been done. |
| 530 | new ValidationTask().execute((Void[]) null); |
| 531 | mValidationRequested = true; |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 535 | @Override |
| 536 | public void onStop() { |
| 537 | super.onStop(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 538 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 539 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
| 540 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop() |
| 545 | * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears |
| 546 | * mWakeLock so the subsequent call to onDestroy does not release it. |
| 547 | */ |
| 548 | @Override |
| 549 | public Object onRetainNonConfigurationInstance() { |
| 550 | NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 551 | Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 552 | mWakeLock = null; |
| 553 | return state; |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public void onDestroy() { |
| 558 | super.onDestroy(); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 559 | |
| 560 | if (mWakeLock != null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 561 | Log.d(TAG, "Releasing and destroying wakelock"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 562 | mWakeLock.release(); |
| 563 | mWakeLock = null; |
| 564 | } |
| 565 | } |
| 566 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 567 | /** |
| 568 | * Start encrypting the device. |
| 569 | */ |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 570 | private void encryptionProgressInit() { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 571 | // Accquire a partial wakelock to prevent the device from sleeping. Note |
| 572 | // we never release this wakelock as we will be restarted after the device |
| 573 | // is encrypted. |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 574 | Log.d(TAG, "Encryption progress screen initializing."); |
Ben Komalo | 9ee164f | 2011-09-21 10:40:50 -0700 | [diff] [blame] | 575 | if (mWakeLock == null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 576 | Log.d(TAG, "Acquiring wakelock."); |
| 577 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 578 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 579 | mWakeLock.acquire(); |
| 580 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 581 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 582 | ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 583 | // Ignore all back presses from now, both hard and soft keys. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 584 | setBackFunctionality(false); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 585 | // Start the first run of progress manually. This method sets up messages to occur at |
| 586 | // repeated intervals. |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 587 | updateProgress(); |
| 588 | } |
| 589 | |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 590 | /** |
| 591 | * Show factory reset screen allowing the user to reset their phone when |
| 592 | * there is nothing else we can do |
| 593 | * @param corrupt true if userdata is corrupt, false if encryption failed |
| 594 | * partway through |
| 595 | */ |
Jeff Sharkey | 1de688d | 2014-09-24 13:57:07 -0700 | [diff] [blame] | 596 | private void showFactoryReset(final boolean corrupt) { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 597 | // Hide the encryption-bot to make room for the "factory reset" button |
| 598 | findViewById(R.id.encroid).setVisibility(View.GONE); |
| 599 | |
| 600 | // Show the reset button, failure text, and a divider |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 601 | final Button button = (Button) findViewById(R.id.factory_reset); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 602 | button.setVisibility(View.VISIBLE); |
| 603 | button.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 604 | @Override |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 605 | public void onClick(View v) { |
| 606 | // Factory reset the device. |
Jeff Sharkey | 1de688d | 2014-09-24 13:57:07 -0700 | [diff] [blame] | 607 | Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR); |
| 608 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 609 | intent.putExtra(Intent.EXTRA_REASON, |
| 610 | "CryptKeeper.showFactoryReset() corrupt=" + corrupt); |
| 611 | sendBroadcast(intent); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 612 | } |
| 613 | }); |
| 614 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 615 | // Alert the user of the failure. |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 616 | if (corrupt) { |
| 617 | ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title); |
| 618 | ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary); |
| 619 | } else { |
| 620 | ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title); |
| 621 | ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary); |
| 622 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 623 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 624 | final View view = findViewById(R.id.bottom_divider); |
| 625 | // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate. |
Ben Komalo | f0104df | 2011-08-16 17:48:42 -0700 | [diff] [blame] | 626 | if (view != null) { |
| 627 | view.setVisibility(View.VISIBLE); |
| 628 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 631 | private void updateProgress() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 632 | final String state = SystemProperties.get("vold.encrypt_progress"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 633 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 634 | if ("error_partially_encrypted".equals(state)) { |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 635 | showFactoryReset(false); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 639 | // Get status as percentage first |
| 640 | CharSequence status = getText(R.string.crypt_keeper_setup_description); |
| 641 | int percent = 0; |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 642 | try { |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 643 | // Force a 50% progress state when debugging the view. |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 644 | percent = isDebugView() ? 50 : Integer.parseInt(state); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 645 | } catch (Exception e) { |
| 646 | Log.w(TAG, "Error parsing progress: " + e.toString()); |
| 647 | } |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 648 | String progress = Integer.toString(percent); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 649 | |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 650 | // Now try to get status as time remaining and replace as appropriate |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 651 | Log.v(TAG, "Encryption progress: " + progress); |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 652 | try { |
| 653 | final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining"); |
| 654 | int time = Integer.parseInt(timeProperty); |
| 655 | if (time >= 0) { |
| 656 | // Round up to multiple of 10 - this way display is less jerky |
| 657 | time = (time + 9) / 10 * 10; |
| 658 | progress = DateUtils.formatElapsedTime(time); |
| 659 | status = getText(R.string.crypt_keeper_setup_time_remaining); |
| 660 | } |
| 661 | } catch (Exception e) { |
| 662 | // Will happen if no time etc - show percentage |
| 663 | } |
| 664 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 665 | final TextView tv = (TextView) findViewById(R.id.status); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 666 | if (tv != null) { |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 667 | tv.setText(TextUtils.expandTemplate(status, progress)); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 668 | } |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 669 | |
| 670 | // Check the progress every 1 seconds |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 671 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 672 | mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 673 | } |
| 674 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 675 | /** Disable password input for a while to force the user to waste time between retries */ |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 676 | private void cooldown() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 677 | final TextView status = (TextView) findViewById(R.id.status); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 678 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 679 | if (mCooldown <= 0) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 680 | // Re-enable the password entry and back presses. |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 681 | if (mPasswordEntry != null) { |
| 682 | mPasswordEntry.setEnabled(true); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 683 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 684 | Context.INPUT_METHOD_SERVICE); |
| 685 | imm.showSoftInput(mPasswordEntry, 0); |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 686 | setBackFunctionality(true); |
| 687 | } |
| 688 | if (mLockPatternView != null) { |
| 689 | mLockPatternView.setEnabled(true); |
| 690 | } |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 691 | status.setText(mStatusString); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 692 | } else { |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 693 | // Disable the password entry and back presses. |
| 694 | if (mPasswordEntry != null) { |
| 695 | mPasswordEntry.setEnabled(false); |
| 696 | } |
| 697 | if (mLockPatternView != null) { |
| 698 | mLockPatternView.setEnabled(false); |
| 699 | } |
| 700 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 701 | CharSequence template = getText(R.string.crypt_keeper_cooldown); |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 702 | status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown))); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 703 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 704 | mCooldown--; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 705 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 706 | mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 707 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 708 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 709 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 710 | /** |
| 711 | * Sets the back status: enabled or disabled according to the parameter. |
| 712 | * @param isEnabled true if back is enabled, false otherwise. |
| 713 | */ |
| 714 | private final void setBackFunctionality(boolean isEnabled) { |
| 715 | mIgnoreBack = !isEnabled; |
| 716 | if (isEnabled) { |
| 717 | mStatusBar.disable(sWidgetsToDisable); |
| 718 | } else { |
| 719 | mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK); |
| 720 | } |
| 721 | } |
| 722 | |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 723 | private void fakeUnlockAttempt(View postingView) { |
| 724 | beginAttempt(); |
| 725 | postingView.postDelayed(mFakeUnlockAttemptRunnable, FAKE_ATTEMPT_DELAY); |
| 726 | } |
| 727 | |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 728 | protected LockPatternView.OnPatternListener mChooseNewLockPatternListener = |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 729 | new LockPatternView.OnPatternListener() { |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 730 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 731 | @Override |
| 732 | public void onPatternStart() { |
| 733 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 734 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 735 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 736 | @Override |
| 737 | public void onPatternCleared() { |
| 738 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 739 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 740 | @Override |
| 741 | public void onPatternDetected(List<LockPatternView.Cell> pattern) { |
| 742 | mLockPatternView.setEnabled(false); |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 743 | if (pattern.size() >= MIN_LENGTH_BEFORE_REPORT) { |
| 744 | new DecryptTask().execute(LockPatternUtils.patternToString(pattern)); |
| 745 | } else { |
| 746 | // Allow user to make as many of these as they want. |
| 747 | fakeUnlockAttempt(mLockPatternView); |
| 748 | } |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 749 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 750 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 751 | @Override |
| 752 | public void onPatternCellAdded(List<Cell> pattern) { |
| 753 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 754 | }; |
| 755 | |
| 756 | private void passwordEntryInit() { |
| 757 | // Password/pin case |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 758 | mPasswordEntry = (EditText) findViewById(R.id.passwordEntry); |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 759 | if (mPasswordEntry != null){ |
| 760 | mPasswordEntry.setOnEditorActionListener(this); |
| 761 | mPasswordEntry.requestFocus(); |
| 762 | // Become quiet when the user interacts with the Edit text screen. |
| 763 | mPasswordEntry.setOnKeyListener(this); |
| 764 | mPasswordEntry.setOnTouchListener(this); |
| 765 | mPasswordEntry.addTextChangedListener(this); |
| 766 | } |
| 767 | |
| 768 | // Pattern case |
| 769 | mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern); |
| 770 | if (mLockPatternView != null) { |
| 771 | mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener); |
| 772 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 773 | |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 774 | // Disable the Emergency call button if the device has no voice telephone capability |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 775 | if (!getTelephonyManager().isVoiceCapable()) { |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 776 | final View emergencyCall = findViewById(R.id.emergencyCallButton); |
| 777 | if (emergencyCall != null) { |
| 778 | Log.d(TAG, "Removing the emergency Call button"); |
| 779 | emergencyCall.setVisibility(View.GONE); |
| 780 | } |
| 781 | } |
| 782 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 783 | final View imeSwitcher = findViewById(R.id.switch_ime_button); |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 784 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 785 | Context.INPUT_METHOD_SERVICE); |
| 786 | if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) { |
| 787 | imeSwitcher.setVisibility(View.VISIBLE); |
| 788 | imeSwitcher.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 789 | @Override |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 790 | public void onClick(View v) { |
| 791 | imm.showInputMethodPicker(); |
| 792 | } |
| 793 | }); |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 794 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 795 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 796 | // We want to keep the screen on while waiting for input. In minimal boot mode, the device |
| 797 | // is completely non-functional, and we want the user to notice the device and enter a |
| 798 | // password. |
| 799 | if (mWakeLock == null) { |
| 800 | Log.d(TAG, "Acquiring wakelock."); |
| 801 | final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 802 | if (pm != null) { |
| 803 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 804 | mWakeLock.acquire(); |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 805 | // Keep awake for 10 minutes - if the user hasn't been alerted by then |
| 806 | // best not to just drain their battery |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 807 | mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600 |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 810 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 811 | // Asynchronously throw up the IME, since there are issues with requesting it to be shown |
| 812 | // immediately. |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 813 | if (mLockPatternView == null && mCooldown <= 0) { |
Paul Lawrence | e54e932 | 2014-03-13 14:05:48 -0700 | [diff] [blame] | 814 | mHandler.postDelayed(new Runnable() { |
| 815 | @Override public void run() { |
| 816 | imm.showSoftInputUnchecked(0, null); |
| 817 | } |
| 818 | }, 0); |
| 819 | } |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 820 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 821 | updateEmergencyCallButtonState(); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 822 | // Notify the user in 120 seconds that we are waiting for him to enter the password. |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 823 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 824 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000); |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 825 | |
John Spurlock | 2c52651 | 2014-09-17 12:55:17 -0400 | [diff] [blame] | 826 | // Dismiss secure & non-secure keyguards while this screen is showing. |
| 827 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
| 828 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 829 | } |
| 830 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 831 | /** |
| 832 | * Method adapted from com.android.inputmethod.latin.Utils |
| 833 | * |
| 834 | * @param imm The input method manager |
| 835 | * @param shouldIncludeAuxiliarySubtypes |
| 836 | * @return true if we have multiple IMEs to choose from |
| 837 | */ |
| 838 | private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm, |
| 839 | final boolean shouldIncludeAuxiliarySubtypes) { |
| 840 | final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); |
| 841 | |
| 842 | // Number of the filtered IMEs |
| 843 | int filteredImisCount = 0; |
| 844 | |
| 845 | for (InputMethodInfo imi : enabledImis) { |
| 846 | // We can return true immediately after we find two or more filtered IMEs. |
| 847 | if (filteredImisCount > 1) return true; |
| 848 | final List<InputMethodSubtype> subtypes = |
| 849 | imm.getEnabledInputMethodSubtypeList(imi, true); |
| 850 | // IMEs that have no subtypes should be counted. |
| 851 | if (subtypes.isEmpty()) { |
| 852 | ++filteredImisCount; |
| 853 | continue; |
| 854 | } |
| 855 | |
| 856 | int auxCount = 0; |
| 857 | for (InputMethodSubtype subtype : subtypes) { |
| 858 | if (subtype.isAuxiliary()) { |
| 859 | ++auxCount; |
| 860 | } |
| 861 | } |
| 862 | final int nonAuxCount = subtypes.size() - auxCount; |
| 863 | |
| 864 | // IMEs that have one or more non-auxiliary subtypes should be counted. |
| 865 | // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary |
| 866 | // subtypes should be counted as well. |
| 867 | if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { |
| 868 | ++filteredImisCount; |
| 869 | continue; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | return filteredImisCount > 1 |
| 874 | // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled |
| 875 | // input method subtype (The current IME should be LatinIME.) |
| 876 | || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; |
| 877 | } |
| 878 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 879 | private IMountService getMountService() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 880 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 881 | if (service != null) { |
| 882 | return IMountService.Stub.asInterface(service); |
| 883 | } |
| 884 | return null; |
| 885 | } |
| 886 | |
| 887 | @Override |
| 888 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 889 | if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) { |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 890 | // Get the password |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 891 | final String password = v.getText().toString(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 892 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 893 | if (TextUtils.isEmpty(password)) { |
| 894 | return true; |
| 895 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 896 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 897 | // Now that we have the password clear the password field. |
| 898 | v.setText(null); |
| 899 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 900 | // Disable the password entry and back keypress while checking the password. These |
| 901 | // we either be re-enabled if the password was wrong or after the cooldown period. |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 902 | mPasswordEntry.setEnabled(false); |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 903 | setBackFunctionality(false); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 904 | |
Paul Crowley | 2555e5f | 2014-11-07 00:52:57 -0800 | [diff] [blame^] | 905 | if (password.length() >= LockPatternUtils.MIN_LOCK_PATTERN_SIZE) { |
| 906 | new DecryptTask().execute(password); |
| 907 | } else { |
| 908 | // Allow user to make as many of these as they want. |
| 909 | fakeUnlockAttempt(mPasswordEntry); |
| 910 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 911 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 912 | return true; |
| 913 | } |
| 914 | return false; |
| 915 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 916 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 917 | /** |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 918 | * Set airplane mode on the device if it isn't an LTE device. |
| 919 | * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save |
| 920 | * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor. |
| 921 | * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid |
| 922 | * both these problems, we turn the radio off. However, on certain networks turning on and |
| 923 | * off the radio takes a long time. In such cases, we are better off leaving the radio |
| 924 | * running so the latency of an E911 call is short. |
| 925 | * The behavior after this is: |
| 926 | * 1. Emergency dialing: the emergency dialer has logic to force the device out of |
| 927 | * airplane mode and restart the radio. |
| 928 | * 2. Full boot: we read the persistent settings from the previous boot and restore the |
| 929 | * radio to whatever it was before it restarted. This also happens when rebooting a |
| 930 | * phone that has no encryption. |
| 931 | */ |
| 932 | private final void setAirplaneModeIfNecessary() { |
| 933 | final boolean isLteDevice = |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 934 | getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 935 | if (!isLteDevice) { |
| 936 | Log.d(TAG, "Going into airplane mode."); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 937 | Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 938 | final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 939 | intent.putExtra("state", true); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 940 | sendBroadcastAsUser(intent, UserHandle.ALL); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
| 944 | /** |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 945 | * Code to update the state of, and handle clicks from, the "Emergency call" button. |
| 946 | * |
| 947 | * This code is mostly duplicated from the corresponding code in |
| 948 | * LockPatternUtils and LockPatternKeyguardView under frameworks/base. |
| 949 | */ |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 950 | private void updateEmergencyCallButtonState() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 951 | final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 952 | // The button isn't present at all in some configurations. |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 953 | if (emergencyCall == null) |
| 954 | return; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 955 | |
| 956 | if (isEmergencyCallCapable()) { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 957 | emergencyCall.setVisibility(View.VISIBLE); |
| 958 | emergencyCall.setOnClickListener(new View.OnClickListener() { |
| 959 | @Override |
| 960 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 961 | public void onClick(View v) { |
| 962 | takeEmergencyCallAction(); |
| 963 | } |
| 964 | }); |
| 965 | } else { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 966 | emergencyCall.setVisibility(View.GONE); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 970 | int textId; |
Tyler Gunn | 3e71b19 | 2014-09-10 15:21:33 -0700 | [diff] [blame] | 971 | if (getTelecomManager().isInCall()) { |
Paul Lawrence | 89c7570 | 2014-07-11 07:34:44 -0700 | [diff] [blame] | 972 | // Show "return to call" |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 973 | textId = R.string.cryptkeeper_return_to_call; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 974 | } else { |
| 975 | textId = R.string.cryptkeeper_emergency_call; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 976 | } |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 977 | emergencyCall.setText(textId); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | private boolean isEmergencyCallCapable() { |
| 981 | return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable); |
| 982 | } |
| 983 | |
| 984 | private void takeEmergencyCallAction() { |
Tyler Gunn | 3e71b19 | 2014-09-10 15:21:33 -0700 | [diff] [blame] | 985 | TelecomManager telecomManager = getTelecomManager(); |
| 986 | if (telecomManager.isInCall()) { |
| 987 | telecomManager.showInCallScreen(false /* showDialpad */); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 988 | } else { |
| 989 | launchEmergencyDialer(); |
| 990 | } |
| 991 | } |
| 992 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 993 | |
| 994 | private void launchEmergencyDialer() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 995 | final Intent intent = new Intent(ACTION_EMERGENCY_DIAL); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 996 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 997 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 998 | setBackFunctionality(true); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 999 | startActivity(intent); |
| 1000 | } |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 1001 | |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 1002 | private TelephonyManager getTelephonyManager() { |
| 1003 | return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
| 1004 | } |
| 1005 | |
Tyler Gunn | 3e71b19 | 2014-09-10 15:21:33 -0700 | [diff] [blame] | 1006 | private TelecomManager getTelecomManager() { |
| 1007 | return (TelecomManager) getSystemService(Context.TELECOM_SERVICE); |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 1010 | /** |
| 1011 | * Listen to key events so we can disable sounds when we get a keyinput in EditText. |
| 1012 | */ |
| 1013 | private void delayAudioNotification() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 1014 | mNotificationCountdown = 20; |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 1019 | delayAudioNotification(); |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | public boolean onTouch(View v, MotionEvent event) { |
| 1025 | delayAudioNotification(); |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | @Override |
| 1035 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 1036 | delayAudioNotification(); |
| 1037 | } |
| 1038 | |
| 1039 | @Override |
| 1040 | public void afterTextChanged(Editable s) { |
| 1041 | return; |
| 1042 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 1043 | } |