blob: d58dddb93fd1d90bcffba37d4f7d279f27fb9997 [file] [log] [blame]
Jason parks8fd5bc92011-01-12 16:03:31 -06001/*
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
17package com.android.settings;
18
Jason parks8fd5bc92011-01-12 16:03:31 -060019import android.app.Activity;
20import android.app.StatusBarManager;
21import android.content.ComponentName;
22import android.content.Context;
Jason parksec5a45e2011-01-18 15:28:36 -060023import android.content.Intent;
Jason parks8fd5bc92011-01-12 16:03:31 -060024import android.content.pm.PackageManager;
Jason parks06c5ff42011-03-01 10:17:40 -060025import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060026import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060027import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060028import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060029import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060030import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070031import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060032import android.os.ServiceManager;
33import android.os.SystemProperties;
34import android.os.storage.IMountService;
David Brown8373b452011-06-20 12:38:45 -070035import android.telephony.TelephonyManager;
Jason parksec5a45e2011-01-18 15:28:36 -060036import android.text.TextUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060037import android.util.Log;
38import android.view.KeyEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080039import android.view.View;
40import android.view.View.OnClickListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060041import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070042import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060043import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070044import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080045import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060046import android.widget.EditText;
47import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060048import android.widget.TextView;
49
Ben Komalo91a2f052011-08-16 17:48:25 -070050import com.android.internal.telephony.ITelephony;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070051
52import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070053
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 parks8fd5bc92011-01-12 16:03:31 -060067public class CryptKeeper extends Activity implements TextView.OnEditorActionListener {
Jason parksec5a45e2011-01-18 15:28:36 -060068 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060069
Jason parks8fd5bc92011-01-12 16:03:31 -060070 private static final String DECRYPT_STATE = "trigger_restart_framework";
Jason parksec5a45e2011-01-18 15:28:36 -060071
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 Brown8373b452011-06-20 12:38:45 -070079 // Intent action for launching the Emergency Dialer activity.
80 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
81
Ben Komalo91a2f052011-08-16 17:48:25 -070082 // 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 Komalo0e666092011-09-01 15:42:00 -070089 /** When encryption is detected, this flag indivates whether or not we've checked for erros. */
90 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -070091 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -070092 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
93 private boolean mEncryptionGoneBad;
94
Andy Stadler14997402011-02-01 15:34:59 -080095 private int mCooldown;
96 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -060097 private EditText mPasswordEntry;
Andy Stadler14997402011-02-01 15:34:59 -080098
99 /**
100 * Used to propagate state through configuration changes (e.g. screen rotation)
101 */
102 private static class NonConfigurationInstanceState {
103 final PowerManager.WakeLock wakelock;
104
105 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
106 wakelock = _wakelock;
107 }
108 }
109
Jason parksf1dbf552011-01-24 16:19:28 -0600110 // This activity is used to fade the screen to black after the password is entered.
111 public static class Blank extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800112 @Override
113 public void onCreate(Bundle savedInstanceState) {
114 super.onCreate(savedInstanceState);
115 setContentView(R.layout.crypt_keeper_blank);
116 }
Jason parksf1dbf552011-01-24 16:19:28 -0600117 }
Jason parksec5a45e2011-01-18 15:28:36 -0600118
Jason parks06c5ff42011-03-01 10:17:40 -0600119 private class DecryptTask extends AsyncTask<String, Void, Integer> {
120 @Override
121 protected Integer doInBackground(String... params) {
122 IMountService service = getMountService();
123 try {
124 return service.decryptStorage(params[0]);
125 } catch (Exception e) {
126 Log.e(TAG, "Error while decrypting...", e);
127 return -1;
128 }
129 }
130
131 @Override
132 protected void onPostExecute(Integer failedAttempts) {
133 if (failedAttempts == 0) {
134 // The password was entered successfully. Start the Blank activity
135 // so this activity animates to black before the devices starts. Note
136 // It has 1 second to complete the animation or it will be frozen
137 // until the boot animation comes back up.
138 Intent intent = new Intent(CryptKeeper.this, Blank.class);
139 finish();
140 startActivity(intent);
141 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
142 // Factory reset the device.
143 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
144 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
145 mCooldown = COOL_DOWN_INTERVAL;
146 cooldown();
147 } else {
148 TextView tv = (TextView) findViewById(R.id.status);
149 tv.setText(R.string.try_again);
150 tv.setVisibility(View.VISIBLE);
151
152 // Reenable the password entry
153 mPasswordEntry.setEnabled(true);
154 }
155 }
156 }
Jason parks75c085e2011-02-10 14:03:47 -0600157
Ben Komalo0e666092011-09-01 15:42:00 -0700158 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
159 @Override
160 protected Boolean doInBackground(Void... params) {
161 IMountService service = getMountService();
162 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700163 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700164 int state = service.getEncryptionState();
165 if (state == IMountService.ENCRYPTION_STATE_NONE) {
166 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
167 return true; // Unexpected, but fine, I guess...
168 }
169 return state == IMountService.ENCRYPTION_STATE_OK;
170 } catch (RemoteException e) {
171 Log.w(TAG, "Unable to get encryption state properly");
172 return true;
173 }
174 }
175
176 @Override
177 protected void onPostExecute(Boolean result) {
178 mValidationComplete = true;
179 if (Boolean.FALSE.equals(result)) {
180 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
181 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700182 } else {
183 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700184 }
185 setupUi();
186 }
187 }
188
Ben Komalo91a2f052011-08-16 17:48:25 -0700189 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600190 @Override
191 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600192 switch (msg.what) {
Jason parksec5a45e2011-01-18 15:28:36 -0600193 case UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600194 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600195 break;
Jason parks35933812011-01-21 15:48:20 -0600196
Jason parksec5a45e2011-01-18 15:28:36 -0600197 case COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600198 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600199 break;
200 }
201 }
202 };
Jason parks35933812011-01-21 15:48:20 -0600203
Ben Komalo91a2f052011-08-16 17:48:25 -0700204 /** @return whether or not this Activity was started for debugging the UI only. */
205 private boolean isDebugView() {
206 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
207 }
208
209 /** @return whether or not this Activity was started for debugging the specific UI view only. */
210 private boolean isDebugView(String viewType /* non-nullable */) {
211 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
212 }
213
Jason parks8fd5bc92011-01-12 16:03:31 -0600214 @Override
215 public void onCreate(Bundle savedInstanceState) {
216 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600217
Andy Stadler95974062011-02-01 17:35:20 -0800218 // If we are not encrypted or encrypting, get out quickly.
Jason parks8fd5bc92011-01-12 16:03:31 -0600219 String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700220 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600221 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600222 PackageManager pm = getPackageManager();
223 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700224 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
225 PackageManager.DONT_KILL_APP);
226 // Typically CryptKeeper is launched as the home app. We didn't
227 // want to be running, so need to finish this activity and re-launch
228 // its intent now that we are not in the way of doing what is really
229 // supposed to happen.
230 // NOTE: This is really grungy. I think it would be better for the
231 // activity manager to explicitly launch the crypt keeper instead of
232 // home in the situation where we need to decrypt the device
233 finish();
234 Intent intent = getIntent();
235 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
236 intent.setComponent(null);
237 startActivity(intent);
Jason parks8fd5bc92011-01-12 16:03:31 -0600238 return;
239 }
Jason parks35933812011-01-21 15:48:20 -0600240
Jason parks39f1e042011-01-20 23:29:28 -0600241 // Disable the status bar
242 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Andy Stadler13d62042011-01-31 19:21:37 -0800243 sbm.disable(StatusBarManager.DISABLE_EXPAND
244 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
Jason parks39f1e042011-01-20 23:29:28 -0600245 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Andy Stadler13d62042011-01-31 19:21:37 -0800246 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -0400247 | StatusBarManager.DISABLE_HOME
248 | StatusBarManager.DISABLE_RECENT
Andy Stadler13d62042011-01-31 19:21:37 -0800249 | StatusBarManager.DISABLE_BACK);
Andy Stadler14997402011-02-01 15:34:59 -0800250
251 // Check for (and recover) retained instance data
252 Object lastInstance = getLastNonConfigurationInstance();
253 if (lastInstance instanceof NonConfigurationInstanceState) {
254 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
255 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700256 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800257 }
Jason parksec5a45e2011-01-18 15:28:36 -0600258 }
Jason parks35933812011-01-21 15:48:20 -0600259
Andy Stadler95974062011-02-01 17:35:20 -0800260 /**
261 * Note, we defer the state check and screen setup to onStart() because this will be
262 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
263 * especially important if we were to lose the wakelock for any reason.
264 */
265 @Override
266 public void onStart() {
267 super.onStart();
268
Ben Komalod4758ef2011-09-08 14:36:08 -0700269 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700270 }
271
272 /**
273 * Initializes the UI based on the current state of encryption.
274 * This is idempotent - calling repeatedly will simply re-initialize the UI.
275 */
276 private void setupUi() {
277 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
278 setContentView(R.layout.crypt_keeper_progress);
279 showFactoryReset();
280 return;
281 }
282
Andy Stadler95974062011-02-01 17:35:20 -0800283 String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700284 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800285 setContentView(R.layout.crypt_keeper_progress);
286 encryptionProgressInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700287 } else if (mValidationComplete) {
Andy Stadler95974062011-02-01 17:35:20 -0800288 setContentView(R.layout.crypt_keeper_password_entry);
289 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700290 } else if (!mValidationRequested) {
291 // We're supposed to be encrypted, but no validation has been done.
292 new ValidationTask().execute((Void[]) null);
293 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800294 }
295 }
296
Jason parksf8217302011-01-26 13:11:42 -0600297 @Override
298 public void onStop() {
299 super.onStop();
300
301 mHandler.removeMessages(COOLDOWN);
302 mHandler.removeMessages(UPDATE_PROGRESS);
Andy Stadler14997402011-02-01 15:34:59 -0800303 }
304
305 /**
306 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
307 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
308 * mWakeLock so the subsequent call to onDestroy does not release it.
309 */
310 @Override
311 public Object onRetainNonConfigurationInstance() {
312 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700313 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800314 mWakeLock = null;
315 return state;
316 }
317
318 @Override
319 public void onDestroy() {
320 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600321
322 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700323 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600324 mWakeLock.release();
325 mWakeLock = null;
326 }
327 }
328
Jason parksec5a45e2011-01-18 15:28:36 -0600329 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600330 // Accquire a partial wakelock to prevent the device from sleeping. Note
331 // we never release this wakelock as we will be restarted after the device
332 // is encrypted.
333
Ben Komalo04606752011-08-18 14:50:26 -0700334 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700335 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700336 Log.d(TAG, "Acquiring wakelock.");
337 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
338 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
339 mWakeLock.acquire();
340 }
Jason parks35933812011-01-21 15:48:20 -0600341
Jason parksf8217302011-01-26 13:11:42 -0600342 ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
343 progressBar.setIndeterminate(true);
344
345 updateProgress();
346 }
347
Andy Stadler13d62042011-01-31 19:21:37 -0800348 private void showFactoryReset() {
349 // Hide the encryption-bot to make room for the "factory reset" button
350 findViewById(R.id.encroid).setVisibility(View.GONE);
351
352 // Show the reset button, failure text, and a divider
353 Button button = (Button) findViewById(R.id.factory_reset);
354 button.setVisibility(View.VISIBLE);
355 button.setOnClickListener(new OnClickListener() {
356 public void onClick(View v) {
357 // Factory reset the device.
358 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
359 }
360 });
361
362 TextView tv = (TextView) findViewById(R.id.title);
363 tv.setText(R.string.crypt_keeper_failed_title);
364
365 tv = (TextView) findViewById(R.id.status);
366 tv.setText(R.string.crypt_keeper_failed_summary);
367
368 View view = findViewById(R.id.bottom_divider);
Ben Komalof0104df2011-08-16 17:48:42 -0700369 if (view != null) {
370 view.setVisibility(View.VISIBLE);
371 }
Andy Stadler13d62042011-01-31 19:21:37 -0800372 }
373
Jason parksf8217302011-01-26 13:11:42 -0600374 private void updateProgress() {
375 String state = SystemProperties.get("vold.encrypt_progress");
376
Ben Komalo0e666092011-09-01 15:42:00 -0700377 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800378 showFactoryReset();
379 return;
380 }
381
Jason parksf8217302011-01-26 13:11:42 -0600382 int progress = 0;
383 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700384 // Force a 50% progress state when debugging the view.
385 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600386 } catch (Exception e) {
387 Log.w(TAG, "Error parsing progress: " + e.toString());
388 }
389
390 CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700391 Log.v(TAG, "Encryption progress: " + progress);
Jason parksf8217302011-01-26 13:11:42 -0600392 TextView tv = (TextView) findViewById(R.id.status);
393 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
394
395 // Check the progress every 5 seconds
396 mHandler.removeMessages(UPDATE_PROGRESS);
397 mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS, 5000);
398 }
399
400 private void cooldown() {
401 TextView tv = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800402
Jason parksf8217302011-01-26 13:11:42 -0600403 if (mCooldown <= 0) {
404 // Re-enable the password entry
Jason parks06c5ff42011-03-01 10:17:40 -0600405 mPasswordEntry.setEnabled(true);
Jason parksf8217302011-01-26 13:11:42 -0600406
Andy Stadler13d62042011-01-31 19:21:37 -0800407 tv.setVisibility(View.GONE);
Jason parksf8217302011-01-26 13:11:42 -0600408 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800409 CharSequence template = getText(R.string.crypt_keeper_cooldown);
410 tv.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
411
412 tv.setVisibility(View.VISIBLE);
Jason parksf8217302011-01-26 13:11:42 -0600413
414 mCooldown--;
415 mHandler.removeMessages(COOLDOWN);
416 mHandler.sendEmptyMessageDelayed(COOLDOWN, 1000); // Tick every second
417 }
Jason parksec5a45e2011-01-18 15:28:36 -0600418 }
Jason parks35933812011-01-21 15:48:20 -0600419
Jason parksec5a45e2011-01-18 15:28:36 -0600420 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600421 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
422 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700423 mPasswordEntry.requestFocus();
Jason parks35933812011-01-21 15:48:20 -0600424
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700425 View imeSwitcher = findViewById(R.id.switch_ime_button);
426 final InputMethodManager imm = (InputMethodManager) getSystemService(
427 Context.INPUT_METHOD_SERVICE);
428 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
429 imeSwitcher.setVisibility(View.VISIBLE);
430 imeSwitcher.setOnClickListener(new OnClickListener() {
431 public void onClick(View v) {
432 imm.showInputMethodPicker();
433 }
434 });
Jason parks00046d62011-06-13 17:38:45 -0500435 }
David Brown8373b452011-06-20 12:38:45 -0700436
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700437 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
438 // immediately.
439 mHandler.postDelayed(new Runnable() {
440 @Override public void run() {
441 imm.showSoftInputUnchecked(0, null);
442 }
443 }, 0);
444
David Brown8373b452011-06-20 12:38:45 -0700445 updateEmergencyCallButtonState();
Jason parks8fd5bc92011-01-12 16:03:31 -0600446 }
447
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700448 /**
449 * Method adapted from com.android.inputmethod.latin.Utils
450 *
451 * @param imm The input method manager
452 * @param shouldIncludeAuxiliarySubtypes
453 * @return true if we have multiple IMEs to choose from
454 */
455 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
456 final boolean shouldIncludeAuxiliarySubtypes) {
457 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
458
459 // Number of the filtered IMEs
460 int filteredImisCount = 0;
461
462 for (InputMethodInfo imi : enabledImis) {
463 // We can return true immediately after we find two or more filtered IMEs.
464 if (filteredImisCount > 1) return true;
465 final List<InputMethodSubtype> subtypes =
466 imm.getEnabledInputMethodSubtypeList(imi, true);
467 // IMEs that have no subtypes should be counted.
468 if (subtypes.isEmpty()) {
469 ++filteredImisCount;
470 continue;
471 }
472
473 int auxCount = 0;
474 for (InputMethodSubtype subtype : subtypes) {
475 if (subtype.isAuxiliary()) {
476 ++auxCount;
477 }
478 }
479 final int nonAuxCount = subtypes.size() - auxCount;
480
481 // IMEs that have one or more non-auxiliary subtypes should be counted.
482 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
483 // subtypes should be counted as well.
484 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
485 ++filteredImisCount;
486 continue;
487 }
488 }
489
490 return filteredImisCount > 1
491 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
492 // input method subtype (The current IME should be LatinIME.)
493 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
494 }
495
Jason parks8fd5bc92011-01-12 16:03:31 -0600496 private IMountService getMountService() {
497 IBinder service = ServiceManager.getService("mount");
498 if (service != null) {
499 return IMountService.Stub.asInterface(service);
500 }
501 return null;
502 }
503
504 @Override
505 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500506 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600507 // Get the password
508 String password = v.getText().toString();
509
Jason parksec5a45e2011-01-18 15:28:36 -0600510 if (TextUtils.isEmpty(password)) {
511 return true;
512 }
Jason parks35933812011-01-21 15:48:20 -0600513
Jason parks8fd5bc92011-01-12 16:03:31 -0600514 // Now that we have the password clear the password field.
515 v.setText(null);
516
Jason parks06c5ff42011-03-01 10:17:40 -0600517 // Disable the password entry while checking the password. This
518 // we either be reenabled if the password was wrong or after the
519 // cooldown period.
520 mPasswordEntry.setEnabled(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600521
Ben Komalo04606752011-08-18 14:50:26 -0700522 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600523 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600524
Jason parks8fd5bc92011-01-12 16:03:31 -0600525 return true;
526 }
527 return false;
528 }
David Brown8373b452011-06-20 12:38:45 -0700529
530 //
531 // Code to update the state of, and handle clicks from, the "Emergency call" button.
532 //
533 // This code is mostly duplicated from the corresponding code in
534 // LockPatternUtils and LockPatternKeyguardView under frameworks/base.
535 //
536
537 private void updateEmergencyCallButtonState() {
538 Button button = (Button) findViewById(R.id.emergencyCallButton);
539 // The button isn't present at all in some configurations.
540 if (button == null) return;
541
542 if (isEmergencyCallCapable()) {
543 button.setVisibility(View.VISIBLE);
544 button.setOnClickListener(new View.OnClickListener() {
545 public void onClick(View v) {
546 takeEmergencyCallAction();
547 }
548 });
549 } else {
550 button.setVisibility(View.GONE);
551 return;
552 }
553
554 int newState = TelephonyManager.getDefault().getCallState();
555 int textId;
556 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
557 // show "return to call" text and show phone icon
558 textId = R.string.cryptkeeper_return_to_call;
559 int phoneCallIcon = R.drawable.stat_sys_phone_call;
560 button.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
561 } else {
562 textId = R.string.cryptkeeper_emergency_call;
563 int emergencyIcon = R.drawable.ic_emergency;
564 button.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
565 }
566 button.setText(textId);
567 }
568
569 private boolean isEmergencyCallCapable() {
570 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
571 }
572
573 private void takeEmergencyCallAction() {
574 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
575 resumeCall();
576 } else {
577 launchEmergencyDialer();
578 }
579 }
580
581 private void resumeCall() {
582 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
583 if (phone != null) {
584 try {
585 phone.showCallScreen();
586 } catch (RemoteException e) {
587 Log.e(TAG, "Error calling ITelephony service: " + e);
588 }
589 }
590 }
591
592 private void launchEmergencyDialer() {
593 Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
594 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
595 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
596 startActivity(intent);
597 }
598}