CryptKeeper.java revision bd4c3210e1e0d6f58b2a5857032a2dacb316be98
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
136f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    private Runnable mClearPatternRunnable = new Runnable() {
137f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void run() {
138f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.clearPattern();
139f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
140f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    };
141f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence
1421499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
1431499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Used to propagate state through configuration changes (e.g. screen rotation)
1441499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
1451499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private static class NonConfigurationInstanceState {
1461499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        final PowerManager.WakeLock wakelock;
1471499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1481499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
1491499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            wakelock = _wakelock;
1501499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
1511499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
1521499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
15306c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private class DecryptTask extends AsyncTask<String, Void, Integer> {
15406c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
15506c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected Integer doInBackground(String... params) {
156de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
15706c5ff4a46686d3f599e40df0768177d3d88913cJason parks            try {
15806c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return service.decryptStorage(params[0]);
15906c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } catch (Exception e) {
16006c5ff4a46686d3f599e40df0768177d3d88913cJason parks                Log.e(TAG, "Error while decrypting...", e);
16106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return -1;
16206c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
16306c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
16406c5ff4a46686d3f599e40df0768177d3d88913cJason parks
16506c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
16606c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected void onPostExecute(Integer failedAttempts) {
16706c5ff4a46686d3f599e40df0768177d3d88913cJason parks            if (failedAttempts == 0) {
16889c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence                // The password was entered successfully. Simply do nothing
16989c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence                // and wait for the service restart to switch to surfacefligner
17006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
17106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Factory reset the device.
17206c5ff4a46686d3f599e40df0768177d3d88913cJason parks                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
17306c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else {
174f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                // Wrong entry. Handle pattern case.
1752daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                if (mLockPatternView != null) {
176f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mLockPatternView.setDisplayMode(DisplayMode.Wrong);
177f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mLockPatternView.removeCallbacks(mClearPatternRunnable);
178f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
179f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                }
180f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
181f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    mCooldown = COOL_DOWN_INTERVAL;
182f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    cooldown();
183f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                } else {
184f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    final TextView status = (TextView) findViewById(R.id.status);
185bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence
186bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
187bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
188bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
189bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        CharSequence warning = TextUtils.expandTemplate(warningTemplate,
190bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                                                                        Integer.toString(remainingAttempts));
191bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        status.setText(warning);
192bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    } else {
193bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                        status.setText(R.string.try_again);
194bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence                    }
195bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence
196f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    if (mLockPatternView != null) {
197f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                        mLockPatternView.setDisplayMode(DisplayMode.Wrong);
198f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    }
199f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    // Reenable the password entry
200f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    if (mPasswordEntry != null) {
201f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                        mPasswordEntry.setEnabled(true);
2025a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        final InputMethodManager imm = (InputMethodManager) getSystemService(
2035a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                                                  Context.INPUT_METHOD_SERVICE);
2045a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        imm.showSoftInput(mPasswordEntry, 0);
20508c6ab0feaf459029d470b5c7a50fb55be763365Paul Lawrence                        setBackFunctionality(true);
206f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    }
207f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    if (mLockPatternView != null) {
208f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                        mLockPatternView.setEnabled(true);
209f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence                    }
2102daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                }
21106c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
21206c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
21306c5ff4a46686d3f599e40df0768177d3d88913cJason parks    }
21475c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parks
2150e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
2160e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2170e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected Boolean doInBackground(Void... params) {
218de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
2190e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            try {
220d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Validating encryption state.");
2210e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                int state = service.getEncryptionState();
2220e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                if (state == IMountService.ENCRYPTION_STATE_NONE) {
2230e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
2240e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    return true; // Unexpected, but fine, I guess...
2250e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                }
2260e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return state == IMountService.ENCRYPTION_STATE_OK;
2270e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            } catch (RemoteException e) {
2280e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Unable to get encryption state properly");
2290e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return true;
2300e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2310e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2320e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
2330e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2340e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected void onPostExecute(Boolean result) {
2350e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            mValidationComplete = true;
2360e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            if (Boolean.FALSE.equals(result)) {
2370e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
2380e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                mEncryptionGoneBad = true;
239d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            } else {
240d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
2410e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2420e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setupUi();
2430e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2440e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
2450e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
24691a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private final Handler mHandler = new Handler() {
247ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        @Override
248ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        public void handleMessage(Message msg) {
249ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            switch (msg.what) {
250de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_UPDATE_PROGRESS:
251f8217304073b5cd13823a0602568394be93bf310Jason parks                updateProgress();
252ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
25335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
254de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_COOLDOWN:
255f8217304073b5cd13823a0602568394be93bf310Jason parks                cooldown();
256ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
257de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
258de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_NOTIFY:
259de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                notifyUser();
260de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                break;
261ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
262ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        }
263ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    };
26435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
265de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private AudioManager mAudioManager;
266bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** The status bar where back/home/recent buttons are shown. */
267bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private StatusBarManager mStatusBar;
268bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
269bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** All the widgets to disable in the status bar */
270bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
271bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ICONS
272bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
273bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_SYSTEM_INFO
274bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_HOME
27566d050bb4b6bf899e363f3e67d1eb5774de744b5Alon Albert            | StatusBarManager.DISABLE_SEARCH
276bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_RECENT;
277de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
27891a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the UI only. */
27991a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView() {
28091a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return getIntent().hasExtra(EXTRA_FORCE_VIEW);
28191a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
28291a2f0566afb91549cbda9289b516154a6467624Ben Komalo
28391a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the specific UI view only. */
28491a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView(String viewType /* non-nullable */) {
28591a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
28691a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
28791a2f0566afb91549cbda9289b516154a6467624Ben Komalo
288de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
289de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Notify the user that we are awaiting input. Currently this sends an audio alert.
290de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
291de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private void notifyUser() {
292d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        if (mNotificationCountdown > 0) {
293d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal            --mNotificationCountdown;
294d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        } else if (mAudioManager != null) {
295de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            try {
296de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // Play the standard keypress sound at full volume. This should be available on
297de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // every device. We cannot play a ringtone here because media services aren't
298de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
299de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // on tablet devices. The idea is to alert the user that something is needed: this
300de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // does not have to be pleasing.
301de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
302de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            } catch (Exception e) {
303de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
304de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
305de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
30686b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        // Notify the user again in 5 seconds.
307de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
30886b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
30973456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence
31073456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence        if (mWakeLock.isHeld()) {
31173456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            if (mReleaseWakeLockCountdown > 0) {
31273456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                --mReleaseWakeLockCountdown;
31373456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            } else {
31473456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                mWakeLock.release();
31573456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            }
31673456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence        }
317de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
318de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
319de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
320de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Ignore back events after the user has entered the decrypt screen and while the device is
321de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * encrypting.
322de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
323de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    @Override
324de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    public void onBackPressed() {
325bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        // In the rare case that something pressed back even though we were disabled.
326de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mIgnoreBack)
327de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            return;
328de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        super.onBackPressed();
329de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
330de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
3318fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
3328fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public void onCreate(Bundle savedInstanceState) {
3338fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        super.onCreate(savedInstanceState);
33435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3359597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        // If we are not encrypted or encrypting, get out quickly.
336de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String state = SystemProperties.get("vold.decrypt");
33791a2f0566afb91549cbda9289b516154a6467624Ben Komalo        if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
33835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks            // Disable the crypt keeper.
3398fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            PackageManager pm = getPackageManager();
3408fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            ComponentName name = new ComponentName(this, CryptKeeper.class);
341140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
342140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn                    PackageManager.DONT_KILL_APP);
343140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // Typically CryptKeeper is launched as the home app.  We didn't
344644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // want to be running, so need to finish this activity.  We can count
345644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // on the activity manager re-launching the new home app upon finishing
346644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // this one, since this will leave the activity stack empty.
347140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // NOTE: This is really grungy.  I think it would be better for the
348140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // activity manager to explicitly launch the crypt keeper instead of
349140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // home in the situation where we need to decrypt the device
350140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            finish();
3518fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return;
3528fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
35335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3547ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        try {
3557ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence            if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
3567ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3577ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence            }
3587ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        } catch (NotFoundException e) {
3597ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        }
3607ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence
361b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // Disable the status bar, but do NOT disable back because the user needs a way to go
362b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // from keyboard settings and back to the password screen.
363bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
364bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar.disable(sWidgetsToDisable);
3651499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
366ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        setAirplaneModeIfNecessary();
367de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
3681499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        // Check for (and recover) retained instance data
369de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final Object lastInstance = getLastNonConfigurationInstance();
3701499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        if (lastInstance instanceof NonConfigurationInstanceState) {
3711499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
3721499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            mWakeLock = retained.wakelock;
3730460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
3741499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
375ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
37635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3779597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    /**
3789597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * Note, we defer the state check and screen setup to onStart() because this will be
3799597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * re-run if the user clicks the power button (sleeping/waking the screen), and this is
3809597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * especially important if we were to lose the wakelock for any reason.
3819597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     */
3829597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    @Override
3839597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    public void onStart() {
3849597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        super.onStart();
385d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        setupUi();
3860e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
3870e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
3880e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /**
3890e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * Initializes the UI based on the current state of encryption.
3900e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * This is idempotent - calling repeatedly will simply re-initialize the UI.
3910e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     */
3920e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private void setupUi() {
3930e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
3940e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setContentView(R.layout.crypt_keeper_progress);
3950e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            showFactoryReset();
3960e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            return;
3970e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
3980e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
399de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String progress = SystemProperties.get("vold.encrypt_progress");
4000e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
4019597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            setContentView(R.layout.crypt_keeper_progress);
4029597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            encryptionProgressInit();
403de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
4049ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence            new AsyncTask<Void, Void, Void>() {
4059ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                int type = StorageManager.CRYPT_TYPE_PASSWORD;
4069ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                String owner_info;
4079ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4089ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                @Override
4099ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                public Void doInBackground(Void... v) {
4109ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    try {
4119ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        final IMountService service = getMountService();
4129ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        type = service.getPasswordType();
4139ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        owner_info = service.getField("OwnerInfo");
4149ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } catch (Exception e) {
4159ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        Log.e(TAG, "Error calling mount service " + e);
4169ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    }
4179ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4189ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    return null;
4199ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                }
420b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
4219ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                @Override
4229ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                public void onPostExecute(java.lang.Void v) {
4239ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    if(type == StorageManager.CRYPT_TYPE_PIN) {
4249ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_pin_entry);
4255a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_pin;
4269ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
4279ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_pattern_entry);
4289ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setBackFunctionality(false);
4295a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_pattern;
4309ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } else {
4319ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_password_entry);
4325a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_password;
4339ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    }
4345a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    final TextView status = (TextView) findViewById(R.id.status);
4355a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    status.setText(mStatusString);
4365a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
4375a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
4385a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    ownerInfo.setText(owner_info);
4395a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    ownerInfo.setSelected(true); // Required for marquee'ing to work
4409ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4419ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    passwordEntryInit();
4425a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
4435a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    if (mCooldown > 0) {
4445a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        setBackFunctionality(false);
4455a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        cooldown(); // in case we are cooling down and coming back from emergency dialler
4465a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    }
4479ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                }
4489ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence            }.execute();
449d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        } else if (!mValidationRequested) {
450d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            // We're supposed to be encrypted, but no validation has been done.
451d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            new ValidationTask().execute((Void[]) null);
452d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            mValidationRequested = true;
4539597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        }
4549597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    }
4559597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler
456f8217304073b5cd13823a0602568394be93bf310Jason parks    @Override
457f8217304073b5cd13823a0602568394be93bf310Jason parks    public void onStop() {
458f8217304073b5cd13823a0602568394be93bf310Jason parks        super.onStop();
459de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_COOLDOWN);
460de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
461de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
4621499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
4631499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
4641499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
4651499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Reconfiguring, so propagate the wakelock to the next instance.  This runs between onStop()
4661499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * and onDestroy() and only if we are changing configuration (e.g. rotation).  Also clears
4671499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * mWakeLock so the subsequent call to onDestroy does not release it.
4681499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
4691499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
4701499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public Object onRetainNonConfigurationInstance() {
4711499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
4720460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
4731499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        mWakeLock = null;
4741499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        return state;
4751499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
4761499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
4771499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
4781499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public void onDestroy() {
4791499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        super.onDestroy();
480f8217304073b5cd13823a0602568394be93bf310Jason parks
481f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mWakeLock != null) {
4820460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Releasing and destroying wakelock");
483f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock.release();
484f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock = null;
485f8217304073b5cd13823a0602568394be93bf310Jason parks        }
486f8217304073b5cd13823a0602568394be93bf310Jason parks    }
487f8217304073b5cd13823a0602568394be93bf310Jason parks
488de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
489de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Start encrypting the device.
490de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
491ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private void encryptionProgressInit() {
49235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // Accquire a partial wakelock to prevent the device from sleeping. Note
49335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // we never release this wakelock as we will be restarted after the device
49435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // is encrypted.
4950460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Encryption progress screen initializing.");
4969ee164f44ea1ad66fa16ed702eb885e36ade0d8bBen Komalo        if (mWakeLock == null) {
4970460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Acquiring wakelock.");
4980460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
4990460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
5000460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock.acquire();
5010460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        }
502f8217304073b5cd13823a0602568394be93bf310Jason parks
503bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
504de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Ignore all back presses from now, both hard and soft keys.
505bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        setBackFunctionality(false);
506de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Start the first run of progress manually. This method sets up messages to occur at
507de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // repeated intervals.
508f8217304073b5cd13823a0602568394be93bf310Jason parks        updateProgress();
509f8217304073b5cd13823a0602568394be93bf310Jason parks    }
510f8217304073b5cd13823a0602568394be93bf310Jason parks
51113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    private void showFactoryReset() {
51213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Hide the encryption-bot to make room for the "factory reset" button
51313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        findViewById(R.id.encroid).setVisibility(View.GONE);
51413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
51513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Show the reset button, failure text, and a divider
516bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button button = (Button) findViewById(R.id.factory_reset);
51713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setVisibility(View.VISIBLE);
51813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setOnClickListener(new OnClickListener() {
519bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                @Override
52013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            public void onClick(View v) {
52113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                // Factory reset the device.
52213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
52313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            }
52413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        });
52513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
526bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // Alert the user of the failure.
527bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
528bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
52913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
530bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View view = findViewById(R.id.bottom_divider);
531bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
532f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        if (view != null) {
533f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo            view.setVisibility(View.VISIBLE);
534f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        }
53513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    }
53613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
537f8217304073b5cd13823a0602568394be93bf310Jason parks    private void updateProgress() {
538bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final String state = SystemProperties.get("vold.encrypt_progress");
539f8217304073b5cd13823a0602568394be93bf310Jason parks
5400e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if ("error_partially_encrypted".equals(state)) {
54113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            showFactoryReset();
54213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            return;
54313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
54413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
545b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Get status as percentage first
546b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        CharSequence status = getText(R.string.crypt_keeper_setup_description);
547b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        int percent = 0;
548f8217304073b5cd13823a0602568394be93bf310Jason parks        try {
54991a2f0566afb91549cbda9289b516154a6467624Ben Komalo            // Force a 50% progress state when debugging the view.
550b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            percent = isDebugView() ? 50 : Integer.parseInt(state);
551f8217304073b5cd13823a0602568394be93bf310Jason parks        } catch (Exception e) {
552f8217304073b5cd13823a0602568394be93bf310Jason parks            Log.w(TAG, "Error parsing progress: " + e.toString());
553f8217304073b5cd13823a0602568394be93bf310Jason parks        }
554b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        String progress = Integer.toString(percent);
55535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
556b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Now try to get status as time remaining and replace as appropriate
5570460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.v(TAG, "Encryption progress: " + progress);
558b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        try {
559b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
560b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            int time = Integer.parseInt(timeProperty);
561b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            if (time >= 0) {
562b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                // Round up to multiple of 10 - this way display is less jerky
563b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                time = (time + 9) / 10 * 10;
564b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                progress = DateUtils.formatElapsedTime(time);
565b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                status = getText(R.string.crypt_keeper_setup_time_remaining);
566b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            }
567b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        } catch (Exception e) {
568b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            // Will happen if no time etc - show percentage
569b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        }
570b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence
571bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView tv = (TextView) findViewById(R.id.status);
572de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (tv != null) {
573b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            tv.setText(TextUtils.expandTemplate(status, progress));
574de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
575b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence
576b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Check the progress every 1 seconds
577de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
578b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
579f8217304073b5cd13823a0602568394be93bf310Jason parks    }
580f8217304073b5cd13823a0602568394be93bf310Jason parks
581de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Disable password input for a while to force the user to waste time between retries */
582f8217304073b5cd13823a0602568394be93bf310Jason parks    private void cooldown() {
583bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView status = (TextView) findViewById(R.id.status);
58413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
585f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mCooldown <= 0) {
586de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Re-enable the password entry and back presses.
5872daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            if (mPasswordEntry != null) {
5882daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                mPasswordEntry.setEnabled(true);
5895a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                final InputMethodManager imm = (InputMethodManager) getSystemService(
5905a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                                          Context.INPUT_METHOD_SERVICE);
5915a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                imm.showSoftInput(mPasswordEntry, 0);
5922daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                setBackFunctionality(true);
5932daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            }
5942daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            if (mLockPatternView != null) {
5952daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                mLockPatternView.setEnabled(true);
5962daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence            }
5975a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            status.setText(mStatusString);
598f8217304073b5cd13823a0602568394be93bf310Jason parks        } else {
5995a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            // Disable the password entry and back presses.
6005a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            if (mPasswordEntry != null) {
6015a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mPasswordEntry.setEnabled(false);
6025a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            }
6035a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            if (mLockPatternView != null) {
6045a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mLockPatternView.setEnabled(false);
6055a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence            }
6065a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
60713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            CharSequence template = getText(R.string.crypt_keeper_cooldown);
608bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
60913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
610f8217304073b5cd13823a0602568394be93bf310Jason parks            mCooldown--;
611de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.removeMessages(MESSAGE_COOLDOWN);
612de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
613f8217304073b5cd13823a0602568394be93bf310Jason parks        }
614ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
61535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
616bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /**
617bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * Sets the back status: enabled or disabled according to the parameter.
618bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * @param isEnabled true if back is enabled, false otherwise.
619bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     */
620bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private final void setBackFunctionality(boolean isEnabled) {
621bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mIgnoreBack = !isEnabled;
622bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        if (isEnabled) {
623bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable);
624bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        } else {
625bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
626bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        }
627bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    }
628bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
629b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
630f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        new LockPatternView.OnPatternListener() {
631b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
632f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
633f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternStart() {
634f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.removeCallbacks(mClearPatternRunnable);
635f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
636b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
637f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
638f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternCleared() {
639f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
640b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
641f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
642f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternDetected(List<LockPatternView.Cell> pattern) {
643f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.setEnabled(false);
644f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
645f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
646b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
647f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
648f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternCellAdded(List<Cell> pattern) {
649f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
650b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     };
651b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
652b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     private void passwordEntryInit() {
653b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Password/pin case
65406c5ff4a46686d3f599e40df0768177d3d88913cJason parks        mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
655b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mPasswordEntry != null){
656b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnEditorActionListener(this);
657b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.requestFocus();
658b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            // Become quiet when the user interacts with the Edit text screen.
659b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnKeyListener(this);
660b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnTouchListener(this);
661b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.addTextChangedListener(this);
662b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
663b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
664b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Pattern case
665b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
666b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mLockPatternView != null) {
667b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
668b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
6699fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
670c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        // Disable the Emergency call button if the device has no voice telephone capability
6713afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon        if (!getTelephonyManager().isVoiceCapable()) {
672c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            final View emergencyCall = findViewById(R.id.emergencyCallButton);
673c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            if (emergencyCall != null) {
674c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                Log.d(TAG, "Removing the emergency Call button");
675c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                emergencyCall.setVisibility(View.GONE);
676c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            }
677c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        }
678c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal
679bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View imeSwitcher = findViewById(R.id.switch_ime_button);
6809fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final InputMethodManager imm = (InputMethodManager) getSystemService(
6819fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                Context.INPUT_METHOD_SERVICE);
6829fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
6839fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setVisibility(View.VISIBLE);
6849fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setOnClickListener(new OnClickListener() {
685bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
6869fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                public void onClick(View v) {
6879fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.showInputMethodPicker();
6889fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
6899fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            });
6909fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        }
6919fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
692de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // We want to keep the screen on while waiting for input. In minimal boot mode, the device
693de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // is completely non-functional, and we want the user to notice the device and enter a
694de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // password.
695de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mWakeLock == null) {
696de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            Log.d(TAG, "Acquiring wakelock.");
697de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
698de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            if (pm != null) {
699de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
700de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock.acquire();
70173456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                // Keep awake for 10 minutes - if the user hasn't been alerted by then
70273456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                // best not to just drain their battery
7035a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
704de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
705de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
70673456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence
7079fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Asynchronously throw up the IME, since there are issues with requesting it to be shown
7089fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // immediately.
7095a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence        if (mLockPatternView == null && mCooldown <= 0) {
710e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence            mHandler.postDelayed(new Runnable() {
711e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                @Override public void run() {
712e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                    imm.showSoftInputUnchecked(0, null);
713e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                }
714e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence            }, 0);
715e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence        }
7169fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7179fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        updateEmergencyCallButtonState();
718d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        // Notify the user in 120 seconds that we are waiting for him to enter the password.
71986b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
720d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
721fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller
722fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        // Dismiss keyguard while this screen is showing.
723fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
7249fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    }
72535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
7269fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    /**
7279fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * Method adapted from com.android.inputmethod.latin.Utils
7289fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     *
7299fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param imm The input method manager
7309fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param shouldIncludeAuxiliarySubtypes
7319fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @return true if we have multiple IMEs to choose from
7329fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     */
7339fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
7349fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final boolean shouldIncludeAuxiliarySubtypes) {
7359fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
7369fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7379fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Number of the filtered IMEs
7389fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        int filteredImisCount = 0;
7399fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7409fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        for (InputMethodInfo imi : enabledImis) {
7419fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // We can return true immediately after we find two or more filtered IMEs.
7429fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (filteredImisCount > 1) return true;
7439fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final List<InputMethodSubtype> subtypes =
7449fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.getEnabledInputMethodSubtypeList(imi, true);
7459fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have no subtypes should be counted.
7469fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (subtypes.isEmpty()) {
7479fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
7489fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
7499fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
75035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
7519fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            int auxCount = 0;
7529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            for (InputMethodSubtype subtype : subtypes) {
7539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                if (subtype.isAuxiliary()) {
7549fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    ++auxCount;
7559fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
7569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
7579fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final int nonAuxCount = subtypes.size() - auxCount;
7589fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
7599fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have one or more non-auxiliary subtypes should be counted.
7609fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
7619fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // subtypes should be counted as well.
7629fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
7639fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
7649fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
7659fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
76600046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        }
7678373b451692a39abdfd12444241c016831e1a4a4David Brown
7689fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        return filteredImisCount > 1
7699fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
7709fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // input method subtype (The current IME should be LatinIME.)
7719fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
7728fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
7738fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
7748fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private IMountService getMountService() {
775bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final IBinder service = ServiceManager.getService("mount");
7768fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        if (service != null) {
7778fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return IMountService.Stub.asInterface(service);
7788fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
7798fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return null;
7808fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
7818fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
7828fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
7838fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
78400046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
7858fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Get the password
786bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final String password = v.getText().toString();
7878fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
788ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            if (TextUtils.isEmpty(password)) {
789ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                return true;
790ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
79135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
7928fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Now that we have the password clear the password field.
7938fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            v.setText(null);
7948fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
795de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Disable the password entry and back keypress while checking the password. These
796de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // we either be re-enabled if the password was wrong or after the cooldown period.
79706c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(false);
798bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(false);
79906c5ff4a46686d3f599e40df0768177d3d88913cJason parks
8000460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Attempting to send command to decrypt");
80106c5ff4a46686d3f599e40df0768177d3d88913cJason parks            new DecryptTask().execute(password);
80235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8038fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return true;
8048fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
8058fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return false;
8068fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
8078373b451692a39abdfd12444241c016831e1a4a4David Brown
808bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
809ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Set airplane mode on the device if it isn't an LTE device.
810ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
811ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
812ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
813ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * both these problems, we turn the radio off. However, on certain networks turning on and
814ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * off the radio takes a long time. In such cases, we are better off leaving the radio
815ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * running so the latency of an E911 call is short.
816ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * The behavior after this is:
817ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 1. Emergency dialing: the emergency dialer has logic to force the device out of
818ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    airplane mode and restart the radio.
819ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 2. Full boot: we read the persistent settings from the previous boot and restore the
820ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    radio to whatever it was before it restarted. This also happens when rebooting a
821ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    phone that has no encryption.
822ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     */
823ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    private final void setAirplaneModeIfNecessary() {
824ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        final boolean isLteDevice =
8253afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon                getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
826ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        if (!isLteDevice) {
827ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            Log.d(TAG, "Going into airplane mode.");
8286a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
829ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
830ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            intent.putExtra("state", true);
8316a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            sendBroadcastAsUser(intent, UserHandle.ALL);
832ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        }
833ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    }
834ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal
835ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    /**
836bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Code to update the state of, and handle clicks from, the "Emergency call" button.
837bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     *
838bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * This code is mostly duplicated from the corresponding code in
839bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
840bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
8418373b451692a39abdfd12444241c016831e1a4a4David Brown    private void updateEmergencyCallButtonState() {
842bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
8438373b451692a39abdfd12444241c016831e1a4a4David Brown        // The button isn't present at all in some configurations.
844bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        if (emergencyCall == null)
845bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            return;
8468373b451692a39abdfd12444241c016831e1a4a4David Brown
8478373b451692a39abdfd12444241c016831e1a4a4David Brown        if (isEmergencyCallCapable()) {
848bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.VISIBLE);
849bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setOnClickListener(new View.OnClickListener() {
850bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
851bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal
8528373b451692a39abdfd12444241c016831e1a4a4David Brown                    public void onClick(View v) {
8538373b451692a39abdfd12444241c016831e1a4a4David Brown                        takeEmergencyCallAction();
8548373b451692a39abdfd12444241c016831e1a4a4David Brown                    }
8558373b451692a39abdfd12444241c016831e1a4a4David Brown                });
8568373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
857bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.GONE);
8588373b451692a39abdfd12444241c016831e1a4a4David Brown            return;
8598373b451692a39abdfd12444241c016831e1a4a4David Brown        }
8608373b451692a39abdfd12444241c016831e1a4a4David Brown
8618373b451692a39abdfd12444241c016831e1a4a4David Brown        int textId;
86235230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        if (getPhoneManager().isInAPhoneCall()) {
86389c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence            // Show "return to call"
8648373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_return_to_call;
8658373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
8668373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_emergency_call;
8678373b451692a39abdfd12444241c016831e1a4a4David Brown        }
868bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        emergencyCall.setText(textId);
8698373b451692a39abdfd12444241c016831e1a4a4David Brown    }
8708373b451692a39abdfd12444241c016831e1a4a4David Brown
8718373b451692a39abdfd12444241c016831e1a4a4David Brown    private boolean isEmergencyCallCapable() {
8728373b451692a39abdfd12444241c016831e1a4a4David Brown        return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
8738373b451692a39abdfd12444241c016831e1a4a4David Brown    }
8748373b451692a39abdfd12444241c016831e1a4a4David Brown
8758373b451692a39abdfd12444241c016831e1a4a4David Brown    private void takeEmergencyCallAction() {
87635230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        PhoneManager phoneManager = getPhoneManager();
87735230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        if (phoneManager.isInAPhoneCall()) {
87835230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon            phoneManager.showCallScreen(false /* showDialpad */);
8798373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
8808373b451692a39abdfd12444241c016831e1a4a4David Brown            launchEmergencyDialer();
8818373b451692a39abdfd12444241c016831e1a4a4David Brown        }
8828373b451692a39abdfd12444241c016831e1a4a4David Brown    }
8838373b451692a39abdfd12444241c016831e1a4a4David Brown
8848373b451692a39abdfd12444241c016831e1a4a4David Brown
8858373b451692a39abdfd12444241c016831e1a4a4David Brown    private void launchEmergencyDialer() {
886bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
8878373b451692a39abdfd12444241c016831e1a4a4David Brown        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
8888373b451692a39abdfd12444241c016831e1a4a4David Brown                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
8895a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence        setBackFunctionality(true);
8908373b451692a39abdfd12444241c016831e1a4a4David Brown        startActivity(intent);
8918373b451692a39abdfd12444241c016831e1a4a4David Brown    }
892d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
8933afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon    private TelephonyManager getTelephonyManager() {
8943afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon        return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
8953afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon    }
8963afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon
89735230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon    private PhoneManager getPhoneManager() {
89835230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon        return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
89935230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon    }
90035230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon
901d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /**
902d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     * Listen to key events so we can disable sounds when we get a keyinput in EditText.
903d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     */
904d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private void delayAudioNotification() {
905d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mNotificationCountdown = 20;
906d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
907d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
908d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
909d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onKey(View v, int keyCode, KeyEvent event) {
910d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
911d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
912d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
913d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
914d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
915d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onTouch(View v, MotionEvent event) {
916d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
917d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
918d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
919d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
920d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
921d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
922d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
923d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
924d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
925d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
926d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void onTextChanged(CharSequence s, int start, int before, int count) {
927d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
928d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
929d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
930d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
931d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void afterTextChanged(Editable s) {
932d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
933d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
9348373b451692a39abdfd12444241c016831e1a4a4David Brown}
935