blob: 7057f1a497c5b691035a6929bc483c35449f4f3d [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
Alon Albert66d050b2013-09-30 17:08:12 -0700242 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800243 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700244
Ben Komalo91a2f052011-08-16 17:48:25 -0700245 /** @return whether or not this Activity was started for debugging the UI only. */
246 private boolean isDebugView() {
247 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
248 }
249
250 /** @return whether or not this Activity was started for debugging the specific UI view only. */
251 private boolean isDebugView(String viewType /* non-nullable */) {
252 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
253 }
254
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700255 /**
256 * Notify the user that we are awaiting input. Currently this sends an audio alert.
257 */
258 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700259 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700260 --mNotificationCountdown;
261 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700262 try {
263 // Play the standard keypress sound at full volume. This should be available on
264 // every device. We cannot play a ringtone here because media services aren't
265 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
266 // on tablet devices. The idea is to alert the user that something is needed: this
267 // does not have to be pleasing.
268 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
269 } catch (Exception e) {
270 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
271 }
272 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700273 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700274 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700275 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700276 }
277
278 /**
279 * Ignore back events after the user has entered the decrypt screen and while the device is
280 * encrypting.
281 */
282 @Override
283 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800284 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700285 if (mIgnoreBack)
286 return;
287 super.onBackPressed();
288 }
289
Jason parks8fd5bc92011-01-12 16:03:31 -0600290 @Override
291 public void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600293
Andy Stadler95974062011-02-01 17:35:20 -0800294 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700295 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700296 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600297 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600298 PackageManager pm = getPackageManager();
299 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700300 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
301 PackageManager.DONT_KILL_APP);
302 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700303 // want to be running, so need to finish this activity. We can count
304 // on the activity manager re-launching the new home app upon finishing
305 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700306 // NOTE: This is really grungy. I think it would be better for the
307 // activity manager to explicitly launch the crypt keeper instead of
308 // home in the situation where we need to decrypt the device
309 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600310 return;
311 }
Jason parks35933812011-01-21 15:48:20 -0600312
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700313 // Disable the status bar, but do NOT disable back because the user needs a way to go
314 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800315 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
316 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800317
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700318 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700319 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800320 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700321 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800322 if (lastInstance instanceof NonConfigurationInstanceState) {
323 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
324 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700325 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800326 }
Jason parksec5a45e2011-01-18 15:28:36 -0600327 }
Jason parks35933812011-01-21 15:48:20 -0600328
Andy Stadler95974062011-02-01 17:35:20 -0800329 /**
330 * Note, we defer the state check and screen setup to onStart() because this will be
331 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
332 * especially important if we were to lose the wakelock for any reason.
333 */
334 @Override
335 public void onStart() {
336 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700337 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700338 }
339
340 /**
341 * Initializes the UI based on the current state of encryption.
342 * This is idempotent - calling repeatedly will simply re-initialize the UI.
343 */
344 private void setupUi() {
345 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
346 setContentView(R.layout.crypt_keeper_progress);
347 showFactoryReset();
348 return;
349 }
350
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700351 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700352 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800353 setContentView(R.layout.crypt_keeper_progress);
354 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700355 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800356 setContentView(R.layout.crypt_keeper_password_entry);
357 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700358 } else if (!mValidationRequested) {
359 // We're supposed to be encrypted, but no validation has been done.
360 new ValidationTask().execute((Void[]) null);
361 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800362 }
363 }
364
Jason parksf8217302011-01-26 13:11:42 -0600365 @Override
366 public void onStop() {
367 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700368 mHandler.removeMessages(MESSAGE_COOLDOWN);
369 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
370 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800371 }
372
373 /**
374 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
375 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
376 * mWakeLock so the subsequent call to onDestroy does not release it.
377 */
378 @Override
379 public Object onRetainNonConfigurationInstance() {
380 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700381 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800382 mWakeLock = null;
383 return state;
384 }
385
386 @Override
387 public void onDestroy() {
388 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600389
390 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700391 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600392 mWakeLock.release();
393 mWakeLock = null;
394 }
395 }
396
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700397 /**
398 * Start encrypting the device.
399 */
Jason parksec5a45e2011-01-18 15:28:36 -0600400 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600401 // Accquire a partial wakelock to prevent the device from sleeping. Note
402 // we never release this wakelock as we will be restarted after the device
403 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700404 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700405 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700406 Log.d(TAG, "Acquiring wakelock.");
407 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
408 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
409 mWakeLock.acquire();
410 }
Jason parks35933812011-01-21 15:48:20 -0600411
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700412 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700413 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800414 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700415 // Start the first run of progress manually. This method sets up messages to occur at
416 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600417 updateProgress();
418 }
419
Andy Stadler13d62042011-01-31 19:21:37 -0800420 private void showFactoryReset() {
421 // Hide the encryption-bot to make room for the "factory reset" button
422 findViewById(R.id.encroid).setVisibility(View.GONE);
423
424 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700425 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800426 button.setVisibility(View.VISIBLE);
427 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700428 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800429 public void onClick(View v) {
430 // Factory reset the device.
431 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
432 }
433 });
434
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700435 // Alert the user of the failure.
436 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
437 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800438
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700439 final View view = findViewById(R.id.bottom_divider);
440 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700441 if (view != null) {
442 view.setVisibility(View.VISIBLE);
443 }
Andy Stadler13d62042011-01-31 19:21:37 -0800444 }
445
Jason parksf8217302011-01-26 13:11:42 -0600446 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700447 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600448
Ben Komalo0e666092011-09-01 15:42:00 -0700449 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800450 showFactoryReset();
451 return;
452 }
453
Jason parksf8217302011-01-26 13:11:42 -0600454 int progress = 0;
455 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700456 // Force a 50% progress state when debugging the view.
457 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600458 } catch (Exception e) {
459 Log.w(TAG, "Error parsing progress: " + e.toString());
460 }
461
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700462 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700463 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700464 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700465 if (tv != null) {
466 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
467 }
Jason parksf8217302011-01-26 13:11:42 -0600468 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700469 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
470 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600471 }
472
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700473 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600474 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700475 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800476
Jason parksf8217302011-01-26 13:11:42 -0600477 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700478 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600479 mPasswordEntry.setEnabled(true);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800480 setBackFunctionality(true);
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700481 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600482 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800483 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700484 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800485
Jason parksf8217302011-01-26 13:11:42 -0600486 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700487 mHandler.removeMessages(MESSAGE_COOLDOWN);
488 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600489 }
Jason parksec5a45e2011-01-18 15:28:36 -0600490 }
Jason parks35933812011-01-21 15:48:20 -0600491
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800492 /**
493 * Sets the back status: enabled or disabled according to the parameter.
494 * @param isEnabled true if back is enabled, false otherwise.
495 */
496 private final void setBackFunctionality(boolean isEnabled) {
497 mIgnoreBack = !isEnabled;
498 if (isEnabled) {
499 mStatusBar.disable(sWidgetsToDisable);
500 } else {
501 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
502 }
503 }
504
Jason parksec5a45e2011-01-18 15:28:36 -0600505 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600506 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
507 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700508 mPasswordEntry.requestFocus();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700509 // Become quiet when the user interacts with the Edit text screen.
510 mPasswordEntry.setOnKeyListener(this);
511 mPasswordEntry.setOnTouchListener(this);
512 mPasswordEntry.addTextChangedListener(this);
Jason parks35933812011-01-21 15:48:20 -0600513
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700514 // Disable the Emergency call button if the device has no voice telephone capability
515 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
516 if (!tm.isVoiceCapable()) {
517 final View emergencyCall = findViewById(R.id.emergencyCallButton);
518 if (emergencyCall != null) {
519 Log.d(TAG, "Removing the emergency Call button");
520 emergencyCall.setVisibility(View.GONE);
521 }
522 }
523
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700524 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700525 final InputMethodManager imm = (InputMethodManager) getSystemService(
526 Context.INPUT_METHOD_SERVICE);
527 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
528 imeSwitcher.setVisibility(View.VISIBLE);
529 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700530 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700531 public void onClick(View v) {
532 imm.showInputMethodPicker();
533 }
534 });
Jason parks00046d62011-06-13 17:38:45 -0500535 }
David Brown8373b452011-06-20 12:38:45 -0700536
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700537 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
538 // is completely non-functional, and we want the user to notice the device and enter a
539 // password.
540 if (mWakeLock == null) {
541 Log.d(TAG, "Acquiring wakelock.");
542 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
543 if (pm != null) {
544 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
545 mWakeLock.acquire();
546 }
547 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700548 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
549 // immediately.
550 mHandler.postDelayed(new Runnable() {
551 @Override public void run() {
552 imm.showSoftInputUnchecked(0, null);
553 }
554 }, 0);
555
David Brown8373b452011-06-20 12:38:45 -0700556 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700557 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700558 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700559 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jason parks8fd5bc92011-01-12 16:03:31 -0600560 }
561
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700562 /**
563 * Method adapted from com.android.inputmethod.latin.Utils
564 *
565 * @param imm The input method manager
566 * @param shouldIncludeAuxiliarySubtypes
567 * @return true if we have multiple IMEs to choose from
568 */
569 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
570 final boolean shouldIncludeAuxiliarySubtypes) {
571 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
572
573 // Number of the filtered IMEs
574 int filteredImisCount = 0;
575
576 for (InputMethodInfo imi : enabledImis) {
577 // We can return true immediately after we find two or more filtered IMEs.
578 if (filteredImisCount > 1) return true;
579 final List<InputMethodSubtype> subtypes =
580 imm.getEnabledInputMethodSubtypeList(imi, true);
581 // IMEs that have no subtypes should be counted.
582 if (subtypes.isEmpty()) {
583 ++filteredImisCount;
584 continue;
585 }
586
587 int auxCount = 0;
588 for (InputMethodSubtype subtype : subtypes) {
589 if (subtype.isAuxiliary()) {
590 ++auxCount;
591 }
592 }
593 final int nonAuxCount = subtypes.size() - auxCount;
594
595 // IMEs that have one or more non-auxiliary subtypes should be counted.
596 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
597 // subtypes should be counted as well.
598 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
599 ++filteredImisCount;
600 continue;
601 }
602 }
603
604 return filteredImisCount > 1
605 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
606 // input method subtype (The current IME should be LatinIME.)
607 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
608 }
609
Jason parks8fd5bc92011-01-12 16:03:31 -0600610 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700611 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600612 if (service != null) {
613 return IMountService.Stub.asInterface(service);
614 }
615 return null;
616 }
617
618 @Override
619 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500620 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600621 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700622 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600623
Jason parksec5a45e2011-01-18 15:28:36 -0600624 if (TextUtils.isEmpty(password)) {
625 return true;
626 }
Jason parks35933812011-01-21 15:48:20 -0600627
Jason parks8fd5bc92011-01-12 16:03:31 -0600628 // Now that we have the password clear the password field.
629 v.setText(null);
630
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700631 // Disable the password entry and back keypress while checking the password. These
632 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600633 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800634 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600635
Ben Komalo04606752011-08-18 14:50:26 -0700636 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600637 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600638
Jason parks8fd5bc92011-01-12 16:03:31 -0600639 return true;
640 }
641 return false;
642 }
David Brown8373b452011-06-20 12:38:45 -0700643
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700644 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700645 * Set airplane mode on the device if it isn't an LTE device.
646 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
647 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
648 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
649 * both these problems, we turn the radio off. However, on certain networks turning on and
650 * off the radio takes a long time. In such cases, we are better off leaving the radio
651 * running so the latency of an E911 call is short.
652 * The behavior after this is:
653 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
654 * airplane mode and restart the radio.
655 * 2. Full boot: we read the persistent settings from the previous boot and restore the
656 * radio to whatever it was before it restarted. This also happens when rebooting a
657 * phone that has no encryption.
658 */
659 private final void setAirplaneModeIfNecessary() {
660 final boolean isLteDevice =
Wink Saville55434042012-06-14 12:33:43 -0700661 TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700662 if (!isLteDevice) {
663 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700664 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700665 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
666 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700667 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700668 }
669 }
670
671 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700672 * Code to update the state of, and handle clicks from, the "Emergency call" button.
673 *
674 * This code is mostly duplicated from the corresponding code in
675 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
676 */
David Brown8373b452011-06-20 12:38:45 -0700677 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700678 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700679 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700680 if (emergencyCall == null)
681 return;
David Brown8373b452011-06-20 12:38:45 -0700682
683 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700684 emergencyCall.setVisibility(View.VISIBLE);
685 emergencyCall.setOnClickListener(new View.OnClickListener() {
686 @Override
687
David Brown8373b452011-06-20 12:38:45 -0700688 public void onClick(View v) {
689 takeEmergencyCallAction();
690 }
691 });
692 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700693 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700694 return;
695 }
696
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700697 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700698 int textId;
699 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700700 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700701 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700702 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
703 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700704 } else {
705 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700706 final int emergencyIcon = R.drawable.ic_emergency;
707 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700708 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700709 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700710 }
711
712 private boolean isEmergencyCallCapable() {
713 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
714 }
715
716 private void takeEmergencyCallAction() {
717 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
718 resumeCall();
719 } else {
720 launchEmergencyDialer();
721 }
722 }
723
724 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700725 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700726 if (phone != null) {
727 try {
728 phone.showCallScreen();
729 } catch (RemoteException e) {
730 Log.e(TAG, "Error calling ITelephony service: " + e);
731 }
732 }
733 }
734
735 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700736 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700737 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
738 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
739 startActivity(intent);
740 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700741
742 /**
743 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
744 */
745 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700746 mNotificationCountdown = 20;
747 }
748
749 @Override
750 public boolean onKey(View v, int keyCode, KeyEvent event) {
751 delayAudioNotification();
752 return false;
753 }
754
755 @Override
756 public boolean onTouch(View v, MotionEvent event) {
757 delayAudioNotification();
758 return false;
759 }
760
761 @Override
762 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
763 return;
764 }
765
766 @Override
767 public void onTextChanged(CharSequence s, int start, int before, int count) {
768 delayAudioNotification();
769 }
770
771 @Override
772 public void afterTextChanged(Editable s) {
773 return;
774 }
David Brown8373b452011-06-20 12:38:45 -0700775}