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; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 25 | import android.media.AudioManager; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 27 | import android.os.Bundle; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 28 | import android.os.Handler; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 29 | import android.os.IBinder; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 30 | import android.os.Message; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 31 | import android.os.PowerManager; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 33 | import android.os.ServiceManager; |
| 34 | import android.os.SystemProperties; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 36 | import android.os.storage.IMountService; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 37 | import android.os.storage.StorageManager; |
Vikram Aggarwal | 6ebbd30 | 2012-05-04 14:09:52 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 39 | import android.telephony.TelephonyManager; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 40 | import android.text.Editable; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 41 | import android.text.TextUtils; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 42 | import android.text.TextWatcher; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | import android.view.KeyEvent; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 45 | import android.view.MotionEvent; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 46 | import android.view.View; |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 47 | import android.view.WindowManager; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 48 | import android.view.View.OnClickListener; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 49 | import android.view.View.OnKeyListener; |
| 50 | import android.view.View.OnTouchListener; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 51 | import android.view.inputmethod.EditorInfo; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 52 | import android.view.inputmethod.InputMethodInfo; |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 53 | import android.view.inputmethod.InputMethodManager; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 54 | import android.view.inputmethod.InputMethodSubtype; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 55 | import android.widget.Button; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 56 | import android.widget.EditText; |
| 57 | import android.widget.ProgressBar; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 58 | import android.widget.TextView; |
| 59 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 60 | import com.android.internal.statusbar.StatusBarIcon; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 61 | import com.android.internal.telephony.ITelephony; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 62 | import com.android.internal.telephony.Phone; |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 63 | import com.android.internal.telephony.PhoneConstants; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 64 | import com.android.internal.widget.LockPatternUtils; |
| 65 | import com.android.internal.widget.LockPatternView; |
| 66 | import com.android.internal.widget.LockPatternView.Cell; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 67 | |
| 68 | import java.util.List; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Settings screens to show the UI flows for encrypting/decrypting the device. |
| 72 | * |
| 73 | * This may be started via adb for debugging the UI layout, without having to go through |
| 74 | * encryption flows everytime. It should be noted that starting the activity in this manner |
| 75 | * is only useful for verifying UI-correctness - the behavior will not be identical. |
| 76 | * <pre> |
| 77 | * $ adb shell pm enable com.android.settings/.CryptKeeper |
| 78 | * $ adb shell am start \ |
| 79 | * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \ |
| 80 | * -n com.android.settings/.CryptKeeper |
| 81 | * </pre> |
| 82 | */ |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 83 | public class CryptKeeper extends Activity implements TextView.OnEditorActionListener, |
| 84 | OnKeyListener, OnTouchListener, TextWatcher { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 85 | private static final String TAG = "CryptKeeper"; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 86 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 87 | private static final String DECRYPT_STATE = "trigger_restart_framework"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 88 | /** Message sent to us to indicate encryption update progress. */ |
| 89 | private static final int MESSAGE_UPDATE_PROGRESS = 1; |
| 90 | /** Message sent to us to cool-down (waste user's time between password attempts) */ |
| 91 | private static final int MESSAGE_COOLDOWN = 2; |
| 92 | /** Message sent to us to indicate alerting the user that we are waiting for password entry */ |
| 93 | private static final int MESSAGE_NOTIFY = 3; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 94 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 95 | // Constants used to control policy. |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 96 | private static final int MAX_FAILED_ATTEMPTS = 30; |
| 97 | private static final int COOL_DOWN_ATTEMPTS = 10; |
| 98 | private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds |
| 99 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 100 | // Intent action for launching the Emergency Dialer activity. |
| 101 | static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 102 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 103 | // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts |
| 104 | private static final String EXTRA_FORCE_VIEW = |
| 105 | "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW"; |
| 106 | private static final String FORCE_VIEW_PROGRESS = "progress"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 107 | private static final String FORCE_VIEW_ERROR = "error"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 108 | private static final String FORCE_VIEW_PASSWORD = "password"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 109 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 110 | /** 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] | 111 | private boolean mValidationComplete; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 112 | private boolean mValidationRequested; |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 113 | /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */ |
| 114 | private boolean mEncryptionGoneBad; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 115 | /** A flag to indicate when the back event should be ignored */ |
| 116 | private boolean mIgnoreBack = false; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 117 | private int mCooldown; |
| 118 | PowerManager.WakeLock mWakeLock; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 119 | private EditText mPasswordEntry; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 120 | private LockPatternView mLockPatternView; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 121 | /** Number of calls to {@link #notifyUser()} to ignore before notifying. */ |
| 122 | private int mNotificationCountdown = 0; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Used to propagate state through configuration changes (e.g. screen rotation) |
| 126 | */ |
| 127 | private static class NonConfigurationInstanceState { |
| 128 | final PowerManager.WakeLock wakelock; |
| 129 | |
| 130 | NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) { |
| 131 | wakelock = _wakelock; |
| 132 | } |
| 133 | } |
| 134 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Activity used to fade the screen to black after the password is entered. |
| 137 | */ |
Vikram Aggarwal | 9f55ae2 | 2012-04-02 13:36:35 -0700 | [diff] [blame] | 138 | public static class FadeToBlack extends Activity { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 139 | @Override |
| 140 | public void onCreate(Bundle savedInstanceState) { |
| 141 | super.onCreate(savedInstanceState); |
| 142 | setContentView(R.layout.crypt_keeper_blank); |
| 143 | } |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 144 | /** Ignore all back events. */ |
| 145 | @Override |
| 146 | public void onBackPressed() { |
| 147 | return; |
| 148 | } |
Jason parks | f1dbf55 | 2011-01-24 16:19:28 -0600 | [diff] [blame] | 149 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 150 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 151 | private class DecryptTask extends AsyncTask<String, Void, Integer> { |
| 152 | @Override |
| 153 | protected Integer doInBackground(String... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 154 | final IMountService service = getMountService(); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 155 | try { |
| 156 | return service.decryptStorage(params[0]); |
| 157 | } catch (Exception e) { |
| 158 | Log.e(TAG, "Error while decrypting...", e); |
| 159 | return -1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | protected void onPostExecute(Integer failedAttempts) { |
| 165 | if (failedAttempts == 0) { |
| 166 | // The password was entered successfully. Start the Blank activity |
| 167 | // so this activity animates to black before the devices starts. Note |
| 168 | // It has 1 second to complete the animation or it will be frozen |
| 169 | // until the boot animation comes back up. |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 170 | Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 171 | finish(); |
| 172 | startActivity(intent); |
| 173 | } else if (failedAttempts == MAX_FAILED_ATTEMPTS) { |
| 174 | // Factory reset the device. |
| 175 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 176 | } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) { |
| 177 | mCooldown = COOL_DOWN_INTERVAL; |
| 178 | cooldown(); |
| 179 | } else { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 180 | final TextView status = (TextView) findViewById(R.id.status); |
| 181 | status.setText(R.string.try_again); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 182 | // Reenable the password entry |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 183 | if (mPasswordEntry != null) { |
| 184 | mPasswordEntry.setEnabled(true); |
| 185 | } |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 189 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 190 | private class ValidationTask extends AsyncTask<Void, Void, Boolean> { |
| 191 | @Override |
| 192 | protected Boolean doInBackground(Void... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 193 | final IMountService service = getMountService(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 194 | try { |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 195 | Log.d(TAG, "Validating encryption state."); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 196 | int state = service.getEncryptionState(); |
| 197 | if (state == IMountService.ENCRYPTION_STATE_NONE) { |
| 198 | Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption."); |
| 199 | return true; // Unexpected, but fine, I guess... |
| 200 | } |
| 201 | return state == IMountService.ENCRYPTION_STATE_OK; |
| 202 | } catch (RemoteException e) { |
| 203 | Log.w(TAG, "Unable to get encryption state properly"); |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | protected void onPostExecute(Boolean result) { |
| 210 | mValidationComplete = true; |
| 211 | if (Boolean.FALSE.equals(result)) { |
| 212 | Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe."); |
| 213 | mEncryptionGoneBad = true; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 214 | } else { |
| 215 | Log.d(TAG, "Encryption state validated. Proceeding to configure UI"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 216 | } |
| 217 | setupUi(); |
| 218 | } |
| 219 | } |
| 220 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 221 | private final Handler mHandler = new Handler() { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 222 | @Override |
| 223 | public void handleMessage(Message msg) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 224 | switch (msg.what) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 225 | case MESSAGE_UPDATE_PROGRESS: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 226 | updateProgress(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 227 | break; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 228 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 229 | case MESSAGE_COOLDOWN: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 230 | cooldown(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 231 | break; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 232 | |
| 233 | case MESSAGE_NOTIFY: |
| 234 | notifyUser(); |
| 235 | break; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | }; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 239 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 240 | private AudioManager mAudioManager; |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 241 | /** The status bar where back/home/recent buttons are shown. */ |
| 242 | private StatusBarManager mStatusBar; |
| 243 | |
| 244 | /** All the widgets to disable in the status bar */ |
| 245 | final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND |
| 246 | | StatusBarManager.DISABLE_NOTIFICATION_ICONS |
| 247 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS |
| 248 | | StatusBarManager.DISABLE_SYSTEM_INFO |
| 249 | | StatusBarManager.DISABLE_HOME |
Alon Albert | 66d050b | 2013-09-30 17:08:12 -0700 | [diff] [blame] | 250 | | StatusBarManager.DISABLE_SEARCH |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 251 | | StatusBarManager.DISABLE_RECENT; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 252 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 253 | /** @return whether or not this Activity was started for debugging the UI only. */ |
| 254 | private boolean isDebugView() { |
| 255 | return getIntent().hasExtra(EXTRA_FORCE_VIEW); |
| 256 | } |
| 257 | |
| 258 | /** @return whether or not this Activity was started for debugging the specific UI view only. */ |
| 259 | private boolean isDebugView(String viewType /* non-nullable */) { |
| 260 | return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW)); |
| 261 | } |
| 262 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 263 | /** |
| 264 | * Notify the user that we are awaiting input. Currently this sends an audio alert. |
| 265 | */ |
| 266 | private void notifyUser() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 267 | if (mNotificationCountdown > 0) { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 268 | --mNotificationCountdown; |
| 269 | } else if (mAudioManager != null) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 270 | try { |
| 271 | // Play the standard keypress sound at full volume. This should be available on |
| 272 | // every device. We cannot play a ringtone here because media services aren't |
| 273 | // available yet. A DTMF-style tone is too soft to be noticed, and might not exist |
| 274 | // on tablet devices. The idea is to alert the user that something is needed: this |
| 275 | // does not have to be pleasing. |
| 276 | mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100); |
| 277 | } catch (Exception e) { |
| 278 | Log.w(TAG, "notifyUser: Exception while playing sound: " + e); |
| 279 | } |
| 280 | } |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 281 | // Notify the user again in 5 seconds. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 282 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 283 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Ignore back events after the user has entered the decrypt screen and while the device is |
| 288 | * encrypting. |
| 289 | */ |
| 290 | @Override |
| 291 | public void onBackPressed() { |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 292 | // 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] | 293 | if (mIgnoreBack) |
| 294 | return; |
| 295 | super.onBackPressed(); |
| 296 | } |
| 297 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 298 | @Override |
| 299 | public void onCreate(Bundle savedInstanceState) { |
| 300 | super.onCreate(savedInstanceState); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 301 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 302 | // If we are not encrypted or encrypting, get out quickly. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 303 | final String state = SystemProperties.get("vold.decrypt"); |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 304 | if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 305 | // Disable the crypt keeper. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 306 | PackageManager pm = getPackageManager(); |
| 307 | ComponentName name = new ComponentName(this, CryptKeeper.class); |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 308 | pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 309 | PackageManager.DONT_KILL_APP); |
| 310 | // Typically CryptKeeper is launched as the home app. We didn't |
Dianne Hackborn | 644fa42 | 2011-10-18 13:38:03 -0700 | [diff] [blame] | 311 | // want to be running, so need to finish this activity. We can count |
| 312 | // on the activity manager re-launching the new home app upon finishing |
| 313 | // this one, since this will leave the activity stack empty. |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 314 | // NOTE: This is really grungy. I think it would be better for the |
| 315 | // activity manager to explicitly launch the crypt keeper instead of |
| 316 | // home in the situation where we need to decrypt the device |
| 317 | finish(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 318 | return; |
| 319 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 320 | |
Vikram Aggarwal | b96b35a | 2012-05-01 11:09:39 -0700 | [diff] [blame] | 321 | // Disable the status bar, but do NOT disable back because the user needs a way to go |
| 322 | // from keyboard settings and back to the password screen. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 323 | mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
| 324 | mStatusBar.disable(sWidgetsToDisable); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 325 | |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 326 | setAirplaneModeIfNecessary(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 327 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 328 | // Check for (and recover) retained instance data |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 329 | final Object lastInstance = getLastNonConfigurationInstance(); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 330 | if (lastInstance instanceof NonConfigurationInstanceState) { |
| 331 | NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance; |
| 332 | mWakeLock = retained.wakelock; |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 333 | Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 334 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 335 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 336 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Note, we defer the state check and screen setup to onStart() because this will be |
| 339 | * re-run if the user clicks the power button (sleeping/waking the screen), and this is |
| 340 | * especially important if we were to lose the wakelock for any reason. |
| 341 | */ |
| 342 | @Override |
| 343 | public void onStart() { |
| 344 | super.onStart(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 345 | setupUi(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Initializes the UI based on the current state of encryption. |
| 350 | * This is idempotent - calling repeatedly will simply re-initialize the UI. |
| 351 | */ |
| 352 | private void setupUi() { |
| 353 | if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) { |
| 354 | setContentView(R.layout.crypt_keeper_progress); |
| 355 | showFactoryReset(); |
| 356 | return; |
| 357 | } |
| 358 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 359 | final String progress = SystemProperties.get("vold.encrypt_progress"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 360 | if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) { |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 361 | setContentView(R.layout.crypt_keeper_progress); |
| 362 | encryptionProgressInit(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 363 | } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) { |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 364 | final IMountService service = getMountService(); |
| 365 | int type = StorageManager.CRYPT_TYPE_PASSWORD; |
| 366 | try { |
| 367 | type = service.getPasswordType(); |
| 368 | } catch (Exception e) { |
| 369 | Log.e(TAG, "Error while getting type - showing default dialog" + e); |
| 370 | } |
| 371 | |
| 372 | if(type == StorageManager.CRYPT_TYPE_PIN) { |
| 373 | setContentView(R.layout.crypt_keeper_pin_entry); |
| 374 | } else if (type == StorageManager.CRYPT_TYPE_PATTERN) { |
| 375 | setContentView(R.layout.crypt_keeper_pattern_entry); |
| 376 | } else { |
| 377 | setContentView(R.layout.crypt_keeper_password_entry); |
| 378 | } |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 379 | passwordEntryInit(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 380 | } else if (!mValidationRequested) { |
| 381 | // We're supposed to be encrypted, but no validation has been done. |
| 382 | new ValidationTask().execute((Void[]) null); |
| 383 | mValidationRequested = true; |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 387 | @Override |
| 388 | public void onStop() { |
| 389 | super.onStop(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 390 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 391 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
| 392 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop() |
| 397 | * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears |
| 398 | * mWakeLock so the subsequent call to onDestroy does not release it. |
| 399 | */ |
| 400 | @Override |
| 401 | public Object onRetainNonConfigurationInstance() { |
| 402 | NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 403 | Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 404 | mWakeLock = null; |
| 405 | return state; |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void onDestroy() { |
| 410 | super.onDestroy(); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 411 | |
| 412 | if (mWakeLock != null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 413 | Log.d(TAG, "Releasing and destroying wakelock"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 414 | mWakeLock.release(); |
| 415 | mWakeLock = null; |
| 416 | } |
| 417 | } |
| 418 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Start encrypting the device. |
| 421 | */ |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 422 | private void encryptionProgressInit() { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 423 | // Accquire a partial wakelock to prevent the device from sleeping. Note |
| 424 | // we never release this wakelock as we will be restarted after the device |
| 425 | // is encrypted. |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 426 | Log.d(TAG, "Encryption progress screen initializing."); |
Ben Komalo | 9ee164f | 2011-09-21 10:40:50 -0700 | [diff] [blame] | 427 | if (mWakeLock == null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 428 | Log.d(TAG, "Acquiring wakelock."); |
| 429 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 430 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 431 | mWakeLock.acquire(); |
| 432 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 433 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 434 | ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 435 | // Ignore all back presses from now, both hard and soft keys. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 436 | setBackFunctionality(false); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 437 | // Start the first run of progress manually. This method sets up messages to occur at |
| 438 | // repeated intervals. |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 439 | updateProgress(); |
| 440 | } |
| 441 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 442 | private void showFactoryReset() { |
| 443 | // Hide the encryption-bot to make room for the "factory reset" button |
| 444 | findViewById(R.id.encroid).setVisibility(View.GONE); |
| 445 | |
| 446 | // Show the reset button, failure text, and a divider |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 447 | final Button button = (Button) findViewById(R.id.factory_reset); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 448 | button.setVisibility(View.VISIBLE); |
| 449 | button.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 450 | @Override |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 451 | public void onClick(View v) { |
| 452 | // Factory reset the device. |
| 453 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 454 | } |
| 455 | }); |
| 456 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 457 | // Alert the user of the failure. |
| 458 | ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title); |
| 459 | ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 460 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 461 | final View view = findViewById(R.id.bottom_divider); |
| 462 | // 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] | 463 | if (view != null) { |
| 464 | view.setVisibility(View.VISIBLE); |
| 465 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 468 | private void updateProgress() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 469 | final String state = SystemProperties.get("vold.encrypt_progress"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 470 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 471 | if ("error_partially_encrypted".equals(state)) { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 472 | showFactoryReset(); |
| 473 | return; |
| 474 | } |
| 475 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 476 | int progress = 0; |
| 477 | try { |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 478 | // Force a 50% progress state when debugging the view. |
| 479 | progress = isDebugView() ? 50 : Integer.parseInt(state); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 480 | } catch (Exception e) { |
| 481 | Log.w(TAG, "Error parsing progress: " + e.toString()); |
| 482 | } |
| 483 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 484 | final CharSequence status = getText(R.string.crypt_keeper_setup_description); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 485 | Log.v(TAG, "Encryption progress: " + progress); |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 486 | final TextView tv = (TextView) findViewById(R.id.status); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 487 | if (tv != null) { |
| 488 | tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress))); |
| 489 | } |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 490 | // Check the progress every 5 seconds |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 491 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
| 492 | mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 493 | } |
| 494 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 495 | /** 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] | 496 | private void cooldown() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 497 | final TextView status = (TextView) findViewById(R.id.status); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 498 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 499 | if (mCooldown <= 0) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 500 | // Re-enable the password entry and back presses. |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 501 | mPasswordEntry.setEnabled(true); |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 502 | setBackFunctionality(true); |
Vikram Aggarwal | f576dd6 | 2012-05-22 10:58:10 -0700 | [diff] [blame] | 503 | status.setText(R.string.enter_password); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 504 | } else { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 505 | CharSequence template = getText(R.string.crypt_keeper_cooldown); |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 506 | status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown))); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 507 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 508 | mCooldown--; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 509 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 510 | mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 511 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 512 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 513 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 514 | /** |
| 515 | * Sets the back status: enabled or disabled according to the parameter. |
| 516 | * @param isEnabled true if back is enabled, false otherwise. |
| 517 | */ |
| 518 | private final void setBackFunctionality(boolean isEnabled) { |
| 519 | mIgnoreBack = !isEnabled; |
| 520 | if (isEnabled) { |
| 521 | mStatusBar.disable(sWidgetsToDisable); |
| 522 | } else { |
| 523 | mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK); |
| 524 | } |
| 525 | } |
| 526 | |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 527 | protected LockPatternView.OnPatternListener mChooseNewLockPatternListener = |
| 528 | new LockPatternView.OnPatternListener() { |
| 529 | |
| 530 | @Override |
| 531 | public void onPatternStart() { |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void onPatternCleared() { |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public void onPatternDetected(List<LockPatternView.Cell> pattern) { |
| 540 | new DecryptTask().execute(LockPatternUtils.patternToString(pattern)); |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void onPatternCellAdded(List<Cell> pattern) { |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | private void passwordEntryInit() { |
| 549 | // Password/pin case |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 550 | mPasswordEntry = (EditText) findViewById(R.id.passwordEntry); |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 551 | if (mPasswordEntry != null){ |
| 552 | mPasswordEntry.setOnEditorActionListener(this); |
| 553 | mPasswordEntry.requestFocus(); |
| 554 | // Become quiet when the user interacts with the Edit text screen. |
| 555 | mPasswordEntry.setOnKeyListener(this); |
| 556 | mPasswordEntry.setOnTouchListener(this); |
| 557 | mPasswordEntry.addTextChangedListener(this); |
| 558 | } |
| 559 | |
| 560 | // Pattern case |
| 561 | mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern); |
| 562 | if (mLockPatternView != null) { |
| 563 | mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener); |
| 564 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 565 | |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 566 | // Disable the Emergency call button if the device has no voice telephone capability |
| 567 | final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
| 568 | if (!tm.isVoiceCapable()) { |
| 569 | final View emergencyCall = findViewById(R.id.emergencyCallButton); |
| 570 | if (emergencyCall != null) { |
| 571 | Log.d(TAG, "Removing the emergency Call button"); |
| 572 | emergencyCall.setVisibility(View.GONE); |
| 573 | } |
| 574 | } |
| 575 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 576 | final View imeSwitcher = findViewById(R.id.switch_ime_button); |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 577 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 578 | Context.INPUT_METHOD_SERVICE); |
| 579 | if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) { |
| 580 | imeSwitcher.setVisibility(View.VISIBLE); |
| 581 | imeSwitcher.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 582 | @Override |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 583 | public void onClick(View v) { |
| 584 | imm.showInputMethodPicker(); |
| 585 | } |
| 586 | }); |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 587 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 588 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 589 | // We want to keep the screen on while waiting for input. In minimal boot mode, the device |
| 590 | // is completely non-functional, and we want the user to notice the device and enter a |
| 591 | // password. |
| 592 | if (mWakeLock == null) { |
| 593 | Log.d(TAG, "Acquiring wakelock."); |
| 594 | final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 595 | if (pm != null) { |
| 596 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 597 | mWakeLock.acquire(); |
| 598 | } |
| 599 | } |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 600 | // Asynchronously throw up the IME, since there are issues with requesting it to be shown |
| 601 | // immediately. |
Paul Lawrence | e54e932 | 2014-03-13 14:05:48 -0700 | [diff] [blame^] | 602 | if (mLockPatternView == null) { |
| 603 | mHandler.postDelayed(new Runnable() { |
| 604 | @Override public void run() { |
| 605 | imm.showSoftInputUnchecked(0, null); |
| 606 | } |
| 607 | }, 0); |
| 608 | } |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 609 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 610 | updateEmergencyCallButtonState(); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 611 | // 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] | 612 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 613 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000); |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 614 | |
| 615 | // Dismiss keyguard while this screen is showing. |
| 616 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 617 | } |
| 618 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 619 | /** |
| 620 | * Method adapted from com.android.inputmethod.latin.Utils |
| 621 | * |
| 622 | * @param imm The input method manager |
| 623 | * @param shouldIncludeAuxiliarySubtypes |
| 624 | * @return true if we have multiple IMEs to choose from |
| 625 | */ |
| 626 | private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm, |
| 627 | final boolean shouldIncludeAuxiliarySubtypes) { |
| 628 | final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); |
| 629 | |
| 630 | // Number of the filtered IMEs |
| 631 | int filteredImisCount = 0; |
| 632 | |
| 633 | for (InputMethodInfo imi : enabledImis) { |
| 634 | // We can return true immediately after we find two or more filtered IMEs. |
| 635 | if (filteredImisCount > 1) return true; |
| 636 | final List<InputMethodSubtype> subtypes = |
| 637 | imm.getEnabledInputMethodSubtypeList(imi, true); |
| 638 | // IMEs that have no subtypes should be counted. |
| 639 | if (subtypes.isEmpty()) { |
| 640 | ++filteredImisCount; |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | int auxCount = 0; |
| 645 | for (InputMethodSubtype subtype : subtypes) { |
| 646 | if (subtype.isAuxiliary()) { |
| 647 | ++auxCount; |
| 648 | } |
| 649 | } |
| 650 | final int nonAuxCount = subtypes.size() - auxCount; |
| 651 | |
| 652 | // IMEs that have one or more non-auxiliary subtypes should be counted. |
| 653 | // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary |
| 654 | // subtypes should be counted as well. |
| 655 | if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { |
| 656 | ++filteredImisCount; |
| 657 | continue; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return filteredImisCount > 1 |
| 662 | // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled |
| 663 | // input method subtype (The current IME should be LatinIME.) |
| 664 | || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; |
| 665 | } |
| 666 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 667 | private IMountService getMountService() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 668 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 669 | if (service != null) { |
| 670 | return IMountService.Stub.asInterface(service); |
| 671 | } |
| 672 | return null; |
| 673 | } |
| 674 | |
| 675 | @Override |
| 676 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 677 | if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) { |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 678 | // Get the password |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 679 | final String password = v.getText().toString(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 680 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 681 | if (TextUtils.isEmpty(password)) { |
| 682 | return true; |
| 683 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 684 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 685 | // Now that we have the password clear the password field. |
| 686 | v.setText(null); |
| 687 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 688 | // Disable the password entry and back keypress while checking the password. These |
| 689 | // 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] | 690 | mPasswordEntry.setEnabled(false); |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 691 | setBackFunctionality(false); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 692 | |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 693 | Log.d(TAG, "Attempting to send command to decrypt"); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 694 | new DecryptTask().execute(password); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 695 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 696 | return true; |
| 697 | } |
| 698 | return false; |
| 699 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 700 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 701 | /** |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 702 | * Set airplane mode on the device if it isn't an LTE device. |
| 703 | * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save |
| 704 | * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor. |
| 705 | * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid |
| 706 | * both these problems, we turn the radio off. However, on certain networks turning on and |
| 707 | * off the radio takes a long time. In such cases, we are better off leaving the radio |
| 708 | * running so the latency of an E911 call is short. |
| 709 | * The behavior after this is: |
| 710 | * 1. Emergency dialing: the emergency dialer has logic to force the device out of |
| 711 | * airplane mode and restart the radio. |
| 712 | * 2. Full boot: we read the persistent settings from the previous boot and restore the |
| 713 | * radio to whatever it was before it restarted. This also happens when rebooting a |
| 714 | * phone that has no encryption. |
| 715 | */ |
| 716 | private final void setAirplaneModeIfNecessary() { |
| 717 | final boolean isLteDevice = |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 718 | TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 719 | if (!isLteDevice) { |
| 720 | Log.d(TAG, "Going into airplane mode."); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 721 | Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 722 | final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 723 | intent.putExtra("state", true); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 724 | sendBroadcastAsUser(intent, UserHandle.ALL); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | /** |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 729 | * Code to update the state of, and handle clicks from, the "Emergency call" button. |
| 730 | * |
| 731 | * This code is mostly duplicated from the corresponding code in |
| 732 | * LockPatternUtils and LockPatternKeyguardView under frameworks/base. |
| 733 | */ |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 734 | private void updateEmergencyCallButtonState() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 735 | final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 736 | // The button isn't present at all in some configurations. |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 737 | if (emergencyCall == null) |
| 738 | return; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 739 | |
| 740 | if (isEmergencyCallCapable()) { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 741 | emergencyCall.setVisibility(View.VISIBLE); |
| 742 | emergencyCall.setOnClickListener(new View.OnClickListener() { |
| 743 | @Override |
| 744 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 745 | public void onClick(View v) { |
| 746 | takeEmergencyCallAction(); |
| 747 | } |
| 748 | }); |
| 749 | } else { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 750 | emergencyCall.setVisibility(View.GONE); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 751 | return; |
| 752 | } |
| 753 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 754 | final int newState = TelephonyManager.getDefault().getCallState(); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 755 | int textId; |
| 756 | if (newState == TelephonyManager.CALL_STATE_OFFHOOK) { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 757 | // Show "return to call" text and show phone icon |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 758 | textId = R.string.cryptkeeper_return_to_call; |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 759 | final int phoneCallIcon = R.drawable.stat_sys_phone_call; |
| 760 | emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 761 | } else { |
| 762 | textId = R.string.cryptkeeper_emergency_call; |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 763 | final int emergencyIcon = R.drawable.ic_emergency; |
| 764 | emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 765 | } |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 766 | emergencyCall.setText(textId); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | private boolean isEmergencyCallCapable() { |
| 770 | return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable); |
| 771 | } |
| 772 | |
| 773 | private void takeEmergencyCallAction() { |
| 774 | if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) { |
| 775 | resumeCall(); |
| 776 | } else { |
| 777 | launchEmergencyDialer(); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | private void resumeCall() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 782 | final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 783 | if (phone != null) { |
| 784 | try { |
| 785 | phone.showCallScreen(); |
| 786 | } catch (RemoteException e) { |
| 787 | Log.e(TAG, "Error calling ITelephony service: " + e); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | private void launchEmergencyDialer() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 793 | final Intent intent = new Intent(ACTION_EMERGENCY_DIAL); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 794 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 795 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 796 | startActivity(intent); |
| 797 | } |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 798 | |
| 799 | /** |
| 800 | * Listen to key events so we can disable sounds when we get a keyinput in EditText. |
| 801 | */ |
| 802 | private void delayAudioNotification() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 803 | mNotificationCountdown = 20; |
| 804 | } |
| 805 | |
| 806 | @Override |
| 807 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 808 | delayAudioNotification(); |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | @Override |
| 813 | public boolean onTouch(View v, MotionEvent event) { |
| 814 | delayAudioNotification(); |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | @Override |
| 819 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | @Override |
| 824 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 825 | delayAudioNotification(); |
| 826 | } |
| 827 | |
| 828 | @Override |
| 829 | public void afterTextChanged(Editable s) { |
| 830 | return; |
| 831 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 832 | } |