18fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks/*
28fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Copyright (C) 2011 The Android Open Source Project
38fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *
48fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Licensed under the Apache License, Version 2.0 (the "License");
58fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * you may not use this file except in compliance with the License.
68fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * You may obtain a copy of the License at
78fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *
88fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *      http://www.apache.org/licenses/LICENSE-2.0
98fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks *
108fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Unless required by applicable law or agreed to in writing, software
118fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * distributed under the License is distributed on an "AS IS" BASIS,
128fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * See the License for the specific language governing permissions and
148fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * limitations under the License.
158fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks */
168fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
178fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parkspackage com.android.settings;
188fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
198fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.app.Activity;
208fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.app.StatusBarManager;
218fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.ComponentName;
228fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.Context;
23ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.content.Intent;
247ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrenceimport android.content.pm.ActivityInfo;
258fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.pm.PackageManager;
267ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrenceimport android.content.res.Resources.NotFoundException;
27de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwalimport android.media.AudioManager;
2806c5ff4a46686d3f599e40df0768177d3d88913cJason parksimport android.os.AsyncTask;
298fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.Bundle;
30ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Handler;
318fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.IBinder;
32ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Message;
3335933812b16f0b4221280bbcc3c32f6540996bf2Jason parksimport android.os.PowerManager;
348373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.os.RemoteException;
358fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.ServiceManager;
368fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.SystemProperties;
376a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tateimport android.os.UserHandle;
38ee2d592c727a18131a051c1389b1867906e18449Sudheer Shankaimport android.os.storage.IStorageManager;
39b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrenceimport android.os.storage.StorageManager;
406ebbd30e18ca514ca9ee87ffa428c55c9fd92a8cVikram Aggarwalimport android.provider.Settings;
413e71b197f7f85c5045a083ecc532954958807be6Tyler Gunnimport android.telecom.TelecomManager;
428373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.telephony.TelephonyManager;
43d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.Editable;
44ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.text.TextUtils;
45d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.TextWatcher;
46b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrenceimport android.text.format.DateUtils;
478fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.util.Log;
488fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.KeyEvent;
49d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.MotionEvent;
5013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View;
5113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View.OnClickListener;
52d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnKeyListener;
53d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnTouchListener;
5419280af592876d1fe7acd4120eb93390c089641cFyodor Kupolovimport android.view.WindowManager;
558fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.inputmethod.EditorInfo;
569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodInfo;
5775c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parksimport android.view.inputmethod.InputMethodManager;
589fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodSubtype;
5913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.widget.Button;
60ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.ProgressBar;
618fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.widget.TextView;
628fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
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;
6739b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.internal.widget.LockPatternView.DisplayMode;
68f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawaimport com.android.settings.widget.ImeAwareEditText;
699fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
709fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport java.util.List;
7191a2f0566afb91549cbda9289b516154a6467624Ben Komalo
7291a2f0566afb91549cbda9289b516154a6467624Ben Komalo/**
7391a2f0566afb91549cbda9289b516154a6467624Ben Komalo * Settings screens to show the UI flows for encrypting/decrypting the device.
7491a2f0566afb91549cbda9289b516154a6467624Ben Komalo *
7591a2f0566afb91549cbda9289b516154a6467624Ben Komalo * This may be started via adb for debugging the UI layout, without having to go through
7691a2f0566afb91549cbda9289b516154a6467624Ben Komalo * encryption flows everytime. It should be noted that starting the activity in this manner
7791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * is only useful for verifying UI-correctness - the behavior will not be identical.
7891a2f0566afb91549cbda9289b516154a6467624Ben Komalo * <pre>
7991a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell pm enable com.android.settings/.CryptKeeper
8091a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell am start \
8191a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
8291a2f0566afb91549cbda9289b516154a6467624Ben Komalo *     -n com.android.settings/.CryptKeeper
8391a2f0566afb91549cbda9289b516154a6467624Ben Komalo * </pre>
8491a2f0566afb91549cbda9289b516154a6467624Ben Komalo */
85d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalpublic class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
86d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        OnKeyListener, OnTouchListener, TextWatcher {
87ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final String TAG = "CryptKeeper";
8835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
898fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    private static final String DECRYPT_STATE = "trigger_restart_framework";
9051e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley
91de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate encryption update progress. */
92de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private static final int MESSAGE_UPDATE_PROGRESS = 1;
93de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** Message sent to us to indicate alerting the user that we are waiting for password entry */
9451e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley    private static final int MESSAGE_NOTIFY = 2;
95de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
96de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    // Constants used to control policy.
9713b8b564f83c9520358647c54ec5b0f890829d0ePaul Crowley    private static final int MAX_FAILED_ATTEMPTS = 30;
98ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private static final int COOL_DOWN_ATTEMPTS = 10;
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
110278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence    private static final String STATE_COOLDOWN = "cooldown";
111278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence
112bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
1130e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mValidationComplete;
114d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo    private boolean mValidationRequested;
1150e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
1160e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private boolean mEncryptionGoneBad;
11787abbd3ea06a4901a4a967ce01e9fded356ae75fPaul Lawrence    /** If gone bad, should we show encryption failed (false) or corrupt (true)*/
11887abbd3ea06a4901a4a967ce01e9fded356ae75fPaul Lawrence    private boolean mCorrupt;
119de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /** A flag to indicate when the back event should be ignored */
12051e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley    /** When set, blocks unlocking. Set every COOL_DOWN_ATTEMPTS attempts, only cleared
12151e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        by power cycling phone. */
12251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley    private boolean mCooldown = false;
12351e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley
1241499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    PowerManager.WakeLock mWakeLock;
125f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa    private ImeAwareEditText mPasswordEntry;
126b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    private LockPatternView mLockPatternView;
127d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
128d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private int mNotificationCountdown = 0;
12973456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence    /** Number of calls to {@link #notifyUser()} before we release the wakelock */
13073456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence    private int mReleaseWakeLockCountdown = 0;
1315a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence    private int mStatusString = R.string.enter_password;
1321499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
133f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    // how long we wait to clear a wrong pattern
134f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
135f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence
1360f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence    // how long we wait to clear a right pattern
1370f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence    private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
1380f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence
1392555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    // When the user enters a short pin/password, run this to show an error,
1402555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    // but don't count it against attempts.
1412555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    private final Runnable mFakeUnlockAttemptRunnable = new Runnable() {
14251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        @Override
1432555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        public void run() {
1442555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            handleBadAttempt(1 /* failedAttempt */);
1452555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        }
1462555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    };
1472555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
1482555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    // TODO: this should be tuned to match minimum decryption timeout
1492555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    private static final int FAKE_ATTEMPT_DELAY = 1000;
1502555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
1512555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    private final Runnable mClearPatternRunnable = new Runnable() {
152529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley        @Override
153f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void run() {
154f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.clearPattern();
155f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
156f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence    };
157f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence
1581499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
1591499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Used to propagate state through configuration changes (e.g. screen rotation)
1601499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
1611499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    private static class NonConfigurationInstanceState {
1621499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        final PowerManager.WakeLock wakelock;
1631499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
1641499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
1651499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            wakelock = _wakelock;
1661499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
1671499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
1681499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
16906c5ff4a46686d3f599e40df0768177d3d88913cJason parks    private class DecryptTask extends AsyncTask<String, Void, Integer> {
170ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence        private void hide(int id) {
171ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence            View view = findViewById(id);
172ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence            if (view != null) {
173ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence                view.setVisibility(View.GONE);
174ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence            }
175ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence        }
176ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence
17706c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
178529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley        protected void onPreExecute() {
179529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley            super.onPreExecute();
1802555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            beginAttempt();
181529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley        }
182529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley
183529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley        @Override
18406c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected Integer doInBackground(String... params) {
185ee2d592c727a18131a051c1389b1867906e18449Sudheer Shanka            final IStorageManager service = getStorageManager();
18606c5ff4a46686d3f599e40df0768177d3d88913cJason parks            try {
18706c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return service.decryptStorage(params[0]);
18806c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } catch (Exception e) {
18906c5ff4a46686d3f599e40df0768177d3d88913cJason parks                Log.e(TAG, "Error while decrypting...", e);
19006c5ff4a46686d3f599e40df0768177d3d88913cJason parks                return -1;
19106c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
19206c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
19306c5ff4a46686d3f599e40df0768177d3d88913cJason parks
19406c5ff4a46686d3f599e40df0768177d3d88913cJason parks        @Override
19506c5ff4a46686d3f599e40df0768177d3d88913cJason parks        protected void onPostExecute(Integer failedAttempts) {
19606c5ff4a46686d3f599e40df0768177d3d88913cJason parks            if (failedAttempts == 0) {
19789c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence                // The password was entered successfully. Simply do nothing
19889c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence                // and wait for the service restart to switch to surfacefligner
1990f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                if (mLockPatternView != null) {
2000f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    mLockPatternView.removeCallbacks(mClearPatternRunnable);
2010f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
2020f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                }
203529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley                final TextView status = (TextView) findViewById(R.id.status);
204529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley                status.setText(R.string.starting_android);
205ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence                hide(R.id.passwordEntry);
206ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence                hide(R.id.switch_ime_button);
207ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence                hide(R.id.lockPattern);
208ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence                hide(R.id.owner_info);
209ced7db460f472a288255f6f75302381356eb9de8Paul Lawrence                hide(R.id.emergencyCallButton);
21006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
21106c5ff4a46686d3f599e40df0768177d3d88913cJason parks                // Factory reset the device.
2123393daccd9b08c29928e7d71b6f6e1c7388c60b3Lenka Trochtova                Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
2131e99f0ce412db497475f7e220090b9c522be6e79Christopher Tate                intent.setPackage("android");
2141de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
2151de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
2161de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                sendBroadcast(intent);
217384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            } else if (failedAttempts == -1) {
218384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                // Right password, but decryption failed. Tell user bad news ...
219384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                setContentView(R.layout.crypt_keeper_progress);
220384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                showFactoryReset(true);
221384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence                return;
22206c5ff4a46686d3f599e40df0768177d3d88913cJason parks            } else {
2232555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                handleBadAttempt(failedAttempts);
2242555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            }
2252555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        }
2262555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    }
227bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence
2282555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    private void beginAttempt() {
2292555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        final TextView status = (TextView) findViewById(R.id.status);
2302555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        status.setText(R.string.checking_decryption);
2312555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    }
232bd4c3210e1e0d6f58b2a5857032a2dacb316be98Paul Lawrence
2332555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    private void handleBadAttempt(Integer failedAttempts) {
2342555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        // Wrong entry. Handle pattern case.
2352555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        if (mLockPatternView != null) {
2362555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            mLockPatternView.setDisplayMode(DisplayMode.Wrong);
2372555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            mLockPatternView.removeCallbacks(mClearPatternRunnable);
2382555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
2392555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        }
2402555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
24151e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley            mCooldown = true;
24251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley            // No need to setBackFunctionality(false) - it's already done
24351e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley            // at this point.
2442555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            cooldown();
2452555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        } else {
2462555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            final TextView status = (TextView) findViewById(R.id.status);
247529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley
2482555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
2492555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
2502555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
2512555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                CharSequence warning = TextUtils.expandTemplate(warningTemplate,
2522555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                        Integer.toString(remainingAttempts));
2532555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                status.setText(warning);
2542555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            } else {
2552555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
2562555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                try {
257ee2d592c727a18131a051c1389b1867906e18449Sudheer Shanka                    final IStorageManager service = getStorageManager();
2582555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                    passwordType = service.getPasswordType();
2592555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                } catch (Exception e) {
2602555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                    Log.e(TAG, "Error calling mount service " + e);
2612555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                }
2622555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
2632555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
2642555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                    status.setText(R.string.cryptkeeper_wrong_pin);
2652555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
2662555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                    status.setText(R.string.cryptkeeper_wrong_pattern);
2672555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                } else {
2682555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                    status.setText(R.string.cryptkeeper_wrong_password);
2692daf2641d364683c21bcbae08a3358d0f3c6af94Paul Lawrence                }
27006c5ff4a46686d3f599e40df0768177d3d88913cJason parks            }
2712555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
2722555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            if (mLockPatternView != null) {
2732555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                mLockPatternView.setDisplayMode(DisplayMode.Wrong);
2742555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                mLockPatternView.setEnabled(true);
2752555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            }
2762555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
2772555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            // Reenable the password entry
2782555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            if (mPasswordEntry != null) {
2792555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                mPasswordEntry.setEnabled(true);
280f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa                mPasswordEntry.scheduleShowSoftInput();
2812555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                setBackFunctionality(true);
2822555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            }
28306c5ff4a46686d3f599e40df0768177d3d88913cJason parks        }
28406c5ff4a46686d3f599e40df0768177d3d88913cJason parks    }
28575c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parks
2860e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
28787abbd3ea06a4901a4a967ce01e9fded356ae75fPaul Lawrence        int state;
28887abbd3ea06a4901a4a967ce01e9fded356ae75fPaul Lawrence
2890e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
2900e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected Boolean doInBackground(Void... params) {
291ee2d592c727a18131a051c1389b1867906e18449Sudheer Shanka            final IStorageManager service = getStorageManager();
2920e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            try {
293d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Validating encryption state.");
29487abbd3ea06a4901a4a967ce01e9fded356ae75fPaul Lawrence                state = service.getEncryptionState();
2951d8da1d51aaa87084c5c0d323615c9d369a7a8b8Sudheer Shanka                if (state == StorageManager.ENCRYPTION_STATE_NONE) {
2960e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
2970e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                    return true; // Unexpected, but fine, I guess...
2980e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                }
2991d8da1d51aaa87084c5c0d323615c9d369a7a8b8Sudheer Shanka                return state == StorageManager.ENCRYPTION_STATE_OK;
3000e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            } catch (RemoteException e) {
3010e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Unable to get encryption state properly");
3020e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                return true;
3030e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
3040e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
3050e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
3060e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        @Override
3070e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        protected void onPostExecute(Boolean result) {
3080e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            mValidationComplete = true;
3090e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            if (Boolean.FALSE.equals(result)) {
3100e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
3110e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo                mEncryptionGoneBad = true;
3121d8da1d51aaa87084c5c0d323615c9d369a7a8b8Sudheer Shanka                mCorrupt = state == StorageManager.ENCRYPTION_STATE_ERROR_CORRUPT;
313d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            } else {
314d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo                Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
3150e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            }
3160e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setupUi();
3170e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
3180e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
3190e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
32091a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private final Handler mHandler = new Handler() {
321ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        @Override
322ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        public void handleMessage(Message msg) {
323ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            switch (msg.what) {
324de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_UPDATE_PROGRESS:
325f8217304073b5cd13823a0602568394be93bf310Jason parks                updateProgress();
326ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                break;
32735933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
328de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            case MESSAGE_NOTIFY:
329de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                notifyUser();
330de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                break;
331ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
332ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks        }
333ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    };
33435933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
335de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private AudioManager mAudioManager;
336bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** The status bar where back/home/recent buttons are shown. */
337bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private StatusBarManager mStatusBar;
338bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
339bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /** All the widgets to disable in the status bar */
340bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
341bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ICONS
342bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
343bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_HOME
34466d050bb4b6bf899e363f3e67d1eb5774de744b5Alon Albert            | StatusBarManager.DISABLE_SEARCH
345bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            | StatusBarManager.DISABLE_RECENT;
346de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
3472555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    protected static final int MIN_LENGTH_BEFORE_REPORT = LockPatternUtils.MIN_LOCK_PATTERN_SIZE;
3482555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
34991a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the UI only. */
35091a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView() {
35191a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return getIntent().hasExtra(EXTRA_FORCE_VIEW);
35291a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
35391a2f0566afb91549cbda9289b516154a6467624Ben Komalo
35491a2f0566afb91549cbda9289b516154a6467624Ben Komalo    /** @return whether or not this Activity was started for debugging the specific UI view only. */
35591a2f0566afb91549cbda9289b516154a6467624Ben Komalo    private boolean isDebugView(String viewType /* non-nullable */) {
35691a2f0566afb91549cbda9289b516154a6467624Ben Komalo        return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
35791a2f0566afb91549cbda9289b516154a6467624Ben Komalo    }
35891a2f0566afb91549cbda9289b516154a6467624Ben Komalo
359de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
360de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Notify the user that we are awaiting input. Currently this sends an audio alert.
361de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
362de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    private void notifyUser() {
363d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        if (mNotificationCountdown > 0) {
364d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal            --mNotificationCountdown;
365d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        } else if (mAudioManager != null) {
366de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            try {
367de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // Play the standard keypress sound at full volume. This should be available on
368de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // every device. We cannot play a ringtone here because media services aren't
369de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
370de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // on tablet devices. The idea is to alert the user that something is needed: this
371de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                // does not have to be pleasing.
372de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
373de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            } catch (Exception e) {
374de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
375de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
376de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
37786b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        // Notify the user again in 5 seconds.
378de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
37986b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
38073456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence
38173456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence        if (mWakeLock.isHeld()) {
38273456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            if (mReleaseWakeLockCountdown > 0) {
38373456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                --mReleaseWakeLockCountdown;
38473456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            } else {
38573456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                mWakeLock.release();
38673456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence            }
38773456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence        }
388de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
389de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
390de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
3912a6552ed502cfa60fec71a9d41a11faa1e4be963Paul Lawrence     * Ignore back events from this activity always - there's nowhere to go back
3922a6552ed502cfa60fec71a9d41a11faa1e4be963Paul Lawrence     * to
393de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
394de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    @Override
395de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    public void onBackPressed() {
396de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    }
397de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal
3988fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
3998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public void onCreate(Bundle savedInstanceState) {
4008fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        super.onCreate(savedInstanceState);
40135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
4029597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        // If we are not encrypted or encrypting, get out quickly.
403de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String state = SystemProperties.get("vold.decrypt");
40491a2f0566afb91549cbda9289b516154a6467624Ben Komalo        if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
4051b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov            disableCryptKeeperComponent(this);
406140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // Typically CryptKeeper is launched as the home app.  We didn't
407644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // want to be running, so need to finish this activity.  We can count
408644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // on the activity manager re-launching the new home app upon finishing
409644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn            // this one, since this will leave the activity stack empty.
410140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // NOTE: This is really grungy.  I think it would be better for the
411140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // activity manager to explicitly launch the crypt keeper instead of
412140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            // home in the situation where we need to decrypt the device
413140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn            finish();
4148fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return;
4158fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
41635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
4177ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        try {
4187ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence            if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
4197ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4207ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence            }
4217ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        } catch (NotFoundException e) {
4227ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence        }
4237ae20e3ce98543888cfccbef6b1254d060bc30caPaul Lawrence
424b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // Disable the status bar, but do NOT disable back because the user needs a way to go
425b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal        // from keyboard settings and back to the password screen.
426bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
427bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        mStatusBar.disable(sWidgetsToDisable);
4281499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
429278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence        if (savedInstanceState != null) {
430278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence            mCooldown = savedInstanceState.getBoolean(STATE_COOLDOWN);
431278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence        }
432278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence
433ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        setAirplaneModeIfNecessary();
434de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
4351499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        // Check for (and recover) retained instance data
436de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final Object lastInstance = getLastNonConfigurationInstance();
4371499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        if (lastInstance instanceof NonConfigurationInstanceState) {
4381499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
4391499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler            mWakeLock = retained.wakelock;
4400460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
4411499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        }
442ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
44335933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
444278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence    @Override
445278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence    public void  onSaveInstanceState(Bundle savedInstanceState) {
446278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence        savedInstanceState.putBoolean(STATE_COOLDOWN, mCooldown);
447278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence    }
448278410435ab39c77b496bf1ecf14c1703ec6c41ePaul Lawrence
4499597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    /**
4509597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * Note, we defer the state check and screen setup to onStart() because this will be
4519597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * re-run if the user clicks the power button (sleeping/waking the screen), and this is
4529597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     * especially important if we were to lose the wakelock for any reason.
4539597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler     */
4549597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    @Override
4559597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    public void onStart() {
4569597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        super.onStart();
457d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        setupUi();
4580e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    }
4590e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
4600e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    /**
4610e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * Initializes the UI based on the current state of encryption.
4620e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     * This is idempotent - calling repeatedly will simply re-initialize the UI.
4630e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo     */
4640e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo    private void setupUi() {
4650e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
4660e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            setContentView(R.layout.crypt_keeper_progress);
46787abbd3ea06a4901a4a967ce01e9fded356ae75fPaul Lawrence            showFactoryReset(mCorrupt);
4680e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo            return;
4690e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        }
4700e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo
471de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        final String progress = SystemProperties.get("vold.encrypt_progress");
4720e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
4739597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            setContentView(R.layout.crypt_keeper_progress);
4749597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler            encryptionProgressInit();
475de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
4769ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence            new AsyncTask<Void, Void, Void>() {
477529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley                int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
4789ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                String owner_info;
4790f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                boolean pattern_visible;
480c04420ce2581627478b59cc5d8b453f1fa329e02Paul Lawrence                boolean password_visible;
4819ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4829ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                @Override
4839ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                public Void doInBackground(Void... v) {
4849ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    try {
485ee2d592c727a18131a051c1389b1867906e18449Sudheer Shanka                        final IStorageManager service = getStorageManager();
486529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley                        passwordType = service.getPasswordType();
487f9d6d2916323472f6c543edcc3956a8d00925e6bElliott Hughes                        owner_info = service.getField(StorageManager.OWNER_INFO_KEY);
488f9d6d2916323472f6c543edcc3956a8d00925e6bElliott Hughes                        pattern_visible = !("0".equals(service.getField(StorageManager.PATTERN_VISIBLE_KEY)));
489c04420ce2581627478b59cc5d8b453f1fa329e02Paul Lawrence                        password_visible = !("0".equals(service.getField(StorageManager.PASSWORD_VISIBLE_KEY)));
4909ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } catch (Exception e) {
4919ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        Log.e(TAG, "Error calling mount service " + e);
4929ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    }
4939ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
4949ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    return null;
4959ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                }
496b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
4979ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                @Override
4989ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                public void onPostExecute(java.lang.Void v) {
499c04420ce2581627478b59cc5d8b453f1fa329e02Paul Lawrence                    Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
500c04420ce2581627478b59cc5d8b453f1fa329e02Paul Lawrence                                  password_visible ? 1 : 0);
501c04420ce2581627478b59cc5d8b453f1fa329e02Paul Lawrence
50251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley                    if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
5039ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_pin_entry);
5045a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_pin;
505529834dae1ce469d64f7e191db43315e88691e7ePaul Crowley                    } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
5069ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_pattern_entry);
5079ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setBackFunctionality(false);
5085a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_pattern;
5099ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    } else {
5109ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                        setContentView(R.layout.crypt_keeper_password_entry);
5115a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        mStatusString = R.string.enter_password;
5129ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    }
5135a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    final TextView status = (TextView) findViewById(R.id.status);
5145a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    status.setText(mStatusString);
5155a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
5165a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
5175a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    ownerInfo.setText(owner_info);
5185a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    ownerInfo.setSelected(true); // Required for marquee'ing to work
5199ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence
5209ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                    passwordEntryInit();
5215a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence
5222a6552ed502cfa60fec71a9d41a11faa1e4be963Paul Lawrence                    findViewById(android.R.id.content).setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
5232a6552ed502cfa60fec71a9d41a11faa1e4be963Paul Lawrence
5240f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    if (mLockPatternView != null) {
5250f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                        mLockPatternView.setInStealthMode(!pattern_visible);
5260f11e15f503d65d5a085157d8ec406a72d712ce7Paul Lawrence                    }
52751e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley                    if (mCooldown) {
52851e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley                        // in case we are cooling down and coming back from emergency dialler
5295a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                        setBackFunctionality(false);
53051e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley                        cooldown();
5315a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                    }
53251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley
5339ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence                }
5349ac2d81d9fd155240e02284894148c6dcbe486ecPaul Lawrence            }.execute();
535d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo        } else if (!mValidationRequested) {
536d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            // We're supposed to be encrypted, but no validation has been done.
537d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            new ValidationTask().execute((Void[]) null);
538d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo            mValidationRequested = true;
5399597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler        }
5409597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler    }
5419597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler
542f8217304073b5cd13823a0602568394be93bf310Jason parks    @Override
543f8217304073b5cd13823a0602568394be93bf310Jason parks    public void onStop() {
544f8217304073b5cd13823a0602568394be93bf310Jason parks        super.onStop();
545de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
546de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
5471499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
5481499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
5491499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    /**
5501499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * Reconfiguring, so propagate the wakelock to the next instance.  This runs between onStop()
5511499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * and onDestroy() and only if we are changing configuration (e.g. rotation).  Also clears
5521499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     * mWakeLock so the subsequent call to onDestroy does not release it.
5531499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler     */
5541499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
5551499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public Object onRetainNonConfigurationInstance() {
5561499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
5570460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
5581499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        mWakeLock = null;
5591499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        return state;
5601499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    }
5611499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler
5621499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    @Override
5631499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler    public void onDestroy() {
5641499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler        super.onDestroy();
565f8217304073b5cd13823a0602568394be93bf310Jason parks
566f8217304073b5cd13823a0602568394be93bf310Jason parks        if (mWakeLock != null) {
5670460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Releasing and destroying wakelock");
568f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock.release();
569f8217304073b5cd13823a0602568394be93bf310Jason parks            mWakeLock = null;
570f8217304073b5cd13823a0602568394be93bf310Jason parks        }
571f8217304073b5cd13823a0602568394be93bf310Jason parks    }
572f8217304073b5cd13823a0602568394be93bf310Jason parks
573de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal    /**
574de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     * Start encrypting the device.
575de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal     */
576ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    private void encryptionProgressInit() {
57735933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // Accquire a partial wakelock to prevent the device from sleeping. Note
57835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // we never release this wakelock as we will be restarted after the device
57935933812b16f0b4221280bbcc3c32f6540996bf2Jason parks        // is encrypted.
5800460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.d(TAG, "Encryption progress screen initializing.");
5819ee164f44ea1ad66fa16ed702eb885e36ade0d8bBen Komalo        if (mWakeLock == null) {
5820460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            Log.d(TAG, "Acquiring wakelock.");
5830460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
5840460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
5850460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo            mWakeLock.acquire();
5860460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        }
587f8217304073b5cd13823a0602568394be93bf310Jason parks
588bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
589de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Ignore all back presses from now, both hard and soft keys.
590bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        setBackFunctionality(false);
591de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // Start the first run of progress manually. This method sets up messages to occur at
592de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // repeated intervals.
593f8217304073b5cd13823a0602568394be93bf310Jason parks        updateProgress();
594f8217304073b5cd13823a0602568394be93bf310Jason parks    }
595f8217304073b5cd13823a0602568394be93bf310Jason parks
596384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence    /**
597384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     * Show factory reset screen allowing the user to reset their phone when
598384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     * there is nothing else we can do
599384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     * @param corrupt true if userdata is corrupt, false if encryption failed
600384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     *        partway through
601384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence     */
6021de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey    private void showFactoryReset(final boolean corrupt) {
60313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Hide the encryption-bot to make room for the "factory reset" button
60413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        findViewById(R.id.encroid).setVisibility(View.GONE);
60513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
60613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        // Show the reset button, failure text, and a divider
607bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button button = (Button) findViewById(R.id.factory_reset);
60813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setVisibility(View.VISIBLE);
60913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        button.setOnClickListener(new OnClickListener() {
610bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                @Override
61113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            public void onClick(View v) {
61213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler                // Factory reset the device.
6133393daccd9b08c29928e7d71b6f6e1c7388c60b3Lenka Trochtova                Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
6141e99f0ce412db497475f7e220090b9c522be6e79Christopher Tate                intent.setPackage("android");
6151de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
6161de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                intent.putExtra(Intent.EXTRA_REASON,
6171de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                        "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
6181de688d911cf96eff39eddc5a7070e3c44efefc1Jeff Sharkey                sendBroadcast(intent);
61913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            }
62013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        });
62113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
622bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // Alert the user of the failure.
623384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence        if (corrupt) {
624384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
625384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
626384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence        } else {
627384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
628384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
629384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence        }
63013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
631bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View view = findViewById(R.id.bottom_divider);
632bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
633f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        if (view != null) {
634f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo            view.setVisibility(View.VISIBLE);
635f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo        }
63613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler    }
63713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
638f8217304073b5cd13823a0602568394be93bf310Jason parks    private void updateProgress() {
639bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final String state = SystemProperties.get("vold.encrypt_progress");
640f8217304073b5cd13823a0602568394be93bf310Jason parks
6410e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo        if ("error_partially_encrypted".equals(state)) {
642384d8e52b6750b2be7079f8828e4d2f038e22180Paul Lawrence            showFactoryReset(false);
64313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler            return;
64413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler        }
64513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler
646b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Get status as percentage first
647b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        CharSequence status = getText(R.string.crypt_keeper_setup_description);
648b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        int percent = 0;
649f8217304073b5cd13823a0602568394be93bf310Jason parks        try {
65091a2f0566afb91549cbda9289b516154a6467624Ben Komalo            // Force a 50% progress state when debugging the view.
651b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            percent = isDebugView() ? 50 : Integer.parseInt(state);
652f8217304073b5cd13823a0602568394be93bf310Jason parks        } catch (Exception e) {
653f8217304073b5cd13823a0602568394be93bf310Jason parks            Log.w(TAG, "Error parsing progress: " + e.toString());
654f8217304073b5cd13823a0602568394be93bf310Jason parks        }
655b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        String progress = Integer.toString(percent);
65635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
657b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Now try to get status as time remaining and replace as appropriate
6580460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo        Log.v(TAG, "Encryption progress: " + progress);
659b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        try {
660b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
661b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            int time = Integer.parseInt(timeProperty);
662b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            if (time >= 0) {
663b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                // Round up to multiple of 10 - this way display is less jerky
664b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                time = (time + 9) / 10 * 10;
665b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                progress = DateUtils.formatElapsedTime(time);
666b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence                status = getText(R.string.crypt_keeper_setup_time_remaining);
667b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            }
668b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        } catch (Exception e) {
669b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            // Will happen if no time etc - show percentage
670b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        }
671b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence
672bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final TextView tv = (TextView) findViewById(R.id.status);
673de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (tv != null) {
674b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence            tv.setText(TextUtils.expandTemplate(status, progress));
675de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
676b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence
677b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        // Check the progress every 1 seconds
678de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
679b15c68fbf19c021eb0a3bed1ccc4dd7fda384ec7Paul Lawrence        mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
680f8217304073b5cd13823a0602568394be93bf310Jason parks    }
681f8217304073b5cd13823a0602568394be93bf310Jason parks
68251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley    /** Insist on a power cycle to force the user to waste time between retries.
68351e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley     *
68451e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley     * Call setBackFunctionality(false) before calling this. */
685f8217304073b5cd13823a0602568394be93bf310Jason parks    private void cooldown() {
68651e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        // Disable the password entry.
68751e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        if (mPasswordEntry != null) {
68851e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley            mPasswordEntry.setEnabled(false);
689f8217304073b5cd13823a0602568394be93bf310Jason parks        }
69051e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        if (mLockPatternView != null) {
69151e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley            mLockPatternView.setEnabled(false);
69251e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        }
69351e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley
69451e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        final TextView status = (TextView) findViewById(R.id.status);
69551e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        status.setText(R.string.crypt_keeper_force_power_cycle);
696ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks    }
69735933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
698bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    /**
699bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * Sets the back status: enabled or disabled according to the parameter.
700bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     * @param isEnabled true if back is enabled, false otherwise.
701bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal     */
702bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    private final void setBackFunctionality(boolean isEnabled) {
703bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        if (isEnabled) {
704bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable);
705bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        } else {
706bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
707bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal        }
708bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal    }
709bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal
7102555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    private void fakeUnlockAttempt(View postingView) {
7112555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        beginAttempt();
7122555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley        postingView.postDelayed(mFakeUnlockAttemptRunnable, FAKE_ATTEMPT_DELAY);
7132555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley    }
7142555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley
715b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence    protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
716f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        new LockPatternView.OnPatternListener() {
717b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
718f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
719f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternStart() {
720f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.removeCallbacks(mClearPatternRunnable);
721f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
722b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
723f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
724f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternCleared() {
725f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
726b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
727f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
728f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternDetected(List<LockPatternView.Cell> pattern) {
729f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence            mLockPatternView.setEnabled(false);
7302555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            if (pattern.size() >= MIN_LENGTH_BEFORE_REPORT) {
7312555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
7322555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            } else {
7332555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                // Allow user to make as many of these as they want.
7342555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                fakeUnlockAttempt(mLockPatternView);
7352555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            }
736f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
737b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
738f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        @Override
739f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        public void onPatternCellAdded(List<Cell> pattern) {
740f6cda3b176d097f8360b5c34c6ba746cd10b4004Paul Lawrence        }
741b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     };
742b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
743b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence     private void passwordEntryInit() {
744b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Password/pin case
745f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa        mPasswordEntry = (ImeAwareEditText) findViewById(R.id.passwordEntry);
746b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mPasswordEntry != null){
747b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnEditorActionListener(this);
748b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.requestFocus();
749b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            // Become quiet when the user interacts with the Edit text screen.
750b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnKeyListener(this);
751b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.setOnTouchListener(this);
752b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mPasswordEntry.addTextChangedListener(this);
753b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
754b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence
755b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        // Pattern case
756b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
757b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        if (mLockPatternView != null) {
758b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence            mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
759b05f39d186d8ce1b53bb4da3cb4c2cd22986f513Paul Lawrence        }
7609fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
761c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        // Disable the Emergency call button if the device has no voice telephone capability
7623afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon        if (!getTelephonyManager().isVoiceCapable()) {
763c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            final View emergencyCall = findViewById(R.id.emergencyCallButton);
764c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            if (emergencyCall != null) {
765c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                Log.d(TAG, "Removing the emergency Call button");
766c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal                emergencyCall.setVisibility(View.GONE);
767c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal            }
768c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal        }
769c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal
770bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final View imeSwitcher = findViewById(R.id.switch_ime_button);
7719fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final InputMethodManager imm = (InputMethodManager) getSystemService(
7729fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                Context.INPUT_METHOD_SERVICE);
7739fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
7749fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setVisibility(View.VISIBLE);
7759fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            imeSwitcher.setOnClickListener(new OnClickListener() {
776bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
7779fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                public void onClick(View v) {
778e792729b4ca60632b43c9e1ee6126fb5e031deaeSeigo Nonaka                    imm.showInputMethodPicker(false /* showAuxiliarySubtypes */);
7799fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
7809fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            });
7819fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        }
7829fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
783de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // We want to keep the screen on while waiting for input. In minimal boot mode, the device
784de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // is completely non-functional, and we want the user to notice the device and enter a
785de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        // password.
786de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        if (mWakeLock == null) {
787de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            Log.d(TAG, "Acquiring wakelock.");
788de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
789de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            if (pm != null) {
790de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
791de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal                mWakeLock.acquire();
79273456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                // Keep awake for 10 minutes - if the user hasn't been alerted by then
79373456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence                // best not to just drain their battery
7945a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence                mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
795de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            }
796de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal        }
79773456acfb45beb7a95b5aee5a1f7566d3b803a86Paul Lawrence
798f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa        // Make sure that the IME is shown when everything becomes ready.
79951e0811a7ac7ec5e31c67a466bb450d80ffaa24aPaul Crowley        if (mLockPatternView == null && !mCooldown) {
800259775314ae834cdc6b3f511c7079d67d5285083Seigo Nonaka            getWindow().setSoftInputMode(
801259775314ae834cdc6b3f511c7079d67d5285083Seigo Nonaka                                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
802f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa            if (mPasswordEntry != null) {
803f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa                mPasswordEntry.scheduleShowSoftInput();
804f6af093e2dfd156768a3937e94d405c04c541feaYohei Yukawa            }
805e54e9323ed423d68f0567dcf05f45c62187ad8ddPaul Lawrence        }
8069fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
8079fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        updateEmergencyCallButtonState();
808d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        // Notify the user in 120 seconds that we are waiting for him to enter the password.
80986b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal        mHandler.removeMessages(MESSAGE_NOTIFY);
810d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
811fb3d5cad48876dbd366fd8b562529d355d2428dbJim Miller
8122c526515775559b34a311400435de28d85b9c79eJohn Spurlock        // Dismiss secure & non-secure keyguards while this screen is showing.
8132c526515775559b34a311400435de28d85b9c79eJohn Spurlock        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
8142c526515775559b34a311400435de28d85b9c79eJohn Spurlock                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
8159fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    }
81635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8179fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    /**
8189fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * Method adapted from com.android.inputmethod.latin.Utils
8199fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     *
8209fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param imm The input method manager
8219fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @param shouldIncludeAuxiliarySubtypes
8229fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     * @return true if we have multiple IMEs to choose from
8239fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo     */
8249fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo    private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
8259fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final boolean shouldIncludeAuxiliarySubtypes) {
8269fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
8279fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
8289fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // Number of the filtered IMEs
8299fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        int filteredImisCount = 0;
8309fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
8319fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        for (InputMethodInfo imi : enabledImis) {
8329fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // We can return true immediately after we find two or more filtered IMEs.
8339fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (filteredImisCount > 1) return true;
8349fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final List<InputMethodSubtype> subtypes =
8359fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    imm.getEnabledInputMethodSubtypeList(imi, true);
8369fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have no subtypes should be counted.
8379fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (subtypes.isEmpty()) {
8389fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
8399fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
8409fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
84135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8429fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            int auxCount = 0;
8439fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            for (InputMethodSubtype subtype : subtypes) {
8449fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                if (subtype.isAuxiliary()) {
8459fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                    ++auxCount;
8469fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                }
8479fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
8489fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            final int nonAuxCount = subtypes.size() - auxCount;
8499fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo
8509fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // IMEs that have one or more non-auxiliary subtypes should be counted.
8519fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
8529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            // subtypes should be counted as well.
8539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
8549fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                ++filteredImisCount;
8559fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                continue;
8569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo            }
85700046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        }
8588373b451692a39abdfd12444241c016831e1a4a4David Brown
8599fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        return filteredImisCount > 1
8609fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
8619fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo        // input method subtype (The current IME should be LatinIME.)
8629fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
8638fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
8648fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
865ee2d592c727a18131a051c1389b1867906e18449Sudheer Shanka    private IStorageManager getStorageManager() {
866bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final IBinder service = ServiceManager.getService("mount");
8678fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        if (service != null) {
868ee2d592c727a18131a051c1389b1867906e18449Sudheer Shanka            return IStorageManager.Stub.asInterface(service);
8698fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
8708fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return null;
8718fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
8728fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
8738fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    @Override
8748fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
87500046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks        if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
8768fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Get the password
877bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            final String password = v.getText().toString();
8788fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
879ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            if (TextUtils.isEmpty(password)) {
880ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks                return true;
881ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks            }
88235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8838fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            // Now that we have the password clear the password field.
8848fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            v.setText(null);
8858fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks
886de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // Disable the password entry and back keypress while checking the password. These
887de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal            // we either be re-enabled if the password was wrong or after the cooldown period.
88806c5ff4a46686d3f599e40df0768177d3d88913cJason parks            mPasswordEntry.setEnabled(false);
889bf459dafdc5d97d8f2103c1d2a9cb65f5096e4f8Vikram Aggarwal            setBackFunctionality(false);
89006c5ff4a46686d3f599e40df0768177d3d88913cJason parks
891233d88a277b5806fd33a5aad50c1014e7af4bd7aAndrew Zeng            if (password.length() >= LockPatternUtils.MIN_LOCK_PASSWORD_SIZE) {
8922555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                new DecryptTask().execute(password);
8932555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            } else {
8942555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                // Allow user to make as many of these as they want.
8952555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley                fakeUnlockAttempt(mPasswordEntry);
8962555e5f1040984a3422c5a4db43421e87d4ca0cePaul Crowley            }
89735933812b16f0b4221280bbcc3c32f6540996bf2Jason parks
8988fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks            return true;
8998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        }
9008fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks        return false;
9018fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks    }
9028373b451692a39abdfd12444241c016831e1a4a4David Brown
903bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal    /**
904ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Set airplane mode on the device if it isn't an LTE device.
905ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
906ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
907ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
908ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * both these problems, we turn the radio off. However, on certain networks turning on and
909ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * off the radio takes a long time. In such cases, we are better off leaving the radio
910ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * running so the latency of an E911 call is short.
911ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * The behavior after this is:
912ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 1. Emergency dialing: the emergency dialer has logic to force the device out of
913ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    airplane mode and restart the radio.
914ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     * 2. Full boot: we read the persistent settings from the previous boot and restore the
915ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    radio to whatever it was before it restarted. This also happens when rebooting a
916ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     *    phone that has no encryption.
917ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal     */
918ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    private final void setAirplaneModeIfNecessary() {
919ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        final boolean isLteDevice =
9203afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon                getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
921ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        if (!isLteDevice) {
922ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            Log.d(TAG, "Going into airplane mode.");
9236a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
924ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
925ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal            intent.putExtra("state", true);
9266a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate            sendBroadcastAsUser(intent, UserHandle.ALL);
927ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal        }
928ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    }
929ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal
930ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal    /**
931bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * Code to update the state of, and handle clicks from, the "Emergency call" button.
932bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     *
933bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * This code is mostly duplicated from the corresponding code in
934bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
935bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal     */
9368373b451692a39abdfd12444241c016831e1a4a4David Brown    private void updateEmergencyCallButtonState() {
937bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
9388373b451692a39abdfd12444241c016831e1a4a4David Brown        // The button isn't present at all in some configurations.
939bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        if (emergencyCall == null)
940bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            return;
9418373b451692a39abdfd12444241c016831e1a4a4David Brown
9428373b451692a39abdfd12444241c016831e1a4a4David Brown        if (isEmergencyCallCapable()) {
943bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.VISIBLE);
944bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setOnClickListener(new View.OnClickListener() {
945bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal                    @Override
946bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal
9478373b451692a39abdfd12444241c016831e1a4a4David Brown                    public void onClick(View v) {
9488373b451692a39abdfd12444241c016831e1a4a4David Brown                        takeEmergencyCallAction();
9498373b451692a39abdfd12444241c016831e1a4a4David Brown                    }
9508373b451692a39abdfd12444241c016831e1a4a4David Brown                });
9518373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
952bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal            emergencyCall.setVisibility(View.GONE);
9538373b451692a39abdfd12444241c016831e1a4a4David Brown            return;
9548373b451692a39abdfd12444241c016831e1a4a4David Brown        }
9558373b451692a39abdfd12444241c016831e1a4a4David Brown
9568373b451692a39abdfd12444241c016831e1a4a4David Brown        int textId;
9573e71b197f7f85c5045a083ecc532954958807be6Tyler Gunn        if (getTelecomManager().isInCall()) {
95889c7570dd42ce6945ce8e092892a22ec79c30c82Paul Lawrence            // Show "return to call"
9598373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_return_to_call;
9608373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
9618373b451692a39abdfd12444241c016831e1a4a4David Brown            textId = R.string.cryptkeeper_emergency_call;
9628373b451692a39abdfd12444241c016831e1a4a4David Brown        }
963bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        emergencyCall.setText(textId);
9648373b451692a39abdfd12444241c016831e1a4a4David Brown    }
9658373b451692a39abdfd12444241c016831e1a4a4David Brown
9668373b451692a39abdfd12444241c016831e1a4a4David Brown    private boolean isEmergencyCallCapable() {
9678373b451692a39abdfd12444241c016831e1a4a4David Brown        return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
9688373b451692a39abdfd12444241c016831e1a4a4David Brown    }
9698373b451692a39abdfd12444241c016831e1a4a4David Brown
9708373b451692a39abdfd12444241c016831e1a4a4David Brown    private void takeEmergencyCallAction() {
9713e71b197f7f85c5045a083ecc532954958807be6Tyler Gunn        TelecomManager telecomManager = getTelecomManager();
9723e71b197f7f85c5045a083ecc532954958807be6Tyler Gunn        if (telecomManager.isInCall()) {
9733e71b197f7f85c5045a083ecc532954958807be6Tyler Gunn            telecomManager.showInCallScreen(false /* showDialpad */);
9748373b451692a39abdfd12444241c016831e1a4a4David Brown        } else {
9758373b451692a39abdfd12444241c016831e1a4a4David Brown            launchEmergencyDialer();
9768373b451692a39abdfd12444241c016831e1a4a4David Brown        }
9778373b451692a39abdfd12444241c016831e1a4a4David Brown    }
9788373b451692a39abdfd12444241c016831e1a4a4David Brown
9798373b451692a39abdfd12444241c016831e1a4a4David Brown
9808373b451692a39abdfd12444241c016831e1a4a4David Brown    private void launchEmergencyDialer() {
981bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal        final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
9828373b451692a39abdfd12444241c016831e1a4a4David Brown        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
9838373b451692a39abdfd12444241c016831e1a4a4David Brown                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
9845a70f05e0d86443cd97b186d2a5101eb4c7eed81Paul Lawrence        setBackFunctionality(true);
9858373b451692a39abdfd12444241c016831e1a4a4David Brown        startActivity(intent);
9868373b451692a39abdfd12444241c016831e1a4a4David Brown    }
987d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
9883afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon    private TelephonyManager getTelephonyManager() {
9893afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon        return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
9903afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon    }
9913afbdf006057c93f930694e26306a3cc9cea34e4Santos Cordon
9923e71b197f7f85c5045a083ecc532954958807be6Tyler Gunn    private TelecomManager getTelecomManager() {
9933e71b197f7f85c5045a083ecc532954958807be6Tyler Gunn        return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
99435230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon    }
99535230e9ed76de518c7ea46d9277bdc8fa765efd8Santos Cordon
996d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    /**
997d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     * Listen to key events so we can disable sounds when we get a keyinput in EditText.
998d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal     */
999d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    private void delayAudioNotification() {
1000d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        mNotificationCountdown = 20;
1001d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
1002d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
1003d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
1004d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onKey(View v, int keyCode, KeyEvent event) {
1005d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
1006d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
1007d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
1008d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
1009d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
1010d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public boolean onTouch(View v, MotionEvent event) {
1011d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
1012d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return false;
1013d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
1014d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
1015d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
1016d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
1017d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
1018d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
1019d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
1020d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
1021d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void onTextChanged(CharSequence s, int start, int before, int count) {
1022d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        delayAudioNotification();
1023d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
1024d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal
1025d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    @Override
1026d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    public void afterTextChanged(Editable s) {
1027d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal        return;
1028d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal    }
10291b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov
10301b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov    private static void disableCryptKeeperComponent(Context context) {
10311b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov        PackageManager pm = context.getPackageManager();
10321b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov        ComponentName name = new ComponentName(context, CryptKeeper.class);
10331b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov        Log.d(TAG, "Disabling component " + name);
1034ae4a557a3f7b85b5c2ccfd275387cd3280c9a73aPaul Lawrence        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
1035ae4a557a3f7b85b5c2ccfd275387cd3280c9a73aPaul Lawrence                PackageManager.DONT_KILL_APP);
10361b5cc427f06019936e40ee8e43ae931b8752eb11Fyodor Kupolov    }
10378373b451692a39abdfd12444241c016831e1a4a4David Brown}
1038