blob: 2739d9127a0b49ea931fd5f9deb851759021dc09 [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;
Andy Stadler14997402011-02-01 15:34:59 -0800101 private int mCooldown;
102 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600103 private EditText mPasswordEntry;
Andy Stadler14997402011-02-01 15:34:59 -0800104
105 /**
106 * Used to propagate state through configuration changes (e.g. screen rotation)
107 */
108 private static class NonConfigurationInstanceState {
109 final PowerManager.WakeLock wakelock;
110
111 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
112 wakelock = _wakelock;
113 }
114 }
115
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700116 /**
117 * Activity used to fade the screen to black after the password is entered.
118 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700119 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800120 @Override
121 public void onCreate(Bundle savedInstanceState) {
122 super.onCreate(savedInstanceState);
123 setContentView(R.layout.crypt_keeper_blank);
124 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700125 /** Ignore all back events. */
126 @Override
127 public void onBackPressed() {
128 return;
129 }
Jason parksf1dbf552011-01-24 16:19:28 -0600130 }
Jason parksec5a45e2011-01-18 15:28:36 -0600131
Jason parks06c5ff42011-03-01 10:17:40 -0600132 private class DecryptTask extends AsyncTask<String, Void, Integer> {
133 @Override
134 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700135 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600136 try {
137 return service.decryptStorage(params[0]);
138 } catch (Exception e) {
139 Log.e(TAG, "Error while decrypting...", e);
140 return -1;
141 }
142 }
143
144 @Override
145 protected void onPostExecute(Integer failedAttempts) {
146 if (failedAttempts == 0) {
147 // The password was entered successfully. Start the Blank activity
148 // so this activity animates to black before the devices starts. Note
149 // It has 1 second to complete the animation or it will be frozen
150 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700151 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600152 finish();
153 startActivity(intent);
154 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
155 // Factory reset the device.
156 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
157 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
158 mCooldown = COOL_DOWN_INTERVAL;
159 cooldown();
160 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700161 final TextView status = (TextView) findViewById(R.id.status);
162 status.setText(R.string.try_again);
163 status.setVisibility(View.VISIBLE);
Jason parks06c5ff42011-03-01 10:17:40 -0600164
165 // Reenable the password entry
166 mPasswordEntry.setEnabled(true);
167 }
168 }
169 }
Jason parks75c085e2011-02-10 14:03:47 -0600170
Ben Komalo0e666092011-09-01 15:42:00 -0700171 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
172 @Override
173 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700174 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700175 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700176 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700177 int state = service.getEncryptionState();
178 if (state == IMountService.ENCRYPTION_STATE_NONE) {
179 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
180 return true; // Unexpected, but fine, I guess...
181 }
182 return state == IMountService.ENCRYPTION_STATE_OK;
183 } catch (RemoteException e) {
184 Log.w(TAG, "Unable to get encryption state properly");
185 return true;
186 }
187 }
188
189 @Override
190 protected void onPostExecute(Boolean result) {
191 mValidationComplete = true;
192 if (Boolean.FALSE.equals(result)) {
193 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
194 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700195 } else {
196 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700197 }
198 setupUi();
199 }
200 }
201
Ben Komalo91a2f052011-08-16 17:48:25 -0700202 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600203 @Override
204 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600205 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700206 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600207 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600208 break;
Jason parks35933812011-01-21 15:48:20 -0600209
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700210 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600211 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600212 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700213
214 case MESSAGE_NOTIFY:
215 notifyUser();
216 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600217 }
218 }
219 };
Jason parks35933812011-01-21 15:48:20 -0600220
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700221 private AudioManager mAudioManager;
222
Ben Komalo91a2f052011-08-16 17:48:25 -0700223 /** @return whether or not this Activity was started for debugging the UI only. */
224 private boolean isDebugView() {
225 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
226 }
227
228 /** @return whether or not this Activity was started for debugging the specific UI view only. */
229 private boolean isDebugView(String viewType /* non-nullable */) {
230 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
231 }
232
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700233 /**
234 * Notify the user that we are awaiting input. Currently this sends an audio alert.
235 */
236 private void notifyUser() {
237 Log.d(TAG, "Notifying user that we are waiting for input...");
238 if (mAudioManager != null) {
239 try {
240 // Play the standard keypress sound at full volume. This should be available on
241 // every device. We cannot play a ringtone here because media services aren't
242 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
243 // on tablet devices. The idea is to alert the user that something is needed: this
244 // does not have to be pleasing.
245 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
246 } catch (Exception e) {
247 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
248 }
249 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700250 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700251 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700252 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700253 }
254
255 /**
256 * Ignore back events after the user has entered the decrypt screen and while the device is
257 * encrypting.
258 */
259 @Override
260 public void onBackPressed() {
261 if (mIgnoreBack)
262 return;
263 super.onBackPressed();
264 }
265
Jason parks8fd5bc92011-01-12 16:03:31 -0600266 @Override
267 public void onCreate(Bundle savedInstanceState) {
268 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600269
Andy Stadler95974062011-02-01 17:35:20 -0800270 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700271 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700272 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600273 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600274 PackageManager pm = getPackageManager();
275 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700276 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
277 PackageManager.DONT_KILL_APP);
278 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700279 // want to be running, so need to finish this activity. We can count
280 // on the activity manager re-launching the new home app upon finishing
281 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700282 // NOTE: This is really grungy. I think it would be better for the
283 // activity manager to explicitly launch the crypt keeper instead of
284 // home in the situation where we need to decrypt the device
285 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600286 return;
287 }
Jason parks35933812011-01-21 15:48:20 -0600288
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700289 // Disable the status bar, but do NOT disable back because the user needs a way to go
290 // from keyboard settings and back to the password screen.
Jason parks39f1e042011-01-20 23:29:28 -0600291 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Andy Stadler13d62042011-01-31 19:21:37 -0800292 sbm.disable(StatusBarManager.DISABLE_EXPAND
293 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
Jason parks39f1e042011-01-20 23:29:28 -0600294 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Andy Stadler13d62042011-01-31 19:21:37 -0800295 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -0400296 | StatusBarManager.DISABLE_HOME
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700297 | StatusBarManager.DISABLE_RECENT);
Andy Stadler14997402011-02-01 15:34:59 -0800298
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700299 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800300 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700301 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800302 if (lastInstance instanceof NonConfigurationInstanceState) {
303 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
304 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700305 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800306 }
Jason parksec5a45e2011-01-18 15:28:36 -0600307 }
Jason parks35933812011-01-21 15:48:20 -0600308
Andy Stadler95974062011-02-01 17:35:20 -0800309 /**
310 * Note, we defer the state check and screen setup to onStart() because this will be
311 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
312 * especially important if we were to lose the wakelock for any reason.
313 */
314 @Override
315 public void onStart() {
316 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700317 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700318 }
319
320 /**
321 * Initializes the UI based on the current state of encryption.
322 * This is idempotent - calling repeatedly will simply re-initialize the UI.
323 */
324 private void setupUi() {
325 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
326 setContentView(R.layout.crypt_keeper_progress);
327 showFactoryReset();
328 return;
329 }
330
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700331 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700332 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800333 setContentView(R.layout.crypt_keeper_progress);
334 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700335 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800336 setContentView(R.layout.crypt_keeper_password_entry);
337 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700338 } else if (!mValidationRequested) {
339 // We're supposed to be encrypted, but no validation has been done.
340 new ValidationTask().execute((Void[]) null);
341 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800342 }
343 }
344
Jason parksf8217302011-01-26 13:11:42 -0600345 @Override
346 public void onStop() {
347 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700348 mHandler.removeMessages(MESSAGE_COOLDOWN);
349 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
350 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800351 }
352
353 /**
354 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
355 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
356 * mWakeLock so the subsequent call to onDestroy does not release it.
357 */
358 @Override
359 public Object onRetainNonConfigurationInstance() {
360 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700361 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800362 mWakeLock = null;
363 return state;
364 }
365
366 @Override
367 public void onDestroy() {
368 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600369
370 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700371 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600372 mWakeLock.release();
373 mWakeLock = null;
374 }
375 }
376
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700377 /**
378 * Start encrypting the device.
379 */
Jason parksec5a45e2011-01-18 15:28:36 -0600380 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600381 // Accquire a partial wakelock to prevent the device from sleeping. Note
382 // we never release this wakelock as we will be restarted after the device
383 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700384 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700385 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700386 Log.d(TAG, "Acquiring wakelock.");
387 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
388 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
389 mWakeLock.acquire();
390 }
Jason parks35933812011-01-21 15:48:20 -0600391
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700392 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700393 // Ignore all back presses from now, both hard and soft keys.
394 mIgnoreBack = true;
395 // Start the first run of progress manually. This method sets up messages to occur at
396 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600397 updateProgress();
398 }
399
Andy Stadler13d62042011-01-31 19:21:37 -0800400 private void showFactoryReset() {
401 // Hide the encryption-bot to make room for the "factory reset" button
402 findViewById(R.id.encroid).setVisibility(View.GONE);
403
404 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700405 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800406 button.setVisibility(View.VISIBLE);
407 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700408 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800409 public void onClick(View v) {
410 // Factory reset the device.
411 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
412 }
413 });
414
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700415 // Alert the user of the failure.
416 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
417 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800418
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700419 final View view = findViewById(R.id.bottom_divider);
420 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700421 if (view != null) {
422 view.setVisibility(View.VISIBLE);
423 }
Andy Stadler13d62042011-01-31 19:21:37 -0800424 }
425
Jason parksf8217302011-01-26 13:11:42 -0600426 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700427 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600428
Ben Komalo0e666092011-09-01 15:42:00 -0700429 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800430 showFactoryReset();
431 return;
432 }
433
Jason parksf8217302011-01-26 13:11:42 -0600434 int progress = 0;
435 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700436 // Force a 50% progress state when debugging the view.
437 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600438 } catch (Exception e) {
439 Log.w(TAG, "Error parsing progress: " + e.toString());
440 }
441
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700442 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700443 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700444 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700445 if (tv != null) {
446 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
447 }
Jason parksf8217302011-01-26 13:11:42 -0600448 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700449 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
450 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600451 }
452
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700453 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600454 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700455 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800456
Jason parksf8217302011-01-26 13:11:42 -0600457 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700458 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600459 mPasswordEntry.setEnabled(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700460 mIgnoreBack = false;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700461 status.setVisibility(View.GONE);
Jason parksf8217302011-01-26 13:11:42 -0600462 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800463 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700464 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800465
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700466 status.setVisibility(View.VISIBLE);
Jason parksf8217302011-01-26 13:11:42 -0600467
468 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700469 mHandler.removeMessages(MESSAGE_COOLDOWN);
470 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600471 }
Jason parksec5a45e2011-01-18 15:28:36 -0600472 }
Jason parks35933812011-01-21 15:48:20 -0600473
Jason parksec5a45e2011-01-18 15:28:36 -0600474 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600475 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
476 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700477 mPasswordEntry.requestFocus();
Jason parks35933812011-01-21 15:48:20 -0600478
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700479 // Disable the Emergency call button if the device has no voice telephone capability
480 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
481 if (!tm.isVoiceCapable()) {
482 final View emergencyCall = findViewById(R.id.emergencyCallButton);
483 if (emergencyCall != null) {
484 Log.d(TAG, "Removing the emergency Call button");
485 emergencyCall.setVisibility(View.GONE);
486 }
487 }
488
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700489 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700490 final InputMethodManager imm = (InputMethodManager) getSystemService(
491 Context.INPUT_METHOD_SERVICE);
492 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
493 imeSwitcher.setVisibility(View.VISIBLE);
494 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700495 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700496 public void onClick(View v) {
497 imm.showInputMethodPicker();
498 }
499 });
Jason parks00046d62011-06-13 17:38:45 -0500500 }
David Brown8373b452011-06-20 12:38:45 -0700501
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700502 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
503 // is completely non-functional, and we want the user to notice the device and enter a
504 // password.
505 if (mWakeLock == null) {
506 Log.d(TAG, "Acquiring wakelock.");
507 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
508 if (pm != null) {
509 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
510 mWakeLock.acquire();
511 }
512 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700513 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
514 // immediately.
515 mHandler.postDelayed(new Runnable() {
516 @Override public void run() {
517 imm.showSoftInputUnchecked(0, null);
518 }
519 }, 0);
520
David Brown8373b452011-06-20 12:38:45 -0700521 updateEmergencyCallButtonState();
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700522 // Notify the user in 30 seconds that we are waiting for him to enter the password.
523 mHandler.removeMessages(MESSAGE_NOTIFY);
524 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 30 * 1000);
Jason parks8fd5bc92011-01-12 16:03:31 -0600525 }
526
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700527 /**
528 * Method adapted from com.android.inputmethod.latin.Utils
529 *
530 * @param imm The input method manager
531 * @param shouldIncludeAuxiliarySubtypes
532 * @return true if we have multiple IMEs to choose from
533 */
534 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
535 final boolean shouldIncludeAuxiliarySubtypes) {
536 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
537
538 // Number of the filtered IMEs
539 int filteredImisCount = 0;
540
541 for (InputMethodInfo imi : enabledImis) {
542 // We can return true immediately after we find two or more filtered IMEs.
543 if (filteredImisCount > 1) return true;
544 final List<InputMethodSubtype> subtypes =
545 imm.getEnabledInputMethodSubtypeList(imi, true);
546 // IMEs that have no subtypes should be counted.
547 if (subtypes.isEmpty()) {
548 ++filteredImisCount;
549 continue;
550 }
551
552 int auxCount = 0;
553 for (InputMethodSubtype subtype : subtypes) {
554 if (subtype.isAuxiliary()) {
555 ++auxCount;
556 }
557 }
558 final int nonAuxCount = subtypes.size() - auxCount;
559
560 // IMEs that have one or more non-auxiliary subtypes should be counted.
561 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
562 // subtypes should be counted as well.
563 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
564 ++filteredImisCount;
565 continue;
566 }
567 }
568
569 return filteredImisCount > 1
570 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
571 // input method subtype (The current IME should be LatinIME.)
572 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
573 }
574
Jason parks8fd5bc92011-01-12 16:03:31 -0600575 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700576 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600577 if (service != null) {
578 return IMountService.Stub.asInterface(service);
579 }
580 return null;
581 }
582
583 @Override
584 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500585 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600586 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700587 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600588
Jason parksec5a45e2011-01-18 15:28:36 -0600589 if (TextUtils.isEmpty(password)) {
590 return true;
591 }
Jason parks35933812011-01-21 15:48:20 -0600592
Jason parks8fd5bc92011-01-12 16:03:31 -0600593 // Now that we have the password clear the password field.
594 v.setText(null);
595
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700596 // Disable the password entry and back keypress while checking the password. These
597 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600598 mPasswordEntry.setEnabled(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700599 mIgnoreBack = true;
Jason parks8fd5bc92011-01-12 16:03:31 -0600600
Ben Komalo04606752011-08-18 14:50:26 -0700601 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600602 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600603
Jason parks8fd5bc92011-01-12 16:03:31 -0600604 return true;
605 }
606 return false;
607 }
David Brown8373b452011-06-20 12:38:45 -0700608
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700609 /**
610 * Code to update the state of, and handle clicks from, the "Emergency call" button.
611 *
612 * This code is mostly duplicated from the corresponding code in
613 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
614 */
David Brown8373b452011-06-20 12:38:45 -0700615 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700616 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700617 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700618 if (emergencyCall == null)
619 return;
David Brown8373b452011-06-20 12:38:45 -0700620
621 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700622 emergencyCall.setVisibility(View.VISIBLE);
623 emergencyCall.setOnClickListener(new View.OnClickListener() {
624 @Override
625
David Brown8373b452011-06-20 12:38:45 -0700626 public void onClick(View v) {
627 takeEmergencyCallAction();
628 }
629 });
630 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700631 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700632 return;
633 }
634
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700635 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700636 int textId;
637 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700638 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700639 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700640 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
641 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700642 } else {
643 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700644 final int emergencyIcon = R.drawable.ic_emergency;
645 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700646 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700647 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700648 }
649
650 private boolean isEmergencyCallCapable() {
651 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
652 }
653
654 private void takeEmergencyCallAction() {
655 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
656 resumeCall();
657 } else {
658 launchEmergencyDialer();
659 }
660 }
661
662 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700663 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700664 if (phone != null) {
665 try {
666 phone.showCallScreen();
667 } catch (RemoteException e) {
668 Log.e(TAG, "Error calling ITelephony service: " + e);
669 }
670 }
671 }
672
673 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700674 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700675 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
676 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
677 startActivity(intent);
678 }
679}