CryptKeeper.java revision fb3d5cad48876dbd366fd8b562529d355d2428db
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;
248fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.pm.PackageManager;
25de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwalimport android.media.AudioManager;
2606c5ff4a46686d3f599e40df0768177d3d88913cJason parksimport android.os.AsyncTask;
278fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.Bundle;
28ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Handler;
298fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.IBinder;
30ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Message;
3135933812b16f0b4221280bbcc3c32f6540996bf2Jason parksimport android.os.PowerManager;
328373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.os.RemoteException;
338fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.ServiceManager;
348fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.SystemProperties;
356a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tateimport android.os.UserHandle;
368fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.storage.IMountService;
376ebbd30e18ca514ca9ee87ffa428c55c9fd92a8cVikram Aggarwalimport android.provider.Settings;
388373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.telephony.TelephonyManager;
39d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.Editable;
40ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.text.TextUtils;
41d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.TextWatcher;
428fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.util.Log;
438fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.KeyEvent;
44d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.MotionEvent;
4513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View;
46fb3d5cad48876dbd366fd8b562529d355d2428dbJim Millerimport android.view.WindowManager;
4713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View.OnClickListener;
48d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnKeyListener;
49d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnTouchListener;
508fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.inputmethod.EditorInfo;
519fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodInfo;
5275c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parksimport android.view.inputmethod.InputMethodManager;
539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodSubtype;
5413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.widget.Button;
55ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.EditText;
56ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.ProgressBar;
578fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.widget.TextView;
588fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
59bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwalimport com.android.internal.statusbar.StatusBarIcon;
6091a2f0566afb91549cbda9289b516154a6467624Ben Komaloimport com.android.internal.telephony.ITelephony;
61ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwalimport com.android.internal.telephony.Phone;
625543404ae0ec9635b59d37fa01074ea598d539ebWink Savilleimport com.android.internal.telephony.PhoneConstants;
639fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
649fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport java.util.List;
6591a2f0566afb91549cbda9289b516154a6467624Ben Komalo
6691a2f0566afb91549cbda9289b516154a6467624Ben Komalo/**
6791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * Settings screens to show the UI flows for encrypting/decrypting the device.
6891a2f0566afb91549cbda9289b516154a6467624Ben Komalo *
6991a2f0566afb91549cbda9289b516154a6467624Ben Komalo * This may be started via adb for debugging the UI layout, without having to go through
7091a2f0566afb91549cbda9289b516154a6467624Ben Komalo * encryption flows everytime. It should be noted that starting the activity in this manner
7191a2f0566afb91549cbda9289b516154a6467624Ben Komalo * is only useful for verifying UI-correctness - the behavior will not be identical.
7291a2f0566afb91549cbda9289b516154a6467624Ben Komalo * <pre>
7391a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell pm enable com.android.settings/.CryptKeeper
7491a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell am start \
7591a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
7691a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -n com.android.settings/.CryptKeeper
7791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * </pre>
7891a2f0566afb91549cbda9289b516154a6467624Ben Komalo */
79d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalpublic class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
80d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        OnKeyListener, OnTouchListener, TextWatcher {
81ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final String TAG = "CryptKeeper";
8235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
838fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private static final String DECRYPT_STATE = "trigger_restart_framework";
84de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate encryption update progress. */
85de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_UPDATE_PROGRESS = 1;
86de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to cool-down (waste user's time between password attempts) */
87de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_COOLDOWN = 2;
88de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate alerting the user that we are waiting for password entry */
89de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_NOTIFY = 3;
90de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
91de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    // Constants used to control policy.
92ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int MAX_FAILED_ATTEMPTS = 30;
93ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_ATTEMPTS = 10;
94ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
95ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks
968373b451692a39abdfd12444241c016831e1a4a4David Brown    // Intent action for launching the Emergency Dialer activity.
978373b451692a39abdfd12444241c016831e1a4a4David Brown    static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
988373b451692a39abdfd12444241c016831e1a4a4David Brown
9991a2f0566afb91549cbda9289b516154a6467624Ben Komalo    // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
10091a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String EXTRA_FORCE_VIEW =
10191a2f0566afb91549cbda9289b516154a6467624Ben Komalo            "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
10291a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String FORCE_VIEW_PROGRESS = "progress";
10391a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String FORCE_VIEW_ERROR = "error";
104de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final String FORCE_VIEW_PASSWORD = "password";
10591a2f0566afb91549cbda9289b516154a6467624Ben Komalo
106bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
1070e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mValidationComplete;
108d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo    private boolean mValidationRequested;
1090e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
1100e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mEncryptionGoneBad;
111de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** A flag to indicate when the back event should be ignored */
112de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private boolean mIgnoreBack = false;
1131499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private int mCooldown;
1141499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    PowerManager.WakeLock mWakeLock;
11506c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private EditText mPasswordEntry;
116d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
117d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private int mNotificationCountdown = 0;
1181499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1191499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
1201499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Used to propagate state through configuration changes (e.g. screen rotation)
1211499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
1221499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private static class NonConfigurationInstanceState {
1231499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        final PowerManager.WakeLock wakelock;
1241499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1251499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
1261499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            wakelock = _wakelock;
1271499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
1281499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
1291499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
130bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
131bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Activity used to fade the screen to black after the password is entered.
132bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
1339f55ae28789754dafeed5c68468d45135d46199eVikram Aggarwal    public static class FadeToBlack extends Activity {
13413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        @Override
13513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        public void onCreate(Bundle savedInstanceState) {
13613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            super.onCreate(savedInstanceState);
13713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            setContentView(R.layout.crypt_keeper_blank);
13813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
139de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        /** Ignore all back events. */
140de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        @Override
141de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        public void onBackPressed() {
142de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            return;
143de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
144f1dbf55819e01f6cf1a99d38a292b15de1262a29Jason parks    }
145ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks
14606c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private class DecryptTask extends AsyncTask<String, Void, Integer> {
14706c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
14806c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected Integer doInBackground(String... params) {
149de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
15006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            try {
15106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return service.decryptStorage(params[0]);
15206c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } catch (Exception e) {
15306c5ff4a46686d3f599e40df0768177d3d88913cJason parks                Log.e(TAG, "Error while decrypting...", e);
15406c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return -1;
15506c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
15606c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
15706c5ff4a46686d3f599e40df0768177d3d88913cJason parks
15806c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
15906c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected void onPostExecute(Integer failedAttempts) {
16006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            if (failedAttempts == 0) {
16106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // The password was entered successfully. Start the Blank activity
16206c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // so this activity animates to black before the devices starts. Note
16306c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // It has 1 second to complete the animation or it will be frozen
16406c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // until the boot animation comes back up.
165bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
16606c5ff4a46686d3f599e40df0768177d3d88913cJason parks                finish();
16706c5ff4a46686d3f599e40df0768177d3d88913cJason parks                startActivity(intent);
16806c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
16906c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Factory reset the device.
17006c5ff4a46686d3f599e40df0768177d3d88913cJason parks                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
17106c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
17206c5ff4a46686d3f599e40df0768177d3d88913cJason parks                mCooldown = COOL_DOWN_INTERVAL;
17306c5ff4a46686d3f599e40df0768177d3d88913cJason parks                cooldown();
17406c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else {
175bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                final TextView status = (TextView) findViewById(R.id.status);
176bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                status.setText(R.string.try_again);
17706c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Reenable the password entry
17806c5ff4a46686d3f599e40df0768177d3d88913cJason parks                mPasswordEntry.setEnabled(true);
17906c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
18006c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
18106c5ff4a46686d3f599e40df0768177d3d88913cJason parks    }
18275c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parks
1830e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
1840e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
1850e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected Boolean doInBackground(Void... params) {
186de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
1870e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            try {
188d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Validating encryption state.");
1890e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                int state = service.getEncryptionState();
1900e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                if (state == IMountService.ENCRYPTION_STATE_NONE) {
1910e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
1920e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    return true; // Unexpected, but fine, I guess...
1930e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                }
1940e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return state == IMountService.ENCRYPTION_STATE_OK;
1950e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            } catch (RemoteException e) {
1960e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Unable to get encryption state properly");
1970e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return true;
1980e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
1990e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2000e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
2010e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2020e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected void onPostExecute(Boolean result) {
2030e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            mValidationComplete = true;
2040e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            if (Boolean.FALSE.equals(result)) {
2050e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
2060e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                mEncryptionGoneBad = true;
207d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            } else {
208d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
2090e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2100e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setupUi();
2110e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2120e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
2130e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
21491a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private final Handler mHandler = new Handler() {
215ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        @Override
216ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        public void handleMessage(Message msg) {
217ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            switch (msg.what) {
218de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_UPDATE_PROGRESS:
219f8217304073b5cd13823a0602568394be93bf310Jason parks                updateProgress();
220ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
22135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
222de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_COOLDOWN:
223f8217304073b5cd13823a0602568394be93bf310Jason parks                cooldown();
224ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
225de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
226de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_NOTIFY:
227de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                notifyUser();
228de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                break;
229ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
230ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        }
231ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    };
23235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
233de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private AudioManager mAudioManager;
234bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** The status bar where back/home/recent buttons are shown. */
235bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private StatusBarManager mStatusBar;
236bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
237bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** All the widgets to disable in the status bar */
238bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
239bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ICONS
240bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
241bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_SYSTEM_INFO
242bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_HOME
24366d050bb4b6bf899e363f3e67d1eb5774de744b5Alon Albert            | StatusBarManager.DISABLE_SEARCH
244bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_RECENT;
245de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
24691a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the UI only. */
24791a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView() {
24891a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return getIntent().hasExtra(EXTRA_FORCE_VIEW);
24991a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
25091a2f0566afb91549cbda9289b516154a6467624Ben Komalo
25191a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the specific UI view only. */
25291a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView(String viewType /* non-nullable */) {
25391a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
25491a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
25591a2f0566afb91549cbda9289b516154a6467624Ben Komalo
256de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
257de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Notify the user that we are awaiting input. Currently this sends an audio alert.
258de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
259de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private void notifyUser() {
260d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        if (mNotificationCountdown > 0) {
261d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal            --mNotificationCountdown;
262d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        } else if (mAudioManager != null) {
263de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            try {
264de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // Play the standard keypress sound at full volume. This should be available on
265de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // every device. We cannot play a ringtone here because media services aren't
266de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
267de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // on tablet devices. The idea is to alert the user that something is needed: this
268de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // does not have to be pleasing.
269de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
270de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            } catch (Exception e) {
271de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
272de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
273de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
27486b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        // Notify the user again in 5 seconds.
275de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
27686b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
277de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
278de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
279de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
280de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Ignore back events after the user has entered the decrypt screen and while the device is
281de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * encrypting.
282de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
283de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    @Override
284de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    public void onBackPressed() {
285bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        // In the rare case that something pressed back even though we were disabled.
286de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mIgnoreBack)
287de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            return;
288de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        super.onBackPressed();
289de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
290de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
2918fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
2928fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public void onCreate(Bundle savedInstanceState) {
2938fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        super.onCreate(savedInstanceState);
29435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
2959597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        // If we are not encrypted or encrypting, get out quickly.
296de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String state = SystemProperties.get("vold.decrypt");
29791a2f0566afb91549cbda9289b516154a6467624Ben Komalo        if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
29835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks            // Disable the crypt keeper.
2998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            PackageManager pm = getPackageManager();
3008fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            ComponentName name = new ComponentName(this, CryptKeeper.class);
301140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
302140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn                    PackageManager.DONT_KILL_APP);
303140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // Typically CryptKeeper is launched as the home app.  We didn't
304644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // want to be running, so need to finish this activity.  We can count
305644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // on the activity manager re-launching the new home app upon finishing
306644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // this one, since this will leave the activity stack empty.
307140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // NOTE: This is really grungy.  I think it would be better for the
308140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // activity manager to explicitly launch the crypt keeper instead of
309140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // home in the situation where we need to decrypt the device
310140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            finish();
3118fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return;
3128fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
31335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
314b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // Disable the status bar, but do NOT disable back because the user needs a way to go
315b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // from keyboard settings and back to the password screen.
316bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
317bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar.disable(sWidgetsToDisable);
3181499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
319ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        setAirplaneModeIfNecessary();
320de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
3211499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        // Check for (and recover) retained instance data
322de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final Object lastInstance = getLastNonConfigurationInstance();
3231499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        if (lastInstance instanceof NonConfigurationInstanceState) {
3241499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
3251499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            mWakeLock = retained.wakelock;
3260460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
3271499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
328ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
32935933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3309597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    /**
3319597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * Note, we defer the state check and screen setup to onStart() because this will be
3329597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * re-run if the user clicks the power button (sleeping/waking the screen), and this is
3339597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * especially important if we were to lose the wakelock for any reason.
3349597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     */
3359597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    @Override
3369597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    public void onStart() {
3379597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        super.onStart();
338d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        setupUi();
3390e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
3400e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
3410e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /**
3420e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * Initializes the UI based on the current state of encryption.
3430e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * This is idempotent - calling repeatedly will simply re-initialize the UI.
3440e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     */
3450e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private void setupUi() {
3460e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
3470e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setContentView(R.layout.crypt_keeper_progress);
3480e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            showFactoryReset();
3490e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            return;
3500e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
3510e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
352de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String progress = SystemProperties.get("vold.encrypt_progress");
3530e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
3549597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            setContentView(R.layout.crypt_keeper_progress);
3559597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            encryptionProgressInit();
356de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
3579597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            setContentView(R.layout.crypt_keeper_password_entry);
3589597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            passwordEntryInit();
359d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        } else if (!mValidationRequested) {
360d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            // We're supposed to be encrypted, but no validation has been done.
361d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            new ValidationTask().execute((Void[]) null);
362d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            mValidationRequested = true;
3639597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        }
3649597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    }
3659597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler
366f8217304073b5cd13823a0602568394be93bf310Jason parks    @Override
367f8217304073b5cd13823a0602568394be93bf310Jason parks    public void onStop() {
368f8217304073b5cd13823a0602568394be93bf310Jason parks        super.onStop();
369de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_COOLDOWN);
370de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
371de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
3721499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
3731499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
3741499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
3751499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Reconfiguring, so propagate the wakelock to the next instance.  This runs between onStop()
3761499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * and onDestroy() and only if we are changing configuration (e.g. rotation).  Also clears
3771499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * mWakeLock so the subsequent call to onDestroy does not release it.
3781499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
3791499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
3801499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public Object onRetainNonConfigurationInstance() {
3811499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
3820460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
3831499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        mWakeLock = null;
3841499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        return state;
3851499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
3861499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
3871499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
3881499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public void onDestroy() {
3891499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        super.onDestroy();
390f8217304073b5cd13823a0602568394be93bf310Jason parks
391f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mWakeLock != null) {
3920460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Releasing and destroying wakelock");
393f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock.release();
394f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock = null;
395f8217304073b5cd13823a0602568394be93bf310Jason parks        }
396f8217304073b5cd13823a0602568394be93bf310Jason parks    }
397f8217304073b5cd13823a0602568394be93bf310Jason parks
398de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
399de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Start encrypting the device.
400de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
401ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private void encryptionProgressInit() {
40235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // Accquire a partial wakelock to prevent the device from sleeping. Note
40335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // we never release this wakelock as we will be restarted after the device
40435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // is encrypted.
4050460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Encryption progress screen initializing.");
4069ee164f44ea1ad66fa16ed702eb885e36ade0d8bBen Komalo        if (mWakeLock == null) {
4070460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Acquiring wakelock.");
4080460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
4090460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
4100460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock.acquire();
4110460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        }
412f8217304073b5cd13823a0602568394be93bf310Jason parks
413bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
414de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Ignore all back presses from now, both hard and soft keys.
415bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        setBackFunctionality(false);
416de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Start the first run of progress manually. This method sets up messages to occur at
417de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // repeated intervals.
418f8217304073b5cd13823a0602568394be93bf310Jason parks        updateProgress();
419f8217304073b5cd13823a0602568394be93bf310Jason parks    }
420f8217304073b5cd13823a0602568394be93bf310Jason parks
42113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    private void showFactoryReset() {
42213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Hide the encryption-bot to make room for the "factory reset" button
42313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        findViewById(R.id.encroid).setVisibility(View.GONE);
42413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
42513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Show the reset button, failure text, and a divider
426bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button button = (Button) findViewById(R.id.factory_reset);
42713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setVisibility(View.VISIBLE);
42813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setOnClickListener(new OnClickListener() {
429bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                @Override
43013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            public void onClick(View v) {
43113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                // Factory reset the device.
43213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
43313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            }
43413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        });
43513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
436bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // Alert the user of the failure.
437bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
438bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
43913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
440bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View view = findViewById(R.id.bottom_divider);
441bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
442f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        if (view != null) {
443f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo            view.setVisibility(View.VISIBLE);
444f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        }
44513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    }
44613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
447f8217304073b5cd13823a0602568394be93bf310Jason parks    private void updateProgress() {
448bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final String state = SystemProperties.get("vold.encrypt_progress");
449f8217304073b5cd13823a0602568394be93bf310Jason parks
4500e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if ("error_partially_encrypted".equals(state)) {
45113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            showFactoryReset();
45213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            return;
45313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
45413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
455f8217304073b5cd13823a0602568394be93bf310Jason parks        int progress = 0;
456f8217304073b5cd13823a0602568394be93bf310Jason parks        try {
45791a2f0566afb91549cbda9289b516154a6467624Ben Komalo            // Force a 50% progress state when debugging the view.
45891a2f0566afb91549cbda9289b516154a6467624Ben Komalo            progress = isDebugView() ? 50 : Integer.parseInt(state);
459f8217304073b5cd13823a0602568394be93bf310Jason parks        } catch (Exception e) {
460f8217304073b5cd13823a0602568394be93bf310Jason parks            Log.w(TAG, "Error parsing progress: " + e.toString());
461f8217304073b5cd13823a0602568394be93bf310Jason parks        }
46235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
463bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final CharSequence status = getText(R.string.crypt_keeper_setup_description);
4640460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.v(TAG, "Encryption progress: " + progress);
465bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView tv = (TextView) findViewById(R.id.status);
466de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (tv != null) {
467de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
468de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
469f8217304073b5cd13823a0602568394be93bf310Jason parks        // Check the progress every 5 seconds
470de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
471de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
472f8217304073b5cd13823a0602568394be93bf310Jason parks    }
473f8217304073b5cd13823a0602568394be93bf310Jason parks
474de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Disable password input for a while to force the user to waste time between retries */
475f8217304073b5cd13823a0602568394be93bf310Jason parks    private void cooldown() {
476bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView status = (TextView) findViewById(R.id.status);
47713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
478f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mCooldown <= 0) {
479de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Re-enable the password entry and back presses.
48006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(true);
481bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(true);
482f576dd6c9f2470516b2fd8494ad210a77f3c8c1fVikram Aggarwal            status.setText(R.string.enter_password);
483f8217304073b5cd13823a0602568394be93bf310Jason parks        } else {
48413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            CharSequence template = getText(R.string.crypt_keeper_cooldown);
485bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
48613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
487f8217304073b5cd13823a0602568394be93bf310Jason parks            mCooldown--;
488de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.removeMessages(MESSAGE_COOLDOWN);
489de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
490f8217304073b5cd13823a0602568394be93bf310Jason parks        }
491ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
49235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
493bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /**
494bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * Sets the back status: enabled or disabled according to the parameter.
495bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * @param isEnabled true if back is enabled, false otherwise.
496bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     */
497bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private final void setBackFunctionality(boolean isEnabled) {
498bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mIgnoreBack = !isEnabled;
499bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        if (isEnabled) {
500bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable);
501bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        } else {
502bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
503bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        }
504bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    }
505bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
506ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private void passwordEntryInit() {
50706c5ff4a46686d3f599e40df0768177d3d88913cJason parks        mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
50806c5ff4a46686d3f599e40df0768177d3d88913cJason parks        mPasswordEntry.setOnEditorActionListener(this);
5099fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        mPasswordEntry.requestFocus();
510d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        // Become quiet when the user interacts with the Edit text screen.
511d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mPasswordEntry.setOnKeyListener(this);
512d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mPasswordEntry.setOnTouchListener(this);
513d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mPasswordEntry.addTextChangedListener(this);
5149fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
515c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        // Disable the Emergency call button if the device has no voice telephone capability
516c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
517c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        if (!tm.isVoiceCapable()) {
518c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            final View emergencyCall = findViewById(R.id.emergencyCallButton);
519c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            if (emergencyCall != null) {
520c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                Log.d(TAG, "Removing the emergency Call button");
521c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                emergencyCall.setVisibility(View.GONE);
522c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            }
523c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        }
524c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal
525bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View imeSwitcher = findViewById(R.id.switch_ime_button);
5269fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final InputMethodManager imm = (InputMethodManager) getSystemService(
5279fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                Context.INPUT_METHOD_SERVICE);
5289fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
5299fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setVisibility(View.VISIBLE);
5309fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setOnClickListener(new OnClickListener() {
531bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
5329fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                public void onClick(View v) {
5339fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.showInputMethodPicker();
5349fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
5359fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            });
5369fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        }
5379fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
538de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // We want to keep the screen on while waiting for input. In minimal boot mode, the device
539de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // is completely non-functional, and we want the user to notice the device and enter a
540de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // password.
541de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mWakeLock == null) {
542de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            Log.d(TAG, "Acquiring wakelock.");
543de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
544de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            if (pm != null) {
545de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
546de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock.acquire();
547de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
548de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
5499fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Asynchronously throw up the IME, since there are issues with requesting it to be shown
5509fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // immediately.
5519fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        mHandler.postDelayed(new Runnable() {
5529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            @Override public void run() {
5539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                imm.showSoftInputUnchecked(0, null);
5549fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
5559fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        }, 0);
5569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
5579fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        updateEmergencyCallButtonState();
558d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        // Notify the user in 120 seconds that we are waiting for him to enter the password.
55986b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
560d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
561fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller
562fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        // Dismiss keyguard while this screen is showing.
563fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
5649fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    }
56535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
5669fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    /**
5679fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * Method adapted from com.android.inputmethod.latin.Utils
5689fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     *
5699fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param imm The input method manager
5709fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param shouldIncludeAuxiliarySubtypes
5719fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @return true if we have multiple IMEs to choose from
5729fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     */
5739fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
5749fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final boolean shouldIncludeAuxiliarySubtypes) {
5759fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
5769fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
5779fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Number of the filtered IMEs
5789fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        int filteredImisCount = 0;
5799fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
5809fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        for (InputMethodInfo imi : enabledImis) {
5819fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // We can return true immediately after we find two or more filtered IMEs.
5829fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (filteredImisCount > 1) return true;
5839fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final List<InputMethodSubtype> subtypes =
5849fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.getEnabledInputMethodSubtypeList(imi, true);
5859fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have no subtypes should be counted.
5869fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (subtypes.isEmpty()) {
5879fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
5889fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
5899fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
59035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
5919fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            int auxCount = 0;
5929fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            for (InputMethodSubtype subtype : subtypes) {
5939fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                if (subtype.isAuxiliary()) {
5949fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    ++auxCount;
5959fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
5969fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
5979fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final int nonAuxCount = subtypes.size() - auxCount;
5989fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
5999fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have one or more non-auxiliary subtypes should be counted.
6009fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
6019fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // subtypes should be counted as well.
6029fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
6039fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
6049fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
6059fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
60600046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        }
6078373b451692a39abdfd12444241c016831e1a4a4David Brown
6089fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        return filteredImisCount > 1
6099fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
6109fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // input method subtype (The current IME should be LatinIME.)
6119fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
6128fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
6138fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
6148fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private IMountService getMountService() {
615bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final IBinder service = ServiceManager.getService("mount");
6168fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        if (service != null) {
6178fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return IMountService.Stub.asInterface(service);
6188fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
6198fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return null;
6208fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
6218fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
6228fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
6238fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
62400046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
6258fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Get the password
626bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final String password = v.getText().toString();
6278fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
628ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            if (TextUtils.isEmpty(password)) {
629ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                return true;
630ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
63135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
6328fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Now that we have the password clear the password field.
6338fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            v.setText(null);
6348fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
635de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Disable the password entry and back keypress while checking the password. These
636de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // we either be re-enabled if the password was wrong or after the cooldown period.
63706c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(false);
638bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(false);
63906c5ff4a46686d3f599e40df0768177d3d88913cJason parks
6400460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Attempting to send command to decrypt");
64106c5ff4a46686d3f599e40df0768177d3d88913cJason parks            new DecryptTask().execute(password);
64235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
6438fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return true;
6448fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
6458fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return false;
6468fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
6478373b451692a39abdfd12444241c016831e1a4a4David Brown
648bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
649ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Set airplane mode on the device if it isn't an LTE device.
650ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
651ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
652ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
653ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * both these problems, we turn the radio off. However, on certain networks turning on and
654ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * off the radio takes a long time. In such cases, we are better off leaving the radio
655ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * running so the latency of an E911 call is short.
656ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * The behavior after this is:
657ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 1. Emergency dialing: the emergency dialer has logic to force the device out of
658ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    airplane mode and restart the radio.
659ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 2. Full boot: we read the persistent settings from the previous boot and restore the
660ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    radio to whatever it was before it restarted. This also happens when rebooting a
661ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    phone that has no encryption.
662ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     */
663ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    private final void setAirplaneModeIfNecessary() {
664ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        final boolean isLteDevice =
6655543404ae0ec9635b59d37fa01074ea598d539ebWink Saville                TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
666ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        if (!isLteDevice) {
667ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            Log.d(TAG, "Going into airplane mode.");
6686a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
669ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
670ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            intent.putExtra("state", true);
6716a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            sendBroadcastAsUser(intent, UserHandle.ALL);
672ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        }
673ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    }
674ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal
675ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    /**
676bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Code to update the state of, and handle clicks from, the "Emergency call" button.
677bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     *
678bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * This code is mostly duplicated from the corresponding code in
679bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
680bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
6818373b451692a39abdfd12444241c016831e1a4a4David Brown    private void updateEmergencyCallButtonState() {
682bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
6838373b451692a39abdfd12444241c016831e1a4a4David Brown        // The button isn't present at all in some configurations.
684bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        if (emergencyCall == null)
685bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            return;
6868373b451692a39abdfd12444241c016831e1a4a4David Brown
6878373b451692a39abdfd12444241c016831e1a4a4David Brown        if (isEmergencyCallCapable()) {
688bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.VISIBLE);
689bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setOnClickListener(new View.OnClickListener() {
690bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
691bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal
6928373b451692a39abdfd12444241c016831e1a4a4David Brown                    public void onClick(View v) {
6938373b451692a39abdfd12444241c016831e1a4a4David Brown                        takeEmergencyCallAction();
6948373b451692a39abdfd12444241c016831e1a4a4David Brown                    }
6958373b451692a39abdfd12444241c016831e1a4a4David Brown                });
6968373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
697bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.GONE);
6988373b451692a39abdfd12444241c016831e1a4a4David Brown            return;
6998373b451692a39abdfd12444241c016831e1a4a4David Brown        }
7008373b451692a39abdfd12444241c016831e1a4a4David Brown
701bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final int newState = TelephonyManager.getDefault().getCallState();
7028373b451692a39abdfd12444241c016831e1a4a4David Brown        int textId;
7038373b451692a39abdfd12444241c016831e1a4a4David Brown        if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
704bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            // Show "return to call" text and show phone icon
7058373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_return_to_call;
706bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final int phoneCallIcon = R.drawable.stat_sys_phone_call;
707bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
7088373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
7098373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_emergency_call;
710bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final int emergencyIcon = R.drawable.ic_emergency;
711bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
7128373b451692a39abdfd12444241c016831e1a4a4David Brown        }
713bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        emergencyCall.setText(textId);
7148373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7158373b451692a39abdfd12444241c016831e1a4a4David Brown
7168373b451692a39abdfd12444241c016831e1a4a4David Brown    private boolean isEmergencyCallCapable() {
7178373b451692a39abdfd12444241c016831e1a4a4David Brown        return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
7188373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7198373b451692a39abdfd12444241c016831e1a4a4David Brown
7208373b451692a39abdfd12444241c016831e1a4a4David Brown    private void takeEmergencyCallAction() {
7218373b451692a39abdfd12444241c016831e1a4a4David Brown        if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
7228373b451692a39abdfd12444241c016831e1a4a4David Brown            resumeCall();
7238373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
7248373b451692a39abdfd12444241c016831e1a4a4David Brown            launchEmergencyDialer();
7258373b451692a39abdfd12444241c016831e1a4a4David Brown        }
7268373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7278373b451692a39abdfd12444241c016831e1a4a4David Brown
7288373b451692a39abdfd12444241c016831e1a4a4David Brown    private void resumeCall() {
729bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
7308373b451692a39abdfd12444241c016831e1a4a4David Brown        if (phone != null) {
7318373b451692a39abdfd12444241c016831e1a4a4David Brown            try {
7328373b451692a39abdfd12444241c016831e1a4a4David Brown                phone.showCallScreen();
7338373b451692a39abdfd12444241c016831e1a4a4David Brown            } catch (RemoteException e) {
7348373b451692a39abdfd12444241c016831e1a4a4David Brown                Log.e(TAG, "Error calling ITelephony service: " + e);
7358373b451692a39abdfd12444241c016831e1a4a4David Brown            }
7368373b451692a39abdfd12444241c016831e1a4a4David Brown        }
7378373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7388373b451692a39abdfd12444241c016831e1a4a4David Brown
7398373b451692a39abdfd12444241c016831e1a4a4David Brown    private void launchEmergencyDialer() {
740bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
7418373b451692a39abdfd12444241c016831e1a4a4David Brown        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
7428373b451692a39abdfd12444241c016831e1a4a4David Brown                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
7438373b451692a39abdfd12444241c016831e1a4a4David Brown        startActivity(intent);
7448373b451692a39abdfd12444241c016831e1a4a4David Brown    }
745d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
746d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /**
747d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     * Listen to key events so we can disable sounds when we get a keyinput in EditText.
748d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     */
749d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private void delayAudioNotification() {
750d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mNotificationCountdown = 20;
751d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
752d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
753d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
754d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onKey(View v, int keyCode, KeyEvent event) {
755d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
756d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
757d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
758d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
759d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
760d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onTouch(View v, MotionEvent event) {
761d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
762d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
763d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
764d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
765d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
766d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
767d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
768d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
769d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
770d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
771d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void onTextChanged(CharSequence s, int start, int before, int count) {
772d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
773d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
774d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
775d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
776d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void afterTextChanged(Editable s) {
777d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
778d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
7798373b451692a39abdfd12444241c016831e1a4a4David Brown}
780