blob: 6795e28470d47436bb7d9f6eb3b7b3423298617a [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;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070052import com.android.internal.telephony.Phone;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070053
54import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070055
56/**
57 * Settings screens to show the UI flows for encrypting/decrypting the device.
58 *
59 * This may be started via adb for debugging the UI layout, without having to go through
60 * encryption flows everytime. It should be noted that starting the activity in this manner
61 * is only useful for verifying UI-correctness - the behavior will not be identical.
62 * <pre>
63 * $ adb shell pm enable com.android.settings/.CryptKeeper
64 * $ adb shell am start \
65 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
66 * -n com.android.settings/.CryptKeeper
67 * </pre>
68 */
Jason parks8fd5bc92011-01-12 16:03:31 -060069public class CryptKeeper extends Activity implements TextView.OnEditorActionListener {
Jason parksec5a45e2011-01-18 15:28:36 -060070 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060071
Jason parks8fd5bc92011-01-12 16:03:31 -060072 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070073 /** Message sent to us to indicate encryption update progress. */
74 private static final int MESSAGE_UPDATE_PROGRESS = 1;
75 /** Message sent to us to cool-down (waste user's time between password attempts) */
76 private static final int MESSAGE_COOLDOWN = 2;
77 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
78 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060079
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070080 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060081 private static final int MAX_FAILED_ATTEMPTS = 30;
82 private static final int COOL_DOWN_ATTEMPTS = 10;
83 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
84
David Brown8373b452011-06-20 12:38:45 -070085 // Intent action for launching the Emergency Dialer activity.
86 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
87
Ben Komalo91a2f052011-08-16 17:48:25 -070088 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
89 private static final String EXTRA_FORCE_VIEW =
90 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
91 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -070092 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070093 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -070094
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -070095 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -070096 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -070097 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -070098 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
99 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700100 /** A flag to indicate when the back event should be ignored */
101 private boolean mIgnoreBack = false;
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 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700251 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700252 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700253 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700254 }
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 Aggarwalea1186d2012-05-03 15:35:03 -0700300 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700301 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800302 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700303 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800304 if (lastInstance instanceof NonConfigurationInstanceState) {
305 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
306 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700307 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800308 }
Jason parksec5a45e2011-01-18 15:28:36 -0600309 }
Jason parks35933812011-01-21 15:48:20 -0600310
Andy Stadler95974062011-02-01 17:35:20 -0800311 /**
312 * Note, we defer the state check and screen setup to onStart() because this will be
313 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
314 * especially important if we were to lose the wakelock for any reason.
315 */
316 @Override
317 public void onStart() {
318 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700319 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700320 }
321
322 /**
323 * Initializes the UI based on the current state of encryption.
324 * This is idempotent - calling repeatedly will simply re-initialize the UI.
325 */
326 private void setupUi() {
327 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
328 setContentView(R.layout.crypt_keeper_progress);
329 showFactoryReset();
330 return;
331 }
332
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700333 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700334 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800335 setContentView(R.layout.crypt_keeper_progress);
336 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700337 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800338 setContentView(R.layout.crypt_keeper_password_entry);
339 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700340 } else if (!mValidationRequested) {
341 // We're supposed to be encrypted, but no validation has been done.
342 new ValidationTask().execute((Void[]) null);
343 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800344 }
345 }
346
Jason parksf8217302011-01-26 13:11:42 -0600347 @Override
348 public void onStop() {
349 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700350 mHandler.removeMessages(MESSAGE_COOLDOWN);
351 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
352 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800353 }
354
355 /**
356 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
357 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
358 * mWakeLock so the subsequent call to onDestroy does not release it.
359 */
360 @Override
361 public Object onRetainNonConfigurationInstance() {
362 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700363 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800364 mWakeLock = null;
365 return state;
366 }
367
368 @Override
369 public void onDestroy() {
370 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600371
372 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700373 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600374 mWakeLock.release();
375 mWakeLock = null;
376 }
377 }
378
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700379 /**
380 * Start encrypting the device.
381 */
Jason parksec5a45e2011-01-18 15:28:36 -0600382 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600383 // Accquire a partial wakelock to prevent the device from sleeping. Note
384 // we never release this wakelock as we will be restarted after the device
385 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700386 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700387 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700388 Log.d(TAG, "Acquiring wakelock.");
389 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
390 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
391 mWakeLock.acquire();
392 }
Jason parks35933812011-01-21 15:48:20 -0600393
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700394 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700395 // Ignore all back presses from now, both hard and soft keys.
396 mIgnoreBack = true;
397 // Start the first run of progress manually. This method sets up messages to occur at
398 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600399 updateProgress();
400 }
401
Andy Stadler13d62042011-01-31 19:21:37 -0800402 private void showFactoryReset() {
403 // Hide the encryption-bot to make room for the "factory reset" button
404 findViewById(R.id.encroid).setVisibility(View.GONE);
405
406 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700407 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800408 button.setVisibility(View.VISIBLE);
409 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700410 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800411 public void onClick(View v) {
412 // Factory reset the device.
413 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
414 }
415 });
416
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700417 // Alert the user of the failure.
418 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
419 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800420
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700421 final View view = findViewById(R.id.bottom_divider);
422 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700423 if (view != null) {
424 view.setVisibility(View.VISIBLE);
425 }
Andy Stadler13d62042011-01-31 19:21:37 -0800426 }
427
Jason parksf8217302011-01-26 13:11:42 -0600428 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700429 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600430
Ben Komalo0e666092011-09-01 15:42:00 -0700431 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800432 showFactoryReset();
433 return;
434 }
435
Jason parksf8217302011-01-26 13:11:42 -0600436 int progress = 0;
437 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700438 // Force a 50% progress state when debugging the view.
439 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600440 } catch (Exception e) {
441 Log.w(TAG, "Error parsing progress: " + e.toString());
442 }
443
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700444 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700445 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700446 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700447 if (tv != null) {
448 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
449 }
Jason parksf8217302011-01-26 13:11:42 -0600450 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700451 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
452 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600453 }
454
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700455 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600456 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700457 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800458
Jason parksf8217302011-01-26 13:11:42 -0600459 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700460 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600461 mPasswordEntry.setEnabled(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700462 mIgnoreBack = false;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700463 status.setVisibility(View.GONE);
Jason parksf8217302011-01-26 13:11:42 -0600464 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800465 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700466 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800467
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700468 status.setVisibility(View.VISIBLE);
Jason parksf8217302011-01-26 13:11:42 -0600469
470 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700471 mHandler.removeMessages(MESSAGE_COOLDOWN);
472 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600473 }
Jason parksec5a45e2011-01-18 15:28:36 -0600474 }
Jason parks35933812011-01-21 15:48:20 -0600475
Jason parksec5a45e2011-01-18 15:28:36 -0600476 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600477 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
478 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700479 mPasswordEntry.requestFocus();
Jason parks35933812011-01-21 15:48:20 -0600480
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700481 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700482 final InputMethodManager imm = (InputMethodManager) getSystemService(
483 Context.INPUT_METHOD_SERVICE);
484 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
485 imeSwitcher.setVisibility(View.VISIBLE);
486 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700487 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700488 public void onClick(View v) {
489 imm.showInputMethodPicker();
490 }
491 });
Jason parks00046d62011-06-13 17:38:45 -0500492 }
David Brown8373b452011-06-20 12:38:45 -0700493
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700494 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
495 // is completely non-functional, and we want the user to notice the device and enter a
496 // password.
497 if (mWakeLock == null) {
498 Log.d(TAG, "Acquiring wakelock.");
499 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
500 if (pm != null) {
501 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
502 mWakeLock.acquire();
503 }
504 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700505 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
506 // immediately.
507 mHandler.postDelayed(new Runnable() {
508 @Override public void run() {
509 imm.showSoftInputUnchecked(0, null);
510 }
511 }, 0);
512
David Brown8373b452011-06-20 12:38:45 -0700513 updateEmergencyCallButtonState();
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700514 // Notify the user in 30 seconds that we are waiting for him to enter the password.
515 mHandler.removeMessages(MESSAGE_NOTIFY);
516 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 30 * 1000);
Jason parks8fd5bc92011-01-12 16:03:31 -0600517 }
518
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700519 /**
520 * Method adapted from com.android.inputmethod.latin.Utils
521 *
522 * @param imm The input method manager
523 * @param shouldIncludeAuxiliarySubtypes
524 * @return true if we have multiple IMEs to choose from
525 */
526 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
527 final boolean shouldIncludeAuxiliarySubtypes) {
528 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
529
530 // Number of the filtered IMEs
531 int filteredImisCount = 0;
532
533 for (InputMethodInfo imi : enabledImis) {
534 // We can return true immediately after we find two or more filtered IMEs.
535 if (filteredImisCount > 1) return true;
536 final List<InputMethodSubtype> subtypes =
537 imm.getEnabledInputMethodSubtypeList(imi, true);
538 // IMEs that have no subtypes should be counted.
539 if (subtypes.isEmpty()) {
540 ++filteredImisCount;
541 continue;
542 }
543
544 int auxCount = 0;
545 for (InputMethodSubtype subtype : subtypes) {
546 if (subtype.isAuxiliary()) {
547 ++auxCount;
548 }
549 }
550 final int nonAuxCount = subtypes.size() - auxCount;
551
552 // IMEs that have one or more non-auxiliary subtypes should be counted.
553 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
554 // subtypes should be counted as well.
555 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
556 ++filteredImisCount;
557 continue;
558 }
559 }
560
561 return filteredImisCount > 1
562 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
563 // input method subtype (The current IME should be LatinIME.)
564 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
565 }
566
Jason parks8fd5bc92011-01-12 16:03:31 -0600567 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700568 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600569 if (service != null) {
570 return IMountService.Stub.asInterface(service);
571 }
572 return null;
573 }
574
575 @Override
576 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500577 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600578 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700579 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600580
Jason parksec5a45e2011-01-18 15:28:36 -0600581 if (TextUtils.isEmpty(password)) {
582 return true;
583 }
Jason parks35933812011-01-21 15:48:20 -0600584
Jason parks8fd5bc92011-01-12 16:03:31 -0600585 // Now that we have the password clear the password field.
586 v.setText(null);
587
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700588 // Disable the password entry and back keypress while checking the password. These
589 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600590 mPasswordEntry.setEnabled(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700591 mIgnoreBack = true;
Jason parks8fd5bc92011-01-12 16:03:31 -0600592
Ben Komalo04606752011-08-18 14:50:26 -0700593 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600594 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600595
Jason parks8fd5bc92011-01-12 16:03:31 -0600596 return true;
597 }
598 return false;
599 }
David Brown8373b452011-06-20 12:38:45 -0700600
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700601 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700602 * Set airplane mode on the device if it isn't an LTE device.
603 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
604 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
605 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
606 * both these problems, we turn the radio off. However, on certain networks turning on and
607 * off the radio takes a long time. In such cases, we are better off leaving the radio
608 * running so the latency of an E911 call is short.
609 * The behavior after this is:
610 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
611 * airplane mode and restart the radio.
612 * 2. Full boot: we read the persistent settings from the previous boot and restore the
613 * radio to whatever it was before it restarted. This also happens when rebooting a
614 * phone that has no encryption.
615 */
616 private final void setAirplaneModeIfNecessary() {
617 final boolean isLteDevice =
618 TelephonyManager.getDefault().getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
619 if (!isLteDevice) {
620 Log.d(TAG, "Going into airplane mode.");
621 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
622 intent.putExtra("state", true);
623 sendBroadcast(intent);
624 }
625 }
626
627 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700628 * Code to update the state of, and handle clicks from, the "Emergency call" button.
629 *
630 * This code is mostly duplicated from the corresponding code in
631 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
632 */
David Brown8373b452011-06-20 12:38:45 -0700633 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700634 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700635 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700636 if (emergencyCall == null)
637 return;
David Brown8373b452011-06-20 12:38:45 -0700638
639 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700640 emergencyCall.setVisibility(View.VISIBLE);
641 emergencyCall.setOnClickListener(new View.OnClickListener() {
642 @Override
643
David Brown8373b452011-06-20 12:38:45 -0700644 public void onClick(View v) {
645 takeEmergencyCallAction();
646 }
647 });
648 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700649 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700650 return;
651 }
652
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700653 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700654 int textId;
655 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700656 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700657 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700658 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
659 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700660 } else {
661 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700662 final int emergencyIcon = R.drawable.ic_emergency;
663 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700664 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700665 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700666 }
667
668 private boolean isEmergencyCallCapable() {
669 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
670 }
671
672 private void takeEmergencyCallAction() {
673 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
674 resumeCall();
675 } else {
676 launchEmergencyDialer();
677 }
678 }
679
680 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700681 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700682 if (phone != null) {
683 try {
684 phone.showCallScreen();
685 } catch (RemoteException e) {
686 Log.e(TAG, "Error calling ITelephony service: " + e);
687 }
688 }
689 }
690
691 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700692 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700693 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
694 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
695 startActivity(intent);
696 }
697}