CryptKeeper.java revision e54e9323ed423d68f0567dcf05f45c62187ad8dd
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;
37b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport android.os.storage.StorageManager;
386ebbd30e18ca514ca9ee87ffa428c55c9fd92a8cVikram Aggarwalimport android.provider.Settings;
398373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.telephony.TelephonyManager;
40d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.Editable;
41ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.text.TextUtils;
42d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.TextWatcher;
438fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.util.Log;
448fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.KeyEvent;
45d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.MotionEvent;
4613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View;
47fb3d5cad48876dbd366fd8b562529d355d2428dbJim Millerimport android.view.WindowManager;
4813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View.OnClickListener;
49d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnKeyListener;
50d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnTouchListener;
518fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.inputmethod.EditorInfo;
529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodInfo;
5375c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parksimport android.view.inputmethod.InputMethodManager;
549fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodSubtype;
5513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.widget.Button;
56ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.EditText;
57ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.ProgressBar;
588fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.widget.TextView;
598fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
60bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwalimport com.android.internal.statusbar.StatusBarIcon;
6191a2f0566afb91549cbda9289b516154a6467624Ben Komaloimport com.android.internal.telephony.ITelephony;
62ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwalimport com.android.internal.telephony.Phone;
635543404ae0ec9635b59d37fa01074ea598d539ebWink Savilleimport com.android.internal.telephony.PhoneConstants;
64b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport com.android.internal.widget.LockPatternUtils;
65b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport com.android.internal.widget.LockPatternView;
66b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport com.android.internal.widget.LockPatternView.Cell;
679fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
689fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport java.util.List;
6991a2f0566afb91549cbda9289b516154a6467624Ben Komalo
7091a2f0566afb91549cbda9289b516154a6467624Ben Komalo/**
7191a2f0566afb91549cbda9289b516154a6467624Ben Komalo * Settings screens to show the UI flows for encrypting/decrypting the device.
7291a2f0566afb91549cbda9289b516154a6467624Ben Komalo *
7391a2f0566afb91549cbda9289b516154a6467624Ben Komalo * This may be started via adb for debugging the UI layout, without having to go through
7491a2f0566afb91549cbda9289b516154a6467624Ben Komalo * encryption flows everytime. It should be noted that starting the activity in this manner
7591a2f0566afb91549cbda9289b516154a6467624Ben Komalo * is only useful for verifying UI-correctness - the behavior will not be identical.
7691a2f0566afb91549cbda9289b516154a6467624Ben Komalo * <pre>
7791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell pm enable com.android.settings/.CryptKeeper
7891a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell am start \
7991a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
8091a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -n com.android.settings/.CryptKeeper
8191a2f0566afb91549cbda9289b516154a6467624Ben Komalo * </pre>
8291a2f0566afb91549cbda9289b516154a6467624Ben Komalo */
83d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalpublic class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
84d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        OnKeyListener, OnTouchListener, TextWatcher {
85ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final String TAG = "CryptKeeper";
8635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
878fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private static final String DECRYPT_STATE = "trigger_restart_framework";
88de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate encryption update progress. */
89de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_UPDATE_PROGRESS = 1;
90de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to cool-down (waste user's time between password attempts) */
91de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_COOLDOWN = 2;
92de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate alerting the user that we are waiting for password entry */
93de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_NOTIFY = 3;
94de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
95de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    // Constants used to control policy.
96ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int MAX_FAILED_ATTEMPTS = 30;
97ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_ATTEMPTS = 10;
98ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
99ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks
1008373b451692a39abdfd12444241c016831e1a4a4David Brown    // Intent action for launching the Emergency Dialer activity.
1018373b451692a39abdfd12444241c016831e1a4a4David Brown    static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
1028373b451692a39abdfd12444241c016831e1a4a4David Brown
10391a2f0566afb91549cbda9289b516154a6467624Ben Komalo    // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
10491a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String EXTRA_FORCE_VIEW =
10591a2f0566afb91549cbda9289b516154a6467624Ben Komalo            "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
10691a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String FORCE_VIEW_PROGRESS = "progress";
10791a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private static final String FORCE_VIEW_ERROR = "error";
108de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final String FORCE_VIEW_PASSWORD = "password";
10991a2f0566afb91549cbda9289b516154a6467624Ben Komalo
110bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
1110e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mValidationComplete;
112d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo    private boolean mValidationRequested;
1130e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
1140e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mEncryptionGoneBad;
115de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** A flag to indicate when the back event should be ignored */
116de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private boolean mIgnoreBack = false;
1171499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private int mCooldown;
1181499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    PowerManager.WakeLock mWakeLock;
11906c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private EditText mPasswordEntry;
120b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    private LockPatternView mLockPatternView;
121d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
122d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private int mNotificationCountdown = 0;
1231499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1241499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
1251499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Used to propagate state through configuration changes (e.g. screen rotation)
1261499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
1271499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private static class NonConfigurationInstanceState {
1281499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        final PowerManager.WakeLock wakelock;
1291499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1301499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
1311499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            wakelock = _wakelock;
1321499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
1331499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
1341499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
135bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
136bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Activity used to fade the screen to black after the password is entered.
137bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
1389f55ae28789754dafeed5c68468d45135d46199eVikram Aggarwal    public static class FadeToBlack extends Activity {
13913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        @Override
14013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        public void onCreate(Bundle savedInstanceState) {
14113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            super.onCreate(savedInstanceState);
14213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            setContentView(R.layout.crypt_keeper_blank);
14313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
144de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        /** Ignore all back events. */
145de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        @Override
146de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        public void onBackPressed() {
147de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            return;
148de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
149f1dbf55819e01f6cf1a99d38a292b15de1262a29Jason parks    }
150ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks
15106c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private class DecryptTask extends AsyncTask<String, Void, Integer> {
15206c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
15306c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected Integer doInBackground(String... params) {
154de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
15506c5ff4a46686d3f599e40df0768177d3d88913cJason parks            try {
15606c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return service.decryptStorage(params[0]);
15706c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } catch (Exception e) {
15806c5ff4a46686d3f599e40df0768177d3d88913cJason parks                Log.e(TAG, "Error while decrypting...", e);
15906c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return -1;
16006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
16106c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
16206c5ff4a46686d3f599e40df0768177d3d88913cJason parks
16306c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
16406c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected void onPostExecute(Integer failedAttempts) {
16506c5ff4a46686d3f599e40df0768177d3d88913cJason parks            if (failedAttempts == 0) {
16606c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // The password was entered successfully. Start the Blank activity
16706c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // so this activity animates to black before the devices starts. Note
16806c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // It has 1 second to complete the animation or it will be frozen
16906c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // until the boot animation comes back up.
170bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
17106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                finish();
17206c5ff4a46686d3f599e40df0768177d3d88913cJason parks                startActivity(intent);
17306c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
17406c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Factory reset the device.
17506c5ff4a46686d3f599e40df0768177d3d88913cJason parks                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
17606c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
17706c5ff4a46686d3f599e40df0768177d3d88913cJason parks                mCooldown = COOL_DOWN_INTERVAL;
17806c5ff4a46686d3f599e40df0768177d3d88913cJason parks                cooldown();
17906c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else {
180bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                final TextView status = (TextView) findViewById(R.id.status);
181bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                status.setText(R.string.try_again);
18206c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Reenable the password entry
183b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                if (mPasswordEntry != null) {
184b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                    mPasswordEntry.setEnabled(true);
185b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                }
18606c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
18706c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
18806c5ff4a46686d3f599e40df0768177d3d88913cJason parks    }
18975c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parks
1900e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
1910e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
1920e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected Boolean doInBackground(Void... params) {
193de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final IMountService service = getMountService();
1940e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            try {
195d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Validating encryption state.");
1960e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                int state = service.getEncryptionState();
1970e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                if (state == IMountService.ENCRYPTION_STATE_NONE) {
1980e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
1990e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    return true; // Unexpected, but fine, I guess...
2000e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                }
2010e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return state == IMountService.ENCRYPTION_STATE_OK;
2020e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            } catch (RemoteException e) {
2030e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Unable to get encryption state properly");
2040e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return true;
2050e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2060e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2070e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
2080e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2090e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected void onPostExecute(Boolean result) {
2100e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            mValidationComplete = true;
2110e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            if (Boolean.FALSE.equals(result)) {
2120e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
2130e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                mEncryptionGoneBad = true;
214d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            } else {
215d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
2160e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
2170e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setupUi();
2180e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
2190e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
2200e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
22191a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private final Handler mHandler = new Handler() {
222ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        @Override
223ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        public void handleMessage(Message msg) {
224ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            switch (msg.what) {
225de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_UPDATE_PROGRESS:
226f8217304073b5cd13823a0602568394be93bf310Jason parks                updateProgress();
227ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
22835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
229de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_COOLDOWN:
230f8217304073b5cd13823a0602568394be93bf310Jason parks                cooldown();
231ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
232de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
233de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_NOTIFY:
234de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                notifyUser();
235de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                break;
236ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
237ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        }
238ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    };
23935933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
240de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private AudioManager mAudioManager;
241bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** The status bar where back/home/recent buttons are shown. */
242bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private StatusBarManager mStatusBar;
243bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
244bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** All the widgets to disable in the status bar */
245bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
246bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ICONS
247bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
248bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_SYSTEM_INFO
249bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_HOME
25066d050bb4b6bf899e363f3e67d1eb5774de744b5Alon Albert            | StatusBarManager.DISABLE_SEARCH
251bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_RECENT;
252de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
25391a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the UI only. */
25491a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView() {
25591a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return getIntent().hasExtra(EXTRA_FORCE_VIEW);
25691a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
25791a2f0566afb91549cbda9289b516154a6467624Ben Komalo
25891a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the specific UI view only. */
25991a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView(String viewType /* non-nullable */) {
26091a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
26191a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
26291a2f0566afb91549cbda9289b516154a6467624Ben Komalo
263de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
264de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Notify the user that we are awaiting input. Currently this sends an audio alert.
265de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
266de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private void notifyUser() {
267d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        if (mNotificationCountdown > 0) {
268d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal            --mNotificationCountdown;
269d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        } else if (mAudioManager != null) {
270de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            try {
271de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // Play the standard keypress sound at full volume. This should be available on
272de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // every device. We cannot play a ringtone here because media services aren't
273de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
274de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // on tablet devices. The idea is to alert the user that something is needed: this
275de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // does not have to be pleasing.
276de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
277de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            } catch (Exception e) {
278de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
279de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
280de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
28186b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        // Notify the user again in 5 seconds.
282de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
28386b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
284de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
285de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
286de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
287de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Ignore back events after the user has entered the decrypt screen and while the device is
288de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * encrypting.
289de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
290de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    @Override
291de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    public void onBackPressed() {
292bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        // In the rare case that something pressed back even though we were disabled.
293de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mIgnoreBack)
294de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            return;
295de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        super.onBackPressed();
296de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
297de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
2988fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
2998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public void onCreate(Bundle savedInstanceState) {
3008fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        super.onCreate(savedInstanceState);
30135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3029597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        // If we are not encrypted or encrypting, get out quickly.
303de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String state = SystemProperties.get("vold.decrypt");
30491a2f0566afb91549cbda9289b516154a6467624Ben Komalo        if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
30535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks            // Disable the crypt keeper.
3068fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            PackageManager pm = getPackageManager();
3078fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            ComponentName name = new ComponentName(this, CryptKeeper.class);
308140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
309140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn                    PackageManager.DONT_KILL_APP);
310140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // Typically CryptKeeper is launched as the home app.  We didn't
311644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // want to be running, so need to finish this activity.  We can count
312644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // on the activity manager re-launching the new home app upon finishing
313644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // this one, since this will leave the activity stack empty.
314140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // NOTE: This is really grungy.  I think it would be better for the
315140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // activity manager to explicitly launch the crypt keeper instead of
316140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // home in the situation where we need to decrypt the device
317140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            finish();
3188fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return;
3198fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
32035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
321b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // Disable the status bar, but do NOT disable back because the user needs a way to go
322b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // from keyboard settings and back to the password screen.
323bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
324bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar.disable(sWidgetsToDisable);
3251499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
326ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        setAirplaneModeIfNecessary();
327de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
3281499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        // Check for (and recover) retained instance data
329de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final Object lastInstance = getLastNonConfigurationInstance();
3301499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        if (lastInstance instanceof NonConfigurationInstanceState) {
3311499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
3321499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            mWakeLock = retained.wakelock;
3330460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
3341499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
335ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
33635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
3379597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    /**
3389597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * Note, we defer the state check and screen setup to onStart() because this will be
3399597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * re-run if the user clicks the power button (sleeping/waking the screen), and this is
3409597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * especially important if we were to lose the wakelock for any reason.
3419597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     */
3429597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    @Override
3439597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    public void onStart() {
3449597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        super.onStart();
345d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        setupUi();
3460e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
3470e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
3480e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /**
3490e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * Initializes the UI based on the current state of encryption.
3500e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * This is idempotent - calling repeatedly will simply re-initialize the UI.
3510e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     */
3520e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private void setupUi() {
3530e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
3540e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setContentView(R.layout.crypt_keeper_progress);
3550e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            showFactoryReset();
3560e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            return;
3570e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
3580e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
359de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String progress = SystemProperties.get("vold.encrypt_progress");
3600e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
3619597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            setContentView(R.layout.crypt_keeper_progress);
3629597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            encryptionProgressInit();
363de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
364b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            final IMountService service = getMountService();
365b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            int type = StorageManager.CRYPT_TYPE_PASSWORD;
366b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            try {
367b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                type = service.getPasswordType();
368b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            } catch (Exception e) {
369b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                Log.e(TAG, "Error while getting type - showing default dialog" + e);
370b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            }
371b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
372b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            if(type == StorageManager.CRYPT_TYPE_PIN) {
373b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                setContentView(R.layout.crypt_keeper_pin_entry);
374b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
375b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                setContentView(R.layout.crypt_keeper_pattern_entry);
376b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            } else {
377b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                setContentView(R.layout.crypt_keeper_password_entry);
378b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            }
3799597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            passwordEntryInit();
380d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        } else if (!mValidationRequested) {
381d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            // We're supposed to be encrypted, but no validation has been done.
382d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            new ValidationTask().execute((Void[]) null);
383d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            mValidationRequested = true;
3849597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        }
3859597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    }
3869597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler
387f8217304073b5cd13823a0602568394be93bf310Jason parks    @Override
388f8217304073b5cd13823a0602568394be93bf310Jason parks    public void onStop() {
389f8217304073b5cd13823a0602568394be93bf310Jason parks        super.onStop();
390de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_COOLDOWN);
391de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
392de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
3931499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
3941499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
3951499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
3961499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Reconfiguring, so propagate the wakelock to the next instance.  This runs between onStop()
3971499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * and onDestroy() and only if we are changing configuration (e.g. rotation).  Also clears
3981499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * mWakeLock so the subsequent call to onDestroy does not release it.
3991499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
4001499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
4011499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public Object onRetainNonConfigurationInstance() {
4021499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
4030460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
4041499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        mWakeLock = null;
4051499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        return state;
4061499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
4071499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
4081499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
4091499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public void onDestroy() {
4101499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        super.onDestroy();
411f8217304073b5cd13823a0602568394be93bf310Jason parks
412f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mWakeLock != null) {
4130460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Releasing and destroying wakelock");
414f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock.release();
415f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock = null;
416f8217304073b5cd13823a0602568394be93bf310Jason parks        }
417f8217304073b5cd13823a0602568394be93bf310Jason parks    }
418f8217304073b5cd13823a0602568394be93bf310Jason parks
419de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
420de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Start encrypting the device.
421de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
422ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private void encryptionProgressInit() {
42335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // Accquire a partial wakelock to prevent the device from sleeping. Note
42435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // we never release this wakelock as we will be restarted after the device
42535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // is encrypted.
4260460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Encryption progress screen initializing.");
4279ee164f44ea1ad66fa16ed702eb885e36ade0d8bBen Komalo        if (mWakeLock == null) {
4280460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Acquiring wakelock.");
4290460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
4300460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
4310460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock.acquire();
4320460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        }
433f8217304073b5cd13823a0602568394be93bf310Jason parks
434bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
435de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Ignore all back presses from now, both hard and soft keys.
436bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        setBackFunctionality(false);
437de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Start the first run of progress manually. This method sets up messages to occur at
438de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // repeated intervals.
439f8217304073b5cd13823a0602568394be93bf310Jason parks        updateProgress();
440f8217304073b5cd13823a0602568394be93bf310Jason parks    }
441f8217304073b5cd13823a0602568394be93bf310Jason parks
44213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    private void showFactoryReset() {
44313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Hide the encryption-bot to make room for the "factory reset" button
44413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        findViewById(R.id.encroid).setVisibility(View.GONE);
44513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
44613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Show the reset button, failure text, and a divider
447bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button button = (Button) findViewById(R.id.factory_reset);
44813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setVisibility(View.VISIBLE);
44913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setOnClickListener(new OnClickListener() {
450bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                @Override
45113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            public void onClick(View v) {
45213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                // Factory reset the device.
45313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
45413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            }
45513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        });
45613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
457bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // Alert the user of the failure.
458bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
459bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
46013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
461bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View view = findViewById(R.id.bottom_divider);
462bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
463f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        if (view != null) {
464f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo            view.setVisibility(View.VISIBLE);
465f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        }
46613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    }
46713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
468f8217304073b5cd13823a0602568394be93bf310Jason parks    private void updateProgress() {
469bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final String state = SystemProperties.get("vold.encrypt_progress");
470f8217304073b5cd13823a0602568394be93bf310Jason parks
4710e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if ("error_partially_encrypted".equals(state)) {
47213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            showFactoryReset();
47313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            return;
47413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
47513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
476f8217304073b5cd13823a0602568394be93bf310Jason parks        int progress = 0;
477f8217304073b5cd13823a0602568394be93bf310Jason parks        try {
47891a2f0566afb91549cbda9289b516154a6467624Ben Komalo            // Force a 50% progress state when debugging the view.
47991a2f0566afb91549cbda9289b516154a6467624Ben Komalo            progress = isDebugView() ? 50 : Integer.parseInt(state);
480f8217304073b5cd13823a0602568394be93bf310Jason parks        } catch (Exception e) {
481f8217304073b5cd13823a0602568394be93bf310Jason parks            Log.w(TAG, "Error parsing progress: " + e.toString());
482f8217304073b5cd13823a0602568394be93bf310Jason parks        }
48335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
484bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final CharSequence status = getText(R.string.crypt_keeper_setup_description);
4850460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.v(TAG, "Encryption progress: " + progress);
486bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView tv = (TextView) findViewById(R.id.status);
487de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (tv != null) {
488de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
489de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
490f8217304073b5cd13823a0602568394be93bf310Jason parks        // Check the progress every 5 seconds
491de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
492de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
493f8217304073b5cd13823a0602568394be93bf310Jason parks    }
494f8217304073b5cd13823a0602568394be93bf310Jason parks
495de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Disable password input for a while to force the user to waste time between retries */
496f8217304073b5cd13823a0602568394be93bf310Jason parks    private void cooldown() {
497bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView status = (TextView) findViewById(R.id.status);
49813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
499f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mCooldown <= 0) {
500de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Re-enable the password entry and back presses.
50106c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(true);
502bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(true);
503f576dd6c9f2470516b2fd8494ad210a77f3c8c1fVikram Aggarwal            status.setText(R.string.enter_password);
504f8217304073b5cd13823a0602568394be93bf310Jason parks        } else {
50513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            CharSequence template = getText(R.string.crypt_keeper_cooldown);
506bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
50713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
508f8217304073b5cd13823a0602568394be93bf310Jason parks            mCooldown--;
509de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.removeMessages(MESSAGE_COOLDOWN);
510de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
511f8217304073b5cd13823a0602568394be93bf310Jason parks        }
512ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
51335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
514bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /**
515bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * Sets the back status: enabled or disabled according to the parameter.
516bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * @param isEnabled true if back is enabled, false otherwise.
517bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     */
518bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private final void setBackFunctionality(boolean isEnabled) {
519bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mIgnoreBack = !isEnabled;
520bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        if (isEnabled) {
521bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable);
522bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        } else {
523bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
524bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        }
525bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    }
526bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
527b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
528b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            new LockPatternView.OnPatternListener() {
529b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
530b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            @Override
531b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            public void onPatternStart() {
532b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            }
533b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
534b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            @Override
535b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            public void onPatternCleared() {
536b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            }
537b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
538b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            @Override
539b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            public void onPatternDetected(List<LockPatternView.Cell> pattern) {
540b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence                new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
541b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            }
542b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
543b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            @Override
544b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            public void onPatternCellAdded(List<Cell> pattern) {
545b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            }
546b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     };
547b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
548b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     private void passwordEntryInit() {
549b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Password/pin case
55006c5ff4a46686d3f599e40df0768177d3d88913cJason parks        mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
551b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mPasswordEntry != null){
552b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnEditorActionListener(this);
553b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.requestFocus();
554b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            // Become quiet when the user interacts with the Edit text screen.
555b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnKeyListener(this);
556b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnTouchListener(this);
557b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.addTextChangedListener(this);
558b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
559b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
560b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Pattern case
561b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
562b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mLockPatternView != null) {
563b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
564b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
5659fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
566c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        // Disable the Emergency call button if the device has no voice telephone capability
567c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
568c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        if (!tm.isVoiceCapable()) {
569c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            final View emergencyCall = findViewById(R.id.emergencyCallButton);
570c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            if (emergencyCall != null) {
571c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                Log.d(TAG, "Removing the emergency Call button");
572c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                emergencyCall.setVisibility(View.GONE);
573c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            }
574c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        }
575c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal
576bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View imeSwitcher = findViewById(R.id.switch_ime_button);
5779fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final InputMethodManager imm = (InputMethodManager) getSystemService(
5789fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                Context.INPUT_METHOD_SERVICE);
5799fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
5809fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setVisibility(View.VISIBLE);
5819fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setOnClickListener(new OnClickListener() {
582bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
5839fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                public void onClick(View v) {
5849fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.showInputMethodPicker();
5859fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
5869fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            });
5879fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        }
5889fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
589de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // We want to keep the screen on while waiting for input. In minimal boot mode, the device
590de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // is completely non-functional, and we want the user to notice the device and enter a
591de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // password.
592de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mWakeLock == null) {
593de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            Log.d(TAG, "Acquiring wakelock.");
594de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
595de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            if (pm != null) {
596de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
597de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock.acquire();
598de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
599de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
6009fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Asynchronously throw up the IME, since there are issues with requesting it to be shown
6019fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // immediately.
602e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence        if (mLockPatternView == null) {
603e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence            mHandler.postDelayed(new Runnable() {
604e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                @Override public void run() {
605e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                    imm.showSoftInputUnchecked(0, null);
606e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence                }
607e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence            }, 0);
608e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence        }
6099fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
6109fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        updateEmergencyCallButtonState();
611d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        // Notify the user in 120 seconds that we are waiting for him to enter the password.
61286b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
613d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
614fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller
615fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        // Dismiss keyguard while this screen is showing.
616fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
6179fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    }
61835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
6199fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    /**
6209fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * Method adapted from com.android.inputmethod.latin.Utils
6219fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     *
6229fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param imm The input method manager
6239fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param shouldIncludeAuxiliarySubtypes
6249fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @return true if we have multiple IMEs to choose from
6259fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     */
6269fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
6279fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final boolean shouldIncludeAuxiliarySubtypes) {
6289fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
6299fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
6309fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Number of the filtered IMEs
6319fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        int filteredImisCount = 0;
6329fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
6339fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        for (InputMethodInfo imi : enabledImis) {
6349fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // We can return true immediately after we find two or more filtered IMEs.
6359fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (filteredImisCount > 1) return true;
6369fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final List<InputMethodSubtype> subtypes =
6379fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.getEnabledInputMethodSubtypeList(imi, true);
6389fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have no subtypes should be counted.
6399fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (subtypes.isEmpty()) {
6409fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
6419fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
6429fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
64335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
6449fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            int auxCount = 0;
6459fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            for (InputMethodSubtype subtype : subtypes) {
6469fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                if (subtype.isAuxiliary()) {
6479fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    ++auxCount;
6489fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
6499fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
6509fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final int nonAuxCount = subtypes.size() - auxCount;
6519fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
6529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have one or more non-auxiliary subtypes should be counted.
6539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
6549fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // subtypes should be counted as well.
6559fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
6569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
6579fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
6589fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
65900046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        }
6608373b451692a39abdfd12444241c016831e1a4a4David Brown
6619fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        return filteredImisCount > 1
6629fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
6639fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // input method subtype (The current IME should be LatinIME.)
6649fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
6658fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
6668fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
6678fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private IMountService getMountService() {
668bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final IBinder service = ServiceManager.getService("mount");
6698fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        if (service != null) {
6708fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return IMountService.Stub.asInterface(service);
6718fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
6728fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return null;
6738fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
6748fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
6758fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
6768fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
67700046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
6788fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Get the password
679bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final String password = v.getText().toString();
6808fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
681ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            if (TextUtils.isEmpty(password)) {
682ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                return true;
683ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
68435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
6858fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Now that we have the password clear the password field.
6868fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            v.setText(null);
6878fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
688de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Disable the password entry and back keypress while checking the password. These
689de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // we either be re-enabled if the password was wrong or after the cooldown period.
69006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(false);
691bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(false);
69206c5ff4a46686d3f599e40df0768177d3d88913cJason parks
6930460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Attempting to send command to decrypt");
69406c5ff4a46686d3f599e40df0768177d3d88913cJason parks            new DecryptTask().execute(password);
69535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
6968fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return true;
6978fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
6988fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return false;
6998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
7008373b451692a39abdfd12444241c016831e1a4a4David Brown
701bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
702ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Set airplane mode on the device if it isn't an LTE device.
703ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
704ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
705ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
706ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * both these problems, we turn the radio off. However, on certain networks turning on and
707ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * off the radio takes a long time. In such cases, we are better off leaving the radio
708ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * running so the latency of an E911 call is short.
709ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * The behavior after this is:
710ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 1. Emergency dialing: the emergency dialer has logic to force the device out of
711ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    airplane mode and restart the radio.
712ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 2. Full boot: we read the persistent settings from the previous boot and restore the
713ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    radio to whatever it was before it restarted. This also happens when rebooting a
714ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    phone that has no encryption.
715ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     */
716ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    private final void setAirplaneModeIfNecessary() {
717ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        final boolean isLteDevice =
7185543404ae0ec9635b59d37fa01074ea598d539ebWink Saville                TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
719ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        if (!isLteDevice) {
720ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            Log.d(TAG, "Going into airplane mode.");
7216a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
722ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
723ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            intent.putExtra("state", true);
7246a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            sendBroadcastAsUser(intent, UserHandle.ALL);
725ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        }
726ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    }
727ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal
728ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    /**
729bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Code to update the state of, and handle clicks from, the "Emergency call" button.
730bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     *
731bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * This code is mostly duplicated from the corresponding code in
732bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
733bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
7348373b451692a39abdfd12444241c016831e1a4a4David Brown    private void updateEmergencyCallButtonState() {
735bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
7368373b451692a39abdfd12444241c016831e1a4a4David Brown        // The button isn't present at all in some configurations.
737bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        if (emergencyCall == null)
738bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            return;
7398373b451692a39abdfd12444241c016831e1a4a4David Brown
7408373b451692a39abdfd12444241c016831e1a4a4David Brown        if (isEmergencyCallCapable()) {
741bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.VISIBLE);
742bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setOnClickListener(new View.OnClickListener() {
743bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
744bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal
7458373b451692a39abdfd12444241c016831e1a4a4David Brown                    public void onClick(View v) {
7468373b451692a39abdfd12444241c016831e1a4a4David Brown                        takeEmergencyCallAction();
7478373b451692a39abdfd12444241c016831e1a4a4David Brown                    }
7488373b451692a39abdfd12444241c016831e1a4a4David Brown                });
7498373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
750bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.GONE);
7518373b451692a39abdfd12444241c016831e1a4a4David Brown            return;
7528373b451692a39abdfd12444241c016831e1a4a4David Brown        }
7538373b451692a39abdfd12444241c016831e1a4a4David Brown
754bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final int newState = TelephonyManager.getDefault().getCallState();
7558373b451692a39abdfd12444241c016831e1a4a4David Brown        int textId;
7568373b451692a39abdfd12444241c016831e1a4a4David Brown        if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
757bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            // Show "return to call" text and show phone icon
7588373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_return_to_call;
759bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final int phoneCallIcon = R.drawable.stat_sys_phone_call;
760bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
7618373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
7628373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_emergency_call;
763bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final int emergencyIcon = R.drawable.ic_emergency;
764bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
7658373b451692a39abdfd12444241c016831e1a4a4David Brown        }
766bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        emergencyCall.setText(textId);
7678373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7688373b451692a39abdfd12444241c016831e1a4a4David Brown
7698373b451692a39abdfd12444241c016831e1a4a4David Brown    private boolean isEmergencyCallCapable() {
7708373b451692a39abdfd12444241c016831e1a4a4David Brown        return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
7718373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7728373b451692a39abdfd12444241c016831e1a4a4David Brown
7738373b451692a39abdfd12444241c016831e1a4a4David Brown    private void takeEmergencyCallAction() {
7748373b451692a39abdfd12444241c016831e1a4a4David Brown        if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
7758373b451692a39abdfd12444241c016831e1a4a4David Brown            resumeCall();
7768373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
7778373b451692a39abdfd12444241c016831e1a4a4David Brown            launchEmergencyDialer();
7788373b451692a39abdfd12444241c016831e1a4a4David Brown        }
7798373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7808373b451692a39abdfd12444241c016831e1a4a4David Brown
7818373b451692a39abdfd12444241c016831e1a4a4David Brown    private void resumeCall() {
782bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
7838373b451692a39abdfd12444241c016831e1a4a4David Brown        if (phone != null) {
7848373b451692a39abdfd12444241c016831e1a4a4David Brown            try {
7858373b451692a39abdfd12444241c016831e1a4a4David Brown                phone.showCallScreen();
7868373b451692a39abdfd12444241c016831e1a4a4David Brown            } catch (RemoteException e) {
7878373b451692a39abdfd12444241c016831e1a4a4David Brown                Log.e(TAG, "Error calling ITelephony service: " + e);
7888373b451692a39abdfd12444241c016831e1a4a4David Brown            }
7898373b451692a39abdfd12444241c016831e1a4a4David Brown        }
7908373b451692a39abdfd12444241c016831e1a4a4David Brown    }
7918373b451692a39abdfd12444241c016831e1a4a4David Brown
7928373b451692a39abdfd12444241c016831e1a4a4David Brown    private void launchEmergencyDialer() {
793bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
7948373b451692a39abdfd12444241c016831e1a4a4David Brown        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
7958373b451692a39abdfd12444241c016831e1a4a4David Brown                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
7968373b451692a39abdfd12444241c016831e1a4a4David Brown        startActivity(intent);
7978373b451692a39abdfd12444241c016831e1a4a4David Brown    }
798d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
799d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /**
800d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     * Listen to key events so we can disable sounds when we get a keyinput in EditText.
801d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     */
802d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private void delayAudioNotification() {
803d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mNotificationCountdown = 20;
804d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
805d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
806d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
807d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onKey(View v, int keyCode, KeyEvent event) {
808d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
809d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
810d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
811d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
812d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
813d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onTouch(View v, MotionEvent event) {
814d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
815d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
816d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
817d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
818d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
819d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
820d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
821d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
822d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
823d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
824d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void onTextChanged(CharSequence s, int start, int before, int count) {
825d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
826d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
827d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
828d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
829d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void afterTextChanged(Editable s) {
830d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
831d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
8328373b451692a39abdfd12444241c016831e1a4a4David Brown}
833