blob: 94ea2e736faa7e6450b4c0e5d2daccbcd22b03f3 [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;
Sudheer Shankaee2d5922016-11-09 15:47:53 -080038import android.os.storage.IStorageManager;
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;
Jason Monk39b46742015-09-10 15:52:51 -040068import com.android.internal.widget.LockPatternView.DisplayMode;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070069
70import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070071
72/**
73 * Settings screens to show the UI flows for encrypting/decrypting the device.
74 *
75 * This may be started via adb for debugging the UI layout, without having to go through
76 * encryption flows everytime. It should be noted that starting the activity in this manner
77 * is only useful for verifying UI-correctness - the behavior will not be identical.
78 * <pre>
79 * $ adb shell pm enable com.android.settings/.CryptKeeper
80 * $ adb shell am start \
81 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
82 * -n com.android.settings/.CryptKeeper
83 * </pre>
84 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070085public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
86 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060087 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060088
Jason parks8fd5bc92011-01-12 16:03:31 -060089 private static final String DECRYPT_STATE = "trigger_restart_framework";
Paul Crowley51e08112014-11-12 12:06:38 +000090
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;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070093 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
Paul Crowley51e08112014-11-12 12:06:38 +000094 private static final int MESSAGE_NOTIFY = 2;
Jason parksec5a45e2011-01-18 15:28:36 -060095
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070096 // Constants used to control policy.
Paul Crowley13b8b562014-11-21 09:43:17 +000097 private static final int MAX_FAILED_ATTEMPTS = 30;
Jason parksec5a45e2011-01-18 15:28:36 -060098 private static final int COOL_DOWN_ATTEMPTS = 10;
Jason parksec5a45e2011-01-18 15:28:36 -060099
David Brown8373b452011-06-20 12:38:45 -0700100 // Intent action for launching the Emergency Dialer activity.
101 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
102
Ben Komalo91a2f052011-08-16 17:48:25 -0700103 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
104 private static final String EXTRA_FORCE_VIEW =
105 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
106 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700107 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700108 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700109
Paul Lawrence27841042015-11-24 12:42:09 -0800110 private static final String STATE_COOLDOWN = "cooldown";
111
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700112 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700113 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700114 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700115 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
116 private boolean mEncryptionGoneBad;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700117 /** If gone bad, should we show encryption failed (false) or corrupt (true)*/
118 private boolean mCorrupt;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700119 /** A flag to indicate when the back event should be ignored */
Paul Crowley51e08112014-11-12 12:06:38 +0000120 /** When set, blocks unlocking. Set every COOL_DOWN_ATTEMPTS attempts, only cleared
121 by power cycling phone. */
122 private boolean mCooldown = false;
123
Andy Stadler14997402011-02-01 15:34:59 -0800124 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600125 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800126 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700127 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
128 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700129 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
130 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700131 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800132
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700133 // how long we wait to clear a wrong pattern
134 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
135
Paul Lawrence0f11e152014-08-20 07:43:32 -0700136 // how long we wait to clear a right pattern
137 private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
138
Paul Crowley2555e5f2014-11-07 00:52:57 -0800139 // When the user enters a short pin/password, run this to show an error,
140 // but don't count it against attempts.
141 private final Runnable mFakeUnlockAttemptRunnable = new Runnable() {
Paul Crowley51e08112014-11-12 12:06:38 +0000142 @Override
Paul Crowley2555e5f2014-11-07 00:52:57 -0800143 public void run() {
144 handleBadAttempt(1 /* failedAttempt */);
145 }
146 };
147
148 // TODO: this should be tuned to match minimum decryption timeout
149 private static final int FAKE_ATTEMPT_DELAY = 1000;
150
151 private final Runnable mClearPatternRunnable = new Runnable() {
Paul Crowley529834d2014-10-21 10:12:47 +0100152 @Override
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700153 public void run() {
154 mLockPatternView.clearPattern();
155 }
156 };
157
Andy Stadler14997402011-02-01 15:34:59 -0800158 /**
159 * Used to propagate state through configuration changes (e.g. screen rotation)
160 */
161 private static class NonConfigurationInstanceState {
162 final PowerManager.WakeLock wakelock;
163
164 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
165 wakelock = _wakelock;
166 }
167 }
168
Jason parks06c5ff42011-03-01 10:17:40 -0600169 private class DecryptTask extends AsyncTask<String, Void, Integer> {
Paul Lawrenceced7db42014-09-18 11:23:40 -0700170 private void hide(int id) {
171 View view = findViewById(id);
172 if (view != null) {
173 view.setVisibility(View.GONE);
174 }
175 }
176
Jason parks06c5ff42011-03-01 10:17:40 -0600177 @Override
Paul Crowley529834d2014-10-21 10:12:47 +0100178 protected void onPreExecute() {
179 super.onPreExecute();
Paul Crowley2555e5f2014-11-07 00:52:57 -0800180 beginAttempt();
Paul Crowley529834d2014-10-21 10:12:47 +0100181 }
182
183 @Override
Jason parks06c5ff42011-03-01 10:17:40 -0600184 protected Integer doInBackground(String... params) {
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800185 final IStorageManager service = getStorageManager();
Jason parks06c5ff42011-03-01 10:17:40 -0600186 try {
187 return service.decryptStorage(params[0]);
188 } catch (Exception e) {
189 Log.e(TAG, "Error while decrypting...", e);
190 return -1;
191 }
192 }
193
194 @Override
195 protected void onPostExecute(Integer failedAttempts) {
196 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700197 // The password was entered successfully. Simply do nothing
198 // and wait for the service restart to switch to surfacefligner
Paul Lawrence0f11e152014-08-20 07:43:32 -0700199 if (mLockPatternView != null) {
200 mLockPatternView.removeCallbacks(mClearPatternRunnable);
201 mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
202 }
Paul Crowley529834d2014-10-21 10:12:47 +0100203 final TextView status = (TextView) findViewById(R.id.status);
204 status.setText(R.string.starting_android);
Paul Lawrenceced7db42014-09-18 11:23:40 -0700205 hide(R.id.passwordEntry);
206 hide(R.id.switch_ime_button);
207 hide(R.id.lockPattern);
Paul Lawrenceced7db42014-09-18 11:23:40 -0700208 hide(R.id.owner_info);
209 hide(R.id.emergencyCallButton);
Jason parks06c5ff42011-03-01 10:17:40 -0600210 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
211 // Factory reset the device.
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100212 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800213 intent.setPackage("android");
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700214 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
215 intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
216 sendBroadcast(intent);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700217 } else if (failedAttempts == -1) {
218 // Right password, but decryption failed. Tell user bad news ...
219 setContentView(R.layout.crypt_keeper_progress);
220 showFactoryReset(true);
221 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600222 } else {
Paul Crowley2555e5f2014-11-07 00:52:57 -0800223 handleBadAttempt(failedAttempts);
224 }
225 }
226 }
227
228 private void beginAttempt() {
229 final TextView status = (TextView) findViewById(R.id.status);
230 status.setText(R.string.checking_decryption);
231 }
232
233 private void handleBadAttempt(Integer failedAttempts) {
234 // Wrong entry. Handle pattern case.
235 if (mLockPatternView != null) {
236 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
237 mLockPatternView.removeCallbacks(mClearPatternRunnable);
238 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
239 }
240 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Paul Crowley51e08112014-11-12 12:06:38 +0000241 mCooldown = true;
242 // No need to setBackFunctionality(false) - it's already done
243 // at this point.
Paul Crowley2555e5f2014-11-07 00:52:57 -0800244 cooldown();
245 } else {
246 final TextView status = (TextView) findViewById(R.id.status);
247
248 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
249 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
250 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
251 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
252 Integer.toString(remainingAttempts));
253 status.setText(warning);
254 } else {
255 int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
256 try {
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800257 final IStorageManager service = getStorageManager();
Paul Crowley2555e5f2014-11-07 00:52:57 -0800258 passwordType = service.getPasswordType();
259 } catch (Exception e) {
260 Log.e(TAG, "Error calling mount service " + e);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700261 }
Paul Crowley2555e5f2014-11-07 00:52:57 -0800262
263 if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
264 status.setText(R.string.cryptkeeper_wrong_pin);
265 } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
266 status.setText(R.string.cryptkeeper_wrong_pattern);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700267 } else {
Paul Crowley2555e5f2014-11-07 00:52:57 -0800268 status.setText(R.string.cryptkeeper_wrong_password);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700269 }
Jason parks06c5ff42011-03-01 10:17:40 -0600270 }
Paul Crowley2555e5f2014-11-07 00:52:57 -0800271
272 if (mLockPatternView != null) {
273 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
274 mLockPatternView.setEnabled(true);
275 }
276
277 // Reenable the password entry
278 if (mPasswordEntry != null) {
279 mPasswordEntry.setEnabled(true);
280 final InputMethodManager imm = (InputMethodManager) getSystemService(
281 Context.INPUT_METHOD_SERVICE);
282 imm.showSoftInput(mPasswordEntry, 0);
283 setBackFunctionality(true);
284 }
Jason parks06c5ff42011-03-01 10:17:40 -0600285 }
286 }
Jason parks75c085e2011-02-10 14:03:47 -0600287
Ben Komalo0e666092011-09-01 15:42:00 -0700288 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
Paul Lawrence87abbd32014-08-28 15:56:38 -0700289 int state;
290
Ben Komalo0e666092011-09-01 15:42:00 -0700291 @Override
292 protected Boolean doInBackground(Void... params) {
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800293 final IStorageManager service = getStorageManager();
Ben Komalo0e666092011-09-01 15:42:00 -0700294 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700295 Log.d(TAG, "Validating encryption state.");
Paul Lawrence87abbd32014-08-28 15:56:38 -0700296 state = service.getEncryptionState();
Sudheer Shanka1d8da1d2016-10-19 11:10:26 -0700297 if (state == StorageManager.ENCRYPTION_STATE_NONE) {
Ben Komalo0e666092011-09-01 15:42:00 -0700298 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
299 return true; // Unexpected, but fine, I guess...
300 }
Sudheer Shanka1d8da1d2016-10-19 11:10:26 -0700301 return state == StorageManager.ENCRYPTION_STATE_OK;
Ben Komalo0e666092011-09-01 15:42:00 -0700302 } catch (RemoteException e) {
303 Log.w(TAG, "Unable to get encryption state properly");
304 return true;
305 }
306 }
307
308 @Override
309 protected void onPostExecute(Boolean result) {
310 mValidationComplete = true;
311 if (Boolean.FALSE.equals(result)) {
312 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
313 mEncryptionGoneBad = true;
Sudheer Shanka1d8da1d2016-10-19 11:10:26 -0700314 mCorrupt = state == StorageManager.ENCRYPTION_STATE_ERROR_CORRUPT;
Ben Komalod4758ef2011-09-08 14:36:08 -0700315 } else {
316 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700317 }
318 setupUi();
319 }
320 }
321
Ben Komalo91a2f052011-08-16 17:48:25 -0700322 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600323 @Override
324 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600325 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700326 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600327 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600328 break;
Jason parks35933812011-01-21 15:48:20 -0600329
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700330 case MESSAGE_NOTIFY:
331 notifyUser();
332 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600333 }
334 }
335 };
Jason parks35933812011-01-21 15:48:20 -0600336
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700337 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800338 /** The status bar where back/home/recent buttons are shown. */
339 private StatusBarManager mStatusBar;
340
341 /** All the widgets to disable in the status bar */
342 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
343 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
344 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800345 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700346 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800347 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700348
Paul Crowley2555e5f2014-11-07 00:52:57 -0800349 protected static final int MIN_LENGTH_BEFORE_REPORT = LockPatternUtils.MIN_LOCK_PATTERN_SIZE;
350
Ben Komalo91a2f052011-08-16 17:48:25 -0700351 /** @return whether or not this Activity was started for debugging the UI only. */
352 private boolean isDebugView() {
353 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
354 }
355
356 /** @return whether or not this Activity was started for debugging the specific UI view only. */
357 private boolean isDebugView(String viewType /* non-nullable */) {
358 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
359 }
360
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700361 /**
362 * Notify the user that we are awaiting input. Currently this sends an audio alert.
363 */
364 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700365 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700366 --mNotificationCountdown;
367 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700368 try {
369 // Play the standard keypress sound at full volume. This should be available on
370 // every device. We cannot play a ringtone here because media services aren't
371 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
372 // on tablet devices. The idea is to alert the user that something is needed: this
373 // does not have to be pleasing.
374 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
375 } catch (Exception e) {
376 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
377 }
378 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700379 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700380 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700381 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700382
383 if (mWakeLock.isHeld()) {
384 if (mReleaseWakeLockCountdown > 0) {
385 --mReleaseWakeLockCountdown;
386 } else {
387 mWakeLock.release();
388 }
389 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700390 }
391
392 /**
Paul Lawrence2a6552e2015-05-14 15:49:08 -0700393 * Ignore back events from this activity always - there's nowhere to go back
394 * to
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700395 */
396 @Override
397 public void onBackPressed() {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700398 }
399
Jason parks8fd5bc92011-01-12 16:03:31 -0600400 @Override
401 public void onCreate(Bundle savedInstanceState) {
402 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600403
Andy Stadler95974062011-02-01 17:35:20 -0800404 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700405 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700406 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Fyodor Kupolov1b5cc422014-12-08 16:13:11 -0800407 disableCryptKeeperComponent(this);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700408 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700409 // want to be running, so need to finish this activity. We can count
410 // on the activity manager re-launching the new home app upon finishing
411 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700412 // NOTE: This is really grungy. I think it would be better for the
413 // activity manager to explicitly launch the crypt keeper instead of
414 // home in the situation where we need to decrypt the device
415 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600416 return;
417 }
Jason parks35933812011-01-21 15:48:20 -0600418
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700419 try {
420 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
421 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
422 }
423 } catch (NotFoundException e) {
424 }
425
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700426 // Disable the status bar, but do NOT disable back because the user needs a way to go
427 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800428 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
429 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800430
Paul Lawrence27841042015-11-24 12:42:09 -0800431 if (savedInstanceState != null) {
432 mCooldown = savedInstanceState.getBoolean(STATE_COOLDOWN);
433 }
434
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700435 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700436 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800437 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700438 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800439 if (lastInstance instanceof NonConfigurationInstanceState) {
440 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
441 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700442 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800443 }
Jason parksec5a45e2011-01-18 15:28:36 -0600444 }
Jason parks35933812011-01-21 15:48:20 -0600445
Paul Lawrence27841042015-11-24 12:42:09 -0800446 @Override
447 public void onSaveInstanceState(Bundle savedInstanceState) {
448 savedInstanceState.putBoolean(STATE_COOLDOWN, mCooldown);
449 }
450
Andy Stadler95974062011-02-01 17:35:20 -0800451 /**
452 * Note, we defer the state check and screen setup to onStart() because this will be
453 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
454 * especially important if we were to lose the wakelock for any reason.
455 */
456 @Override
457 public void onStart() {
458 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700459 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700460 }
461
462 /**
463 * Initializes the UI based on the current state of encryption.
464 * This is idempotent - calling repeatedly will simply re-initialize the UI.
465 */
466 private void setupUi() {
467 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
468 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence87abbd32014-08-28 15:56:38 -0700469 showFactoryReset(mCorrupt);
Ben Komalo0e666092011-09-01 15:42:00 -0700470 return;
471 }
472
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700473 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700474 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800475 setContentView(R.layout.crypt_keeper_progress);
476 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700477 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700478 new AsyncTask<Void, Void, Void>() {
Paul Crowley529834d2014-10-21 10:12:47 +0100479 int passwordType = StorageManager.CRYPT_TYPE_PASSWORD;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700480 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700481 boolean pattern_visible;
Paul Lawrencec04420c2015-05-18 13:25:01 -0700482 boolean password_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800483
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700484 @Override
485 public Void doInBackground(Void... v) {
486 try {
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800487 final IStorageManager service = getStorageManager();
Paul Crowley529834d2014-10-21 10:12:47 +0100488 passwordType = service.getPasswordType();
Elliott Hughesf9d6d292014-10-02 13:29:58 -0700489 owner_info = service.getField(StorageManager.OWNER_INFO_KEY);
490 pattern_visible = !("0".equals(service.getField(StorageManager.PATTERN_VISIBLE_KEY)));
Paul Lawrencec04420c2015-05-18 13:25:01 -0700491 password_visible = !("0".equals(service.getField(StorageManager.PASSWORD_VISIBLE_KEY)));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700492 } catch (Exception e) {
493 Log.e(TAG, "Error calling mount service " + e);
494 }
495
496 return null;
497 }
498
499 @Override
500 public void onPostExecute(java.lang.Void v) {
Paul Lawrencec04420c2015-05-18 13:25:01 -0700501 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
502 password_visible ? 1 : 0);
503
Paul Crowley51e08112014-11-12 12:06:38 +0000504 if (passwordType == StorageManager.CRYPT_TYPE_PIN) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700505 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700506 mStatusString = R.string.enter_pin;
Paul Crowley529834d2014-10-21 10:12:47 +0100507 } else if (passwordType == StorageManager.CRYPT_TYPE_PATTERN) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700508 setContentView(R.layout.crypt_keeper_pattern_entry);
509 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700510 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700511 } else {
512 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700513 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700514 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700515 final TextView status = (TextView) findViewById(R.id.status);
516 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700517
Paul Lawrence5a70f052014-06-13 11:09:55 -0700518 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
519 ownerInfo.setText(owner_info);
520 ownerInfo.setSelected(true); // Required for marquee'ing to work
521
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700522 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700523
Paul Lawrence2a6552e2015-05-14 15:49:08 -0700524 findViewById(android.R.id.content).setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
525
Paul Lawrence0f11e152014-08-20 07:43:32 -0700526 if (mLockPatternView != null) {
527 mLockPatternView.setInStealthMode(!pattern_visible);
528 }
Paul Crowley51e08112014-11-12 12:06:38 +0000529 if (mCooldown) {
530 // in case we are cooling down and coming back from emergency dialler
Paul Lawrence5a70f052014-06-13 11:09:55 -0700531 setBackFunctionality(false);
Paul Crowley51e08112014-11-12 12:06:38 +0000532 cooldown();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700533 }
Paul Crowley51e08112014-11-12 12:06:38 +0000534
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700535 }
536 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700537 } else if (!mValidationRequested) {
538 // We're supposed to be encrypted, but no validation has been done.
539 new ValidationTask().execute((Void[]) null);
540 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800541 }
542 }
543
Jason parksf8217302011-01-26 13:11:42 -0600544 @Override
545 public void onStop() {
546 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700547 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
548 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800549 }
550
551 /**
552 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
553 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
554 * mWakeLock so the subsequent call to onDestroy does not release it.
555 */
556 @Override
557 public Object onRetainNonConfigurationInstance() {
558 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700559 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800560 mWakeLock = null;
561 return state;
562 }
563
564 @Override
565 public void onDestroy() {
566 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600567
568 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700569 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600570 mWakeLock.release();
571 mWakeLock = null;
572 }
573 }
574
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700575 /**
576 * Start encrypting the device.
577 */
Jason parksec5a45e2011-01-18 15:28:36 -0600578 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600579 // Accquire a partial wakelock to prevent the device from sleeping. Note
580 // we never release this wakelock as we will be restarted after the device
581 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700582 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700583 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700584 Log.d(TAG, "Acquiring wakelock.");
585 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
586 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
587 mWakeLock.acquire();
588 }
Jason parks35933812011-01-21 15:48:20 -0600589
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700590 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700591 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800592 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700593 // Start the first run of progress manually. This method sets up messages to occur at
594 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600595 updateProgress();
596 }
597
Paul Lawrence384d8e52014-08-08 15:11:36 -0700598 /**
599 * Show factory reset screen allowing the user to reset their phone when
600 * there is nothing else we can do
601 * @param corrupt true if userdata is corrupt, false if encryption failed
602 * partway through
603 */
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700604 private void showFactoryReset(final boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800605 // Hide the encryption-bot to make room for the "factory reset" button
606 findViewById(R.id.encroid).setVisibility(View.GONE);
607
608 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700609 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800610 button.setVisibility(View.VISIBLE);
611 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700612 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800613 public void onClick(View v) {
614 // Factory reset the device.
Lenka Trochtova3393dac2017-02-13 15:05:27 +0100615 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
Christopher Tate1e99f0c2017-01-25 14:46:48 -0800616 intent.setPackage("android");
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700617 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
618 intent.putExtra(Intent.EXTRA_REASON,
619 "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
620 sendBroadcast(intent);
Andy Stadler13d62042011-01-31 19:21:37 -0800621 }
622 });
623
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700624 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700625 if (corrupt) {
626 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
627 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
628 } else {
629 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
630 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
631 }
Andy Stadler13d62042011-01-31 19:21:37 -0800632
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700633 final View view = findViewById(R.id.bottom_divider);
634 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700635 if (view != null) {
636 view.setVisibility(View.VISIBLE);
637 }
Andy Stadler13d62042011-01-31 19:21:37 -0800638 }
639
Jason parksf8217302011-01-26 13:11:42 -0600640 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700641 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600642
Ben Komalo0e666092011-09-01 15:42:00 -0700643 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700644 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800645 return;
646 }
647
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700648 // Get status as percentage first
649 CharSequence status = getText(R.string.crypt_keeper_setup_description);
650 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600651 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700652 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700653 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600654 } catch (Exception e) {
655 Log.w(TAG, "Error parsing progress: " + e.toString());
656 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700657 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600658
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700659 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700660 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700661 try {
662 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
663 int time = Integer.parseInt(timeProperty);
664 if (time >= 0) {
665 // Round up to multiple of 10 - this way display is less jerky
666 time = (time + 9) / 10 * 10;
667 progress = DateUtils.formatElapsedTime(time);
668 status = getText(R.string.crypt_keeper_setup_time_remaining);
669 }
670 } catch (Exception e) {
671 // Will happen if no time etc - show percentage
672 }
673
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700674 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700675 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700676 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700677 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700678
679 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700680 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700681 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600682 }
683
Paul Crowley51e08112014-11-12 12:06:38 +0000684 /** Insist on a power cycle to force the user to waste time between retries.
685 *
686 * Call setBackFunctionality(false) before calling this. */
Jason parksf8217302011-01-26 13:11:42 -0600687 private void cooldown() {
Paul Crowley51e08112014-11-12 12:06:38 +0000688 // Disable the password entry.
689 if (mPasswordEntry != null) {
690 mPasswordEntry.setEnabled(false);
Jason parksf8217302011-01-26 13:11:42 -0600691 }
Paul Crowley51e08112014-11-12 12:06:38 +0000692 if (mLockPatternView != null) {
693 mLockPatternView.setEnabled(false);
694 }
695
696 final TextView status = (TextView) findViewById(R.id.status);
697 status.setText(R.string.crypt_keeper_force_power_cycle);
Jason parksec5a45e2011-01-18 15:28:36 -0600698 }
Jason parks35933812011-01-21 15:48:20 -0600699
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800700 /**
701 * Sets the back status: enabled or disabled according to the parameter.
702 * @param isEnabled true if back is enabled, false otherwise.
703 */
704 private final void setBackFunctionality(boolean isEnabled) {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800705 if (isEnabled) {
706 mStatusBar.disable(sWidgetsToDisable);
707 } else {
708 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
709 }
710 }
711
Paul Crowley2555e5f2014-11-07 00:52:57 -0800712 private void fakeUnlockAttempt(View postingView) {
713 beginAttempt();
714 postingView.postDelayed(mFakeUnlockAttemptRunnable, FAKE_ATTEMPT_DELAY);
715 }
716
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800717 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700718 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800719
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700720 @Override
721 public void onPatternStart() {
722 mLockPatternView.removeCallbacks(mClearPatternRunnable);
723 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800724
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700725 @Override
726 public void onPatternCleared() {
727 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800728
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700729 @Override
730 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
731 mLockPatternView.setEnabled(false);
Paul Crowley2555e5f2014-11-07 00:52:57 -0800732 if (pattern.size() >= MIN_LENGTH_BEFORE_REPORT) {
733 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
734 } else {
735 // Allow user to make as many of these as they want.
736 fakeUnlockAttempt(mLockPatternView);
737 }
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700738 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800739
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700740 @Override
741 public void onPatternCellAdded(List<Cell> pattern) {
742 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800743 };
744
745 private void passwordEntryInit() {
746 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600747 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800748 if (mPasswordEntry != null){
749 mPasswordEntry.setOnEditorActionListener(this);
750 mPasswordEntry.requestFocus();
751 // Become quiet when the user interacts with the Edit text screen.
752 mPasswordEntry.setOnKeyListener(this);
753 mPasswordEntry.setOnTouchListener(this);
754 mPasswordEntry.addTextChangedListener(this);
755 }
756
757 // Pattern case
758 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
759 if (mLockPatternView != null) {
760 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
761 }
Jason parks35933812011-01-21 15:48:20 -0600762
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700763 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700764 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700765 final View emergencyCall = findViewById(R.id.emergencyCallButton);
766 if (emergencyCall != null) {
767 Log.d(TAG, "Removing the emergency Call button");
768 emergencyCall.setVisibility(View.GONE);
769 }
770 }
771
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700772 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700773 final InputMethodManager imm = (InputMethodManager) getSystemService(
774 Context.INPUT_METHOD_SERVICE);
775 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
776 imeSwitcher.setVisibility(View.VISIBLE);
777 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700778 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700779 public void onClick(View v) {
Seigo Nonakae7927292015-05-04 17:31:00 -0700780 imm.showInputMethodPicker(false /* showAuxiliarySubtypes */);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700781 }
782 });
Jason parks00046d62011-06-13 17:38:45 -0500783 }
David Brown8373b452011-06-20 12:38:45 -0700784
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700785 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
786 // is completely non-functional, and we want the user to notice the device and enter a
787 // password.
788 if (mWakeLock == null) {
789 Log.d(TAG, "Acquiring wakelock.");
790 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
791 if (pm != null) {
792 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
793 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700794 // Keep awake for 10 minutes - if the user hasn't been alerted by then
795 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700796 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700797 }
798 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700799
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700800 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
801 // immediately.
Paul Crowley51e08112014-11-12 12:06:38 +0000802 if (mLockPatternView == null && !mCooldown) {
Seigo Nonaka25977532016-04-13 08:41:43 +0000803 getWindow().setSoftInputMode(
804 WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Paul Lawrencee54e9322014-03-13 14:05:48 -0700805 mHandler.postDelayed(new Runnable() {
806 @Override public void run() {
807 imm.showSoftInputUnchecked(0, null);
808 }
809 }, 0);
810 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700811
David Brown8373b452011-06-20 12:38:45 -0700812 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700813 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700814 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700815 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800816
John Spurlock2c526512014-09-17 12:55:17 -0400817 // Dismiss secure & non-secure keyguards while this screen is showing.
818 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
819 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Jason parks8fd5bc92011-01-12 16:03:31 -0600820 }
821
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700822 /**
823 * Method adapted from com.android.inputmethod.latin.Utils
824 *
825 * @param imm The input method manager
826 * @param shouldIncludeAuxiliarySubtypes
827 * @return true if we have multiple IMEs to choose from
828 */
829 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
830 final boolean shouldIncludeAuxiliarySubtypes) {
831 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
832
833 // Number of the filtered IMEs
834 int filteredImisCount = 0;
835
836 for (InputMethodInfo imi : enabledImis) {
837 // We can return true immediately after we find two or more filtered IMEs.
838 if (filteredImisCount > 1) return true;
839 final List<InputMethodSubtype> subtypes =
840 imm.getEnabledInputMethodSubtypeList(imi, true);
841 // IMEs that have no subtypes should be counted.
842 if (subtypes.isEmpty()) {
843 ++filteredImisCount;
844 continue;
845 }
846
847 int auxCount = 0;
848 for (InputMethodSubtype subtype : subtypes) {
849 if (subtype.isAuxiliary()) {
850 ++auxCount;
851 }
852 }
853 final int nonAuxCount = subtypes.size() - auxCount;
854
855 // IMEs that have one or more non-auxiliary subtypes should be counted.
856 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
857 // subtypes should be counted as well.
858 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
859 ++filteredImisCount;
860 continue;
861 }
862 }
863
864 return filteredImisCount > 1
865 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
866 // input method subtype (The current IME should be LatinIME.)
867 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
868 }
869
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800870 private IStorageManager getStorageManager() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700871 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600872 if (service != null) {
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800873 return IStorageManager.Stub.asInterface(service);
Jason parks8fd5bc92011-01-12 16:03:31 -0600874 }
875 return null;
876 }
877
878 @Override
879 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500880 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600881 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700882 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600883
Jason parksec5a45e2011-01-18 15:28:36 -0600884 if (TextUtils.isEmpty(password)) {
885 return true;
886 }
Jason parks35933812011-01-21 15:48:20 -0600887
Jason parks8fd5bc92011-01-12 16:03:31 -0600888 // Now that we have the password clear the password field.
889 v.setText(null);
890
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700891 // Disable the password entry and back keypress while checking the password. These
892 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600893 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800894 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600895
Paul Crowley2555e5f2014-11-07 00:52:57 -0800896 if (password.length() >= LockPatternUtils.MIN_LOCK_PATTERN_SIZE) {
897 new DecryptTask().execute(password);
898 } else {
899 // Allow user to make as many of these as they want.
900 fakeUnlockAttempt(mPasswordEntry);
901 }
Jason parks35933812011-01-21 15:48:20 -0600902
Jason parks8fd5bc92011-01-12 16:03:31 -0600903 return true;
904 }
905 return false;
906 }
David Brown8373b452011-06-20 12:38:45 -0700907
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700908 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700909 * Set airplane mode on the device if it isn't an LTE device.
910 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
911 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
912 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
913 * both these problems, we turn the radio off. However, on certain networks turning on and
914 * off the radio takes a long time. In such cases, we are better off leaving the radio
915 * running so the latency of an E911 call is short.
916 * The behavior after this is:
917 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
918 * airplane mode and restart the radio.
919 * 2. Full boot: we read the persistent settings from the previous boot and restore the
920 * radio to whatever it was before it restarted. This also happens when rebooting a
921 * phone that has no encryption.
922 */
923 private final void setAirplaneModeIfNecessary() {
924 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700925 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700926 if (!isLteDevice) {
927 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700928 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700929 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
930 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700931 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700932 }
933 }
934
935 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700936 * Code to update the state of, and handle clicks from, the "Emergency call" button.
937 *
938 * This code is mostly duplicated from the corresponding code in
939 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
940 */
David Brown8373b452011-06-20 12:38:45 -0700941 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700942 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700943 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700944 if (emergencyCall == null)
945 return;
David Brown8373b452011-06-20 12:38:45 -0700946
947 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700948 emergencyCall.setVisibility(View.VISIBLE);
949 emergencyCall.setOnClickListener(new View.OnClickListener() {
950 @Override
951
David Brown8373b452011-06-20 12:38:45 -0700952 public void onClick(View v) {
953 takeEmergencyCallAction();
954 }
955 });
956 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700957 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700958 return;
959 }
960
David Brown8373b452011-06-20 12:38:45 -0700961 int textId;
Tyler Gunn3e71b192014-09-10 15:21:33 -0700962 if (getTelecomManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700963 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700964 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700965 } else {
966 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700967 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700968 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700969 }
970
971 private boolean isEmergencyCallCapable() {
972 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
973 }
974
975 private void takeEmergencyCallAction() {
Tyler Gunn3e71b192014-09-10 15:21:33 -0700976 TelecomManager telecomManager = getTelecomManager();
977 if (telecomManager.isInCall()) {
978 telecomManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700979 } else {
980 launchEmergencyDialer();
981 }
982 }
983
David Brown8373b452011-06-20 12:38:45 -0700984
985 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700986 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700987 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
988 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700989 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700990 startActivity(intent);
991 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700992
Santos Cordon3afbdf02014-05-29 21:48:16 -0700993 private TelephonyManager getTelephonyManager() {
994 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
995 }
996
Tyler Gunn3e71b192014-09-10 15:21:33 -0700997 private TelecomManager getTelecomManager() {
998 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700999 }
1000
Vikram Aggarwald1147252012-05-08 13:52:30 -07001001 /**
1002 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
1003 */
1004 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -07001005 mNotificationCountdown = 20;
1006 }
1007
1008 @Override
1009 public boolean onKey(View v, int keyCode, KeyEvent event) {
1010 delayAudioNotification();
1011 return false;
1012 }
1013
1014 @Override
1015 public boolean onTouch(View v, MotionEvent event) {
1016 delayAudioNotification();
1017 return false;
1018 }
1019
1020 @Override
1021 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
1022 return;
1023 }
1024
1025 @Override
1026 public void onTextChanged(CharSequence s, int start, int before, int count) {
1027 delayAudioNotification();
1028 }
1029
1030 @Override
1031 public void afterTextChanged(Editable s) {
1032 return;
1033 }
Fyodor Kupolov1b5cc422014-12-08 16:13:11 -08001034
1035 private static void disableCryptKeeperComponent(Context context) {
1036 PackageManager pm = context.getPackageManager();
1037 ComponentName name = new ComponentName(context, CryptKeeper.class);
1038 Log.d(TAG, "Disabling component " + name);
Paul Lawrenceae4a5572015-09-19 02:29:37 +00001039 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
1040 PackageManager.DONT_KILL_APP);
Fyodor Kupolov1b5cc422014-12-08 16:13:11 -08001041 }
David Brown8373b452011-06-20 12:38:45 -07001042}