blob: fa63a74203ece8c8c881ca8c5f545c618f865d89 [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;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070024import android.content.pm.ActivityInfo;
Jason parks8fd5bc92011-01-12 16:03:31 -060025import android.content.pm.PackageManager;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070026import android.content.res.Resources.NotFoundException;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070027import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060028import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060031import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060032import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060033import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070034import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060035import android.os.ServiceManager;
36import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070037import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080039import android.os.storage.StorageManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070040import android.provider.Settings;
Tyler Gunn3e71b192014-09-10 15:21:33 -070041import android.telecom.TelecomManager;
David Brown8373b452011-06-20 12:38:45 -070042import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070043import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060044import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070045import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070046import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060047import android.util.Log;
48import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070049import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080050import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080051import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080052import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070053import android.view.View.OnKeyListener;
54import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060055import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070056import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060057import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070058import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080059import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060060import android.widget.EditText;
61import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060062import android.widget.TextView;
63
Wink Saville55434042012-06-14 12:33:43 -070064import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080065import com.android.internal.widget.LockPatternUtils;
66import com.android.internal.widget.LockPatternView;
67import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070068
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070069import static com.android.internal.widget.LockPatternView.DisplayMode;
70
Ben Komalo9fcb6a72011-08-26 14:40:18 -070071import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070072
73/**
74 * Settings screens to show the UI flows for encrypting/decrypting the device.
75 *
76 * This may be started via adb for debugging the UI layout, without having to go through
77 * encryption flows everytime. It should be noted that starting the activity in this manner
78 * is only useful for verifying UI-correctness - the behavior will not be identical.
79 * <pre>
80 * $ adb shell pm enable com.android.settings/.CryptKeeper
81 * $ adb shell am start \
82 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
83 * -n com.android.settings/.CryptKeeper
84 * </pre>
85 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070086public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
87 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060088 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060089
Jason parks8fd5bc92011-01-12 16:03:31 -060090 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070091 /** Message sent to us to indicate encryption update progress. */
92 private static final int MESSAGE_UPDATE_PROGRESS = 1;
93 /** Message sent to us to cool-down (waste user's time between password attempts) */
94 private static final int MESSAGE_COOLDOWN = 2;
95 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
96 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060097
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070098 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060099 private static final int MAX_FAILED_ATTEMPTS = 30;
100 private static final int COOL_DOWN_ATTEMPTS = 10;
101 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
102
David Brown8373b452011-06-20 12:38:45 -0700103 // Intent action for launching the Emergency Dialer activity.
104 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
105
Ben Komalo91a2f052011-08-16 17:48:25 -0700106 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
107 private static final String EXTRA_FORCE_VIEW =
108 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
109 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700110 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700111 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700112
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700113 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700114 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700115 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700116 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
117 private boolean mEncryptionGoneBad;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700118 /** If gone bad, should we show encryption failed (false) or corrupt (true)*/
119 private boolean mCorrupt;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700120 /** A flag to indicate when the back event should be ignored */
121 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800122 private int mCooldown;
123 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600124 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800125 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700126 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
127 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700128 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
129 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700130 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800131
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700132 // how long we wait to clear a wrong pattern
133 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
134
Paul Lawrence0f11e152014-08-20 07:43:32 -0700135 // how long we wait to clear a right pattern
136 private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
137
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700138 private Runnable mClearPatternRunnable = new Runnable() {
Paul Crowley529834d2014-10-21 10:12:47 +0100139 @Override
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700140 public void run() {
141 mLockPatternView.clearPattern();
142 }
143 };
144
Andy Stadler14997402011-02-01 15:34:59 -0800145 /**
146 * Used to propagate state through configuration changes (e.g. screen rotation)
147 */
148 private static class NonConfigurationInstanceState {
149 final PowerManager.WakeLock wakelock;
150
151 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
152 wakelock = _wakelock;
153 }
154 }
155
Jason parks06c5ff42011-03-01 10:17:40 -0600156 private class DecryptTask extends AsyncTask<String, Void, Integer> {
Paul Lawrenceced7db42014-09-18 11:23:40 -0700157 private void hide(int id) {
158 View view = findViewById(id);
159 if (view != null) {
160 view.setVisibility(View.GONE);
161 }
162 }
163
Jason parks06c5ff42011-03-01 10:17:40 -0600164 @Override
Paul Crowley529834d2014-10-21 10:12:47 +0100165 protected void onPreExecute() {
166 super.onPreExecute();
167 final TextView status = (TextView) findViewById(R.id.status);
168 status.setText(R.string.checking_decryption);
169 }
170
171 @Override
Jason parks06c5ff42011-03-01 10:17:40 -0600172 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700173 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600174 try {
175 return service.decryptStorage(params[0]);
176 } catch (Exception e) {
177 Log.e(TAG, "Error while decrypting...", e);
178 return -1;
179 }
180 }
181
182 @Override
183 protected void onPostExecute(Integer failedAttempts) {
184 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700185 // The password was entered successfully. Simply do nothing
186 // and wait for the service restart to switch to surfacefligner
Paul Lawrence0f11e152014-08-20 07:43:32 -0700187 if (mLockPatternView != null) {
188 mLockPatternView.removeCallbacks(mClearPatternRunnable);
189 mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
190 }
Paul Crowley529834d2014-10-21 10:12:47 +0100191 final TextView status = (TextView) findViewById(R.id.status);
192 status.setText(R.string.starting_android);
Paul Lawrenceced7db42014-09-18 11:23:40 -0700193 hide(R.id.passwordEntry);
194 hide(R.id.switch_ime_button);
195 hide(R.id.lockPattern);
Paul Lawrenceced7db42014-09-18 11:23:40 -0700196 hide(R.id.owner_info);
197 hide(R.id.emergencyCallButton);
Jason parks06c5ff42011-03-01 10:17:40 -0600198 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
199 // Factory reset the device.
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700200 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
201 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
202 intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
203 sendBroadcast(intent);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700204 } else if (failedAttempts == -1) {
205 // Right password, but decryption failed. Tell user bad news ...
206 setContentView(R.layout.crypt_keeper_progress);
207 showFactoryReset(true);
208 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600209 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700210 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700211 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700212 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
213 mLockPatternView.removeCallbacks(mClearPatternRunnable);
214 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
215 }
216 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
217 mCooldown = COOL_DOWN_INTERVAL;
218 cooldown();
219 } else {
220 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700221
222 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
223 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
224 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
225 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
226 Integer.toString(remainingAttempts));
227 status.setText(warning);
228 } else {
Paul Crowley529834d2014-10-21 10:12:47 +0100229 int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
230 try {
231 final IMountService service = getMountService();
232 passwordType = service.getPasswordType();
233 } catch (Exception e) {
234 Log.e(TAG, "Error calling mount service " + e);
235 }
236
237 if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
238 status.setText(R.string.cryptkeeper_wrong_pin);
239 } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
240 status.setText(R.string.cryptkeeper_wrong_password);
241 } else {
242 status.setText(R.string.cryptkeeper_wrong_pin);
243 }
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700244 }
245
Paul Crowley529834d2014-10-21 10:12:47 +0100246
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700247 if (mLockPatternView != null) {
248 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
249 }
250 // Reenable the password entry
251 if (mPasswordEntry != null) {
252 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700253 final InputMethodManager imm = (InputMethodManager) getSystemService(
254 Context.INPUT_METHOD_SERVICE);
255 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700256 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700257 }
258 if (mLockPatternView != null) {
259 mLockPatternView.setEnabled(true);
260 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700261 }
Jason parks06c5ff42011-03-01 10:17:40 -0600262 }
263 }
264 }
Jason parks75c085e2011-02-10 14:03:47 -0600265
Ben Komalo0e666092011-09-01 15:42:00 -0700266 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
Paul Lawrence87abbd32014-08-28 15:56:38 -0700267 int state;
268
Ben Komalo0e666092011-09-01 15:42:00 -0700269 @Override
270 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700271 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700272 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700273 Log.d(TAG, "Validating encryption state.");
Paul Lawrence87abbd32014-08-28 15:56:38 -0700274 state = service.getEncryptionState();
Ben Komalo0e666092011-09-01 15:42:00 -0700275 if (state == IMountService.ENCRYPTION_STATE_NONE) {
276 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
277 return true; // Unexpected, but fine, I guess...
278 }
279 return state == IMountService.ENCRYPTION_STATE_OK;
280 } catch (RemoteException e) {
281 Log.w(TAG, "Unable to get encryption state properly");
282 return true;
283 }
284 }
285
286 @Override
287 protected void onPostExecute(Boolean result) {
288 mValidationComplete = true;
289 if (Boolean.FALSE.equals(result)) {
290 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
291 mEncryptionGoneBad = true;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700292 mCorrupt = state == IMountService.ENCRYPTION_STATE_ERROR_CORRUPT;
Ben Komalod4758ef2011-09-08 14:36:08 -0700293 } else {
294 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700295 }
296 setupUi();
297 }
298 }
299
Ben Komalo91a2f052011-08-16 17:48:25 -0700300 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600301 @Override
302 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600303 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700304 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600305 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600306 break;
Jason parks35933812011-01-21 15:48:20 -0600307
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700308 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600309 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600310 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700311
312 case MESSAGE_NOTIFY:
313 notifyUser();
314 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600315 }
316 }
317 };
Jason parks35933812011-01-21 15:48:20 -0600318
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700319 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800320 /** The status bar where back/home/recent buttons are shown. */
321 private StatusBarManager mStatusBar;
322
323 /** All the widgets to disable in the status bar */
324 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
325 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
326 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
327 | StatusBarManager.DISABLE_SYSTEM_INFO
328 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700329 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800330 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700331
Ben Komalo91a2f052011-08-16 17:48:25 -0700332 /** @return whether or not this Activity was started for debugging the UI only. */
333 private boolean isDebugView() {
334 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
335 }
336
337 /** @return whether or not this Activity was started for debugging the specific UI view only. */
338 private boolean isDebugView(String viewType /* non-nullable */) {
339 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
340 }
341
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700342 /**
343 * Notify the user that we are awaiting input. Currently this sends an audio alert.
344 */
345 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700346 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700347 --mNotificationCountdown;
348 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700349 try {
350 // Play the standard keypress sound at full volume. This should be available on
351 // every device. We cannot play a ringtone here because media services aren't
352 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
353 // on tablet devices. The idea is to alert the user that something is needed: this
354 // does not have to be pleasing.
355 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
356 } catch (Exception e) {
357 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
358 }
359 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700360 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700361 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700362 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700363
364 if (mWakeLock.isHeld()) {
365 if (mReleaseWakeLockCountdown > 0) {
366 --mReleaseWakeLockCountdown;
367 } else {
368 mWakeLock.release();
369 }
370 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700371 }
372
373 /**
374 * Ignore back events after the user has entered the decrypt screen and while the device is
375 * encrypting.
376 */
377 @Override
378 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800379 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700380 if (mIgnoreBack)
381 return;
382 super.onBackPressed();
383 }
384
Jason parks8fd5bc92011-01-12 16:03:31 -0600385 @Override
386 public void onCreate(Bundle savedInstanceState) {
387 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600388
Andy Stadler95974062011-02-01 17:35:20 -0800389 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700390 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700391 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600392 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600393 PackageManager pm = getPackageManager();
394 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700395 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
396 PackageManager.DONT_KILL_APP);
397 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700398 // want to be running, so need to finish this activity. We can count
399 // on the activity manager re-launching the new home app upon finishing
400 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700401 // NOTE: This is really grungy. I think it would be better for the
402 // activity manager to explicitly launch the crypt keeper instead of
403 // home in the situation where we need to decrypt the device
404 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600405 return;
406 }
Jason parks35933812011-01-21 15:48:20 -0600407
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700408 try {
409 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
410 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
411 }
412 } catch (NotFoundException e) {
413 }
414
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700415 // Disable the status bar, but do NOT disable back because the user needs a way to go
416 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800417 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
418 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800419
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700420 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700421 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800422 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700423 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800424 if (lastInstance instanceof NonConfigurationInstanceState) {
425 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
426 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700427 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800428 }
Jason parksec5a45e2011-01-18 15:28:36 -0600429 }
Jason parks35933812011-01-21 15:48:20 -0600430
Andy Stadler95974062011-02-01 17:35:20 -0800431 /**
432 * Note, we defer the state check and screen setup to onStart() because this will be
433 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
434 * especially important if we were to lose the wakelock for any reason.
435 */
436 @Override
437 public void onStart() {
438 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700439 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700440 }
441
442 /**
443 * Initializes the UI based on the current state of encryption.
444 * This is idempotent - calling repeatedly will simply re-initialize the UI.
445 */
446 private void setupUi() {
447 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
448 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence87abbd32014-08-28 15:56:38 -0700449 showFactoryReset(mCorrupt);
Ben Komalo0e666092011-09-01 15:42:00 -0700450 return;
451 }
452
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700453 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700454 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800455 setContentView(R.layout.crypt_keeper_progress);
456 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700457 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700458 new AsyncTask<Void, Void, Void>() {
Paul Crowley529834d2014-10-21 10:12:47 +0100459 int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700460 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700461 boolean pattern_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800462
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700463 @Override
464 public Void doInBackground(Void... v) {
465 try {
466 final IMountService service = getMountService();
Paul Crowley529834d2014-10-21 10:12:47 +0100467 passwordType = service.getPasswordType();
Elliott Hughesf9d6d292014-10-02 13:29:58 -0700468 owner_info = service.getField(StorageManager.OWNER_INFO_KEY);
469 pattern_visible = !("0".equals(service.getField(StorageManager.PATTERN_VISIBLE_KEY)));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700470 } catch (Exception e) {
471 Log.e(TAG, "Error calling mount service " + e);
472 }
473
474 return null;
475 }
476
477 @Override
478 public void onPostExecute(java.lang.Void v) {
Paul Crowley529834d2014-10-21 10:12:47 +0100479 if(passwordType == StorageManager.CRYPT_TYPE_PIN) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700480 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700481 mStatusString = R.string.enter_pin;
Paul Crowley529834d2014-10-21 10:12:47 +0100482 } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700483 setContentView(R.layout.crypt_keeper_pattern_entry);
484 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700485 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700486 } else {
487 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700488 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700489 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700490 final TextView status = (TextView) findViewById(R.id.status);
491 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700492
Paul Lawrence5a70f052014-06-13 11:09:55 -0700493 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
494 ownerInfo.setText(owner_info);
495 ownerInfo.setSelected(true); // Required for marquee'ing to work
496
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700497 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700498
Paul Lawrence0f11e152014-08-20 07:43:32 -0700499 if (mLockPatternView != null) {
500 mLockPatternView.setInStealthMode(!pattern_visible);
501 }
502
Paul Lawrence5a70f052014-06-13 11:09:55 -0700503 if (mCooldown > 0) {
504 setBackFunctionality(false);
505 cooldown(); // in case we are cooling down and coming back from emergency dialler
506 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700507 }
508 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700509 } else if (!mValidationRequested) {
510 // We're supposed to be encrypted, but no validation has been done.
511 new ValidationTask().execute((Void[]) null);
512 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800513 }
514 }
515
Jason parksf8217302011-01-26 13:11:42 -0600516 @Override
517 public void onStop() {
518 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700519 mHandler.removeMessages(MESSAGE_COOLDOWN);
520 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
521 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800522 }
523
524 /**
525 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
526 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
527 * mWakeLock so the subsequent call to onDestroy does not release it.
528 */
529 @Override
530 public Object onRetainNonConfigurationInstance() {
531 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700532 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800533 mWakeLock = null;
534 return state;
535 }
536
537 @Override
538 public void onDestroy() {
539 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600540
541 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700542 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600543 mWakeLock.release();
544 mWakeLock = null;
545 }
546 }
547
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700548 /**
549 * Start encrypting the device.
550 */
Jason parksec5a45e2011-01-18 15:28:36 -0600551 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600552 // Accquire a partial wakelock to prevent the device from sleeping. Note
553 // we never release this wakelock as we will be restarted after the device
554 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700555 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700556 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700557 Log.d(TAG, "Acquiring wakelock.");
558 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
559 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
560 mWakeLock.acquire();
561 }
Jason parks35933812011-01-21 15:48:20 -0600562
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700563 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700564 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800565 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700566 // Start the first run of progress manually. This method sets up messages to occur at
567 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600568 updateProgress();
569 }
570
Paul Lawrence384d8e52014-08-08 15:11:36 -0700571 /**
572 * Show factory reset screen allowing the user to reset their phone when
573 * there is nothing else we can do
574 * @param corrupt true if userdata is corrupt, false if encryption failed
575 * partway through
576 */
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700577 private void showFactoryReset(final boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800578 // Hide the encryption-bot to make room for the "factory reset" button
579 findViewById(R.id.encroid).setVisibility(View.GONE);
580
581 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700582 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800583 button.setVisibility(View.VISIBLE);
584 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700585 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800586 public void onClick(View v) {
587 // Factory reset the device.
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700588 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
589 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
590 intent.putExtra(Intent.EXTRA_REASON,
591 "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
592 sendBroadcast(intent);
Andy Stadler13d62042011-01-31 19:21:37 -0800593 }
594 });
595
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700596 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700597 if (corrupt) {
598 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
599 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
600 } else {
601 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
602 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
603 }
Andy Stadler13d62042011-01-31 19:21:37 -0800604
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700605 final View view = findViewById(R.id.bottom_divider);
606 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700607 if (view != null) {
608 view.setVisibility(View.VISIBLE);
609 }
Andy Stadler13d62042011-01-31 19:21:37 -0800610 }
611
Jason parksf8217302011-01-26 13:11:42 -0600612 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700613 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600614
Ben Komalo0e666092011-09-01 15:42:00 -0700615 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700616 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800617 return;
618 }
619
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700620 // Get status as percentage first
621 CharSequence status = getText(R.string.crypt_keeper_setup_description);
622 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600623 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700624 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700625 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600626 } catch (Exception e) {
627 Log.w(TAG, "Error parsing progress: " + e.toString());
628 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700629 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600630
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700631 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700632 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700633 try {
634 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
635 int time = Integer.parseInt(timeProperty);
636 if (time >= 0) {
637 // Round up to multiple of 10 - this way display is less jerky
638 time = (time + 9) / 10 * 10;
639 progress = DateUtils.formatElapsedTime(time);
640 status = getText(R.string.crypt_keeper_setup_time_remaining);
641 }
642 } catch (Exception e) {
643 // Will happen if no time etc - show percentage
644 }
645
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700646 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700647 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700648 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700649 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700650
651 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700652 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700653 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600654 }
655
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700656 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600657 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700658 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800659
Jason parksf8217302011-01-26 13:11:42 -0600660 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700661 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700662 if (mPasswordEntry != null) {
663 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700664 final InputMethodManager imm = (InputMethodManager) getSystemService(
665 Context.INPUT_METHOD_SERVICE);
666 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700667 setBackFunctionality(true);
668 }
669 if (mLockPatternView != null) {
670 mLockPatternView.setEnabled(true);
671 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700672 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600673 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700674 // Disable the password entry and back presses.
675 if (mPasswordEntry != null) {
676 mPasswordEntry.setEnabled(false);
677 }
678 if (mLockPatternView != null) {
679 mLockPatternView.setEnabled(false);
680 }
681
Andy Stadler13d62042011-01-31 19:21:37 -0800682 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700683 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800684
Jason parksf8217302011-01-26 13:11:42 -0600685 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700686 mHandler.removeMessages(MESSAGE_COOLDOWN);
687 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600688 }
Jason parksec5a45e2011-01-18 15:28:36 -0600689 }
Jason parks35933812011-01-21 15:48:20 -0600690
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800691 /**
692 * Sets the back status: enabled or disabled according to the parameter.
693 * @param isEnabled true if back is enabled, false otherwise.
694 */
695 private final void setBackFunctionality(boolean isEnabled) {
696 mIgnoreBack = !isEnabled;
697 if (isEnabled) {
698 mStatusBar.disable(sWidgetsToDisable);
699 } else {
700 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
701 }
702 }
703
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800704 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700705 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800706
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700707 @Override
708 public void onPatternStart() {
709 mLockPatternView.removeCallbacks(mClearPatternRunnable);
710 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800711
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700712 @Override
713 public void onPatternCleared() {
714 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800715
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700716 @Override
717 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
718 mLockPatternView.setEnabled(false);
719 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
720 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800721
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700722 @Override
723 public void onPatternCellAdded(List<Cell> pattern) {
724 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800725 };
726
727 private void passwordEntryInit() {
728 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600729 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800730 if (mPasswordEntry != null){
731 mPasswordEntry.setOnEditorActionListener(this);
732 mPasswordEntry.requestFocus();
733 // Become quiet when the user interacts with the Edit text screen.
734 mPasswordEntry.setOnKeyListener(this);
735 mPasswordEntry.setOnTouchListener(this);
736 mPasswordEntry.addTextChangedListener(this);
737 }
738
739 // Pattern case
740 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
741 if (mLockPatternView != null) {
742 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
743 }
Jason parks35933812011-01-21 15:48:20 -0600744
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700745 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700746 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700747 final View emergencyCall = findViewById(R.id.emergencyCallButton);
748 if (emergencyCall != null) {
749 Log.d(TAG, "Removing the emergency Call button");
750 emergencyCall.setVisibility(View.GONE);
751 }
752 }
753
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700754 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700755 final InputMethodManager imm = (InputMethodManager) getSystemService(
756 Context.INPUT_METHOD_SERVICE);
757 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
758 imeSwitcher.setVisibility(View.VISIBLE);
759 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700760 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700761 public void onClick(View v) {
762 imm.showInputMethodPicker();
763 }
764 });
Jason parks00046d62011-06-13 17:38:45 -0500765 }
David Brown8373b452011-06-20 12:38:45 -0700766
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700767 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
768 // is completely non-functional, and we want the user to notice the device and enter a
769 // password.
770 if (mWakeLock == null) {
771 Log.d(TAG, "Acquiring wakelock.");
772 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
773 if (pm != null) {
774 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
775 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700776 // Keep awake for 10 minutes - if the user hasn't been alerted by then
777 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700778 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700779 }
780 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700781
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700782 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
783 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700784 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700785 mHandler.postDelayed(new Runnable() {
786 @Override public void run() {
787 imm.showSoftInputUnchecked(0, null);
788 }
789 }, 0);
790 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700791
David Brown8373b452011-06-20 12:38:45 -0700792 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700793 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700794 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700795 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800796
John Spurlock2c526512014-09-17 12:55:17 -0400797 // Dismiss secure & non-secure keyguards while this screen is showing.
798 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
799 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Jason parks8fd5bc92011-01-12 16:03:31 -0600800 }
801
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700802 /**
803 * Method adapted from com.android.inputmethod.latin.Utils
804 *
805 * @param imm The input method manager
806 * @param shouldIncludeAuxiliarySubtypes
807 * @return true if we have multiple IMEs to choose from
808 */
809 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
810 final boolean shouldIncludeAuxiliarySubtypes) {
811 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
812
813 // Number of the filtered IMEs
814 int filteredImisCount = 0;
815
816 for (InputMethodInfo imi : enabledImis) {
817 // We can return true immediately after we find two or more filtered IMEs.
818 if (filteredImisCount > 1) return true;
819 final List<InputMethodSubtype> subtypes =
820 imm.getEnabledInputMethodSubtypeList(imi, true);
821 // IMEs that have no subtypes should be counted.
822 if (subtypes.isEmpty()) {
823 ++filteredImisCount;
824 continue;
825 }
826
827 int auxCount = 0;
828 for (InputMethodSubtype subtype : subtypes) {
829 if (subtype.isAuxiliary()) {
830 ++auxCount;
831 }
832 }
833 final int nonAuxCount = subtypes.size() - auxCount;
834
835 // IMEs that have one or more non-auxiliary subtypes should be counted.
836 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
837 // subtypes should be counted as well.
838 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
839 ++filteredImisCount;
840 continue;
841 }
842 }
843
844 return filteredImisCount > 1
845 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
846 // input method subtype (The current IME should be LatinIME.)
847 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
848 }
849
Jason parks8fd5bc92011-01-12 16:03:31 -0600850 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700851 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600852 if (service != null) {
853 return IMountService.Stub.asInterface(service);
854 }
855 return null;
856 }
857
858 @Override
859 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500860 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600861 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700862 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600863
Jason parksec5a45e2011-01-18 15:28:36 -0600864 if (TextUtils.isEmpty(password)) {
865 return true;
866 }
Jason parks35933812011-01-21 15:48:20 -0600867
Jason parks8fd5bc92011-01-12 16:03:31 -0600868 // Now that we have the password clear the password field.
869 v.setText(null);
870
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700871 // Disable the password entry and back keypress while checking the password. These
872 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600873 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800874 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600875
Jason parks06c5ff42011-03-01 10:17:40 -0600876 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600877
Jason parks8fd5bc92011-01-12 16:03:31 -0600878 return true;
879 }
880 return false;
881 }
David Brown8373b452011-06-20 12:38:45 -0700882
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700883 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700884 * Set airplane mode on the device if it isn't an LTE device.
885 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
886 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
887 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
888 * both these problems, we turn the radio off. However, on certain networks turning on and
889 * off the radio takes a long time. In such cases, we are better off leaving the radio
890 * running so the latency of an E911 call is short.
891 * The behavior after this is:
892 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
893 * airplane mode and restart the radio.
894 * 2. Full boot: we read the persistent settings from the previous boot and restore the
895 * radio to whatever it was before it restarted. This also happens when rebooting a
896 * phone that has no encryption.
897 */
898 private final void setAirplaneModeIfNecessary() {
899 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700900 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700901 if (!isLteDevice) {
902 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700903 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700904 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
905 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700906 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700907 }
908 }
909
910 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700911 * Code to update the state of, and handle clicks from, the "Emergency call" button.
912 *
913 * This code is mostly duplicated from the corresponding code in
914 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
915 */
David Brown8373b452011-06-20 12:38:45 -0700916 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700917 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700918 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700919 if (emergencyCall == null)
920 return;
David Brown8373b452011-06-20 12:38:45 -0700921
922 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700923 emergencyCall.setVisibility(View.VISIBLE);
924 emergencyCall.setOnClickListener(new View.OnClickListener() {
925 @Override
926
David Brown8373b452011-06-20 12:38:45 -0700927 public void onClick(View v) {
928 takeEmergencyCallAction();
929 }
930 });
931 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700932 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700933 return;
934 }
935
David Brown8373b452011-06-20 12:38:45 -0700936 int textId;
Tyler Gunn3e71b192014-09-10 15:21:33 -0700937 if (getTelecomManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700938 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700939 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700940 } else {
941 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700942 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700943 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700944 }
945
946 private boolean isEmergencyCallCapable() {
947 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
948 }
949
950 private void takeEmergencyCallAction() {
Tyler Gunn3e71b192014-09-10 15:21:33 -0700951 TelecomManager telecomManager = getTelecomManager();
952 if (telecomManager.isInCall()) {
953 telecomManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700954 } else {
955 launchEmergencyDialer();
956 }
957 }
958
David Brown8373b452011-06-20 12:38:45 -0700959
960 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700961 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700962 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
963 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700964 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700965 startActivity(intent);
966 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700967
Santos Cordon3afbdf02014-05-29 21:48:16 -0700968 private TelephonyManager getTelephonyManager() {
969 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
970 }
971
Tyler Gunn3e71b192014-09-10 15:21:33 -0700972 private TelecomManager getTelecomManager() {
973 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700974 }
975
Vikram Aggarwald1147252012-05-08 13:52:30 -0700976 /**
977 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
978 */
979 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700980 mNotificationCountdown = 20;
981 }
982
983 @Override
984 public boolean onKey(View v, int keyCode, KeyEvent event) {
985 delayAudioNotification();
986 return false;
987 }
988
989 @Override
990 public boolean onTouch(View v, MotionEvent event) {
991 delayAudioNotification();
992 return false;
993 }
994
995 @Override
996 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
997 return;
998 }
999
1000 @Override
1001 public void onTextChanged(CharSequence s, int start, int before, int count) {
1002 delayAudioNotification();
1003 }
1004
1005 @Override
1006 public void afterTextChanged(Editable s) {
1007 return;
1008 }
David Brown8373b452011-06-20 12:38:45 -07001009}