CryptKeeper.java revision 0f11e15f503d65d5a085157d8ec406a72d712ce7
18fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks/*
28fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Copyright (C) 2011 The Android Open Source Project
38fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *
48fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Licensed under the Apache License, Version 2.0 (the "License");
58fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * you may not use this file except in compliance with the License.
68fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * You may obtain a copy of the License at
78fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *
88fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *      http://www.apache.org/licenses/LICENSE-2.0
98fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *
108fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Unless required by applicable law or agreed to in writing, software
118fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * distributed under the License is distributed on an "AS IS" BASIS,
128fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * See the License for the specific language governing permissions and
148fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * limitations under the License.
158fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks */
168fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
178fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parkspackage com.android.settings;
188fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
198fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.app.Activity;
208fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.app.StatusBarManager;
218fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.ComponentName;
228fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.Context;
23ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.content.Intent;
247ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrenceimport android.content.pm.ActivityInfo;
258fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.pm.PackageManager;
267ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrenceimport android.content.res.Resources.NotFoundException;
27de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwalimport android.media.AudioManager;
2806c5ff4a46686d3f599e40df0768177d3d88913cJason parksimport android.os.AsyncTask;
298fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.Bundle;
30ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Handler;
318fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.IBinder;
32ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Message;
3335933812b16f0b4221280bbcc3c32f6540996bf2Jason parksimport android.os.PowerManager;
348373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.os.RemoteException;
358fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.ServiceManager;
368fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.SystemProperties;
376a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tateimport android.os.UserHandle;
388fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.storage.IMountService;
39b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport android.os.storage.StorageManager;
4035230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon
4135230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordonimport android.phone.PhoneManager;
426ebbd30e18ca514ca9ee87ffa428c55c9fd92a8cVikram Aggarwalimport android.provider.Settings;
438373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.telephony.TelephonyManager;
44d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.Editable;
45ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.text.TextUtils;
46d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.TextWatcher;
47b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrenceimport android.text.format.DateUtils;
488fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.util.Log;
498fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.KeyEvent;
50d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.MotionEvent;
5113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View;
52fb3d5cad48876dbd366fd8b562529d355d2428dbJim Millerimport android.view.WindowManager;
5313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View.OnClickListener;
54d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnKeyListener;
55d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnTouchListener;
568fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.inputmethod.EditorInfo;
579fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodInfo;
5875c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parksimport android.view.inputmethod.InputMethodManager;
599fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodSubtype;
6013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.widget.Button;
61ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.EditText;
62ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.ProgressBar;
638fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.widget.TextView;
648fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
65bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwalimport com.android.internal.statusbar.StatusBarIcon;
66ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwalimport com.android.internal.telephony.Phone;
675543404ae0ec9635b59d37fa01074ea598d539ebWink Savilleimport com.android.internal.telephony.PhoneConstants;
68b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport com.android.internal.widget.LockPatternUtils;
69b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport com.android.internal.widget.LockPatternView;
70b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport com.android.internal.widget.LockPatternView.Cell;
719fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
72f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrenceimport static com.android.internal.widget.LockPatternView.DisplayMode;
73f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence
749fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport java.util.List;
7591a2f0566afb91549cbda9289b516154a6467624Ben Komalo
7691a2f0566afb91549cbda9289b516154a6467624Ben Komalo/**
7791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * Settings screens to show the UI flows for encrypting/decrypting the device.
7891a2f0566afb91549cbda9289b516154a6467624Ben Komalo *
7991a2f0566afb91549cbda9289b516154a6467624Ben Komalo * This may be started via adb for debugging the UI layout, without having to go through
8091a2f0566afb91549cbda9289b516154a6467624Ben Komalo * encryption flows everytime. It should be noted that starting the activity in this manner
8191a2f0566afb91549cbda9289b516154a6467624Ben Komalo * is only useful for verifying UI-correctness - the behavior will not be identical.
8291a2f0566afb91549cbda9289b516154a6467624Ben Komalo * <pre>
8391a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell pm enable com.android.settings/.CryptKeeper
8491a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell am start \
8591a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
8691a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -n com.android.settings/.CryptKeeper
8791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * </pre>
8891a2f0566afb91549cbda9289b516154a6467624Ben Komalo */
89d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalpublic class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
90d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        OnKeyListener, OnTouchListener, TextWatcher {
91ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final String TAG = "CryptKeeper";
9235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
938fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private static final String DECRYPT_STATE = "trigger_restart_framework";
94de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate encryption update progress. */
95de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_UPDATE_PROGRESS = 1;
96de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to cool-down (waste user's time between password attempts) */
97de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_COOLDOWN = 2;
98de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate alerting the user that we are waiting for password entry */
99de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_NOTIFY = 3;
100de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
101de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    // Constants used to control policy.
102ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int MAX_FAILED_ATTEMPTS = 30;
103ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_ATTEMPTS = 10;
104ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
105ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks
1068373b451692a39abdfd12444241c016831e1a4a4David Brown    // Intent action for launching the Emergency Dialer activity.
1078373b451692a39abdfd12444241c016831e1a4a4David Brown    static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
1088373b451692a39abdfd12444241c016831e1a4a4David Brown
10991a2f0566afb91549cbda9289b516154a6467624Ben Komalo    // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
11091a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String EXTRA_FORCE_VIEW =
11191a2f0566afb91549cbda9289b516154a6467624Ben Komalo            "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
11291a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String FORCE_VIEW_PROGRESS = "progress";
11391a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String FORCE_VIEW_ERROR = "error";
114de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final String FORCE_VIEW_PASSWORD = "password";
11591a2f0566afb91549cbda9289b516154a6467624Ben Komalo
116bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
1170e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mValidationComplete;
118d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo    private boolean mValidationRequested;
1190e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
1200e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mEncryptionGoneBad;
121de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** A flag to indicate when the back event should be ignored */
122de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private boolean mIgnoreBack = false;
1231499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private int mCooldown;
1241499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    PowerManager.WakeLock mWakeLock;
12506c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private EditText mPasswordEntry;
126b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    private LockPatternView mLockPatternView;
127d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
128d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private int mNotificationCountdown = 0;
12973456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence    /** Number of calls to {@link #notifyUser()} before we release the wakelock */
13073456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence    private int mReleaseWakeLockCountdown = 0;
1315a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence    private int mStatusString = R.string.enter_password;
1321499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
133f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    // how long we wait to clear a wrong pattern
134f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
135f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence
1360f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence    // how long we wait to clear a right pattern
1370f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence    private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
1380f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence
139f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    private Runnable mClearPatternRunnable = new Runnable() {
140f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void run() {
141f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.clearPattern();
142f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
143f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    };
144f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence
1451499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
1461499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Used to propagate state through configuration changes (e.g. screen rotation)
1471499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
1481499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private static class NonConfigurationInstanceState {
1491499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        final PowerManager.WakeLock wakelock;
1501499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1511499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
1521499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            wakelock = _wakelock;
1531499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
1541499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
1551499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
15606c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private class DecryptTask extends AsyncTask<String, Void, Integer> {
15706c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
15806c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected Integer doInBackground(String... params) {
159de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
16006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            try {
16106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return service.decryptStorage(params[0]);
16206c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } catch (Exception e) {
16306c5ff4a46686d3f599e40df0768177d3d88913cJason parks                Log.e(TAG, "Error while decrypting...", e);
16406c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return -1;
16506c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
16606c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
16706c5ff4a46686d3f599e40df0768177d3d88913cJason parks
16806c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
16906c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected void onPostExecute(Integer failedAttempts) {
17006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            if (failedAttempts == 0) {
17189c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence                // The password was entered successfully. Simply do nothing
17289c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence                // and wait for the service restart to switch to surfacefligner
1730f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                if (mLockPatternView != null) {
1740f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    mLockPatternView.removeCallbacks(mClearPatternRunnable);
1750f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
1760f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                }
17706c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
17806c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Factory reset the device.
17906c5ff4a46686d3f599e40df0768177d3d88913cJason parks                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
180384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            } else if (failedAttempts == -1) {
181384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                // Right password, but decryption failed. Tell user bad news ...
182384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                setContentView(R.layout.crypt_keeper_progress);
183384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                showFactoryReset(true);
184384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                return;
18506c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else {
186f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                // Wrong entry. Handle pattern case.
1872daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                if (mLockPatternView != null) {
188f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mLockPatternView.setDisplayMode(DisplayMode.Wrong);
189f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mLockPatternView.removeCallbacks(mClearPatternRunnable);
190f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
191f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                }
192f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
193f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mCooldown = COOL_DOWN_INTERVAL;
194f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    cooldown();
195f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                } else {
196f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    final TextView status = (TextView) findViewById(R.id.status);
197bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence
198bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
199bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
200bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
201bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        CharSequence warning = TextUtils.expandTemplate(warningTemplate,
202bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                                                                        Integer.toString(remainingAttempts));
203bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        status.setText(warning);
204bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    } else {
205bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        status.setText(R.string.try_again);
206bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    }
207bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence
208f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    if (mLockPatternView != null) {
209f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                        mLockPatternView.setDisplayMode(DisplayMode.Wrong);
210f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    }
211f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    // Reenable the password entry
212f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    if (mPasswordEntry != null) {
213f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                        mPasswordEntry.setEnabled(true);
2145a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        final InputMethodManager imm = (InputMethodManager) getSystemService(
2155a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                                                  Context.INPUT_METHOD_SERVICE);
2165a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        imm.showSoftInput(mPasswordEntry, 0);
21708c6ab0feaf459029d470b5c7a50fb55be763365Paul Lawrence                        setBackFunctionality(true);
218f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    }
219f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    if (mLockPatternView != null) {
220f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                        mLockPatternView.setEnabled(true);
221f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    }
2222daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                }
22306c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
22406c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
22506c5ff4a46686d3f599e40df0768177d3d88913cJason parks    }
22675c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parks
2270e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
2280e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2290e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected Boolean doInBackground(Void... params) {
230de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
2310e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            try {
232d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Validating encryption state.");
2330e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                int state = service.getEncryptionState();
2340e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                if (state == IMountService.ENCRYPTION_STATE_NONE) {
2350e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
2360e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    return true; // Unexpected, but fine, I guess...
2370e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                }
2380e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return state == IMountService.ENCRYPTION_STATE_OK;
2390e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            } catch (RemoteException e) {
2400e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Unable to get encryption state properly");
2410e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return true;
2420e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2430e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2440e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
2450e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2460e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected void onPostExecute(Boolean result) {
2470e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            mValidationComplete = true;
2480e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            if (Boolean.FALSE.equals(result)) {
2490e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
2500e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                mEncryptionGoneBad = true;
251d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            } else {
252d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
2530e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2540e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setupUi();
2550e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2560e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
2570e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
25891a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private final Handler mHandler = new Handler() {
259ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        @Override
260ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        public void handleMessage(Message msg) {
261ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            switch (msg.what) {
262de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_UPDATE_PROGRESS:
263f8217304073b5cd13823a0602568394be93bf310Jason parks                updateProgress();
264ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
26535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
266de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_COOLDOWN:
267f8217304073b5cd13823a0602568394be93bf310Jason parks                cooldown();
268ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
269de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
270de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_NOTIFY:
271de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                notifyUser();
272de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                break;
273ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
274ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        }
275ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    };
27635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
277de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private AudioManager mAudioManager;
278bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** The status bar where back/home/recent buttons are shown. */
279bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private StatusBarManager mStatusBar;
280bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
281bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** All the widgets to disable in the status bar */
282bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
283bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ICONS
284bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
285bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_SYSTEM_INFO
286bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_HOME
28766d050bb4b6bf899e363f3e67d1eb5774de744b5Alon Albert            | StatusBarManager.DISABLE_SEARCH
288bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_RECENT;
289de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
29091a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the UI only. */
29191a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView() {
29291a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return getIntent().hasExtra(EXTRA_FORCE_VIEW);
29391a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
29491a2f0566afb91549cbda9289b516154a6467624Ben Komalo
29591a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the specific UI view only. */
29691a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView(String viewType /* non-nullable */) {
29791a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
29891a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
29991a2f0566afb91549cbda9289b516154a6467624Ben Komalo
300de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
301de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Notify the user that we are awaiting input. Currently this sends an audio alert.
302de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
303de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private void notifyUser() {
304d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        if (mNotificationCountdown > 0) {
305d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal            --mNotificationCountdown;
306d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        } else if (mAudioManager != null) {
307de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            try {
308de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // Play the standard keypress sound at full volume. This should be available on
309de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // every device. We cannot play a ringtone here because media services aren't
310de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
311de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // on tablet devices. The idea is to alert the user that something is needed: this
312de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // does not have to be pleasing.
313de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
314de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            } catch (Exception e) {
315de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
316de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
317de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
31886b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        // Notify the user again in 5 seconds.
319de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
32086b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
32173456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence
32273456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence        if (mWakeLock.isHeld()) {
32373456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            if (mReleaseWakeLockCountdown > 0) {
32473456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                --mReleaseWakeLockCountdown;
32573456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            } else {
32673456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                mWakeLock.release();
32773456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            }
32873456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence        }
329de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
330de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
331de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
332de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Ignore back events after the user has entered the decrypt screen and while the device is
333de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * encrypting.
334de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
335de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    @Override
336de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    public void onBackPressed() {
337bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        // In the rare case that something pressed back even though we were disabled.
338de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mIgnoreBack)
339de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            return;
340de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        super.onBackPressed();
341de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
342de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
3438fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
3448fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public void onCreate(Bundle savedInstanceState) {
3458fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        super.onCreate(savedInstanceState);
34635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3479597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        // If we are not encrypted or encrypting, get out quickly.
348de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String state = SystemProperties.get("vold.decrypt");
34991a2f0566afb91549cbda9289b516154a6467624Ben Komalo        if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
35035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks            // Disable the crypt keeper.
3518fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            PackageManager pm = getPackageManager();
3528fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            ComponentName name = new ComponentName(this, CryptKeeper.class);
353140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
354140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn                    PackageManager.DONT_KILL_APP);
355140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // Typically CryptKeeper is launched as the home app.  We didn't
356644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // want to be running, so need to finish this activity.  We can count
357644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // on the activity manager re-launching the new home app upon finishing
358644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // this one, since this will leave the activity stack empty.
359140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // NOTE: This is really grungy.  I think it would be better for the
360140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // activity manager to explicitly launch the crypt keeper instead of
361140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // home in the situation where we need to decrypt the device
362140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            finish();
3638fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return;
3648fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
36535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3667ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        try {
3677ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence            if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
3687ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3697ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence            }
3707ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        } catch (NotFoundException e) {
3717ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        }
3727ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence
373b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // Disable the status bar, but do NOT disable back because the user needs a way to go
374b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // from keyboard settings and back to the password screen.
375bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
376bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar.disable(sWidgetsToDisable);
3771499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
378ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        setAirplaneModeIfNecessary();
379de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
3801499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        // Check for (and recover) retained instance data
381de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final Object lastInstance = getLastNonConfigurationInstance();
3821499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        if (lastInstance instanceof NonConfigurationInstanceState) {
3831499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
3841499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            mWakeLock = retained.wakelock;
3850460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
3861499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
387ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
38835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3899597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    /**
3909597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * Note, we defer the state check and screen setup to onStart() because this will be
3919597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * re-run if the user clicks the power button (sleeping/waking the screen), and this is
3929597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * especially important if we were to lose the wakelock for any reason.
3939597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     */
3949597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    @Override
3959597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    public void onStart() {
3969597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        super.onStart();
397d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        setupUi();
3980e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
3990e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
4000e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /**
4010e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * Initializes the UI based on the current state of encryption.
4020e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * This is idempotent - calling repeatedly will simply re-initialize the UI.
4030e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     */
4040e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private void setupUi() {
4050e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
4060e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setContentView(R.layout.crypt_keeper_progress);
407384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            showFactoryReset(false);
4080e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            return;
4090e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
4100e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
411de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String progress = SystemProperties.get("vold.encrypt_progress");
4120e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
4139597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            setContentView(R.layout.crypt_keeper_progress);
4149597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            encryptionProgressInit();
415de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
4169ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence            new AsyncTask<Void, Void, Void>() {
4179ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                int type = StorageManager.CRYPT_TYPE_PASSWORD;
4189ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                String owner_info;
4190f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                boolean pattern_visible;
4209ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4219ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                @Override
4229ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                public Void doInBackground(Void... v) {
4239ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    try {
4249ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        final IMountService service = getMountService();
4259ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        type = service.getPasswordType();
4269ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        owner_info = service.getField("OwnerInfo");
4270f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                        pattern_visible = !("0".equals(service.getField("PatternVisible")));
4289ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } catch (Exception e) {
4299ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        Log.e(TAG, "Error calling mount service " + e);
4309ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    }
4319ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4329ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    return null;
4339ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                }
434b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
4359ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                @Override
4369ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                public void onPostExecute(java.lang.Void v) {
4379ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    if(type == StorageManager.CRYPT_TYPE_PIN) {
4389ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_pin_entry);
4395a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_pin;
4409ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
4419ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_pattern_entry);
4429ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setBackFunctionality(false);
4435a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_pattern;
4449ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } else {
4459ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_password_entry);
4465a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_password;
4479ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    }
4485a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    final TextView status = (TextView) findViewById(R.id.status);
4495a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    status.setText(mStatusString);
4505a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
4515a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
4525a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    ownerInfo.setText(owner_info);
4535a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    ownerInfo.setSelected(true); // Required for marquee'ing to work
4549ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4559ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    passwordEntryInit();
4565a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
4570f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    if (mLockPatternView != null) {
4580f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                        mLockPatternView.setInStealthMode(!pattern_visible);
4590f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    }
4600f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence
4615a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    if (mCooldown > 0) {
4625a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        setBackFunctionality(false);
4635a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        cooldown(); // in case we are cooling down and coming back from emergency dialler
4645a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    }
4659ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                }
4669ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence            }.execute();
467d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        } else if (!mValidationRequested) {
468d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            // We're supposed to be encrypted, but no validation has been done.
469d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            new ValidationTask().execute((Void[]) null);
470d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            mValidationRequested = true;
4719597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        }
4729597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    }
4739597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler
474f8217304073b5cd13823a0602568394be93bf310Jason parks    @Override
475f8217304073b5cd13823a0602568394be93bf310Jason parks    public void onStop() {
476f8217304073b5cd13823a0602568394be93bf310Jason parks        super.onStop();
477de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_COOLDOWN);
478de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
479de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
4801499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
4811499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
4821499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
4831499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Reconfiguring, so propagate the wakelock to the next instance.  This runs between onStop()
4841499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * and onDestroy() and only if we are changing configuration (e.g. rotation).  Also clears
4851499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * mWakeLock so the subsequent call to onDestroy does not release it.
4861499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
4871499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
4881499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public Object onRetainNonConfigurationInstance() {
4891499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
4900460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
4911499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        mWakeLock = null;
4921499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        return state;
4931499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
4941499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
4951499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
4961499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public void onDestroy() {
4971499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        super.onDestroy();
498f8217304073b5cd13823a0602568394be93bf310Jason parks
499f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mWakeLock != null) {
5000460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Releasing and destroying wakelock");
501f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock.release();
502f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock = null;
503f8217304073b5cd13823a0602568394be93bf310Jason parks        }
504f8217304073b5cd13823a0602568394be93bf310Jason parks    }
505f8217304073b5cd13823a0602568394be93bf310Jason parks
506de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
507de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Start encrypting the device.
508de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
509ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private void encryptionProgressInit() {
51035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // Accquire a partial wakelock to prevent the device from sleeping. Note
51135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // we never release this wakelock as we will be restarted after the device
51235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // is encrypted.
5130460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Encryption progress screen initializing.");
5149ee164f44ea1ad66fa16ed702eb885e36ade0d8bBen Komalo        if (mWakeLock == null) {
5150460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Acquiring wakelock.");
5160460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
5170460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
5180460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock.acquire();
5190460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        }
520f8217304073b5cd13823a0602568394be93bf310Jason parks
521bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
522de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Ignore all back presses from now, both hard and soft keys.
523bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        setBackFunctionality(false);
524de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Start the first run of progress manually. This method sets up messages to occur at
525de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // repeated intervals.
526f8217304073b5cd13823a0602568394be93bf310Jason parks        updateProgress();
527f8217304073b5cd13823a0602568394be93bf310Jason parks    }
528f8217304073b5cd13823a0602568394be93bf310Jason parks
529384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence    /**
530384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     * Show factory reset screen allowing the user to reset their phone when
531384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     * there is nothing else we can do
532384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     * @param corrupt true if userdata is corrupt, false if encryption failed
533384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     *        partway through
534384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     */
535384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence    private void showFactoryReset(boolean corrupt) {
53613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Hide the encryption-bot to make room for the "factory reset" button
53713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        findViewById(R.id.encroid).setVisibility(View.GONE);
53813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
53913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Show the reset button, failure text, and a divider
540bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button button = (Button) findViewById(R.id.factory_reset);
54113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setVisibility(View.VISIBLE);
54213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setOnClickListener(new OnClickListener() {
543bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                @Override
54413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            public void onClick(View v) {
54513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                // Factory reset the device.
54613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
54713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            }
54813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        });
54913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
550bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // Alert the user of the failure.
551384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence        if (corrupt) {
552384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
553384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
554384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence        } else {
555384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
556384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
557384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence        }
55813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
559bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View view = findViewById(R.id.bottom_divider);
560bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
561f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        if (view != null) {
562f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo            view.setVisibility(View.VISIBLE);
563f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        }
56413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    }
56513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
566f8217304073b5cd13823a0602568394be93bf310Jason parks    private void updateProgress() {
567bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final String state = SystemProperties.get("vold.encrypt_progress");
568f8217304073b5cd13823a0602568394be93bf310Jason parks
5690e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if ("error_partially_encrypted".equals(state)) {
570384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            showFactoryReset(false);
57113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            return;
57213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
57313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
574b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Get status as percentage first
575b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        CharSequence status = getText(R.string.crypt_keeper_setup_description);
576b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        int percent = 0;
577f8217304073b5cd13823a0602568394be93bf310Jason parks        try {
57891a2f0566afb91549cbda9289b516154a6467624Ben Komalo            // Force a 50% progress state when debugging the view.
579b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            percent = isDebugView() ? 50 : Integer.parseInt(state);
580f8217304073b5cd13823a0602568394be93bf310Jason parks        } catch (Exception e) {
581f8217304073b5cd13823a0602568394be93bf310Jason parks            Log.w(TAG, "Error parsing progress: " + e.toString());
582f8217304073b5cd13823a0602568394be93bf310Jason parks        }
583b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        String progress = Integer.toString(percent);
58435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
585b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Now try to get status as time remaining and replace as appropriate
5860460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.v(TAG, "Encryption progress: " + progress);
587b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        try {
588b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
589b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            int time = Integer.parseInt(timeProperty);
590b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            if (time >= 0) {
591b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                // Round up to multiple of 10 - this way display is less jerky
592b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                time = (time + 9) / 10 * 10;
593b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                progress = DateUtils.formatElapsedTime(time);
594b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                status = getText(R.string.crypt_keeper_setup_time_remaining);
595b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            }
596b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        } catch (Exception e) {
597b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            // Will happen if no time etc - show percentage
598b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        }
599b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence
600bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView tv = (TextView) findViewById(R.id.status);
601de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (tv != null) {
602b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            tv.setText(TextUtils.expandTemplate(status, progress));
603de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
604b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence
605b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Check the progress every 1 seconds
606de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
607b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
608f8217304073b5cd13823a0602568394be93bf310Jason parks    }
609f8217304073b5cd13823a0602568394be93bf310Jason parks
610de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Disable password input for a while to force the user to waste time between retries */
611f8217304073b5cd13823a0602568394be93bf310Jason parks    private void cooldown() {
612bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView status = (TextView) findViewById(R.id.status);
61313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
614f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mCooldown <= 0) {
615de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Re-enable the password entry and back presses.
6162daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            if (mPasswordEntry != null) {
6172daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                mPasswordEntry.setEnabled(true);
6185a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                final InputMethodManager imm = (InputMethodManager) getSystemService(
6195a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                                          Context.INPUT_METHOD_SERVICE);
6205a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                imm.showSoftInput(mPasswordEntry, 0);
6212daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                setBackFunctionality(true);
6222daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            }
6232daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            if (mLockPatternView != null) {
6242daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                mLockPatternView.setEnabled(true);
6252daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            }
6265a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            status.setText(mStatusString);
627f8217304073b5cd13823a0602568394be93bf310Jason parks        } else {
6285a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            // Disable the password entry and back presses.
6295a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            if (mPasswordEntry != null) {
6305a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mPasswordEntry.setEnabled(false);
6315a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            }
6325a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            if (mLockPatternView != null) {
6335a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mLockPatternView.setEnabled(false);
6345a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            }
6355a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
63613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            CharSequence template = getText(R.string.crypt_keeper_cooldown);
637bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
63813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
639f8217304073b5cd13823a0602568394be93bf310Jason parks            mCooldown--;
640de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.removeMessages(MESSAGE_COOLDOWN);
641de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
642f8217304073b5cd13823a0602568394be93bf310Jason parks        }
643ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
64435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
645bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /**
646bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * Sets the back status: enabled or disabled according to the parameter.
647bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * @param isEnabled true if back is enabled, false otherwise.
648bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     */
649bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private final void setBackFunctionality(boolean isEnabled) {
650bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mIgnoreBack = !isEnabled;
651bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        if (isEnabled) {
652bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable);
653bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        } else {
654bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
655bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        }
656bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    }
657bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
658b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
659f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        new LockPatternView.OnPatternListener() {
660b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
661f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
662f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternStart() {
663f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.removeCallbacks(mClearPatternRunnable);
664f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
665b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
666f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
667f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternCleared() {
668f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
669b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
670f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
671f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternDetected(List<LockPatternView.Cell> pattern) {
672f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.setEnabled(false);
673f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
674f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
675b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
676f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
677f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternCellAdded(List<Cell> pattern) {
678f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
679b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     };
680b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
681b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     private void passwordEntryInit() {
682b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Password/pin case
68306c5ff4a46686d3f599e40df0768177d3d88913cJason parks        mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
684b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mPasswordEntry != null){
685b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnEditorActionListener(this);
686b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.requestFocus();
687b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            // Become quiet when the user interacts with the Edit text screen.
688b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnKeyListener(this);
689b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnTouchListener(this);
690b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.addTextChangedListener(this);
691b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
692b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
693b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Pattern case
694b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
695b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mLockPatternView != null) {
696b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
697b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
6989fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
699c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        // Disable the Emergency call button if the device has no voice telephone capability
7003afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon        if (!getTelephonyManager().isVoiceCapable()) {
701c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            final View emergencyCall = findViewById(R.id.emergencyCallButton);
702c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            if (emergencyCall != null) {
703c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                Log.d(TAG, "Removing the emergency Call button");
704c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                emergencyCall.setVisibility(View.GONE);
705c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            }
706c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        }
707c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal
708bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View imeSwitcher = findViewById(R.id.switch_ime_button);
7099fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final InputMethodManager imm = (InputMethodManager) getSystemService(
7109fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                Context.INPUT_METHOD_SERVICE);
7119fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
7129fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setVisibility(View.VISIBLE);
7139fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setOnClickListener(new OnClickListener() {
714bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
7159fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                public void onClick(View v) {
7169fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.showInputMethodPicker();
7179fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
7189fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            });
7199fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        }
7209fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
721de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // We want to keep the screen on while waiting for input. In minimal boot mode, the device
722de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // is completely non-functional, and we want the user to notice the device and enter a
723de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // password.
724de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mWakeLock == null) {
725de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            Log.d(TAG, "Acquiring wakelock.");
726de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
727de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            if (pm != null) {
728de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
729de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock.acquire();
73073456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                // Keep awake for 10 minutes - if the user hasn't been alerted by then
73173456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                // best not to just drain their battery
7325a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
733de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
734de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
73573456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence
7369fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Asynchronously throw up the IME, since there are issues with requesting it to be shown
7379fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // immediately.
7385a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence        if (mLockPatternView == null && mCooldown <= 0) {
739e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence            mHandler.postDelayed(new Runnable() {
740e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                @Override public void run() {
741e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                    imm.showSoftInputUnchecked(0, null);
742e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                }
743e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence            }, 0);
744e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence        }
7459fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7469fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        updateEmergencyCallButtonState();
747d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        // Notify the user in 120 seconds that we are waiting for him to enter the password.
74886b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
749d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
750fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller
751fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        // Dismiss keyguard while this screen is showing.
752fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
7539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    }
75435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
7559fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    /**
7569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * Method adapted from com.android.inputmethod.latin.Utils
7579fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     *
7589fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param imm The input method manager
7599fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param shouldIncludeAuxiliarySubtypes
7609fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @return true if we have multiple IMEs to choose from
7619fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     */
7629fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
7639fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final boolean shouldIncludeAuxiliarySubtypes) {
7649fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
7659fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7669fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Number of the filtered IMEs
7679fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        int filteredImisCount = 0;
7689fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7699fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        for (InputMethodInfo imi : enabledImis) {
7709fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // We can return true immediately after we find two or more filtered IMEs.
7719fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (filteredImisCount > 1) return true;
7729fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final List<InputMethodSubtype> subtypes =
7739fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.getEnabledInputMethodSubtypeList(imi, true);
7749fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have no subtypes should be counted.
7759fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (subtypes.isEmpty()) {
7769fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
7779fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
7789fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
77935933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
7809fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            int auxCount = 0;
7819fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            for (InputMethodSubtype subtype : subtypes) {
7829fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                if (subtype.isAuxiliary()) {
7839fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    ++auxCount;
7849fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
7859fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
7869fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final int nonAuxCount = subtypes.size() - auxCount;
7879fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7889fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have one or more non-auxiliary subtypes should be counted.
7899fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
7909fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // subtypes should be counted as well.
7919fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
7929fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
7939fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
7949fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
79500046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        }
7968373b451692a39abdfd12444241c016831e1a4a4David Brown
7979fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        return filteredImisCount > 1
7989fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
7999fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // input method subtype (The current IME should be LatinIME.)
8009fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
8018fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
8028fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
8038fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private IMountService getMountService() {
804bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final IBinder service = ServiceManager.getService("mount");
8058fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        if (service != null) {
8068fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return IMountService.Stub.asInterface(service);
8078fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
8088fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return null;
8098fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
8108fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
8118fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
8128fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
81300046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
8148fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Get the password
815bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final String password = v.getText().toString();
8168fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
817ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            if (TextUtils.isEmpty(password)) {
818ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                return true;
819ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
82035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8218fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Now that we have the password clear the password field.
8228fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            v.setText(null);
8238fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
824de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Disable the password entry and back keypress while checking the password. These
825de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // we either be re-enabled if the password was wrong or after the cooldown period.
82606c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(false);
827bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(false);
82806c5ff4a46686d3f599e40df0768177d3d88913cJason parks
8290460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Attempting to send command to decrypt");
83006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            new DecryptTask().execute(password);
83135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8328fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return true;
8338fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
8348fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return false;
8358fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
8368373b451692a39abdfd12444241c016831e1a4a4David Brown
837bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
838ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Set airplane mode on the device if it isn't an LTE device.
839ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
840ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
841ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
842ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * both these problems, we turn the radio off. However, on certain networks turning on and
843ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * off the radio takes a long time. In such cases, we are better off leaving the radio
844ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * running so the latency of an E911 call is short.
845ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * The behavior after this is:
846ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 1. Emergency dialing: the emergency dialer has logic to force the device out of
847ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    airplane mode and restart the radio.
848ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 2. Full boot: we read the persistent settings from the previous boot and restore the
849ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    radio to whatever it was before it restarted. This also happens when rebooting a
850ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    phone that has no encryption.
851ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     */
852ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    private final void setAirplaneModeIfNecessary() {
853ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        final boolean isLteDevice =
8543afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon                getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
855ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        if (!isLteDevice) {
856ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            Log.d(TAG, "Going into airplane mode.");
8576a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
858ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
859ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            intent.putExtra("state", true);
8606a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            sendBroadcastAsUser(intent, UserHandle.ALL);
861ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        }
862ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    }
863ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal
864ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    /**
865bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Code to update the state of, and handle clicks from, the "Emergency call" button.
866bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     *
867bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * This code is mostly duplicated from the corresponding code in
868bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
869bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
8708373b451692a39abdfd12444241c016831e1a4a4David Brown    private void updateEmergencyCallButtonState() {
871bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
8728373b451692a39abdfd12444241c016831e1a4a4David Brown        // The button isn't present at all in some configurations.
873bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        if (emergencyCall == null)
874bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            return;
8758373b451692a39abdfd12444241c016831e1a4a4David Brown
8768373b451692a39abdfd12444241c016831e1a4a4David Brown        if (isEmergencyCallCapable()) {
877bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.VISIBLE);
878bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setOnClickListener(new View.OnClickListener() {
879bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
880bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal
8818373b451692a39abdfd12444241c016831e1a4a4David Brown                    public void onClick(View v) {
8828373b451692a39abdfd12444241c016831e1a4a4David Brown                        takeEmergencyCallAction();
8838373b451692a39abdfd12444241c016831e1a4a4David Brown                    }
8848373b451692a39abdfd12444241c016831e1a4a4David Brown                });
8858373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
886bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.GONE);
8878373b451692a39abdfd12444241c016831e1a4a4David Brown            return;
8888373b451692a39abdfd12444241c016831e1a4a4David Brown        }
8898373b451692a39abdfd12444241c016831e1a4a4David Brown
8908373b451692a39abdfd12444241c016831e1a4a4David Brown        int textId;
89135230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        if (getPhoneManager().isInAPhoneCall()) {
89289c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence            // Show "return to call"
8938373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_return_to_call;
8948373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
8958373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_emergency_call;
8968373b451692a39abdfd12444241c016831e1a4a4David Brown        }
897bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        emergencyCall.setText(textId);
8988373b451692a39abdfd12444241c016831e1a4a4David Brown    }
8998373b451692a39abdfd12444241c016831e1a4a4David Brown
9008373b451692a39abdfd12444241c016831e1a4a4David Brown    private boolean isEmergencyCallCapable() {
9018373b451692a39abdfd12444241c016831e1a4a4David Brown        return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
9028373b451692a39abdfd12444241c016831e1a4a4David Brown    }
9038373b451692a39abdfd12444241c016831e1a4a4David Brown
9048373b451692a39abdfd12444241c016831e1a4a4David Brown    private void takeEmergencyCallAction() {
90535230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        PhoneManager phoneManager = getPhoneManager();
90635230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        if (phoneManager.isInAPhoneCall()) {
90735230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon            phoneManager.showCallScreen(false /* showDialpad */);
9088373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
9098373b451692a39abdfd12444241c016831e1a4a4David Brown            launchEmergencyDialer();
9108373b451692a39abdfd12444241c016831e1a4a4David Brown        }
9118373b451692a39abdfd12444241c016831e1a4a4David Brown    }
9128373b451692a39abdfd12444241c016831e1a4a4David Brown
9138373b451692a39abdfd12444241c016831e1a4a4David Brown
9148373b451692a39abdfd12444241c016831e1a4a4David Brown    private void launchEmergencyDialer() {
915bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
9168373b451692a39abdfd12444241c016831e1a4a4David Brown        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
9178373b451692a39abdfd12444241c016831e1a4a4David Brown                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
9185a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence        setBackFunctionality(true);
9198373b451692a39abdfd12444241c016831e1a4a4David Brown        startActivity(intent);
9208373b451692a39abdfd12444241c016831e1a4a4David Brown    }
921d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
9223afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon    private TelephonyManager getTelephonyManager() {
9233afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon        return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
9243afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon    }
9253afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon
92635230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon    private PhoneManager getPhoneManager() {
92735230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
92835230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon    }
92935230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon
930d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /**
931d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     * Listen to key events so we can disable sounds when we get a keyinput in EditText.
932d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     */
933d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private void delayAudioNotification() {
934d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mNotificationCountdown = 20;
935d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
936d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
937d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
938d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onKey(View v, int keyCode, KeyEvent event) {
939d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
940d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
941d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
942d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
943d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
944d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onTouch(View v, MotionEvent event) {
945d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
946d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
947d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
948d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
949d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
950d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
951d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
952d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
953d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
954d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
955d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void onTextChanged(CharSequence s, int start, int before, int count) {
956d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
957d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
958d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
959d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
960d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void afterTextChanged(Editable s) {
961d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
962d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
9638373b451692a39abdfd12444241c016831e1a4a4David Brown}
964