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; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 25 | import android.os.AsyncTask; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 26 | import android.os.Bundle; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 27 | import android.os.Handler; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 28 | import android.os.IBinder; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 29 | import android.os.Message; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 30 | import android.os.PowerManager; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 32 | import android.os.ServiceManager; |
| 33 | import android.os.SystemProperties; |
| 34 | import android.os.storage.IMountService; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 35 | import android.telephony.TelephonyManager; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 36 | import android.text.TextUtils; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.view.KeyEvent; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 39 | import android.view.View; |
| 40 | import android.view.View.OnClickListener; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 41 | import android.view.inputmethod.EditorInfo; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 42 | import android.view.inputmethod.InputMethodInfo; |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 43 | import android.view.inputmethod.InputMethodManager; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 44 | import android.view.inputmethod.InputMethodSubtype; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 45 | import android.widget.Button; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 46 | import android.widget.EditText; |
| 47 | import android.widget.ProgressBar; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 48 | import android.widget.TextView; |
| 49 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 50 | import com.android.internal.telephony.ITelephony; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 51 | |
| 52 | import java.util.List; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Settings screens to show the UI flows for encrypting/decrypting the device. |
| 56 | * |
| 57 | * This may be started via adb for debugging the UI layout, without having to go through |
| 58 | * encryption flows everytime. It should be noted that starting the activity in this manner |
| 59 | * is only useful for verifying UI-correctness - the behavior will not be identical. |
| 60 | * <pre> |
| 61 | * $ adb shell pm enable com.android.settings/.CryptKeeper |
| 62 | * $ adb shell am start \ |
| 63 | * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \ |
| 64 | * -n com.android.settings/.CryptKeeper |
| 65 | * </pre> |
| 66 | */ |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 67 | public class CryptKeeper extends Activity implements TextView.OnEditorActionListener { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 68 | private static final String TAG = "CryptKeeper"; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 69 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 70 | private static final String DECRYPT_STATE = "trigger_restart_framework"; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 71 | |
| 72 | private static final int UPDATE_PROGRESS = 1; |
| 73 | private static final int COOLDOWN = 2; |
| 74 | |
| 75 | private static final int MAX_FAILED_ATTEMPTS = 30; |
| 76 | private static final int COOL_DOWN_ATTEMPTS = 10; |
| 77 | private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds |
| 78 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 79 | // Intent action for launching the Emergency Dialer activity. |
| 80 | static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 81 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 82 | // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts |
| 83 | private static final String EXTRA_FORCE_VIEW = |
| 84 | "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW"; |
| 85 | private static final String FORCE_VIEW_PROGRESS = "progress"; |
| 86 | private static final String FORCE_VIEW_ENTRY = "entry"; |
| 87 | private static final String FORCE_VIEW_ERROR = "error"; |
| 88 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame^] | 89 | /** When encryption is detected, this flag indivates whether or not we've checked for erros. */ |
| 90 | private boolean mValidationComplete; |
| 91 | /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */ |
| 92 | private boolean mEncryptionGoneBad; |
| 93 | |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 94 | private int mCooldown; |
| 95 | PowerManager.WakeLock mWakeLock; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 96 | private EditText mPasswordEntry; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Used to propagate state through configuration changes (e.g. screen rotation) |
| 100 | */ |
| 101 | private static class NonConfigurationInstanceState { |
| 102 | final PowerManager.WakeLock wakelock; |
| 103 | |
| 104 | NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) { |
| 105 | wakelock = _wakelock; |
| 106 | } |
| 107 | } |
| 108 | |
Jason parks | f1dbf55 | 2011-01-24 16:19:28 -0600 | [diff] [blame] | 109 | // This activity is used to fade the screen to black after the password is entered. |
| 110 | public static class Blank extends Activity { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public void onCreate(Bundle savedInstanceState) { |
| 113 | super.onCreate(savedInstanceState); |
| 114 | setContentView(R.layout.crypt_keeper_blank); |
| 115 | } |
Jason parks | f1dbf55 | 2011-01-24 16:19:28 -0600 | [diff] [blame] | 116 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 117 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 118 | private class DecryptTask extends AsyncTask<String, Void, Integer> { |
| 119 | @Override |
| 120 | protected Integer doInBackground(String... params) { |
| 121 | IMountService service = getMountService(); |
| 122 | try { |
| 123 | return service.decryptStorage(params[0]); |
| 124 | } catch (Exception e) { |
| 125 | Log.e(TAG, "Error while decrypting...", e); |
| 126 | return -1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | protected void onPostExecute(Integer failedAttempts) { |
| 132 | if (failedAttempts == 0) { |
| 133 | // The password was entered successfully. Start the Blank activity |
| 134 | // so this activity animates to black before the devices starts. Note |
| 135 | // It has 1 second to complete the animation or it will be frozen |
| 136 | // until the boot animation comes back up. |
| 137 | Intent intent = new Intent(CryptKeeper.this, Blank.class); |
| 138 | finish(); |
| 139 | startActivity(intent); |
| 140 | } else if (failedAttempts == MAX_FAILED_ATTEMPTS) { |
| 141 | // Factory reset the device. |
| 142 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 143 | } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) { |
| 144 | mCooldown = COOL_DOWN_INTERVAL; |
| 145 | cooldown(); |
| 146 | } else { |
| 147 | TextView tv = (TextView) findViewById(R.id.status); |
| 148 | tv.setText(R.string.try_again); |
| 149 | tv.setVisibility(View.VISIBLE); |
| 150 | |
| 151 | // Reenable the password entry |
| 152 | mPasswordEntry.setEnabled(true); |
| 153 | } |
| 154 | } |
| 155 | } |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 156 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame^] | 157 | private class ValidationTask extends AsyncTask<Void, Void, Boolean> { |
| 158 | @Override |
| 159 | protected Boolean doInBackground(Void... params) { |
| 160 | IMountService service = getMountService(); |
| 161 | try { |
| 162 | int state = service.getEncryptionState(); |
| 163 | if (state == IMountService.ENCRYPTION_STATE_NONE) { |
| 164 | Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption."); |
| 165 | return true; // Unexpected, but fine, I guess... |
| 166 | } |
| 167 | return state == IMountService.ENCRYPTION_STATE_OK; |
| 168 | } catch (RemoteException e) { |
| 169 | Log.w(TAG, "Unable to get encryption state properly"); |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | protected void onPostExecute(Boolean result) { |
| 176 | mValidationComplete = true; |
| 177 | if (Boolean.FALSE.equals(result)) { |
| 178 | Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe."); |
| 179 | mEncryptionGoneBad = true; |
| 180 | } |
| 181 | setupUi(); |
| 182 | } |
| 183 | } |
| 184 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 185 | private final Handler mHandler = new Handler() { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 186 | @Override |
| 187 | public void handleMessage(Message msg) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 188 | switch (msg.what) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 189 | case UPDATE_PROGRESS: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 190 | updateProgress(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 191 | break; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 192 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 193 | case COOLDOWN: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 194 | cooldown(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | }; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 199 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 200 | /** @return whether or not this Activity was started for debugging the UI only. */ |
| 201 | private boolean isDebugView() { |
| 202 | return getIntent().hasExtra(EXTRA_FORCE_VIEW); |
| 203 | } |
| 204 | |
| 205 | /** @return whether or not this Activity was started for debugging the specific UI view only. */ |
| 206 | private boolean isDebugView(String viewType /* non-nullable */) { |
| 207 | return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW)); |
| 208 | } |
| 209 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 210 | @Override |
| 211 | public void onCreate(Bundle savedInstanceState) { |
| 212 | super.onCreate(savedInstanceState); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 213 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 214 | // If we are not encrypted or encrypting, get out quickly. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 215 | String state = SystemProperties.get("vold.decrypt"); |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 216 | if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 217 | // Disable the crypt keeper. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 218 | PackageManager pm = getPackageManager(); |
| 219 | ComponentName name = new ComponentName(this, CryptKeeper.class); |
| 220 | pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0); |
| 221 | return; |
| 222 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 223 | |
Jason parks | 39f1e04 | 2011-01-20 23:29:28 -0600 | [diff] [blame] | 224 | // Disable the status bar |
| 225 | StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 226 | sbm.disable(StatusBarManager.DISABLE_EXPAND |
| 227 | | StatusBarManager.DISABLE_NOTIFICATION_ICONS |
Jason parks | 39f1e04 | 2011-01-20 23:29:28 -0600 | [diff] [blame] | 228 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 229 | | StatusBarManager.DISABLE_SYSTEM_INFO |
| 230 | | StatusBarManager.DISABLE_NAVIGATION |
| 231 | | StatusBarManager.DISABLE_BACK); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 232 | |
| 233 | // Check for (and recover) retained instance data |
| 234 | Object lastInstance = getLastNonConfigurationInstance(); |
| 235 | if (lastInstance instanceof NonConfigurationInstanceState) { |
| 236 | NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance; |
| 237 | mWakeLock = retained.wakelock; |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 238 | Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 239 | } |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame^] | 240 | |
| 241 | // Check the encryption status to ensure something hasn't gone bad. |
| 242 | new ValidationTask().execute((Void[]) null); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 243 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 244 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Note, we defer the state check and screen setup to onStart() because this will be |
| 247 | * re-run if the user clicks the power button (sleeping/waking the screen), and this is |
| 248 | * especially important if we were to lose the wakelock for any reason. |
| 249 | */ |
| 250 | @Override |
| 251 | public void onStart() { |
| 252 | super.onStart(); |
| 253 | |
| 254 | // Check to see why we were started. |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame^] | 255 | if (mValidationComplete) { |
| 256 | setupUi(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Initializes the UI based on the current state of encryption. |
| 262 | * This is idempotent - calling repeatedly will simply re-initialize the UI. |
| 263 | */ |
| 264 | private void setupUi() { |
| 265 | if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) { |
| 266 | setContentView(R.layout.crypt_keeper_progress); |
| 267 | showFactoryReset(); |
| 268 | return; |
| 269 | } |
| 270 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 271 | String progress = SystemProperties.get("vold.encrypt_progress"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame^] | 272 | if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) { |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 273 | setContentView(R.layout.crypt_keeper_progress); |
| 274 | encryptionProgressInit(); |
| 275 | } else { |
| 276 | setContentView(R.layout.crypt_keeper_password_entry); |
| 277 | passwordEntryInit(); |
| 278 | } |
| 279 | } |
| 280 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 281 | @Override |
| 282 | public void onStop() { |
| 283 | super.onStop(); |
| 284 | |
| 285 | mHandler.removeMessages(COOLDOWN); |
| 286 | mHandler.removeMessages(UPDATE_PROGRESS); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop() |
| 291 | * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears |
| 292 | * mWakeLock so the subsequent call to onDestroy does not release it. |
| 293 | */ |
| 294 | @Override |
| 295 | public Object onRetainNonConfigurationInstance() { |
| 296 | NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 297 | Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 298 | mWakeLock = null; |
| 299 | return state; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void onDestroy() { |
| 304 | super.onDestroy(); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 305 | |
| 306 | if (mWakeLock != null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 307 | Log.d(TAG, "Releasing and destroying wakelock"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 308 | mWakeLock.release(); |
| 309 | mWakeLock = null; |
| 310 | } |
| 311 | } |
| 312 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 313 | private void encryptionProgressInit() { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 314 | // Accquire a partial wakelock to prevent the device from sleeping. Note |
| 315 | // we never release this wakelock as we will be restarted after the device |
| 316 | // is encrypted. |
| 317 | |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 318 | Log.d(TAG, "Encryption progress screen initializing."); |
| 319 | if (mWakeLock != null) { |
| 320 | Log.d(TAG, "Acquiring wakelock."); |
| 321 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 322 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 323 | mWakeLock.acquire(); |
| 324 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 325 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 326 | ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); |
| 327 | progressBar.setIndeterminate(true); |
| 328 | |
| 329 | updateProgress(); |
| 330 | } |
| 331 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 332 | private void showFactoryReset() { |
| 333 | // Hide the encryption-bot to make room for the "factory reset" button |
| 334 | findViewById(R.id.encroid).setVisibility(View.GONE); |
| 335 | |
| 336 | // Show the reset button, failure text, and a divider |
| 337 | Button button = (Button) findViewById(R.id.factory_reset); |
| 338 | button.setVisibility(View.VISIBLE); |
| 339 | button.setOnClickListener(new OnClickListener() { |
| 340 | public void onClick(View v) { |
| 341 | // Factory reset the device. |
| 342 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 343 | } |
| 344 | }); |
| 345 | |
| 346 | TextView tv = (TextView) findViewById(R.id.title); |
| 347 | tv.setText(R.string.crypt_keeper_failed_title); |
| 348 | |
| 349 | tv = (TextView) findViewById(R.id.status); |
| 350 | tv.setText(R.string.crypt_keeper_failed_summary); |
| 351 | |
| 352 | View view = findViewById(R.id.bottom_divider); |
Ben Komalo | f0104df | 2011-08-16 17:48:42 -0700 | [diff] [blame] | 353 | if (view != null) { |
| 354 | view.setVisibility(View.VISIBLE); |
| 355 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 358 | private void updateProgress() { |
| 359 | String state = SystemProperties.get("vold.encrypt_progress"); |
| 360 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame^] | 361 | if ("error_partially_encrypted".equals(state)) { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 362 | showFactoryReset(); |
| 363 | return; |
| 364 | } |
| 365 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 366 | int progress = 0; |
| 367 | try { |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 368 | // Force a 50% progress state when debugging the view. |
| 369 | progress = isDebugView() ? 50 : Integer.parseInt(state); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 370 | } catch (Exception e) { |
| 371 | Log.w(TAG, "Error parsing progress: " + e.toString()); |
| 372 | } |
| 373 | |
| 374 | CharSequence status = getText(R.string.crypt_keeper_setup_description); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 375 | Log.v(TAG, "Encryption progress: " + progress); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 376 | TextView tv = (TextView) findViewById(R.id.status); |
| 377 | tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress))); |
| 378 | |
| 379 | // Check the progress every 5 seconds |
| 380 | mHandler.removeMessages(UPDATE_PROGRESS); |
| 381 | mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS, 5000); |
| 382 | } |
| 383 | |
| 384 | private void cooldown() { |
| 385 | TextView tv = (TextView) findViewById(R.id.status); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 386 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 387 | if (mCooldown <= 0) { |
| 388 | // Re-enable the password entry |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 389 | mPasswordEntry.setEnabled(true); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 390 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 391 | tv.setVisibility(View.GONE); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 392 | } else { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 393 | CharSequence template = getText(R.string.crypt_keeper_cooldown); |
| 394 | tv.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown))); |
| 395 | |
| 396 | tv.setVisibility(View.VISIBLE); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 397 | |
| 398 | mCooldown--; |
| 399 | mHandler.removeMessages(COOLDOWN); |
| 400 | mHandler.sendEmptyMessageDelayed(COOLDOWN, 1000); // Tick every second |
| 401 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 402 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 403 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 404 | private void passwordEntryInit() { |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 405 | mPasswordEntry = (EditText) findViewById(R.id.passwordEntry); |
| 406 | mPasswordEntry.setOnEditorActionListener(this); |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 407 | mPasswordEntry.requestFocus(); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 408 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 409 | View imeSwitcher = findViewById(R.id.switch_ime_button); |
| 410 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 411 | Context.INPUT_METHOD_SERVICE); |
| 412 | if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) { |
| 413 | imeSwitcher.setVisibility(View.VISIBLE); |
| 414 | imeSwitcher.setOnClickListener(new OnClickListener() { |
| 415 | public void onClick(View v) { |
| 416 | imm.showInputMethodPicker(); |
| 417 | } |
| 418 | }); |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 419 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 420 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 421 | // Asynchronously throw up the IME, since there are issues with requesting it to be shown |
| 422 | // immediately. |
| 423 | mHandler.postDelayed(new Runnable() { |
| 424 | @Override public void run() { |
| 425 | imm.showSoftInputUnchecked(0, null); |
| 426 | } |
| 427 | }, 0); |
| 428 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 429 | updateEmergencyCallButtonState(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 430 | } |
| 431 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 432 | /** |
| 433 | * Method adapted from com.android.inputmethod.latin.Utils |
| 434 | * |
| 435 | * @param imm The input method manager |
| 436 | * @param shouldIncludeAuxiliarySubtypes |
| 437 | * @return true if we have multiple IMEs to choose from |
| 438 | */ |
| 439 | private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm, |
| 440 | final boolean shouldIncludeAuxiliarySubtypes) { |
| 441 | final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); |
| 442 | |
| 443 | // Number of the filtered IMEs |
| 444 | int filteredImisCount = 0; |
| 445 | |
| 446 | for (InputMethodInfo imi : enabledImis) { |
| 447 | // We can return true immediately after we find two or more filtered IMEs. |
| 448 | if (filteredImisCount > 1) return true; |
| 449 | final List<InputMethodSubtype> subtypes = |
| 450 | imm.getEnabledInputMethodSubtypeList(imi, true); |
| 451 | // IMEs that have no subtypes should be counted. |
| 452 | if (subtypes.isEmpty()) { |
| 453 | ++filteredImisCount; |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | int auxCount = 0; |
| 458 | for (InputMethodSubtype subtype : subtypes) { |
| 459 | if (subtype.isAuxiliary()) { |
| 460 | ++auxCount; |
| 461 | } |
| 462 | } |
| 463 | final int nonAuxCount = subtypes.size() - auxCount; |
| 464 | |
| 465 | // IMEs that have one or more non-auxiliary subtypes should be counted. |
| 466 | // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary |
| 467 | // subtypes should be counted as well. |
| 468 | if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { |
| 469 | ++filteredImisCount; |
| 470 | continue; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return filteredImisCount > 1 |
| 475 | // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled |
| 476 | // input method subtype (The current IME should be LatinIME.) |
| 477 | || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; |
| 478 | } |
| 479 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 480 | private IMountService getMountService() { |
| 481 | IBinder service = ServiceManager.getService("mount"); |
| 482 | if (service != null) { |
| 483 | return IMountService.Stub.asInterface(service); |
| 484 | } |
| 485 | return null; |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 490 | if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) { |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 491 | // Get the password |
| 492 | String password = v.getText().toString(); |
| 493 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 494 | if (TextUtils.isEmpty(password)) { |
| 495 | return true; |
| 496 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 497 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 498 | // Now that we have the password clear the password field. |
| 499 | v.setText(null); |
| 500 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 501 | // Disable the password entry while checking the password. This |
| 502 | // we either be reenabled if the password was wrong or after the |
| 503 | // cooldown period. |
| 504 | mPasswordEntry.setEnabled(false); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 505 | |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 506 | Log.d(TAG, "Attempting to send command to decrypt"); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 507 | new DecryptTask().execute(password); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 508 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | return false; |
| 512 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 513 | |
| 514 | // |
| 515 | // Code to update the state of, and handle clicks from, the "Emergency call" button. |
| 516 | // |
| 517 | // This code is mostly duplicated from the corresponding code in |
| 518 | // LockPatternUtils and LockPatternKeyguardView under frameworks/base. |
| 519 | // |
| 520 | |
| 521 | private void updateEmergencyCallButtonState() { |
| 522 | Button button = (Button) findViewById(R.id.emergencyCallButton); |
| 523 | // The button isn't present at all in some configurations. |
| 524 | if (button == null) return; |
| 525 | |
| 526 | if (isEmergencyCallCapable()) { |
| 527 | button.setVisibility(View.VISIBLE); |
| 528 | button.setOnClickListener(new View.OnClickListener() { |
| 529 | public void onClick(View v) { |
| 530 | takeEmergencyCallAction(); |
| 531 | } |
| 532 | }); |
| 533 | } else { |
| 534 | button.setVisibility(View.GONE); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | int newState = TelephonyManager.getDefault().getCallState(); |
| 539 | int textId; |
| 540 | if (newState == TelephonyManager.CALL_STATE_OFFHOOK) { |
| 541 | // show "return to call" text and show phone icon |
| 542 | textId = R.string.cryptkeeper_return_to_call; |
| 543 | int phoneCallIcon = R.drawable.stat_sys_phone_call; |
| 544 | button.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0); |
| 545 | } else { |
| 546 | textId = R.string.cryptkeeper_emergency_call; |
| 547 | int emergencyIcon = R.drawable.ic_emergency; |
| 548 | button.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0); |
| 549 | } |
| 550 | button.setText(textId); |
| 551 | } |
| 552 | |
| 553 | private boolean isEmergencyCallCapable() { |
| 554 | return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable); |
| 555 | } |
| 556 | |
| 557 | private void takeEmergencyCallAction() { |
| 558 | if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) { |
| 559 | resumeCall(); |
| 560 | } else { |
| 561 | launchEmergencyDialer(); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | private void resumeCall() { |
| 566 | ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); |
| 567 | if (phone != null) { |
| 568 | try { |
| 569 | phone.showCallScreen(); |
| 570 | } catch (RemoteException e) { |
| 571 | Log.e(TAG, "Error calling ITelephony service: " + e); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | private void launchEmergencyDialer() { |
| 577 | Intent intent = new Intent(ACTION_EMERGENCY_DIAL); |
| 578 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 579 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 580 | startActivity(intent); |
| 581 | } |
| 582 | } |