Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.StatusBarManager; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 23 | import android.content.Intent; |
Paul Lawrence | 7ae20e3 | 2014-07-22 15:00:50 -0700 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Paul Lawrence | 7ae20e3 | 2014-07-22 15:00:50 -0700 | [diff] [blame] | 26 | import android.content.res.Resources.NotFoundException; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 27 | import android.media.AudioManager; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 28 | import android.os.AsyncTask; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 29 | import android.os.Bundle; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 30 | import android.os.Handler; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 31 | import android.os.IBinder; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 32 | import android.os.Message; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 33 | import android.os.PowerManager; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 35 | import android.os.ServiceManager; |
| 36 | import android.os.SystemProperties; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 38 | import android.os.storage.IMountService; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 39 | import android.os.storage.StorageManager; |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame] | 40 | |
| 41 | import android.phone.PhoneManager; |
Vikram Aggarwal | 6ebbd30 | 2012-05-04 14:09:52 -0700 | [diff] [blame] | 42 | import android.provider.Settings; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 43 | import android.telephony.TelephonyManager; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 44 | import android.text.Editable; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 45 | import android.text.TextUtils; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 46 | import android.text.TextWatcher; |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 47 | import android.text.format.DateUtils; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 48 | import android.util.Log; |
| 49 | import android.view.KeyEvent; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 50 | import android.view.MotionEvent; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 51 | import android.view.View; |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 52 | import android.view.WindowManager; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 53 | import android.view.View.OnClickListener; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 54 | import android.view.View.OnKeyListener; |
| 55 | import android.view.View.OnTouchListener; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 56 | import android.view.inputmethod.EditorInfo; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 57 | import android.view.inputmethod.InputMethodInfo; |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 58 | import android.view.inputmethod.InputMethodManager; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 59 | import android.view.inputmethod.InputMethodSubtype; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 60 | import android.widget.Button; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 61 | import android.widget.EditText; |
| 62 | import android.widget.ProgressBar; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 63 | import android.widget.TextView; |
| 64 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 65 | import com.android.internal.statusbar.StatusBarIcon; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 66 | import com.android.internal.telephony.Phone; |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 67 | import com.android.internal.telephony.PhoneConstants; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 68 | import com.android.internal.widget.LockPatternUtils; |
| 69 | import com.android.internal.widget.LockPatternView; |
| 70 | import com.android.internal.widget.LockPatternView.Cell; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 71 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 72 | import static com.android.internal.widget.LockPatternView.DisplayMode; |
| 73 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 74 | import java.util.List; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Settings screens to show the UI flows for encrypting/decrypting the device. |
| 78 | * |
| 79 | * This may be started via adb for debugging the UI layout, without having to go through |
| 80 | * encryption flows everytime. It should be noted that starting the activity in this manner |
| 81 | * is only useful for verifying UI-correctness - the behavior will not be identical. |
| 82 | * <pre> |
| 83 | * $ adb shell pm enable com.android.settings/.CryptKeeper |
| 84 | * $ adb shell am start \ |
| 85 | * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \ |
| 86 | * -n com.android.settings/.CryptKeeper |
| 87 | * </pre> |
| 88 | */ |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 89 | public class CryptKeeper extends Activity implements TextView.OnEditorActionListener, |
| 90 | OnKeyListener, OnTouchListener, TextWatcher { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 91 | private static final String TAG = "CryptKeeper"; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 92 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 93 | private static final String DECRYPT_STATE = "trigger_restart_framework"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 94 | /** Message sent to us to indicate encryption update progress. */ |
| 95 | private static final int MESSAGE_UPDATE_PROGRESS = 1; |
| 96 | /** Message sent to us to cool-down (waste user's time between password attempts) */ |
| 97 | private static final int MESSAGE_COOLDOWN = 2; |
| 98 | /** Message sent to us to indicate alerting the user that we are waiting for password entry */ |
| 99 | private static final int MESSAGE_NOTIFY = 3; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 100 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 101 | // Constants used to control policy. |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 102 | private static final int MAX_FAILED_ATTEMPTS = 30; |
| 103 | private static final int COOL_DOWN_ATTEMPTS = 10; |
| 104 | private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds |
| 105 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 106 | // Intent action for launching the Emergency Dialer activity. |
| 107 | static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 108 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 109 | // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts |
| 110 | private static final String EXTRA_FORCE_VIEW = |
| 111 | "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW"; |
| 112 | private static final String FORCE_VIEW_PROGRESS = "progress"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 113 | private static final String FORCE_VIEW_ERROR = "error"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 114 | private static final String FORCE_VIEW_PASSWORD = "password"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 115 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 116 | /** When encryption is detected, this flag indicates whether or not we've checked for errors. */ |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 117 | private boolean mValidationComplete; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 118 | private boolean mValidationRequested; |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 119 | /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */ |
| 120 | private boolean mEncryptionGoneBad; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 121 | /** A flag to indicate when the back event should be ignored */ |
| 122 | private boolean mIgnoreBack = false; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 123 | private int mCooldown; |
| 124 | PowerManager.WakeLock mWakeLock; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 125 | private EditText mPasswordEntry; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 126 | private LockPatternView mLockPatternView; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 127 | /** Number of calls to {@link #notifyUser()} to ignore before notifying. */ |
| 128 | private int mNotificationCountdown = 0; |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 129 | /** Number of calls to {@link #notifyUser()} before we release the wakelock */ |
| 130 | private int mReleaseWakeLockCountdown = 0; |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 131 | private int mStatusString = R.string.enter_password; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 132 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 133 | // how long we wait to clear a wrong pattern |
| 134 | private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500; |
| 135 | |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame^] | 136 | // how long we wait to clear a right pattern |
| 137 | private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500; |
| 138 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 139 | private Runnable mClearPatternRunnable = new Runnable() { |
| 140 | public void run() { |
| 141 | mLockPatternView.clearPattern(); |
| 142 | } |
| 143 | }; |
| 144 | |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 145 | /** |
| 146 | * Used to propagate state through configuration changes (e.g. screen rotation) |
| 147 | */ |
| 148 | private static class NonConfigurationInstanceState { |
| 149 | final PowerManager.WakeLock wakelock; |
| 150 | |
| 151 | NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) { |
| 152 | wakelock = _wakelock; |
| 153 | } |
| 154 | } |
| 155 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 156 | private class DecryptTask extends AsyncTask<String, Void, Integer> { |
| 157 | @Override |
| 158 | protected Integer doInBackground(String... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 159 | final IMountService service = getMountService(); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 160 | try { |
| 161 | return service.decryptStorage(params[0]); |
| 162 | } catch (Exception e) { |
| 163 | Log.e(TAG, "Error while decrypting...", e); |
| 164 | return -1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void onPostExecute(Integer failedAttempts) { |
| 170 | if (failedAttempts == 0) { |
Paul Lawrence | 89c7570 | 2014-07-11 07:34:44 -0700 | [diff] [blame] | 171 | // The password was entered successfully. Simply do nothing |
| 172 | // and wait for the service restart to switch to surfacefligner |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame^] | 173 | if (mLockPatternView != null) { |
| 174 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 175 | mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS); |
| 176 | } |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 177 | } else if (failedAttempts == MAX_FAILED_ATTEMPTS) { |
| 178 | // Factory reset the device. |
| 179 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 180 | } else if (failedAttempts == -1) { |
| 181 | // Right password, but decryption failed. Tell user bad news ... |
| 182 | setContentView(R.layout.crypt_keeper_progress); |
| 183 | showFactoryReset(true); |
| 184 | return; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 185 | } else { |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 186 | // Wrong entry. Handle pattern case. |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 187 | if (mLockPatternView != null) { |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 188 | mLockPatternView.setDisplayMode(DisplayMode.Wrong); |
| 189 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 190 | mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS); |
| 191 | } |
| 192 | if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) { |
| 193 | mCooldown = COOL_DOWN_INTERVAL; |
| 194 | cooldown(); |
| 195 | } else { |
| 196 | final TextView status = (TextView) findViewById(R.id.status); |
Paul Lawrence | bd4c321 | 2014-07-23 13:57:17 -0700 | [diff] [blame] | 197 | |
| 198 | int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts; |
| 199 | if (remainingAttempts < COOL_DOWN_ATTEMPTS) { |
| 200 | CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe); |
| 201 | CharSequence warning = TextUtils.expandTemplate(warningTemplate, |
| 202 | Integer.toString(remainingAttempts)); |
| 203 | status.setText(warning); |
| 204 | } else { |
| 205 | status.setText(R.string.try_again); |
| 206 | } |
| 207 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 208 | if (mLockPatternView != null) { |
| 209 | mLockPatternView.setDisplayMode(DisplayMode.Wrong); |
| 210 | } |
| 211 | // Reenable the password entry |
| 212 | if (mPasswordEntry != null) { |
| 213 | mPasswordEntry.setEnabled(true); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 214 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 215 | Context.INPUT_METHOD_SERVICE); |
| 216 | imm.showSoftInput(mPasswordEntry, 0); |
Paul Lawrence | 08c6ab0 | 2014-06-19 13:34:58 -0700 | [diff] [blame] | 217 | setBackFunctionality(true); |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 218 | } |
| 219 | if (mLockPatternView != null) { |
| 220 | mLockPatternView.setEnabled(true); |
| 221 | } |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 222 | } |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 226 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 227 | private class ValidationTask extends AsyncTask<Void, Void, Boolean> { |
| 228 | @Override |
| 229 | protected Boolean doInBackground(Void... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 230 | final IMountService service = getMountService(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 231 | try { |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 232 | Log.d(TAG, "Validating encryption state."); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 233 | int state = service.getEncryptionState(); |
| 234 | if (state == IMountService.ENCRYPTION_STATE_NONE) { |
| 235 | Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption."); |
| 236 | return true; // Unexpected, but fine, I guess... |
| 237 | } |
| 238 | return state == IMountService.ENCRYPTION_STATE_OK; |
| 239 | } catch (RemoteException e) { |
| 240 | Log.w(TAG, "Unable to get encryption state properly"); |
| 241 | return true; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | protected void onPostExecute(Boolean result) { |
| 247 | mValidationComplete = true; |
| 248 | if (Boolean.FALSE.equals(result)) { |
| 249 | Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe."); |
| 250 | mEncryptionGoneBad = true; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 251 | } else { |
| 252 | Log.d(TAG, "Encryption state validated. Proceeding to configure UI"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 253 | } |
| 254 | setupUi(); |
| 255 | } |
| 256 | } |
| 257 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 258 | private final Handler mHandler = new Handler() { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 259 | @Override |
| 260 | public void handleMessage(Message msg) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 261 | switch (msg.what) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 262 | case MESSAGE_UPDATE_PROGRESS: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 263 | updateProgress(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 264 | break; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 265 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 266 | case MESSAGE_COOLDOWN: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 267 | cooldown(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 268 | break; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 269 | |
| 270 | case MESSAGE_NOTIFY: |
| 271 | notifyUser(); |
| 272 | break; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | }; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 276 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 277 | private AudioManager mAudioManager; |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 278 | /** The status bar where back/home/recent buttons are shown. */ |
| 279 | private StatusBarManager mStatusBar; |
| 280 | |
| 281 | /** All the widgets to disable in the status bar */ |
| 282 | final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND |
| 283 | | StatusBarManager.DISABLE_NOTIFICATION_ICONS |
| 284 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS |
| 285 | | StatusBarManager.DISABLE_SYSTEM_INFO |
| 286 | | StatusBarManager.DISABLE_HOME |
Alon Albert | 66d050b | 2013-09-30 17:08:12 -0700 | [diff] [blame] | 287 | | StatusBarManager.DISABLE_SEARCH |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 288 | | StatusBarManager.DISABLE_RECENT; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 289 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 290 | /** @return whether or not this Activity was started for debugging the UI only. */ |
| 291 | private boolean isDebugView() { |
| 292 | return getIntent().hasExtra(EXTRA_FORCE_VIEW); |
| 293 | } |
| 294 | |
| 295 | /** @return whether or not this Activity was started for debugging the specific UI view only. */ |
| 296 | private boolean isDebugView(String viewType /* non-nullable */) { |
| 297 | return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW)); |
| 298 | } |
| 299 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Notify the user that we are awaiting input. Currently this sends an audio alert. |
| 302 | */ |
| 303 | private void notifyUser() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 304 | if (mNotificationCountdown > 0) { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 305 | --mNotificationCountdown; |
| 306 | } else if (mAudioManager != null) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 307 | try { |
| 308 | // Play the standard keypress sound at full volume. This should be available on |
| 309 | // every device. We cannot play a ringtone here because media services aren't |
| 310 | // available yet. A DTMF-style tone is too soft to be noticed, and might not exist |
| 311 | // on tablet devices. The idea is to alert the user that something is needed: this |
| 312 | // does not have to be pleasing. |
| 313 | mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100); |
| 314 | } catch (Exception e) { |
| 315 | Log.w(TAG, "notifyUser: Exception while playing sound: " + e); |
| 316 | } |
| 317 | } |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 318 | // Notify the user again in 5 seconds. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 319 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 320 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000); |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 321 | |
| 322 | if (mWakeLock.isHeld()) { |
| 323 | if (mReleaseWakeLockCountdown > 0) { |
| 324 | --mReleaseWakeLockCountdown; |
| 325 | } else { |
| 326 | mWakeLock.release(); |
| 327 | } |
| 328 | } |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Ignore back events after the user has entered the decrypt screen and while the device is |
| 333 | * encrypting. |
| 334 | */ |
| 335 | @Override |
| 336 | public void onBackPressed() { |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 337 | // In the rare case that something pressed back even though we were disabled. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 338 | if (mIgnoreBack) |
| 339 | return; |
| 340 | super.onBackPressed(); |
| 341 | } |
| 342 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 343 | @Override |
| 344 | public void onCreate(Bundle savedInstanceState) { |
| 345 | super.onCreate(savedInstanceState); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 346 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 347 | // If we are not encrypted or encrypting, get out quickly. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 348 | final String state = SystemProperties.get("vold.decrypt"); |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 349 | if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 350 | // Disable the crypt keeper. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 351 | PackageManager pm = getPackageManager(); |
| 352 | ComponentName name = new ComponentName(this, CryptKeeper.class); |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 353 | pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 354 | PackageManager.DONT_KILL_APP); |
| 355 | // Typically CryptKeeper is launched as the home app. We didn't |
Dianne Hackborn | 644fa42 | 2011-10-18 13:38:03 -0700 | [diff] [blame] | 356 | // want to be running, so need to finish this activity. We can count |
| 357 | // on the activity manager re-launching the new home app upon finishing |
| 358 | // this one, since this will leave the activity stack empty. |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 359 | // NOTE: This is really grungy. I think it would be better for the |
| 360 | // activity manager to explicitly launch the crypt keeper instead of |
| 361 | // home in the situation where we need to decrypt the device |
| 362 | finish(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 363 | return; |
| 364 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 365 | |
Paul Lawrence | 7ae20e3 | 2014-07-22 15:00:50 -0700 | [diff] [blame] | 366 | try { |
| 367 | if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) { |
| 368 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 369 | } |
| 370 | } catch (NotFoundException e) { |
| 371 | } |
| 372 | |
Vikram Aggarwal | b96b35a | 2012-05-01 11:09:39 -0700 | [diff] [blame] | 373 | // Disable the status bar, but do NOT disable back because the user needs a way to go |
| 374 | // from keyboard settings and back to the password screen. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 375 | mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
| 376 | mStatusBar.disable(sWidgetsToDisable); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 377 | |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 378 | setAirplaneModeIfNecessary(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 379 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 380 | // Check for (and recover) retained instance data |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 381 | final Object lastInstance = getLastNonConfigurationInstance(); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 382 | if (lastInstance instanceof NonConfigurationInstanceState) { |
| 383 | NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance; |
| 384 | mWakeLock = retained.wakelock; |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 385 | Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 386 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 387 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 388 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Note, we defer the state check and screen setup to onStart() because this will be |
| 391 | * re-run if the user clicks the power button (sleeping/waking the screen), and this is |
| 392 | * especially important if we were to lose the wakelock for any reason. |
| 393 | */ |
| 394 | @Override |
| 395 | public void onStart() { |
| 396 | super.onStart(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 397 | setupUi(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Initializes the UI based on the current state of encryption. |
| 402 | * This is idempotent - calling repeatedly will simply re-initialize the UI. |
| 403 | */ |
| 404 | private void setupUi() { |
| 405 | if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) { |
| 406 | setContentView(R.layout.crypt_keeper_progress); |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 407 | showFactoryReset(false); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 411 | final String progress = SystemProperties.get("vold.encrypt_progress"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 412 | if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) { |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 413 | setContentView(R.layout.crypt_keeper_progress); |
| 414 | encryptionProgressInit(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 415 | } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) { |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 416 | new AsyncTask<Void, Void, Void>() { |
| 417 | int type = StorageManager.CRYPT_TYPE_PASSWORD; |
| 418 | String owner_info; |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame^] | 419 | boolean pattern_visible; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 420 | |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 421 | @Override |
| 422 | public Void doInBackground(Void... v) { |
| 423 | try { |
| 424 | final IMountService service = getMountService(); |
| 425 | type = service.getPasswordType(); |
| 426 | owner_info = service.getField("OwnerInfo"); |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame^] | 427 | pattern_visible = !("0".equals(service.getField("PatternVisible"))); |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 428 | } catch (Exception e) { |
| 429 | Log.e(TAG, "Error calling mount service " + e); |
| 430 | } |
| 431 | |
| 432 | return null; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void onPostExecute(java.lang.Void v) { |
| 437 | if(type == StorageManager.CRYPT_TYPE_PIN) { |
| 438 | setContentView(R.layout.crypt_keeper_pin_entry); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 439 | mStatusString = R.string.enter_pin; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 440 | } else if (type == StorageManager.CRYPT_TYPE_PATTERN) { |
| 441 | setContentView(R.layout.crypt_keeper_pattern_entry); |
| 442 | setBackFunctionality(false); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 443 | mStatusString = R.string.enter_pattern; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 444 | } else { |
| 445 | setContentView(R.layout.crypt_keeper_password_entry); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 446 | mStatusString = R.string.enter_password; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 447 | } |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 448 | final TextView status = (TextView) findViewById(R.id.status); |
| 449 | status.setText(mStatusString); |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 450 | |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 451 | final TextView ownerInfo = (TextView) findViewById(R.id.owner_info); |
| 452 | ownerInfo.setText(owner_info); |
| 453 | ownerInfo.setSelected(true); // Required for marquee'ing to work |
| 454 | |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 455 | passwordEntryInit(); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 456 | |
Paul Lawrence | 0f11e15 | 2014-08-20 07:43:32 -0700 | [diff] [blame^] | 457 | if (mLockPatternView != null) { |
| 458 | mLockPatternView.setInStealthMode(!pattern_visible); |
| 459 | } |
| 460 | |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 461 | if (mCooldown > 0) { |
| 462 | setBackFunctionality(false); |
| 463 | cooldown(); // in case we are cooling down and coming back from emergency dialler |
| 464 | } |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 465 | } |
| 466 | }.execute(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 467 | } else if (!mValidationRequested) { |
| 468 | // We're supposed to be encrypted, but no validation has been done. |
| 469 | new ValidationTask().execute((Void[]) null); |
| 470 | mValidationRequested = true; |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 474 | @Override |
| 475 | public void onStop() { |
| 476 | super.onStop(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 477 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 478 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
| 479 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop() |
| 484 | * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears |
| 485 | * mWakeLock so the subsequent call to onDestroy does not release it. |
| 486 | */ |
| 487 | @Override |
| 488 | public Object onRetainNonConfigurationInstance() { |
| 489 | NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 490 | Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 491 | mWakeLock = null; |
| 492 | return state; |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public void onDestroy() { |
| 497 | super.onDestroy(); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 498 | |
| 499 | if (mWakeLock != null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 500 | Log.d(TAG, "Releasing and destroying wakelock"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 501 | mWakeLock.release(); |
| 502 | mWakeLock = null; |
| 503 | } |
| 504 | } |
| 505 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 506 | /** |
| 507 | * Start encrypting the device. |
| 508 | */ |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 509 | private void encryptionProgressInit() { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 510 | // Accquire a partial wakelock to prevent the device from sleeping. Note |
| 511 | // we never release this wakelock as we will be restarted after the device |
| 512 | // is encrypted. |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 513 | Log.d(TAG, "Encryption progress screen initializing."); |
Ben Komalo | 9ee164f | 2011-09-21 10:40:50 -0700 | [diff] [blame] | 514 | if (mWakeLock == null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 515 | Log.d(TAG, "Acquiring wakelock."); |
| 516 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 517 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 518 | mWakeLock.acquire(); |
| 519 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 520 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 521 | ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 522 | // Ignore all back presses from now, both hard and soft keys. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 523 | setBackFunctionality(false); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 524 | // Start the first run of progress manually. This method sets up messages to occur at |
| 525 | // repeated intervals. |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 526 | updateProgress(); |
| 527 | } |
| 528 | |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 529 | /** |
| 530 | * Show factory reset screen allowing the user to reset their phone when |
| 531 | * there is nothing else we can do |
| 532 | * @param corrupt true if userdata is corrupt, false if encryption failed |
| 533 | * partway through |
| 534 | */ |
| 535 | private void showFactoryReset(boolean corrupt) { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 536 | // Hide the encryption-bot to make room for the "factory reset" button |
| 537 | findViewById(R.id.encroid).setVisibility(View.GONE); |
| 538 | |
| 539 | // Show the reset button, failure text, and a divider |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 540 | final Button button = (Button) findViewById(R.id.factory_reset); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 541 | button.setVisibility(View.VISIBLE); |
| 542 | button.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 543 | @Override |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 544 | public void onClick(View v) { |
| 545 | // Factory reset the device. |
| 546 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 547 | } |
| 548 | }); |
| 549 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 550 | // Alert the user of the failure. |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 551 | if (corrupt) { |
| 552 | ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title); |
| 553 | ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary); |
| 554 | } else { |
| 555 | ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title); |
| 556 | ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary); |
| 557 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 558 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 559 | final View view = findViewById(R.id.bottom_divider); |
| 560 | // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate. |
Ben Komalo | f0104df | 2011-08-16 17:48:42 -0700 | [diff] [blame] | 561 | if (view != null) { |
| 562 | view.setVisibility(View.VISIBLE); |
| 563 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 566 | private void updateProgress() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 567 | final String state = SystemProperties.get("vold.encrypt_progress"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 568 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 569 | if ("error_partially_encrypted".equals(state)) { |
Paul Lawrence | 384d8e5 | 2014-08-08 15:11:36 -0700 | [diff] [blame] | 570 | showFactoryReset(false); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 574 | // Get status as percentage first |
| 575 | CharSequence status = getText(R.string.crypt_keeper_setup_description); |
| 576 | int percent = 0; |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 577 | try { |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 578 | // Force a 50% progress state when debugging the view. |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 579 | percent = isDebugView() ? 50 : Integer.parseInt(state); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 580 | } catch (Exception e) { |
| 581 | Log.w(TAG, "Error parsing progress: " + e.toString()); |
| 582 | } |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 583 | String progress = Integer.toString(percent); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 584 | |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 585 | // Now try to get status as time remaining and replace as appropriate |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 586 | Log.v(TAG, "Encryption progress: " + progress); |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 587 | try { |
| 588 | final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining"); |
| 589 | int time = Integer.parseInt(timeProperty); |
| 590 | if (time >= 0) { |
| 591 | // Round up to multiple of 10 - this way display is less jerky |
| 592 | time = (time + 9) / 10 * 10; |
| 593 | progress = DateUtils.formatElapsedTime(time); |
| 594 | status = getText(R.string.crypt_keeper_setup_time_remaining); |
| 595 | } |
| 596 | } catch (Exception e) { |
| 597 | // Will happen if no time etc - show percentage |
| 598 | } |
| 599 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 600 | final TextView tv = (TextView) findViewById(R.id.status); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 601 | if (tv != null) { |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 602 | tv.setText(TextUtils.expandTemplate(status, progress)); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 603 | } |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 604 | |
| 605 | // Check the progress every 1 seconds |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 606 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 607 | mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 608 | } |
| 609 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 610 | /** Disable password input for a while to force the user to waste time between retries */ |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 611 | private void cooldown() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 612 | final TextView status = (TextView) findViewById(R.id.status); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 613 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 614 | if (mCooldown <= 0) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 615 | // Re-enable the password entry and back presses. |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 616 | if (mPasswordEntry != null) { |
| 617 | mPasswordEntry.setEnabled(true); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 618 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 619 | Context.INPUT_METHOD_SERVICE); |
| 620 | imm.showSoftInput(mPasswordEntry, 0); |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 621 | setBackFunctionality(true); |
| 622 | } |
| 623 | if (mLockPatternView != null) { |
| 624 | mLockPatternView.setEnabled(true); |
| 625 | } |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 626 | status.setText(mStatusString); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 627 | } else { |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 628 | // Disable the password entry and back presses. |
| 629 | if (mPasswordEntry != null) { |
| 630 | mPasswordEntry.setEnabled(false); |
| 631 | } |
| 632 | if (mLockPatternView != null) { |
| 633 | mLockPatternView.setEnabled(false); |
| 634 | } |
| 635 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 636 | CharSequence template = getText(R.string.crypt_keeper_cooldown); |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 637 | status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown))); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 638 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 639 | mCooldown--; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 640 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 641 | mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 642 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 643 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 644 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 645 | /** |
| 646 | * Sets the back status: enabled or disabled according to the parameter. |
| 647 | * @param isEnabled true if back is enabled, false otherwise. |
| 648 | */ |
| 649 | private final void setBackFunctionality(boolean isEnabled) { |
| 650 | mIgnoreBack = !isEnabled; |
| 651 | if (isEnabled) { |
| 652 | mStatusBar.disable(sWidgetsToDisable); |
| 653 | } else { |
| 654 | mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK); |
| 655 | } |
| 656 | } |
| 657 | |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 658 | protected LockPatternView.OnPatternListener mChooseNewLockPatternListener = |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 659 | new LockPatternView.OnPatternListener() { |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 660 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 661 | @Override |
| 662 | public void onPatternStart() { |
| 663 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 664 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 665 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 666 | @Override |
| 667 | public void onPatternCleared() { |
| 668 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 669 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 670 | @Override |
| 671 | public void onPatternDetected(List<LockPatternView.Cell> pattern) { |
| 672 | mLockPatternView.setEnabled(false); |
| 673 | new DecryptTask().execute(LockPatternUtils.patternToString(pattern)); |
| 674 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 675 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 676 | @Override |
| 677 | public void onPatternCellAdded(List<Cell> pattern) { |
| 678 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 679 | }; |
| 680 | |
| 681 | private void passwordEntryInit() { |
| 682 | // Password/pin case |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 683 | mPasswordEntry = (EditText) findViewById(R.id.passwordEntry); |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 684 | if (mPasswordEntry != null){ |
| 685 | mPasswordEntry.setOnEditorActionListener(this); |
| 686 | mPasswordEntry.requestFocus(); |
| 687 | // Become quiet when the user interacts with the Edit text screen. |
| 688 | mPasswordEntry.setOnKeyListener(this); |
| 689 | mPasswordEntry.setOnTouchListener(this); |
| 690 | mPasswordEntry.addTextChangedListener(this); |
| 691 | } |
| 692 | |
| 693 | // Pattern case |
| 694 | mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern); |
| 695 | if (mLockPatternView != null) { |
| 696 | mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener); |
| 697 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 698 | |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 699 | // Disable the Emergency call button if the device has no voice telephone capability |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 700 | if (!getTelephonyManager().isVoiceCapable()) { |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 701 | final View emergencyCall = findViewById(R.id.emergencyCallButton); |
| 702 | if (emergencyCall != null) { |
| 703 | Log.d(TAG, "Removing the emergency Call button"); |
| 704 | emergencyCall.setVisibility(View.GONE); |
| 705 | } |
| 706 | } |
| 707 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 708 | final View imeSwitcher = findViewById(R.id.switch_ime_button); |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 709 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 710 | Context.INPUT_METHOD_SERVICE); |
| 711 | if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) { |
| 712 | imeSwitcher.setVisibility(View.VISIBLE); |
| 713 | imeSwitcher.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 714 | @Override |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 715 | public void onClick(View v) { |
| 716 | imm.showInputMethodPicker(); |
| 717 | } |
| 718 | }); |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 719 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 720 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 721 | // We want to keep the screen on while waiting for input. In minimal boot mode, the device |
| 722 | // is completely non-functional, and we want the user to notice the device and enter a |
| 723 | // password. |
| 724 | if (mWakeLock == null) { |
| 725 | Log.d(TAG, "Acquiring wakelock."); |
| 726 | final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 727 | if (pm != null) { |
| 728 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 729 | mWakeLock.acquire(); |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 730 | // Keep awake for 10 minutes - if the user hasn't been alerted by then |
| 731 | // best not to just drain their battery |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 732 | mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600 |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 735 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 736 | // Asynchronously throw up the IME, since there are issues with requesting it to be shown |
| 737 | // immediately. |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 738 | if (mLockPatternView == null && mCooldown <= 0) { |
Paul Lawrence | e54e932 | 2014-03-13 14:05:48 -0700 | [diff] [blame] | 739 | mHandler.postDelayed(new Runnable() { |
| 740 | @Override public void run() { |
| 741 | imm.showSoftInputUnchecked(0, null); |
| 742 | } |
| 743 | }, 0); |
| 744 | } |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 745 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 746 | updateEmergencyCallButtonState(); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 747 | // Notify the user in 120 seconds that we are waiting for him to enter the password. |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 748 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 749 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000); |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 750 | |
| 751 | // Dismiss keyguard while this screen is showing. |
| 752 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 753 | } |
| 754 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 755 | /** |
| 756 | * Method adapted from com.android.inputmethod.latin.Utils |
| 757 | * |
| 758 | * @param imm The input method manager |
| 759 | * @param shouldIncludeAuxiliarySubtypes |
| 760 | * @return true if we have multiple IMEs to choose from |
| 761 | */ |
| 762 | private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm, |
| 763 | final boolean shouldIncludeAuxiliarySubtypes) { |
| 764 | final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); |
| 765 | |
| 766 | // Number of the filtered IMEs |
| 767 | int filteredImisCount = 0; |
| 768 | |
| 769 | for (InputMethodInfo imi : enabledImis) { |
| 770 | // We can return true immediately after we find two or more filtered IMEs. |
| 771 | if (filteredImisCount > 1) return true; |
| 772 | final List<InputMethodSubtype> subtypes = |
| 773 | imm.getEnabledInputMethodSubtypeList(imi, true); |
| 774 | // IMEs that have no subtypes should be counted. |
| 775 | if (subtypes.isEmpty()) { |
| 776 | ++filteredImisCount; |
| 777 | continue; |
| 778 | } |
| 779 | |
| 780 | int auxCount = 0; |
| 781 | for (InputMethodSubtype subtype : subtypes) { |
| 782 | if (subtype.isAuxiliary()) { |
| 783 | ++auxCount; |
| 784 | } |
| 785 | } |
| 786 | final int nonAuxCount = subtypes.size() - auxCount; |
| 787 | |
| 788 | // IMEs that have one or more non-auxiliary subtypes should be counted. |
| 789 | // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary |
| 790 | // subtypes should be counted as well. |
| 791 | if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { |
| 792 | ++filteredImisCount; |
| 793 | continue; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | return filteredImisCount > 1 |
| 798 | // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled |
| 799 | // input method subtype (The current IME should be LatinIME.) |
| 800 | || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; |
| 801 | } |
| 802 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 803 | private IMountService getMountService() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 804 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 805 | if (service != null) { |
| 806 | return IMountService.Stub.asInterface(service); |
| 807 | } |
| 808 | return null; |
| 809 | } |
| 810 | |
| 811 | @Override |
| 812 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 813 | if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) { |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 814 | // Get the password |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 815 | final String password = v.getText().toString(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 816 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 817 | if (TextUtils.isEmpty(password)) { |
| 818 | return true; |
| 819 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 820 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 821 | // Now that we have the password clear the password field. |
| 822 | v.setText(null); |
| 823 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 824 | // Disable the password entry and back keypress while checking the password. These |
| 825 | // we either be re-enabled if the password was wrong or after the cooldown period. |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 826 | mPasswordEntry.setEnabled(false); |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 827 | setBackFunctionality(false); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 828 | |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 829 | Log.d(TAG, "Attempting to send command to decrypt"); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 830 | new DecryptTask().execute(password); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 831 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 832 | return true; |
| 833 | } |
| 834 | return false; |
| 835 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 836 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 837 | /** |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 838 | * Set airplane mode on the device if it isn't an LTE device. |
| 839 | * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save |
| 840 | * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor. |
| 841 | * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid |
| 842 | * both these problems, we turn the radio off. However, on certain networks turning on and |
| 843 | * off the radio takes a long time. In such cases, we are better off leaving the radio |
| 844 | * running so the latency of an E911 call is short. |
| 845 | * The behavior after this is: |
| 846 | * 1. Emergency dialing: the emergency dialer has logic to force the device out of |
| 847 | * airplane mode and restart the radio. |
| 848 | * 2. Full boot: we read the persistent settings from the previous boot and restore the |
| 849 | * radio to whatever it was before it restarted. This also happens when rebooting a |
| 850 | * phone that has no encryption. |
| 851 | */ |
| 852 | private final void setAirplaneModeIfNecessary() { |
| 853 | final boolean isLteDevice = |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 854 | getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 855 | if (!isLteDevice) { |
| 856 | Log.d(TAG, "Going into airplane mode."); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 857 | Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 858 | final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 859 | intent.putExtra("state", true); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 860 | sendBroadcastAsUser(intent, UserHandle.ALL); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | /** |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 865 | * Code to update the state of, and handle clicks from, the "Emergency call" button. |
| 866 | * |
| 867 | * This code is mostly duplicated from the corresponding code in |
| 868 | * LockPatternUtils and LockPatternKeyguardView under frameworks/base. |
| 869 | */ |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 870 | private void updateEmergencyCallButtonState() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 871 | final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 872 | // The button isn't present at all in some configurations. |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 873 | if (emergencyCall == null) |
| 874 | return; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 875 | |
| 876 | if (isEmergencyCallCapable()) { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 877 | emergencyCall.setVisibility(View.VISIBLE); |
| 878 | emergencyCall.setOnClickListener(new View.OnClickListener() { |
| 879 | @Override |
| 880 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 881 | public void onClick(View v) { |
| 882 | takeEmergencyCallAction(); |
| 883 | } |
| 884 | }); |
| 885 | } else { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 886 | emergencyCall.setVisibility(View.GONE); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 887 | return; |
| 888 | } |
| 889 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 890 | int textId; |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame] | 891 | if (getPhoneManager().isInAPhoneCall()) { |
Paul Lawrence | 89c7570 | 2014-07-11 07:34:44 -0700 | [diff] [blame] | 892 | // Show "return to call" |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 893 | textId = R.string.cryptkeeper_return_to_call; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 894 | } else { |
| 895 | textId = R.string.cryptkeeper_emergency_call; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 896 | } |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 897 | emergencyCall.setText(textId); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | private boolean isEmergencyCallCapable() { |
| 901 | return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable); |
| 902 | } |
| 903 | |
| 904 | private void takeEmergencyCallAction() { |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame] | 905 | PhoneManager phoneManager = getPhoneManager(); |
| 906 | if (phoneManager.isInAPhoneCall()) { |
| 907 | phoneManager.showCallScreen(false /* showDialpad */); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 908 | } else { |
| 909 | launchEmergencyDialer(); |
| 910 | } |
| 911 | } |
| 912 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 913 | |
| 914 | private void launchEmergencyDialer() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 915 | final Intent intent = new Intent(ACTION_EMERGENCY_DIAL); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 916 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 917 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 918 | setBackFunctionality(true); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 919 | startActivity(intent); |
| 920 | } |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 921 | |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 922 | private TelephonyManager getTelephonyManager() { |
| 923 | return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
| 924 | } |
| 925 | |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame] | 926 | private PhoneManager getPhoneManager() { |
| 927 | return (PhoneManager) getSystemService(Context.PHONE_SERVICE); |
| 928 | } |
| 929 | |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 930 | /** |
| 931 | * Listen to key events so we can disable sounds when we get a keyinput in EditText. |
| 932 | */ |
| 933 | private void delayAudioNotification() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 934 | mNotificationCountdown = 20; |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 939 | delayAudioNotification(); |
| 940 | return false; |
| 941 | } |
| 942 | |
| 943 | @Override |
| 944 | public boolean onTouch(View v, MotionEvent event) { |
| 945 | delayAudioNotification(); |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | @Override |
| 950 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | @Override |
| 955 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 956 | delayAudioNotification(); |
| 957 | } |
| 958 | |
| 959 | @Override |
| 960 | public void afterTextChanged(Editable s) { |
| 961 | return; |
| 962 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 963 | } |