blob: 560fe76f870356a929648180f8fb859960d20518 [file] [log] [blame]
Jason parks8fd5bc92011-01-12 16:03:31 -06001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason parks8fd5bc92011-01-12 16:03:31 -060019import android.app.Activity;
20import android.app.StatusBarManager;
21import android.content.ComponentName;
22import android.content.Context;
Jason parksec5a45e2011-01-18 15:28:36 -060023import android.content.Intent;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070024import android.content.pm.ActivityInfo;
Jason parks8fd5bc92011-01-12 16:03:31 -060025import android.content.pm.PackageManager;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070026import android.content.res.Resources.NotFoundException;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070027import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060028import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060031import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060032import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060033import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070034import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060035import android.os.ServiceManager;
36import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070037import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080039import android.os.storage.StorageManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070040import android.provider.Settings;
Tyler Gunn3e71b192014-09-10 15:21:33 -070041import android.telecom.TelecomManager;
David Brown8373b452011-06-20 12:38:45 -070042import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070043import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060044import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070045import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070046import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060047import android.util.Log;
48import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070049import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080050import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080051import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080052import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070053import android.view.View.OnKeyListener;
54import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060055import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070056import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060057import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070058import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080059import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060060import android.widget.EditText;
61import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060062import android.widget.TextView;
63
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080064import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070065import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070066import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080067import com.android.internal.widget.LockPatternUtils;
68import com.android.internal.widget.LockPatternView;
69import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070070
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070071import static com.android.internal.widget.LockPatternView.DisplayMode;
72
Ben Komalo9fcb6a72011-08-26 14:40:18 -070073import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070074
75/**
76 * Settings screens to show the UI flows for encrypting/decrypting the device.
77 *
78 * This may be started via adb for debugging the UI layout, without having to go through
79 * encryption flows everytime. It should be noted that starting the activity in this manner
80 * is only useful for verifying UI-correctness - the behavior will not be identical.
81 * <pre>
82 * $ adb shell pm enable com.android.settings/.CryptKeeper
83 * $ adb shell am start \
84 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
85 * -n com.android.settings/.CryptKeeper
86 * </pre>
87 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070088public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
89 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060090 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060091
Jason parks8fd5bc92011-01-12 16:03:31 -060092 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070093 /** Message sent to us to indicate encryption update progress. */
94 private static final int MESSAGE_UPDATE_PROGRESS = 1;
95 /** Message sent to us to cool-down (waste user's time between password attempts) */
96 private static final int MESSAGE_COOLDOWN = 2;
97 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
98 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060099
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700100 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600101 private static final int MAX_FAILED_ATTEMPTS = 30;
102 private static final int COOL_DOWN_ATTEMPTS = 10;
103 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
104
David Brown8373b452011-06-20 12:38:45 -0700105 // Intent action for launching the Emergency Dialer activity.
106 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
107
Ben Komalo91a2f052011-08-16 17:48:25 -0700108 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
109 private static final String EXTRA_FORCE_VIEW =
110 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
111 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700112 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700113 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700114
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700115 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700116 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700117 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700118 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
119 private boolean mEncryptionGoneBad;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700120 /** If gone bad, should we show encryption failed (false) or corrupt (true)*/
121 private boolean mCorrupt;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700122 /** A flag to indicate when the back event should be ignored */
123 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800124 private int mCooldown;
125 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600126 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800127 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700128 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
129 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700130 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
131 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700132 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800133
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700134 // how long we wait to clear a wrong pattern
135 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
136
Paul Lawrence0f11e152014-08-20 07:43:32 -0700137 // how long we wait to clear a right pattern
138 private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
139
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700140 private Runnable mClearPatternRunnable = new Runnable() {
141 public void run() {
142 mLockPatternView.clearPattern();
143 }
144 };
145
Andy Stadler14997402011-02-01 15:34:59 -0800146 /**
147 * Used to propagate state through configuration changes (e.g. screen rotation)
148 */
149 private static class NonConfigurationInstanceState {
150 final PowerManager.WakeLock wakelock;
151
152 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
153 wakelock = _wakelock;
154 }
155 }
156
Jason parks06c5ff42011-03-01 10:17:40 -0600157 private class DecryptTask extends AsyncTask<String, Void, Integer> {
158 @Override
159 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700160 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600161 try {
162 return service.decryptStorage(params[0]);
163 } catch (Exception e) {
164 Log.e(TAG, "Error while decrypting...", e);
165 return -1;
166 }
167 }
168
169 @Override
170 protected void onPostExecute(Integer failedAttempts) {
171 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700172 // The password was entered successfully. Simply do nothing
173 // and wait for the service restart to switch to surfacefligner
Paul Lawrence0f11e152014-08-20 07:43:32 -0700174 if (mLockPatternView != null) {
175 mLockPatternView.removeCallbacks(mClearPatternRunnable);
176 mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
177 }
Jason parks06c5ff42011-03-01 10:17:40 -0600178 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
179 // Factory reset the device.
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700180 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
181 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
182 intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
183 sendBroadcast(intent);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700184 } else if (failedAttempts == -1) {
185 // Right password, but decryption failed. Tell user bad news ...
186 setContentView(R.layout.crypt_keeper_progress);
187 showFactoryReset(true);
188 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600189 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700190 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700191 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700192 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
193 mLockPatternView.removeCallbacks(mClearPatternRunnable);
194 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
195 }
196 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
197 mCooldown = COOL_DOWN_INTERVAL;
198 cooldown();
199 } else {
200 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700201
202 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
203 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
204 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
205 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
206 Integer.toString(remainingAttempts));
207 status.setText(warning);
208 } else {
209 status.setText(R.string.try_again);
210 }
211
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700212 if (mLockPatternView != null) {
213 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
214 }
215 // Reenable the password entry
216 if (mPasswordEntry != null) {
217 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700218 final InputMethodManager imm = (InputMethodManager) getSystemService(
219 Context.INPUT_METHOD_SERVICE);
220 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700221 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700222 }
223 if (mLockPatternView != null) {
224 mLockPatternView.setEnabled(true);
225 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700226 }
Jason parks06c5ff42011-03-01 10:17:40 -0600227 }
228 }
229 }
Jason parks75c085e2011-02-10 14:03:47 -0600230
Ben Komalo0e666092011-09-01 15:42:00 -0700231 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
Paul Lawrence87abbd32014-08-28 15:56:38 -0700232 int state;
233
Ben Komalo0e666092011-09-01 15:42:00 -0700234 @Override
235 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700236 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700237 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700238 Log.d(TAG, "Validating encryption state.");
Paul Lawrence87abbd32014-08-28 15:56:38 -0700239 state = service.getEncryptionState();
Ben Komalo0e666092011-09-01 15:42:00 -0700240 if (state == IMountService.ENCRYPTION_STATE_NONE) {
241 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
242 return true; // Unexpected, but fine, I guess...
243 }
244 return state == IMountService.ENCRYPTION_STATE_OK;
245 } catch (RemoteException e) {
246 Log.w(TAG, "Unable to get encryption state properly");
247 return true;
248 }
249 }
250
251 @Override
252 protected void onPostExecute(Boolean result) {
253 mValidationComplete = true;
254 if (Boolean.FALSE.equals(result)) {
255 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
256 mEncryptionGoneBad = true;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700257 mCorrupt = state == IMountService.ENCRYPTION_STATE_ERROR_CORRUPT;
Ben Komalod4758ef2011-09-08 14:36:08 -0700258 } else {
259 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700260 }
261 setupUi();
262 }
263 }
264
Ben Komalo91a2f052011-08-16 17:48:25 -0700265 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600266 @Override
267 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600268 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700269 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600270 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600271 break;
Jason parks35933812011-01-21 15:48:20 -0600272
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700273 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600274 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600275 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700276
277 case MESSAGE_NOTIFY:
278 notifyUser();
279 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600280 }
281 }
282 };
Jason parks35933812011-01-21 15:48:20 -0600283
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700284 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800285 /** The status bar where back/home/recent buttons are shown. */
286 private StatusBarManager mStatusBar;
287
288 /** All the widgets to disable in the status bar */
289 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
290 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
291 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
292 | StatusBarManager.DISABLE_SYSTEM_INFO
293 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700294 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800295 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700296
Ben Komalo91a2f052011-08-16 17:48:25 -0700297 /** @return whether or not this Activity was started for debugging the UI only. */
298 private boolean isDebugView() {
299 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
300 }
301
302 /** @return whether or not this Activity was started for debugging the specific UI view only. */
303 private boolean isDebugView(String viewType /* non-nullable */) {
304 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
305 }
306
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700307 /**
308 * Notify the user that we are awaiting input. Currently this sends an audio alert.
309 */
310 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700311 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700312 --mNotificationCountdown;
313 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700314 try {
315 // Play the standard keypress sound at full volume. This should be available on
316 // every device. We cannot play a ringtone here because media services aren't
317 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
318 // on tablet devices. The idea is to alert the user that something is needed: this
319 // does not have to be pleasing.
320 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
321 } catch (Exception e) {
322 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
323 }
324 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700325 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700326 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700327 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700328
329 if (mWakeLock.isHeld()) {
330 if (mReleaseWakeLockCountdown > 0) {
331 --mReleaseWakeLockCountdown;
332 } else {
333 mWakeLock.release();
334 }
335 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700336 }
337
338 /**
339 * Ignore back events after the user has entered the decrypt screen and while the device is
340 * encrypting.
341 */
342 @Override
343 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800344 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700345 if (mIgnoreBack)
346 return;
347 super.onBackPressed();
348 }
349
Jason parks8fd5bc92011-01-12 16:03:31 -0600350 @Override
351 public void onCreate(Bundle savedInstanceState) {
352 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600353
Andy Stadler95974062011-02-01 17:35:20 -0800354 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700355 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700356 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600357 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600358 PackageManager pm = getPackageManager();
359 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700360 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
361 PackageManager.DONT_KILL_APP);
362 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700363 // want to be running, so need to finish this activity. We can count
364 // on the activity manager re-launching the new home app upon finishing
365 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700366 // NOTE: This is really grungy. I think it would be better for the
367 // activity manager to explicitly launch the crypt keeper instead of
368 // home in the situation where we need to decrypt the device
369 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600370 return;
371 }
Jason parks35933812011-01-21 15:48:20 -0600372
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700373 try {
374 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
375 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
376 }
377 } catch (NotFoundException e) {
378 }
379
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700380 // Disable the status bar, but do NOT disable back because the user needs a way to go
381 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800382 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
383 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800384
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700385 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700386 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800387 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700388 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800389 if (lastInstance instanceof NonConfigurationInstanceState) {
390 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
391 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700392 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800393 }
Jason parksec5a45e2011-01-18 15:28:36 -0600394 }
Jason parks35933812011-01-21 15:48:20 -0600395
Andy Stadler95974062011-02-01 17:35:20 -0800396 /**
397 * Note, we defer the state check and screen setup to onStart() because this will be
398 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
399 * especially important if we were to lose the wakelock for any reason.
400 */
401 @Override
402 public void onStart() {
403 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700404 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700405 }
406
407 /**
408 * Initializes the UI based on the current state of encryption.
409 * This is idempotent - calling repeatedly will simply re-initialize the UI.
410 */
411 private void setupUi() {
412 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
413 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence87abbd32014-08-28 15:56:38 -0700414 showFactoryReset(mCorrupt);
Ben Komalo0e666092011-09-01 15:42:00 -0700415 return;
416 }
417
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700418 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700419 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800420 setContentView(R.layout.crypt_keeper_progress);
421 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700422 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700423 new AsyncTask<Void, Void, Void>() {
424 int type = StorageManager.CRYPT_TYPE_PASSWORD;
425 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700426 boolean pattern_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800427
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700428 @Override
429 public Void doInBackground(Void... v) {
430 try {
431 final IMountService service = getMountService();
432 type = service.getPasswordType();
433 owner_info = service.getField("OwnerInfo");
Paul Lawrence0f11e152014-08-20 07:43:32 -0700434 pattern_visible = !("0".equals(service.getField("PatternVisible")));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700435 } catch (Exception e) {
436 Log.e(TAG, "Error calling mount service " + e);
437 }
438
439 return null;
440 }
441
442 @Override
443 public void onPostExecute(java.lang.Void v) {
444 if(type == StorageManager.CRYPT_TYPE_PIN) {
445 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700446 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700447 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
448 setContentView(R.layout.crypt_keeper_pattern_entry);
449 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700450 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700451 } else {
452 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700453 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700454 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700455 final TextView status = (TextView) findViewById(R.id.status);
456 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700457
Paul Lawrence5a70f052014-06-13 11:09:55 -0700458 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
459 ownerInfo.setText(owner_info);
460 ownerInfo.setSelected(true); // Required for marquee'ing to work
461
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700462 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700463
Paul Lawrence0f11e152014-08-20 07:43:32 -0700464 if (mLockPatternView != null) {
465 mLockPatternView.setInStealthMode(!pattern_visible);
466 }
467
Paul Lawrence5a70f052014-06-13 11:09:55 -0700468 if (mCooldown > 0) {
469 setBackFunctionality(false);
470 cooldown(); // in case we are cooling down and coming back from emergency dialler
471 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700472 }
473 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700474 } else if (!mValidationRequested) {
475 // We're supposed to be encrypted, but no validation has been done.
476 new ValidationTask().execute((Void[]) null);
477 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800478 }
479 }
480
Jason parksf8217302011-01-26 13:11:42 -0600481 @Override
482 public void onStop() {
483 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700484 mHandler.removeMessages(MESSAGE_COOLDOWN);
485 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
486 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800487 }
488
489 /**
490 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
491 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
492 * mWakeLock so the subsequent call to onDestroy does not release it.
493 */
494 @Override
495 public Object onRetainNonConfigurationInstance() {
496 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700497 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800498 mWakeLock = null;
499 return state;
500 }
501
502 @Override
503 public void onDestroy() {
504 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600505
506 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700507 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600508 mWakeLock.release();
509 mWakeLock = null;
510 }
511 }
512
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700513 /**
514 * Start encrypting the device.
515 */
Jason parksec5a45e2011-01-18 15:28:36 -0600516 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600517 // Accquire a partial wakelock to prevent the device from sleeping. Note
518 // we never release this wakelock as we will be restarted after the device
519 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700520 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700521 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700522 Log.d(TAG, "Acquiring wakelock.");
523 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
524 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
525 mWakeLock.acquire();
526 }
Jason parks35933812011-01-21 15:48:20 -0600527
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700528 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700529 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800530 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700531 // Start the first run of progress manually. This method sets up messages to occur at
532 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600533 updateProgress();
534 }
535
Paul Lawrence384d8e52014-08-08 15:11:36 -0700536 /**
537 * Show factory reset screen allowing the user to reset their phone when
538 * there is nothing else we can do
539 * @param corrupt true if userdata is corrupt, false if encryption failed
540 * partway through
541 */
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700542 private void showFactoryReset(final boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800543 // Hide the encryption-bot to make room for the "factory reset" button
544 findViewById(R.id.encroid).setVisibility(View.GONE);
545
546 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700547 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800548 button.setVisibility(View.VISIBLE);
549 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700550 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800551 public void onClick(View v) {
552 // Factory reset the device.
Jeff Sharkey1de688d2014-09-24 13:57:07 -0700553 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
554 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
555 intent.putExtra(Intent.EXTRA_REASON,
556 "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
557 sendBroadcast(intent);
Andy Stadler13d62042011-01-31 19:21:37 -0800558 }
559 });
560
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700561 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700562 if (corrupt) {
563 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
564 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
565 } else {
566 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
567 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
568 }
Andy Stadler13d62042011-01-31 19:21:37 -0800569
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700570 final View view = findViewById(R.id.bottom_divider);
571 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700572 if (view != null) {
573 view.setVisibility(View.VISIBLE);
574 }
Andy Stadler13d62042011-01-31 19:21:37 -0800575 }
576
Jason parksf8217302011-01-26 13:11:42 -0600577 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700578 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600579
Ben Komalo0e666092011-09-01 15:42:00 -0700580 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700581 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800582 return;
583 }
584
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700585 // Get status as percentage first
586 CharSequence status = getText(R.string.crypt_keeper_setup_description);
587 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600588 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700589 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700590 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600591 } catch (Exception e) {
592 Log.w(TAG, "Error parsing progress: " + e.toString());
593 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700594 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600595
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700596 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700597 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700598 try {
599 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
600 int time = Integer.parseInt(timeProperty);
601 if (time >= 0) {
602 // Round up to multiple of 10 - this way display is less jerky
603 time = (time + 9) / 10 * 10;
604 progress = DateUtils.formatElapsedTime(time);
605 status = getText(R.string.crypt_keeper_setup_time_remaining);
606 }
607 } catch (Exception e) {
608 // Will happen if no time etc - show percentage
609 }
610
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700611 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700612 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700613 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700614 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700615
616 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700617 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700618 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600619 }
620
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700621 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600622 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700623 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800624
Jason parksf8217302011-01-26 13:11:42 -0600625 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700626 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700627 if (mPasswordEntry != null) {
628 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700629 final InputMethodManager imm = (InputMethodManager) getSystemService(
630 Context.INPUT_METHOD_SERVICE);
631 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700632 setBackFunctionality(true);
633 }
634 if (mLockPatternView != null) {
635 mLockPatternView.setEnabled(true);
636 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700637 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600638 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700639 // Disable the password entry and back presses.
640 if (mPasswordEntry != null) {
641 mPasswordEntry.setEnabled(false);
642 }
643 if (mLockPatternView != null) {
644 mLockPatternView.setEnabled(false);
645 }
646
Andy Stadler13d62042011-01-31 19:21:37 -0800647 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700648 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800649
Jason parksf8217302011-01-26 13:11:42 -0600650 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700651 mHandler.removeMessages(MESSAGE_COOLDOWN);
652 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600653 }
Jason parksec5a45e2011-01-18 15:28:36 -0600654 }
Jason parks35933812011-01-21 15:48:20 -0600655
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800656 /**
657 * Sets the back status: enabled or disabled according to the parameter.
658 * @param isEnabled true if back is enabled, false otherwise.
659 */
660 private final void setBackFunctionality(boolean isEnabled) {
661 mIgnoreBack = !isEnabled;
662 if (isEnabled) {
663 mStatusBar.disable(sWidgetsToDisable);
664 } else {
665 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
666 }
667 }
668
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800669 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700670 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800671
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700672 @Override
673 public void onPatternStart() {
674 mLockPatternView.removeCallbacks(mClearPatternRunnable);
675 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800676
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700677 @Override
678 public void onPatternCleared() {
679 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800680
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700681 @Override
682 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
683 mLockPatternView.setEnabled(false);
684 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
685 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800686
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700687 @Override
688 public void onPatternCellAdded(List<Cell> pattern) {
689 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800690 };
691
692 private void passwordEntryInit() {
693 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600694 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800695 if (mPasswordEntry != null){
696 mPasswordEntry.setOnEditorActionListener(this);
697 mPasswordEntry.requestFocus();
698 // Become quiet when the user interacts with the Edit text screen.
699 mPasswordEntry.setOnKeyListener(this);
700 mPasswordEntry.setOnTouchListener(this);
701 mPasswordEntry.addTextChangedListener(this);
702 }
703
704 // Pattern case
705 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
706 if (mLockPatternView != null) {
707 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
708 }
Jason parks35933812011-01-21 15:48:20 -0600709
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700710 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700711 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700712 final View emergencyCall = findViewById(R.id.emergencyCallButton);
713 if (emergencyCall != null) {
714 Log.d(TAG, "Removing the emergency Call button");
715 emergencyCall.setVisibility(View.GONE);
716 }
717 }
718
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700719 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700720 final InputMethodManager imm = (InputMethodManager) getSystemService(
721 Context.INPUT_METHOD_SERVICE);
722 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
723 imeSwitcher.setVisibility(View.VISIBLE);
724 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700725 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700726 public void onClick(View v) {
727 imm.showInputMethodPicker();
728 }
729 });
Jason parks00046d62011-06-13 17:38:45 -0500730 }
David Brown8373b452011-06-20 12:38:45 -0700731
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700732 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
733 // is completely non-functional, and we want the user to notice the device and enter a
734 // password.
735 if (mWakeLock == null) {
736 Log.d(TAG, "Acquiring wakelock.");
737 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
738 if (pm != null) {
739 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
740 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700741 // Keep awake for 10 minutes - if the user hasn't been alerted by then
742 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700743 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700744 }
745 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700746
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700747 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
748 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700749 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700750 mHandler.postDelayed(new Runnable() {
751 @Override public void run() {
752 imm.showSoftInputUnchecked(0, null);
753 }
754 }, 0);
755 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700756
David Brown8373b452011-06-20 12:38:45 -0700757 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700758 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700759 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700760 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800761
John Spurlock2c526512014-09-17 12:55:17 -0400762 // Dismiss secure & non-secure keyguards while this screen is showing.
763 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
764 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Jason parks8fd5bc92011-01-12 16:03:31 -0600765 }
766
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700767 /**
768 * Method adapted from com.android.inputmethod.latin.Utils
769 *
770 * @param imm The input method manager
771 * @param shouldIncludeAuxiliarySubtypes
772 * @return true if we have multiple IMEs to choose from
773 */
774 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
775 final boolean shouldIncludeAuxiliarySubtypes) {
776 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
777
778 // Number of the filtered IMEs
779 int filteredImisCount = 0;
780
781 for (InputMethodInfo imi : enabledImis) {
782 // We can return true immediately after we find two or more filtered IMEs.
783 if (filteredImisCount > 1) return true;
784 final List<InputMethodSubtype> subtypes =
785 imm.getEnabledInputMethodSubtypeList(imi, true);
786 // IMEs that have no subtypes should be counted.
787 if (subtypes.isEmpty()) {
788 ++filteredImisCount;
789 continue;
790 }
791
792 int auxCount = 0;
793 for (InputMethodSubtype subtype : subtypes) {
794 if (subtype.isAuxiliary()) {
795 ++auxCount;
796 }
797 }
798 final int nonAuxCount = subtypes.size() - auxCount;
799
800 // IMEs that have one or more non-auxiliary subtypes should be counted.
801 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
802 // subtypes should be counted as well.
803 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
804 ++filteredImisCount;
805 continue;
806 }
807 }
808
809 return filteredImisCount > 1
810 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
811 // input method subtype (The current IME should be LatinIME.)
812 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
813 }
814
Jason parks8fd5bc92011-01-12 16:03:31 -0600815 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700816 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600817 if (service != null) {
818 return IMountService.Stub.asInterface(service);
819 }
820 return null;
821 }
822
823 @Override
824 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500825 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600826 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700827 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600828
Jason parksec5a45e2011-01-18 15:28:36 -0600829 if (TextUtils.isEmpty(password)) {
830 return true;
831 }
Jason parks35933812011-01-21 15:48:20 -0600832
Jason parks8fd5bc92011-01-12 16:03:31 -0600833 // Now that we have the password clear the password field.
834 v.setText(null);
835
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700836 // Disable the password entry and back keypress while checking the password. These
837 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600838 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800839 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600840
Ben Komalo04606752011-08-18 14:50:26 -0700841 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600842 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600843
Jason parks8fd5bc92011-01-12 16:03:31 -0600844 return true;
845 }
846 return false;
847 }
David Brown8373b452011-06-20 12:38:45 -0700848
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700849 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700850 * Set airplane mode on the device if it isn't an LTE device.
851 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
852 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
853 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
854 * both these problems, we turn the radio off. However, on certain networks turning on and
855 * off the radio takes a long time. In such cases, we are better off leaving the radio
856 * running so the latency of an E911 call is short.
857 * The behavior after this is:
858 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
859 * airplane mode and restart the radio.
860 * 2. Full boot: we read the persistent settings from the previous boot and restore the
861 * radio to whatever it was before it restarted. This also happens when rebooting a
862 * phone that has no encryption.
863 */
864 private final void setAirplaneModeIfNecessary() {
865 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700866 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700867 if (!isLteDevice) {
868 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700869 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700870 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
871 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700872 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700873 }
874 }
875
876 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700877 * Code to update the state of, and handle clicks from, the "Emergency call" button.
878 *
879 * This code is mostly duplicated from the corresponding code in
880 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
881 */
David Brown8373b452011-06-20 12:38:45 -0700882 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700883 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700884 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700885 if (emergencyCall == null)
886 return;
David Brown8373b452011-06-20 12:38:45 -0700887
888 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700889 emergencyCall.setVisibility(View.VISIBLE);
890 emergencyCall.setOnClickListener(new View.OnClickListener() {
891 @Override
892
David Brown8373b452011-06-20 12:38:45 -0700893 public void onClick(View v) {
894 takeEmergencyCallAction();
895 }
896 });
897 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700898 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700899 return;
900 }
901
David Brown8373b452011-06-20 12:38:45 -0700902 int textId;
Tyler Gunn3e71b192014-09-10 15:21:33 -0700903 if (getTelecomManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700904 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700905 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700906 } else {
907 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700908 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700909 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700910 }
911
912 private boolean isEmergencyCallCapable() {
913 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
914 }
915
916 private void takeEmergencyCallAction() {
Tyler Gunn3e71b192014-09-10 15:21:33 -0700917 TelecomManager telecomManager = getTelecomManager();
918 if (telecomManager.isInCall()) {
919 telecomManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700920 } else {
921 launchEmergencyDialer();
922 }
923 }
924
David Brown8373b452011-06-20 12:38:45 -0700925
926 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700927 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700928 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
929 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700930 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700931 startActivity(intent);
932 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700933
Santos Cordon3afbdf02014-05-29 21:48:16 -0700934 private TelephonyManager getTelephonyManager() {
935 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
936 }
937
Tyler Gunn3e71b192014-09-10 15:21:33 -0700938 private TelecomManager getTelecomManager() {
939 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700940 }
941
Vikram Aggarwald1147252012-05-08 13:52:30 -0700942 /**
943 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
944 */
945 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700946 mNotificationCountdown = 20;
947 }
948
949 @Override
950 public boolean onKey(View v, int keyCode, KeyEvent event) {
951 delayAudioNotification();
952 return false;
953 }
954
955 @Override
956 public boolean onTouch(View v, MotionEvent event) {
957 delayAudioNotification();
958 return false;
959 }
960
961 @Override
962 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
963 return;
964 }
965
966 @Override
967 public void onTextChanged(CharSequence s, int start, int before, int count) {
968 delayAudioNotification();
969 }
970
971 @Override
972 public void afterTextChanged(Editable s) {
973 return;
974 }
David Brown8373b452011-06-20 12:38:45 -0700975}