blob: 129b201847d117e265bed280a7904cfd3fbd0ac2 [file] [log] [blame]
Jason parks8fd5bc92011-01-12 16:03:31 -06001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason parks8fd5bc92011-01-12 16:03:31 -060019import android.app.Activity;
20import android.app.StatusBarManager;
21import android.content.ComponentName;
22import android.content.Context;
Jason parksec5a45e2011-01-18 15:28:36 -060023import android.content.Intent;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070024import android.content.pm.ActivityInfo;
Jason parks8fd5bc92011-01-12 16:03:31 -060025import android.content.pm.PackageManager;
Paul Lawrence7ae20e32014-07-22 15:00:50 -070026import android.content.res.Resources.NotFoundException;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070027import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060028import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060031import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060032import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060033import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070034import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060035import android.os.ServiceManager;
36import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070037import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080039import android.os.storage.StorageManager;
Santos Cordon35230e92014-07-07 16:25:10 -070040
41import android.phone.PhoneManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070042import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070043import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060045import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070046import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070047import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060048import android.util.Log;
49import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070050import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080051import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080052import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080053import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070054import android.view.View.OnKeyListener;
55import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060056import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070057import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060058import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070059import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080060import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060061import android.widget.EditText;
62import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060063import android.widget.TextView;
64
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080065import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070066import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070067import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080068import com.android.internal.widget.LockPatternUtils;
69import com.android.internal.widget.LockPatternView;
70import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070071
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070072import static com.android.internal.widget.LockPatternView.DisplayMode;
73
Ben Komalo9fcb6a72011-08-26 14:40:18 -070074import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070075
76/**
77 * Settings screens to show the UI flows for encrypting/decrypting the device.
78 *
79 * This may be started via adb for debugging the UI layout, without having to go through
80 * encryption flows everytime. It should be noted that starting the activity in this manner
81 * is only useful for verifying UI-correctness - the behavior will not be identical.
82 * <pre>
83 * $ adb shell pm enable com.android.settings/.CryptKeeper
84 * $ adb shell am start \
85 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
86 * -n com.android.settings/.CryptKeeper
87 * </pre>
88 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070089public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
90 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060091 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060092
Jason parks8fd5bc92011-01-12 16:03:31 -060093 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070094 /** Message sent to us to indicate encryption update progress. */
95 private static final int MESSAGE_UPDATE_PROGRESS = 1;
96 /** Message sent to us to cool-down (waste user's time between password attempts) */
97 private static final int MESSAGE_COOLDOWN = 2;
98 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
99 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -0600100
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700101 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600102 private static final int MAX_FAILED_ATTEMPTS = 30;
103 private static final int COOL_DOWN_ATTEMPTS = 10;
104 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
105
David Brown8373b452011-06-20 12:38:45 -0700106 // Intent action for launching the Emergency Dialer activity.
107 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
108
Ben Komalo91a2f052011-08-16 17:48:25 -0700109 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
110 private static final String EXTRA_FORCE_VIEW =
111 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
112 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700113 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700114 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700115
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700116 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700117 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700118 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700119 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
120 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700121 /** A flag to indicate when the back event should be ignored */
122 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800123 private int mCooldown;
124 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600125 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800126 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700127 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
128 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700129 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
130 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700131 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800132
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700133 // how long we wait to clear a wrong pattern
134 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
135
136 private Runnable mClearPatternRunnable = new Runnable() {
137 public void run() {
138 mLockPatternView.clearPattern();
139 }
140 };
141
Andy Stadler14997402011-02-01 15:34:59 -0800142 /**
143 * Used to propagate state through configuration changes (e.g. screen rotation)
144 */
145 private static class NonConfigurationInstanceState {
146 final PowerManager.WakeLock wakelock;
147
148 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
149 wakelock = _wakelock;
150 }
151 }
152
Jason parks06c5ff42011-03-01 10:17:40 -0600153 private class DecryptTask extends AsyncTask<String, Void, Integer> {
154 @Override
155 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700156 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600157 try {
158 return service.decryptStorage(params[0]);
159 } catch (Exception e) {
160 Log.e(TAG, "Error while decrypting...", e);
161 return -1;
162 }
163 }
164
165 @Override
166 protected void onPostExecute(Integer failedAttempts) {
167 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700168 // The password was entered successfully. Simply do nothing
169 // and wait for the service restart to switch to surfacefligner
Jason parks06c5ff42011-03-01 10:17:40 -0600170 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
171 // Factory reset the device.
172 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parks06c5ff42011-03-01 10:17:40 -0600173 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700174 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700175 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700176 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
177 mLockPatternView.removeCallbacks(mClearPatternRunnable);
178 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
179 }
180 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
181 mCooldown = COOL_DOWN_INTERVAL;
182 cooldown();
183 } else {
184 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700185
186 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
187 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
188 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
189 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
190 Integer.toString(remainingAttempts));
191 status.setText(warning);
192 } else {
193 status.setText(R.string.try_again);
194 }
195
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700196 if (mLockPatternView != null) {
197 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
198 }
199 // Reenable the password entry
200 if (mPasswordEntry != null) {
201 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700202 final InputMethodManager imm = (InputMethodManager) getSystemService(
203 Context.INPUT_METHOD_SERVICE);
204 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700205 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700206 }
207 if (mLockPatternView != null) {
208 mLockPatternView.setEnabled(true);
209 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700210 }
Jason parks06c5ff42011-03-01 10:17:40 -0600211 }
212 }
213 }
Jason parks75c085e2011-02-10 14:03:47 -0600214
Ben Komalo0e666092011-09-01 15:42:00 -0700215 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
216 @Override
217 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700218 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700219 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700220 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700221 int state = service.getEncryptionState();
222 if (state == IMountService.ENCRYPTION_STATE_NONE) {
223 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
224 return true; // Unexpected, but fine, I guess...
225 }
226 return state == IMountService.ENCRYPTION_STATE_OK;
227 } catch (RemoteException e) {
228 Log.w(TAG, "Unable to get encryption state properly");
229 return true;
230 }
231 }
232
233 @Override
234 protected void onPostExecute(Boolean result) {
235 mValidationComplete = true;
236 if (Boolean.FALSE.equals(result)) {
237 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
238 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700239 } else {
240 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700241 }
242 setupUi();
243 }
244 }
245
Ben Komalo91a2f052011-08-16 17:48:25 -0700246 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600247 @Override
248 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600249 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700250 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600251 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600252 break;
Jason parks35933812011-01-21 15:48:20 -0600253
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700254 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600255 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600256 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700257
258 case MESSAGE_NOTIFY:
259 notifyUser();
260 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600261 }
262 }
263 };
Jason parks35933812011-01-21 15:48:20 -0600264
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700265 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800266 /** The status bar where back/home/recent buttons are shown. */
267 private StatusBarManager mStatusBar;
268
269 /** All the widgets to disable in the status bar */
270 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
271 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
272 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
273 | StatusBarManager.DISABLE_SYSTEM_INFO
274 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700275 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800276 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700277
Ben Komalo91a2f052011-08-16 17:48:25 -0700278 /** @return whether or not this Activity was started for debugging the UI only. */
279 private boolean isDebugView() {
280 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
281 }
282
283 /** @return whether or not this Activity was started for debugging the specific UI view only. */
284 private boolean isDebugView(String viewType /* non-nullable */) {
285 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
286 }
287
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700288 /**
289 * Notify the user that we are awaiting input. Currently this sends an audio alert.
290 */
291 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700292 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700293 --mNotificationCountdown;
294 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700295 try {
296 // Play the standard keypress sound at full volume. This should be available on
297 // every device. We cannot play a ringtone here because media services aren't
298 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
299 // on tablet devices. The idea is to alert the user that something is needed: this
300 // does not have to be pleasing.
301 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
302 } catch (Exception e) {
303 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
304 }
305 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700306 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700307 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700308 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700309
310 if (mWakeLock.isHeld()) {
311 if (mReleaseWakeLockCountdown > 0) {
312 --mReleaseWakeLockCountdown;
313 } else {
314 mWakeLock.release();
315 }
316 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700317 }
318
319 /**
320 * Ignore back events after the user has entered the decrypt screen and while the device is
321 * encrypting.
322 */
323 @Override
324 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800325 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700326 if (mIgnoreBack)
327 return;
328 super.onBackPressed();
329 }
330
Jason parks8fd5bc92011-01-12 16:03:31 -0600331 @Override
332 public void onCreate(Bundle savedInstanceState) {
333 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600334
Andy Stadler95974062011-02-01 17:35:20 -0800335 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700336 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700337 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600338 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600339 PackageManager pm = getPackageManager();
340 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700341 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
342 PackageManager.DONT_KILL_APP);
343 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700344 // want to be running, so need to finish this activity. We can count
345 // on the activity manager re-launching the new home app upon finishing
346 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700347 // NOTE: This is really grungy. I think it would be better for the
348 // activity manager to explicitly launch the crypt keeper instead of
349 // home in the situation where we need to decrypt the device
350 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600351 return;
352 }
Jason parks35933812011-01-21 15:48:20 -0600353
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700354 try {
355 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
356 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
357 }
358 } catch (NotFoundException e) {
359 }
360
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700361 // Disable the status bar, but do NOT disable back because the user needs a way to go
362 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800363 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
364 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800365
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700366 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700367 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800368 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700369 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800370 if (lastInstance instanceof NonConfigurationInstanceState) {
371 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
372 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700373 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800374 }
Jason parksec5a45e2011-01-18 15:28:36 -0600375 }
Jason parks35933812011-01-21 15:48:20 -0600376
Andy Stadler95974062011-02-01 17:35:20 -0800377 /**
378 * Note, we defer the state check and screen setup to onStart() because this will be
379 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
380 * especially important if we were to lose the wakelock for any reason.
381 */
382 @Override
383 public void onStart() {
384 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700385 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700386 }
387
388 /**
389 * Initializes the UI based on the current state of encryption.
390 * This is idempotent - calling repeatedly will simply re-initialize the UI.
391 */
392 private void setupUi() {
393 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
394 setContentView(R.layout.crypt_keeper_progress);
395 showFactoryReset();
396 return;
397 }
398
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700399 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700400 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800401 setContentView(R.layout.crypt_keeper_progress);
402 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700403 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700404 new AsyncTask<Void, Void, Void>() {
405 int type = StorageManager.CRYPT_TYPE_PASSWORD;
406 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800407
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700408 @Override
409 public Void doInBackground(Void... v) {
410 try {
411 final IMountService service = getMountService();
412 type = service.getPasswordType();
413 owner_info = service.getField("OwnerInfo");
414 } catch (Exception e) {
415 Log.e(TAG, "Error calling mount service " + e);
416 }
417
418 return null;
419 }
420
421 @Override
422 public void onPostExecute(java.lang.Void v) {
423 if(type == StorageManager.CRYPT_TYPE_PIN) {
424 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700425 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700426 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
427 setContentView(R.layout.crypt_keeper_pattern_entry);
428 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700429 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700430 } else {
431 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700432 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700433 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700434 final TextView status = (TextView) findViewById(R.id.status);
435 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700436
Paul Lawrence5a70f052014-06-13 11:09:55 -0700437 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
438 ownerInfo.setText(owner_info);
439 ownerInfo.setSelected(true); // Required for marquee'ing to work
440
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700441 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700442
443 if (mCooldown > 0) {
444 setBackFunctionality(false);
445 cooldown(); // in case we are cooling down and coming back from emergency dialler
446 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700447 }
448 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700449 } else if (!mValidationRequested) {
450 // We're supposed to be encrypted, but no validation has been done.
451 new ValidationTask().execute((Void[]) null);
452 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800453 }
454 }
455
Jason parksf8217302011-01-26 13:11:42 -0600456 @Override
457 public void onStop() {
458 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700459 mHandler.removeMessages(MESSAGE_COOLDOWN);
460 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
461 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800462 }
463
464 /**
465 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
466 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
467 * mWakeLock so the subsequent call to onDestroy does not release it.
468 */
469 @Override
470 public Object onRetainNonConfigurationInstance() {
471 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700472 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800473 mWakeLock = null;
474 return state;
475 }
476
477 @Override
478 public void onDestroy() {
479 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600480
481 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700482 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600483 mWakeLock.release();
484 mWakeLock = null;
485 }
486 }
487
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700488 /**
489 * Start encrypting the device.
490 */
Jason parksec5a45e2011-01-18 15:28:36 -0600491 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600492 // Accquire a partial wakelock to prevent the device from sleeping. Note
493 // we never release this wakelock as we will be restarted after the device
494 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700495 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700496 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700497 Log.d(TAG, "Acquiring wakelock.");
498 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
499 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
500 mWakeLock.acquire();
501 }
Jason parks35933812011-01-21 15:48:20 -0600502
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700503 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700504 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800505 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700506 // Start the first run of progress manually. This method sets up messages to occur at
507 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600508 updateProgress();
509 }
510
Andy Stadler13d62042011-01-31 19:21:37 -0800511 private void showFactoryReset() {
512 // Hide the encryption-bot to make room for the "factory reset" button
513 findViewById(R.id.encroid).setVisibility(View.GONE);
514
515 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700516 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800517 button.setVisibility(View.VISIBLE);
518 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700519 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800520 public void onClick(View v) {
521 // Factory reset the device.
522 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
523 }
524 });
525
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700526 // Alert the user of the failure.
527 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
528 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800529
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700530 final View view = findViewById(R.id.bottom_divider);
531 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700532 if (view != null) {
533 view.setVisibility(View.VISIBLE);
534 }
Andy Stadler13d62042011-01-31 19:21:37 -0800535 }
536
Jason parksf8217302011-01-26 13:11:42 -0600537 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700538 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600539
Ben Komalo0e666092011-09-01 15:42:00 -0700540 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800541 showFactoryReset();
542 return;
543 }
544
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700545 // Get status as percentage first
546 CharSequence status = getText(R.string.crypt_keeper_setup_description);
547 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600548 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700549 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700550 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600551 } catch (Exception e) {
552 Log.w(TAG, "Error parsing progress: " + e.toString());
553 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700554 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600555
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700556 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700557 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700558 try {
559 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
560 int time = Integer.parseInt(timeProperty);
561 if (time >= 0) {
562 // Round up to multiple of 10 - this way display is less jerky
563 time = (time + 9) / 10 * 10;
564 progress = DateUtils.formatElapsedTime(time);
565 status = getText(R.string.crypt_keeper_setup_time_remaining);
566 }
567 } catch (Exception e) {
568 // Will happen if no time etc - show percentage
569 }
570
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700571 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700572 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700573 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700574 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700575
576 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700577 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700578 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600579 }
580
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700581 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600582 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700583 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800584
Jason parksf8217302011-01-26 13:11:42 -0600585 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700586 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700587 if (mPasswordEntry != null) {
588 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700589 final InputMethodManager imm = (InputMethodManager) getSystemService(
590 Context.INPUT_METHOD_SERVICE);
591 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700592 setBackFunctionality(true);
593 }
594 if (mLockPatternView != null) {
595 mLockPatternView.setEnabled(true);
596 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700597 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600598 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700599 // Disable the password entry and back presses.
600 if (mPasswordEntry != null) {
601 mPasswordEntry.setEnabled(false);
602 }
603 if (mLockPatternView != null) {
604 mLockPatternView.setEnabled(false);
605 }
606
Andy Stadler13d62042011-01-31 19:21:37 -0800607 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700608 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800609
Jason parksf8217302011-01-26 13:11:42 -0600610 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700611 mHandler.removeMessages(MESSAGE_COOLDOWN);
612 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600613 }
Jason parksec5a45e2011-01-18 15:28:36 -0600614 }
Jason parks35933812011-01-21 15:48:20 -0600615
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800616 /**
617 * Sets the back status: enabled or disabled according to the parameter.
618 * @param isEnabled true if back is enabled, false otherwise.
619 */
620 private final void setBackFunctionality(boolean isEnabled) {
621 mIgnoreBack = !isEnabled;
622 if (isEnabled) {
623 mStatusBar.disable(sWidgetsToDisable);
624 } else {
625 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
626 }
627 }
628
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800629 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700630 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800631
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700632 @Override
633 public void onPatternStart() {
634 mLockPatternView.removeCallbacks(mClearPatternRunnable);
635 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800636
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700637 @Override
638 public void onPatternCleared() {
639 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800640
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700641 @Override
642 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
643 mLockPatternView.setEnabled(false);
644 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
645 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800646
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700647 @Override
648 public void onPatternCellAdded(List<Cell> pattern) {
649 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800650 };
651
652 private void passwordEntryInit() {
653 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600654 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800655 if (mPasswordEntry != null){
656 mPasswordEntry.setOnEditorActionListener(this);
657 mPasswordEntry.requestFocus();
658 // Become quiet when the user interacts with the Edit text screen.
659 mPasswordEntry.setOnKeyListener(this);
660 mPasswordEntry.setOnTouchListener(this);
661 mPasswordEntry.addTextChangedListener(this);
662 }
663
664 // Pattern case
665 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
666 if (mLockPatternView != null) {
667 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
668 }
Jason parks35933812011-01-21 15:48:20 -0600669
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700670 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700671 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700672 final View emergencyCall = findViewById(R.id.emergencyCallButton);
673 if (emergencyCall != null) {
674 Log.d(TAG, "Removing the emergency Call button");
675 emergencyCall.setVisibility(View.GONE);
676 }
677 }
678
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700679 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700680 final InputMethodManager imm = (InputMethodManager) getSystemService(
681 Context.INPUT_METHOD_SERVICE);
682 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
683 imeSwitcher.setVisibility(View.VISIBLE);
684 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700685 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700686 public void onClick(View v) {
687 imm.showInputMethodPicker();
688 }
689 });
Jason parks00046d62011-06-13 17:38:45 -0500690 }
David Brown8373b452011-06-20 12:38:45 -0700691
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700692 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
693 // is completely non-functional, and we want the user to notice the device and enter a
694 // password.
695 if (mWakeLock == null) {
696 Log.d(TAG, "Acquiring wakelock.");
697 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
698 if (pm != null) {
699 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
700 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700701 // Keep awake for 10 minutes - if the user hasn't been alerted by then
702 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700703 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700704 }
705 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700706
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700707 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
708 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700709 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700710 mHandler.postDelayed(new Runnable() {
711 @Override public void run() {
712 imm.showSoftInputUnchecked(0, null);
713 }
714 }, 0);
715 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700716
David Brown8373b452011-06-20 12:38:45 -0700717 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700718 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700719 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700720 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800721
722 // Dismiss keyguard while this screen is showing.
723 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600724 }
725
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700726 /**
727 * Method adapted from com.android.inputmethod.latin.Utils
728 *
729 * @param imm The input method manager
730 * @param shouldIncludeAuxiliarySubtypes
731 * @return true if we have multiple IMEs to choose from
732 */
733 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
734 final boolean shouldIncludeAuxiliarySubtypes) {
735 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
736
737 // Number of the filtered IMEs
738 int filteredImisCount = 0;
739
740 for (InputMethodInfo imi : enabledImis) {
741 // We can return true immediately after we find two or more filtered IMEs.
742 if (filteredImisCount > 1) return true;
743 final List<InputMethodSubtype> subtypes =
744 imm.getEnabledInputMethodSubtypeList(imi, true);
745 // IMEs that have no subtypes should be counted.
746 if (subtypes.isEmpty()) {
747 ++filteredImisCount;
748 continue;
749 }
750
751 int auxCount = 0;
752 for (InputMethodSubtype subtype : subtypes) {
753 if (subtype.isAuxiliary()) {
754 ++auxCount;
755 }
756 }
757 final int nonAuxCount = subtypes.size() - auxCount;
758
759 // IMEs that have one or more non-auxiliary subtypes should be counted.
760 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
761 // subtypes should be counted as well.
762 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
763 ++filteredImisCount;
764 continue;
765 }
766 }
767
768 return filteredImisCount > 1
769 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
770 // input method subtype (The current IME should be LatinIME.)
771 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
772 }
773
Jason parks8fd5bc92011-01-12 16:03:31 -0600774 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700775 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600776 if (service != null) {
777 return IMountService.Stub.asInterface(service);
778 }
779 return null;
780 }
781
782 @Override
783 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500784 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600785 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700786 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600787
Jason parksec5a45e2011-01-18 15:28:36 -0600788 if (TextUtils.isEmpty(password)) {
789 return true;
790 }
Jason parks35933812011-01-21 15:48:20 -0600791
Jason parks8fd5bc92011-01-12 16:03:31 -0600792 // Now that we have the password clear the password field.
793 v.setText(null);
794
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700795 // Disable the password entry and back keypress while checking the password. These
796 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600797 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800798 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600799
Ben Komalo04606752011-08-18 14:50:26 -0700800 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600801 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600802
Jason parks8fd5bc92011-01-12 16:03:31 -0600803 return true;
804 }
805 return false;
806 }
David Brown8373b452011-06-20 12:38:45 -0700807
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700808 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700809 * Set airplane mode on the device if it isn't an LTE device.
810 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
811 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
812 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
813 * both these problems, we turn the radio off. However, on certain networks turning on and
814 * off the radio takes a long time. In such cases, we are better off leaving the radio
815 * running so the latency of an E911 call is short.
816 * The behavior after this is:
817 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
818 * airplane mode and restart the radio.
819 * 2. Full boot: we read the persistent settings from the previous boot and restore the
820 * radio to whatever it was before it restarted. This also happens when rebooting a
821 * phone that has no encryption.
822 */
823 private final void setAirplaneModeIfNecessary() {
824 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700825 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700826 if (!isLteDevice) {
827 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700828 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700829 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
830 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700831 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700832 }
833 }
834
835 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700836 * Code to update the state of, and handle clicks from, the "Emergency call" button.
837 *
838 * This code is mostly duplicated from the corresponding code in
839 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
840 */
David Brown8373b452011-06-20 12:38:45 -0700841 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700842 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700843 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700844 if (emergencyCall == null)
845 return;
David Brown8373b452011-06-20 12:38:45 -0700846
847 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700848 emergencyCall.setVisibility(View.VISIBLE);
849 emergencyCall.setOnClickListener(new View.OnClickListener() {
850 @Override
851
David Brown8373b452011-06-20 12:38:45 -0700852 public void onClick(View v) {
853 takeEmergencyCallAction();
854 }
855 });
856 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700857 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700858 return;
859 }
860
David Brown8373b452011-06-20 12:38:45 -0700861 int textId;
Santos Cordon35230e92014-07-07 16:25:10 -0700862 if (getPhoneManager().isInAPhoneCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700863 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700864 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700865 } else {
866 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700867 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700868 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700869 }
870
871 private boolean isEmergencyCallCapable() {
872 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
873 }
874
875 private void takeEmergencyCallAction() {
Santos Cordon35230e92014-07-07 16:25:10 -0700876 PhoneManager phoneManager = getPhoneManager();
877 if (phoneManager.isInAPhoneCall()) {
878 phoneManager.showCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700879 } else {
880 launchEmergencyDialer();
881 }
882 }
883
David Brown8373b452011-06-20 12:38:45 -0700884
885 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700886 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700887 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
888 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700889 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700890 startActivity(intent);
891 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700892
Santos Cordon3afbdf02014-05-29 21:48:16 -0700893 private TelephonyManager getTelephonyManager() {
894 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
895 }
896
Santos Cordon35230e92014-07-07 16:25:10 -0700897 private PhoneManager getPhoneManager() {
898 return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
899 }
900
Vikram Aggarwald1147252012-05-08 13:52:30 -0700901 /**
902 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
903 */
904 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700905 mNotificationCountdown = 20;
906 }
907
908 @Override
909 public boolean onKey(View v, int keyCode, KeyEvent event) {
910 delayAudioNotification();
911 return false;
912 }
913
914 @Override
915 public boolean onTouch(View v, MotionEvent event) {
916 delayAudioNotification();
917 return false;
918 }
919
920 @Override
921 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
922 return;
923 }
924
925 @Override
926 public void onTextChanged(CharSequence s, int start, int before, int count) {
927 delayAudioNotification();
928 }
929
930 @Override
931 public void afterTextChanged(Editable s) {
932 return;
933 }
David Brown8373b452011-06-20 12:38:45 -0700934}