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