blob: 45b4ff134e314fab1295ee13e5ea08f1f11a6b71 [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;
51import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070052import android.view.View.OnKeyListener;
53import android.view.View.OnTouchListener;
Fyodor Kupolov19280af2015-01-27 11:59:00 -080054import android.view.WindowManager;
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
69import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070070
Fyodor Kupolov19280af2015-01-27 11:59:00 -080071import static com.android.internal.widget.LockPatternView.DisplayMode;
72
Ben Komalo91a2f052011-08-16 17:48:25 -070073/**
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";
Paul Crowley51e08112014-11-12 12:06:38 +000091
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070092 /** Message sent to us to indicate encryption update progress. */
93 private static final int MESSAGE_UPDATE_PROGRESS = 1;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070094 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
Paul Crowley51e08112014-11-12 12:06:38 +000095 private static final int MESSAGE_NOTIFY = 2;
Jason parksec5a45e2011-01-18 15:28:36 -060096
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070097 // Constants used to control policy.
Paul Crowley13b8b562014-11-21 09:43:17 +000098 private static final int MAX_FAILED_ATTEMPTS = 30;
Jason parksec5a45e2011-01-18 15:28:36 -060099 private static final int COOL_DOWN_ATTEMPTS = 10;
Jason parksec5a45e2011-01-18 15:28:36 -0600100
David Brown8373b452011-06-20 12:38:45 -0700101 // Intent action for launching the Emergency Dialer activity.
102 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
103
Ben Komalo91a2f052011-08-16 17:48:25 -0700104 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
105 private static final String EXTRA_FORCE_VIEW =
106 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
107 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700108 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700109 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700110
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700111 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700112 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700113 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700114 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
115 private boolean mEncryptionGoneBad;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700116 /** If gone bad, should we show encryption failed (false) or corrupt (true)*/
117 private boolean mCorrupt;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700118 /** A flag to indicate when the back event should be ignored */
Paul Crowley51e08112014-11-12 12:06:38 +0000119 /** When set, blocks unlocking. Set every COOL_DOWN_ATTEMPTS attempts, only cleared
120 by power cycling phone. */
121 private boolean mCooldown = false;
122
Andy Stadler14997402011-02-01 15:34:59 -0800123 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 Crowley2555e5f2014-11-07 00:52:57 -0800138 // When the user enters a short pin/password, run this to show an error,
139 // but don't count it against attempts.
140 private final Runnable mFakeUnlockAttemptRunnable = new Runnable() {
Paul Crowley51e08112014-11-12 12:06:38 +0000141 @Override
Paul Crowley2555e5f2014-11-07 00:52:57 -0800142 public void run() {
143 handleBadAttempt(1 /* failedAttempt */);
144 }
145 };
146
147 // TODO: this should be tuned to match minimum decryption timeout
148 private static final int FAKE_ATTEMPT_DELAY = 1000;
149
150 private final Runnable mClearPatternRunnable = new Runnable() {
Paul Crowley529834d2014-10-21 10:12:47 +0100151 @Override
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700152 public void run() {
153 mLockPatternView.clearPattern();
154 }
155 };
156
Andy Stadler14997402011-02-01 15:34:59 -0800157 /**
158 * Used to propagate state through configuration changes (e.g. screen rotation)
159 */
160 private static class NonConfigurationInstanceState {
161 final PowerManager.WakeLock wakelock;
162
163 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
164 wakelock = _wakelock;
165 }
166 }
167
Jason parks06c5ff42011-03-01 10:17:40 -0600168 private class DecryptTask extends AsyncTask<String, Void, Integer> {
Paul Lawrenceced7db42014-09-18 11:23:40 -0700169 private void hide(int id) {
170 View view = findViewById(id);
171 if (view != null) {
172 view.setVisibility(View.GONE);
173 }
174 }
175
Jason parks06c5ff42011-03-01 10:17:40 -0600176 @Override
Paul Crowley529834d2014-10-21 10:12:47 +0100177 protected void onPreExecute() {
178 super.onPreExecute();
Paul Crowley2555e5f2014-11-07 00:52:57 -0800179 beginAttempt();
Paul Crowley529834d2014-10-21 10:12:47 +0100180 }
181
182 @Override
Jason parks06c5ff42011-03-01 10:17:40 -0600183 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700184 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600185 try {
186 return service.decryptStorage(params[0]);
187 } catch (Exception e) {
188 Log.e(TAG, "Error while decrypting...", e);
189 return -1;
190 }
191 }
192
193 @Override
194 protected void onPostExecute(Integer failedAttempts) {
195 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700196 // The password was entered successfully. Simply do nothing
197 // and wait for the service restart to switch to surfacefligner
Paul Lawrence0f11e152014-08-20 07:43:32 -0700198 if (mLockPatternView != null) {
199 mLockPatternView.removeCallbacks(mClearPatternRunnable);
200 mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
201 }
Paul Crowley529834d2014-10-21 10:12:47 +0100202 final TextView status = (TextView) findViewById(R.id.status);
203 status.setText(R.string.starting_android);
Paul Lawrenceced7db42014-09-18 11:23:40 -0700204 hide(R.id.passwordEntry);
205 hide(R.id.switch_ime_button);
206 hide(R.id.lockPattern);
Paul Lawrenceced7db42014-09-18 11:23:40 -0700207 hide(R.id.owner_info);
208 hide(R.id.emergencyCallButton);
Jason parks06c5ff42011-03-01 10:17:40 -0600209 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
210 // Factory reset the device.
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700211 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
212 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
213 intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
214 sendBroadcast(intent);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700215 } else if (failedAttempts == -1) {
216 // Right password, but decryption failed. Tell user bad news ...
217 setContentView(R.layout.crypt_keeper_progress);
218 showFactoryReset(true);
219 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600220 } else {
Paul Crowley2555e5f2014-11-07 00:52:57 -0800221 handleBadAttempt(failedAttempts);
222 }
223 }
224 }
225
226 private void beginAttempt() {
227 final TextView status = (TextView) findViewById(R.id.status);
228 status.setText(R.string.checking_decryption);
229 }
230
231 private void handleBadAttempt(Integer failedAttempts) {
232 // Wrong entry. Handle pattern case.
233 if (mLockPatternView != null) {
234 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
235 mLockPatternView.removeCallbacks(mClearPatternRunnable);
236 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
237 }
238 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Paul Crowley51e08112014-11-12 12:06:38 +0000239 mCooldown = true;
240 // No need to setBackFunctionality(false) - it's already done
241 // at this point.
Paul Crowley2555e5f2014-11-07 00:52:57 -0800242 cooldown();
243 } else {
244 final TextView status = (TextView) findViewById(R.id.status);
245
246 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
247 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
248 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
249 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
250 Integer.toString(remainingAttempts));
251 status.setText(warning);
252 } else {
253 int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
254 try {
255 final IMountService service = getMountService();
256 passwordType = service.getPasswordType();
257 } catch (Exception e) {
258 Log.e(TAG, "Error calling mount service " + e);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700259 }
Paul Crowley2555e5f2014-11-07 00:52:57 -0800260
261 if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
262 status.setText(R.string.cryptkeeper_wrong_pin);
263 } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
264 status.setText(R.string.cryptkeeper_wrong_pattern);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700265 } else {
Paul Crowley2555e5f2014-11-07 00:52:57 -0800266 status.setText(R.string.cryptkeeper_wrong_password);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700267 }
Jason parks06c5ff42011-03-01 10:17:40 -0600268 }
Paul Crowley2555e5f2014-11-07 00:52:57 -0800269
270 if (mLockPatternView != null) {
271 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
272 mLockPatternView.setEnabled(true);
273 }
274
275 // Reenable the password entry
276 if (mPasswordEntry != null) {
277 mPasswordEntry.setEnabled(true);
278 final InputMethodManager imm = (InputMethodManager) getSystemService(
279 Context.INPUT_METHOD_SERVICE);
280 imm.showSoftInput(mPasswordEntry, 0);
281 setBackFunctionality(true);
282 }
Jason parks06c5ff42011-03-01 10:17:40 -0600283 }
284 }
Jason parks75c085e2011-02-10 14:03:47 -0600285
Ben Komalo0e666092011-09-01 15:42:00 -0700286 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
Paul Lawrence87abbd32014-08-28 15:56:38 -0700287 int state;
288
Ben Komalo0e666092011-09-01 15:42:00 -0700289 @Override
290 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700291 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700292 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700293 Log.d(TAG, "Validating encryption state.");
Paul Lawrence87abbd32014-08-28 15:56:38 -0700294 state = service.getEncryptionState();
Ben Komalo0e666092011-09-01 15:42:00 -0700295 if (state == IMountService.ENCRYPTION_STATE_NONE) {
296 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
297 return true; // Unexpected, but fine, I guess...
298 }
299 return state == IMountService.ENCRYPTION_STATE_OK;
300 } catch (RemoteException e) {
301 Log.w(TAG, "Unable to get encryption state properly");
302 return true;
303 }
304 }
305
306 @Override
307 protected void onPostExecute(Boolean result) {
308 mValidationComplete = true;
309 if (Boolean.FALSE.equals(result)) {
310 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
311 mEncryptionGoneBad = true;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700312 mCorrupt = state == IMountService.ENCRYPTION_STATE_ERROR_CORRUPT;
Ben Komalod4758ef2011-09-08 14:36:08 -0700313 } else {
314 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700315 }
316 setupUi();
317 }
318 }
319
Ben Komalo91a2f052011-08-16 17:48:25 -0700320 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600321 @Override
322 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600323 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700324 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600325 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600326 break;
Jason parks35933812011-01-21 15:48:20 -0600327
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700328 case MESSAGE_NOTIFY:
329 notifyUser();
330 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600331 }
332 }
333 };
Jason parks35933812011-01-21 15:48:20 -0600334
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700335 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800336 /** The status bar where back/home/recent buttons are shown. */
337 private StatusBarManager mStatusBar;
338
339 /** All the widgets to disable in the status bar */
340 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
341 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
342 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
343 | StatusBarManager.DISABLE_SYSTEM_INFO
344 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700345 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800346 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700347
Paul Crowley2555e5f2014-11-07 00:52:57 -0800348 protected static final int MIN_LENGTH_BEFORE_REPORT = LockPatternUtils.MIN_LOCK_PATTERN_SIZE;
349
Ben Komalo91a2f052011-08-16 17:48:25 -0700350 /** @return whether or not this Activity was started for debugging the UI only. */
351 private boolean isDebugView() {
352 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
353 }
354
355 /** @return whether or not this Activity was started for debugging the specific UI view only. */
356 private boolean isDebugView(String viewType /* non-nullable */) {
357 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
358 }
359
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700360 /**
361 * Notify the user that we are awaiting input. Currently this sends an audio alert.
362 */
363 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700364 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700365 --mNotificationCountdown;
366 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700367 try {
368 // Play the standard keypress sound at full volume. This should be available on
369 // every device. We cannot play a ringtone here because media services aren't
370 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
371 // on tablet devices. The idea is to alert the user that something is needed: this
372 // does not have to be pleasing.
373 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
374 } catch (Exception e) {
375 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
376 }
377 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700378 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700379 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700380 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700381
382 if (mWakeLock.isHeld()) {
383 if (mReleaseWakeLockCountdown > 0) {
384 --mReleaseWakeLockCountdown;
385 } else {
386 mWakeLock.release();
387 }
388 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700389 }
390
391 /**
Paul Lawrence2a6552e2015-05-14 15:49:08 -0700392 * Ignore back events from this activity always - there's nowhere to go back
393 * to
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700394 */
395 @Override
396 public void onBackPressed() {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700397 }
398
Jason parks8fd5bc92011-01-12 16:03:31 -0600399 @Override
400 public void onCreate(Bundle savedInstanceState) {
401 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600402
Andy Stadler95974062011-02-01 17:35:20 -0800403 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700404 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700405 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Fyodor Kupolov1b5cc422014-12-08 16:13:11 -0800406 disableCryptKeeperComponent(this);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700407 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700408 // want to be running, so need to finish this activity. We can count
409 // on the activity manager re-launching the new home app upon finishing
410 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700411 // NOTE: This is really grungy. I think it would be better for the
412 // activity manager to explicitly launch the crypt keeper instead of
413 // home in the situation where we need to decrypt the device
414 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600415 return;
416 }
Jason parks35933812011-01-21 15:48:20 -0600417
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700418 try {
419 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
420 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
421 }
422 } catch (NotFoundException e) {
423 }
424
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700425 // Disable the status bar, but do NOT disable back because the user needs a way to go
426 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800427 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
428 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800429
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700430 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700431 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800432 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700433 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800434 if (lastInstance instanceof NonConfigurationInstanceState) {
435 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
436 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700437 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800438 }
Jason parksec5a45e2011-01-18 15:28:36 -0600439 }
Jason parks35933812011-01-21 15:48:20 -0600440
Andy Stadler95974062011-02-01 17:35:20 -0800441 /**
442 * Note, we defer the state check and screen setup to onStart() because this will be
443 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
444 * especially important if we were to lose the wakelock for any reason.
445 */
446 @Override
447 public void onStart() {
448 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700449 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700450 }
451
452 /**
453 * Initializes the UI based on the current state of encryption.
454 * This is idempotent - calling repeatedly will simply re-initialize the UI.
455 */
456 private void setupUi() {
457 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
458 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence87abbd32014-08-28 15:56:38 -0700459 showFactoryReset(mCorrupt);
Ben Komalo0e666092011-09-01 15:42:00 -0700460 return;
461 }
462
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700463 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700464 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800465 setContentView(R.layout.crypt_keeper_progress);
466 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700467 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700468 new AsyncTask<Void, Void, Void>() {
Paul Crowley529834d2014-10-21 10:12:47 +0100469 int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700470 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700471 boolean pattern_visible;
Paul Lawrencec04420c2015-05-18 13:25:01 -0700472 boolean password_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800473
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700474 @Override
475 public Void doInBackground(Void... v) {
476 try {
477 final IMountService service = getMountService();
Paul Crowley529834d2014-10-21 10:12:47 +0100478 passwordType = service.getPasswordType();
Elliott Hughesf9d6d292014-10-02 13:29:58 -0700479 owner_info = service.getField(StorageManager.OWNER_INFO_KEY);
480 pattern_visible = !("0".equals(service.getField(StorageManager.PATTERN_VISIBLE_KEY)));
Paul Lawrencec04420c2015-05-18 13:25:01 -0700481 password_visible = !("0".equals(service.getField(StorageManager.PASSWORD_VISIBLE_KEY)));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700482 } catch (Exception e) {
483 Log.e(TAG, "Error calling mount service " + e);
484 }
485
486 return null;
487 }
488
489 @Override
490 public void onPostExecute(java.lang.Void v) {
Paul Lawrencec04420c2015-05-18 13:25:01 -0700491 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
492 password_visible ? 1 : 0);
493
Paul Crowley51e08112014-11-12 12:06:38 +0000494 if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700495 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700496 mStatusString = R.string.enter_pin;
Paul Crowley529834d2014-10-21 10:12:47 +0100497 } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700498 setContentView(R.layout.crypt_keeper_pattern_entry);
499 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700500 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700501 } else {
502 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700503 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700504 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700505 final TextView status = (TextView) findViewById(R.id.status);
506 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700507
Paul Lawrence5a70f052014-06-13 11:09:55 -0700508 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
509 ownerInfo.setText(owner_info);
510 ownerInfo.setSelected(true); // Required for marquee'ing to work
511
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700512 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700513
Paul Lawrence2a6552e2015-05-14 15:49:08 -0700514 findViewById(android.R.id.content).setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
515
Paul Lawrence0f11e152014-08-20 07:43:32 -0700516 if (mLockPatternView != null) {
517 mLockPatternView.setInStealthMode(!pattern_visible);
518 }
Paul Crowley51e08112014-11-12 12:06:38 +0000519 if (mCooldown) {
520 // in case we are cooling down and coming back from emergency dialler
Paul Lawrence5a70f052014-06-13 11:09:55 -0700521 setBackFunctionality(false);
Paul Crowley51e08112014-11-12 12:06:38 +0000522 cooldown();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700523 }
Paul Crowley51e08112014-11-12 12:06:38 +0000524
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700525 }
526 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700527 } else if (!mValidationRequested) {
528 // We're supposed to be encrypted, but no validation has been done.
529 new ValidationTask().execute((Void[]) null);
530 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800531 }
532 }
533
Jason parksf8217302011-01-26 13:11:42 -0600534 @Override
535 public void onStop() {
536 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700537 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
538 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800539 }
540
541 /**
542 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
543 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
544 * mWakeLock so the subsequent call to onDestroy does not release it.
545 */
546 @Override
547 public Object onRetainNonConfigurationInstance() {
548 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700549 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800550 mWakeLock = null;
551 return state;
552 }
553
554 @Override
555 public void onDestroy() {
556 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600557
558 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700559 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600560 mWakeLock.release();
561 mWakeLock = null;
562 }
563 }
564
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700565 /**
566 * Start encrypting the device.
567 */
Jason parksec5a45e2011-01-18 15:28:36 -0600568 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600569 // Accquire a partial wakelock to prevent the device from sleeping. Note
570 // we never release this wakelock as we will be restarted after the device
571 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700572 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700573 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700574 Log.d(TAG, "Acquiring wakelock.");
575 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
576 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
577 mWakeLock.acquire();
578 }
Jason parks35933812011-01-21 15:48:20 -0600579
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700580 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700581 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800582 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700583 // Start the first run of progress manually. This method sets up messages to occur at
584 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600585 updateProgress();
586 }
587
Paul Lawrence384d8e52014-08-08 15:11:36 -0700588 /**
589 * Show factory reset screen allowing the user to reset their phone when
590 * there is nothing else we can do
591 * @param corrupt true if userdata is corrupt, false if encryption failed
592 * partway through
593 */
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700594 private void showFactoryReset(final boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800595 // Hide the encryption-bot to make room for the "factory reset" button
596 findViewById(R.id.encroid).setVisibility(View.GONE);
597
598 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700599 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800600 button.setVisibility(View.VISIBLE);
601 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700602 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800603 public void onClick(View v) {
604 // Factory reset the device.
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700605 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
606 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
607 intent.putExtra(Intent.EXTRA_REASON,
608 "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
609 sendBroadcast(intent);
Andy Stadler13d62042011-01-31 19:21:37 -0800610 }
611 });
612
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700613 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700614 if (corrupt) {
615 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
616 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
617 } else {
618 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
619 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
620 }
Andy Stadler13d62042011-01-31 19:21:37 -0800621
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700622 final View view = findViewById(R.id.bottom_divider);
623 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700624 if (view != null) {
625 view.setVisibility(View.VISIBLE);
626 }
Andy Stadler13d62042011-01-31 19:21:37 -0800627 }
628
Jason parksf8217302011-01-26 13:11:42 -0600629 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700630 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600631
Ben Komalo0e666092011-09-01 15:42:00 -0700632 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700633 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800634 return;
635 }
636
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700637 // Get status as percentage first
638 CharSequence status = getText(R.string.crypt_keeper_setup_description);
639 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600640 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700641 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700642 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600643 } catch (Exception e) {
644 Log.w(TAG, "Error parsing progress: " + e.toString());
645 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700646 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600647
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700648 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700649 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700650 try {
651 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
652 int time = Integer.parseInt(timeProperty);
653 if (time >= 0) {
654 // Round up to multiple of 10 - this way display is less jerky
655 time = (time + 9) / 10 * 10;
656 progress = DateUtils.formatElapsedTime(time);
657 status = getText(R.string.crypt_keeper_setup_time_remaining);
658 }
659 } catch (Exception e) {
660 // Will happen if no time etc - show percentage
661 }
662
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700663 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700664 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700665 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700666 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700667
668 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700669 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700670 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600671 }
672
Paul Crowley51e08112014-11-12 12:06:38 +0000673 /** Insist on a power cycle to force the user to waste time between retries.
674 *
675 * Call setBackFunctionality(false) before calling this. */
Jason parksf8217302011-01-26 13:11:42 -0600676 private void cooldown() {
Paul Crowley51e08112014-11-12 12:06:38 +0000677 // Disable the password entry.
678 if (mPasswordEntry != null) {
679 mPasswordEntry.setEnabled(false);
Jason parksf8217302011-01-26 13:11:42 -0600680 }
Paul Crowley51e08112014-11-12 12:06:38 +0000681 if (mLockPatternView != null) {
682 mLockPatternView.setEnabled(false);
683 }
684
685 final TextView status = (TextView) findViewById(R.id.status);
686 status.setText(R.string.crypt_keeper_force_power_cycle);
Jason parksec5a45e2011-01-18 15:28:36 -0600687 }
Jason parks35933812011-01-21 15:48:20 -0600688
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800689 /**
690 * Sets the back status: enabled or disabled according to the parameter.
691 * @param isEnabled true if back is enabled, false otherwise.
692 */
693 private final void setBackFunctionality(boolean isEnabled) {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800694 if (isEnabled) {
695 mStatusBar.disable(sWidgetsToDisable);
696 } else {
697 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
698 }
699 }
700
Paul Crowley2555e5f2014-11-07 00:52:57 -0800701 private void fakeUnlockAttempt(View postingView) {
702 beginAttempt();
703 postingView.postDelayed(mFakeUnlockAttemptRunnable, FAKE_ATTEMPT_DELAY);
704 }
705
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800706 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700707 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800708
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700709 @Override
710 public void onPatternStart() {
711 mLockPatternView.removeCallbacks(mClearPatternRunnable);
712 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800713
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700714 @Override
715 public void onPatternCleared() {
716 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800717
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700718 @Override
719 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
720 mLockPatternView.setEnabled(false);
Paul Crowley2555e5f2014-11-07 00:52:57 -0800721 if (pattern.size() >= MIN_LENGTH_BEFORE_REPORT) {
722 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
723 } else {
724 // Allow user to make as many of these as they want.
725 fakeUnlockAttempt(mLockPatternView);
726 }
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700727 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800728
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700729 @Override
730 public void onPatternCellAdded(List<Cell> pattern) {
731 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800732 };
733
734 private void passwordEntryInit() {
735 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600736 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800737 if (mPasswordEntry != null){
738 mPasswordEntry.setOnEditorActionListener(this);
739 mPasswordEntry.requestFocus();
740 // Become quiet when the user interacts with the Edit text screen.
741 mPasswordEntry.setOnKeyListener(this);
742 mPasswordEntry.setOnTouchListener(this);
743 mPasswordEntry.addTextChangedListener(this);
744 }
745
746 // Pattern case
747 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
748 if (mLockPatternView != null) {
749 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
750 }
Jason parks35933812011-01-21 15:48:20 -0600751
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700752 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700753 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700754 final View emergencyCall = findViewById(R.id.emergencyCallButton);
755 if (emergencyCall != null) {
756 Log.d(TAG, "Removing the emergency Call button");
757 emergencyCall.setVisibility(View.GONE);
758 }
759 }
760
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700761 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700762 final InputMethodManager imm = (InputMethodManager) getSystemService(
763 Context.INPUT_METHOD_SERVICE);
764 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
765 imeSwitcher.setVisibility(View.VISIBLE);
766 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700767 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700768 public void onClick(View v) {
Seigo Nonakae7927292015-05-04 17:31:00 -0700769 imm.showInputMethodPicker(false /* showAuxiliarySubtypes */);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700770 }
771 });
Jason parks00046d62011-06-13 17:38:45 -0500772 }
David Brown8373b452011-06-20 12:38:45 -0700773
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700774 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
775 // is completely non-functional, and we want the user to notice the device and enter a
776 // password.
777 if (mWakeLock == null) {
778 Log.d(TAG, "Acquiring wakelock.");
779 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
780 if (pm != null) {
781 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
782 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700783 // Keep awake for 10 minutes - if the user hasn't been alerted by then
784 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700785 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700786 }
787 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700788
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700789 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
790 // immediately.
Paul Crowley51e08112014-11-12 12:06:38 +0000791 if (mLockPatternView == null && !mCooldown) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700792 mHandler.postDelayed(new Runnable() {
793 @Override public void run() {
794 imm.showSoftInputUnchecked(0, null);
795 }
796 }, 0);
797 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700798
David Brown8373b452011-06-20 12:38:45 -0700799 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700800 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700801 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700802 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800803
John Spurlock2c526512014-09-17 12:55:17 -0400804 // Dismiss secure & non-secure keyguards while this screen is showing.
805 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
806 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Jason parks8fd5bc92011-01-12 16:03:31 -0600807 }
808
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700809 /**
810 * Method adapted from com.android.inputmethod.latin.Utils
811 *
812 * @param imm The input method manager
813 * @param shouldIncludeAuxiliarySubtypes
814 * @return true if we have multiple IMEs to choose from
815 */
816 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
817 final boolean shouldIncludeAuxiliarySubtypes) {
818 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
819
820 // Number of the filtered IMEs
821 int filteredImisCount = 0;
822
823 for (InputMethodInfo imi : enabledImis) {
824 // We can return true immediately after we find two or more filtered IMEs.
825 if (filteredImisCount > 1) return true;
826 final List<InputMethodSubtype> subtypes =
827 imm.getEnabledInputMethodSubtypeList(imi, true);
828 // IMEs that have no subtypes should be counted.
829 if (subtypes.isEmpty()) {
830 ++filteredImisCount;
831 continue;
832 }
833
834 int auxCount = 0;
835 for (InputMethodSubtype subtype : subtypes) {
836 if (subtype.isAuxiliary()) {
837 ++auxCount;
838 }
839 }
840 final int nonAuxCount = subtypes.size() - auxCount;
841
842 // IMEs that have one or more non-auxiliary subtypes should be counted.
843 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
844 // subtypes should be counted as well.
845 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
846 ++filteredImisCount;
847 continue;
848 }
849 }
850
851 return filteredImisCount > 1
852 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
853 // input method subtype (The current IME should be LatinIME.)
854 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
855 }
856
Jason parks8fd5bc92011-01-12 16:03:31 -0600857 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700858 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600859 if (service != null) {
860 return IMountService.Stub.asInterface(service);
861 }
862 return null;
863 }
864
865 @Override
866 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500867 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600868 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700869 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600870
Jason parksec5a45e2011-01-18 15:28:36 -0600871 if (TextUtils.isEmpty(password)) {
872 return true;
873 }
Jason parks35933812011-01-21 15:48:20 -0600874
Jason parks8fd5bc92011-01-12 16:03:31 -0600875 // Now that we have the password clear the password field.
876 v.setText(null);
877
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700878 // Disable the password entry and back keypress while checking the password. These
879 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600880 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800881 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600882
Paul Crowley2555e5f2014-11-07 00:52:57 -0800883 if (password.length() >= LockPatternUtils.MIN_LOCK_PATTERN_SIZE) {
884 new DecryptTask().execute(password);
885 } else {
886 // Allow user to make as many of these as they want.
887 fakeUnlockAttempt(mPasswordEntry);
888 }
Jason parks35933812011-01-21 15:48:20 -0600889
Jason parks8fd5bc92011-01-12 16:03:31 -0600890 return true;
891 }
892 return false;
893 }
David Brown8373b452011-06-20 12:38:45 -0700894
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700895 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700896 * Set airplane mode on the device if it isn't an LTE device.
897 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
898 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
899 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
900 * both these problems, we turn the radio off. However, on certain networks turning on and
901 * off the radio takes a long time. In such cases, we are better off leaving the radio
902 * running so the latency of an E911 call is short.
903 * The behavior after this is:
904 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
905 * airplane mode and restart the radio.
906 * 2. Full boot: we read the persistent settings from the previous boot and restore the
907 * radio to whatever it was before it restarted. This also happens when rebooting a
908 * phone that has no encryption.
909 */
910 private final void setAirplaneModeIfNecessary() {
911 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700912 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700913 if (!isLteDevice) {
914 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700915 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700916 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
917 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700918 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700919 }
920 }
921
922 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700923 * Code to update the state of, and handle clicks from, the "Emergency call" button.
924 *
925 * This code is mostly duplicated from the corresponding code in
926 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
927 */
David Brown8373b452011-06-20 12:38:45 -0700928 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700929 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700930 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700931 if (emergencyCall == null)
932 return;
David Brown8373b452011-06-20 12:38:45 -0700933
934 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700935 emergencyCall.setVisibility(View.VISIBLE);
936 emergencyCall.setOnClickListener(new View.OnClickListener() {
937 @Override
938
David Brown8373b452011-06-20 12:38:45 -0700939 public void onClick(View v) {
940 takeEmergencyCallAction();
941 }
942 });
943 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700944 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700945 return;
946 }
947
David Brown8373b452011-06-20 12:38:45 -0700948 int textId;
Tyler Gunn3e71b192014-09-10 15:21:33 -0700949 if (getTelecomManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700950 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700951 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700952 } else {
953 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700954 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700955 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700956 }
957
958 private boolean isEmergencyCallCapable() {
959 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
960 }
961
962 private void takeEmergencyCallAction() {
Tyler Gunn3e71b192014-09-10 15:21:33 -0700963 TelecomManager telecomManager = getTelecomManager();
964 if (telecomManager.isInCall()) {
965 telecomManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700966 } else {
967 launchEmergencyDialer();
968 }
969 }
970
David Brown8373b452011-06-20 12:38:45 -0700971
972 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700973 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700974 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
975 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700976 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700977 startActivity(intent);
978 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700979
Santos Cordon3afbdf02014-05-29 21:48:16 -0700980 private TelephonyManager getTelephonyManager() {
981 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
982 }
983
Tyler Gunn3e71b192014-09-10 15:21:33 -0700984 private TelecomManager getTelecomManager() {
985 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700986 }
987
Vikram Aggarwald1147252012-05-08 13:52:30 -0700988 /**
989 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
990 */
991 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700992 mNotificationCountdown = 20;
993 }
994
995 @Override
996 public boolean onKey(View v, int keyCode, KeyEvent event) {
997 delayAudioNotification();
998 return false;
999 }
1000
1001 @Override
1002 public boolean onTouch(View v, MotionEvent event) {
1003 delayAudioNotification();
1004 return false;
1005 }
1006
1007 @Override
1008 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
1009 return;
1010 }
1011
1012 @Override
1013 public void onTextChanged(CharSequence s, int start, int before, int count) {
1014 delayAudioNotification();
1015 }
1016
1017 @Override
1018 public void afterTextChanged(Editable s) {
1019 return;
1020 }
Fyodor Kupolov1b5cc422014-12-08 16:13:11 -08001021
1022 private static void disableCryptKeeperComponent(Context context) {
1023 PackageManager pm = context.getPackageManager();
1024 ComponentName name = new ComponentName(context, CryptKeeper.class);
1025 Log.d(TAG, "Disabling component " + name);
Paul Lawrenceae4a5572015-09-19 02:29:37 +00001026 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
1027 PackageManager.DONT_KILL_APP);
Fyodor Kupolov1b5cc422014-12-08 16:13:11 -08001028 }
David Brown8373b452011-06-20 12:38:45 -07001029}