blob: 10c067d8da63576edad3185ed62511643f315927 [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;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070036import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070037import android.telephony.TelephonyManager;
Jason parksec5a45e2011-01-18 15:28:36 -060038import android.text.TextUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060039import android.util.Log;
40import android.view.KeyEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080041import android.view.View;
42import android.view.View.OnClickListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060043import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070044import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060045import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070046import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080047import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060048import android.widget.EditText;
49import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060050import android.widget.TextView;
51
Ben Komalo91a2f052011-08-16 17:48:25 -070052import com.android.internal.telephony.ITelephony;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070053import com.android.internal.telephony.Phone;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070054
55import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070056
57/**
58 * Settings screens to show the UI flows for encrypting/decrypting the device.
59 *
60 * This may be started via adb for debugging the UI layout, without having to go through
61 * encryption flows everytime. It should be noted that starting the activity in this manner
62 * is only useful for verifying UI-correctness - the behavior will not be identical.
63 * <pre>
64 * $ adb shell pm enable com.android.settings/.CryptKeeper
65 * $ adb shell am start \
66 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
67 * -n com.android.settings/.CryptKeeper
68 * </pre>
69 */
Jason parks8fd5bc92011-01-12 16:03:31 -060070public class CryptKeeper extends Activity implements TextView.OnEditorActionListener {
Jason parksec5a45e2011-01-18 15:28:36 -060071 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060072
Jason parks8fd5bc92011-01-12 16:03:31 -060073 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070074 /** Message sent to us to indicate encryption update progress. */
75 private static final int MESSAGE_UPDATE_PROGRESS = 1;
76 /** Message sent to us to cool-down (waste user's time between password attempts) */
77 private static final int MESSAGE_COOLDOWN = 2;
78 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
79 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060080
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070081 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060082 private static final int MAX_FAILED_ATTEMPTS = 30;
83 private static final int COOL_DOWN_ATTEMPTS = 10;
84 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
85
David Brown8373b452011-06-20 12:38:45 -070086 // Intent action for launching the Emergency Dialer activity.
87 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
88
Ben Komalo91a2f052011-08-16 17:48:25 -070089 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
90 private static final String EXTRA_FORCE_VIEW =
91 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
92 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -070093 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070094 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -070095
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -070096 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -070097 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -070098 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -070099 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
100 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700101 /** A flag to indicate when the back event should be ignored */
102 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800103 private int mCooldown;
104 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600105 private EditText mPasswordEntry;
Andy Stadler14997402011-02-01 15:34:59 -0800106
107 /**
108 * Used to propagate state through configuration changes (e.g. screen rotation)
109 */
110 private static class NonConfigurationInstanceState {
111 final PowerManager.WakeLock wakelock;
112
113 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
114 wakelock = _wakelock;
115 }
116 }
117
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700118 /**
119 * Activity used to fade the screen to black after the password is entered.
120 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700121 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800122 @Override
123 public void onCreate(Bundle savedInstanceState) {
124 super.onCreate(savedInstanceState);
125 setContentView(R.layout.crypt_keeper_blank);
126 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700127 /** Ignore all back events. */
128 @Override
129 public void onBackPressed() {
130 return;
131 }
Jason parksf1dbf552011-01-24 16:19:28 -0600132 }
Jason parksec5a45e2011-01-18 15:28:36 -0600133
Jason parks06c5ff42011-03-01 10:17:40 -0600134 private class DecryptTask extends AsyncTask<String, Void, Integer> {
135 @Override
136 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700137 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600138 try {
139 return service.decryptStorage(params[0]);
140 } catch (Exception e) {
141 Log.e(TAG, "Error while decrypting...", e);
142 return -1;
143 }
144 }
145
146 @Override
147 protected void onPostExecute(Integer failedAttempts) {
148 if (failedAttempts == 0) {
149 // The password was entered successfully. Start the Blank activity
150 // so this activity animates to black before the devices starts. Note
151 // It has 1 second to complete the animation or it will be frozen
152 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700153 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600154 finish();
155 startActivity(intent);
156 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
157 // Factory reset the device.
158 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
159 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
160 mCooldown = COOL_DOWN_INTERVAL;
161 cooldown();
162 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700163 final TextView status = (TextView) findViewById(R.id.status);
164 status.setText(R.string.try_again);
165 status.setVisibility(View.VISIBLE);
Jason parks06c5ff42011-03-01 10:17:40 -0600166
167 // Reenable the password entry
168 mPasswordEntry.setEnabled(true);
169 }
170 }
171 }
Jason parks75c085e2011-02-10 14:03:47 -0600172
Ben Komalo0e666092011-09-01 15:42:00 -0700173 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
174 @Override
175 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700176 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700177 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700178 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700179 int state = service.getEncryptionState();
180 if (state == IMountService.ENCRYPTION_STATE_NONE) {
181 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
182 return true; // Unexpected, but fine, I guess...
183 }
184 return state == IMountService.ENCRYPTION_STATE_OK;
185 } catch (RemoteException e) {
186 Log.w(TAG, "Unable to get encryption state properly");
187 return true;
188 }
189 }
190
191 @Override
192 protected void onPostExecute(Boolean result) {
193 mValidationComplete = true;
194 if (Boolean.FALSE.equals(result)) {
195 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
196 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700197 } else {
198 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700199 }
200 setupUi();
201 }
202 }
203
Ben Komalo91a2f052011-08-16 17:48:25 -0700204 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600205 @Override
206 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600207 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700208 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600209 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600210 break;
Jason parks35933812011-01-21 15:48:20 -0600211
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700212 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600213 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600214 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700215
216 case MESSAGE_NOTIFY:
217 notifyUser();
218 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600219 }
220 }
221 };
Jason parks35933812011-01-21 15:48:20 -0600222
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700223 private AudioManager mAudioManager;
224
Ben Komalo91a2f052011-08-16 17:48:25 -0700225 /** @return whether or not this Activity was started for debugging the UI only. */
226 private boolean isDebugView() {
227 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
228 }
229
230 /** @return whether or not this Activity was started for debugging the specific UI view only. */
231 private boolean isDebugView(String viewType /* non-nullable */) {
232 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
233 }
234
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700235 /**
236 * Notify the user that we are awaiting input. Currently this sends an audio alert.
237 */
238 private void notifyUser() {
239 Log.d(TAG, "Notifying user that we are waiting for input...");
240 if (mAudioManager != null) {
241 try {
242 // Play the standard keypress sound at full volume. This should be available on
243 // every device. We cannot play a ringtone here because media services aren't
244 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
245 // on tablet devices. The idea is to alert the user that something is needed: this
246 // does not have to be pleasing.
247 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
248 } catch (Exception e) {
249 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
250 }
251 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700252 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700253 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700254 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700255 }
256
257 /**
258 * Ignore back events after the user has entered the decrypt screen and while the device is
259 * encrypting.
260 */
261 @Override
262 public void onBackPressed() {
263 if (mIgnoreBack)
264 return;
265 super.onBackPressed();
266 }
267
Jason parks8fd5bc92011-01-12 16:03:31 -0600268 @Override
269 public void onCreate(Bundle savedInstanceState) {
270 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600271
Andy Stadler95974062011-02-01 17:35:20 -0800272 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700273 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700274 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600275 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600276 PackageManager pm = getPackageManager();
277 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700278 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
279 PackageManager.DONT_KILL_APP);
280 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700281 // want to be running, so need to finish this activity. We can count
282 // on the activity manager re-launching the new home app upon finishing
283 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700284 // NOTE: This is really grungy. I think it would be better for the
285 // activity manager to explicitly launch the crypt keeper instead of
286 // home in the situation where we need to decrypt the device
287 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600288 return;
289 }
Jason parks35933812011-01-21 15:48:20 -0600290
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700291 // Disable the status bar, but do NOT disable back because the user needs a way to go
292 // from keyboard settings and back to the password screen.
Jason parks39f1e042011-01-20 23:29:28 -0600293 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Andy Stadler13d62042011-01-31 19:21:37 -0800294 sbm.disable(StatusBarManager.DISABLE_EXPAND
295 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
Jason parks39f1e042011-01-20 23:29:28 -0600296 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Andy Stadler13d62042011-01-31 19:21:37 -0800297 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -0400298 | StatusBarManager.DISABLE_HOME
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700299 | StatusBarManager.DISABLE_RECENT);
Andy Stadler14997402011-02-01 15:34:59 -0800300
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700301 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700302 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800303 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700304 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800305 if (lastInstance instanceof NonConfigurationInstanceState) {
306 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
307 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700308 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800309 }
Jason parksec5a45e2011-01-18 15:28:36 -0600310 }
Jason parks35933812011-01-21 15:48:20 -0600311
Andy Stadler95974062011-02-01 17:35:20 -0800312 /**
313 * Note, we defer the state check and screen setup to onStart() because this will be
314 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
315 * especially important if we were to lose the wakelock for any reason.
316 */
317 @Override
318 public void onStart() {
319 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700320 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700321 }
322
323 /**
324 * Initializes the UI based on the current state of encryption.
325 * This is idempotent - calling repeatedly will simply re-initialize the UI.
326 */
327 private void setupUi() {
328 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
329 setContentView(R.layout.crypt_keeper_progress);
330 showFactoryReset();
331 return;
332 }
333
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700334 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700335 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800336 setContentView(R.layout.crypt_keeper_progress);
337 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700338 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800339 setContentView(R.layout.crypt_keeper_password_entry);
340 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700341 } else if (!mValidationRequested) {
342 // We're supposed to be encrypted, but no validation has been done.
343 new ValidationTask().execute((Void[]) null);
344 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800345 }
346 }
347
Jason parksf8217302011-01-26 13:11:42 -0600348 @Override
349 public void onStop() {
350 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700351 mHandler.removeMessages(MESSAGE_COOLDOWN);
352 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
353 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800354 }
355
356 /**
357 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
358 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
359 * mWakeLock so the subsequent call to onDestroy does not release it.
360 */
361 @Override
362 public Object onRetainNonConfigurationInstance() {
363 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700364 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800365 mWakeLock = null;
366 return state;
367 }
368
369 @Override
370 public void onDestroy() {
371 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600372
373 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700374 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600375 mWakeLock.release();
376 mWakeLock = null;
377 }
378 }
379
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700380 /**
381 * Start encrypting the device.
382 */
Jason parksec5a45e2011-01-18 15:28:36 -0600383 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600384 // Accquire a partial wakelock to prevent the device from sleeping. Note
385 // we never release this wakelock as we will be restarted after the device
386 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700387 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700388 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700389 Log.d(TAG, "Acquiring wakelock.");
390 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
391 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
392 mWakeLock.acquire();
393 }
Jason parks35933812011-01-21 15:48:20 -0600394
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700395 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700396 // Ignore all back presses from now, both hard and soft keys.
397 mIgnoreBack = true;
398 // Start the first run of progress manually. This method sets up messages to occur at
399 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600400 updateProgress();
401 }
402
Andy Stadler13d62042011-01-31 19:21:37 -0800403 private void showFactoryReset() {
404 // Hide the encryption-bot to make room for the "factory reset" button
405 findViewById(R.id.encroid).setVisibility(View.GONE);
406
407 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700408 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800409 button.setVisibility(View.VISIBLE);
410 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700411 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800412 public void onClick(View v) {
413 // Factory reset the device.
414 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
415 }
416 });
417
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700418 // Alert the user of the failure.
419 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
420 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800421
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700422 final View view = findViewById(R.id.bottom_divider);
423 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700424 if (view != null) {
425 view.setVisibility(View.VISIBLE);
426 }
Andy Stadler13d62042011-01-31 19:21:37 -0800427 }
428
Jason parksf8217302011-01-26 13:11:42 -0600429 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700430 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600431
Ben Komalo0e666092011-09-01 15:42:00 -0700432 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800433 showFactoryReset();
434 return;
435 }
436
Jason parksf8217302011-01-26 13:11:42 -0600437 int progress = 0;
438 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700439 // Force a 50% progress state when debugging the view.
440 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600441 } catch (Exception e) {
442 Log.w(TAG, "Error parsing progress: " + e.toString());
443 }
444
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700445 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700446 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700447 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700448 if (tv != null) {
449 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
450 }
Jason parksf8217302011-01-26 13:11:42 -0600451 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700452 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
453 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600454 }
455
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700456 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600457 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700458 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800459
Jason parksf8217302011-01-26 13:11:42 -0600460 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700461 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600462 mPasswordEntry.setEnabled(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700463 mIgnoreBack = false;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700464 status.setVisibility(View.GONE);
Jason parksf8217302011-01-26 13:11:42 -0600465 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800466 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700467 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800468
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700469 status.setVisibility(View.VISIBLE);
Jason parksf8217302011-01-26 13:11:42 -0600470
471 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700472 mHandler.removeMessages(MESSAGE_COOLDOWN);
473 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600474 }
Jason parksec5a45e2011-01-18 15:28:36 -0600475 }
Jason parks35933812011-01-21 15:48:20 -0600476
Jason parksec5a45e2011-01-18 15:28:36 -0600477 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600478 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
479 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700480 mPasswordEntry.requestFocus();
Jason parks35933812011-01-21 15:48:20 -0600481
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700482 // Disable the Emergency call button if the device has no voice telephone capability
483 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
484 if (!tm.isVoiceCapable()) {
485 final View emergencyCall = findViewById(R.id.emergencyCallButton);
486 if (emergencyCall != null) {
487 Log.d(TAG, "Removing the emergency Call button");
488 emergencyCall.setVisibility(View.GONE);
489 }
490 }
491
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700492 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700493 final InputMethodManager imm = (InputMethodManager) getSystemService(
494 Context.INPUT_METHOD_SERVICE);
495 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
496 imeSwitcher.setVisibility(View.VISIBLE);
497 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700498 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700499 public void onClick(View v) {
500 imm.showInputMethodPicker();
501 }
502 });
Jason parks00046d62011-06-13 17:38:45 -0500503 }
David Brown8373b452011-06-20 12:38:45 -0700504
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700505 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
506 // is completely non-functional, and we want the user to notice the device and enter a
507 // password.
508 if (mWakeLock == null) {
509 Log.d(TAG, "Acquiring wakelock.");
510 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
511 if (pm != null) {
512 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
513 mWakeLock.acquire();
514 }
515 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700516 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
517 // immediately.
518 mHandler.postDelayed(new Runnable() {
519 @Override public void run() {
520 imm.showSoftInputUnchecked(0, null);
521 }
522 }, 0);
523
David Brown8373b452011-06-20 12:38:45 -0700524 updateEmergencyCallButtonState();
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700525 // Notify the user in 30 seconds that we are waiting for him to enter the password.
526 mHandler.removeMessages(MESSAGE_NOTIFY);
527 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 30 * 1000);
Jason parks8fd5bc92011-01-12 16:03:31 -0600528 }
529
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700530 /**
531 * Method adapted from com.android.inputmethod.latin.Utils
532 *
533 * @param imm The input method manager
534 * @param shouldIncludeAuxiliarySubtypes
535 * @return true if we have multiple IMEs to choose from
536 */
537 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
538 final boolean shouldIncludeAuxiliarySubtypes) {
539 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
540
541 // Number of the filtered IMEs
542 int filteredImisCount = 0;
543
544 for (InputMethodInfo imi : enabledImis) {
545 // We can return true immediately after we find two or more filtered IMEs.
546 if (filteredImisCount > 1) return true;
547 final List<InputMethodSubtype> subtypes =
548 imm.getEnabledInputMethodSubtypeList(imi, true);
549 // IMEs that have no subtypes should be counted.
550 if (subtypes.isEmpty()) {
551 ++filteredImisCount;
552 continue;
553 }
554
555 int auxCount = 0;
556 for (InputMethodSubtype subtype : subtypes) {
557 if (subtype.isAuxiliary()) {
558 ++auxCount;
559 }
560 }
561 final int nonAuxCount = subtypes.size() - auxCount;
562
563 // IMEs that have one or more non-auxiliary subtypes should be counted.
564 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
565 // subtypes should be counted as well.
566 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
567 ++filteredImisCount;
568 continue;
569 }
570 }
571
572 return filteredImisCount > 1
573 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
574 // input method subtype (The current IME should be LatinIME.)
575 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
576 }
577
Jason parks8fd5bc92011-01-12 16:03:31 -0600578 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700579 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600580 if (service != null) {
581 return IMountService.Stub.asInterface(service);
582 }
583 return null;
584 }
585
586 @Override
587 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500588 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600589 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700590 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600591
Jason parksec5a45e2011-01-18 15:28:36 -0600592 if (TextUtils.isEmpty(password)) {
593 return true;
594 }
Jason parks35933812011-01-21 15:48:20 -0600595
Jason parks8fd5bc92011-01-12 16:03:31 -0600596 // Now that we have the password clear the password field.
597 v.setText(null);
598
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700599 // Disable the password entry and back keypress while checking the password. These
600 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600601 mPasswordEntry.setEnabled(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700602 mIgnoreBack = true;
Jason parks8fd5bc92011-01-12 16:03:31 -0600603
Ben Komalo04606752011-08-18 14:50:26 -0700604 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600605 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600606
Jason parks8fd5bc92011-01-12 16:03:31 -0600607 return true;
608 }
609 return false;
610 }
David Brown8373b452011-06-20 12:38:45 -0700611
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700612 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700613 * Set airplane mode on the device if it isn't an LTE device.
614 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
615 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
616 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
617 * both these problems, we turn the radio off. However, on certain networks turning on and
618 * off the radio takes a long time. In such cases, we are better off leaving the radio
619 * running so the latency of an E911 call is short.
620 * The behavior after this is:
621 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
622 * airplane mode and restart the radio.
623 * 2. Full boot: we read the persistent settings from the previous boot and restore the
624 * radio to whatever it was before it restarted. This also happens when rebooting a
625 * phone that has no encryption.
626 */
627 private final void setAirplaneModeIfNecessary() {
628 final boolean isLteDevice =
629 TelephonyManager.getDefault().getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
630 if (!isLteDevice) {
631 Log.d(TAG, "Going into airplane mode.");
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -0700632 Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700633 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
634 intent.putExtra("state", true);
635 sendBroadcast(intent);
636 }
637 }
638
639 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700640 * Code to update the state of, and handle clicks from, the "Emergency call" button.
641 *
642 * This code is mostly duplicated from the corresponding code in
643 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
644 */
David Brown8373b452011-06-20 12:38:45 -0700645 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700646 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700647 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700648 if (emergencyCall == null)
649 return;
David Brown8373b452011-06-20 12:38:45 -0700650
651 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700652 emergencyCall.setVisibility(View.VISIBLE);
653 emergencyCall.setOnClickListener(new View.OnClickListener() {
654 @Override
655
David Brown8373b452011-06-20 12:38:45 -0700656 public void onClick(View v) {
657 takeEmergencyCallAction();
658 }
659 });
660 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700661 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700662 return;
663 }
664
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700665 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700666 int textId;
667 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700668 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700669 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700670 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
671 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700672 } else {
673 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700674 final int emergencyIcon = R.drawable.ic_emergency;
675 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700676 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700677 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700678 }
679
680 private boolean isEmergencyCallCapable() {
681 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
682 }
683
684 private void takeEmergencyCallAction() {
685 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
686 resumeCall();
687 } else {
688 launchEmergencyDialer();
689 }
690 }
691
692 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700693 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700694 if (phone != null) {
695 try {
696 phone.showCallScreen();
697 } catch (RemoteException e) {
698 Log.e(TAG, "Error calling ITelephony service: " + e);
699 }
700 }
701 }
702
703 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700704 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700705 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
706 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
707 startActivity(intent);
708 }
709}