blob: 9a0964747bf72a68800803f70427b97c0f910824 [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
Paul Lawrence0f11e152014-08-20 07:43:32 -0700135 // how long we wait to clear a right pattern
136 private static final int RIGHT_PATTERN_CLEAR_TIMEOUT_MS = 500;
137
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700138 private Runnable mClearPatternRunnable = new Runnable() {
139 public void run() {
140 mLockPatternView.clearPattern();
141 }
142 };
143
Andy Stadler14997402011-02-01 15:34:59 -0800144 /**
145 * Used to propagate state through configuration changes (e.g. screen rotation)
146 */
147 private static class NonConfigurationInstanceState {
148 final PowerManager.WakeLock wakelock;
149
150 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
151 wakelock = _wakelock;
152 }
153 }
154
Jason parks06c5ff42011-03-01 10:17:40 -0600155 private class DecryptTask extends AsyncTask<String, Void, Integer> {
156 @Override
157 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700158 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600159 try {
160 return service.decryptStorage(params[0]);
161 } catch (Exception e) {
162 Log.e(TAG, "Error while decrypting...", e);
163 return -1;
164 }
165 }
166
167 @Override
168 protected void onPostExecute(Integer failedAttempts) {
169 if (failedAttempts == 0) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700170 // The password was entered successfully. Simply do nothing
171 // and wait for the service restart to switch to surfacefligner
Paul Lawrence0f11e152014-08-20 07:43:32 -0700172 if (mLockPatternView != null) {
173 mLockPatternView.removeCallbacks(mClearPatternRunnable);
174 mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
175 }
Jason parks06c5ff42011-03-01 10:17:40 -0600176 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
177 // Factory reset the device.
178 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Paul Lawrence384d8e52014-08-08 15:11:36 -0700179 } else if (failedAttempts == -1) {
180 // Right password, but decryption failed. Tell user bad news ...
181 setContentView(R.layout.crypt_keeper_progress);
182 showFactoryReset(true);
183 return;
Jason parks06c5ff42011-03-01 10:17:40 -0600184 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700185 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700186 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700187 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
188 mLockPatternView.removeCallbacks(mClearPatternRunnable);
189 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
190 }
191 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
192 mCooldown = COOL_DOWN_INTERVAL;
193 cooldown();
194 } else {
195 final TextView status = (TextView) findViewById(R.id.status);
Paul Lawrencebd4c3212014-07-23 13:57:17 -0700196
197 int remainingAttempts = MAX_FAILED_ATTEMPTS - failedAttempts;
198 if (remainingAttempts < COOL_DOWN_ATTEMPTS) {
199 CharSequence warningTemplate = getText(R.string.crypt_keeper_warn_wipe);
200 CharSequence warning = TextUtils.expandTemplate(warningTemplate,
201 Integer.toString(remainingAttempts));
202 status.setText(warning);
203 } else {
204 status.setText(R.string.try_again);
205 }
206
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700207 if (mLockPatternView != null) {
208 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
209 }
210 // Reenable the password entry
211 if (mPasswordEntry != null) {
212 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700213 final InputMethodManager imm = (InputMethodManager) getSystemService(
214 Context.INPUT_METHOD_SERVICE);
215 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700216 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700217 }
218 if (mLockPatternView != null) {
219 mLockPatternView.setEnabled(true);
220 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700221 }
Jason parks06c5ff42011-03-01 10:17:40 -0600222 }
223 }
224 }
Jason parks75c085e2011-02-10 14:03:47 -0600225
Ben Komalo0e666092011-09-01 15:42:00 -0700226 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
227 @Override
228 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700229 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700230 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700231 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700232 int state = service.getEncryptionState();
233 if (state == IMountService.ENCRYPTION_STATE_NONE) {
234 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
235 return true; // Unexpected, but fine, I guess...
236 }
237 return state == IMountService.ENCRYPTION_STATE_OK;
238 } catch (RemoteException e) {
239 Log.w(TAG, "Unable to get encryption state properly");
240 return true;
241 }
242 }
243
244 @Override
245 protected void onPostExecute(Boolean result) {
246 mValidationComplete = true;
247 if (Boolean.FALSE.equals(result)) {
248 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
249 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700250 } else {
251 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700252 }
253 setupUi();
254 }
255 }
256
Ben Komalo91a2f052011-08-16 17:48:25 -0700257 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600258 @Override
259 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600260 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700261 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600262 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600263 break;
Jason parks35933812011-01-21 15:48:20 -0600264
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700265 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600266 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600267 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700268
269 case MESSAGE_NOTIFY:
270 notifyUser();
271 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600272 }
273 }
274 };
Jason parks35933812011-01-21 15:48:20 -0600275
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700276 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800277 /** The status bar where back/home/recent buttons are shown. */
278 private StatusBarManager mStatusBar;
279
280 /** All the widgets to disable in the status bar */
281 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
282 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
283 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
284 | StatusBarManager.DISABLE_SYSTEM_INFO
285 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700286 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800287 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700288
Ben Komalo91a2f052011-08-16 17:48:25 -0700289 /** @return whether or not this Activity was started for debugging the UI only. */
290 private boolean isDebugView() {
291 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
292 }
293
294 /** @return whether or not this Activity was started for debugging the specific UI view only. */
295 private boolean isDebugView(String viewType /* non-nullable */) {
296 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
297 }
298
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700299 /**
300 * Notify the user that we are awaiting input. Currently this sends an audio alert.
301 */
302 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700303 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700304 --mNotificationCountdown;
305 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700306 try {
307 // Play the standard keypress sound at full volume. This should be available on
308 // every device. We cannot play a ringtone here because media services aren't
309 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
310 // on tablet devices. The idea is to alert the user that something is needed: this
311 // does not have to be pleasing.
312 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
313 } catch (Exception e) {
314 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
315 }
316 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700317 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700318 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700319 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700320
321 if (mWakeLock.isHeld()) {
322 if (mReleaseWakeLockCountdown > 0) {
323 --mReleaseWakeLockCountdown;
324 } else {
325 mWakeLock.release();
326 }
327 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700328 }
329
330 /**
331 * Ignore back events after the user has entered the decrypt screen and while the device is
332 * encrypting.
333 */
334 @Override
335 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800336 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700337 if (mIgnoreBack)
338 return;
339 super.onBackPressed();
340 }
341
Jason parks8fd5bc92011-01-12 16:03:31 -0600342 @Override
343 public void onCreate(Bundle savedInstanceState) {
344 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600345
Andy Stadler95974062011-02-01 17:35:20 -0800346 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700347 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700348 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600349 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600350 PackageManager pm = getPackageManager();
351 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700352 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
353 PackageManager.DONT_KILL_APP);
354 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700355 // want to be running, so need to finish this activity. We can count
356 // on the activity manager re-launching the new home app upon finishing
357 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700358 // NOTE: This is really grungy. I think it would be better for the
359 // activity manager to explicitly launch the crypt keeper instead of
360 // home in the situation where we need to decrypt the device
361 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600362 return;
363 }
Jason parks35933812011-01-21 15:48:20 -0600364
Paul Lawrence7ae20e32014-07-22 15:00:50 -0700365 try {
366 if (getResources().getBoolean(R.bool.crypt_keeper_allow_rotation)) {
367 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
368 }
369 } catch (NotFoundException e) {
370 }
371
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700372 // Disable the status bar, but do NOT disable back because the user needs a way to go
373 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800374 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
375 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800376
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700377 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700378 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800379 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700380 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800381 if (lastInstance instanceof NonConfigurationInstanceState) {
382 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
383 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700384 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800385 }
Jason parksec5a45e2011-01-18 15:28:36 -0600386 }
Jason parks35933812011-01-21 15:48:20 -0600387
Andy Stadler95974062011-02-01 17:35:20 -0800388 /**
389 * Note, we defer the state check and screen setup to onStart() because this will be
390 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
391 * especially important if we were to lose the wakelock for any reason.
392 */
393 @Override
394 public void onStart() {
395 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700396 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700397 }
398
399 /**
400 * Initializes the UI based on the current state of encryption.
401 * This is idempotent - calling repeatedly will simply re-initialize the UI.
402 */
403 private void setupUi() {
404 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
405 setContentView(R.layout.crypt_keeper_progress);
Paul Lawrence384d8e52014-08-08 15:11:36 -0700406 showFactoryReset(false);
Ben Komalo0e666092011-09-01 15:42:00 -0700407 return;
408 }
409
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700410 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700411 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800412 setContentView(R.layout.crypt_keeper_progress);
413 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700414 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700415 new AsyncTask<Void, Void, Void>() {
416 int type = StorageManager.CRYPT_TYPE_PASSWORD;
417 String owner_info;
Paul Lawrence0f11e152014-08-20 07:43:32 -0700418 boolean pattern_visible;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800419
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700420 @Override
421 public Void doInBackground(Void... v) {
422 try {
423 final IMountService service = getMountService();
424 type = service.getPasswordType();
425 owner_info = service.getField("OwnerInfo");
Paul Lawrence0f11e152014-08-20 07:43:32 -0700426 pattern_visible = !("0".equals(service.getField("PatternVisible")));
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700427 } catch (Exception e) {
428 Log.e(TAG, "Error calling mount service " + e);
429 }
430
431 return null;
432 }
433
434 @Override
435 public void onPostExecute(java.lang.Void v) {
436 if(type == StorageManager.CRYPT_TYPE_PIN) {
437 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700438 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700439 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
440 setContentView(R.layout.crypt_keeper_pattern_entry);
441 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700442 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700443 } else {
444 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700445 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700446 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700447 final TextView status = (TextView) findViewById(R.id.status);
448 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700449
Paul Lawrence5a70f052014-06-13 11:09:55 -0700450 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
451 ownerInfo.setText(owner_info);
452 ownerInfo.setSelected(true); // Required for marquee'ing to work
453
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700454 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700455
Paul Lawrence0f11e152014-08-20 07:43:32 -0700456 if (mLockPatternView != null) {
457 mLockPatternView.setInStealthMode(!pattern_visible);
458 }
459
Paul Lawrence5a70f052014-06-13 11:09:55 -0700460 if (mCooldown > 0) {
461 setBackFunctionality(false);
462 cooldown(); // in case we are cooling down and coming back from emergency dialler
463 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700464 }
465 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700466 } else if (!mValidationRequested) {
467 // We're supposed to be encrypted, but no validation has been done.
468 new ValidationTask().execute((Void[]) null);
469 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800470 }
471 }
472
Jason parksf8217302011-01-26 13:11:42 -0600473 @Override
474 public void onStop() {
475 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700476 mHandler.removeMessages(MESSAGE_COOLDOWN);
477 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
478 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800479 }
480
481 /**
482 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
483 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
484 * mWakeLock so the subsequent call to onDestroy does not release it.
485 */
486 @Override
487 public Object onRetainNonConfigurationInstance() {
488 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700489 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800490 mWakeLock = null;
491 return state;
492 }
493
494 @Override
495 public void onDestroy() {
496 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600497
498 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700499 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600500 mWakeLock.release();
501 mWakeLock = null;
502 }
503 }
504
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700505 /**
506 * Start encrypting the device.
507 */
Jason parksec5a45e2011-01-18 15:28:36 -0600508 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600509 // Accquire a partial wakelock to prevent the device from sleeping. Note
510 // we never release this wakelock as we will be restarted after the device
511 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700512 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700513 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700514 Log.d(TAG, "Acquiring wakelock.");
515 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
516 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
517 mWakeLock.acquire();
518 }
Jason parks35933812011-01-21 15:48:20 -0600519
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700520 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700521 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800522 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700523 // Start the first run of progress manually. This method sets up messages to occur at
524 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600525 updateProgress();
526 }
527
Paul Lawrence384d8e52014-08-08 15:11:36 -0700528 /**
529 * Show factory reset screen allowing the user to reset their phone when
530 * there is nothing else we can do
531 * @param corrupt true if userdata is corrupt, false if encryption failed
532 * partway through
533 */
534 private void showFactoryReset(boolean corrupt) {
Andy Stadler13d62042011-01-31 19:21:37 -0800535 // Hide the encryption-bot to make room for the "factory reset" button
536 findViewById(R.id.encroid).setVisibility(View.GONE);
537
538 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700539 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800540 button.setVisibility(View.VISIBLE);
541 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700542 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800543 public void onClick(View v) {
544 // Factory reset the device.
545 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
546 }
547 });
548
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700549 // Alert the user of the failure.
Paul Lawrence384d8e52014-08-08 15:11:36 -0700550 if (corrupt) {
551 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_data_corrupt_title);
552 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_data_corrupt_summary);
553 } else {
554 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
555 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
556 }
Andy Stadler13d62042011-01-31 19:21:37 -0800557
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700558 final View view = findViewById(R.id.bottom_divider);
559 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700560 if (view != null) {
561 view.setVisibility(View.VISIBLE);
562 }
Andy Stadler13d62042011-01-31 19:21:37 -0800563 }
564
Jason parksf8217302011-01-26 13:11:42 -0600565 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700566 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600567
Ben Komalo0e666092011-09-01 15:42:00 -0700568 if ("error_partially_encrypted".equals(state)) {
Paul Lawrence384d8e52014-08-08 15:11:36 -0700569 showFactoryReset(false);
Andy Stadler13d62042011-01-31 19:21:37 -0800570 return;
571 }
572
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700573 // Get status as percentage first
574 CharSequence status = getText(R.string.crypt_keeper_setup_description);
575 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600576 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700577 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700578 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600579 } catch (Exception e) {
580 Log.w(TAG, "Error parsing progress: " + e.toString());
581 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700582 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600583
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700584 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700585 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700586 try {
587 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
588 int time = Integer.parseInt(timeProperty);
589 if (time >= 0) {
590 // Round up to multiple of 10 - this way display is less jerky
591 time = (time + 9) / 10 * 10;
592 progress = DateUtils.formatElapsedTime(time);
593 status = getText(R.string.crypt_keeper_setup_time_remaining);
594 }
595 } catch (Exception e) {
596 // Will happen if no time etc - show percentage
597 }
598
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700599 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700600 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700601 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700602 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700603
604 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700605 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700606 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600607 }
608
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700609 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600610 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700611 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800612
Jason parksf8217302011-01-26 13:11:42 -0600613 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700614 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700615 if (mPasswordEntry != null) {
616 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700617 final InputMethodManager imm = (InputMethodManager) getSystemService(
618 Context.INPUT_METHOD_SERVICE);
619 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700620 setBackFunctionality(true);
621 }
622 if (mLockPatternView != null) {
623 mLockPatternView.setEnabled(true);
624 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700625 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600626 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700627 // Disable the password entry and back presses.
628 if (mPasswordEntry != null) {
629 mPasswordEntry.setEnabled(false);
630 }
631 if (mLockPatternView != null) {
632 mLockPatternView.setEnabled(false);
633 }
634
Andy Stadler13d62042011-01-31 19:21:37 -0800635 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700636 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800637
Jason parksf8217302011-01-26 13:11:42 -0600638 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700639 mHandler.removeMessages(MESSAGE_COOLDOWN);
640 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600641 }
Jason parksec5a45e2011-01-18 15:28:36 -0600642 }
Jason parks35933812011-01-21 15:48:20 -0600643
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800644 /**
645 * Sets the back status: enabled or disabled according to the parameter.
646 * @param isEnabled true if back is enabled, false otherwise.
647 */
648 private final void setBackFunctionality(boolean isEnabled) {
649 mIgnoreBack = !isEnabled;
650 if (isEnabled) {
651 mStatusBar.disable(sWidgetsToDisable);
652 } else {
653 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
654 }
655 }
656
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800657 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700658 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800659
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700660 @Override
661 public void onPatternStart() {
662 mLockPatternView.removeCallbacks(mClearPatternRunnable);
663 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800664
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700665 @Override
666 public void onPatternCleared() {
667 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800668
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700669 @Override
670 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
671 mLockPatternView.setEnabled(false);
672 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
673 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800674
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700675 @Override
676 public void onPatternCellAdded(List<Cell> pattern) {
677 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800678 };
679
680 private void passwordEntryInit() {
681 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600682 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800683 if (mPasswordEntry != null){
684 mPasswordEntry.setOnEditorActionListener(this);
685 mPasswordEntry.requestFocus();
686 // Become quiet when the user interacts with the Edit text screen.
687 mPasswordEntry.setOnKeyListener(this);
688 mPasswordEntry.setOnTouchListener(this);
689 mPasswordEntry.addTextChangedListener(this);
690 }
691
692 // Pattern case
693 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
694 if (mLockPatternView != null) {
695 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
696 }
Jason parks35933812011-01-21 15:48:20 -0600697
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700698 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700699 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700700 final View emergencyCall = findViewById(R.id.emergencyCallButton);
701 if (emergencyCall != null) {
702 Log.d(TAG, "Removing the emergency Call button");
703 emergencyCall.setVisibility(View.GONE);
704 }
705 }
706
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700707 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700708 final InputMethodManager imm = (InputMethodManager) getSystemService(
709 Context.INPUT_METHOD_SERVICE);
710 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
711 imeSwitcher.setVisibility(View.VISIBLE);
712 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700713 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700714 public void onClick(View v) {
715 imm.showInputMethodPicker();
716 }
717 });
Jason parks00046d62011-06-13 17:38:45 -0500718 }
David Brown8373b452011-06-20 12:38:45 -0700719
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700720 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
721 // is completely non-functional, and we want the user to notice the device and enter a
722 // password.
723 if (mWakeLock == null) {
724 Log.d(TAG, "Acquiring wakelock.");
725 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
726 if (pm != null) {
727 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
728 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700729 // Keep awake for 10 minutes - if the user hasn't been alerted by then
730 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700731 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700732 }
733 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700734
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700735 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
736 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700737 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700738 mHandler.postDelayed(new Runnable() {
739 @Override public void run() {
740 imm.showSoftInputUnchecked(0, null);
741 }
742 }, 0);
743 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700744
David Brown8373b452011-06-20 12:38:45 -0700745 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700746 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700747 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700748 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800749
750 // Dismiss keyguard while this screen is showing.
751 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600752 }
753
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700754 /**
755 * Method adapted from com.android.inputmethod.latin.Utils
756 *
757 * @param imm The input method manager
758 * @param shouldIncludeAuxiliarySubtypes
759 * @return true if we have multiple IMEs to choose from
760 */
761 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
762 final boolean shouldIncludeAuxiliarySubtypes) {
763 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
764
765 // Number of the filtered IMEs
766 int filteredImisCount = 0;
767
768 for (InputMethodInfo imi : enabledImis) {
769 // We can return true immediately after we find two or more filtered IMEs.
770 if (filteredImisCount > 1) return true;
771 final List<InputMethodSubtype> subtypes =
772 imm.getEnabledInputMethodSubtypeList(imi, true);
773 // IMEs that have no subtypes should be counted.
774 if (subtypes.isEmpty()) {
775 ++filteredImisCount;
776 continue;
777 }
778
779 int auxCount = 0;
780 for (InputMethodSubtype subtype : subtypes) {
781 if (subtype.isAuxiliary()) {
782 ++auxCount;
783 }
784 }
785 final int nonAuxCount = subtypes.size() - auxCount;
786
787 // IMEs that have one or more non-auxiliary subtypes should be counted.
788 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
789 // subtypes should be counted as well.
790 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
791 ++filteredImisCount;
792 continue;
793 }
794 }
795
796 return filteredImisCount > 1
797 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
798 // input method subtype (The current IME should be LatinIME.)
799 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
800 }
801
Jason parks8fd5bc92011-01-12 16:03:31 -0600802 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700803 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600804 if (service != null) {
805 return IMountService.Stub.asInterface(service);
806 }
807 return null;
808 }
809
810 @Override
811 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500812 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600813 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700814 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600815
Jason parksec5a45e2011-01-18 15:28:36 -0600816 if (TextUtils.isEmpty(password)) {
817 return true;
818 }
Jason parks35933812011-01-21 15:48:20 -0600819
Jason parks8fd5bc92011-01-12 16:03:31 -0600820 // Now that we have the password clear the password field.
821 v.setText(null);
822
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700823 // Disable the password entry and back keypress while checking the password. These
824 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600825 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800826 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600827
Ben Komalo04606752011-08-18 14:50:26 -0700828 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600829 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600830
Jason parks8fd5bc92011-01-12 16:03:31 -0600831 return true;
832 }
833 return false;
834 }
David Brown8373b452011-06-20 12:38:45 -0700835
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700836 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700837 * Set airplane mode on the device if it isn't an LTE device.
838 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
839 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
840 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
841 * both these problems, we turn the radio off. However, on certain networks turning on and
842 * off the radio takes a long time. In such cases, we are better off leaving the radio
843 * running so the latency of an E911 call is short.
844 * The behavior after this is:
845 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
846 * airplane mode and restart the radio.
847 * 2. Full boot: we read the persistent settings from the previous boot and restore the
848 * radio to whatever it was before it restarted. This also happens when rebooting a
849 * phone that has no encryption.
850 */
851 private final void setAirplaneModeIfNecessary() {
852 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700853 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700854 if (!isLteDevice) {
855 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700856 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700857 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
858 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700859 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700860 }
861 }
862
863 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700864 * Code to update the state of, and handle clicks from, the "Emergency call" button.
865 *
866 * This code is mostly duplicated from the corresponding code in
867 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
868 */
David Brown8373b452011-06-20 12:38:45 -0700869 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700870 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700871 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700872 if (emergencyCall == null)
873 return;
David Brown8373b452011-06-20 12:38:45 -0700874
875 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700876 emergencyCall.setVisibility(View.VISIBLE);
877 emergencyCall.setOnClickListener(new View.OnClickListener() {
878 @Override
879
David Brown8373b452011-06-20 12:38:45 -0700880 public void onClick(View v) {
881 takeEmergencyCallAction();
882 }
883 });
884 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700885 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700886 return;
887 }
888
David Brown8373b452011-06-20 12:38:45 -0700889 int textId;
Nancy Chendb848a52014-08-21 22:53:05 -0700890 if (getTelecommManager().isInCall()) {
Paul Lawrence89c75702014-07-11 07:34:44 -0700891 // Show "return to call"
David Brown8373b452011-06-20 12:38:45 -0700892 textId = R.string.cryptkeeper_return_to_call;
David Brown8373b452011-06-20 12:38:45 -0700893 } else {
894 textId = R.string.cryptkeeper_emergency_call;
David Brown8373b452011-06-20 12:38:45 -0700895 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700896 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700897 }
898
899 private boolean isEmergencyCallCapable() {
900 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
901 }
902
903 private void takeEmergencyCallAction() {
Nancy Chendb848a52014-08-21 22:53:05 -0700904 TelecommManager telecommManager = getTelecommManager();
905 if (telecommManager.isInCall()) {
906 telecommManager.showInCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700907 } else {
908 launchEmergencyDialer();
909 }
910 }
911
David Brown8373b452011-06-20 12:38:45 -0700912
913 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700914 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700915 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
916 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700917 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700918 startActivity(intent);
919 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700920
Santos Cordon3afbdf02014-05-29 21:48:16 -0700921 private TelephonyManager getTelephonyManager() {
922 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
923 }
924
Nancy Chendb848a52014-08-21 22:53:05 -0700925 private TelecommManager getTelecommManager() {
926 return (TelecommManager) getSystemService(Context.TELECOMM_SERVICE);
Santos Cordon35230e92014-07-07 16:25:10 -0700927 }
928
Vikram Aggarwald1147252012-05-08 13:52:30 -0700929 /**
930 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
931 */
932 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700933 mNotificationCountdown = 20;
934 }
935
936 @Override
937 public boolean onKey(View v, int keyCode, KeyEvent event) {
938 delayAudioNotification();
939 return false;
940 }
941
942 @Override
943 public boolean onTouch(View v, MotionEvent event) {
944 delayAudioNotification();
945 return false;
946 }
947
948 @Override
949 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
950 return;
951 }
952
953 @Override
954 public void onTextChanged(CharSequence s, int start, int before, int count) {
955 delayAudioNotification();
956 }
957
958 @Override
959 public void afterTextChanged(Editable s) {
960 return;
961 }
David Brown8373b452011-06-20 12:38:45 -0700962}