blob: 7f159ab3430e68fbf4e8b6e9c48fd644d355be7a [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;
Jason parks8fd5bc92011-01-12 16:03:31 -060024import android.content.pm.PackageManager;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070025import android.media.AudioManager;
Jason parks06c5ff42011-03-01 10:17:40 -060026import android.os.AsyncTask;
Jason parks8fd5bc92011-01-12 16:03:31 -060027import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060028import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060029import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060031import android.os.PowerManager;
David Brown8373b452011-06-20 12:38:45 -070032import android.os.RemoteException;
Jason parks8fd5bc92011-01-12 16:03:31 -060033import android.os.ServiceManager;
34import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070035import android.os.UserHandle;
Jason parks8fd5bc92011-01-12 16:03:31 -060036import android.os.storage.IMountService;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080037import android.os.storage.StorageManager;
Santos Cordon35230e92014-07-07 16:25:10 -070038
39import android.phone.PhoneManager;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070040import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070041import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070042import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060043import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070045import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060046import android.util.Log;
47import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070048import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080049import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080050import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080051import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070052import android.view.View.OnKeyListener;
53import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060054import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070055import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060056import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070057import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080058import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060059import android.widget.EditText;
60import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060061import android.widget.TextView;
62
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080063import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070064import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070065import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080066import com.android.internal.widget.LockPatternUtils;
67import com.android.internal.widget.LockPatternView;
68import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070069
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070070import static com.android.internal.widget.LockPatternView.DisplayMode;
71
Ben Komalo9fcb6a72011-08-26 14:40:18 -070072import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070073
74/**
75 * Settings screens to show the UI flows for encrypting/decrypting the device.
76 *
77 * This may be started via adb for debugging the UI layout, without having to go through
78 * encryption flows everytime. It should be noted that starting the activity in this manner
79 * is only useful for verifying UI-correctness - the behavior will not be identical.
80 * <pre>
81 * $ adb shell pm enable com.android.settings/.CryptKeeper
82 * $ adb shell am start \
83 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
84 * -n com.android.settings/.CryptKeeper
85 * </pre>
86 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070087public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
88 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060089 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060090
Jason parks8fd5bc92011-01-12 16:03:31 -060091 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070092 /** Message sent to us to indicate encryption update progress. */
93 private static final int MESSAGE_UPDATE_PROGRESS = 1;
94 /** Message sent to us to cool-down (waste user's time between password attempts) */
95 private static final int MESSAGE_COOLDOWN = 2;
96 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
97 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060098
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070099 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -0600100 private static final int MAX_FAILED_ATTEMPTS = 30;
101 private static final int COOL_DOWN_ATTEMPTS = 10;
102 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
103
David Brown8373b452011-06-20 12:38:45 -0700104 // Intent action for launching the Emergency Dialer activity.
105 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
106
Ben Komalo91a2f052011-08-16 17:48:25 -0700107 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
108 private static final String EXTRA_FORCE_VIEW =
109 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
110 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700111 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700112 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700113
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700114 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700115 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700116 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700117 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
118 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700119 /** A flag to indicate when the back event should be ignored */
120 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800121 private int mCooldown;
122 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600123 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800124 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700125 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
126 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700127 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
128 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700129 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800130
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700131 // how long we wait to clear a wrong pattern
132 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
133
134 private Runnable mClearPatternRunnable = new Runnable() {
135 public void run() {
136 mLockPatternView.clearPattern();
137 }
138 };
139
Andy Stadler14997402011-02-01 15:34:59 -0800140 /**
141 * Used to propagate state through configuration changes (e.g. screen rotation)
142 */
143 private static class NonConfigurationInstanceState {
144 final PowerManager.WakeLock wakelock;
145
146 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
147 wakelock = _wakelock;
148 }
149 }
150
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700151 /**
152 * Activity used to fade the screen to black after the password is entered.
153 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700154 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800155 @Override
156 public void onCreate(Bundle savedInstanceState) {
157 super.onCreate(savedInstanceState);
158 setContentView(R.layout.crypt_keeper_blank);
159 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700160 /** Ignore all back events. */
161 @Override
162 public void onBackPressed() {
163 return;
164 }
Jason parksf1dbf552011-01-24 16:19:28 -0600165 }
Jason parksec5a45e2011-01-18 15:28:36 -0600166
Jason parks06c5ff42011-03-01 10:17:40 -0600167 private class DecryptTask extends AsyncTask<String, Void, Integer> {
168 @Override
169 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700170 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600171 try {
172 return service.decryptStorage(params[0]);
173 } catch (Exception e) {
174 Log.e(TAG, "Error while decrypting...", e);
175 return -1;
176 }
177 }
178
179 @Override
180 protected void onPostExecute(Integer failedAttempts) {
181 if (failedAttempts == 0) {
182 // The password was entered successfully. Start the Blank activity
183 // so this activity animates to black before the devices starts. Note
184 // It has 1 second to complete the animation or it will be frozen
185 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700186 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600187 finish();
188 startActivity(intent);
189 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
190 // Factory reset the device.
191 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parks06c5ff42011-03-01 10:17:40 -0600192 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700193 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700194 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700195 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
196 mLockPatternView.removeCallbacks(mClearPatternRunnable);
197 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
198 }
199 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
200 mCooldown = COOL_DOWN_INTERVAL;
201 cooldown();
202 } else {
203 final TextView status = (TextView) findViewById(R.id.status);
204 status.setText(R.string.try_again);
205 if (mLockPatternView != null) {
206 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
207 }
208 // Reenable the password entry
209 if (mPasswordEntry != null) {
210 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700211 final InputMethodManager imm = (InputMethodManager) getSystemService(
212 Context.INPUT_METHOD_SERVICE);
213 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700214 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700215 }
216 if (mLockPatternView != null) {
217 mLockPatternView.setEnabled(true);
218 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700219 }
Jason parks06c5ff42011-03-01 10:17:40 -0600220 }
221 }
222 }
Jason parks75c085e2011-02-10 14:03:47 -0600223
Ben Komalo0e666092011-09-01 15:42:00 -0700224 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
225 @Override
226 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700227 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700228 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700229 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700230 int state = service.getEncryptionState();
231 if (state == IMountService.ENCRYPTION_STATE_NONE) {
232 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
233 return true; // Unexpected, but fine, I guess...
234 }
235 return state == IMountService.ENCRYPTION_STATE_OK;
236 } catch (RemoteException e) {
237 Log.w(TAG, "Unable to get encryption state properly");
238 return true;
239 }
240 }
241
242 @Override
243 protected void onPostExecute(Boolean result) {
244 mValidationComplete = true;
245 if (Boolean.FALSE.equals(result)) {
246 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
247 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700248 } else {
249 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700250 }
251 setupUi();
252 }
253 }
254
Ben Komalo91a2f052011-08-16 17:48:25 -0700255 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600256 @Override
257 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600258 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700259 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600260 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600261 break;
Jason parks35933812011-01-21 15:48:20 -0600262
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700263 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600264 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600265 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700266
267 case MESSAGE_NOTIFY:
268 notifyUser();
269 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600270 }
271 }
272 };
Jason parks35933812011-01-21 15:48:20 -0600273
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700274 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800275 /** The status bar where back/home/recent buttons are shown. */
276 private StatusBarManager mStatusBar;
277
278 /** All the widgets to disable in the status bar */
279 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
280 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
281 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
282 | StatusBarManager.DISABLE_SYSTEM_INFO
283 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700284 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800285 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700286
Ben Komalo91a2f052011-08-16 17:48:25 -0700287 /** @return whether or not this Activity was started for debugging the UI only. */
288 private boolean isDebugView() {
289 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
290 }
291
292 /** @return whether or not this Activity was started for debugging the specific UI view only. */
293 private boolean isDebugView(String viewType /* non-nullable */) {
294 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
295 }
296
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700297 /**
298 * Notify the user that we are awaiting input. Currently this sends an audio alert.
299 */
300 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700301 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700302 --mNotificationCountdown;
303 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700304 try {
305 // Play the standard keypress sound at full volume. This should be available on
306 // every device. We cannot play a ringtone here because media services aren't
307 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
308 // on tablet devices. The idea is to alert the user that something is needed: this
309 // does not have to be pleasing.
310 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
311 } catch (Exception e) {
312 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
313 }
314 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700315 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700316 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700317 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700318
319 if (mWakeLock.isHeld()) {
320 if (mReleaseWakeLockCountdown > 0) {
321 --mReleaseWakeLockCountdown;
322 } else {
323 mWakeLock.release();
324 }
325 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700326 }
327
328 /**
329 * Ignore back events after the user has entered the decrypt screen and while the device is
330 * encrypting.
331 */
332 @Override
333 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800334 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700335 if (mIgnoreBack)
336 return;
337 super.onBackPressed();
338 }
339
Jason parks8fd5bc92011-01-12 16:03:31 -0600340 @Override
341 public void onCreate(Bundle savedInstanceState) {
342 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600343
Andy Stadler95974062011-02-01 17:35:20 -0800344 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700345 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700346 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600347 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600348 PackageManager pm = getPackageManager();
349 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700350 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
351 PackageManager.DONT_KILL_APP);
352 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700353 // want to be running, so need to finish this activity. We can count
354 // on the activity manager re-launching the new home app upon finishing
355 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700356 // NOTE: This is really grungy. I think it would be better for the
357 // activity manager to explicitly launch the crypt keeper instead of
358 // home in the situation where we need to decrypt the device
359 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600360 return;
361 }
Jason parks35933812011-01-21 15:48:20 -0600362
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700363 // Disable the status bar, but do NOT disable back because the user needs a way to go
364 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800365 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
366 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800367
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700368 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700369 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800370 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700371 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800372 if (lastInstance instanceof NonConfigurationInstanceState) {
373 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
374 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700375 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800376 }
Jason parksec5a45e2011-01-18 15:28:36 -0600377 }
Jason parks35933812011-01-21 15:48:20 -0600378
Andy Stadler95974062011-02-01 17:35:20 -0800379 /**
380 * Note, we defer the state check and screen setup to onStart() because this will be
381 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
382 * especially important if we were to lose the wakelock for any reason.
383 */
384 @Override
385 public void onStart() {
386 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700387 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700388 }
389
390 /**
391 * Initializes the UI based on the current state of encryption.
392 * This is idempotent - calling repeatedly will simply re-initialize the UI.
393 */
394 private void setupUi() {
395 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
396 setContentView(R.layout.crypt_keeper_progress);
397 showFactoryReset();
398 return;
399 }
400
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700401 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700402 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800403 setContentView(R.layout.crypt_keeper_progress);
404 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700405 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700406 new AsyncTask<Void, Void, Void>() {
407 int type = StorageManager.CRYPT_TYPE_PASSWORD;
408 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800409
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700410 @Override
411 public Void doInBackground(Void... v) {
412 try {
413 final IMountService service = getMountService();
414 type = service.getPasswordType();
415 owner_info = service.getField("OwnerInfo");
416 } catch (Exception e) {
417 Log.e(TAG, "Error calling mount service " + e);
418 }
419
420 return null;
421 }
422
423 @Override
424 public void onPostExecute(java.lang.Void v) {
425 if(type == StorageManager.CRYPT_TYPE_PIN) {
426 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700427 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700428 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
429 setContentView(R.layout.crypt_keeper_pattern_entry);
430 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700431 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700432 } else {
433 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700434 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700435 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700436 final TextView status = (TextView) findViewById(R.id.status);
437 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700438
Paul Lawrence5a70f052014-06-13 11:09:55 -0700439 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
440 ownerInfo.setText(owner_info);
441 ownerInfo.setSelected(true); // Required for marquee'ing to work
442
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700443 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700444
445 if (mCooldown > 0) {
446 setBackFunctionality(false);
447 cooldown(); // in case we are cooling down and coming back from emergency dialler
448 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700449 }
450 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700451 } else if (!mValidationRequested) {
452 // We're supposed to be encrypted, but no validation has been done.
453 new ValidationTask().execute((Void[]) null);
454 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800455 }
456 }
457
Jason parksf8217302011-01-26 13:11:42 -0600458 @Override
459 public void onStop() {
460 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700461 mHandler.removeMessages(MESSAGE_COOLDOWN);
462 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
463 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800464 }
465
466 /**
467 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
468 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
469 * mWakeLock so the subsequent call to onDestroy does not release it.
470 */
471 @Override
472 public Object onRetainNonConfigurationInstance() {
473 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700474 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800475 mWakeLock = null;
476 return state;
477 }
478
479 @Override
480 public void onDestroy() {
481 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600482
483 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700484 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600485 mWakeLock.release();
486 mWakeLock = null;
487 }
488 }
489
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700490 /**
491 * Start encrypting the device.
492 */
Jason parksec5a45e2011-01-18 15:28:36 -0600493 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600494 // Accquire a partial wakelock to prevent the device from sleeping. Note
495 // we never release this wakelock as we will be restarted after the device
496 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700497 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700498 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700499 Log.d(TAG, "Acquiring wakelock.");
500 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
501 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
502 mWakeLock.acquire();
503 }
Jason parks35933812011-01-21 15:48:20 -0600504
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700505 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700506 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800507 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700508 // Start the first run of progress manually. This method sets up messages to occur at
509 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600510 updateProgress();
511 }
512
Andy Stadler13d62042011-01-31 19:21:37 -0800513 private void showFactoryReset() {
514 // Hide the encryption-bot to make room for the "factory reset" button
515 findViewById(R.id.encroid).setVisibility(View.GONE);
516
517 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700518 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800519 button.setVisibility(View.VISIBLE);
520 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700521 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800522 public void onClick(View v) {
523 // Factory reset the device.
524 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
525 }
526 });
527
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700528 // Alert the user of the failure.
529 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
530 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800531
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700532 final View view = findViewById(R.id.bottom_divider);
533 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700534 if (view != null) {
535 view.setVisibility(View.VISIBLE);
536 }
Andy Stadler13d62042011-01-31 19:21:37 -0800537 }
538
Jason parksf8217302011-01-26 13:11:42 -0600539 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700540 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600541
Ben Komalo0e666092011-09-01 15:42:00 -0700542 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800543 showFactoryReset();
544 return;
545 }
546
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700547 // Get status as percentage first
548 CharSequence status = getText(R.string.crypt_keeper_setup_description);
549 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600550 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700551 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700552 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600553 } catch (Exception e) {
554 Log.w(TAG, "Error parsing progress: " + e.toString());
555 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700556 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600557
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700558 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700559 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700560 try {
561 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
562 int time = Integer.parseInt(timeProperty);
563 if (time >= 0) {
564 // Round up to multiple of 10 - this way display is less jerky
565 time = (time + 9) / 10 * 10;
566 progress = DateUtils.formatElapsedTime(time);
567 status = getText(R.string.crypt_keeper_setup_time_remaining);
568 }
569 } catch (Exception e) {
570 // Will happen if no time etc - show percentage
571 }
572
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700573 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700574 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700575 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700576 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700577
578 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700579 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700580 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600581 }
582
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700583 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600584 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700585 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800586
Jason parksf8217302011-01-26 13:11:42 -0600587 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700588 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700589 if (mPasswordEntry != null) {
590 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700591 final InputMethodManager imm = (InputMethodManager) getSystemService(
592 Context.INPUT_METHOD_SERVICE);
593 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700594 setBackFunctionality(true);
595 }
596 if (mLockPatternView != null) {
597 mLockPatternView.setEnabled(true);
598 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700599 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600600 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700601 // Disable the password entry and back presses.
602 if (mPasswordEntry != null) {
603 mPasswordEntry.setEnabled(false);
604 }
605 if (mLockPatternView != null) {
606 mLockPatternView.setEnabled(false);
607 }
608
Andy Stadler13d62042011-01-31 19:21:37 -0800609 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700610 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800611
Jason parksf8217302011-01-26 13:11:42 -0600612 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700613 mHandler.removeMessages(MESSAGE_COOLDOWN);
614 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600615 }
Jason parksec5a45e2011-01-18 15:28:36 -0600616 }
Jason parks35933812011-01-21 15:48:20 -0600617
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800618 /**
619 * Sets the back status: enabled or disabled according to the parameter.
620 * @param isEnabled true if back is enabled, false otherwise.
621 */
622 private final void setBackFunctionality(boolean isEnabled) {
623 mIgnoreBack = !isEnabled;
624 if (isEnabled) {
625 mStatusBar.disable(sWidgetsToDisable);
626 } else {
627 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
628 }
629 }
630
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800631 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700632 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800633
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700634 @Override
635 public void onPatternStart() {
636 mLockPatternView.removeCallbacks(mClearPatternRunnable);
637 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800638
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700639 @Override
640 public void onPatternCleared() {
641 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800642
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700643 @Override
644 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
645 mLockPatternView.setEnabled(false);
646 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
647 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800648
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700649 @Override
650 public void onPatternCellAdded(List<Cell> pattern) {
651 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800652 };
653
654 private void passwordEntryInit() {
655 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600656 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800657 if (mPasswordEntry != null){
658 mPasswordEntry.setOnEditorActionListener(this);
659 mPasswordEntry.requestFocus();
660 // Become quiet when the user interacts with the Edit text screen.
661 mPasswordEntry.setOnKeyListener(this);
662 mPasswordEntry.setOnTouchListener(this);
663 mPasswordEntry.addTextChangedListener(this);
664 }
665
666 // Pattern case
667 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
668 if (mLockPatternView != null) {
669 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
670 }
Jason parks35933812011-01-21 15:48:20 -0600671
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700672 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700673 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700674 final View emergencyCall = findViewById(R.id.emergencyCallButton);
675 if (emergencyCall != null) {
676 Log.d(TAG, "Removing the emergency Call button");
677 emergencyCall.setVisibility(View.GONE);
678 }
679 }
680
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700681 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700682 final InputMethodManager imm = (InputMethodManager) getSystemService(
683 Context.INPUT_METHOD_SERVICE);
684 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
685 imeSwitcher.setVisibility(View.VISIBLE);
686 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700687 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700688 public void onClick(View v) {
689 imm.showInputMethodPicker();
690 }
691 });
Jason parks00046d62011-06-13 17:38:45 -0500692 }
David Brown8373b452011-06-20 12:38:45 -0700693
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700694 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
695 // is completely non-functional, and we want the user to notice the device and enter a
696 // password.
697 if (mWakeLock == null) {
698 Log.d(TAG, "Acquiring wakelock.");
699 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
700 if (pm != null) {
701 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
702 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700703 // Keep awake for 10 minutes - if the user hasn't been alerted by then
704 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700705 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700706 }
707 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700708
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700709 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
710 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700711 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700712 mHandler.postDelayed(new Runnable() {
713 @Override public void run() {
714 imm.showSoftInputUnchecked(0, null);
715 }
716 }, 0);
717 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700718
David Brown8373b452011-06-20 12:38:45 -0700719 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700720 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700721 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700722 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800723
724 // Dismiss keyguard while this screen is showing.
725 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600726 }
727
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700728 /**
729 * Method adapted from com.android.inputmethod.latin.Utils
730 *
731 * @param imm The input method manager
732 * @param shouldIncludeAuxiliarySubtypes
733 * @return true if we have multiple IMEs to choose from
734 */
735 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
736 final boolean shouldIncludeAuxiliarySubtypes) {
737 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
738
739 // Number of the filtered IMEs
740 int filteredImisCount = 0;
741
742 for (InputMethodInfo imi : enabledImis) {
743 // We can return true immediately after we find two or more filtered IMEs.
744 if (filteredImisCount > 1) return true;
745 final List<InputMethodSubtype> subtypes =
746 imm.getEnabledInputMethodSubtypeList(imi, true);
747 // IMEs that have no subtypes should be counted.
748 if (subtypes.isEmpty()) {
749 ++filteredImisCount;
750 continue;
751 }
752
753 int auxCount = 0;
754 for (InputMethodSubtype subtype : subtypes) {
755 if (subtype.isAuxiliary()) {
756 ++auxCount;
757 }
758 }
759 final int nonAuxCount = subtypes.size() - auxCount;
760
761 // IMEs that have one or more non-auxiliary subtypes should be counted.
762 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
763 // subtypes should be counted as well.
764 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
765 ++filteredImisCount;
766 continue;
767 }
768 }
769
770 return filteredImisCount > 1
771 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
772 // input method subtype (The current IME should be LatinIME.)
773 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
774 }
775
Jason parks8fd5bc92011-01-12 16:03:31 -0600776 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700777 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600778 if (service != null) {
779 return IMountService.Stub.asInterface(service);
780 }
781 return null;
782 }
783
784 @Override
785 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500786 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600787 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700788 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600789
Jason parksec5a45e2011-01-18 15:28:36 -0600790 if (TextUtils.isEmpty(password)) {
791 return true;
792 }
Jason parks35933812011-01-21 15:48:20 -0600793
Jason parks8fd5bc92011-01-12 16:03:31 -0600794 // Now that we have the password clear the password field.
795 v.setText(null);
796
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700797 // Disable the password entry and back keypress while checking the password. These
798 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600799 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800800 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600801
Ben Komalo04606752011-08-18 14:50:26 -0700802 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600803 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600804
Jason parks8fd5bc92011-01-12 16:03:31 -0600805 return true;
806 }
807 return false;
808 }
David Brown8373b452011-06-20 12:38:45 -0700809
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700810 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700811 * Set airplane mode on the device if it isn't an LTE device.
812 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
813 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
814 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
815 * both these problems, we turn the radio off. However, on certain networks turning on and
816 * off the radio takes a long time. In such cases, we are better off leaving the radio
817 * running so the latency of an E911 call is short.
818 * The behavior after this is:
819 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
820 * airplane mode and restart the radio.
821 * 2. Full boot: we read the persistent settings from the previous boot and restore the
822 * radio to whatever it was before it restarted. This also happens when rebooting a
823 * phone that has no encryption.
824 */
825 private final void setAirplaneModeIfNecessary() {
826 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700827 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700828 if (!isLteDevice) {
829 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700830 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700831 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
832 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700833 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700834 }
835 }
836
837 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700838 * Code to update the state of, and handle clicks from, the "Emergency call" button.
839 *
840 * This code is mostly duplicated from the corresponding code in
841 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
842 */
David Brown8373b452011-06-20 12:38:45 -0700843 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700844 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700845 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700846 if (emergencyCall == null)
847 return;
David Brown8373b452011-06-20 12:38:45 -0700848
849 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700850 emergencyCall.setVisibility(View.VISIBLE);
851 emergencyCall.setOnClickListener(new View.OnClickListener() {
852 @Override
853
David Brown8373b452011-06-20 12:38:45 -0700854 public void onClick(View v) {
855 takeEmergencyCallAction();
856 }
857 });
858 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700859 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700860 return;
861 }
862
David Brown8373b452011-06-20 12:38:45 -0700863 int textId;
Santos Cordon35230e92014-07-07 16:25:10 -0700864 if (getPhoneManager().isInAPhoneCall()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700865 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700866 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700867 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
868 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700869 } else {
870 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700871 final int emergencyIcon = R.drawable.ic_emergency;
872 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700873 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700874 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700875 }
876
877 private boolean isEmergencyCallCapable() {
878 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
879 }
880
881 private void takeEmergencyCallAction() {
Santos Cordon35230e92014-07-07 16:25:10 -0700882 PhoneManager phoneManager = getPhoneManager();
883 if (phoneManager.isInAPhoneCall()) {
884 phoneManager.showCallScreen(false /* showDialpad */);
David Brown8373b452011-06-20 12:38:45 -0700885 } else {
886 launchEmergencyDialer();
887 }
888 }
889
David Brown8373b452011-06-20 12:38:45 -0700890
891 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700892 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700893 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
894 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700895 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700896 startActivity(intent);
897 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700898
Santos Cordon3afbdf02014-05-29 21:48:16 -0700899 private TelephonyManager getTelephonyManager() {
900 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
901 }
902
Santos Cordon35230e92014-07-07 16:25:10 -0700903 private PhoneManager getPhoneManager() {
904 return (PhoneManager) getSystemService(Context.PHONE_SERVICE);
905 }
906
Vikram Aggarwald1147252012-05-08 13:52:30 -0700907 /**
908 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
909 */
910 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700911 mNotificationCountdown = 20;
912 }
913
914 @Override
915 public boolean onKey(View v, int keyCode, KeyEvent event) {
916 delayAudioNotification();
917 return false;
918 }
919
920 @Override
921 public boolean onTouch(View v, MotionEvent event) {
922 delayAudioNotification();
923 return false;
924 }
925
926 @Override
927 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
928 return;
929 }
930
931 @Override
932 public void onTextChanged(CharSequence s, int start, int before, int count) {
933 delayAudioNotification();
934 }
935
936 @Override
937 public void afterTextChanged(Editable s) {
938 return;
939 }
David Brown8373b452011-06-20 12:38:45 -0700940}