blob: c2295e6b9568ff2291baafb24faef250f1982917 [file] [log] [blame]
Jason parks8fd5bc92011-01-12 16:03:31 -06001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason parks8fd5bc92011-01-12 16:03:31 -060019import android.app.Activity;
20import android.app.StatusBarManager;
21import android.content.ComponentName;
22import android.content.Context;
Jason parksec5a45e2011-01-18 15:28:36 -060023import android.content.Intent;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070024import android.content.pm.ActivityInfo;
Jason parks8fd5bc92011-01-12 16:03:31 -060025import android.content.pm.PackageManager;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070026import android.content.res.Resources.NotFoundException;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070027import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060028import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060031import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060032import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060033import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070034import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060035import android.os.ServiceManager;
36import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070037import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080039import android.os.storage.StorageManager;
Santos Cordon35230e92014-07-07 16:25:10 -070040
41import android.phone.PhoneManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070042import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070043import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060045import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070046import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070047import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060048import android.util.Log;
49import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070050import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080051import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080052import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080053import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070054import android.view.View.OnKeyListener;
55import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060056import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070057import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060058import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070059import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080060import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060061import android.widget.EditText;
62import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060063import android.widget.TextView;
64
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080065import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070066import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070067import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080068import com.android.internal.widget.LockPatternUtils;
69import com.android.internal.widget.LockPatternView;
70import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070071
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070072import static com.android.internal.widget.LockPatternView.DisplayMode;
73
Ben Komalo9fcb6a72011-08-26 14:40:18 -070074import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070075
76/**
77 * Settings screens to show the UI flows for encrypting/decrypting the device.
78 *
79 * This may be started via adb for debugging the UI layout, without having to go through
80 * encryption flows everytime. It should be noted that starting the activity in this manner
81 * is only useful for verifying UI-correctness - the behavior will not be identical.
82 * <pre>
83 * $ adb shell pm enable com.android.settings/.CryptKeeper
84 * $ adb shell am start \
85 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
86 * -n com.android.settings/.CryptKeeper
87 * </pre>
88 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070089public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
90 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060091 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060092
Jason parks8fd5bc92011-01-12 16:03:31 -060093 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070094 /** Message sent to us to indicate encryption update progress. */
95 private static final int MESSAGE_UPDATE_PROGRESS = 1;
96 /** Message sent to us to cool-down (waste user's time between password attempts) */
97 private static final int MESSAGE_COOLDOWN = 2;
98 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
99 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -0600100
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700101 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600102 private static final int MAX_FAILED_ATTEMPTS = 30;
103 private static final int COOL_DOWN_ATTEMPTS = 10;
104 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
105
David Brown8373b452011-06-20 12:38:45 -0700106 // Intent action for launching the Emergency Dialer activity.
107 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
108
Ben Komalo91a2f052011-08-16 17:48:25 -0700109 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
110 private static final String EXTRA_FORCE_VIEW =
111 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
112 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700113 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700114 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700115
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700116 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700117 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700118 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700119 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
120 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700121 /** A flag to indicate when the back event should be ignored */
122 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800123 private int mCooldown;
124 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600125 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800126 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700127 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
128 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700129 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
130 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700131 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800132
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700133 // how long we wait to clear a wrong pattern
134 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
135
136 private Runnable mClearPatternRunnable = new Runnable() {
137 public void run() {
138 mLockPatternView.clearPattern();
139 }
140 };
141
Andy Stadler14997402011-02-01 15:34:59 -0800142 /**
143 * Used to propagate state through configuration changes (e.g. screen rotation)
144 */
145 private static class NonConfigurationInstanceState {
146 final PowerManager.WakeLock wakelock;
147
148 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
149 wakelock = _wakelock;
150 }
151 }
152
Jason parks06c5ff42011-03-01 10:17:40 -0600153 private class DecryptTask extends AsyncTask<String, Void, Integer> {
154 @Override
155 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700156 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600157 try {
158 return service.decryptStorage(params[0]);
159 } catch (Exception e) {
160 Log.e(TAG, "Error while decrypting...", e);
161 return -1;
162 }
163 }
164
165 @Override
166 protected void onPostExecute(Integer failedAttempts) {
167 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700168 // The password was entered successfully. Simply do nothing
169 // and wait for the service restart to switch to surfacefligner
Jason parks06c5ff42011-03-01 10:17:40 -0600170 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
171 // Factory reset the device.
172 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Paul Lawrence384d8e52014-08-08 15:11:36 -0700173 } else if (failedAttempts == -1) {
174 // Right password, but decryption failed. Tell user bad news ...
175 setContentView(R.layout.crypt_keeper_progress);
176 showFactoryReset(true);
177 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600178 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700179 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700180 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700181 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
182 mLockPatternView.removeCallbacks(mClearPatternRunnable);
183 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
184 }
185 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
186 mCooldown = COOL_DOWN_INTERVAL;
187 cooldown();
188 } else {
189 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700190
191 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
192 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
193 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
194 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
195 Integer.toString(remainingAttempts));
196 status.setText(warning);
197 } else {
198 status.setText(R.string.try_again);
199 }
200
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700201 if (mLockPatternView != null) {
202 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
203 }
204 // Reenable the password entry
205 if (mPasswordEntry != null) {
206 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700207 final InputMethodManager imm = (InputMethodManager) getSystemService(
208 Context.INPUT_METHOD_SERVICE);
209 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700210 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700211 }
212 if (mLockPatternView != null) {
213 mLockPatternView.setEnabled(true);
214 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700215 }
Jason parks06c5ff42011-03-01 10:17:40 -0600216 }
217 }
218 }
Jason parks75c085e2011-02-10 14:03:47 -0600219
Ben Komalo0e666092011-09-01 15:42:00 -0700220 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
221 @Override
222 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700223 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700224 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700225 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700226 int state = service.getEncryptionState();
227 if (state == IMountService.ENCRYPTION_STATE_NONE) {
228 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
229 return true; // Unexpected, but fine, I guess...
230 }
231 return state == IMountService.ENCRYPTION_STATE_OK;
232 } catch (RemoteException e) {
233 Log.w(TAG, "Unable to get encryption state properly");
234 return true;
235 }
236 }
237
238 @Override
239 protected void onPostExecute(Boolean result) {
240 mValidationComplete = true;
241 if (Boolean.FALSE.equals(result)) {
242 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
243 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700244 } else {
245 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700246 }
247 setupUi();
248 }
249 }
250
Ben Komalo91a2f052011-08-16 17:48:25 -0700251 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600252 @Override
253 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600254 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700255 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600256 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600257 break;
Jason parks35933812011-01-21 15:48:20 -0600258
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700259 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600260 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600261 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700262
263 case MESSAGE_NOTIFY:
264 notifyUser();
265 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600266 }
267 }
268 };
Jason parks35933812011-01-21 15:48:20 -0600269
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700270 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800271 /** The status bar where back/home/recent buttons are shown. */
272 private StatusBarManager mStatusBar;
273
274 /** All the widgets to disable in the status bar */
275 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
276 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
277 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
278 | StatusBarManager.DISABLE_SYSTEM_INFO
279 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700280 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800281 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700282
Ben Komalo91a2f052011-08-16 17:48:25 -0700283 /** @return whether or not this Activity was started for debugging the UI only. */
284 private boolean isDebugView() {
285 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
286 }
287
288 /** @return whether or not this Activity was started for debugging the specific UI view only. */
289 private boolean isDebugView(String viewType /* non-nullable */) {
290 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
291 }
292
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700293 /**
294 * Notify the user that we are awaiting input. Currently this sends an audio alert.
295 */
296 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700297 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700298 --mNotificationCountdown;
299 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700300 try {
301 // Play the standard keypress sound at full volume. This should be available on
302 // every device. We cannot play a ringtone here because media services aren't
303 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
304 // on tablet devices. The idea is to alert the user that something is needed: this
305 // does not have to be pleasing.
306 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
307 } catch (Exception e) {
308 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
309 }
310 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700311 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700312 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700313 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700314
315 if (mWakeLock.isHeld()) {
316 if (mReleaseWakeLockCountdown > 0) {
317 --mReleaseWakeLockCountdown;
318 } else {
319 mWakeLock.release();
320 }
321 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700322 }
323
324 /**
325 * Ignore back events after the user has entered the decrypt screen and while the device is
326 * encrypting.
327 */
328 @Override
329 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800330 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700331 if (mIgnoreBack)
332 return;
333 super.onBackPressed();
334 }
335
Jason parks8fd5bc92011-01-12 16:03:31 -0600336 @Override
337 public void onCreate(Bundle savedInstanceState) {
338 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600339
Andy Stadler95974062011-02-01 17:35:20 -0800340 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700341 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700342 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600343 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600344 PackageManager pm = getPackageManager();
345 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700346 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
347 PackageManager.DONT_KILL_APP);
348 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700349 // want to be running, so need to finish this activity. We can count
350 // on the activity manager re-launching the new home app upon finishing
351 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700352 // NOTE: This is really grungy. I think it would be better for the
353 // activity manager to explicitly launch the crypt keeper instead of
354 // home in the situation where we need to decrypt the device
355 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600356 return;
357 }
Jason parks35933812011-01-21 15:48:20 -0600358
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700359 try {
360 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
361 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
362 }
363 } catch (NotFoundException e) {
364 }
365
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700366 // Disable the status bar, but do NOT disable back because the user needs a way to go
367 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800368 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
369 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800370
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700371 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700372 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800373 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700374 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800375 if (lastInstance instanceof NonConfigurationInstanceState) {
376 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
377 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700378 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800379 }
Jason parksec5a45e2011-01-18 15:28:36 -0600380 }
Jason parks35933812011-01-21 15:48:20 -0600381
Andy Stadler95974062011-02-01 17:35:20 -0800382 /**
383 * Note, we defer the state check and screen setup to onStart() because this will be
384 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
385 * especially important if we were to lose the wakelock for any reason.
386 */
387 @Override
388 public void onStart() {
389 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700390 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700391 }
392
393 /**
394 * Initializes the UI based on the current state of encryption.
395 * This is idempotent - calling repeatedly will simply re-initialize the UI.
396 */
397 private void setupUi() {
398 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
399 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700400 showFactoryReset(false);
Ben Komalo0e666092011-09-01 15:42:00 -0700401 return;
402 }
403
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700404 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700405 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800406 setContentView(R.layout.crypt_keeper_progress);
407 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700408 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700409 new AsyncTask<Void, Void, Void>() {
410 int type = StorageManager.CRYPT_TYPE_PASSWORD;
411 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800412
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700413 @Override
414 public Void doInBackground(Void... v) {
415 try {
416 final IMountService service = getMountService();
417 type = service.getPasswordType();
418 owner_info = service.getField("OwnerInfo");
419 } catch (Exception e) {
420 Log.e(TAG, "Error calling mount service " + e);
421 }
422
423 return null;
424 }
425
426 @Override
427 public void onPostExecute(java.lang.Void v) {
428 if(type == StorageManager.CRYPT_TYPE_PIN) {
429 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700430 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700431 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
432 setContentView(R.layout.crypt_keeper_pattern_entry);
433 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700434 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700435 } else {
436 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700437 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700438 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700439 final TextView status = (TextView) findViewById(R.id.status);
440 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700441
Paul Lawrence5a70f052014-06-13 11:09:55 -0700442 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
443 ownerInfo.setText(owner_info);
444 ownerInfo.setSelected(true); // Required for marquee'ing to work
445
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700446 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700447
448 if (mCooldown > 0) {
449 setBackFunctionality(false);
450 cooldown(); // in case we are cooling down and coming back from emergency dialler
451 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700452 }
453 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700454 } else if (!mValidationRequested) {
455 // We're supposed to be encrypted, but no validation has been done.
456 new ValidationTask().execute((Void[]) null);
457 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800458 }
459 }
460
Jason parksf8217302011-01-26 13:11:42 -0600461 @Override
462 public void onStop() {
463 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700464 mHandler.removeMessages(MESSAGE_COOLDOWN);
465 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
466 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800467 }
468
469 /**
470 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
471 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
472 * mWakeLock so the subsequent call to onDestroy does not release it.
473 */
474 @Override
475 public Object onRetainNonConfigurationInstance() {
476 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700477 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800478 mWakeLock = null;
479 return state;
480 }
481
482 @Override
483 public void onDestroy() {
484 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600485
486 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700487 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600488 mWakeLock.release();
489 mWakeLock = null;
490 }
491 }
492
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700493 /**
494 * Start encrypting the device.
495 */
Jason parksec5a45e2011-01-18 15:28:36 -0600496 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600497 // Accquire a partial wakelock to prevent the device from sleeping. Note
498 // we never release this wakelock as we will be restarted after the device
499 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700500 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700501 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700502 Log.d(TAG, "Acquiring wakelock.");
503 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
504 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
505 mWakeLock.acquire();
506 }
Jason parks35933812011-01-21 15:48:20 -0600507
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700508 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700509 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800510 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700511 // Start the first run of progress manually. This method sets up messages to occur at
512 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600513 updateProgress();
514 }
515
Paul Lawrence384d8e52014-08-08 15:11:36 -0700516 /**
517 * Show factory reset screen allowing the user to reset their phone when
518 * there is nothing else we can do
519 * @param corrupt true if userdata is corrupt, false if encryption failed
520 * partway through
521 */
522 private void showFactoryReset(boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800523 // Hide the encryption-bot to make room for the "factory reset" button
524 findViewById(R.id.encroid).setVisibility(View.GONE);
525
526 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700527 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800528 button.setVisibility(View.VISIBLE);
529 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700530 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800531 public void onClick(View v) {
532 // Factory reset the device.
533 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
534 }
535 });
536
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700537 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700538 if (corrupt) {
539 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
540 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
541 } else {
542 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
543 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
544 }
Andy Stadler13d62042011-01-31 19:21:37 -0800545
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700546 final View view = findViewById(R.id.bottom_divider);
547 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700548 if (view != null) {
549 view.setVisibility(View.VISIBLE);
550 }
Andy Stadler13d62042011-01-31 19:21:37 -0800551 }
552
Jason parksf8217302011-01-26 13:11:42 -0600553 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700554 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600555
Ben Komalo0e666092011-09-01 15:42:00 -0700556 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700557 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800558 return;
559 }
560
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700561 // Get status as percentage first
562 CharSequence status = getText(R.string.crypt_keeper_setup_description);
563 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600564 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700565 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700566 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600567 } catch (Exception e) {
568 Log.w(TAG, "Error parsing progress: " + e.toString());
569 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700570 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600571
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700572 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700573 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700574 try {
575 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
576 int time = Integer.parseInt(timeProperty);
577 if (time >= 0) {
578 // Round up to multiple of 10 - this way display is less jerky
579 time = (time + 9) / 10 * 10;
580 progress = DateUtils.formatElapsedTime(time);
581 status = getText(R.string.crypt_keeper_setup_time_remaining);
582 }
583 } catch (Exception e) {
584 // Will happen if no time etc - show percentage
585 }
586
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700587 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700588 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700589 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700590 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700591
592 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700593 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700594 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600595 }
596
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700597 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600598 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700599 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800600
Jason parksf8217302011-01-26 13:11:42 -0600601 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700602 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700603 if (mPasswordEntry != null) {
604 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700605 final InputMethodManager imm = (InputMethodManager) getSystemService(
606 Context.INPUT_METHOD_SERVICE);
607 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700608 setBackFunctionality(true);
609 }
610 if (mLockPatternView != null) {
611 mLockPatternView.setEnabled(true);
612 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700613 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600614 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700615 // Disable the password entry and back presses.
616 if (mPasswordEntry != null) {
617 mPasswordEntry.setEnabled(false);
618 }
619 if (mLockPatternView != null) {
620 mLockPatternView.setEnabled(false);
621 }
622
Andy Stadler13d62042011-01-31 19:21:37 -0800623 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700624 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800625
Jason parksf8217302011-01-26 13:11:42 -0600626 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700627 mHandler.removeMessages(MESSAGE_COOLDOWN);
628 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600629 }
Jason parksec5a45e2011-01-18 15:28:36 -0600630 }
Jason parks35933812011-01-21 15:48:20 -0600631
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800632 /**
633 * Sets the back status: enabled or disabled according to the parameter.
634 * @param isEnabled true if back is enabled, false otherwise.
635 */
636 private final void setBackFunctionality(boolean isEnabled) {
637 mIgnoreBack = !isEnabled;
638 if (isEnabled) {
639 mStatusBar.disable(sWidgetsToDisable);
640 } else {
641 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
642 }
643 }
644
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800645 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700646 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800647
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700648 @Override
649 public void onPatternStart() {
650 mLockPatternView.removeCallbacks(mClearPatternRunnable);
651 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800652
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700653 @Override
654 public void onPatternCleared() {
655 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800656
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700657 @Override
658 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
659 mLockPatternView.setEnabled(false);
660 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
661 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800662
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700663 @Override
664 public void onPatternCellAdded(List<Cell> pattern) {
665 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800666 };
667
668 private void passwordEntryInit() {
669 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600670 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800671 if (mPasswordEntry != null){
672 mPasswordEntry.setOnEditorActionListener(this);
673 mPasswordEntry.requestFocus();
674 // Become quiet when the user interacts with the Edit text screen.
675 mPasswordEntry.setOnKeyListener(this);
676 mPasswordEntry.setOnTouchListener(this);
677 mPasswordEntry.addTextChangedListener(this);
678 }
679
680 // Pattern case
681 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
682 if (mLockPatternView != null) {
683 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
684 }
Jason parks35933812011-01-21 15:48:20 -0600685
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700686 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700687 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700688 final View emergencyCall = findViewById(R.id.emergencyCallButton);
689 if (emergencyCall != null) {
690 Log.d(TAG, "Removing the emergency Call button");
691 emergencyCall.setVisibility(View.GONE);
692 }
693 }
694
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700695 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700696 final InputMethodManager imm = (InputMethodManager) getSystemService(
697 Context.INPUT_METHOD_SERVICE);
698 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
699 imeSwitcher.setVisibility(View.VISIBLE);
700 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700701 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700702 public void onClick(View v) {
703 imm.showInputMethodPicker();
704 }
705 });
Jason parks00046d62011-06-13 17:38:45 -0500706 }
David Brown8373b452011-06-20 12:38:45 -0700707
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700708 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
709 // is completely non-functional, and we want the user to notice the device and enter a
710 // password.
711 if (mWakeLock == null) {
712 Log.d(TAG, "Acquiring wakelock.");
713 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
714 if (pm != null) {
715 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
716 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700717 // Keep awake for 10 minutes - if the user hasn't been alerted by then
718 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700719 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700720 }
721 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700722
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700723 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
724 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700725 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700726 mHandler.postDelayed(new Runnable() {
727 @Override public void run() {
728 imm.showSoftInputUnchecked(0, null);
729 }
730 }, 0);
731 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700732
David Brown8373b452011-06-20 12:38:45 -0700733 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700734 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700735 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700736 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800737
738 // Dismiss keyguard while this screen is showing.
739 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600740 }
741
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700742 /**
743 * Method adapted from com.android.inputmethod.latin.Utils
744 *
745 * @param imm The input method manager
746 * @param shouldIncludeAuxiliarySubtypes
747 * @return true if we have multiple IMEs to choose from
748 */
749 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
750 final boolean shouldIncludeAuxiliarySubtypes) {
751 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
752
753 // Number of the filtered IMEs
754 int filteredImisCount = 0;
755
756 for (InputMethodInfo imi : enabledImis) {
757 // We can return true immediately after we find two or more filtered IMEs.
758 if (filteredImisCount > 1) return true;
759 final List<InputMethodSubtype> subtypes =
760 imm.getEnabledInputMethodSubtypeList(imi, true);
761 // IMEs that have no subtypes should be counted.
762 if (subtypes.isEmpty()) {
763 ++filteredImisCount;
764 continue;
765 }
766
767 int auxCount = 0;
768 for (InputMethodSubtype subtype : subtypes) {
769 if (subtype.isAuxiliary()) {
770 ++auxCount;
771 }
772 }
773 final int nonAuxCount = subtypes.size() - auxCount;
774
775 // IMEs that have one or more non-auxiliary subtypes should be counted.
776 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
777 // subtypes should be counted as well.
778 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
779 ++filteredImisCount;
780 continue;
781 }
782 }
783
784 return filteredImisCount > 1
785 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
786 // input method subtype (The current IME should be LatinIME.)
787 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
788 }
789
Jason parks8fd5bc92011-01-12 16:03:31 -0600790 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700791 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600792 if (service != null) {
793 return IMountService.Stub.asInterface(service);
794 }
795 return null;
796 }
797
798 @Override
799 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500800 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600801 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700802 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600803
Jason parksec5a45e2011-01-18 15:28:36 -0600804 if (TextUtils.isEmpty(password)) {
805 return true;
806 }
Jason parks35933812011-01-21 15:48:20 -0600807
Jason parks8fd5bc92011-01-12 16:03:31 -0600808 // Now that we have the password clear the password field.
809 v.setText(null);
810
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700811 // Disable the password entry and back keypress while checking the password. These
812 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600813 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800814 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600815
Ben Komalo04606752011-08-18 14:50:26 -0700816 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600817 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600818
Jason parks8fd5bc92011-01-12 16:03:31 -0600819 return true;
820 }
821 return false;
822 }
David Brown8373b452011-06-20 12:38:45 -0700823
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700824 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700825 * Set airplane mode on the device if it isn't an LTE device.
826 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
827 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
828 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
829 * both these problems, we turn the radio off. However, on certain networks turning on and
830 * off the radio takes a long time. In such cases, we are better off leaving the radio
831 * running so the latency of an E911 call is short.
832 * The behavior after this is:
833 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
834 * airplane mode and restart the radio.
835 * 2. Full boot: we read the persistent settings from the previous boot and restore the
836 * radio to whatever it was before it restarted. This also happens when rebooting a
837 * phone that has no encryption.
838 */
839 private final void setAirplaneModeIfNecessary() {
840 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700841 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700842 if (!isLteDevice) {
843 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700844 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700845 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
846 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700847 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700848 }
849 }
850
851 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700852 * Code to update the state of, and handle clicks from, the "Emergency call" button.
853 *
854 * This code is mostly duplicated from the corresponding code in
855 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
856 */
David Brown8373b452011-06-20 12:38:45 -0700857 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700858 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700859 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700860 if (emergencyCall == null)
861 return;
David Brown8373b452011-06-20 12:38:45 -0700862
863 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700864 emergencyCall.setVisibility(View.VISIBLE);
865 emergencyCall.setOnClickListener(new View.OnClickListener() {
866 @Override
867
David Brown8373b452011-06-20 12:38:45 -0700868 public void onClick(View v) {
869 takeEmergencyCallAction();
870 }
871 });
872 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700873 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700874 return;
875 }
876
David Brown8373b452011-06-20 12:38:45 -0700877 int textId;
Santos Cordon35230e92014-07-07 16:25:10 -0700878 if (getPhoneManager().isInAPhoneCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700879 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700880 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700881 } else {
882 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700883 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700884 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700885 }
886
887 private boolean isEmergencyCallCapable() {
888 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
889 }
890
891 private void takeEmergencyCallAction() {
Santos Cordon35230e92014-07-07 16:25:10 -0700892 PhoneManager phoneManager = getPhoneManager();
893 if (phoneManager.isInAPhoneCall()) {
894 phoneManager.showCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700895 } else {
896 launchEmergencyDialer();
897 }
898 }
899
David Brown8373b452011-06-20 12:38:45 -0700900
901 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700902 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700903 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
904 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700905 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700906 startActivity(intent);
907 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700908
Santos Cordon3afbdf02014-05-29 21:48:16 -0700909 private TelephonyManager getTelephonyManager() {
910 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
911 }
912
Santos Cordon35230e92014-07-07 16:25:10 -0700913 private PhoneManager getPhoneManager() {
914 return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
915 }
916
Vikram Aggarwald1147252012-05-08 13:52:30 -0700917 /**
918 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
919 */
920 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700921 mNotificationCountdown = 20;
922 }
923
924 @Override
925 public boolean onKey(View v, int keyCode, KeyEvent event) {
926 delayAudioNotification();
927 return false;
928 }
929
930 @Override
931 public boolean onTouch(View v, MotionEvent event) {
932 delayAudioNotification();
933 return false;
934 }
935
936 @Override
937 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
938 return;
939 }
940
941 @Override
942 public void onTextChanged(CharSequence s, int start, int before, int count) {
943 delayAudioNotification();
944 }
945
946 @Override
947 public void afterTextChanged(Editable s) {
948 return;
949 }
David Brown8373b452011-06-20 12:38:45 -0700950}