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