blob: bff75fb13e6df866347179ead770ce5dcb6d160c [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
Paul Lawrence0f11e152014-08-20 07:43:32 -0700136 // how long we wait to clear a right pattern
137 private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
138
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700139 private Runnable mClearPatternRunnable = new Runnable() {
140 public void run() {
141 mLockPatternView.clearPattern();
142 }
143 };
144
Andy Stadler14997402011-02-01 15:34:59 -0800145 /**
146 * Used to propagate state through configuration changes (e.g. screen rotation)
147 */
148 private static class NonConfigurationInstanceState {
149 final PowerManager.WakeLock wakelock;
150
151 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
152 wakelock = _wakelock;
153 }
154 }
155
Jason parks06c5ff42011-03-01 10:17:40 -0600156 private class DecryptTask extends AsyncTask<String, Void, Integer> {
157 @Override
158 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700159 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600160 try {
161 return service.decryptStorage(params[0]);
162 } catch (Exception e) {
163 Log.e(TAG, "Error while decrypting...", e);
164 return -1;
165 }
166 }
167
168 @Override
169 protected void onPostExecute(Integer failedAttempts) {
170 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700171 // The password was entered successfully. Simply do nothing
172 // and wait for the service restart to switch to surfacefligner
Paul Lawrence0f11e152014-08-20 07:43:32 -0700173 if (mLockPatternView != null) {
174 mLockPatternView.removeCallbacks(mClearPatternRunnable);
175 mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
176 }
Jason parks06c5ff42011-03-01 10:17:40 -0600177 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
178 // Factory reset the device.
179 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Paul Lawrence384d8e52014-08-08 15:11:36 -0700180 } else if (failedAttempts == -1) {
181 // Right password, but decryption failed. Tell user bad news ...
182 setContentView(R.layout.crypt_keeper_progress);
183 showFactoryReset(true);
184 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600185 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700186 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700187 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700188 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
189 mLockPatternView.removeCallbacks(mClearPatternRunnable);
190 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
191 }
192 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
193 mCooldown = COOL_DOWN_INTERVAL;
194 cooldown();
195 } else {
196 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700197
198 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
199 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
200 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
201 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
202 Integer.toString(remainingAttempts));
203 status.setText(warning);
204 } else {
205 status.setText(R.string.try_again);
206 }
207
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700208 if (mLockPatternView != null) {
209 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
210 }
211 // Reenable the password entry
212 if (mPasswordEntry != null) {
213 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700214 final InputMethodManager imm = (InputMethodManager) getSystemService(
215 Context.INPUT_METHOD_SERVICE);
216 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700217 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700218 }
219 if (mLockPatternView != null) {
220 mLockPatternView.setEnabled(true);
221 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700222 }
Jason parks06c5ff42011-03-01 10:17:40 -0600223 }
224 }
225 }
Jason parks75c085e2011-02-10 14:03:47 -0600226
Ben Komalo0e666092011-09-01 15:42:00 -0700227 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
228 @Override
229 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700230 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700231 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700232 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700233 int state = service.getEncryptionState();
234 if (state == IMountService.ENCRYPTION_STATE_NONE) {
235 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
236 return true; // Unexpected, but fine, I guess...
237 }
238 return state == IMountService.ENCRYPTION_STATE_OK;
239 } catch (RemoteException e) {
240 Log.w(TAG, "Unable to get encryption state properly");
241 return true;
242 }
243 }
244
245 @Override
246 protected void onPostExecute(Boolean result) {
247 mValidationComplete = true;
248 if (Boolean.FALSE.equals(result)) {
249 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
250 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700251 } else {
252 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700253 }
254 setupUi();
255 }
256 }
257
Ben Komalo91a2f052011-08-16 17:48:25 -0700258 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600259 @Override
260 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600261 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700262 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600263 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600264 break;
Jason parks35933812011-01-21 15:48:20 -0600265
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700266 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600267 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600268 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700269
270 case MESSAGE_NOTIFY:
271 notifyUser();
272 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600273 }
274 }
275 };
Jason parks35933812011-01-21 15:48:20 -0600276
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700277 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800278 /** The status bar where back/home/recent buttons are shown. */
279 private StatusBarManager mStatusBar;
280
281 /** All the widgets to disable in the status bar */
282 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
283 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
284 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
285 | StatusBarManager.DISABLE_SYSTEM_INFO
286 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700287 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800288 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700289
Ben Komalo91a2f052011-08-16 17:48:25 -0700290 /** @return whether or not this Activity was started for debugging the UI only. */
291 private boolean isDebugView() {
292 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
293 }
294
295 /** @return whether or not this Activity was started for debugging the specific UI view only. */
296 private boolean isDebugView(String viewType /* non-nullable */) {
297 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
298 }
299
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700300 /**
301 * Notify the user that we are awaiting input. Currently this sends an audio alert.
302 */
303 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700304 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700305 --mNotificationCountdown;
306 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700307 try {
308 // Play the standard keypress sound at full volume. This should be available on
309 // every device. We cannot play a ringtone here because media services aren't
310 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
311 // on tablet devices. The idea is to alert the user that something is needed: this
312 // does not have to be pleasing.
313 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
314 } catch (Exception e) {
315 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
316 }
317 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700318 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700319 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700320 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700321
322 if (mWakeLock.isHeld()) {
323 if (mReleaseWakeLockCountdown > 0) {
324 --mReleaseWakeLockCountdown;
325 } else {
326 mWakeLock.release();
327 }
328 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700329 }
330
331 /**
332 * Ignore back events after the user has entered the decrypt screen and while the device is
333 * encrypting.
334 */
335 @Override
336 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800337 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700338 if (mIgnoreBack)
339 return;
340 super.onBackPressed();
341 }
342
Jason parks8fd5bc92011-01-12 16:03:31 -0600343 @Override
344 public void onCreate(Bundle savedInstanceState) {
345 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600346
Andy Stadler95974062011-02-01 17:35:20 -0800347 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700348 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700349 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600350 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600351 PackageManager pm = getPackageManager();
352 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700353 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
354 PackageManager.DONT_KILL_APP);
355 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700356 // want to be running, so need to finish this activity. We can count
357 // on the activity manager re-launching the new home app upon finishing
358 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700359 // NOTE: This is really grungy. I think it would be better for the
360 // activity manager to explicitly launch the crypt keeper instead of
361 // home in the situation where we need to decrypt the device
362 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600363 return;
364 }
Jason parks35933812011-01-21 15:48:20 -0600365
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700366 try {
367 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
368 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
369 }
370 } catch (NotFoundException e) {
371 }
372
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700373 // Disable the status bar, but do NOT disable back because the user needs a way to go
374 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800375 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
376 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800377
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700378 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700379 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800380 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700381 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800382 if (lastInstance instanceof NonConfigurationInstanceState) {
383 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
384 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700385 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800386 }
Jason parksec5a45e2011-01-18 15:28:36 -0600387 }
Jason parks35933812011-01-21 15:48:20 -0600388
Andy Stadler95974062011-02-01 17:35:20 -0800389 /**
390 * Note, we defer the state check and screen setup to onStart() because this will be
391 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
392 * especially important if we were to lose the wakelock for any reason.
393 */
394 @Override
395 public void onStart() {
396 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700397 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700398 }
399
400 /**
401 * Initializes the UI based on the current state of encryption.
402 * This is idempotent - calling repeatedly will simply re-initialize the UI.
403 */
404 private void setupUi() {
405 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
406 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700407 showFactoryReset(false);
Ben Komalo0e666092011-09-01 15:42:00 -0700408 return;
409 }
410
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700411 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700412 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800413 setContentView(R.layout.crypt_keeper_progress);
414 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700415 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700416 new AsyncTask<Void, Void, Void>() {
417 int type = StorageManager.CRYPT_TYPE_PASSWORD;
418 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700419 boolean pattern_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800420
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700421 @Override
422 public Void doInBackground(Void... v) {
423 try {
424 final IMountService service = getMountService();
425 type = service.getPasswordType();
426 owner_info = service.getField("OwnerInfo");
Paul Lawrence0f11e152014-08-20 07:43:32 -0700427 pattern_visible = !("0".equals(service.getField("PatternVisible")));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700428 } catch (Exception e) {
429 Log.e(TAG, "Error calling mount service " + e);
430 }
431
432 return null;
433 }
434
435 @Override
436 public void onPostExecute(java.lang.Void v) {
437 if(type == StorageManager.CRYPT_TYPE_PIN) {
438 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700439 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700440 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
441 setContentView(R.layout.crypt_keeper_pattern_entry);
442 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700443 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700444 } else {
445 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700446 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700447 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700448 final TextView status = (TextView) findViewById(R.id.status);
449 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700450
Paul Lawrence5a70f052014-06-13 11:09:55 -0700451 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
452 ownerInfo.setText(owner_info);
453 ownerInfo.setSelected(true); // Required for marquee'ing to work
454
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700455 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700456
Paul Lawrence0f11e152014-08-20 07:43:32 -0700457 if (mLockPatternView != null) {
458 mLockPatternView.setInStealthMode(!pattern_visible);
459 }
460
Paul Lawrence5a70f052014-06-13 11:09:55 -0700461 if (mCooldown > 0) {
462 setBackFunctionality(false);
463 cooldown(); // in case we are cooling down and coming back from emergency dialler
464 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700465 }
466 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700467 } else if (!mValidationRequested) {
468 // We're supposed to be encrypted, but no validation has been done.
469 new ValidationTask().execute((Void[]) null);
470 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800471 }
472 }
473
Jason parksf8217302011-01-26 13:11:42 -0600474 @Override
475 public void onStop() {
476 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700477 mHandler.removeMessages(MESSAGE_COOLDOWN);
478 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
479 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800480 }
481
482 /**
483 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
484 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
485 * mWakeLock so the subsequent call to onDestroy does not release it.
486 */
487 @Override
488 public Object onRetainNonConfigurationInstance() {
489 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700490 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800491 mWakeLock = null;
492 return state;
493 }
494
495 @Override
496 public void onDestroy() {
497 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600498
499 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700500 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600501 mWakeLock.release();
502 mWakeLock = null;
503 }
504 }
505
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700506 /**
507 * Start encrypting the device.
508 */
Jason parksec5a45e2011-01-18 15:28:36 -0600509 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600510 // Accquire a partial wakelock to prevent the device from sleeping. Note
511 // we never release this wakelock as we will be restarted after the device
512 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700513 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700514 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700515 Log.d(TAG, "Acquiring wakelock.");
516 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
517 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
518 mWakeLock.acquire();
519 }
Jason parks35933812011-01-21 15:48:20 -0600520
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700521 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700522 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800523 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700524 // Start the first run of progress manually. This method sets up messages to occur at
525 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600526 updateProgress();
527 }
528
Paul Lawrence384d8e52014-08-08 15:11:36 -0700529 /**
530 * Show factory reset screen allowing the user to reset their phone when
531 * there is nothing else we can do
532 * @param corrupt true if userdata is corrupt, false if encryption failed
533 * partway through
534 */
535 private void showFactoryReset(boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800536 // Hide the encryption-bot to make room for the "factory reset" button
537 findViewById(R.id.encroid).setVisibility(View.GONE);
538
539 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700540 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800541 button.setVisibility(View.VISIBLE);
542 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700543 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800544 public void onClick(View v) {
545 // Factory reset the device.
546 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
547 }
548 });
549
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700550 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700551 if (corrupt) {
552 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
553 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
554 } else {
555 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
556 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
557 }
Andy Stadler13d62042011-01-31 19:21:37 -0800558
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700559 final View view = findViewById(R.id.bottom_divider);
560 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700561 if (view != null) {
562 view.setVisibility(View.VISIBLE);
563 }
Andy Stadler13d62042011-01-31 19:21:37 -0800564 }
565
Jason parksf8217302011-01-26 13:11:42 -0600566 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700567 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600568
Ben Komalo0e666092011-09-01 15:42:00 -0700569 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700570 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800571 return;
572 }
573
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700574 // Get status as percentage first
575 CharSequence status = getText(R.string.crypt_keeper_setup_description);
576 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600577 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700578 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700579 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600580 } catch (Exception e) {
581 Log.w(TAG, "Error parsing progress: " + e.toString());
582 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700583 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600584
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700585 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700586 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700587 try {
588 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
589 int time = Integer.parseInt(timeProperty);
590 if (time >= 0) {
591 // Round up to multiple of 10 - this way display is less jerky
592 time = (time + 9) / 10 * 10;
593 progress = DateUtils.formatElapsedTime(time);
594 status = getText(R.string.crypt_keeper_setup_time_remaining);
595 }
596 } catch (Exception e) {
597 // Will happen if no time etc - show percentage
598 }
599
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700600 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700601 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700602 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700603 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700604
605 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700606 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700607 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600608 }
609
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700610 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600611 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700612 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800613
Jason parksf8217302011-01-26 13:11:42 -0600614 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700615 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700616 if (mPasswordEntry != null) {
617 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700618 final InputMethodManager imm = (InputMethodManager) getSystemService(
619 Context.INPUT_METHOD_SERVICE);
620 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700621 setBackFunctionality(true);
622 }
623 if (mLockPatternView != null) {
624 mLockPatternView.setEnabled(true);
625 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700626 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600627 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700628 // Disable the password entry and back presses.
629 if (mPasswordEntry != null) {
630 mPasswordEntry.setEnabled(false);
631 }
632 if (mLockPatternView != null) {
633 mLockPatternView.setEnabled(false);
634 }
635
Andy Stadler13d62042011-01-31 19:21:37 -0800636 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700637 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800638
Jason parksf8217302011-01-26 13:11:42 -0600639 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700640 mHandler.removeMessages(MESSAGE_COOLDOWN);
641 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600642 }
Jason parksec5a45e2011-01-18 15:28:36 -0600643 }
Jason parks35933812011-01-21 15:48:20 -0600644
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800645 /**
646 * Sets the back status: enabled or disabled according to the parameter.
647 * @param isEnabled true if back is enabled, false otherwise.
648 */
649 private final void setBackFunctionality(boolean isEnabled) {
650 mIgnoreBack = !isEnabled;
651 if (isEnabled) {
652 mStatusBar.disable(sWidgetsToDisable);
653 } else {
654 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
655 }
656 }
657
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800658 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700659 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800660
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700661 @Override
662 public void onPatternStart() {
663 mLockPatternView.removeCallbacks(mClearPatternRunnable);
664 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800665
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700666 @Override
667 public void onPatternCleared() {
668 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800669
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700670 @Override
671 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
672 mLockPatternView.setEnabled(false);
673 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
674 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800675
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700676 @Override
677 public void onPatternCellAdded(List<Cell> pattern) {
678 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800679 };
680
681 private void passwordEntryInit() {
682 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600683 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800684 if (mPasswordEntry != null){
685 mPasswordEntry.setOnEditorActionListener(this);
686 mPasswordEntry.requestFocus();
687 // Become quiet when the user interacts with the Edit text screen.
688 mPasswordEntry.setOnKeyListener(this);
689 mPasswordEntry.setOnTouchListener(this);
690 mPasswordEntry.addTextChangedListener(this);
691 }
692
693 // Pattern case
694 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
695 if (mLockPatternView != null) {
696 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
697 }
Jason parks35933812011-01-21 15:48:20 -0600698
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700699 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700700 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700701 final View emergencyCall = findViewById(R.id.emergencyCallButton);
702 if (emergencyCall != null) {
703 Log.d(TAG, "Removing the emergency Call button");
704 emergencyCall.setVisibility(View.GONE);
705 }
706 }
707
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700708 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700709 final InputMethodManager imm = (InputMethodManager) getSystemService(
710 Context.INPUT_METHOD_SERVICE);
711 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
712 imeSwitcher.setVisibility(View.VISIBLE);
713 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700714 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700715 public void onClick(View v) {
716 imm.showInputMethodPicker();
717 }
718 });
Jason parks00046d62011-06-13 17:38:45 -0500719 }
David Brown8373b452011-06-20 12:38:45 -0700720
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700721 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
722 // is completely non-functional, and we want the user to notice the device and enter a
723 // password.
724 if (mWakeLock == null) {
725 Log.d(TAG, "Acquiring wakelock.");
726 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
727 if (pm != null) {
728 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
729 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700730 // Keep awake for 10 minutes - if the user hasn't been alerted by then
731 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700732 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700733 }
734 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700735
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700736 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
737 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700738 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700739 mHandler.postDelayed(new Runnable() {
740 @Override public void run() {
741 imm.showSoftInputUnchecked(0, null);
742 }
743 }, 0);
744 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700745
David Brown8373b452011-06-20 12:38:45 -0700746 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700747 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700748 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700749 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800750
751 // Dismiss keyguard while this screen is showing.
752 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600753 }
754
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700755 /**
756 * Method adapted from com.android.inputmethod.latin.Utils
757 *
758 * @param imm The input method manager
759 * @param shouldIncludeAuxiliarySubtypes
760 * @return true if we have multiple IMEs to choose from
761 */
762 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
763 final boolean shouldIncludeAuxiliarySubtypes) {
764 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
765
766 // Number of the filtered IMEs
767 int filteredImisCount = 0;
768
769 for (InputMethodInfo imi : enabledImis) {
770 // We can return true immediately after we find two or more filtered IMEs.
771 if (filteredImisCount > 1) return true;
772 final List<InputMethodSubtype> subtypes =
773 imm.getEnabledInputMethodSubtypeList(imi, true);
774 // IMEs that have no subtypes should be counted.
775 if (subtypes.isEmpty()) {
776 ++filteredImisCount;
777 continue;
778 }
779
780 int auxCount = 0;
781 for (InputMethodSubtype subtype : subtypes) {
782 if (subtype.isAuxiliary()) {
783 ++auxCount;
784 }
785 }
786 final int nonAuxCount = subtypes.size() - auxCount;
787
788 // IMEs that have one or more non-auxiliary subtypes should be counted.
789 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
790 // subtypes should be counted as well.
791 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
792 ++filteredImisCount;
793 continue;
794 }
795 }
796
797 return filteredImisCount > 1
798 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
799 // input method subtype (The current IME should be LatinIME.)
800 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
801 }
802
Jason parks8fd5bc92011-01-12 16:03:31 -0600803 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700804 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600805 if (service != null) {
806 return IMountService.Stub.asInterface(service);
807 }
808 return null;
809 }
810
811 @Override
812 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500813 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600814 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700815 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600816
Jason parksec5a45e2011-01-18 15:28:36 -0600817 if (TextUtils.isEmpty(password)) {
818 return true;
819 }
Jason parks35933812011-01-21 15:48:20 -0600820
Jason parks8fd5bc92011-01-12 16:03:31 -0600821 // Now that we have the password clear the password field.
822 v.setText(null);
823
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700824 // Disable the password entry and back keypress while checking the password. These
825 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600826 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800827 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600828
Ben Komalo04606752011-08-18 14:50:26 -0700829 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600830 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600831
Jason parks8fd5bc92011-01-12 16:03:31 -0600832 return true;
833 }
834 return false;
835 }
David Brown8373b452011-06-20 12:38:45 -0700836
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700837 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700838 * Set airplane mode on the device if it isn't an LTE device.
839 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
840 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
841 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
842 * both these problems, we turn the radio off. However, on certain networks turning on and
843 * off the radio takes a long time. In such cases, we are better off leaving the radio
844 * running so the latency of an E911 call is short.
845 * The behavior after this is:
846 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
847 * airplane mode and restart the radio.
848 * 2. Full boot: we read the persistent settings from the previous boot and restore the
849 * radio to whatever it was before it restarted. This also happens when rebooting a
850 * phone that has no encryption.
851 */
852 private final void setAirplaneModeIfNecessary() {
853 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700854 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700855 if (!isLteDevice) {
856 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700857 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700858 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
859 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700860 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700861 }
862 }
863
864 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700865 * Code to update the state of, and handle clicks from, the "Emergency call" button.
866 *
867 * This code is mostly duplicated from the corresponding code in
868 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
869 */
David Brown8373b452011-06-20 12:38:45 -0700870 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700871 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700872 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700873 if (emergencyCall == null)
874 return;
David Brown8373b452011-06-20 12:38:45 -0700875
876 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700877 emergencyCall.setVisibility(View.VISIBLE);
878 emergencyCall.setOnClickListener(new View.OnClickListener() {
879 @Override
880
David Brown8373b452011-06-20 12:38:45 -0700881 public void onClick(View v) {
882 takeEmergencyCallAction();
883 }
884 });
885 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700886 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700887 return;
888 }
889
David Brown8373b452011-06-20 12:38:45 -0700890 int textId;
Santos Cordon35230e92014-07-07 16:25:10 -0700891 if (getPhoneManager().isInAPhoneCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700892 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700893 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700894 } else {
895 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700896 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700897 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700898 }
899
900 private boolean isEmergencyCallCapable() {
901 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
902 }
903
904 private void takeEmergencyCallAction() {
Santos Cordon35230e92014-07-07 16:25:10 -0700905 PhoneManager phoneManager = getPhoneManager();
906 if (phoneManager.isInAPhoneCall()) {
907 phoneManager.showCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700908 } else {
909 launchEmergencyDialer();
910 }
911 }
912
David Brown8373b452011-06-20 12:38:45 -0700913
914 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700915 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700916 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
917 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700918 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700919 startActivity(intent);
920 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700921
Santos Cordon3afbdf02014-05-29 21:48:16 -0700922 private TelephonyManager getTelephonyManager() {
923 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
924 }
925
Santos Cordon35230e92014-07-07 16:25:10 -0700926 private PhoneManager getPhoneManager() {
927 return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
928 }
929
Vikram Aggarwald1147252012-05-08 13:52:30 -0700930 /**
931 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
932 */
933 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700934 mNotificationCountdown = 20;
935 }
936
937 @Override
938 public boolean onKey(View v, int keyCode, KeyEvent event) {
939 delayAudioNotification();
940 return false;
941 }
942
943 @Override
944 public boolean onTouch(View v, MotionEvent event) {
945 delayAudioNotification();
946 return false;
947 }
948
949 @Override
950 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
951 return;
952 }
953
954 @Override
955 public void onTextChanged(CharSequence s, int start, int before, int count) {
956 delayAudioNotification();
957 }
958
959 @Override
960 public void afterTextChanged(Editable s) {
961 return;
962 }
David Brown8373b452011-06-20 12:38:45 -0700963}