blob: 86e7387792b9a227421d82b54e1de75f34b2d5b8 [file] [log] [blame]
Jason parks8fd5bc92011-01-12 16:03:31 -06001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason parks8fd5bc92011-01-12 16:03:31 -060019import android.app.Activity;
20import android.app.StatusBarManager;
21import android.content.ComponentName;
22import android.content.Context;
Jason parksec5a45e2011-01-18 15:28:36 -060023import android.content.Intent;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070024import android.content.pm.ActivityInfo;
Jason parks8fd5bc92011-01-12 16:03:31 -060025import android.content.pm.PackageManager;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070026import android.content.res.Resources.NotFoundException;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070027import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060028import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060031import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060032import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060033import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070034import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060035import android.os.ServiceManager;
36import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070037import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080039import android.os.storage.StorageManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070040import android.provider.Settings;
Nancy Chendb848a52014-08-21 22:53:05 -070041import android.telecomm.TelecommManager;
David Brown8373b452011-06-20 12:38:45 -070042import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070043import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060044import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070045import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070046import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060047import android.util.Log;
48import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070049import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080050import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080051import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080052import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070053import android.view.View.OnKeyListener;
54import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060055import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070056import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060057import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070058import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080059import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060060import android.widget.EditText;
61import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060062import android.widget.TextView;
63
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080064import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070065import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070066import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080067import com.android.internal.widget.LockPatternUtils;
68import com.android.internal.widget.LockPatternView;
69import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070070
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070071import static com.android.internal.widget.LockPatternView.DisplayMode;
72
Ben Komalo9fcb6a72011-08-26 14:40:18 -070073import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070074
75/**
76 * Settings screens to show the UI flows for encrypting/decrypting the device.
77 *
78 * This may be started via adb for debugging the UI layout, without having to go through
79 * encryption flows everytime. It should be noted that starting the activity in this manner
80 * is only useful for verifying UI-correctness - the behavior will not be identical.
81 * <pre>
82 * $ adb shell pm enable com.android.settings/.CryptKeeper
83 * $ adb shell am start \
84 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
85 * -n com.android.settings/.CryptKeeper
86 * </pre>
87 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070088public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
89 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060090 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060091
Jason parks8fd5bc92011-01-12 16:03:31 -060092 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070093 /** Message sent to us to indicate encryption update progress. */
94 private static final int MESSAGE_UPDATE_PROGRESS = 1;
95 /** Message sent to us to cool-down (waste user's time between password attempts) */
96 private static final int MESSAGE_COOLDOWN = 2;
97 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
98 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060099
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700100 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600101 private static final int MAX_FAILED_ATTEMPTS = 30;
102 private static final int COOL_DOWN_ATTEMPTS = 10;
103 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
104
David Brown8373b452011-06-20 12:38:45 -0700105 // Intent action for launching the Emergency Dialer activity.
106 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
107
Ben Komalo91a2f052011-08-16 17:48:25 -0700108 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
109 private static final String EXTRA_FORCE_VIEW =
110 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
111 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700112 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700113 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700114
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700115 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700116 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700117 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700118 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
119 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700120 /** A flag to indicate when the back event should be ignored */
121 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800122 private int mCooldown;
123 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600124 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800125 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700126 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
127 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700128 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
129 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700130 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800131
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700132 // how long we wait to clear a wrong pattern
133 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
134
135 private Runnable mClearPatternRunnable = new Runnable() {
136 public void run() {
137 mLockPatternView.clearPattern();
138 }
139 };
140
Andy Stadler14997402011-02-01 15:34:59 -0800141 /**
142 * Used to propagate state through configuration changes (e.g. screen rotation)
143 */
144 private static class NonConfigurationInstanceState {
145 final PowerManager.WakeLock wakelock;
146
147 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
148 wakelock = _wakelock;
149 }
150 }
151
Jason parks06c5ff42011-03-01 10:17:40 -0600152 private class DecryptTask extends AsyncTask<String, Void, Integer> {
153 @Override
154 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700155 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600156 try {
157 return service.decryptStorage(params[0]);
158 } catch (Exception e) {
159 Log.e(TAG, "Error while decrypting...", e);
160 return -1;
161 }
162 }
163
164 @Override
165 protected void onPostExecute(Integer failedAttempts) {
166 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700167 // The password was entered successfully. Simply do nothing
168 // and wait for the service restart to switch to surfacefligner
Jason parks06c5ff42011-03-01 10:17:40 -0600169 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
170 // Factory reset the device.
171 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Paul Lawrence384d8e52014-08-08 15:11:36 -0700172 } else if (failedAttempts == -1) {
173 // Right password, but decryption failed. Tell user bad news ...
174 setContentView(R.layout.crypt_keeper_progress);
175 showFactoryReset(true);
176 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600177 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700178 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700179 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700180 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
181 mLockPatternView.removeCallbacks(mClearPatternRunnable);
182 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
183 }
184 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
185 mCooldown = COOL_DOWN_INTERVAL;
186 cooldown();
187 } else {
188 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700189
190 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
191 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
192 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
193 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
194 Integer.toString(remainingAttempts));
195 status.setText(warning);
196 } else {
197 status.setText(R.string.try_again);
198 }
199
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700200 if (mLockPatternView != null) {
201 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
202 }
203 // Reenable the password entry
204 if (mPasswordEntry != null) {
205 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700206 final InputMethodManager imm = (InputMethodManager) getSystemService(
207 Context.INPUT_METHOD_SERVICE);
208 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700209 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700210 }
211 if (mLockPatternView != null) {
212 mLockPatternView.setEnabled(true);
213 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700214 }
Jason parks06c5ff42011-03-01 10:17:40 -0600215 }
216 }
217 }
Jason parks75c085e2011-02-10 14:03:47 -0600218
Ben Komalo0e666092011-09-01 15:42:00 -0700219 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
220 @Override
221 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700222 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700223 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700224 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700225 int state = service.getEncryptionState();
226 if (state == IMountService.ENCRYPTION_STATE_NONE) {
227 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
228 return true; // Unexpected, but fine, I guess...
229 }
230 return state == IMountService.ENCRYPTION_STATE_OK;
231 } catch (RemoteException e) {
232 Log.w(TAG, "Unable to get encryption state properly");
233 return true;
234 }
235 }
236
237 @Override
238 protected void onPostExecute(Boolean result) {
239 mValidationComplete = true;
240 if (Boolean.FALSE.equals(result)) {
241 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
242 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700243 } else {
244 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700245 }
246 setupUi();
247 }
248 }
249
Ben Komalo91a2f052011-08-16 17:48:25 -0700250 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600251 @Override
252 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600253 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700254 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600255 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600256 break;
Jason parks35933812011-01-21 15:48:20 -0600257
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700258 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600259 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600260 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700261
262 case MESSAGE_NOTIFY:
263 notifyUser();
264 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600265 }
266 }
267 };
Jason parks35933812011-01-21 15:48:20 -0600268
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700269 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800270 /** The status bar where back/home/recent buttons are shown. */
271 private StatusBarManager mStatusBar;
272
273 /** All the widgets to disable in the status bar */
274 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
275 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
276 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
277 | StatusBarManager.DISABLE_SYSTEM_INFO
278 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700279 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800280 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700281
Ben Komalo91a2f052011-08-16 17:48:25 -0700282 /** @return whether or not this Activity was started for debugging the UI only. */
283 private boolean isDebugView() {
284 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
285 }
286
287 /** @return whether or not this Activity was started for debugging the specific UI view only. */
288 private boolean isDebugView(String viewType /* non-nullable */) {
289 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
290 }
291
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700292 /**
293 * Notify the user that we are awaiting input. Currently this sends an audio alert.
294 */
295 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700296 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700297 --mNotificationCountdown;
298 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700299 try {
300 // Play the standard keypress sound at full volume. This should be available on
301 // every device. We cannot play a ringtone here because media services aren't
302 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
303 // on tablet devices. The idea is to alert the user that something is needed: this
304 // does not have to be pleasing.
305 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
306 } catch (Exception e) {
307 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
308 }
309 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700310 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700311 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700312 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700313
314 if (mWakeLock.isHeld()) {
315 if (mReleaseWakeLockCountdown > 0) {
316 --mReleaseWakeLockCountdown;
317 } else {
318 mWakeLock.release();
319 }
320 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700321 }
322
323 /**
324 * Ignore back events after the user has entered the decrypt screen and while the device is
325 * encrypting.
326 */
327 @Override
328 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800329 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700330 if (mIgnoreBack)
331 return;
332 super.onBackPressed();
333 }
334
Jason parks8fd5bc92011-01-12 16:03:31 -0600335 @Override
336 public void onCreate(Bundle savedInstanceState) {
337 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600338
Andy Stadler95974062011-02-01 17:35:20 -0800339 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700340 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700341 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600342 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600343 PackageManager pm = getPackageManager();
344 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700345 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
346 PackageManager.DONT_KILL_APP);
347 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700348 // want to be running, so need to finish this activity. We can count
349 // on the activity manager re-launching the new home app upon finishing
350 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700351 // NOTE: This is really grungy. I think it would be better for the
352 // activity manager to explicitly launch the crypt keeper instead of
353 // home in the situation where we need to decrypt the device
354 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600355 return;
356 }
Jason parks35933812011-01-21 15:48:20 -0600357
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700358 try {
359 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
360 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
361 }
362 } catch (NotFoundException e) {
363 }
364
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700365 // Disable the status bar, but do NOT disable back because the user needs a way to go
366 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800367 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
368 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800369
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700370 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700371 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800372 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700373 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800374 if (lastInstance instanceof NonConfigurationInstanceState) {
375 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
376 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700377 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800378 }
Jason parksec5a45e2011-01-18 15:28:36 -0600379 }
Jason parks35933812011-01-21 15:48:20 -0600380
Andy Stadler95974062011-02-01 17:35:20 -0800381 /**
382 * Note, we defer the state check and screen setup to onStart() because this will be
383 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
384 * especially important if we were to lose the wakelock for any reason.
385 */
386 @Override
387 public void onStart() {
388 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700389 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700390 }
391
392 /**
393 * Initializes the UI based on the current state of encryption.
394 * This is idempotent - calling repeatedly will simply re-initialize the UI.
395 */
396 private void setupUi() {
397 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
398 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700399 showFactoryReset(false);
Ben Komalo0e666092011-09-01 15:42:00 -0700400 return;
401 }
402
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700403 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700404 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800405 setContentView(R.layout.crypt_keeper_progress);
406 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700407 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700408 new AsyncTask<Void, Void, Void>() {
409 int type = StorageManager.CRYPT_TYPE_PASSWORD;
410 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800411
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700412 @Override
413 public Void doInBackground(Void... v) {
414 try {
415 final IMountService service = getMountService();
416 type = service.getPasswordType();
417 owner_info = service.getField("OwnerInfo");
418 } catch (Exception e) {
419 Log.e(TAG, "Error calling mount service " + e);
420 }
421
422 return null;
423 }
424
425 @Override
426 public void onPostExecute(java.lang.Void v) {
427 if(type == StorageManager.CRYPT_TYPE_PIN) {
428 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700429 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700430 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
431 setContentView(R.layout.crypt_keeper_pattern_entry);
432 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700433 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700434 } else {
435 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700436 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700437 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700438 final TextView status = (TextView) findViewById(R.id.status);
439 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700440
Paul Lawrence5a70f052014-06-13 11:09:55 -0700441 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
442 ownerInfo.setText(owner_info);
443 ownerInfo.setSelected(true); // Required for marquee'ing to work
444
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700445 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700446
447 if (mCooldown > 0) {
448 setBackFunctionality(false);
449 cooldown(); // in case we are cooling down and coming back from emergency dialler
450 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700451 }
452 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700453 } else if (!mValidationRequested) {
454 // We're supposed to be encrypted, but no validation has been done.
455 new ValidationTask().execute((Void[]) null);
456 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800457 }
458 }
459
Jason parksf8217302011-01-26 13:11:42 -0600460 @Override
461 public void onStop() {
462 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700463 mHandler.removeMessages(MESSAGE_COOLDOWN);
464 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
465 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800466 }
467
468 /**
469 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
470 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
471 * mWakeLock so the subsequent call to onDestroy does not release it.
472 */
473 @Override
474 public Object onRetainNonConfigurationInstance() {
475 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700476 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800477 mWakeLock = null;
478 return state;
479 }
480
481 @Override
482 public void onDestroy() {
483 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600484
485 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700486 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600487 mWakeLock.release();
488 mWakeLock = null;
489 }
490 }
491
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700492 /**
493 * Start encrypting the device.
494 */
Jason parksec5a45e2011-01-18 15:28:36 -0600495 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600496 // Accquire a partial wakelock to prevent the device from sleeping. Note
497 // we never release this wakelock as we will be restarted after the device
498 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700499 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700500 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700501 Log.d(TAG, "Acquiring wakelock.");
502 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
503 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
504 mWakeLock.acquire();
505 }
Jason parks35933812011-01-21 15:48:20 -0600506
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700507 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700508 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800509 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700510 // Start the first run of progress manually. This method sets up messages to occur at
511 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600512 updateProgress();
513 }
514
Paul Lawrence384d8e52014-08-08 15:11:36 -0700515 /**
516 * Show factory reset screen allowing the user to reset their phone when
517 * there is nothing else we can do
518 * @param corrupt true if userdata is corrupt, false if encryption failed
519 * partway through
520 */
521 private void showFactoryReset(boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800522 // Hide the encryption-bot to make room for the "factory reset" button
523 findViewById(R.id.encroid).setVisibility(View.GONE);
524
525 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700526 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800527 button.setVisibility(View.VISIBLE);
528 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700529 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800530 public void onClick(View v) {
531 // Factory reset the device.
532 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
533 }
534 });
535
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700536 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700537 if (corrupt) {
538 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
539 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
540 } else {
541 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
542 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
543 }
Andy Stadler13d62042011-01-31 19:21:37 -0800544
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700545 final View view = findViewById(R.id.bottom_divider);
546 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700547 if (view != null) {
548 view.setVisibility(View.VISIBLE);
549 }
Andy Stadler13d62042011-01-31 19:21:37 -0800550 }
551
Jason parksf8217302011-01-26 13:11:42 -0600552 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700553 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600554
Ben Komalo0e666092011-09-01 15:42:00 -0700555 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700556 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800557 return;
558 }
559
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700560 // Get status as percentage first
561 CharSequence status = getText(R.string.crypt_keeper_setup_description);
562 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600563 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700564 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700565 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600566 } catch (Exception e) {
567 Log.w(TAG, "Error parsing progress: " + e.toString());
568 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700569 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600570
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700571 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700572 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700573 try {
574 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
575 int time = Integer.parseInt(timeProperty);
576 if (time >= 0) {
577 // Round up to multiple of 10 - this way display is less jerky
578 time = (time + 9) / 10 * 10;
579 progress = DateUtils.formatElapsedTime(time);
580 status = getText(R.string.crypt_keeper_setup_time_remaining);
581 }
582 } catch (Exception e) {
583 // Will happen if no time etc - show percentage
584 }
585
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700586 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700587 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700588 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700589 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700590
591 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700592 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700593 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600594 }
595
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700596 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600597 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700598 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800599
Jason parksf8217302011-01-26 13:11:42 -0600600 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700601 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700602 if (mPasswordEntry != null) {
603 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700604 final InputMethodManager imm = (InputMethodManager) getSystemService(
605 Context.INPUT_METHOD_SERVICE);
606 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700607 setBackFunctionality(true);
608 }
609 if (mLockPatternView != null) {
610 mLockPatternView.setEnabled(true);
611 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700612 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600613 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700614 // Disable the password entry and back presses.
615 if (mPasswordEntry != null) {
616 mPasswordEntry.setEnabled(false);
617 }
618 if (mLockPatternView != null) {
619 mLockPatternView.setEnabled(false);
620 }
621
Andy Stadler13d62042011-01-31 19:21:37 -0800622 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700623 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800624
Jason parksf8217302011-01-26 13:11:42 -0600625 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700626 mHandler.removeMessages(MESSAGE_COOLDOWN);
627 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600628 }
Jason parksec5a45e2011-01-18 15:28:36 -0600629 }
Jason parks35933812011-01-21 15:48:20 -0600630
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800631 /**
632 * Sets the back status: enabled or disabled according to the parameter.
633 * @param isEnabled true if back is enabled, false otherwise.
634 */
635 private final void setBackFunctionality(boolean isEnabled) {
636 mIgnoreBack = !isEnabled;
637 if (isEnabled) {
638 mStatusBar.disable(sWidgetsToDisable);
639 } else {
640 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
641 }
642 }
643
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800644 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700645 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800646
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700647 @Override
648 public void onPatternStart() {
649 mLockPatternView.removeCallbacks(mClearPatternRunnable);
650 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800651
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700652 @Override
653 public void onPatternCleared() {
654 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800655
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700656 @Override
657 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
658 mLockPatternView.setEnabled(false);
659 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
660 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800661
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700662 @Override
663 public void onPatternCellAdded(List<Cell> pattern) {
664 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800665 };
666
667 private void passwordEntryInit() {
668 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600669 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800670 if (mPasswordEntry != null){
671 mPasswordEntry.setOnEditorActionListener(this);
672 mPasswordEntry.requestFocus();
673 // Become quiet when the user interacts with the Edit text screen.
674 mPasswordEntry.setOnKeyListener(this);
675 mPasswordEntry.setOnTouchListener(this);
676 mPasswordEntry.addTextChangedListener(this);
677 }
678
679 // Pattern case
680 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
681 if (mLockPatternView != null) {
682 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
683 }
Jason parks35933812011-01-21 15:48:20 -0600684
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700685 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700686 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700687 final View emergencyCall = findViewById(R.id.emergencyCallButton);
688 if (emergencyCall != null) {
689 Log.d(TAG, "Removing the emergency Call button");
690 emergencyCall.setVisibility(View.GONE);
691 }
692 }
693
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700694 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700695 final InputMethodManager imm = (InputMethodManager) getSystemService(
696 Context.INPUT_METHOD_SERVICE);
697 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
698 imeSwitcher.setVisibility(View.VISIBLE);
699 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700700 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700701 public void onClick(View v) {
702 imm.showInputMethodPicker();
703 }
704 });
Jason parks00046d62011-06-13 17:38:45 -0500705 }
David Brown8373b452011-06-20 12:38:45 -0700706
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700707 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
708 // is completely non-functional, and we want the user to notice the device and enter a
709 // password.
710 if (mWakeLock == null) {
711 Log.d(TAG, "Acquiring wakelock.");
712 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
713 if (pm != null) {
714 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
715 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700716 // Keep awake for 10 minutes - if the user hasn't been alerted by then
717 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700718 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700719 }
720 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700721
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700722 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
723 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700724 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700725 mHandler.postDelayed(new Runnable() {
726 @Override public void run() {
727 imm.showSoftInputUnchecked(0, null);
728 }
729 }, 0);
730 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700731
David Brown8373b452011-06-20 12:38:45 -0700732 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700733 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700734 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700735 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800736
737 // Dismiss keyguard while this screen is showing.
738 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600739 }
740
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700741 /**
742 * Method adapted from com.android.inputmethod.latin.Utils
743 *
744 * @param imm The input method manager
745 * @param shouldIncludeAuxiliarySubtypes
746 * @return true if we have multiple IMEs to choose from
747 */
748 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
749 final boolean shouldIncludeAuxiliarySubtypes) {
750 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
751
752 // Number of the filtered IMEs
753 int filteredImisCount = 0;
754
755 for (InputMethodInfo imi : enabledImis) {
756 // We can return true immediately after we find two or more filtered IMEs.
757 if (filteredImisCount > 1) return true;
758 final List<InputMethodSubtype> subtypes =
759 imm.getEnabledInputMethodSubtypeList(imi, true);
760 // IMEs that have no subtypes should be counted.
761 if (subtypes.isEmpty()) {
762 ++filteredImisCount;
763 continue;
764 }
765
766 int auxCount = 0;
767 for (InputMethodSubtype subtype : subtypes) {
768 if (subtype.isAuxiliary()) {
769 ++auxCount;
770 }
771 }
772 final int nonAuxCount = subtypes.size() - auxCount;
773
774 // IMEs that have one or more non-auxiliary subtypes should be counted.
775 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
776 // subtypes should be counted as well.
777 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
778 ++filteredImisCount;
779 continue;
780 }
781 }
782
783 return filteredImisCount > 1
784 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
785 // input method subtype (The current IME should be LatinIME.)
786 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
787 }
788
Jason parks8fd5bc92011-01-12 16:03:31 -0600789 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700790 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600791 if (service != null) {
792 return IMountService.Stub.asInterface(service);
793 }
794 return null;
795 }
796
797 @Override
798 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500799 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600800 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700801 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600802
Jason parksec5a45e2011-01-18 15:28:36 -0600803 if (TextUtils.isEmpty(password)) {
804 return true;
805 }
Jason parks35933812011-01-21 15:48:20 -0600806
Jason parks8fd5bc92011-01-12 16:03:31 -0600807 // Now that we have the password clear the password field.
808 v.setText(null);
809
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700810 // Disable the password entry and back keypress while checking the password. These
811 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600812 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800813 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600814
Ben Komalo04606752011-08-18 14:50:26 -0700815 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600816 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600817
Jason parks8fd5bc92011-01-12 16:03:31 -0600818 return true;
819 }
820 return false;
821 }
David Brown8373b452011-06-20 12:38:45 -0700822
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700823 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700824 * Set airplane mode on the device if it isn't an LTE device.
825 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
826 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
827 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
828 * both these problems, we turn the radio off. However, on certain networks turning on and
829 * off the radio takes a long time. In such cases, we are better off leaving the radio
830 * running so the latency of an E911 call is short.
831 * The behavior after this is:
832 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
833 * airplane mode and restart the radio.
834 * 2. Full boot: we read the persistent settings from the previous boot and restore the
835 * radio to whatever it was before it restarted. This also happens when rebooting a
836 * phone that has no encryption.
837 */
838 private final void setAirplaneModeIfNecessary() {
839 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700840 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700841 if (!isLteDevice) {
842 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700843 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700844 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
845 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700846 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700847 }
848 }
849
850 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700851 * Code to update the state of, and handle clicks from, the "Emergency call" button.
852 *
853 * This code is mostly duplicated from the corresponding code in
854 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
855 */
David Brown8373b452011-06-20 12:38:45 -0700856 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700857 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700858 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700859 if (emergencyCall == null)
860 return;
David Brown8373b452011-06-20 12:38:45 -0700861
862 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700863 emergencyCall.setVisibility(View.VISIBLE);
864 emergencyCall.setOnClickListener(new View.OnClickListener() {
865 @Override
866
David Brown8373b452011-06-20 12:38:45 -0700867 public void onClick(View v) {
868 takeEmergencyCallAction();
869 }
870 });
871 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700872 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700873 return;
874 }
875
David Brown8373b452011-06-20 12:38:45 -0700876 int textId;
Nancy Chendb848a52014-08-21 22:53:05 -0700877 if (getTelecommManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700878 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700879 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700880 } else {
881 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700882 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700883 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700884 }
885
886 private boolean isEmergencyCallCapable() {
887 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
888 }
889
890 private void takeEmergencyCallAction() {
Nancy Chendb848a52014-08-21 22:53:05 -0700891 TelecommManager telecommManager = getTelecommManager();
892 if (telecommManager.isInCall()) {
893 telecommManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700894 } else {
895 launchEmergencyDialer();
896 }
897 }
898
David Brown8373b452011-06-20 12:38:45 -0700899
900 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700901 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700902 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
903 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700904 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700905 startActivity(intent);
906 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700907
Santos Cordon3afbdf02014-05-29 21:48:16 -0700908 private TelephonyManager getTelephonyManager() {
909 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
910 }
911
Nancy Chendb848a52014-08-21 22:53:05 -0700912 private TelecommManager getTelecommManager() {
913 return (TelecommManager) getSystemService(Context.TELECOMM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700914 }
915
Vikram Aggarwald1147252012-05-08 13:52:30 -0700916 /**
917 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
918 */
919 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700920 mNotificationCountdown = 20;
921 }
922
923 @Override
924 public boolean onKey(View v, int keyCode, KeyEvent event) {
925 delayAudioNotification();
926 return false;
927 }
928
929 @Override
930 public boolean onTouch(View v, MotionEvent event) {
931 delayAudioNotification();
932 return false;
933 }
934
935 @Override
936 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
937 return;
938 }
939
940 @Override
941 public void onTextChanged(CharSequence s, int start, int before, int count) {
942 delayAudioNotification();
943 }
944
945 @Override
946 public void afterTextChanged(Editable s) {
947 return;
948 }
David Brown8373b452011-06-20 12:38:45 -0700949}