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