blob: 94c793ddc12ea8577778bd9e5c2ad537d11eb59d [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;
Christopher Tate6a5929b2012-09-10 15:39:05 -070035import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060036import android.os.storage.IMountService;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070037import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070038import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070039import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060040import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070041import android.text.TextWatcher;
Jason parks8fd5bc92011-01-12 16:03:31 -060042import android.util.Log;
43import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080045import android.view.View;
46import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070047import android.view.View.OnKeyListener;
48import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060049import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070050import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060051import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070052import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080053import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060054import android.widget.EditText;
55import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060056import android.widget.TextView;
57
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080058import com.android.internal.statusbar.StatusBarIcon;
Ben Komalo91a2f052011-08-16 17:48:25 -070059import com.android.internal.telephony.ITelephony;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070060import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070061import com.android.internal.telephony.PhoneConstants;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070062
63import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070064
65/**
66 * Settings screens to show the UI flows for encrypting/decrypting the device.
67 *
68 * This may be started via adb for debugging the UI layout, without having to go through
69 * encryption flows everytime. It should be noted that starting the activity in this manner
70 * is only useful for verifying UI-correctness - the behavior will not be identical.
71 * <pre>
72 * $ adb shell pm enable com.android.settings/.CryptKeeper
73 * $ adb shell am start \
74 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
75 * -n com.android.settings/.CryptKeeper
76 * </pre>
77 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070078public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
79 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060080 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060081
Jason parks8fd5bc92011-01-12 16:03:31 -060082 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070083 /** Message sent to us to indicate encryption update progress. */
84 private static final int MESSAGE_UPDATE_PROGRESS = 1;
85 /** Message sent to us to cool-down (waste user's time between password attempts) */
86 private static final int MESSAGE_COOLDOWN = 2;
87 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
88 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060089
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070090 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060091 private static final int MAX_FAILED_ATTEMPTS = 30;
92 private static final int COOL_DOWN_ATTEMPTS = 10;
93 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
94
David Brown8373b452011-06-20 12:38:45 -070095 // Intent action for launching the Emergency Dialer activity.
96 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
97
Ben Komalo91a2f052011-08-16 17:48:25 -070098 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
99 private static final String EXTRA_FORCE_VIEW =
100 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
101 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700102 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700103 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700104
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700105 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700106 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700107 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700108 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
109 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700110 /** A flag to indicate when the back event should be ignored */
111 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800112 private int mCooldown;
113 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600114 private EditText mPasswordEntry;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700115 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
116 private int mNotificationCountdown = 0;
Andy Stadler14997402011-02-01 15:34:59 -0800117
118 /**
119 * Used to propagate state through configuration changes (e.g. screen rotation)
120 */
121 private static class NonConfigurationInstanceState {
122 final PowerManager.WakeLock wakelock;
123
124 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
125 wakelock = _wakelock;
126 }
127 }
128
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700129 /**
130 * Activity used to fade the screen to black after the password is entered.
131 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700132 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800133 @Override
134 public void onCreate(Bundle savedInstanceState) {
135 super.onCreate(savedInstanceState);
136 setContentView(R.layout.crypt_keeper_blank);
137 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700138 /** Ignore all back events. */
139 @Override
140 public void onBackPressed() {
141 return;
142 }
Jason parksf1dbf552011-01-24 16:19:28 -0600143 }
Jason parksec5a45e2011-01-18 15:28:36 -0600144
Jason parks06c5ff42011-03-01 10:17:40 -0600145 private class DecryptTask extends AsyncTask<String, Void, Integer> {
146 @Override
147 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700148 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600149 try {
150 return service.decryptStorage(params[0]);
151 } catch (Exception e) {
152 Log.e(TAG, "Error while decrypting...", e);
153 return -1;
154 }
155 }
156
157 @Override
158 protected void onPostExecute(Integer failedAttempts) {
159 if (failedAttempts == 0) {
160 // The password was entered successfully. Start the Blank activity
161 // so this activity animates to black before the devices starts. Note
162 // It has 1 second to complete the animation or it will be frozen
163 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700164 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600165 finish();
166 startActivity(intent);
167 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
168 // Factory reset the device.
169 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
170 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
171 mCooldown = COOL_DOWN_INTERVAL;
172 cooldown();
173 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700174 final TextView status = (TextView) findViewById(R.id.status);
175 status.setText(R.string.try_again);
Jason parks06c5ff42011-03-01 10:17:40 -0600176 // Reenable the password entry
177 mPasswordEntry.setEnabled(true);
178 }
179 }
180 }
Jason parks75c085e2011-02-10 14:03:47 -0600181
Ben Komalo0e666092011-09-01 15:42:00 -0700182 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
183 @Override
184 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700185 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700186 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700187 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700188 int state = service.getEncryptionState();
189 if (state == IMountService.ENCRYPTION_STATE_NONE) {
190 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
191 return true; // Unexpected, but fine, I guess...
192 }
193 return state == IMountService.ENCRYPTION_STATE_OK;
194 } catch (RemoteException e) {
195 Log.w(TAG, "Unable to get encryption state properly");
196 return true;
197 }
198 }
199
200 @Override
201 protected void onPostExecute(Boolean result) {
202 mValidationComplete = true;
203 if (Boolean.FALSE.equals(result)) {
204 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
205 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700206 } else {
207 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700208 }
209 setupUi();
210 }
211 }
212
Ben Komalo91a2f052011-08-16 17:48:25 -0700213 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600214 @Override
215 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600216 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700217 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600218 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600219 break;
Jason parks35933812011-01-21 15:48:20 -0600220
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700221 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600222 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600223 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700224
225 case MESSAGE_NOTIFY:
226 notifyUser();
227 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600228 }
229 }
230 };
Jason parks35933812011-01-21 15:48:20 -0600231
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700232 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800233 /** The status bar where back/home/recent buttons are shown. */
234 private StatusBarManager mStatusBar;
235
236 /** All the widgets to disable in the status bar */
237 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
238 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
239 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
240 | StatusBarManager.DISABLE_SYSTEM_INFO
241 | StatusBarManager.DISABLE_HOME
242 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700243
Ben Komalo91a2f052011-08-16 17:48:25 -0700244 /** @return whether or not this Activity was started for debugging the UI only. */
245 private boolean isDebugView() {
246 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
247 }
248
249 /** @return whether or not this Activity was started for debugging the specific UI view only. */
250 private boolean isDebugView(String viewType /* non-nullable */) {
251 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
252 }
253
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700254 /**
255 * Notify the user that we are awaiting input. Currently this sends an audio alert.
256 */
257 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700258 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700259 --mNotificationCountdown;
260 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700261 try {
262 // Play the standard keypress sound at full volume. This should be available on
263 // every device. We cannot play a ringtone here because media services aren't
264 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
265 // on tablet devices. The idea is to alert the user that something is needed: this
266 // does not have to be pleasing.
267 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
268 } catch (Exception e) {
269 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
270 }
271 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700272 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700273 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700274 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700275 }
276
277 /**
278 * Ignore back events after the user has entered the decrypt screen and while the device is
279 * encrypting.
280 */
281 @Override
282 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800283 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700284 if (mIgnoreBack)
285 return;
286 super.onBackPressed();
287 }
288
Jason parks8fd5bc92011-01-12 16:03:31 -0600289 @Override
290 public void onCreate(Bundle savedInstanceState) {
291 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600292
Andy Stadler95974062011-02-01 17:35:20 -0800293 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700294 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700295 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600296 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600297 PackageManager pm = getPackageManager();
298 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700299 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
300 PackageManager.DONT_KILL_APP);
301 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700302 // want to be running, so need to finish this activity. We can count
303 // on the activity manager re-launching the new home app upon finishing
304 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700305 // NOTE: This is really grungy. I think it would be better for the
306 // activity manager to explicitly launch the crypt keeper instead of
307 // home in the situation where we need to decrypt the device
308 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600309 return;
310 }
Jason parks35933812011-01-21 15:48:20 -0600311
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700312 // Disable the status bar, but do NOT disable back because the user needs a way to go
313 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800314 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
315 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800316
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700317 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700318 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800319 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700320 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800321 if (lastInstance instanceof NonConfigurationInstanceState) {
322 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
323 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700324 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800325 }
Jason parksec5a45e2011-01-18 15:28:36 -0600326 }
Jason parks35933812011-01-21 15:48:20 -0600327
Andy Stadler95974062011-02-01 17:35:20 -0800328 /**
329 * Note, we defer the state check and screen setup to onStart() because this will be
330 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
331 * especially important if we were to lose the wakelock for any reason.
332 */
333 @Override
334 public void onStart() {
335 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700336 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700337 }
338
339 /**
340 * Initializes the UI based on the current state of encryption.
341 * This is idempotent - calling repeatedly will simply re-initialize the UI.
342 */
343 private void setupUi() {
344 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
345 setContentView(R.layout.crypt_keeper_progress);
346 showFactoryReset();
347 return;
348 }
349
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700350 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700351 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800352 setContentView(R.layout.crypt_keeper_progress);
353 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700354 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800355 setContentView(R.layout.crypt_keeper_password_entry);
356 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700357 } else if (!mValidationRequested) {
358 // We're supposed to be encrypted, but no validation has been done.
359 new ValidationTask().execute((Void[]) null);
360 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800361 }
362 }
363
Jason parksf8217302011-01-26 13:11:42 -0600364 @Override
365 public void onStop() {
366 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700367 mHandler.removeMessages(MESSAGE_COOLDOWN);
368 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
369 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800370 }
371
372 /**
373 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
374 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
375 * mWakeLock so the subsequent call to onDestroy does not release it.
376 */
377 @Override
378 public Object onRetainNonConfigurationInstance() {
379 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700380 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800381 mWakeLock = null;
382 return state;
383 }
384
385 @Override
386 public void onDestroy() {
387 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600388
389 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700390 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600391 mWakeLock.release();
392 mWakeLock = null;
393 }
394 }
395
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700396 /**
397 * Start encrypting the device.
398 */
Jason parksec5a45e2011-01-18 15:28:36 -0600399 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600400 // Accquire a partial wakelock to prevent the device from sleeping. Note
401 // we never release this wakelock as we will be restarted after the device
402 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700403 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700404 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700405 Log.d(TAG, "Acquiring wakelock.");
406 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
407 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
408 mWakeLock.acquire();
409 }
Jason parks35933812011-01-21 15:48:20 -0600410
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700411 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700412 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800413 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700414 // Start the first run of progress manually. This method sets up messages to occur at
415 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600416 updateProgress();
417 }
418
Andy Stadler13d62042011-01-31 19:21:37 -0800419 private void showFactoryReset() {
420 // Hide the encryption-bot to make room for the "factory reset" button
421 findViewById(R.id.encroid).setVisibility(View.GONE);
422
423 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700424 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800425 button.setVisibility(View.VISIBLE);
426 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700427 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800428 public void onClick(View v) {
429 // Factory reset the device.
430 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
431 }
432 });
433
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700434 // Alert the user of the failure.
435 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
436 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800437
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700438 final View view = findViewById(R.id.bottom_divider);
439 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700440 if (view != null) {
441 view.setVisibility(View.VISIBLE);
442 }
Andy Stadler13d62042011-01-31 19:21:37 -0800443 }
444
Jason parksf8217302011-01-26 13:11:42 -0600445 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700446 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600447
Ben Komalo0e666092011-09-01 15:42:00 -0700448 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800449 showFactoryReset();
450 return;
451 }
452
Jason parksf8217302011-01-26 13:11:42 -0600453 int progress = 0;
454 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700455 // Force a 50% progress state when debugging the view.
456 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600457 } catch (Exception e) {
458 Log.w(TAG, "Error parsing progress: " + e.toString());
459 }
460
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700461 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700462 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700463 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700464 if (tv != null) {
465 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
466 }
Jason parksf8217302011-01-26 13:11:42 -0600467 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700468 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
469 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600470 }
471
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700472 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600473 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700474 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800475
Jason parksf8217302011-01-26 13:11:42 -0600476 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700477 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600478 mPasswordEntry.setEnabled(true);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800479 setBackFunctionality(true);
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700480 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600481 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800482 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700483 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800484
Jason parksf8217302011-01-26 13:11:42 -0600485 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700486 mHandler.removeMessages(MESSAGE_COOLDOWN);
487 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600488 }
Jason parksec5a45e2011-01-18 15:28:36 -0600489 }
Jason parks35933812011-01-21 15:48:20 -0600490
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800491 /**
492 * Sets the back status: enabled or disabled according to the parameter.
493 * @param isEnabled true if back is enabled, false otherwise.
494 */
495 private final void setBackFunctionality(boolean isEnabled) {
496 mIgnoreBack = !isEnabled;
497 if (isEnabled) {
498 mStatusBar.disable(sWidgetsToDisable);
499 } else {
500 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
501 }
502 }
503
Jason parksec5a45e2011-01-18 15:28:36 -0600504 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600505 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
506 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700507 mPasswordEntry.requestFocus();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700508 // Become quiet when the user interacts with the Edit text screen.
509 mPasswordEntry.setOnKeyListener(this);
510 mPasswordEntry.setOnTouchListener(this);
511 mPasswordEntry.addTextChangedListener(this);
Jason parks35933812011-01-21 15:48:20 -0600512
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700513 // Disable the Emergency call button if the device has no voice telephone capability
514 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
515 if (!tm.isVoiceCapable()) {
516 final View emergencyCall = findViewById(R.id.emergencyCallButton);
517 if (emergencyCall != null) {
518 Log.d(TAG, "Removing the emergency Call button");
519 emergencyCall.setVisibility(View.GONE);
520 }
521 }
522
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700523 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700524 final InputMethodManager imm = (InputMethodManager) getSystemService(
525 Context.INPUT_METHOD_SERVICE);
526 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
527 imeSwitcher.setVisibility(View.VISIBLE);
528 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700529 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700530 public void onClick(View v) {
531 imm.showInputMethodPicker();
532 }
533 });
Jason parks00046d62011-06-13 17:38:45 -0500534 }
David Brown8373b452011-06-20 12:38:45 -0700535
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700536 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
537 // is completely non-functional, and we want the user to notice the device and enter a
538 // password.
539 if (mWakeLock == null) {
540 Log.d(TAG, "Acquiring wakelock.");
541 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
542 if (pm != null) {
543 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
544 mWakeLock.acquire();
545 }
546 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700547 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
548 // immediately.
549 mHandler.postDelayed(new Runnable() {
550 @Override public void run() {
551 imm.showSoftInputUnchecked(0, null);
552 }
553 }, 0);
554
David Brown8373b452011-06-20 12:38:45 -0700555 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700556 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700557 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700558 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jason parks8fd5bc92011-01-12 16:03:31 -0600559 }
560
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700561 /**
562 * Method adapted from com.android.inputmethod.latin.Utils
563 *
564 * @param imm The input method manager
565 * @param shouldIncludeAuxiliarySubtypes
566 * @return true if we have multiple IMEs to choose from
567 */
568 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
569 final boolean shouldIncludeAuxiliarySubtypes) {
570 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
571
572 // Number of the filtered IMEs
573 int filteredImisCount = 0;
574
575 for (InputMethodInfo imi : enabledImis) {
576 // We can return true immediately after we find two or more filtered IMEs.
577 if (filteredImisCount > 1) return true;
578 final List<InputMethodSubtype> subtypes =
579 imm.getEnabledInputMethodSubtypeList(imi, true);
580 // IMEs that have no subtypes should be counted.
581 if (subtypes.isEmpty()) {
582 ++filteredImisCount;
583 continue;
584 }
585
586 int auxCount = 0;
587 for (InputMethodSubtype subtype : subtypes) {
588 if (subtype.isAuxiliary()) {
589 ++auxCount;
590 }
591 }
592 final int nonAuxCount = subtypes.size() - auxCount;
593
594 // IMEs that have one or more non-auxiliary subtypes should be counted.
595 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
596 // subtypes should be counted as well.
597 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
598 ++filteredImisCount;
599 continue;
600 }
601 }
602
603 return filteredImisCount > 1
604 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
605 // input method subtype (The current IME should be LatinIME.)
606 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
607 }
608
Jason parks8fd5bc92011-01-12 16:03:31 -0600609 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700610 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600611 if (service != null) {
612 return IMountService.Stub.asInterface(service);
613 }
614 return null;
615 }
616
617 @Override
618 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500619 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600620 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700621 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600622
Jason parksec5a45e2011-01-18 15:28:36 -0600623 if (TextUtils.isEmpty(password)) {
624 return true;
625 }
Jason parks35933812011-01-21 15:48:20 -0600626
Jason parks8fd5bc92011-01-12 16:03:31 -0600627 // Now that we have the password clear the password field.
628 v.setText(null);
629
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700630 // Disable the password entry and back keypress while checking the password. These
631 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600632 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800633 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600634
Ben Komalo04606752011-08-18 14:50:26 -0700635 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600636 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600637
Jason parks8fd5bc92011-01-12 16:03:31 -0600638 return true;
639 }
640 return false;
641 }
David Brown8373b452011-06-20 12:38:45 -0700642
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700643 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700644 * Set airplane mode on the device if it isn't an LTE device.
645 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
646 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
647 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
648 * both these problems, we turn the radio off. However, on certain networks turning on and
649 * off the radio takes a long time. In such cases, we are better off leaving the radio
650 * running so the latency of an E911 call is short.
651 * The behavior after this is:
652 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
653 * airplane mode and restart the radio.
654 * 2. Full boot: we read the persistent settings from the previous boot and restore the
655 * radio to whatever it was before it restarted. This also happens when rebooting a
656 * phone that has no encryption.
657 */
658 private final void setAirplaneModeIfNecessary() {
659 final boolean isLteDevice =
Wink Saville55434042012-06-14 12:33:43 -0700660 TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700661 if (!isLteDevice) {
662 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700663 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700664 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
665 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700666 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700667 }
668 }
669
670 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700671 * Code to update the state of, and handle clicks from, the "Emergency call" button.
672 *
673 * This code is mostly duplicated from the corresponding code in
674 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
675 */
David Brown8373b452011-06-20 12:38:45 -0700676 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700677 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700678 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700679 if (emergencyCall == null)
680 return;
David Brown8373b452011-06-20 12:38:45 -0700681
682 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700683 emergencyCall.setVisibility(View.VISIBLE);
684 emergencyCall.setOnClickListener(new View.OnClickListener() {
685 @Override
686
David Brown8373b452011-06-20 12:38:45 -0700687 public void onClick(View v) {
688 takeEmergencyCallAction();
689 }
690 });
691 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700692 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700693 return;
694 }
695
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700696 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700697 int textId;
698 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700699 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700700 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700701 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
702 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700703 } else {
704 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700705 final int emergencyIcon = R.drawable.ic_emergency;
706 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700707 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700708 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700709 }
710
711 private boolean isEmergencyCallCapable() {
712 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
713 }
714
715 private void takeEmergencyCallAction() {
716 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
717 resumeCall();
718 } else {
719 launchEmergencyDialer();
720 }
721 }
722
723 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700724 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700725 if (phone != null) {
726 try {
727 phone.showCallScreen();
728 } catch (RemoteException e) {
729 Log.e(TAG, "Error calling ITelephony service: " + e);
730 }
731 }
732 }
733
734 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700735 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700736 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
737 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
738 startActivity(intent);
739 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700740
741 /**
742 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
743 */
744 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700745 mNotificationCountdown = 20;
746 }
747
748 @Override
749 public boolean onKey(View v, int keyCode, KeyEvent event) {
750 delayAudioNotification();
751 return false;
752 }
753
754 @Override
755 public boolean onTouch(View v, MotionEvent event) {
756 delayAudioNotification();
757 return false;
758 }
759
760 @Override
761 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
762 return;
763 }
764
765 @Override
766 public void onTextChanged(CharSequence s, int start, int before, int count) {
767 delayAudioNotification();
768 }
769
770 @Override
771 public void afterTextChanged(Editable s) {
772 return;
773 }
David Brown8373b452011-06-20 12:38:45 -0700774}