blob: 0ddeb50b71ac4184c023e7ca958fd5f7fa281f67 [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;
Jason parks8fd5bc92011-01-12 16:03:31 -060024import android.content.pm.PackageManager;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070025import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060026import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060027import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060028import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060031import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070032import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060033import android.os.ServiceManager;
34import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070035import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060036import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080037import android.os.storage.StorageManager;
Santos Cordon35230e92014-07-07 16:25:10 -070038
39import android.phone.PhoneManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070040import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070041import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070042import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060043import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070045import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060046import android.util.Log;
47import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070048import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080049import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080050import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080051import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070052import android.view.View.OnKeyListener;
53import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060054import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070055import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060056import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070057import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080058import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060059import android.widget.EditText;
60import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060061import android.widget.TextView;
62
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080063import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070064import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070065import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080066import com.android.internal.widget.LockPatternUtils;
67import com.android.internal.widget.LockPatternView;
68import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070069
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070070import static com.android.internal.widget.LockPatternView.DisplayMode;
71
Ben Komalo9fcb6a72011-08-26 14:40:18 -070072import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070073
74/**
75 * Settings screens to show the UI flows for encrypting/decrypting the device.
76 *
77 * This may be started via adb for debugging the UI layout, without having to go through
78 * encryption flows everytime. It should be noted that starting the activity in this manner
79 * is only useful for verifying UI-correctness - the behavior will not be identical.
80 * <pre>
81 * $ adb shell pm enable com.android.settings/.CryptKeeper
82 * $ adb shell am start \
83 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
84 * -n com.android.settings/.CryptKeeper
85 * </pre>
86 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070087public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
88 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060089 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060090
Jason parks8fd5bc92011-01-12 16:03:31 -060091 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070092 /** Message sent to us to indicate encryption update progress. */
93 private static final int MESSAGE_UPDATE_PROGRESS = 1;
94 /** Message sent to us to cool-down (waste user's time between password attempts) */
95 private static final int MESSAGE_COOLDOWN = 2;
96 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
97 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060098
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070099 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600100 private static final int MAX_FAILED_ATTEMPTS = 30;
101 private static final int COOL_DOWN_ATTEMPTS = 10;
102 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
103
David Brown8373b452011-06-20 12:38:45 -0700104 // Intent action for launching the Emergency Dialer activity.
105 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
106
Ben Komalo91a2f052011-08-16 17:48:25 -0700107 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
108 private static final String EXTRA_FORCE_VIEW =
109 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
110 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700111 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700112 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700113
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700114 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700115 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700116 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700117 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
118 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700119 /** A flag to indicate when the back event should be ignored */
120 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800121 private int mCooldown;
122 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600123 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800124 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700125 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
126 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700127 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
128 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700129 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800130
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700131 // how long we wait to clear a wrong pattern
132 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
133
134 private Runnable mClearPatternRunnable = new Runnable() {
135 public void run() {
136 mLockPatternView.clearPattern();
137 }
138 };
139
Andy Stadler14997402011-02-01 15:34:59 -0800140 /**
141 * Used to propagate state through configuration changes (e.g. screen rotation)
142 */
143 private static class NonConfigurationInstanceState {
144 final PowerManager.WakeLock wakelock;
145
146 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
147 wakelock = _wakelock;
148 }
149 }
150
Jason parks06c5ff42011-03-01 10:17:40 -0600151 private class DecryptTask extends AsyncTask<String, Void, Integer> {
152 @Override
153 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700154 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600155 try {
156 return service.decryptStorage(params[0]);
157 } catch (Exception e) {
158 Log.e(TAG, "Error while decrypting...", e);
159 return -1;
160 }
161 }
162
163 @Override
164 protected void onPostExecute(Integer failedAttempts) {
165 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700166 // The password was entered successfully. Simply do nothing
167 // and wait for the service restart to switch to surfacefligner
Jason parks06c5ff42011-03-01 10:17:40 -0600168 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
169 // Factory reset the device.
170 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parks06c5ff42011-03-01 10:17:40 -0600171 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700172 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700173 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700174 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
175 mLockPatternView.removeCallbacks(mClearPatternRunnable);
176 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
177 }
178 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
179 mCooldown = COOL_DOWN_INTERVAL;
180 cooldown();
181 } else {
182 final TextView status = (TextView) findViewById(R.id.status);
183 status.setText(R.string.try_again);
184 if (mLockPatternView != null) {
185 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
186 }
187 // Reenable the password entry
188 if (mPasswordEntry != null) {
189 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700190 final InputMethodManager imm = (InputMethodManager) getSystemService(
191 Context.INPUT_METHOD_SERVICE);
192 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700193 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700194 }
195 if (mLockPatternView != null) {
196 mLockPatternView.setEnabled(true);
197 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700198 }
Jason parks06c5ff42011-03-01 10:17:40 -0600199 }
200 }
201 }
Jason parks75c085e2011-02-10 14:03:47 -0600202
Ben Komalo0e666092011-09-01 15:42:00 -0700203 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
204 @Override
205 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700206 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700207 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700208 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700209 int state = service.getEncryptionState();
210 if (state == IMountService.ENCRYPTION_STATE_NONE) {
211 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
212 return true; // Unexpected, but fine, I guess...
213 }
214 return state == IMountService.ENCRYPTION_STATE_OK;
215 } catch (RemoteException e) {
216 Log.w(TAG, "Unable to get encryption state properly");
217 return true;
218 }
219 }
220
221 @Override
222 protected void onPostExecute(Boolean result) {
223 mValidationComplete = true;
224 if (Boolean.FALSE.equals(result)) {
225 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
226 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700227 } else {
228 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700229 }
230 setupUi();
231 }
232 }
233
Ben Komalo91a2f052011-08-16 17:48:25 -0700234 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600235 @Override
236 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600237 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700238 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600239 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600240 break;
Jason parks35933812011-01-21 15:48:20 -0600241
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700242 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600243 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600244 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700245
246 case MESSAGE_NOTIFY:
247 notifyUser();
248 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600249 }
250 }
251 };
Jason parks35933812011-01-21 15:48:20 -0600252
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700253 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800254 /** The status bar where back/home/recent buttons are shown. */
255 private StatusBarManager mStatusBar;
256
257 /** All the widgets to disable in the status bar */
258 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
259 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
260 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
261 | StatusBarManager.DISABLE_SYSTEM_INFO
262 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700263 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800264 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700265
Ben Komalo91a2f052011-08-16 17:48:25 -0700266 /** @return whether or not this Activity was started for debugging the UI only. */
267 private boolean isDebugView() {
268 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
269 }
270
271 /** @return whether or not this Activity was started for debugging the specific UI view only. */
272 private boolean isDebugView(String viewType /* non-nullable */) {
273 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
274 }
275
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700276 /**
277 * Notify the user that we are awaiting input. Currently this sends an audio alert.
278 */
279 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700280 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700281 --mNotificationCountdown;
282 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700283 try {
284 // Play the standard keypress sound at full volume. This should be available on
285 // every device. We cannot play a ringtone here because media services aren't
286 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
287 // on tablet devices. The idea is to alert the user that something is needed: this
288 // does not have to be pleasing.
289 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
290 } catch (Exception e) {
291 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
292 }
293 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700294 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700295 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700296 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700297
298 if (mWakeLock.isHeld()) {
299 if (mReleaseWakeLockCountdown > 0) {
300 --mReleaseWakeLockCountdown;
301 } else {
302 mWakeLock.release();
303 }
304 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700305 }
306
307 /**
308 * Ignore back events after the user has entered the decrypt screen and while the device is
309 * encrypting.
310 */
311 @Override
312 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800313 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700314 if (mIgnoreBack)
315 return;
316 super.onBackPressed();
317 }
318
Jason parks8fd5bc92011-01-12 16:03:31 -0600319 @Override
320 public void onCreate(Bundle savedInstanceState) {
321 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600322
Andy Stadler95974062011-02-01 17:35:20 -0800323 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700324 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700325 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600326 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600327 PackageManager pm = getPackageManager();
328 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700329 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
330 PackageManager.DONT_KILL_APP);
331 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700332 // want to be running, so need to finish this activity. We can count
333 // on the activity manager re-launching the new home app upon finishing
334 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700335 // NOTE: This is really grungy. I think it would be better for the
336 // activity manager to explicitly launch the crypt keeper instead of
337 // home in the situation where we need to decrypt the device
338 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600339 return;
340 }
Jason parks35933812011-01-21 15:48:20 -0600341
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700342 // Disable the status bar, but do NOT disable back because the user needs a way to go
343 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800344 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
345 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800346
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700347 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700348 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800349 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700350 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800351 if (lastInstance instanceof NonConfigurationInstanceState) {
352 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
353 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700354 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800355 }
Jason parksec5a45e2011-01-18 15:28:36 -0600356 }
Jason parks35933812011-01-21 15:48:20 -0600357
Andy Stadler95974062011-02-01 17:35:20 -0800358 /**
359 * Note, we defer the state check and screen setup to onStart() because this will be
360 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
361 * especially important if we were to lose the wakelock for any reason.
362 */
363 @Override
364 public void onStart() {
365 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700366 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700367 }
368
369 /**
370 * Initializes the UI based on the current state of encryption.
371 * This is idempotent - calling repeatedly will simply re-initialize the UI.
372 */
373 private void setupUi() {
374 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
375 setContentView(R.layout.crypt_keeper_progress);
376 showFactoryReset();
377 return;
378 }
379
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700380 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700381 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800382 setContentView(R.layout.crypt_keeper_progress);
383 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700384 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700385 new AsyncTask<Void, Void, Void>() {
386 int type = StorageManager.CRYPT_TYPE_PASSWORD;
387 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800388
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700389 @Override
390 public Void doInBackground(Void... v) {
391 try {
392 final IMountService service = getMountService();
393 type = service.getPasswordType();
394 owner_info = service.getField("OwnerInfo");
395 } catch (Exception e) {
396 Log.e(TAG, "Error calling mount service " + e);
397 }
398
399 return null;
400 }
401
402 @Override
403 public void onPostExecute(java.lang.Void v) {
404 if(type == StorageManager.CRYPT_TYPE_PIN) {
405 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700406 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700407 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
408 setContentView(R.layout.crypt_keeper_pattern_entry);
409 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700410 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700411 } else {
412 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700413 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700414 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700415 final TextView status = (TextView) findViewById(R.id.status);
416 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700417
Paul Lawrence5a70f052014-06-13 11:09:55 -0700418 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
419 ownerInfo.setText(owner_info);
420 ownerInfo.setSelected(true); // Required for marquee'ing to work
421
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700422 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700423
424 if (mCooldown > 0) {
425 setBackFunctionality(false);
426 cooldown(); // in case we are cooling down and coming back from emergency dialler
427 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700428 }
429 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700430 } else if (!mValidationRequested) {
431 // We're supposed to be encrypted, but no validation has been done.
432 new ValidationTask().execute((Void[]) null);
433 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800434 }
435 }
436
Jason parksf8217302011-01-26 13:11:42 -0600437 @Override
438 public void onStop() {
439 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700440 mHandler.removeMessages(MESSAGE_COOLDOWN);
441 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
442 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800443 }
444
445 /**
446 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
447 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
448 * mWakeLock so the subsequent call to onDestroy does not release it.
449 */
450 @Override
451 public Object onRetainNonConfigurationInstance() {
452 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700453 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800454 mWakeLock = null;
455 return state;
456 }
457
458 @Override
459 public void onDestroy() {
460 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600461
462 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700463 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600464 mWakeLock.release();
465 mWakeLock = null;
466 }
467 }
468
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700469 /**
470 * Start encrypting the device.
471 */
Jason parksec5a45e2011-01-18 15:28:36 -0600472 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600473 // Accquire a partial wakelock to prevent the device from sleeping. Note
474 // we never release this wakelock as we will be restarted after the device
475 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700476 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700477 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700478 Log.d(TAG, "Acquiring wakelock.");
479 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
480 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
481 mWakeLock.acquire();
482 }
Jason parks35933812011-01-21 15:48:20 -0600483
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700484 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700485 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800486 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700487 // Start the first run of progress manually. This method sets up messages to occur at
488 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600489 updateProgress();
490 }
491
Andy Stadler13d62042011-01-31 19:21:37 -0800492 private void showFactoryReset() {
493 // Hide the encryption-bot to make room for the "factory reset" button
494 findViewById(R.id.encroid).setVisibility(View.GONE);
495
496 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700497 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800498 button.setVisibility(View.VISIBLE);
499 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700500 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800501 public void onClick(View v) {
502 // Factory reset the device.
503 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
504 }
505 });
506
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700507 // Alert the user of the failure.
508 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
509 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800510
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700511 final View view = findViewById(R.id.bottom_divider);
512 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700513 if (view != null) {
514 view.setVisibility(View.VISIBLE);
515 }
Andy Stadler13d62042011-01-31 19:21:37 -0800516 }
517
Jason parksf8217302011-01-26 13:11:42 -0600518 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700519 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600520
Ben Komalo0e666092011-09-01 15:42:00 -0700521 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800522 showFactoryReset();
523 return;
524 }
525
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700526 // Get status as percentage first
527 CharSequence status = getText(R.string.crypt_keeper_setup_description);
528 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600529 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700530 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700531 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600532 } catch (Exception e) {
533 Log.w(TAG, "Error parsing progress: " + e.toString());
534 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700535 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600536
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700537 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700538 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700539 try {
540 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
541 int time = Integer.parseInt(timeProperty);
542 if (time >= 0) {
543 // Round up to multiple of 10 - this way display is less jerky
544 time = (time + 9) / 10 * 10;
545 progress = DateUtils.formatElapsedTime(time);
546 status = getText(R.string.crypt_keeper_setup_time_remaining);
547 }
548 } catch (Exception e) {
549 // Will happen if no time etc - show percentage
550 }
551
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700552 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700553 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700554 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700555 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700556
557 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700558 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700559 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600560 }
561
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700562 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600563 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700564 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800565
Jason parksf8217302011-01-26 13:11:42 -0600566 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700567 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700568 if (mPasswordEntry != null) {
569 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700570 final InputMethodManager imm = (InputMethodManager) getSystemService(
571 Context.INPUT_METHOD_SERVICE);
572 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700573 setBackFunctionality(true);
574 }
575 if (mLockPatternView != null) {
576 mLockPatternView.setEnabled(true);
577 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700578 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600579 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700580 // Disable the password entry and back presses.
581 if (mPasswordEntry != null) {
582 mPasswordEntry.setEnabled(false);
583 }
584 if (mLockPatternView != null) {
585 mLockPatternView.setEnabled(false);
586 }
587
Andy Stadler13d62042011-01-31 19:21:37 -0800588 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700589 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800590
Jason parksf8217302011-01-26 13:11:42 -0600591 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700592 mHandler.removeMessages(MESSAGE_COOLDOWN);
593 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600594 }
Jason parksec5a45e2011-01-18 15:28:36 -0600595 }
Jason parks35933812011-01-21 15:48:20 -0600596
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800597 /**
598 * Sets the back status: enabled or disabled according to the parameter.
599 * @param isEnabled true if back is enabled, false otherwise.
600 */
601 private final void setBackFunctionality(boolean isEnabled) {
602 mIgnoreBack = !isEnabled;
603 if (isEnabled) {
604 mStatusBar.disable(sWidgetsToDisable);
605 } else {
606 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
607 }
608 }
609
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800610 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700611 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800612
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700613 @Override
614 public void onPatternStart() {
615 mLockPatternView.removeCallbacks(mClearPatternRunnable);
616 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800617
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700618 @Override
619 public void onPatternCleared() {
620 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800621
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700622 @Override
623 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
624 mLockPatternView.setEnabled(false);
625 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
626 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800627
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700628 @Override
629 public void onPatternCellAdded(List<Cell> pattern) {
630 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800631 };
632
633 private void passwordEntryInit() {
634 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600635 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800636 if (mPasswordEntry != null){
637 mPasswordEntry.setOnEditorActionListener(this);
638 mPasswordEntry.requestFocus();
639 // Become quiet when the user interacts with the Edit text screen.
640 mPasswordEntry.setOnKeyListener(this);
641 mPasswordEntry.setOnTouchListener(this);
642 mPasswordEntry.addTextChangedListener(this);
643 }
644
645 // Pattern case
646 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
647 if (mLockPatternView != null) {
648 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
649 }
Jason parks35933812011-01-21 15:48:20 -0600650
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700651 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700652 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700653 final View emergencyCall = findViewById(R.id.emergencyCallButton);
654 if (emergencyCall != null) {
655 Log.d(TAG, "Removing the emergency Call button");
656 emergencyCall.setVisibility(View.GONE);
657 }
658 }
659
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700660 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700661 final InputMethodManager imm = (InputMethodManager) getSystemService(
662 Context.INPUT_METHOD_SERVICE);
663 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
664 imeSwitcher.setVisibility(View.VISIBLE);
665 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700666 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700667 public void onClick(View v) {
668 imm.showInputMethodPicker();
669 }
670 });
Jason parks00046d62011-06-13 17:38:45 -0500671 }
David Brown8373b452011-06-20 12:38:45 -0700672
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700673 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
674 // is completely non-functional, and we want the user to notice the device and enter a
675 // password.
676 if (mWakeLock == null) {
677 Log.d(TAG, "Acquiring wakelock.");
678 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
679 if (pm != null) {
680 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
681 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700682 // Keep awake for 10 minutes - if the user hasn't been alerted by then
683 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700684 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700685 }
686 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700687
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700688 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
689 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700690 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700691 mHandler.postDelayed(new Runnable() {
692 @Override public void run() {
693 imm.showSoftInputUnchecked(0, null);
694 }
695 }, 0);
696 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700697
David Brown8373b452011-06-20 12:38:45 -0700698 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700699 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700700 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700701 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800702
703 // Dismiss keyguard while this screen is showing.
704 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600705 }
706
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700707 /**
708 * Method adapted from com.android.inputmethod.latin.Utils
709 *
710 * @param imm The input method manager
711 * @param shouldIncludeAuxiliarySubtypes
712 * @return true if we have multiple IMEs to choose from
713 */
714 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
715 final boolean shouldIncludeAuxiliarySubtypes) {
716 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
717
718 // Number of the filtered IMEs
719 int filteredImisCount = 0;
720
721 for (InputMethodInfo imi : enabledImis) {
722 // We can return true immediately after we find two or more filtered IMEs.
723 if (filteredImisCount > 1) return true;
724 final List<InputMethodSubtype> subtypes =
725 imm.getEnabledInputMethodSubtypeList(imi, true);
726 // IMEs that have no subtypes should be counted.
727 if (subtypes.isEmpty()) {
728 ++filteredImisCount;
729 continue;
730 }
731
732 int auxCount = 0;
733 for (InputMethodSubtype subtype : subtypes) {
734 if (subtype.isAuxiliary()) {
735 ++auxCount;
736 }
737 }
738 final int nonAuxCount = subtypes.size() - auxCount;
739
740 // IMEs that have one or more non-auxiliary subtypes should be counted.
741 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
742 // subtypes should be counted as well.
743 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
744 ++filteredImisCount;
745 continue;
746 }
747 }
748
749 return filteredImisCount > 1
750 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
751 // input method subtype (The current IME should be LatinIME.)
752 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
753 }
754
Jason parks8fd5bc92011-01-12 16:03:31 -0600755 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700756 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600757 if (service != null) {
758 return IMountService.Stub.asInterface(service);
759 }
760 return null;
761 }
762
763 @Override
764 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500765 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600766 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700767 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600768
Jason parksec5a45e2011-01-18 15:28:36 -0600769 if (TextUtils.isEmpty(password)) {
770 return true;
771 }
Jason parks35933812011-01-21 15:48:20 -0600772
Jason parks8fd5bc92011-01-12 16:03:31 -0600773 // Now that we have the password clear the password field.
774 v.setText(null);
775
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700776 // Disable the password entry and back keypress while checking the password. These
777 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600778 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800779 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600780
Ben Komalo04606752011-08-18 14:50:26 -0700781 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600782 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600783
Jason parks8fd5bc92011-01-12 16:03:31 -0600784 return true;
785 }
786 return false;
787 }
David Brown8373b452011-06-20 12:38:45 -0700788
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700789 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700790 * Set airplane mode on the device if it isn't an LTE device.
791 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
792 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
793 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
794 * both these problems, we turn the radio off. However, on certain networks turning on and
795 * off the radio takes a long time. In such cases, we are better off leaving the radio
796 * running so the latency of an E911 call is short.
797 * The behavior after this is:
798 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
799 * airplane mode and restart the radio.
800 * 2. Full boot: we read the persistent settings from the previous boot and restore the
801 * radio to whatever it was before it restarted. This also happens when rebooting a
802 * phone that has no encryption.
803 */
804 private final void setAirplaneModeIfNecessary() {
805 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700806 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700807 if (!isLteDevice) {
808 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700809 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700810 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
811 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700812 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700813 }
814 }
815
816 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700817 * Code to update the state of, and handle clicks from, the "Emergency call" button.
818 *
819 * This code is mostly duplicated from the corresponding code in
820 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
821 */
David Brown8373b452011-06-20 12:38:45 -0700822 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700823 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700824 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700825 if (emergencyCall == null)
826 return;
David Brown8373b452011-06-20 12:38:45 -0700827
828 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700829 emergencyCall.setVisibility(View.VISIBLE);
830 emergencyCall.setOnClickListener(new View.OnClickListener() {
831 @Override
832
David Brown8373b452011-06-20 12:38:45 -0700833 public void onClick(View v) {
834 takeEmergencyCallAction();
835 }
836 });
837 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700838 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700839 return;
840 }
841
David Brown8373b452011-06-20 12:38:45 -0700842 int textId;
Santos Cordon35230e92014-07-07 16:25:10 -0700843 if (getPhoneManager().isInAPhoneCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700844 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700845 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700846 } else {
847 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700848 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700849 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700850 }
851
852 private boolean isEmergencyCallCapable() {
853 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
854 }
855
856 private void takeEmergencyCallAction() {
Santos Cordon35230e92014-07-07 16:25:10 -0700857 PhoneManager phoneManager = getPhoneManager();
858 if (phoneManager.isInAPhoneCall()) {
859 phoneManager.showCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700860 } else {
861 launchEmergencyDialer();
862 }
863 }
864
David Brown8373b452011-06-20 12:38:45 -0700865
866 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700867 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700868 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
869 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700870 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700871 startActivity(intent);
872 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700873
Santos Cordon3afbdf02014-05-29 21:48:16 -0700874 private TelephonyManager getTelephonyManager() {
875 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
876 }
877
Santos Cordon35230e92014-07-07 16:25:10 -0700878 private PhoneManager getPhoneManager() {
879 return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
880 }
881
Vikram Aggarwald1147252012-05-08 13:52:30 -0700882 /**
883 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
884 */
885 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700886 mNotificationCountdown = 20;
887 }
888
889 @Override
890 public boolean onKey(View v, int keyCode, KeyEvent event) {
891 delayAudioNotification();
892 return false;
893 }
894
895 @Override
896 public boolean onTouch(View v, MotionEvent event) {
897 delayAudioNotification();
898 return false;
899 }
900
901 @Override
902 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
903 return;
904 }
905
906 @Override
907 public void onTextChanged(CharSequence s, int start, int before, int count) {
908 delayAudioNotification();
909 }
910
911 @Override
912 public void afterTextChanged(Editable s) {
913 return;
914 }
David Brown8373b452011-06-20 12:38:45 -0700915}