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