CryptKeeper.java revision 6a5929b086a61a955a4a2f43f5fa251e8754ff4a
18fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks/* 28fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Copyright (C) 2011 The Android Open Source Project 38fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * 48fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Licensed under the Apache License, Version 2.0 (the "License"); 58fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * you may not use this file except in compliance with the License. 68fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * You may obtain a copy of the License at 78fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * 88fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * http://www.apache.org/licenses/LICENSE-2.0 98fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * 108fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * Unless required by applicable law or agreed to in writing, software 118fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * distributed under the License is distributed on an "AS IS" BASIS, 128fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * See the License for the specific language governing permissions and 148fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks * limitations under the License. 158fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks */ 168fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 178fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parkspackage com.android.settings; 188fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 198fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.app.Activity; 208fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.app.StatusBarManager; 218fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.ComponentName; 228fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.Context; 23ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.content.Intent; 248fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.content.pm.PackageManager; 25de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwalimport android.media.AudioManager; 2606c5ff4a46686d3f599e40df0768177d3d88913cJason parksimport android.os.AsyncTask; 278fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.Bundle; 28ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Handler; 298fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.IBinder; 30ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.os.Message; 3135933812b16f0b4221280bbcc3c32f6540996bf2Jason parksimport android.os.PowerManager; 328373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.os.RemoteException; 338fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.ServiceManager; 348fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.SystemProperties; 356a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tateimport android.os.UserHandle; 368fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.os.storage.IMountService; 376ebbd30e18ca514ca9ee87ffa428c55c9fd92a8cVikram Aggarwalimport android.provider.Settings; 388373b451692a39abdfd12444241c016831e1a4a4David Brownimport android.telephony.TelephonyManager; 39d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.Editable; 40ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.text.TextUtils; 41d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.text.TextWatcher; 428fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.util.Log; 438fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.KeyEvent; 44d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.MotionEvent; 4513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View; 4613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.view.View.OnClickListener; 47d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnKeyListener; 48d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalimport android.view.View.OnTouchListener; 498fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.view.inputmethod.EditorInfo; 509fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodInfo; 5175c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parksimport android.view.inputmethod.InputMethodManager; 529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport android.view.inputmethod.InputMethodSubtype; 5313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadlerimport android.widget.Button; 54ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.EditText; 55ec5a45e79cb7161adfabf475342c1e1b25994276Jason parksimport android.widget.ProgressBar; 568fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parksimport android.widget.TextView; 578fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 5891a2f0566afb91549cbda9289b516154a6467624Ben Komaloimport com.android.internal.telephony.ITelephony; 59ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwalimport com.android.internal.telephony.Phone; 605543404ae0ec9635b59d37fa01074ea598d539ebWink Savilleimport com.android.internal.telephony.PhoneConstants; 619fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 629fcb6a7fac02aef85085c2db616556257e1642f5Ben Komaloimport java.util.List; 6391a2f0566afb91549cbda9289b516154a6467624Ben Komalo 6491a2f0566afb91549cbda9289b516154a6467624Ben Komalo/** 6591a2f0566afb91549cbda9289b516154a6467624Ben Komalo * Settings screens to show the UI flows for encrypting/decrypting the device. 6691a2f0566afb91549cbda9289b516154a6467624Ben Komalo * 6791a2f0566afb91549cbda9289b516154a6467624Ben Komalo * This may be started via adb for debugging the UI layout, without having to go through 6891a2f0566afb91549cbda9289b516154a6467624Ben Komalo * encryption flows everytime. It should be noted that starting the activity in this manner 6991a2f0566afb91549cbda9289b516154a6467624Ben Komalo * is only useful for verifying UI-correctness - the behavior will not be identical. 7091a2f0566afb91549cbda9289b516154a6467624Ben Komalo * <pre> 7191a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell pm enable com.android.settings/.CryptKeeper 7291a2f0566afb91549cbda9289b516154a6467624Ben Komalo * $ adb shell am start \ 7391a2f0566afb91549cbda9289b516154a6467624Ben Komalo * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \ 7491a2f0566afb91549cbda9289b516154a6467624Ben Komalo * -n com.android.settings/.CryptKeeper 7591a2f0566afb91549cbda9289b516154a6467624Ben Komalo * </pre> 7691a2f0566afb91549cbda9289b516154a6467624Ben Komalo */ 77d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwalpublic class CryptKeeper extends Activity implements TextView.OnEditorActionListener, 78d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal OnKeyListener, OnTouchListener, TextWatcher { 79ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks private static final String TAG = "CryptKeeper"; 8035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 818fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks private static final String DECRYPT_STATE = "trigger_restart_framework"; 82de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** Message sent to us to indicate encryption update progress. */ 83de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private static final int MESSAGE_UPDATE_PROGRESS = 1; 84de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** Message sent to us to cool-down (waste user's time between password attempts) */ 85de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private static final int MESSAGE_COOLDOWN = 2; 86de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** Message sent to us to indicate alerting the user that we are waiting for password entry */ 87de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private static final int MESSAGE_NOTIFY = 3; 88de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal 89de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // Constants used to control policy. 90ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks private static final int MAX_FAILED_ATTEMPTS = 30; 91ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks private static final int COOL_DOWN_ATTEMPTS = 10; 92ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds 93ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks 948373b451692a39abdfd12444241c016831e1a4a4David Brown // Intent action for launching the Emergency Dialer activity. 958373b451692a39abdfd12444241c016831e1a4a4David Brown static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; 968373b451692a39abdfd12444241c016831e1a4a4David Brown 9791a2f0566afb91549cbda9289b516154a6467624Ben Komalo // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts 9891a2f0566afb91549cbda9289b516154a6467624Ben Komalo private static final String EXTRA_FORCE_VIEW = 9991a2f0566afb91549cbda9289b516154a6467624Ben Komalo "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW"; 10091a2f0566afb91549cbda9289b516154a6467624Ben Komalo private static final String FORCE_VIEW_PROGRESS = "progress"; 10191a2f0566afb91549cbda9289b516154a6467624Ben Komalo private static final String FORCE_VIEW_ERROR = "error"; 102de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private static final String FORCE_VIEW_PASSWORD = "password"; 10391a2f0566afb91549cbda9289b516154a6467624Ben Komalo 104bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal /** When encryption is detected, this flag indicates whether or not we've checked for errors. */ 1050e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo private boolean mValidationComplete; 106d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo private boolean mValidationRequested; 1070e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */ 1080e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo private boolean mEncryptionGoneBad; 109de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** A flag to indicate when the back event should be ignored */ 110de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private boolean mIgnoreBack = false; 1111499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler private int mCooldown; 1121499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler PowerManager.WakeLock mWakeLock; 11306c5ff4a46686d3f599e40df0768177d3d88913cJason parks private EditText mPasswordEntry; 114d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal /** Number of calls to {@link #notifyUser()} to ignore before notifying. */ 115d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal private int mNotificationCountdown = 0; 1161499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler 1171499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler /** 1181499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler * Used to propagate state through configuration changes (e.g. screen rotation) 1191499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler */ 1201499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler private static class NonConfigurationInstanceState { 1211499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler final PowerManager.WakeLock wakelock; 1221499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler 1231499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) { 1241499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler wakelock = _wakelock; 1251499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler } 1261499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler } 1271499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler 128bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal /** 129bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal * Activity used to fade the screen to black after the password is entered. 130bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal */ 1319f55ae28789754dafeed5c68468d45135d46199eVikram Aggarwal public static class FadeToBlack extends Activity { 13213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler @Override 13313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler public void onCreate(Bundle savedInstanceState) { 13413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler super.onCreate(savedInstanceState); 13513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler setContentView(R.layout.crypt_keeper_blank); 13613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler } 137de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** Ignore all back events. */ 138de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal @Override 139de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal public void onBackPressed() { 140de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal return; 141de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 142f1dbf55819e01f6cf1a99d38a292b15de1262a29Jason parks } 143ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks 14406c5ff4a46686d3f599e40df0768177d3d88913cJason parks private class DecryptTask extends AsyncTask<String, Void, Integer> { 14506c5ff4a46686d3f599e40df0768177d3d88913cJason parks @Override 14606c5ff4a46686d3f599e40df0768177d3d88913cJason parks protected Integer doInBackground(String... params) { 147de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal final IMountService service = getMountService(); 14806c5ff4a46686d3f599e40df0768177d3d88913cJason parks try { 14906c5ff4a46686d3f599e40df0768177d3d88913cJason parks return service.decryptStorage(params[0]); 15006c5ff4a46686d3f599e40df0768177d3d88913cJason parks } catch (Exception e) { 15106c5ff4a46686d3f599e40df0768177d3d88913cJason parks Log.e(TAG, "Error while decrypting...", e); 15206c5ff4a46686d3f599e40df0768177d3d88913cJason parks return -1; 15306c5ff4a46686d3f599e40df0768177d3d88913cJason parks } 15406c5ff4a46686d3f599e40df0768177d3d88913cJason parks } 15506c5ff4a46686d3f599e40df0768177d3d88913cJason parks 15606c5ff4a46686d3f599e40df0768177d3d88913cJason parks @Override 15706c5ff4a46686d3f599e40df0768177d3d88913cJason parks protected void onPostExecute(Integer failedAttempts) { 15806c5ff4a46686d3f599e40df0768177d3d88913cJason parks if (failedAttempts == 0) { 15906c5ff4a46686d3f599e40df0768177d3d88913cJason parks // The password was entered successfully. Start the Blank activity 16006c5ff4a46686d3f599e40df0768177d3d88913cJason parks // so this activity animates to black before the devices starts. Note 16106c5ff4a46686d3f599e40df0768177d3d88913cJason parks // It has 1 second to complete the animation or it will be frozen 16206c5ff4a46686d3f599e40df0768177d3d88913cJason parks // until the boot animation comes back up. 163bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class); 16406c5ff4a46686d3f599e40df0768177d3d88913cJason parks finish(); 16506c5ff4a46686d3f599e40df0768177d3d88913cJason parks startActivity(intent); 16606c5ff4a46686d3f599e40df0768177d3d88913cJason parks } else if (failedAttempts == MAX_FAILED_ATTEMPTS) { 16706c5ff4a46686d3f599e40df0768177d3d88913cJason parks // Factory reset the device. 16806c5ff4a46686d3f599e40df0768177d3d88913cJason parks sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); 16906c5ff4a46686d3f599e40df0768177d3d88913cJason parks } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) { 17006c5ff4a46686d3f599e40df0768177d3d88913cJason parks mCooldown = COOL_DOWN_INTERVAL; 17106c5ff4a46686d3f599e40df0768177d3d88913cJason parks cooldown(); 17206c5ff4a46686d3f599e40df0768177d3d88913cJason parks } else { 173bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final TextView status = (TextView) findViewById(R.id.status); 174bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal status.setText(R.string.try_again); 17506c5ff4a46686d3f599e40df0768177d3d88913cJason parks // Reenable the password entry 17606c5ff4a46686d3f599e40df0768177d3d88913cJason parks mPasswordEntry.setEnabled(true); 17706c5ff4a46686d3f599e40df0768177d3d88913cJason parks } 17806c5ff4a46686d3f599e40df0768177d3d88913cJason parks } 17906c5ff4a46686d3f599e40df0768177d3d88913cJason parks } 18075c085ee890744cdd4b90c72f8b50e6aeeb31e88Jason parks 1810e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo private class ValidationTask extends AsyncTask<Void, Void, Boolean> { 1820e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo @Override 1830e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo protected Boolean doInBackground(Void... params) { 184de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal final IMountService service = getMountService(); 1850e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo try { 186d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo Log.d(TAG, "Validating encryption state."); 1870e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo int state = service.getEncryptionState(); 1880e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo if (state == IMountService.ENCRYPTION_STATE_NONE) { 1890e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption."); 1900e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo return true; // Unexpected, but fine, I guess... 1910e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 1920e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo return state == IMountService.ENCRYPTION_STATE_OK; 1930e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } catch (RemoteException e) { 1940e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo Log.w(TAG, "Unable to get encryption state properly"); 1950e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo return true; 1960e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 1970e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 1980e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo 1990e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo @Override 2000e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo protected void onPostExecute(Boolean result) { 2010e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo mValidationComplete = true; 2020e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo if (Boolean.FALSE.equals(result)) { 2030e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe."); 2040e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo mEncryptionGoneBad = true; 205d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo } else { 206d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo Log.d(TAG, "Encryption state validated. Proceeding to configure UI"); 2070e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 2080e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo setupUi(); 2090e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 2100e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 2110e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo 21291a2f0566afb91549cbda9289b516154a6467624Ben Komalo private final Handler mHandler = new Handler() { 213ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks @Override 214ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks public void handleMessage(Message msg) { 215ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks switch (msg.what) { 216de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal case MESSAGE_UPDATE_PROGRESS: 217f8217304073b5cd13823a0602568394be93bf310Jason parks updateProgress(); 218ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks break; 21935933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 220de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal case MESSAGE_COOLDOWN: 221f8217304073b5cd13823a0602568394be93bf310Jason parks cooldown(); 222ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks break; 223de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal 224de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal case MESSAGE_NOTIFY: 225de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal notifyUser(); 226de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal break; 227ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks } 228ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks } 229ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks }; 23035933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 231de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private AudioManager mAudioManager; 232de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal 23391a2f0566afb91549cbda9289b516154a6467624Ben Komalo /** @return whether or not this Activity was started for debugging the UI only. */ 23491a2f0566afb91549cbda9289b516154a6467624Ben Komalo private boolean isDebugView() { 23591a2f0566afb91549cbda9289b516154a6467624Ben Komalo return getIntent().hasExtra(EXTRA_FORCE_VIEW); 23691a2f0566afb91549cbda9289b516154a6467624Ben Komalo } 23791a2f0566afb91549cbda9289b516154a6467624Ben Komalo 23891a2f0566afb91549cbda9289b516154a6467624Ben Komalo /** @return whether or not this Activity was started for debugging the specific UI view only. */ 23991a2f0566afb91549cbda9289b516154a6467624Ben Komalo private boolean isDebugView(String viewType /* non-nullable */) { 24091a2f0566afb91549cbda9289b516154a6467624Ben Komalo return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW)); 24191a2f0566afb91549cbda9289b516154a6467624Ben Komalo } 24291a2f0566afb91549cbda9289b516154a6467624Ben Komalo 243de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** 244de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal * Notify the user that we are awaiting input. Currently this sends an audio alert. 245de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal */ 246de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal private void notifyUser() { 247d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal if (mNotificationCountdown > 0) { 248d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal Log.d(TAG, "Counting down to notify user..." + mNotificationCountdown); 249d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal --mNotificationCountdown; 250d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } else if (mAudioManager != null) { 251d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal Log.d(TAG, "Notifying user that we are waiting for input..."); 252de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal try { 253de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // Play the standard keypress sound at full volume. This should be available on 254de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // every device. We cannot play a ringtone here because media services aren't 255de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // available yet. A DTMF-style tone is too soft to be noticed, and might not exist 256de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // on tablet devices. The idea is to alert the user that something is needed: this 257de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // does not have to be pleasing. 258de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100); 259de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } catch (Exception e) { 260de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal Log.w(TAG, "notifyUser: Exception while playing sound: " + e); 261de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 262de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 26386b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal // Notify the user again in 5 seconds. 264de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.removeMessages(MESSAGE_NOTIFY); 26586b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000); 266de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 267de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal 268de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** 269de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal * Ignore back events after the user has entered the decrypt screen and while the device is 270de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal * encrypting. 271de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal */ 272de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal @Override 273de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal public void onBackPressed() { 274de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal if (mIgnoreBack) 275de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal return; 276de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal super.onBackPressed(); 277de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 278de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal 2798fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks @Override 2808fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks public void onCreate(Bundle savedInstanceState) { 2818fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks super.onCreate(savedInstanceState); 28235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 2839597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler // If we are not encrypted or encrypting, get out quickly. 284de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal final String state = SystemProperties.get("vold.decrypt"); 28591a2f0566afb91549cbda9289b516154a6467624Ben Komalo if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) { 28635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks // Disable the crypt keeper. 2878fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks PackageManager pm = getPackageManager(); 2888fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks ComponentName name = new ComponentName(this, CryptKeeper.class); 289140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 290140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn PackageManager.DONT_KILL_APP); 291140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn // Typically CryptKeeper is launched as the home app. We didn't 292644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn // want to be running, so need to finish this activity. We can count 293644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn // on the activity manager re-launching the new home app upon finishing 294644fa4243b4c288c85845b3f86aab25135635729Dianne Hackborn // this one, since this will leave the activity stack empty. 295140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn // NOTE: This is really grungy. I think it would be better for the 296140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn // activity manager to explicitly launch the crypt keeper instead of 297140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn // home in the situation where we need to decrypt the device 298140f6c6cf705f020c0ee6a2843cf1691cc68e729Dianne Hackborn finish(); 2998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks return; 3008fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks } 30135933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 302b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal // Disable the status bar, but do NOT disable back because the user needs a way to go 303b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal // from keyboard settings and back to the password screen. 30439f1e04079976d3d10fdd8e5dd36c833bab8a371Jason parks StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); 30513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler sbm.disable(StatusBarManager.DISABLE_EXPAND 30613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler | StatusBarManager.DISABLE_NOTIFICATION_ICONS 30739f1e04079976d3d10fdd8e5dd36c833bab8a371Jason parks | StatusBarManager.DISABLE_NOTIFICATION_ALERTS 30813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler | StatusBarManager.DISABLE_SYSTEM_INFO 3094d2bfd149118c8b82827d76d1d110fd5c65294c8Daniel Sandler | StatusBarManager.DISABLE_HOME 310b96b35a62b1f092b6322643c3568f3e712543e53Vikram Aggarwal | StatusBarManager.DISABLE_RECENT); 3111499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler 312ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal setAirplaneModeIfNecessary(); 313de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 3141499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler // Check for (and recover) retained instance data 315de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal final Object lastInstance = getLastNonConfigurationInstance(); 3161499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler if (lastInstance instanceof NonConfigurationInstanceState) { 3171499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance; 3181499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler mWakeLock = retained.wakelock; 3190460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState"); 3201499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler } 321ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks } 32235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 3239597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler /** 3249597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler * Note, we defer the state check and screen setup to onStart() because this will be 3259597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler * re-run if the user clicks the power button (sleeping/waking the screen), and this is 3269597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler * especially important if we were to lose the wakelock for any reason. 3279597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler */ 3289597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler @Override 3299597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler public void onStart() { 3309597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler super.onStart(); 331d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo setupUi(); 3320e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 3330e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo 3340e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo /** 3350e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo * Initializes the UI based on the current state of encryption. 3360e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo * This is idempotent - calling repeatedly will simply re-initialize the UI. 3370e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo */ 3380e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo private void setupUi() { 3390e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) { 3400e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo setContentView(R.layout.crypt_keeper_progress); 3410e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo showFactoryReset(); 3420e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo return; 3430e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo } 3440e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo 345de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal final String progress = SystemProperties.get("vold.encrypt_progress"); 3460e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) { 3479597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler setContentView(R.layout.crypt_keeper_progress); 3489597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler encryptionProgressInit(); 349de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) { 3509597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler setContentView(R.layout.crypt_keeper_password_entry); 3519597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler passwordEntryInit(); 352d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo } else if (!mValidationRequested) { 353d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo // We're supposed to be encrypted, but no validation has been done. 354d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo new ValidationTask().execute((Void[]) null); 355d4758efef6d6564d7b4b0b747d0a9ae9b44a38c7Ben Komalo mValidationRequested = true; 3569597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler } 3579597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler } 3589597406aa1b83867b03a47d1a5162969b924f2fcAndy Stadler 359f8217304073b5cd13823a0602568394be93bf310Jason parks @Override 360f8217304073b5cd13823a0602568394be93bf310Jason parks public void onStop() { 361f8217304073b5cd13823a0602568394be93bf310Jason parks super.onStop(); 362de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.removeMessages(MESSAGE_COOLDOWN); 363de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); 364de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.removeMessages(MESSAGE_NOTIFY); 3651499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler } 3661499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler 3671499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler /** 3681499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop() 3691499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears 3701499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler * mWakeLock so the subsequent call to onDestroy does not release it. 3711499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler */ 3721499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler @Override 3731499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler public Object onRetainNonConfigurationInstance() { 3741499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock); 3750460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState"); 3761499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler mWakeLock = null; 3771499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler return state; 3781499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler } 3791499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler 3801499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler @Override 3811499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler public void onDestroy() { 3821499740c13af5fbd5766a87825c3e789b37bab24Andy Stadler super.onDestroy(); 383f8217304073b5cd13823a0602568394be93bf310Jason parks 384f8217304073b5cd13823a0602568394be93bf310Jason parks if (mWakeLock != null) { 3850460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.d(TAG, "Releasing and destroying wakelock"); 386f8217304073b5cd13823a0602568394be93bf310Jason parks mWakeLock.release(); 387f8217304073b5cd13823a0602568394be93bf310Jason parks mWakeLock = null; 388f8217304073b5cd13823a0602568394be93bf310Jason parks } 389f8217304073b5cd13823a0602568394be93bf310Jason parks } 390f8217304073b5cd13823a0602568394be93bf310Jason parks 391de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** 392de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal * Start encrypting the device. 393de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal */ 394ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks private void encryptionProgressInit() { 39535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks // Accquire a partial wakelock to prevent the device from sleeping. Note 39635933812b16f0b4221280bbcc3c32f6540996bf2Jason parks // we never release this wakelock as we will be restarted after the device 39735933812b16f0b4221280bbcc3c32f6540996bf2Jason parks // is encrypted. 3980460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.d(TAG, "Encryption progress screen initializing."); 3999ee164f44ea1ad66fa16ed702eb885e36ade0d8bBen Komalo if (mWakeLock == null) { 4000460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.d(TAG, "Acquiring wakelock."); 4010460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 4020460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); 4030460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo mWakeLock.acquire(); 4040460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo } 405f8217304073b5cd13823a0602568394be93bf310Jason parks 406bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true); 407de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // Ignore all back presses from now, both hard and soft keys. 408de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mIgnoreBack = true; 409de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // Start the first run of progress manually. This method sets up messages to occur at 410de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // repeated intervals. 411f8217304073b5cd13823a0602568394be93bf310Jason parks updateProgress(); 412f8217304073b5cd13823a0602568394be93bf310Jason parks } 413f8217304073b5cd13823a0602568394be93bf310Jason parks 41413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler private void showFactoryReset() { 41513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler // Hide the encryption-bot to make room for the "factory reset" button 41613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler findViewById(R.id.encroid).setVisibility(View.GONE); 41713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 41813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler // Show the reset button, failure text, and a divider 419bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final Button button = (Button) findViewById(R.id.factory_reset); 42013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler button.setVisibility(View.VISIBLE); 42113d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler button.setOnClickListener(new OnClickListener() { 422bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal @Override 42313d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler public void onClick(View v) { 42413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler // Factory reset the device. 42513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); 42613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler } 42713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler }); 42813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 429bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal // Alert the user of the failure. 430bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title); 431bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary); 43213d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 433bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final View view = findViewById(R.id.bottom_divider); 434bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate. 435f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo if (view != null) { 436f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo view.setVisibility(View.VISIBLE); 437f0104df823a93ef4c3336118ddb02a0924e520daBen Komalo } 43813d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler } 43913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 440f8217304073b5cd13823a0602568394be93bf310Jason parks private void updateProgress() { 441bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final String state = SystemProperties.get("vold.encrypt_progress"); 442f8217304073b5cd13823a0602568394be93bf310Jason parks 4430e66609c95047ba7e5c0408bdc88af4315fd35b1Ben Komalo if ("error_partially_encrypted".equals(state)) { 44413d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler showFactoryReset(); 44513d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler return; 44613d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler } 44713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 448f8217304073b5cd13823a0602568394be93bf310Jason parks int progress = 0; 449f8217304073b5cd13823a0602568394be93bf310Jason parks try { 45091a2f0566afb91549cbda9289b516154a6467624Ben Komalo // Force a 50% progress state when debugging the view. 45191a2f0566afb91549cbda9289b516154a6467624Ben Komalo progress = isDebugView() ? 50 : Integer.parseInt(state); 452f8217304073b5cd13823a0602568394be93bf310Jason parks } catch (Exception e) { 453f8217304073b5cd13823a0602568394be93bf310Jason parks Log.w(TAG, "Error parsing progress: " + e.toString()); 454f8217304073b5cd13823a0602568394be93bf310Jason parks } 45535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 456bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final CharSequence status = getText(R.string.crypt_keeper_setup_description); 4570460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.v(TAG, "Encryption progress: " + progress); 458bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final TextView tv = (TextView) findViewById(R.id.status); 459de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal if (tv != null) { 460de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress))); 461de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 462f8217304073b5cd13823a0602568394be93bf310Jason parks // Check the progress every 5 seconds 463de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); 464de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000); 465f8217304073b5cd13823a0602568394be93bf310Jason parks } 466f8217304073b5cd13823a0602568394be93bf310Jason parks 467de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal /** Disable password input for a while to force the user to waste time between retries */ 468f8217304073b5cd13823a0602568394be93bf310Jason parks private void cooldown() { 469bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final TextView status = (TextView) findViewById(R.id.status); 47013d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 471f8217304073b5cd13823a0602568394be93bf310Jason parks if (mCooldown <= 0) { 472de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // Re-enable the password entry and back presses. 47306c5ff4a46686d3f599e40df0768177d3d88913cJason parks mPasswordEntry.setEnabled(true); 474de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mIgnoreBack = false; 475f576dd6c9f2470516b2fd8494ad210a77f3c8c1fVikram Aggarwal status.setText(R.string.enter_password); 476f8217304073b5cd13823a0602568394be93bf310Jason parks } else { 47713d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler CharSequence template = getText(R.string.crypt_keeper_cooldown); 478bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown))); 47913d62049f7c96c47356f23eaee7f0997612fdf9fAndy Stadler 480f8217304073b5cd13823a0602568394be93bf310Jason parks mCooldown--; 481de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.removeMessages(MESSAGE_COOLDOWN); 482de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second 483f8217304073b5cd13823a0602568394be93bf310Jason parks } 484ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks } 48535933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 486ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks private void passwordEntryInit() { 48706c5ff4a46686d3f599e40df0768177d3d88913cJason parks mPasswordEntry = (EditText) findViewById(R.id.passwordEntry); 48806c5ff4a46686d3f599e40df0768177d3d88913cJason parks mPasswordEntry.setOnEditorActionListener(this); 4899fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo mPasswordEntry.requestFocus(); 490d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal // Become quiet when the user interacts with the Edit text screen. 491d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal mPasswordEntry.setOnKeyListener(this); 492d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal mPasswordEntry.setOnTouchListener(this); 493d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal mPasswordEntry.addTextChangedListener(this); 4949fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 495c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal // Disable the Emergency call button if the device has no voice telephone capability 496c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 497c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal if (!tm.isVoiceCapable()) { 498c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal final View emergencyCall = findViewById(R.id.emergencyCallButton); 499c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal if (emergencyCall != null) { 500c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal Log.d(TAG, "Removing the emergency Call button"); 501c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal emergencyCall.setVisibility(View.GONE); 502c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal } 503c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal } 504c62d321e8dcb023e8c7299802a75a271b7ca98d9Vikram Aggarwal 505bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final View imeSwitcher = findViewById(R.id.switch_ime_button); 5069fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo final InputMethodManager imm = (InputMethodManager) getSystemService( 5079fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo Context.INPUT_METHOD_SERVICE); 5089fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) { 5099fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo imeSwitcher.setVisibility(View.VISIBLE); 5109fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo imeSwitcher.setOnClickListener(new OnClickListener() { 511bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal @Override 5129fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo public void onClick(View v) { 5139fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo imm.showInputMethodPicker(); 5149fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 5159fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo }); 5169fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 5179fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 518de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // We want to keep the screen on while waiting for input. In minimal boot mode, the device 519de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // is completely non-functional, and we want the user to notice the device and enter a 520de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // password. 521de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal if (mWakeLock == null) { 522de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal Log.d(TAG, "Acquiring wakelock."); 523de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 524de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal if (pm != null) { 525de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); 526de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mWakeLock.acquire(); 527de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 528de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal } 5299fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // Asynchronously throw up the IME, since there are issues with requesting it to be shown 5309fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // immediately. 5319fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo mHandler.postDelayed(new Runnable() { 5329fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo @Override public void run() { 5339fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo imm.showSoftInputUnchecked(0, null); 5349fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 5359fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo }, 0); 5369fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 5379fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo updateEmergencyCallButtonState(); 538d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal // Notify the user in 120 seconds that we are waiting for him to enter the password. 53986b93937fa417647d22ee15d3a0aff7a751be17aVikram Aggarwal mHandler.removeMessages(MESSAGE_NOTIFY); 540d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000); 5419fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 54235933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 5439fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo /** 5449fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo * Method adapted from com.android.inputmethod.latin.Utils 5459fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo * 5469fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo * @param imm The input method manager 5479fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo * @param shouldIncludeAuxiliarySubtypes 5489fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo * @return true if we have multiple IMEs to choose from 5499fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo */ 5509fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm, 5519fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo final boolean shouldIncludeAuxiliarySubtypes) { 5529fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); 5539fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 5549fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // Number of the filtered IMEs 5559fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo int filteredImisCount = 0; 5569fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 5579fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo for (InputMethodInfo imi : enabledImis) { 5589fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // We can return true immediately after we find two or more filtered IMEs. 5599fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo if (filteredImisCount > 1) return true; 5609fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo final List<InputMethodSubtype> subtypes = 5619fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo imm.getEnabledInputMethodSubtypeList(imi, true); 5629fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // IMEs that have no subtypes should be counted. 5639fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo if (subtypes.isEmpty()) { 5649fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo ++filteredImisCount; 5659fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo continue; 5669fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 56735933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 5689fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo int auxCount = 0; 5699fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo for (InputMethodSubtype subtype : subtypes) { 5709fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo if (subtype.isAuxiliary()) { 5719fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo ++auxCount; 5729fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 5739fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 5749fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo final int nonAuxCount = subtypes.size() - auxCount; 5759fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo 5769fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // IMEs that have one or more non-auxiliary subtypes should be counted. 5779fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary 5789fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // subtypes should be counted as well. 5799fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { 5809fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo ++filteredImisCount; 5819fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo continue; 5829fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo } 58300046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks } 5848373b451692a39abdfd12444241c016831e1a4a4David Brown 5859fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo return filteredImisCount > 1 5869fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled 5879fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo // input method subtype (The current IME should be LatinIME.) 5889fcb6a7fac02aef85085c2db616556257e1642f5Ben Komalo || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; 5898fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks } 5908fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 5918fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks private IMountService getMountService() { 592bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final IBinder service = ServiceManager.getService("mount"); 5938fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks if (service != null) { 5948fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks return IMountService.Stub.asInterface(service); 5958fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks } 5968fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks return null; 5978fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks } 5988fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 5998fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks @Override 6008fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 60100046d6e176eb05c3e3b0d13d38a8da7491bba5eJason parks if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) { 6028fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks // Get the password 603bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final String password = v.getText().toString(); 6048fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 605ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks if (TextUtils.isEmpty(password)) { 606ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks return true; 607ec5a45e79cb7161adfabf475342c1e1b25994276Jason parks } 60835933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 6098fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks // Now that we have the password clear the password field. 6108fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks v.setText(null); 6118fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks 612de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // Disable the password entry and back keypress while checking the password. These 613de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal // we either be re-enabled if the password was wrong or after the cooldown period. 61406c5ff4a46686d3f599e40df0768177d3d88913cJason parks mPasswordEntry.setEnabled(false); 615de3c9cb412688eb841993ad971d06039d8a952c3Vikram Aggarwal mIgnoreBack = true; 61606c5ff4a46686d3f599e40df0768177d3d88913cJason parks 6170460675b7c0d5a9b02dae01578c64ff0453e4fb7Ben Komalo Log.d(TAG, "Attempting to send command to decrypt"); 61806c5ff4a46686d3f599e40df0768177d3d88913cJason parks new DecryptTask().execute(password); 61935933812b16f0b4221280bbcc3c32f6540996bf2Jason parks 6208fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks return true; 6218fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks } 6228fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks return false; 6238fd5bc9e6ff0d02012df670a6bf4bcdaf903c09dJason parks } 6248373b451692a39abdfd12444241c016831e1a4a4David Brown 625bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal /** 626ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * Set airplane mode on the device if it isn't an LTE device. 627ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save 628ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor. 629ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid 630ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * both these problems, we turn the radio off. However, on certain networks turning on and 631ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * off the radio takes a long time. In such cases, we are better off leaving the radio 632ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * running so the latency of an E911 call is short. 633ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * The behavior after this is: 634ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * 1. Emergency dialing: the emergency dialer has logic to force the device out of 635ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * airplane mode and restart the radio. 636ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * 2. Full boot: we read the persistent settings from the previous boot and restore the 637ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * radio to whatever it was before it restarted. This also happens when rebooting a 638ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal * phone that has no encryption. 639ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal */ 640ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal private final void setAirplaneModeIfNecessary() { 641ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal final boolean isLteDevice = 6425543404ae0ec9635b59d37fa01074ea598d539ebWink Saville TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE; 643ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal if (!isLteDevice) { 644ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal Log.d(TAG, "Going into airplane mode."); 6456a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); 646ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 647ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal intent.putExtra("state", true); 6486a5929b086a61a955a4a2f43f5fa251e8754ff4aChristopher Tate sendBroadcastAsUser(intent, UserHandle.ALL); 649ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal } 650ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal } 651ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal 652ea1186db19e673b44084b43b6a70bbdb07480aa9Vikram Aggarwal /** 653bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal * Code to update the state of, and handle clicks from, the "Emergency call" button. 654bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal * 655bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal * This code is mostly duplicated from the corresponding code in 656bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal * LockPatternUtils and LockPatternKeyguardView under frameworks/base. 657bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal */ 6588373b451692a39abdfd12444241c016831e1a4a4David Brown private void updateEmergencyCallButtonState() { 659bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton); 6608373b451692a39abdfd12444241c016831e1a4a4David Brown // The button isn't present at all in some configurations. 661bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal if (emergencyCall == null) 662bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal return; 6638373b451692a39abdfd12444241c016831e1a4a4David Brown 6648373b451692a39abdfd12444241c016831e1a4a4David Brown if (isEmergencyCallCapable()) { 665bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal emergencyCall.setVisibility(View.VISIBLE); 666bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal emergencyCall.setOnClickListener(new View.OnClickListener() { 667bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal @Override 668bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal 6698373b451692a39abdfd12444241c016831e1a4a4David Brown public void onClick(View v) { 6708373b451692a39abdfd12444241c016831e1a4a4David Brown takeEmergencyCallAction(); 6718373b451692a39abdfd12444241c016831e1a4a4David Brown } 6728373b451692a39abdfd12444241c016831e1a4a4David Brown }); 6738373b451692a39abdfd12444241c016831e1a4a4David Brown } else { 674bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal emergencyCall.setVisibility(View.GONE); 6758373b451692a39abdfd12444241c016831e1a4a4David Brown return; 6768373b451692a39abdfd12444241c016831e1a4a4David Brown } 6778373b451692a39abdfd12444241c016831e1a4a4David Brown 678bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final int newState = TelephonyManager.getDefault().getCallState(); 6798373b451692a39abdfd12444241c016831e1a4a4David Brown int textId; 6808373b451692a39abdfd12444241c016831e1a4a4David Brown if (newState == TelephonyManager.CALL_STATE_OFFHOOK) { 681bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal // Show "return to call" text and show phone icon 6828373b451692a39abdfd12444241c016831e1a4a4David Brown textId = R.string.cryptkeeper_return_to_call; 683bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final int phoneCallIcon = R.drawable.stat_sys_phone_call; 684bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0); 6858373b451692a39abdfd12444241c016831e1a4a4David Brown } else { 6868373b451692a39abdfd12444241c016831e1a4a4David Brown textId = R.string.cryptkeeper_emergency_call; 687bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final int emergencyIcon = R.drawable.ic_emergency; 688bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0); 6898373b451692a39abdfd12444241c016831e1a4a4David Brown } 690bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal emergencyCall.setText(textId); 6918373b451692a39abdfd12444241c016831e1a4a4David Brown } 6928373b451692a39abdfd12444241c016831e1a4a4David Brown 6938373b451692a39abdfd12444241c016831e1a4a4David Brown private boolean isEmergencyCallCapable() { 6948373b451692a39abdfd12444241c016831e1a4a4David Brown return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable); 6958373b451692a39abdfd12444241c016831e1a4a4David Brown } 6968373b451692a39abdfd12444241c016831e1a4a4David Brown 6978373b451692a39abdfd12444241c016831e1a4a4David Brown private void takeEmergencyCallAction() { 6988373b451692a39abdfd12444241c016831e1a4a4David Brown if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) { 6998373b451692a39abdfd12444241c016831e1a4a4David Brown resumeCall(); 7008373b451692a39abdfd12444241c016831e1a4a4David Brown } else { 7018373b451692a39abdfd12444241c016831e1a4a4David Brown launchEmergencyDialer(); 7028373b451692a39abdfd12444241c016831e1a4a4David Brown } 7038373b451692a39abdfd12444241c016831e1a4a4David Brown } 7048373b451692a39abdfd12444241c016831e1a4a4David Brown 7058373b451692a39abdfd12444241c016831e1a4a4David Brown private void resumeCall() { 706bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); 7078373b451692a39abdfd12444241c016831e1a4a4David Brown if (phone != null) { 7088373b451692a39abdfd12444241c016831e1a4a4David Brown try { 7098373b451692a39abdfd12444241c016831e1a4a4David Brown phone.showCallScreen(); 7108373b451692a39abdfd12444241c016831e1a4a4David Brown } catch (RemoteException e) { 7118373b451692a39abdfd12444241c016831e1a4a4David Brown Log.e(TAG, "Error calling ITelephony service: " + e); 7128373b451692a39abdfd12444241c016831e1a4a4David Brown } 7138373b451692a39abdfd12444241c016831e1a4a4David Brown } 7148373b451692a39abdfd12444241c016831e1a4a4David Brown } 7158373b451692a39abdfd12444241c016831e1a4a4David Brown 7168373b451692a39abdfd12444241c016831e1a4a4David Brown private void launchEmergencyDialer() { 717bfa3a64310162c8f29eae208220988380ffeea6eVikram Aggarwal final Intent intent = new Intent(ACTION_EMERGENCY_DIAL); 7188373b451692a39abdfd12444241c016831e1a4a4David Brown intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 7198373b451692a39abdfd12444241c016831e1a4a4David Brown | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 7208373b451692a39abdfd12444241c016831e1a4a4David Brown startActivity(intent); 7218373b451692a39abdfd12444241c016831e1a4a4David Brown } 722d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal 723d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal /** 724d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal * Listen to key events so we can disable sounds when we get a keyinput in EditText. 725d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal */ 726d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal private void delayAudioNotification() { 727d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal Log.d(TAG, "User entering password: delay audio notification"); 728d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal mNotificationCountdown = 20; 729d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } 730d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal 731d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal @Override 732d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal public boolean onKey(View v, int keyCode, KeyEvent event) { 733d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal delayAudioNotification(); 734d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal return false; 735d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } 736d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal 737d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal @Override 738d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal public boolean onTouch(View v, MotionEvent event) { 739d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal delayAudioNotification(); 740d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal return false; 741d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } 742d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal 743d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal @Override 744d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal public void beforeTextChanged(CharSequence s, int start, int count, int after) { 745d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal return; 746d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } 747d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal 748d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal @Override 749d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal public void onTextChanged(CharSequence s, int start, int before, int count) { 750d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal delayAudioNotification(); 751d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } 752d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal 753d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal @Override 754d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal public void afterTextChanged(Editable s) { 755d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal return; 756d1147250dd5fb7022b01a5c899d365069bb625f8Vikram Aggarwal } 7578373b451692a39abdfd12444241c016831e1a4a4David Brown} 758