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