blob: b82c02a2d7bff70cf9c0adea5ba486566215c016 [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;
Santos Cordon35230e92014-07-07 16:25:10 -070040
41import android.phone.PhoneManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070042import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070043import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060045import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070046import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070047import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060048import android.util.Log;
49import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070050import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080051import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080052import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080053import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070054import android.view.View.OnKeyListener;
55import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060056import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070057import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060058import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070059import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080060import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060061import android.widget.EditText;
62import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060063import android.widget.TextView;
64
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080065import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070066import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070067import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080068import com.android.internal.widget.LockPatternUtils;
69import com.android.internal.widget.LockPatternView;
70import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070071
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070072import static com.android.internal.widget.LockPatternView.DisplayMode;
73
Ben Komalo9fcb6a72011-08-26 14:40:18 -070074import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070075
76/**
77 * Settings screens to show the UI flows for encrypting/decrypting the device.
78 *
79 * This may be started via adb for debugging the UI layout, without having to go through
80 * encryption flows everytime. It should be noted that starting the activity in this manner
81 * is only useful for verifying UI-correctness - the behavior will not be identical.
82 * <pre>
83 * $ adb shell pm enable com.android.settings/.CryptKeeper
84 * $ adb shell am start \
85 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
86 * -n com.android.settings/.CryptKeeper
87 * </pre>
88 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070089public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
90 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060091 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060092
Jason parks8fd5bc92011-01-12 16:03:31 -060093 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070094 /** Message sent to us to indicate encryption update progress. */
95 private static final int MESSAGE_UPDATE_PROGRESS = 1;
96 /** Message sent to us to cool-down (waste user's time between password attempts) */
97 private static final int MESSAGE_COOLDOWN = 2;
98 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
99 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -0600100
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700101 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600102 private static final int MAX_FAILED_ATTEMPTS = 30;
103 private static final int COOL_DOWN_ATTEMPTS = 10;
104 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
105
David Brown8373b452011-06-20 12:38:45 -0700106 // Intent action for launching the Emergency Dialer activity.
107 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
108
Ben Komalo91a2f052011-08-16 17:48:25 -0700109 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
110 private static final String EXTRA_FORCE_VIEW =
111 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
112 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700113 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700114 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700115
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700116 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700117 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700118 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700119 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
120 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700121 /** A flag to indicate when the back event should be ignored */
122 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800123 private int mCooldown;
124 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600125 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800126 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700127 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
128 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700129 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
130 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700131 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800132
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700133 // how long we wait to clear a wrong pattern
134 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
135
136 private Runnable mClearPatternRunnable = new Runnable() {
137 public void run() {
138 mLockPatternView.clearPattern();
139 }
140 };
141
Andy Stadler14997402011-02-01 15:34:59 -0800142 /**
143 * Used to propagate state through configuration changes (e.g. screen rotation)
144 */
145 private static class NonConfigurationInstanceState {
146 final PowerManager.WakeLock wakelock;
147
148 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
149 wakelock = _wakelock;
150 }
151 }
152
Jason parks06c5ff42011-03-01 10:17:40 -0600153 private class DecryptTask extends AsyncTask<String, Void, Integer> {
154 @Override
155 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700156 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600157 try {
158 return service.decryptStorage(params[0]);
159 } catch (Exception e) {
160 Log.e(TAG, "Error while decrypting...", e);
161 return -1;
162 }
163 }
164
165 @Override
166 protected void onPostExecute(Integer failedAttempts) {
167 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700168 // The password was entered successfully. Simply do nothing
169 // and wait for the service restart to switch to surfacefligner
Jason parks06c5ff42011-03-01 10:17:40 -0600170 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
171 // Factory reset the device.
172 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parks06c5ff42011-03-01 10:17:40 -0600173 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700174 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700175 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700176 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
177 mLockPatternView.removeCallbacks(mClearPatternRunnable);
178 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
179 }
180 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
181 mCooldown = COOL_DOWN_INTERVAL;
182 cooldown();
183 } else {
184 final TextView status = (TextView) findViewById(R.id.status);
185 status.setText(R.string.try_again);
186 if (mLockPatternView != null) {
187 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
188 }
189 // Reenable the password entry
190 if (mPasswordEntry != null) {
191 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700192 final InputMethodManager imm = (InputMethodManager) getSystemService(
193 Context.INPUT_METHOD_SERVICE);
194 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700195 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700196 }
197 if (mLockPatternView != null) {
198 mLockPatternView.setEnabled(true);
199 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700200 }
Jason parks06c5ff42011-03-01 10:17:40 -0600201 }
202 }
203 }
Jason parks75c085e2011-02-10 14:03:47 -0600204
Ben Komalo0e666092011-09-01 15:42:00 -0700205 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
206 @Override
207 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700208 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700209 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700210 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700211 int state = service.getEncryptionState();
212 if (state == IMountService.ENCRYPTION_STATE_NONE) {
213 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
214 return true; // Unexpected, but fine, I guess...
215 }
216 return state == IMountService.ENCRYPTION_STATE_OK;
217 } catch (RemoteException e) {
218 Log.w(TAG, "Unable to get encryption state properly");
219 return true;
220 }
221 }
222
223 @Override
224 protected void onPostExecute(Boolean result) {
225 mValidationComplete = true;
226 if (Boolean.FALSE.equals(result)) {
227 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
228 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700229 } else {
230 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700231 }
232 setupUi();
233 }
234 }
235
Ben Komalo91a2f052011-08-16 17:48:25 -0700236 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600237 @Override
238 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600239 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700240 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600241 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600242 break;
Jason parks35933812011-01-21 15:48:20 -0600243
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700244 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600245 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600246 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700247
248 case MESSAGE_NOTIFY:
249 notifyUser();
250 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600251 }
252 }
253 };
Jason parks35933812011-01-21 15:48:20 -0600254
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700255 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800256 /** The status bar where back/home/recent buttons are shown. */
257 private StatusBarManager mStatusBar;
258
259 /** All the widgets to disable in the status bar */
260 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
261 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
262 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
263 | StatusBarManager.DISABLE_SYSTEM_INFO
264 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700265 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800266 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700267
Ben Komalo91a2f052011-08-16 17:48:25 -0700268 /** @return whether or not this Activity was started for debugging the UI only. */
269 private boolean isDebugView() {
270 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
271 }
272
273 /** @return whether or not this Activity was started for debugging the specific UI view only. */
274 private boolean isDebugView(String viewType /* non-nullable */) {
275 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
276 }
277
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700278 /**
279 * Notify the user that we are awaiting input. Currently this sends an audio alert.
280 */
281 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700282 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700283 --mNotificationCountdown;
284 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700285 try {
286 // Play the standard keypress sound at full volume. This should be available on
287 // every device. We cannot play a ringtone here because media services aren't
288 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
289 // on tablet devices. The idea is to alert the user that something is needed: this
290 // does not have to be pleasing.
291 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
292 } catch (Exception e) {
293 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
294 }
295 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700296 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700297 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700298 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700299
300 if (mWakeLock.isHeld()) {
301 if (mReleaseWakeLockCountdown > 0) {
302 --mReleaseWakeLockCountdown;
303 } else {
304 mWakeLock.release();
305 }
306 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700307 }
308
309 /**
310 * Ignore back events after the user has entered the decrypt screen and while the device is
311 * encrypting.
312 */
313 @Override
314 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800315 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700316 if (mIgnoreBack)
317 return;
318 super.onBackPressed();
319 }
320
Jason parks8fd5bc92011-01-12 16:03:31 -0600321 @Override
322 public void onCreate(Bundle savedInstanceState) {
323 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600324
Andy Stadler95974062011-02-01 17:35:20 -0800325 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700326 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700327 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600328 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600329 PackageManager pm = getPackageManager();
330 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700331 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
332 PackageManager.DONT_KILL_APP);
333 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700334 // want to be running, so need to finish this activity. We can count
335 // on the activity manager re-launching the new home app upon finishing
336 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700337 // NOTE: This is really grungy. I think it would be better for the
338 // activity manager to explicitly launch the crypt keeper instead of
339 // home in the situation where we need to decrypt the device
340 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600341 return;
342 }
Jason parks35933812011-01-21 15:48:20 -0600343
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700344 try {
345 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
346 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
347 }
348 } catch (NotFoundException e) {
349 }
350
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700351 // Disable the status bar, but do NOT disable back because the user needs a way to go
352 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800353 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
354 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800355
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700356 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700357 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800358 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700359 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800360 if (lastInstance instanceof NonConfigurationInstanceState) {
361 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
362 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700363 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800364 }
Jason parksec5a45e2011-01-18 15:28:36 -0600365 }
Jason parks35933812011-01-21 15:48:20 -0600366
Andy Stadler95974062011-02-01 17:35:20 -0800367 /**
368 * Note, we defer the state check and screen setup to onStart() because this will be
369 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
370 * especially important if we were to lose the wakelock for any reason.
371 */
372 @Override
373 public void onStart() {
374 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700375 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700376 }
377
378 /**
379 * Initializes the UI based on the current state of encryption.
380 * This is idempotent - calling repeatedly will simply re-initialize the UI.
381 */
382 private void setupUi() {
383 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
384 setContentView(R.layout.crypt_keeper_progress);
385 showFactoryReset();
386 return;
387 }
388
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700389 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700390 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800391 setContentView(R.layout.crypt_keeper_progress);
392 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700393 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700394 new AsyncTask<Void, Void, Void>() {
395 int type = StorageManager.CRYPT_TYPE_PASSWORD;
396 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800397
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700398 @Override
399 public Void doInBackground(Void... v) {
400 try {
401 final IMountService service = getMountService();
402 type = service.getPasswordType();
403 owner_info = service.getField("OwnerInfo");
404 } catch (Exception e) {
405 Log.e(TAG, "Error calling mount service " + e);
406 }
407
408 return null;
409 }
410
411 @Override
412 public void onPostExecute(java.lang.Void v) {
413 if(type == StorageManager.CRYPT_TYPE_PIN) {
414 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700415 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700416 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
417 setContentView(R.layout.crypt_keeper_pattern_entry);
418 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700419 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700420 } else {
421 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700422 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700423 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700424 final TextView status = (TextView) findViewById(R.id.status);
425 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700426
Paul Lawrence5a70f052014-06-13 11:09:55 -0700427 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
428 ownerInfo.setText(owner_info);
429 ownerInfo.setSelected(true); // Required for marquee'ing to work
430
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700431 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700432
433 if (mCooldown > 0) {
434 setBackFunctionality(false);
435 cooldown(); // in case we are cooling down and coming back from emergency dialler
436 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700437 }
438 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700439 } else if (!mValidationRequested) {
440 // We're supposed to be encrypted, but no validation has been done.
441 new ValidationTask().execute((Void[]) null);
442 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800443 }
444 }
445
Jason parksf8217302011-01-26 13:11:42 -0600446 @Override
447 public void onStop() {
448 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700449 mHandler.removeMessages(MESSAGE_COOLDOWN);
450 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
451 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800452 }
453
454 /**
455 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
456 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
457 * mWakeLock so the subsequent call to onDestroy does not release it.
458 */
459 @Override
460 public Object onRetainNonConfigurationInstance() {
461 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700462 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800463 mWakeLock = null;
464 return state;
465 }
466
467 @Override
468 public void onDestroy() {
469 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600470
471 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700472 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600473 mWakeLock.release();
474 mWakeLock = null;
475 }
476 }
477
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700478 /**
479 * Start encrypting the device.
480 */
Jason parksec5a45e2011-01-18 15:28:36 -0600481 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600482 // Accquire a partial wakelock to prevent the device from sleeping. Note
483 // we never release this wakelock as we will be restarted after the device
484 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700485 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700486 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700487 Log.d(TAG, "Acquiring wakelock.");
488 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
489 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
490 mWakeLock.acquire();
491 }
Jason parks35933812011-01-21 15:48:20 -0600492
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700493 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700494 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800495 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700496 // Start the first run of progress manually. This method sets up messages to occur at
497 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600498 updateProgress();
499 }
500
Andy Stadler13d62042011-01-31 19:21:37 -0800501 private void showFactoryReset() {
502 // Hide the encryption-bot to make room for the "factory reset" button
503 findViewById(R.id.encroid).setVisibility(View.GONE);
504
505 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700506 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800507 button.setVisibility(View.VISIBLE);
508 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700509 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800510 public void onClick(View v) {
511 // Factory reset the device.
512 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
513 }
514 });
515
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700516 // Alert the user of the failure.
517 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
518 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800519
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700520 final View view = findViewById(R.id.bottom_divider);
521 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700522 if (view != null) {
523 view.setVisibility(View.VISIBLE);
524 }
Andy Stadler13d62042011-01-31 19:21:37 -0800525 }
526
Jason parksf8217302011-01-26 13:11:42 -0600527 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700528 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600529
Ben Komalo0e666092011-09-01 15:42:00 -0700530 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800531 showFactoryReset();
532 return;
533 }
534
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700535 // Get status as percentage first
536 CharSequence status = getText(R.string.crypt_keeper_setup_description);
537 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600538 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700539 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700540 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600541 } catch (Exception e) {
542 Log.w(TAG, "Error parsing progress: " + e.toString());
543 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700544 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600545
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700546 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700547 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700548 try {
549 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
550 int time = Integer.parseInt(timeProperty);
551 if (time >= 0) {
552 // Round up to multiple of 10 - this way display is less jerky
553 time = (time + 9) / 10 * 10;
554 progress = DateUtils.formatElapsedTime(time);
555 status = getText(R.string.crypt_keeper_setup_time_remaining);
556 }
557 } catch (Exception e) {
558 // Will happen if no time etc - show percentage
559 }
560
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700561 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700562 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700563 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700564 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700565
566 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700567 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700568 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600569 }
570
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700571 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600572 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700573 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800574
Jason parksf8217302011-01-26 13:11:42 -0600575 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700576 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700577 if (mPasswordEntry != null) {
578 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700579 final InputMethodManager imm = (InputMethodManager) getSystemService(
580 Context.INPUT_METHOD_SERVICE);
581 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700582 setBackFunctionality(true);
583 }
584 if (mLockPatternView != null) {
585 mLockPatternView.setEnabled(true);
586 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700587 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600588 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700589 // Disable the password entry and back presses.
590 if (mPasswordEntry != null) {
591 mPasswordEntry.setEnabled(false);
592 }
593 if (mLockPatternView != null) {
594 mLockPatternView.setEnabled(false);
595 }
596
Andy Stadler13d62042011-01-31 19:21:37 -0800597 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700598 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800599
Jason parksf8217302011-01-26 13:11:42 -0600600 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700601 mHandler.removeMessages(MESSAGE_COOLDOWN);
602 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600603 }
Jason parksec5a45e2011-01-18 15:28:36 -0600604 }
Jason parks35933812011-01-21 15:48:20 -0600605
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800606 /**
607 * Sets the back status: enabled or disabled according to the parameter.
608 * @param isEnabled true if back is enabled, false otherwise.
609 */
610 private final void setBackFunctionality(boolean isEnabled) {
611 mIgnoreBack = !isEnabled;
612 if (isEnabled) {
613 mStatusBar.disable(sWidgetsToDisable);
614 } else {
615 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
616 }
617 }
618
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800619 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700620 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800621
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700622 @Override
623 public void onPatternStart() {
624 mLockPatternView.removeCallbacks(mClearPatternRunnable);
625 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800626
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700627 @Override
628 public void onPatternCleared() {
629 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800630
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700631 @Override
632 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
633 mLockPatternView.setEnabled(false);
634 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
635 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800636
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700637 @Override
638 public void onPatternCellAdded(List<Cell> pattern) {
639 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800640 };
641
642 private void passwordEntryInit() {
643 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600644 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800645 if (mPasswordEntry != null){
646 mPasswordEntry.setOnEditorActionListener(this);
647 mPasswordEntry.requestFocus();
648 // Become quiet when the user interacts with the Edit text screen.
649 mPasswordEntry.setOnKeyListener(this);
650 mPasswordEntry.setOnTouchListener(this);
651 mPasswordEntry.addTextChangedListener(this);
652 }
653
654 // Pattern case
655 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
656 if (mLockPatternView != null) {
657 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
658 }
Jason parks35933812011-01-21 15:48:20 -0600659
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700660 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700661 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700662 final View emergencyCall = findViewById(R.id.emergencyCallButton);
663 if (emergencyCall != null) {
664 Log.d(TAG, "Removing the emergency Call button");
665 emergencyCall.setVisibility(View.GONE);
666 }
667 }
668
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700669 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700670 final InputMethodManager imm = (InputMethodManager) getSystemService(
671 Context.INPUT_METHOD_SERVICE);
672 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
673 imeSwitcher.setVisibility(View.VISIBLE);
674 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700675 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700676 public void onClick(View v) {
677 imm.showInputMethodPicker();
678 }
679 });
Jason parks00046d62011-06-13 17:38:45 -0500680 }
David Brown8373b452011-06-20 12:38:45 -0700681
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700682 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
683 // is completely non-functional, and we want the user to notice the device and enter a
684 // password.
685 if (mWakeLock == null) {
686 Log.d(TAG, "Acquiring wakelock.");
687 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
688 if (pm != null) {
689 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
690 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700691 // Keep awake for 10 minutes - if the user hasn't been alerted by then
692 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700693 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700694 }
695 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700696
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700697 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
698 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700699 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700700 mHandler.postDelayed(new Runnable() {
701 @Override public void run() {
702 imm.showSoftInputUnchecked(0, null);
703 }
704 }, 0);
705 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700706
David Brown8373b452011-06-20 12:38:45 -0700707 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700708 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700709 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700710 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800711
712 // Dismiss keyguard while this screen is showing.
713 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600714 }
715
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700716 /**
717 * Method adapted from com.android.inputmethod.latin.Utils
718 *
719 * @param imm The input method manager
720 * @param shouldIncludeAuxiliarySubtypes
721 * @return true if we have multiple IMEs to choose from
722 */
723 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
724 final boolean shouldIncludeAuxiliarySubtypes) {
725 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
726
727 // Number of the filtered IMEs
728 int filteredImisCount = 0;
729
730 for (InputMethodInfo imi : enabledImis) {
731 // We can return true immediately after we find two or more filtered IMEs.
732 if (filteredImisCount > 1) return true;
733 final List<InputMethodSubtype> subtypes =
734 imm.getEnabledInputMethodSubtypeList(imi, true);
735 // IMEs that have no subtypes should be counted.
736 if (subtypes.isEmpty()) {
737 ++filteredImisCount;
738 continue;
739 }
740
741 int auxCount = 0;
742 for (InputMethodSubtype subtype : subtypes) {
743 if (subtype.isAuxiliary()) {
744 ++auxCount;
745 }
746 }
747 final int nonAuxCount = subtypes.size() - auxCount;
748
749 // IMEs that have one or more non-auxiliary subtypes should be counted.
750 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
751 // subtypes should be counted as well.
752 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
753 ++filteredImisCount;
754 continue;
755 }
756 }
757
758 return filteredImisCount > 1
759 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
760 // input method subtype (The current IME should be LatinIME.)
761 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
762 }
763
Jason parks8fd5bc92011-01-12 16:03:31 -0600764 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700765 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600766 if (service != null) {
767 return IMountService.Stub.asInterface(service);
768 }
769 return null;
770 }
771
772 @Override
773 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500774 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600775 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700776 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600777
Jason parksec5a45e2011-01-18 15:28:36 -0600778 if (TextUtils.isEmpty(password)) {
779 return true;
780 }
Jason parks35933812011-01-21 15:48:20 -0600781
Jason parks8fd5bc92011-01-12 16:03:31 -0600782 // Now that we have the password clear the password field.
783 v.setText(null);
784
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700785 // Disable the password entry and back keypress while checking the password. These
786 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600787 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800788 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600789
Ben Komalo04606752011-08-18 14:50:26 -0700790 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600791 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600792
Jason parks8fd5bc92011-01-12 16:03:31 -0600793 return true;
794 }
795 return false;
796 }
David Brown8373b452011-06-20 12:38:45 -0700797
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700798 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700799 * Set airplane mode on the device if it isn't an LTE device.
800 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
801 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
802 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
803 * both these problems, we turn the radio off. However, on certain networks turning on and
804 * off the radio takes a long time. In such cases, we are better off leaving the radio
805 * running so the latency of an E911 call is short.
806 * The behavior after this is:
807 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
808 * airplane mode and restart the radio.
809 * 2. Full boot: we read the persistent settings from the previous boot and restore the
810 * radio to whatever it was before it restarted. This also happens when rebooting a
811 * phone that has no encryption.
812 */
813 private final void setAirplaneModeIfNecessary() {
814 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700815 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700816 if (!isLteDevice) {
817 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700818 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700819 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
820 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700821 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700822 }
823 }
824
825 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700826 * Code to update the state of, and handle clicks from, the "Emergency call" button.
827 *
828 * This code is mostly duplicated from the corresponding code in
829 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
830 */
David Brown8373b452011-06-20 12:38:45 -0700831 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700832 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700833 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700834 if (emergencyCall == null)
835 return;
David Brown8373b452011-06-20 12:38:45 -0700836
837 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700838 emergencyCall.setVisibility(View.VISIBLE);
839 emergencyCall.setOnClickListener(new View.OnClickListener() {
840 @Override
841
David Brown8373b452011-06-20 12:38:45 -0700842 public void onClick(View v) {
843 takeEmergencyCallAction();
844 }
845 });
846 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700847 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700848 return;
849 }
850
David Brown8373b452011-06-20 12:38:45 -0700851 int textId;
Santos Cordon35230e92014-07-07 16:25:10 -0700852 if (getPhoneManager().isInAPhoneCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700853 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700854 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700855 } else {
856 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700857 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700858 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700859 }
860
861 private boolean isEmergencyCallCapable() {
862 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
863 }
864
865 private void takeEmergencyCallAction() {
Santos Cordon35230e92014-07-07 16:25:10 -0700866 PhoneManager phoneManager = getPhoneManager();
867 if (phoneManager.isInAPhoneCall()) {
868 phoneManager.showCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700869 } else {
870 launchEmergencyDialer();
871 }
872 }
873
David Brown8373b452011-06-20 12:38:45 -0700874
875 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700876 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
878 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700879 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700880 startActivity(intent);
881 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700882
Santos Cordon3afbdf02014-05-29 21:48:16 -0700883 private TelephonyManager getTelephonyManager() {
884 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
885 }
886
Santos Cordon35230e92014-07-07 16:25:10 -0700887 private PhoneManager getPhoneManager() {
888 return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
889 }
890
Vikram Aggarwald1147252012-05-08 13:52:30 -0700891 /**
892 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
893 */
894 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700895 mNotificationCountdown = 20;
896 }
897
898 @Override
899 public boolean onKey(View v, int keyCode, KeyEvent event) {
900 delayAudioNotification();
901 return false;
902 }
903
904 @Override
905 public boolean onTouch(View v, MotionEvent event) {
906 delayAudioNotification();
907 return false;
908 }
909
910 @Override
911 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
912 return;
913 }
914
915 @Override
916 public void onTextChanged(CharSequence s, int start, int before, int count) {
917 delayAudioNotification();
918 }
919
920 @Override
921 public void afterTextChanged(Editable s) {
922 return;
923 }
David Brown8373b452011-06-20 12:38:45 -0700924}