blob: 013fd3a78a98d0a6f4e4fd4a0cfe03f4afb683e7 [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.
180 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Paul Lawrence384d8e52014-08-08 15:11:36 -0700181 } else if (failedAttempts == -1) {
182 // Right password, but decryption failed. Tell user bad news ...
183 setContentView(R.layout.crypt_keeper_progress);
184 showFactoryReset(true);
185 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600186 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700187 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700188 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700189 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
190 mLockPatternView.removeCallbacks(mClearPatternRunnable);
191 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
192 }
193 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
194 mCooldown = COOL_DOWN_INTERVAL;
195 cooldown();
196 } else {
197 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700198
199 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
200 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
201 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
202 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
203 Integer.toString(remainingAttempts));
204 status.setText(warning);
205 } else {
206 status.setText(R.string.try_again);
207 }
208
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700209 if (mLockPatternView != null) {
210 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
211 }
212 // Reenable the password entry
213 if (mPasswordEntry != null) {
214 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700215 final InputMethodManager imm = (InputMethodManager) getSystemService(
216 Context.INPUT_METHOD_SERVICE);
217 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700218 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700219 }
220 if (mLockPatternView != null) {
221 mLockPatternView.setEnabled(true);
222 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700223 }
Jason parks06c5ff42011-03-01 10:17:40 -0600224 }
225 }
226 }
Jason parks75c085e2011-02-10 14:03:47 -0600227
Ben Komalo0e666092011-09-01 15:42:00 -0700228 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
Paul Lawrence87abbd32014-08-28 15:56:38 -0700229 int state;
230
Ben Komalo0e666092011-09-01 15:42:00 -0700231 @Override
232 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700233 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700234 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700235 Log.d(TAG, "Validating encryption state.");
Paul Lawrence87abbd32014-08-28 15:56:38 -0700236 state = service.getEncryptionState();
Ben Komalo0e666092011-09-01 15:42:00 -0700237 if (state == IMountService.ENCRYPTION_STATE_NONE) {
238 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
239 return true; // Unexpected, but fine, I guess...
240 }
241 return state == IMountService.ENCRYPTION_STATE_OK;
242 } catch (RemoteException e) {
243 Log.w(TAG, "Unable to get encryption state properly");
244 return true;
245 }
246 }
247
248 @Override
249 protected void onPostExecute(Boolean result) {
250 mValidationComplete = true;
251 if (Boolean.FALSE.equals(result)) {
252 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
253 mEncryptionGoneBad = true;
Paul Lawrence87abbd32014-08-28 15:56:38 -0700254 mCorrupt = state == IMountService.ENCRYPTION_STATE_ERROR_CORRUPT;
Ben Komalod4758ef2011-09-08 14:36:08 -0700255 } else {
256 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700257 }
258 setupUi();
259 }
260 }
261
Ben Komalo91a2f052011-08-16 17:48:25 -0700262 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600263 @Override
264 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600265 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700266 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600267 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600268 break;
Jason parks35933812011-01-21 15:48:20 -0600269
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700270 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600271 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600272 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700273
274 case MESSAGE_NOTIFY:
275 notifyUser();
276 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600277 }
278 }
279 };
Jason parks35933812011-01-21 15:48:20 -0600280
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700281 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800282 /** The status bar where back/home/recent buttons are shown. */
283 private StatusBarManager mStatusBar;
284
285 /** All the widgets to disable in the status bar */
286 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
287 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
288 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
289 | StatusBarManager.DISABLE_SYSTEM_INFO
290 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700291 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800292 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700293
Ben Komalo91a2f052011-08-16 17:48:25 -0700294 /** @return whether or not this Activity was started for debugging the UI only. */
295 private boolean isDebugView() {
296 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
297 }
298
299 /** @return whether or not this Activity was started for debugging the specific UI view only. */
300 private boolean isDebugView(String viewType /* non-nullable */) {
301 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
302 }
303
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700304 /**
305 * Notify the user that we are awaiting input. Currently this sends an audio alert.
306 */
307 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700308 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700309 --mNotificationCountdown;
310 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700311 try {
312 // Play the standard keypress sound at full volume. This should be available on
313 // every device. We cannot play a ringtone here because media services aren't
314 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
315 // on tablet devices. The idea is to alert the user that something is needed: this
316 // does not have to be pleasing.
317 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
318 } catch (Exception e) {
319 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
320 }
321 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700322 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700323 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700324 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700325
326 if (mWakeLock.isHeld()) {
327 if (mReleaseWakeLockCountdown > 0) {
328 --mReleaseWakeLockCountdown;
329 } else {
330 mWakeLock.release();
331 }
332 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700333 }
334
335 /**
336 * Ignore back events after the user has entered the decrypt screen and while the device is
337 * encrypting.
338 */
339 @Override
340 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800341 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700342 if (mIgnoreBack)
343 return;
344 super.onBackPressed();
345 }
346
Jason parks8fd5bc92011-01-12 16:03:31 -0600347 @Override
348 public void onCreate(Bundle savedInstanceState) {
349 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600350
Andy Stadler95974062011-02-01 17:35:20 -0800351 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700352 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700353 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600354 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600355 PackageManager pm = getPackageManager();
356 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700357 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
358 PackageManager.DONT_KILL_APP);
359 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700360 // want to be running, so need to finish this activity. We can count
361 // on the activity manager re-launching the new home app upon finishing
362 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700363 // NOTE: This is really grungy. I think it would be better for the
364 // activity manager to explicitly launch the crypt keeper instead of
365 // home in the situation where we need to decrypt the device
366 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600367 return;
368 }
Jason parks35933812011-01-21 15:48:20 -0600369
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700370 try {
371 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
372 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
373 }
374 } catch (NotFoundException e) {
375 }
376
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700377 // Disable the status bar, but do NOT disable back because the user needs a way to go
378 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800379 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
380 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800381
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700382 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700383 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800384 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700385 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800386 if (lastInstance instanceof NonConfigurationInstanceState) {
387 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
388 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700389 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800390 }
Jason parksec5a45e2011-01-18 15:28:36 -0600391 }
Jason parks35933812011-01-21 15:48:20 -0600392
Andy Stadler95974062011-02-01 17:35:20 -0800393 /**
394 * Note, we defer the state check and screen setup to onStart() because this will be
395 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
396 * especially important if we were to lose the wakelock for any reason.
397 */
398 @Override
399 public void onStart() {
400 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700401 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700402 }
403
404 /**
405 * Initializes the UI based on the current state of encryption.
406 * This is idempotent - calling repeatedly will simply re-initialize the UI.
407 */
408 private void setupUi() {
409 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
410 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence87abbd32014-08-28 15:56:38 -0700411 showFactoryReset(mCorrupt);
Ben Komalo0e666092011-09-01 15:42:00 -0700412 return;
413 }
414
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700415 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700416 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800417 setContentView(R.layout.crypt_keeper_progress);
418 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700419 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700420 new AsyncTask<Void, Void, Void>() {
421 int type = StorageManager.CRYPT_TYPE_PASSWORD;
422 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700423 boolean pattern_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800424
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700425 @Override
426 public Void doInBackground(Void... v) {
427 try {
428 final IMountService service = getMountService();
429 type = service.getPasswordType();
430 owner_info = service.getField("OwnerInfo");
Paul Lawrence0f11e152014-08-20 07:43:32 -0700431 pattern_visible = !("0".equals(service.getField("PatternVisible")));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700432 } catch (Exception e) {
433 Log.e(TAG, "Error calling mount service " + e);
434 }
435
436 return null;
437 }
438
439 @Override
440 public void onPostExecute(java.lang.Void v) {
441 if(type == StorageManager.CRYPT_TYPE_PIN) {
442 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700443 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700444 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
445 setContentView(R.layout.crypt_keeper_pattern_entry);
446 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700447 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700448 } else {
449 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700450 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700451 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700452 final TextView status = (TextView) findViewById(R.id.status);
453 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700454
Paul Lawrence5a70f052014-06-13 11:09:55 -0700455 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
456 ownerInfo.setText(owner_info);
457 ownerInfo.setSelected(true); // Required for marquee'ing to work
458
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700459 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700460
Paul Lawrence0f11e152014-08-20 07:43:32 -0700461 if (mLockPatternView != null) {
462 mLockPatternView.setInStealthMode(!pattern_visible);
463 }
464
Paul Lawrence5a70f052014-06-13 11:09:55 -0700465 if (mCooldown > 0) {
466 setBackFunctionality(false);
467 cooldown(); // in case we are cooling down and coming back from emergency dialler
468 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700469 }
470 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700471 } else if (!mValidationRequested) {
472 // We're supposed to be encrypted, but no validation has been done.
473 new ValidationTask().execute((Void[]) null);
474 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800475 }
476 }
477
Jason parksf8217302011-01-26 13:11:42 -0600478 @Override
479 public void onStop() {
480 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700481 mHandler.removeMessages(MESSAGE_COOLDOWN);
482 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
483 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800484 }
485
486 /**
487 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
488 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
489 * mWakeLock so the subsequent call to onDestroy does not release it.
490 */
491 @Override
492 public Object onRetainNonConfigurationInstance() {
493 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700494 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800495 mWakeLock = null;
496 return state;
497 }
498
499 @Override
500 public void onDestroy() {
501 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600502
503 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700504 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600505 mWakeLock.release();
506 mWakeLock = null;
507 }
508 }
509
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700510 /**
511 * Start encrypting the device.
512 */
Jason parksec5a45e2011-01-18 15:28:36 -0600513 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600514 // Accquire a partial wakelock to prevent the device from sleeping. Note
515 // we never release this wakelock as we will be restarted after the device
516 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700517 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700518 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700519 Log.d(TAG, "Acquiring wakelock.");
520 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
521 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
522 mWakeLock.acquire();
523 }
Jason parks35933812011-01-21 15:48:20 -0600524
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700525 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700526 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800527 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700528 // Start the first run of progress manually. This method sets up messages to occur at
529 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600530 updateProgress();
531 }
532
Paul Lawrence384d8e52014-08-08 15:11:36 -0700533 /**
534 * Show factory reset screen allowing the user to reset their phone when
535 * there is nothing else we can do
536 * @param corrupt true if userdata is corrupt, false if encryption failed
537 * partway through
538 */
539 private void showFactoryReset(boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800540 // Hide the encryption-bot to make room for the "factory reset" button
541 findViewById(R.id.encroid).setVisibility(View.GONE);
542
543 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700544 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800545 button.setVisibility(View.VISIBLE);
546 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700547 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800548 public void onClick(View v) {
549 // Factory reset the device.
550 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
551 }
552 });
553
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700554 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700555 if (corrupt) {
556 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
557 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
558 } else {
559 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
560 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
561 }
Andy Stadler13d62042011-01-31 19:21:37 -0800562
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700563 final View view = findViewById(R.id.bottom_divider);
564 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700565 if (view != null) {
566 view.setVisibility(View.VISIBLE);
567 }
Andy Stadler13d62042011-01-31 19:21:37 -0800568 }
569
Jason parksf8217302011-01-26 13:11:42 -0600570 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700571 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600572
Ben Komalo0e666092011-09-01 15:42:00 -0700573 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700574 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800575 return;
576 }
577
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700578 // Get status as percentage first
579 CharSequence status = getText(R.string.crypt_keeper_setup_description);
580 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600581 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700582 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700583 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600584 } catch (Exception e) {
585 Log.w(TAG, "Error parsing progress: " + e.toString());
586 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700587 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600588
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700589 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700590 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700591 try {
592 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
593 int time = Integer.parseInt(timeProperty);
594 if (time >= 0) {
595 // Round up to multiple of 10 - this way display is less jerky
596 time = (time + 9) / 10 * 10;
597 progress = DateUtils.formatElapsedTime(time);
598 status = getText(R.string.crypt_keeper_setup_time_remaining);
599 }
600 } catch (Exception e) {
601 // Will happen if no time etc - show percentage
602 }
603
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700604 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700605 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700606 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700607 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700608
609 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700610 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700611 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600612 }
613
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700614 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600615 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700616 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800617
Jason parksf8217302011-01-26 13:11:42 -0600618 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700619 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700620 if (mPasswordEntry != null) {
621 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700622 final InputMethodManager imm = (InputMethodManager) getSystemService(
623 Context.INPUT_METHOD_SERVICE);
624 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700625 setBackFunctionality(true);
626 }
627 if (mLockPatternView != null) {
628 mLockPatternView.setEnabled(true);
629 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700630 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600631 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700632 // Disable the password entry and back presses.
633 if (mPasswordEntry != null) {
634 mPasswordEntry.setEnabled(false);
635 }
636 if (mLockPatternView != null) {
637 mLockPatternView.setEnabled(false);
638 }
639
Andy Stadler13d62042011-01-31 19:21:37 -0800640 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700641 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800642
Jason parksf8217302011-01-26 13:11:42 -0600643 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700644 mHandler.removeMessages(MESSAGE_COOLDOWN);
645 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600646 }
Jason parksec5a45e2011-01-18 15:28:36 -0600647 }
Jason parks35933812011-01-21 15:48:20 -0600648
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800649 /**
650 * Sets the back status: enabled or disabled according to the parameter.
651 * @param isEnabled true if back is enabled, false otherwise.
652 */
653 private final void setBackFunctionality(boolean isEnabled) {
654 mIgnoreBack = !isEnabled;
655 if (isEnabled) {
656 mStatusBar.disable(sWidgetsToDisable);
657 } else {
658 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
659 }
660 }
661
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800662 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700663 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800664
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700665 @Override
666 public void onPatternStart() {
667 mLockPatternView.removeCallbacks(mClearPatternRunnable);
668 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800669
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700670 @Override
671 public void onPatternCleared() {
672 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800673
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700674 @Override
675 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
676 mLockPatternView.setEnabled(false);
677 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
678 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800679
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700680 @Override
681 public void onPatternCellAdded(List<Cell> pattern) {
682 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800683 };
684
685 private void passwordEntryInit() {
686 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600687 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800688 if (mPasswordEntry != null){
689 mPasswordEntry.setOnEditorActionListener(this);
690 mPasswordEntry.requestFocus();
691 // Become quiet when the user interacts with the Edit text screen.
692 mPasswordEntry.setOnKeyListener(this);
693 mPasswordEntry.setOnTouchListener(this);
694 mPasswordEntry.addTextChangedListener(this);
695 }
696
697 // Pattern case
698 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
699 if (mLockPatternView != null) {
700 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
701 }
Jason parks35933812011-01-21 15:48:20 -0600702
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700703 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700704 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700705 final View emergencyCall = findViewById(R.id.emergencyCallButton);
706 if (emergencyCall != null) {
707 Log.d(TAG, "Removing the emergency Call button");
708 emergencyCall.setVisibility(View.GONE);
709 }
710 }
711
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700712 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700713 final InputMethodManager imm = (InputMethodManager) getSystemService(
714 Context.INPUT_METHOD_SERVICE);
715 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
716 imeSwitcher.setVisibility(View.VISIBLE);
717 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700718 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700719 public void onClick(View v) {
720 imm.showInputMethodPicker();
721 }
722 });
Jason parks00046d62011-06-13 17:38:45 -0500723 }
David Brown8373b452011-06-20 12:38:45 -0700724
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700725 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
726 // is completely non-functional, and we want the user to notice the device and enter a
727 // password.
728 if (mWakeLock == null) {
729 Log.d(TAG, "Acquiring wakelock.");
730 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
731 if (pm != null) {
732 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
733 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700734 // Keep awake for 10 minutes - if the user hasn't been alerted by then
735 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700736 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700737 }
738 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700739
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700740 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
741 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700742 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700743 mHandler.postDelayed(new Runnable() {
744 @Override public void run() {
745 imm.showSoftInputUnchecked(0, null);
746 }
747 }, 0);
748 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700749
David Brown8373b452011-06-20 12:38:45 -0700750 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700751 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700752 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700753 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800754
John Spurlock2c526512014-09-17 12:55:17 -0400755 // Dismiss secure & non-secure keyguards while this screen is showing.
756 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
757 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Jason parks8fd5bc92011-01-12 16:03:31 -0600758 }
759
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700760 /**
761 * Method adapted from com.android.inputmethod.latin.Utils
762 *
763 * @param imm The input method manager
764 * @param shouldIncludeAuxiliarySubtypes
765 * @return true if we have multiple IMEs to choose from
766 */
767 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
768 final boolean shouldIncludeAuxiliarySubtypes) {
769 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
770
771 // Number of the filtered IMEs
772 int filteredImisCount = 0;
773
774 for (InputMethodInfo imi : enabledImis) {
775 // We can return true immediately after we find two or more filtered IMEs.
776 if (filteredImisCount > 1) return true;
777 final List<InputMethodSubtype> subtypes =
778 imm.getEnabledInputMethodSubtypeList(imi, true);
779 // IMEs that have no subtypes should be counted.
780 if (subtypes.isEmpty()) {
781 ++filteredImisCount;
782 continue;
783 }
784
785 int auxCount = 0;
786 for (InputMethodSubtype subtype : subtypes) {
787 if (subtype.isAuxiliary()) {
788 ++auxCount;
789 }
790 }
791 final int nonAuxCount = subtypes.size() - auxCount;
792
793 // IMEs that have one or more non-auxiliary subtypes should be counted.
794 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
795 // subtypes should be counted as well.
796 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
797 ++filteredImisCount;
798 continue;
799 }
800 }
801
802 return filteredImisCount > 1
803 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
804 // input method subtype (The current IME should be LatinIME.)
805 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
806 }
807
Jason parks8fd5bc92011-01-12 16:03:31 -0600808 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700809 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600810 if (service != null) {
811 return IMountService.Stub.asInterface(service);
812 }
813 return null;
814 }
815
816 @Override
817 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500818 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600819 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700820 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600821
Jason parksec5a45e2011-01-18 15:28:36 -0600822 if (TextUtils.isEmpty(password)) {
823 return true;
824 }
Jason parks35933812011-01-21 15:48:20 -0600825
Jason parks8fd5bc92011-01-12 16:03:31 -0600826 // Now that we have the password clear the password field.
827 v.setText(null);
828
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700829 // Disable the password entry and back keypress while checking the password. These
830 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600831 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800832 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600833
Ben Komalo04606752011-08-18 14:50:26 -0700834 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600835 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600836
Jason parks8fd5bc92011-01-12 16:03:31 -0600837 return true;
838 }
839 return false;
840 }
David Brown8373b452011-06-20 12:38:45 -0700841
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700842 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700843 * Set airplane mode on the device if it isn't an LTE device.
844 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
845 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
846 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
847 * both these problems, we turn the radio off. However, on certain networks turning on and
848 * off the radio takes a long time. In such cases, we are better off leaving the radio
849 * running so the latency of an E911 call is short.
850 * The behavior after this is:
851 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
852 * airplane mode and restart the radio.
853 * 2. Full boot: we read the persistent settings from the previous boot and restore the
854 * radio to whatever it was before it restarted. This also happens when rebooting a
855 * phone that has no encryption.
856 */
857 private final void setAirplaneModeIfNecessary() {
858 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700859 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700860 if (!isLteDevice) {
861 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700862 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700863 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
864 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700865 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700866 }
867 }
868
869 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700870 * Code to update the state of, and handle clicks from, the "Emergency call" button.
871 *
872 * This code is mostly duplicated from the corresponding code in
873 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
874 */
David Brown8373b452011-06-20 12:38:45 -0700875 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700876 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700877 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700878 if (emergencyCall == null)
879 return;
David Brown8373b452011-06-20 12:38:45 -0700880
881 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700882 emergencyCall.setVisibility(View.VISIBLE);
883 emergencyCall.setOnClickListener(new View.OnClickListener() {
884 @Override
885
David Brown8373b452011-06-20 12:38:45 -0700886 public void onClick(View v) {
887 takeEmergencyCallAction();
888 }
889 });
890 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700891 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700892 return;
893 }
894
David Brown8373b452011-06-20 12:38:45 -0700895 int textId;
Tyler Gunn3e71b192014-09-10 15:21:33 -0700896 if (getTelecomManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700897 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700898 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700899 } else {
900 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700901 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700902 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700903 }
904
905 private boolean isEmergencyCallCapable() {
906 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
907 }
908
909 private void takeEmergencyCallAction() {
Tyler Gunn3e71b192014-09-10 15:21:33 -0700910 TelecomManager telecomManager = getTelecomManager();
911 if (telecomManager.isInCall()) {
912 telecomManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700913 } else {
914 launchEmergencyDialer();
915 }
916 }
917
David Brown8373b452011-06-20 12:38:45 -0700918
919 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700920 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700921 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
922 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700923 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700924 startActivity(intent);
925 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700926
Santos Cordon3afbdf02014-05-29 21:48:16 -0700927 private TelephonyManager getTelephonyManager() {
928 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
929 }
930
Tyler Gunn3e71b192014-09-10 15:21:33 -0700931 private TelecomManager getTelecomManager() {
932 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700933 }
934
Vikram Aggarwald1147252012-05-08 13:52:30 -0700935 /**
936 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
937 */
938 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700939 mNotificationCountdown = 20;
940 }
941
942 @Override
943 public boolean onKey(View v, int keyCode, KeyEvent event) {
944 delayAudioNotification();
945 return false;
946 }
947
948 @Override
949 public boolean onTouch(View v, MotionEvent event) {
950 delayAudioNotification();
951 return false;
952 }
953
954 @Override
955 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
956 return;
957 }
958
959 @Override
960 public void onTextChanged(CharSequence s, int start, int before, int count) {
961 delayAudioNotification();
962 }
963
964 @Override
965 public void afterTextChanged(Editable s) {
966 return;
967 }
David Brown8373b452011-06-20 12:38:45 -0700968}