blob: 75dd565eee5f07ec200d2219f751138abfe6550c [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;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070025import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060026import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060027import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060028import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060031import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070032import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060033import android.os.ServiceManager;
34import android.os.SystemProperties;
35import android.os.storage.IMountService;
David Brown8373b452011-06-20 12:38:45 -070036import android.telephony.TelephonyManager;
Jason parksec5a45e2011-01-18 15:28:36 -060037import android.text.TextUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.util.Log;
39import android.view.KeyEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080040import android.view.View;
41import android.view.View.OnClickListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060042import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070043import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060044import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070045import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080046import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060047import android.widget.EditText;
48import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060049import android.widget.TextView;
50
Ben Komalo91a2f052011-08-16 17:48:25 -070051import com.android.internal.telephony.ITelephony;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070052
53import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070054
55/**
56 * Settings screens to show the UI flows for encrypting/decrypting the device.
57 *
58 * This may be started via adb for debugging the UI layout, without having to go through
59 * encryption flows everytime. It should be noted that starting the activity in this manner
60 * is only useful for verifying UI-correctness - the behavior will not be identical.
61 * <pre>
62 * $ adb shell pm enable com.android.settings/.CryptKeeper
63 * $ adb shell am start \
64 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
65 * -n com.android.settings/.CryptKeeper
66 * </pre>
67 */
Jason parks8fd5bc92011-01-12 16:03:31 -060068public class CryptKeeper extends Activity implements TextView.OnEditorActionListener {
Jason parksec5a45e2011-01-18 15:28:36 -060069 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060070
Jason parks8fd5bc92011-01-12 16:03:31 -060071 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070072 /** Message sent to us to indicate encryption update progress. */
73 private static final int MESSAGE_UPDATE_PROGRESS = 1;
74 /** Message sent to us to cool-down (waste user's time between password attempts) */
75 private static final int MESSAGE_COOLDOWN = 2;
76 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
77 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060078
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070079 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060080 private static final int MAX_FAILED_ATTEMPTS = 30;
81 private static final int COOL_DOWN_ATTEMPTS = 10;
82 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
83
David Brown8373b452011-06-20 12:38:45 -070084 // Intent action for launching the Emergency Dialer activity.
85 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
86
Ben Komalo91a2f052011-08-16 17:48:25 -070087 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
88 private static final String EXTRA_FORCE_VIEW =
89 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
90 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -070091 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070092 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -070093
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -070094 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -070095 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -070096 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -070097 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
98 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070099 /** A flag to indicate when the back event should be ignored */
100 private boolean mIgnoreBack = false;
Ben Komalo0e666092011-09-01 15:42:00 -0700101
Andy Stadler14997402011-02-01 15:34:59 -0800102 private int mCooldown;
103 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600104 private EditText mPasswordEntry;
Andy Stadler14997402011-02-01 15:34:59 -0800105
106 /**
107 * Used to propagate state through configuration changes (e.g. screen rotation)
108 */
109 private static class NonConfigurationInstanceState {
110 final PowerManager.WakeLock wakelock;
111
112 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
113 wakelock = _wakelock;
114 }
115 }
116
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700117 /**
118 * Activity used to fade the screen to black after the password is entered.
119 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700120 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800121 @Override
122 public void onCreate(Bundle savedInstanceState) {
123 super.onCreate(savedInstanceState);
124 setContentView(R.layout.crypt_keeper_blank);
125 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700126 /** Ignore all back events. */
127 @Override
128 public void onBackPressed() {
129 return;
130 }
Jason parksf1dbf552011-01-24 16:19:28 -0600131 }
Jason parksec5a45e2011-01-18 15:28:36 -0600132
Jason parks06c5ff42011-03-01 10:17:40 -0600133 private class DecryptTask extends AsyncTask<String, Void, Integer> {
134 @Override
135 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700136 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600137 try {
138 return service.decryptStorage(params[0]);
139 } catch (Exception e) {
140 Log.e(TAG, "Error while decrypting...", e);
141 return -1;
142 }
143 }
144
145 @Override
146 protected void onPostExecute(Integer failedAttempts) {
147 if (failedAttempts == 0) {
148 // The password was entered successfully. Start the Blank activity
149 // so this activity animates to black before the devices starts. Note
150 // It has 1 second to complete the animation or it will be frozen
151 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700152 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600153 finish();
154 startActivity(intent);
155 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
156 // Factory reset the device.
157 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
158 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
159 mCooldown = COOL_DOWN_INTERVAL;
160 cooldown();
161 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700162 final TextView status = (TextView) findViewById(R.id.status);
163 status.setText(R.string.try_again);
164 status.setVisibility(View.VISIBLE);
Jason parks06c5ff42011-03-01 10:17:40 -0600165
166 // Reenable the password entry
167 mPasswordEntry.setEnabled(true);
168 }
169 }
170 }
Jason parks75c085e2011-02-10 14:03:47 -0600171
Ben Komalo0e666092011-09-01 15:42:00 -0700172 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
173 @Override
174 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700175 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700176 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700177 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700178 int state = service.getEncryptionState();
179 if (state == IMountService.ENCRYPTION_STATE_NONE) {
180 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
181 return true; // Unexpected, but fine, I guess...
182 }
183 return state == IMountService.ENCRYPTION_STATE_OK;
184 } catch (RemoteException e) {
185 Log.w(TAG, "Unable to get encryption state properly");
186 return true;
187 }
188 }
189
190 @Override
191 protected void onPostExecute(Boolean result) {
192 mValidationComplete = true;
193 if (Boolean.FALSE.equals(result)) {
194 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
195 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700196 } else {
197 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700198 }
199 setupUi();
200 }
201 }
202
Ben Komalo91a2f052011-08-16 17:48:25 -0700203 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600204 @Override
205 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600206 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700207 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600208 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600209 break;
Jason parks35933812011-01-21 15:48:20 -0600210
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700211 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600212 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600213 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700214
215 case MESSAGE_NOTIFY:
216 notifyUser();
217 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600218 }
219 }
220 };
Jason parks35933812011-01-21 15:48:20 -0600221
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700222 private AudioManager mAudioManager;
223
Ben Komalo91a2f052011-08-16 17:48:25 -0700224 /** @return whether or not this Activity was started for debugging the UI only. */
225 private boolean isDebugView() {
226 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
227 }
228
229 /** @return whether or not this Activity was started for debugging the specific UI view only. */
230 private boolean isDebugView(String viewType /* non-nullable */) {
231 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
232 }
233
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700234 /**
235 * Notify the user that we are awaiting input. Currently this sends an audio alert.
236 */
237 private void notifyUser() {
238 Log.d(TAG, "Notifying user that we are waiting for input...");
239 if (mAudioManager != null) {
240 try {
241 // Play the standard keypress sound at full volume. This should be available on
242 // every device. We cannot play a ringtone here because media services aren't
243 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
244 // on tablet devices. The idea is to alert the user that something is needed: this
245 // does not have to be pleasing.
246 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
247 } catch (Exception e) {
248 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
249 }
250 }
251 // Notify the user again in 30 seconds.
252 mHandler.removeMessages(MESSAGE_NOTIFY);
253 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 30 * 1000);
254 }
255
256 /**
257 * Ignore back events after the user has entered the decrypt screen and while the device is
258 * encrypting.
259 */
260 @Override
261 public void onBackPressed() {
262 if (mIgnoreBack)
263 return;
264 super.onBackPressed();
265 }
266
Jason parks8fd5bc92011-01-12 16:03:31 -0600267 @Override
268 public void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600270
Andy Stadler95974062011-02-01 17:35:20 -0800271 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700272 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700273 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600274 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600275 PackageManager pm = getPackageManager();
276 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700277 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
278 PackageManager.DONT_KILL_APP);
279 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700280 // want to be running, so need to finish this activity. We can count
281 // on the activity manager re-launching the new home app upon finishing
282 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700283 // NOTE: This is really grungy. I think it would be better for the
284 // activity manager to explicitly launch the crypt keeper instead of
285 // home in the situation where we need to decrypt the device
286 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600287 return;
288 }
Jason parks35933812011-01-21 15:48:20 -0600289
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700290 // Disable the status bar, but do NOT disable back because the user needs a way to go
291 // from keyboard settings and back to the password screen.
Jason parks39f1e042011-01-20 23:29:28 -0600292 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Andy Stadler13d62042011-01-31 19:21:37 -0800293 sbm.disable(StatusBarManager.DISABLE_EXPAND
294 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
Jason parks39f1e042011-01-20 23:29:28 -0600295 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Andy Stadler13d62042011-01-31 19:21:37 -0800296 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -0400297 | StatusBarManager.DISABLE_HOME
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700298 | StatusBarManager.DISABLE_RECENT);
Andy Stadler14997402011-02-01 15:34:59 -0800299
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700300 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800301 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700302 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800303 if (lastInstance instanceof NonConfigurationInstanceState) {
304 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
305 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700306 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800307 }
Jason parksec5a45e2011-01-18 15:28:36 -0600308 }
Jason parks35933812011-01-21 15:48:20 -0600309
Andy Stadler95974062011-02-01 17:35:20 -0800310 /**
311 * Note, we defer the state check and screen setup to onStart() because this will be
312 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
313 * especially important if we were to lose the wakelock for any reason.
314 */
315 @Override
316 public void onStart() {
317 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700318 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700319 }
320
321 /**
322 * Initializes the UI based on the current state of encryption.
323 * This is idempotent - calling repeatedly will simply re-initialize the UI.
324 */
325 private void setupUi() {
326 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
327 setContentView(R.layout.crypt_keeper_progress);
328 showFactoryReset();
329 return;
330 }
331
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700332 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700333 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800334 setContentView(R.layout.crypt_keeper_progress);
335 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700336 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800337 setContentView(R.layout.crypt_keeper_password_entry);
338 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700339 } else if (!mValidationRequested) {
340 // We're supposed to be encrypted, but no validation has been done.
341 new ValidationTask().execute((Void[]) null);
342 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800343 }
344 }
345
Jason parksf8217302011-01-26 13:11:42 -0600346 @Override
347 public void onStop() {
348 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700349 mHandler.removeMessages(MESSAGE_COOLDOWN);
350 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
351 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800352 }
353
354 /**
355 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
356 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
357 * mWakeLock so the subsequent call to onDestroy does not release it.
358 */
359 @Override
360 public Object onRetainNonConfigurationInstance() {
361 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700362 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800363 mWakeLock = null;
364 return state;
365 }
366
367 @Override
368 public void onDestroy() {
369 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600370
371 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700372 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600373 mWakeLock.release();
374 mWakeLock = null;
375 }
376 }
377
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700378 /**
379 * Start encrypting the device.
380 */
Jason parksec5a45e2011-01-18 15:28:36 -0600381 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600382 // Accquire a partial wakelock to prevent the device from sleeping. Note
383 // we never release this wakelock as we will be restarted after the device
384 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700385 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700386 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700387 Log.d(TAG, "Acquiring wakelock.");
388 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
389 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
390 mWakeLock.acquire();
391 }
Jason parks35933812011-01-21 15:48:20 -0600392
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700393 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700394 // Ignore all back presses from now, both hard and soft keys.
395 mIgnoreBack = true;
396 // Start the first run of progress manually. This method sets up messages to occur at
397 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600398 updateProgress();
399 }
400
Andy Stadler13d62042011-01-31 19:21:37 -0800401 private void showFactoryReset() {
402 // Hide the encryption-bot to make room for the "factory reset" button
403 findViewById(R.id.encroid).setVisibility(View.GONE);
404
405 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700406 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800407 button.setVisibility(View.VISIBLE);
408 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700409 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800410 public void onClick(View v) {
411 // Factory reset the device.
412 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
413 }
414 });
415
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700416 // Alert the user of the failure.
417 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
418 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800419
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700420 final View view = findViewById(R.id.bottom_divider);
421 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700422 if (view != null) {
423 view.setVisibility(View.VISIBLE);
424 }
Andy Stadler13d62042011-01-31 19:21:37 -0800425 }
426
Jason parksf8217302011-01-26 13:11:42 -0600427 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700428 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600429
Ben Komalo0e666092011-09-01 15:42:00 -0700430 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800431 showFactoryReset();
432 return;
433 }
434
Jason parksf8217302011-01-26 13:11:42 -0600435 int progress = 0;
436 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700437 // Force a 50% progress state when debugging the view.
438 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600439 } catch (Exception e) {
440 Log.w(TAG, "Error parsing progress: " + e.toString());
441 }
442
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700443 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700444 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700445 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700446 if (tv != null) {
447 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
448 }
Jason parksf8217302011-01-26 13:11:42 -0600449 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700450 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
451 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600452 }
453
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700454 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600455 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700456 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800457
Jason parksf8217302011-01-26 13:11:42 -0600458 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700459 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600460 mPasswordEntry.setEnabled(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700461 mIgnoreBack = false;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700462 status.setVisibility(View.GONE);
Jason parksf8217302011-01-26 13:11:42 -0600463 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800464 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700465 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800466
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700467 status.setVisibility(View.VISIBLE);
Jason parksf8217302011-01-26 13:11:42 -0600468
469 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700470 mHandler.removeMessages(MESSAGE_COOLDOWN);
471 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600472 }
Jason parksec5a45e2011-01-18 15:28:36 -0600473 }
Jason parks35933812011-01-21 15:48:20 -0600474
Jason parksec5a45e2011-01-18 15:28:36 -0600475 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600476 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
477 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700478 mPasswordEntry.requestFocus();
Jason parks35933812011-01-21 15:48:20 -0600479
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700480 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700481 final InputMethodManager imm = (InputMethodManager) getSystemService(
482 Context.INPUT_METHOD_SERVICE);
483 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
484 imeSwitcher.setVisibility(View.VISIBLE);
485 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700486 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700487 public void onClick(View v) {
488 imm.showInputMethodPicker();
489 }
490 });
Jason parks00046d62011-06-13 17:38:45 -0500491 }
David Brown8373b452011-06-20 12:38:45 -0700492
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700493 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
494 // is completely non-functional, and we want the user to notice the device and enter a
495 // password.
496 if (mWakeLock == null) {
497 Log.d(TAG, "Acquiring wakelock.");
498 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
499 if (pm != null) {
500 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
501 mWakeLock.acquire();
502 }
503 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700504 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
505 // immediately.
506 mHandler.postDelayed(new Runnable() {
507 @Override public void run() {
508 imm.showSoftInputUnchecked(0, null);
509 }
510 }, 0);
511
David Brown8373b452011-06-20 12:38:45 -0700512 updateEmergencyCallButtonState();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700513 // Notify the user that we are waiting for him to enter the password to get the device
514 // out of this completely dead state.
515 notifyUser();
Jason parks8fd5bc92011-01-12 16:03:31 -0600516 }
517
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700518 /**
519 * Method adapted from com.android.inputmethod.latin.Utils
520 *
521 * @param imm The input method manager
522 * @param shouldIncludeAuxiliarySubtypes
523 * @return true if we have multiple IMEs to choose from
524 */
525 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
526 final boolean shouldIncludeAuxiliarySubtypes) {
527 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
528
529 // Number of the filtered IMEs
530 int filteredImisCount = 0;
531
532 for (InputMethodInfo imi : enabledImis) {
533 // We can return true immediately after we find two or more filtered IMEs.
534 if (filteredImisCount > 1) return true;
535 final List<InputMethodSubtype> subtypes =
536 imm.getEnabledInputMethodSubtypeList(imi, true);
537 // IMEs that have no subtypes should be counted.
538 if (subtypes.isEmpty()) {
539 ++filteredImisCount;
540 continue;
541 }
542
543 int auxCount = 0;
544 for (InputMethodSubtype subtype : subtypes) {
545 if (subtype.isAuxiliary()) {
546 ++auxCount;
547 }
548 }
549 final int nonAuxCount = subtypes.size() - auxCount;
550
551 // IMEs that have one or more non-auxiliary subtypes should be counted.
552 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
553 // subtypes should be counted as well.
554 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
555 ++filteredImisCount;
556 continue;
557 }
558 }
559
560 return filteredImisCount > 1
561 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
562 // input method subtype (The current IME should be LatinIME.)
563 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
564 }
565
Jason parks8fd5bc92011-01-12 16:03:31 -0600566 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700567 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600568 if (service != null) {
569 return IMountService.Stub.asInterface(service);
570 }
571 return null;
572 }
573
574 @Override
575 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500576 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600577 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700578 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600579
Jason parksec5a45e2011-01-18 15:28:36 -0600580 if (TextUtils.isEmpty(password)) {
581 return true;
582 }
Jason parks35933812011-01-21 15:48:20 -0600583
Jason parks8fd5bc92011-01-12 16:03:31 -0600584 // Now that we have the password clear the password field.
585 v.setText(null);
586
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700587 // Disable the password entry and back keypress while checking the password. These
588 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600589 mPasswordEntry.setEnabled(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700590 mIgnoreBack = true;
Jason parks8fd5bc92011-01-12 16:03:31 -0600591
Ben Komalo04606752011-08-18 14:50:26 -0700592 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600593 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600594
Jason parks8fd5bc92011-01-12 16:03:31 -0600595 return true;
596 }
597 return false;
598 }
David Brown8373b452011-06-20 12:38:45 -0700599
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700600 /**
601 * Code to update the state of, and handle clicks from, the "Emergency call" button.
602 *
603 * This code is mostly duplicated from the corresponding code in
604 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
605 */
David Brown8373b452011-06-20 12:38:45 -0700606 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700607 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700608 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700609 if (emergencyCall == null)
610 return;
David Brown8373b452011-06-20 12:38:45 -0700611
612 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700613 emergencyCall.setVisibility(View.VISIBLE);
614 emergencyCall.setOnClickListener(new View.OnClickListener() {
615 @Override
616
David Brown8373b452011-06-20 12:38:45 -0700617 public void onClick(View v) {
618 takeEmergencyCallAction();
619 }
620 });
621 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700622 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700623 return;
624 }
625
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700626 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700627 int textId;
628 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700629 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700630 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700631 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
632 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700633 } else {
634 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700635 final int emergencyIcon = R.drawable.ic_emergency;
636 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700637 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700638 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700639 }
640
641 private boolean isEmergencyCallCapable() {
642 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
643 }
644
645 private void takeEmergencyCallAction() {
646 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
647 resumeCall();
648 } else {
649 launchEmergencyDialer();
650 }
651 }
652
653 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700654 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700655 if (phone != null) {
656 try {
657 phone.showCallScreen();
658 } catch (RemoteException e) {
659 Log.e(TAG, "Error calling ITelephony service: " + e);
660 }
661 }
662 }
663
664 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700665 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700666 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
667 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
668 startActivity(intent);
669 }
670}