CryptKeeper.java revision 1b5cc427f06019936e40ee8e43ae931b8752eb11
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.ActivityManager;
21import android.app.StatusBarManager;
22import android.content.BroadcastReceiver;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
26import android.content.pm.ActivityInfo;
27import android.content.pm.PackageManager;
28import android.content.res.Resources.NotFoundException;
29import android.media.AudioManager;
30import android.os.AsyncTask;
31import android.os.Bundle;
32import android.os.Handler;
33import android.os.IBinder;
34import android.os.Message;
35import android.os.PowerManager;
36import android.os.RemoteException;
37import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.os.UserHandle;
40import android.os.storage.IMountService;
41import android.os.storage.StorageManager;
42import android.provider.Settings;
43import android.telecom.TelecomManager;
44import android.telephony.TelephonyManager;
45import android.text.Editable;
46import android.text.TextUtils;
47import android.text.TextWatcher;
48import android.text.format.DateUtils;
49import android.util.Log;
50import android.view.KeyEvent;
51import android.view.MotionEvent;
52import android.view.View;
53import android.view.WindowManager;
54import android.view.View.OnClickListener;
55import android.view.View.OnKeyListener;
56import android.view.View.OnTouchListener;
57import android.view.inputmethod.EditorInfo;
58import android.view.inputmethod.InputMethodInfo;
59import android.view.inputmethod.InputMethodManager;
60import android.view.inputmethod.InputMethodSubtype;
61import android.widget.Button;
62import android.widget.EditText;
63import android.widget.ProgressBar;
64import android.widget.TextView;
65
66import com.android.internal.telephony.PhoneConstants;
67import com.android.internal.widget.LockPatternUtils;
68import com.android.internal.widget.LockPatternView;
69import com.android.internal.widget.LockPatternView.Cell;
70
71import static com.android.internal.widget.LockPatternView.DisplayMode;
72
73import java.util.List;
74
75/**
76 * Settings screens to show the UI flows for encrypting/decrypting the device.
77 *
78 * This may be started via adb for debugging the UI layout, without having to go through
79 * encryption flows everytime. It should be noted that starting the activity in this manner
80 * is only useful for verifying UI-correctness - the behavior will not be identical.
81 * <pre>
82 * $ adb shell pm enable com.android.settings/.CryptKeeper
83 * $ adb shell am start \
84 *     -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
85 *     -n com.android.settings/.CryptKeeper
86 * </pre>
87 */
88public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
89        OnKeyListener, OnTouchListener, TextWatcher {
90    private static final String TAG = "CryptKeeper";
91
92    private static final String DECRYPT_STATE = "trigger_restart_framework";
93
94    /** Message sent to us to indicate encryption update progress. */
95    private static final int MESSAGE_UPDATE_PROGRESS = 1;
96    /** Message sent to us to indicate alerting the user that we are waiting for password entry */
97    private static final int MESSAGE_NOTIFY = 2;
98
99    // Constants used to control policy.
100    private static final int MAX_FAILED_ATTEMPTS = 30;
101    private static final int COOL_DOWN_ATTEMPTS = 10;
102
103    // Intent action for launching the Emergency Dialer activity.
104    static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
105
106    // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
107    private static final String EXTRA_FORCE_VIEW =
108            "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
109    private static final String FORCE_VIEW_PROGRESS = "progress";
110    private static final String FORCE_VIEW_ERROR = "error";
111    private static final String FORCE_VIEW_PASSWORD = "password";
112
113    /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
114    private boolean mValidationComplete;
115    private boolean mValidationRequested;
116    /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
117    private boolean mEncryptionGoneBad;
118    /** If gone bad, should we show encryption failed (false) or corrupt (true)*/
119    private boolean mCorrupt;
120    /** A flag to indicate when the back event should be ignored */
121    private boolean mIgnoreBack = false;
122    /** When set, blocks unlocking. Set every COOL_DOWN_ATTEMPTS attempts, only cleared
123        by power cycling phone. */
124    private boolean mCooldown = false;
125
126    PowerManager.WakeLock mWakeLock;
127    private EditText mPasswordEntry;
128    private LockPatternView mLockPatternView;
129    /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
130    private int mNotificationCountdown = 0;
131    /** Number of calls to {@link #notifyUser()} before we release the wakelock */
132    private int mReleaseWakeLockCountdown = 0;
133    private int mStatusString = R.string.enter_password;
134
135    // how long we wait to clear a wrong pattern
136    private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
137
138    // how long we wait to clear a right pattern
139    private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
140
141    // When the user enters a short pin/password, run this to show an error,
142    // but don't count it against attempts.
143    private final Runnable mFakeUnlockAttemptRunnable = new Runnable() {
144        @Override
145        public void run() {
146            handleBadAttempt(1 /* failedAttempt */);
147        }
148    };
149
150    // TODO: this should be tuned to match minimum decryption timeout
151    private static final int FAKE_ATTEMPT_DELAY = 1000;
152
153    private final Runnable mClearPatternRunnable = new Runnable() {
154        @Override
155        public void run() {
156            mLockPatternView.clearPattern();
157        }
158    };
159
160    /**
161     * Used to propagate state through configuration changes (e.g. screen rotation)
162     */
163    private static class NonConfigurationInstanceState {
164        final PowerManager.WakeLock wakelock;
165
166        NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
167            wakelock = _wakelock;
168        }
169    }
170
171    private class DecryptTask extends AsyncTask<String, Void, Integer> {
172        private void hide(int id) {
173            View view = findViewById(id);
174            if (view != null) {
175                view.setVisibility(View.GONE);
176            }
177        }
178
179        @Override
180        protected void onPreExecute() {
181            super.onPreExecute();
182            beginAttempt();
183        }
184
185        @Override
186        protected Integer doInBackground(String... params) {
187            final IMountService service = getMountService();
188            try {
189                return service.decryptStorage(params[0]);
190            } catch (Exception e) {
191                Log.e(TAG, "Error while decrypting...", e);
192                return -1;
193            }
194        }
195
196        @Override
197        protected void onPostExecute(Integer failedAttempts) {
198            if (failedAttempts == 0) {
199                // The password was entered successfully. Simply do nothing
200                // and wait for the service restart to switch to surfacefligner
201                if (mLockPatternView != null) {
202                    mLockPatternView.removeCallbacks(mClearPatternRunnable);
203                    mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
204                }
205                final TextView status = (TextView) findViewById(R.id.status);
206                status.setText(R.string.starting_android);
207                hide(R.id.passwordEntry);
208                hide(R.id.switch_ime_button);
209                hide(R.id.lockPattern);
210                hide(R.id.owner_info);
211                hide(R.id.emergencyCallButton);
212            } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
213                // Factory reset the device.
214                Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
215                intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
216                intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
217                sendBroadcast(intent);
218            } else if (failedAttempts == -1) {
219                // Right password, but decryption failed. Tell user bad news ...
220                setContentView(R.layout.crypt_keeper_progress);
221                showFactoryReset(true);
222                return;
223            } else {
224                handleBadAttempt(failedAttempts);
225            }
226        }
227    }
228
229    private void beginAttempt() {
230        final TextView status = (TextView) findViewById(R.id.status);
231        status.setText(R.string.checking_decryption);
232    }
233
234    private void handleBadAttempt(Integer failedAttempts) {
235        // Wrong entry. Handle pattern case.
236        if (mLockPatternView != null) {
237            mLockPatternView.setDisplayMode(DisplayMode.Wrong);
238            mLockPatternView.removeCallbacks(mClearPatternRunnable);
239            mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
240        }
241        if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
242            mCooldown = true;
243            // No need to setBackFunctionality(false) - it's already done
244            // at this point.
245            cooldown();
246        } else {
247            final TextView status = (TextView) findViewById(R.id.status);
248
249            int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
250            if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
251                CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
252                CharSequence warning = TextUtils.expandTemplate(warningTemplate,
253                        Integer.toString(remainingAttempts));
254                status.setText(warning);
255            } else {
256                int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
257                try {
258                    final IMountService service = getMountService();
259                    passwordType = service.getPasswordType();
260                } catch (Exception e) {
261                    Log.e(TAG, "Error calling mount service " + e);
262                }
263
264                if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
265                    status.setText(R.string.cryptkeeper_wrong_pin);
266                } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
267                    status.setText(R.string.cryptkeeper_wrong_pattern);
268                } else {
269                    status.setText(R.string.cryptkeeper_wrong_password);
270                }
271            }
272
273            if (mLockPatternView != null) {
274                mLockPatternView.setDisplayMode(DisplayMode.Wrong);
275                mLockPatternView.setEnabled(true);
276            }
277
278            // Reenable the password entry
279            if (mPasswordEntry != null) {
280                mPasswordEntry.setEnabled(true);
281                final InputMethodManager imm = (InputMethodManager) getSystemService(
282                        Context.INPUT_METHOD_SERVICE);
283                imm.showSoftInput(mPasswordEntry, 0);
284                setBackFunctionality(true);
285            }
286        }
287    }
288
289    private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
290        int state;
291
292        @Override
293        protected Boolean doInBackground(Void... params) {
294            final IMountService service = getMountService();
295            try {
296                Log.d(TAG, "Validating encryption state.");
297                state = service.getEncryptionState();
298                if (state == IMountService.ENCRYPTION_STATE_NONE) {
299                    Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
300                    return true; // Unexpected, but fine, I guess...
301                }
302                return state == IMountService.ENCRYPTION_STATE_OK;
303            } catch (RemoteException e) {
304                Log.w(TAG, "Unable to get encryption state properly");
305                return true;
306            }
307        }
308
309        @Override
310        protected void onPostExecute(Boolean result) {
311            mValidationComplete = true;
312            if (Boolean.FALSE.equals(result)) {
313                Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
314                mEncryptionGoneBad = true;
315                mCorrupt = state == IMountService.ENCRYPTION_STATE_ERROR_CORRUPT;
316            } else {
317                Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
318            }
319            setupUi();
320        }
321    }
322
323    private final Handler mHandler = new Handler() {
324        @Override
325        public void handleMessage(Message msg) {
326            switch (msg.what) {
327            case MESSAGE_UPDATE_PROGRESS:
328                updateProgress();
329                break;
330
331            case MESSAGE_NOTIFY:
332                notifyUser();
333                break;
334            }
335        }
336    };
337
338    private AudioManager mAudioManager;
339    /** The status bar where back/home/recent buttons are shown. */
340    private StatusBarManager mStatusBar;
341
342    /** All the widgets to disable in the status bar */
343    final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
344            | StatusBarManager.DISABLE_NOTIFICATION_ICONS
345            | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
346            | StatusBarManager.DISABLE_SYSTEM_INFO
347            | StatusBarManager.DISABLE_HOME
348            | StatusBarManager.DISABLE_SEARCH
349            | StatusBarManager.DISABLE_RECENT;
350
351    protected static final int MIN_LENGTH_BEFORE_REPORT = LockPatternUtils.MIN_LOCK_PATTERN_SIZE;
352
353    /** @return whether or not this Activity was started for debugging the UI only. */
354    private boolean isDebugView() {
355        return getIntent().hasExtra(EXTRA_FORCE_VIEW);
356    }
357
358    /** @return whether or not this Activity was started for debugging the specific UI view only. */
359    private boolean isDebugView(String viewType /* non-nullable */) {
360        return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
361    }
362
363    /**
364     * Notify the user that we are awaiting input. Currently this sends an audio alert.
365     */
366    private void notifyUser() {
367        if (mNotificationCountdown > 0) {
368            --mNotificationCountdown;
369        } else if (mAudioManager != null) {
370            try {
371                // Play the standard keypress sound at full volume. This should be available on
372                // every device. We cannot play a ringtone here because media services aren't
373                // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
374                // on tablet devices. The idea is to alert the user that something is needed: this
375                // does not have to be pleasing.
376                mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
377            } catch (Exception e) {
378                Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
379            }
380        }
381        // Notify the user again in 5 seconds.
382        mHandler.removeMessages(MESSAGE_NOTIFY);
383        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
384
385        if (mWakeLock.isHeld()) {
386            if (mReleaseWakeLockCountdown > 0) {
387                --mReleaseWakeLockCountdown;
388            } else {
389                mWakeLock.release();
390            }
391        }
392    }
393
394    /**
395     * Ignore back events after the user has entered the decrypt screen and while the device is
396     * encrypting.
397     */
398    @Override
399    public void onBackPressed() {
400        // In the rare case that something pressed back even though we were disabled.
401        if (mIgnoreBack)
402            return;
403        super.onBackPressed();
404    }
405
406    @Override
407    public void onCreate(Bundle savedInstanceState) {
408        super.onCreate(savedInstanceState);
409
410        // If we are not encrypted or encrypting, get out quickly.
411        final String state = SystemProperties.get("vold.decrypt");
412        if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
413            disableCryptKeeperComponent(this);
414            // Typically CryptKeeper is launched as the home app.  We didn't
415            // want to be running, so need to finish this activity.  We can count
416            // on the activity manager re-launching the new home app upon finishing
417            // this one, since this will leave the activity stack empty.
418            // NOTE: This is really grungy.  I think it would be better for the
419            // activity manager to explicitly launch the crypt keeper instead of
420            // home in the situation where we need to decrypt the device
421            finish();
422            return;
423        }
424
425        try {
426            if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
427                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
428            }
429        } catch (NotFoundException e) {
430        }
431
432        // Disable the status bar, but do NOT disable back because the user needs a way to go
433        // from keyboard settings and back to the password screen.
434        mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
435        mStatusBar.disable(sWidgetsToDisable);
436
437        setAirplaneModeIfNecessary();
438        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
439        // Check for (and recover) retained instance data
440        final Object lastInstance = getLastNonConfigurationInstance();
441        if (lastInstance instanceof NonConfigurationInstanceState) {
442            NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
443            mWakeLock = retained.wakelock;
444            Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
445        }
446    }
447
448    /**
449     * Note, we defer the state check and screen setup to onStart() because this will be
450     * re-run if the user clicks the power button (sleeping/waking the screen), and this is
451     * especially important if we were to lose the wakelock for any reason.
452     */
453    @Override
454    public void onStart() {
455        super.onStart();
456        setupUi();
457    }
458
459    /**
460     * Initializes the UI based on the current state of encryption.
461     * This is idempotent - calling repeatedly will simply re-initialize the UI.
462     */
463    private void setupUi() {
464        if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
465            setContentView(R.layout.crypt_keeper_progress);
466            showFactoryReset(mCorrupt);
467            return;
468        }
469
470        final String progress = SystemProperties.get("vold.encrypt_progress");
471        if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
472            setContentView(R.layout.crypt_keeper_progress);
473            encryptionProgressInit();
474        } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
475            new AsyncTask<Void, Void, Void>() {
476                int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
477                String owner_info;
478                boolean pattern_visible;
479
480                @Override
481                public Void doInBackground(Void... v) {
482                    try {
483                        final IMountService service = getMountService();
484                        passwordType = service.getPasswordType();
485                        owner_info = service.getField(StorageManager.OWNER_INFO_KEY);
486                        pattern_visible = !("0".equals(service.getField(StorageManager.PATTERN_VISIBLE_KEY)));
487                    } catch (Exception e) {
488                        Log.e(TAG, "Error calling mount service " + e);
489                    }
490
491                    return null;
492                }
493
494                @Override
495                public void onPostExecute(java.lang.Void v) {
496                    if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
497                        setContentView(R.layout.crypt_keeper_pin_entry);
498                        mStatusString = R.string.enter_pin;
499                    } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
500                        setContentView(R.layout.crypt_keeper_pattern_entry);
501                        setBackFunctionality(false);
502                        mStatusString = R.string.enter_pattern;
503                    } else {
504                        setContentView(R.layout.crypt_keeper_password_entry);
505                        mStatusString = R.string.enter_password;
506                    }
507                    final TextView status = (TextView) findViewById(R.id.status);
508                    status.setText(mStatusString);
509
510                    final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
511                    ownerInfo.setText(owner_info);
512                    ownerInfo.setSelected(true); // Required for marquee'ing to work
513
514                    passwordEntryInit();
515
516                    if (mLockPatternView != null) {
517                        mLockPatternView.setInStealthMode(!pattern_visible);
518                    }
519                    if (mCooldown) {
520                        // in case we are cooling down and coming back from emergency dialler
521                        setBackFunctionality(false);
522                        cooldown();
523                    }
524
525                }
526            }.execute();
527        } else if (!mValidationRequested) {
528            // We're supposed to be encrypted, but no validation has been done.
529            new ValidationTask().execute((Void[]) null);
530            mValidationRequested = true;
531        }
532    }
533
534    @Override
535    public void onStop() {
536        super.onStop();
537        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
538        mHandler.removeMessages(MESSAGE_NOTIFY);
539    }
540
541    /**
542     * Reconfiguring, so propagate the wakelock to the next instance.  This runs between onStop()
543     * and onDestroy() and only if we are changing configuration (e.g. rotation).  Also clears
544     * mWakeLock so the subsequent call to onDestroy does not release it.
545     */
546    @Override
547    public Object onRetainNonConfigurationInstance() {
548        NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
549        Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
550        mWakeLock = null;
551        return state;
552    }
553
554    @Override
555    public void onDestroy() {
556        super.onDestroy();
557
558        if (mWakeLock != null) {
559            Log.d(TAG, "Releasing and destroying wakelock");
560            mWakeLock.release();
561            mWakeLock = null;
562        }
563    }
564
565    /**
566     * Start encrypting the device.
567     */
568    private void encryptionProgressInit() {
569        // Accquire a partial wakelock to prevent the device from sleeping. Note
570        // we never release this wakelock as we will be restarted after the device
571        // is encrypted.
572        Log.d(TAG, "Encryption progress screen initializing.");
573        if (mWakeLock == null) {
574            Log.d(TAG, "Acquiring wakelock.");
575            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
576            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
577            mWakeLock.acquire();
578        }
579
580        ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
581        // Ignore all back presses from now, both hard and soft keys.
582        setBackFunctionality(false);
583        // Start the first run of progress manually. This method sets up messages to occur at
584        // repeated intervals.
585        updateProgress();
586    }
587
588    /**
589     * Show factory reset screen allowing the user to reset their phone when
590     * there is nothing else we can do
591     * @param corrupt true if userdata is corrupt, false if encryption failed
592     *        partway through
593     */
594    private void showFactoryReset(final boolean corrupt) {
595        // Hide the encryption-bot to make room for the "factory reset" button
596        findViewById(R.id.encroid).setVisibility(View.GONE);
597
598        // Show the reset button, failure text, and a divider
599        final Button button = (Button) findViewById(R.id.factory_reset);
600        button.setVisibility(View.VISIBLE);
601        button.setOnClickListener(new OnClickListener() {
602                @Override
603            public void onClick(View v) {
604                // Factory reset the device.
605                Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
606                intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
607                intent.putExtra(Intent.EXTRA_REASON,
608                        "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
609                sendBroadcast(intent);
610            }
611        });
612
613        // Alert the user of the failure.
614        if (corrupt) {
615            ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
616            ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
617        } else {
618            ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
619            ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
620        }
621
622        final View view = findViewById(R.id.bottom_divider);
623        // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
624        if (view != null) {
625            view.setVisibility(View.VISIBLE);
626        }
627    }
628
629    private void updateProgress() {
630        final String state = SystemProperties.get("vold.encrypt_progress");
631
632        if ("error_partially_encrypted".equals(state)) {
633            showFactoryReset(false);
634            return;
635        }
636
637        // Get status as percentage first
638        CharSequence status = getText(R.string.crypt_keeper_setup_description);
639        int percent = 0;
640        try {
641            // Force a 50% progress state when debugging the view.
642            percent = isDebugView() ? 50 : Integer.parseInt(state);
643        } catch (Exception e) {
644            Log.w(TAG, "Error parsing progress: " + e.toString());
645        }
646        String progress = Integer.toString(percent);
647
648        // Now try to get status as time remaining and replace as appropriate
649        Log.v(TAG, "Encryption progress: " + progress);
650        try {
651            final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
652            int time = Integer.parseInt(timeProperty);
653            if (time >= 0) {
654                // Round up to multiple of 10 - this way display is less jerky
655                time = (time + 9) / 10 * 10;
656                progress = DateUtils.formatElapsedTime(time);
657                status = getText(R.string.crypt_keeper_setup_time_remaining);
658            }
659        } catch (Exception e) {
660            // Will happen if no time etc - show percentage
661        }
662
663        final TextView tv = (TextView) findViewById(R.id.status);
664        if (tv != null) {
665            tv.setText(TextUtils.expandTemplate(status, progress));
666        }
667
668        // Check the progress every 1 seconds
669        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
670        mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
671    }
672
673    /** Insist on a power cycle to force the user to waste time between retries.
674     *
675     * Call setBackFunctionality(false) before calling this. */
676    private void cooldown() {
677        // Disable the password entry.
678        if (mPasswordEntry != null) {
679            mPasswordEntry.setEnabled(false);
680        }
681        if (mLockPatternView != null) {
682            mLockPatternView.setEnabled(false);
683        }
684
685        final TextView status = (TextView) findViewById(R.id.status);
686        status.setText(R.string.crypt_keeper_force_power_cycle);
687    }
688
689    /**
690     * Sets the back status: enabled or disabled according to the parameter.
691     * @param isEnabled true if back is enabled, false otherwise.
692     */
693    private final void setBackFunctionality(boolean isEnabled) {
694        mIgnoreBack = !isEnabled;
695        if (isEnabled) {
696            mStatusBar.disable(sWidgetsToDisable);
697        } else {
698            mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
699        }
700    }
701
702    private void fakeUnlockAttempt(View postingView) {
703        beginAttempt();
704        postingView.postDelayed(mFakeUnlockAttemptRunnable, FAKE_ATTEMPT_DELAY);
705    }
706
707    protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
708        new LockPatternView.OnPatternListener() {
709
710        @Override
711        public void onPatternStart() {
712            mLockPatternView.removeCallbacks(mClearPatternRunnable);
713        }
714
715        @Override
716        public void onPatternCleared() {
717        }
718
719        @Override
720        public void onPatternDetected(List<LockPatternView.Cell> pattern) {
721            mLockPatternView.setEnabled(false);
722            if (pattern.size() >= MIN_LENGTH_BEFORE_REPORT) {
723                new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
724            } else {
725                // Allow user to make as many of these as they want.
726                fakeUnlockAttempt(mLockPatternView);
727            }
728        }
729
730        @Override
731        public void onPatternCellAdded(List<Cell> pattern) {
732        }
733     };
734
735     private void passwordEntryInit() {
736        // Password/pin case
737        mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
738        if (mPasswordEntry != null){
739            mPasswordEntry.setOnEditorActionListener(this);
740            mPasswordEntry.requestFocus();
741            // Become quiet when the user interacts with the Edit text screen.
742            mPasswordEntry.setOnKeyListener(this);
743            mPasswordEntry.setOnTouchListener(this);
744            mPasswordEntry.addTextChangedListener(this);
745        }
746
747        // Pattern case
748        mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
749        if (mLockPatternView != null) {
750            mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
751        }
752
753        // Disable the Emergency call button if the device has no voice telephone capability
754        if (!getTelephonyManager().isVoiceCapable()) {
755            final View emergencyCall = findViewById(R.id.emergencyCallButton);
756            if (emergencyCall != null) {
757                Log.d(TAG, "Removing the emergency Call button");
758                emergencyCall.setVisibility(View.GONE);
759            }
760        }
761
762        final View imeSwitcher = findViewById(R.id.switch_ime_button);
763        final InputMethodManager imm = (InputMethodManager) getSystemService(
764                Context.INPUT_METHOD_SERVICE);
765        if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
766            imeSwitcher.setVisibility(View.VISIBLE);
767            imeSwitcher.setOnClickListener(new OnClickListener() {
768                    @Override
769                public void onClick(View v) {
770                    imm.showInputMethodPicker();
771                }
772            });
773        }
774
775        // We want to keep the screen on while waiting for input. In minimal boot mode, the device
776        // is completely non-functional, and we want the user to notice the device and enter a
777        // password.
778        if (mWakeLock == null) {
779            Log.d(TAG, "Acquiring wakelock.");
780            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
781            if (pm != null) {
782                mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
783                mWakeLock.acquire();
784                // Keep awake for 10 minutes - if the user hasn't been alerted by then
785                // best not to just drain their battery
786                mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
787            }
788        }
789
790        // Asynchronously throw up the IME, since there are issues with requesting it to be shown
791        // immediately.
792        if (mLockPatternView == null && !mCooldown) {
793            mHandler.postDelayed(new Runnable() {
794                @Override public void run() {
795                    imm.showSoftInputUnchecked(0, null);
796                }
797            }, 0);
798        }
799
800        updateEmergencyCallButtonState();
801        // Notify the user in 120 seconds that we are waiting for him to enter the password.
802        mHandler.removeMessages(MESSAGE_NOTIFY);
803        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
804
805        // Dismiss secure & non-secure keyguards while this screen is showing.
806        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
807                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
808    }
809
810    /**
811     * Method adapted from com.android.inputmethod.latin.Utils
812     *
813     * @param imm The input method manager
814     * @param shouldIncludeAuxiliarySubtypes
815     * @return true if we have multiple IMEs to choose from
816     */
817    private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
818            final boolean shouldIncludeAuxiliarySubtypes) {
819        final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
820
821        // Number of the filtered IMEs
822        int filteredImisCount = 0;
823
824        for (InputMethodInfo imi : enabledImis) {
825            // We can return true immediately after we find two or more filtered IMEs.
826            if (filteredImisCount > 1) return true;
827            final List<InputMethodSubtype> subtypes =
828                    imm.getEnabledInputMethodSubtypeList(imi, true);
829            // IMEs that have no subtypes should be counted.
830            if (subtypes.isEmpty()) {
831                ++filteredImisCount;
832                continue;
833            }
834
835            int auxCount = 0;
836            for (InputMethodSubtype subtype : subtypes) {
837                if (subtype.isAuxiliary()) {
838                    ++auxCount;
839                }
840            }
841            final int nonAuxCount = subtypes.size() - auxCount;
842
843            // IMEs that have one or more non-auxiliary subtypes should be counted.
844            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
845            // subtypes should be counted as well.
846            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
847                ++filteredImisCount;
848                continue;
849            }
850        }
851
852        return filteredImisCount > 1
853        // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
854        // input method subtype (The current IME should be LatinIME.)
855                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
856    }
857
858    private IMountService getMountService() {
859        final IBinder service = ServiceManager.getService("mount");
860        if (service != null) {
861            return IMountService.Stub.asInterface(service);
862        }
863        return null;
864    }
865
866    @Override
867    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
868        if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
869            // Get the password
870            final String password = v.getText().toString();
871
872            if (TextUtils.isEmpty(password)) {
873                return true;
874            }
875
876            // Now that we have the password clear the password field.
877            v.setText(null);
878
879            // Disable the password entry and back keypress while checking the password. These
880            // we either be re-enabled if the password was wrong or after the cooldown period.
881            mPasswordEntry.setEnabled(false);
882            setBackFunctionality(false);
883
884            if (password.length() >= LockPatternUtils.MIN_LOCK_PATTERN_SIZE) {
885                new DecryptTask().execute(password);
886            } else {
887                // Allow user to make as many of these as they want.
888                fakeUnlockAttempt(mPasswordEntry);
889            }
890
891            return true;
892        }
893        return false;
894    }
895
896    /**
897     * Set airplane mode on the device if it isn't an LTE device.
898     * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
899     * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
900     * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
901     * both these problems, we turn the radio off. However, on certain networks turning on and
902     * off the radio takes a long time. In such cases, we are better off leaving the radio
903     * running so the latency of an E911 call is short.
904     * The behavior after this is:
905     * 1. Emergency dialing: the emergency dialer has logic to force the device out of
906     *    airplane mode and restart the radio.
907     * 2. Full boot: we read the persistent settings from the previous boot and restore the
908     *    radio to whatever it was before it restarted. This also happens when rebooting a
909     *    phone that has no encryption.
910     */
911    private final void setAirplaneModeIfNecessary() {
912        final boolean isLteDevice =
913                getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
914        if (!isLteDevice) {
915            Log.d(TAG, "Going into airplane mode.");
916            Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
917            final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
918            intent.putExtra("state", true);
919            sendBroadcastAsUser(intent, UserHandle.ALL);
920        }
921    }
922
923    /**
924     * Code to update the state of, and handle clicks from, the "Emergency call" button.
925     *
926     * This code is mostly duplicated from the corresponding code in
927     * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
928     */
929    private void updateEmergencyCallButtonState() {
930        final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
931        // The button isn't present at all in some configurations.
932        if (emergencyCall == null)
933            return;
934
935        if (isEmergencyCallCapable()) {
936            emergencyCall.setVisibility(View.VISIBLE);
937            emergencyCall.setOnClickListener(new View.OnClickListener() {
938                    @Override
939
940                    public void onClick(View v) {
941                        takeEmergencyCallAction();
942                    }
943                });
944        } else {
945            emergencyCall.setVisibility(View.GONE);
946            return;
947        }
948
949        int textId;
950        if (getTelecomManager().isInCall()) {
951            // Show "return to call"
952            textId = R.string.cryptkeeper_return_to_call;
953        } else {
954            textId = R.string.cryptkeeper_emergency_call;
955        }
956        emergencyCall.setText(textId);
957    }
958
959    private boolean isEmergencyCallCapable() {
960        return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
961    }
962
963    private void takeEmergencyCallAction() {
964        TelecomManager telecomManager = getTelecomManager();
965        if (telecomManager.isInCall()) {
966            telecomManager.showInCallScreen(false /* showDialpad */);
967        } else {
968            launchEmergencyDialer();
969        }
970    }
971
972
973    private void launchEmergencyDialer() {
974        final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
975        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
976                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
977        setBackFunctionality(true);
978        startActivity(intent);
979    }
980
981    private TelephonyManager getTelephonyManager() {
982        return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
983    }
984
985    private TelecomManager getTelecomManager() {
986        return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
987    }
988
989    /**
990     * Listen to key events so we can disable sounds when we get a keyinput in EditText.
991     */
992    private void delayAudioNotification() {
993        mNotificationCountdown = 20;
994    }
995
996    @Override
997    public boolean onKey(View v, int keyCode, KeyEvent event) {
998        delayAudioNotification();
999        return false;
1000    }
1001
1002    @Override
1003    public boolean onTouch(View v, MotionEvent event) {
1004        delayAudioNotification();
1005        return false;
1006    }
1007
1008    @Override
1009    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
1010        return;
1011    }
1012
1013    @Override
1014    public void onTextChanged(CharSequence s, int start, int before, int count) {
1015        delayAudioNotification();
1016    }
1017
1018    @Override
1019    public void afterTextChanged(Editable s) {
1020        return;
1021    }
1022
1023    private static void disableCryptKeeperComponent(Context context) {
1024        PackageManager pm = context.getPackageManager();
1025        ComponentName name = new ComponentName(context, CryptKeeper.class);
1026        Log.d(TAG, "Disabling component " + name);
1027        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
1028                PackageManager.DONT_KILL_APP);
1029    }
1030
1031    public static class UserInitBroadcastReceiver extends BroadcastReceiver {
1032
1033        @Override
1034        public void onReceive(Context context, Intent intent) {
1035            final String intentAction = intent.getAction();
1036            // Disable CryptKeeper activity if user is not primary
1037            if (Intent.ACTION_USER_INITIALIZE.equals(intentAction)
1038                    && UserHandle.USER_OWNER != UserHandle.myUserId()) {
1039                disableCryptKeeperComponent(context);
1040            }
1041        }
1042    }
1043}
1044