blob: 151927d9b52de666bdeac2c667345abc9f9da29d [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;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070038import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070039import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070040import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060041import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070042import android.text.TextWatcher;
Paul Lawrenceb15c68f2014-06-05 07:24:23 -070043import android.text.format.DateUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060044import android.util.Log;
45import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070046import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080047import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080048import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080049import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070050import android.view.View.OnKeyListener;
51import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060052import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070053import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060054import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070055import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080056import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060057import android.widget.EditText;
58import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060059import android.widget.TextView;
60
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080061import com.android.internal.statusbar.StatusBarIcon;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070062import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070063import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080064import com.android.internal.widget.LockPatternUtils;
65import com.android.internal.widget.LockPatternView;
66import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070067
Paul Lawrencef6cda3b2014-06-12 08:26:08 -070068import static com.android.internal.widget.LockPatternView.DisplayMode;
69
Ben Komalo9fcb6a72011-08-26 14:40:18 -070070import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070071
72/**
73 * Settings screens to show the UI flows for encrypting/decrypting the device.
74 *
75 * This may be started via adb for debugging the UI layout, without having to go through
76 * encryption flows everytime. It should be noted that starting the activity in this manner
77 * is only useful for verifying UI-correctness - the behavior will not be identical.
78 * <pre>
79 * $ adb shell pm enable com.android.settings/.CryptKeeper
80 * $ adb shell am start \
81 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
82 * -n com.android.settings/.CryptKeeper
83 * </pre>
84 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070085public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
86 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060087 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060088
Jason parks8fd5bc92011-01-12 16:03:31 -060089 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070090 /** Message sent to us to indicate encryption update progress. */
91 private static final int MESSAGE_UPDATE_PROGRESS = 1;
92 /** Message sent to us to cool-down (waste user's time between password attempts) */
93 private static final int MESSAGE_COOLDOWN = 2;
94 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
95 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060096
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070097 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060098 private static final int MAX_FAILED_ATTEMPTS = 30;
99 private static final int COOL_DOWN_ATTEMPTS = 10;
100 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
101
David Brown8373b452011-06-20 12:38:45 -0700102 // Intent action for launching the Emergency Dialer activity.
103 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
104
Ben Komalo91a2f052011-08-16 17:48:25 -0700105 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
106 private static final String EXTRA_FORCE_VIEW =
107 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
108 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700109 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700110 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700111
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700112 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700113 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700114 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700115 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
116 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700117 /** A flag to indicate when the back event should be ignored */
118 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800119 private int mCooldown;
120 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600121 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800122 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700123 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
124 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700125 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
126 private int mReleaseWakeLockCountdown = 0;
Paul Lawrence5a70f052014-06-13 11:09:55 -0700127 private int mStatusString = R.string.enter_password;
Andy Stadler14997402011-02-01 15:34:59 -0800128
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700129 // how long we wait to clear a wrong pattern
130 private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500;
131
132 private Runnable mClearPatternRunnable = new Runnable() {
133 public void run() {
134 mLockPatternView.clearPattern();
135 }
136 };
137
Andy Stadler14997402011-02-01 15:34:59 -0800138 /**
139 * Used to propagate state through configuration changes (e.g. screen rotation)
140 */
141 private static class NonConfigurationInstanceState {
142 final PowerManager.WakeLock wakelock;
143
144 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
145 wakelock = _wakelock;
146 }
147 }
148
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700149 /**
150 * Activity used to fade the screen to black after the password is entered.
151 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700152 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800153 @Override
154 public void onCreate(Bundle savedInstanceState) {
155 super.onCreate(savedInstanceState);
156 setContentView(R.layout.crypt_keeper_blank);
157 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700158 /** Ignore all back events. */
159 @Override
160 public void onBackPressed() {
161 return;
162 }
Jason parksf1dbf552011-01-24 16:19:28 -0600163 }
Jason parksec5a45e2011-01-18 15:28:36 -0600164
Jason parks06c5ff42011-03-01 10:17:40 -0600165 private class DecryptTask extends AsyncTask<String, Void, Integer> {
166 @Override
167 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700168 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600169 try {
170 return service.decryptStorage(params[0]);
171 } catch (Exception e) {
172 Log.e(TAG, "Error while decrypting...", e);
173 return -1;
174 }
175 }
176
177 @Override
178 protected void onPostExecute(Integer failedAttempts) {
179 if (failedAttempts == 0) {
180 // The password was entered successfully. Start the Blank activity
181 // so this activity animates to black before the devices starts. Note
182 // It has 1 second to complete the animation or it will be frozen
183 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700184 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600185 finish();
186 startActivity(intent);
187 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
188 // Factory reset the device.
189 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parks06c5ff42011-03-01 10:17:40 -0600190 } else {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700191 // Wrong entry. Handle pattern case.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700192 if (mLockPatternView != null) {
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700193 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
194 mLockPatternView.removeCallbacks(mClearPatternRunnable);
195 mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS);
196 }
197 if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
198 mCooldown = COOL_DOWN_INTERVAL;
199 cooldown();
200 } else {
201 final TextView status = (TextView) findViewById(R.id.status);
202 status.setText(R.string.try_again);
203 if (mLockPatternView != null) {
204 mLockPatternView.setDisplayMode(DisplayMode.Wrong);
205 }
206 // Reenable the password entry
207 if (mPasswordEntry != null) {
208 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700209 final InputMethodManager imm = (InputMethodManager) getSystemService(
210 Context.INPUT_METHOD_SERVICE);
211 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence08c6ab02014-06-19 13:34:58 -0700212 setBackFunctionality(true);
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700213 }
214 if (mLockPatternView != null) {
215 mLockPatternView.setEnabled(true);
216 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700217 }
Jason parks06c5ff42011-03-01 10:17:40 -0600218 }
219 }
220 }
Jason parks75c085e2011-02-10 14:03:47 -0600221
Ben Komalo0e666092011-09-01 15:42:00 -0700222 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
223 @Override
224 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700225 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700226 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700227 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700228 int state = service.getEncryptionState();
229 if (state == IMountService.ENCRYPTION_STATE_NONE) {
230 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
231 return true; // Unexpected, but fine, I guess...
232 }
233 return state == IMountService.ENCRYPTION_STATE_OK;
234 } catch (RemoteException e) {
235 Log.w(TAG, "Unable to get encryption state properly");
236 return true;
237 }
238 }
239
240 @Override
241 protected void onPostExecute(Boolean result) {
242 mValidationComplete = true;
243 if (Boolean.FALSE.equals(result)) {
244 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
245 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700246 } else {
247 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700248 }
249 setupUi();
250 }
251 }
252
Ben Komalo91a2f052011-08-16 17:48:25 -0700253 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600254 @Override
255 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600256 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700257 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600258 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600259 break;
Jason parks35933812011-01-21 15:48:20 -0600260
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700261 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600262 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600263 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700264
265 case MESSAGE_NOTIFY:
266 notifyUser();
267 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600268 }
269 }
270 };
Jason parks35933812011-01-21 15:48:20 -0600271
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700272 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800273 /** The status bar where back/home/recent buttons are shown. */
274 private StatusBarManager mStatusBar;
275
276 /** All the widgets to disable in the status bar */
277 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
278 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
279 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
280 | StatusBarManager.DISABLE_SYSTEM_INFO
281 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700282 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800283 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700284
Ben Komalo91a2f052011-08-16 17:48:25 -0700285 /** @return whether or not this Activity was started for debugging the UI only. */
286 private boolean isDebugView() {
287 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
288 }
289
290 /** @return whether or not this Activity was started for debugging the specific UI view only. */
291 private boolean isDebugView(String viewType /* non-nullable */) {
292 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
293 }
294
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700295 /**
296 * Notify the user that we are awaiting input. Currently this sends an audio alert.
297 */
298 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700299 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700300 --mNotificationCountdown;
301 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700302 try {
303 // Play the standard keypress sound at full volume. This should be available on
304 // every device. We cannot play a ringtone here because media services aren't
305 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
306 // on tablet devices. The idea is to alert the user that something is needed: this
307 // does not have to be pleasing.
308 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
309 } catch (Exception e) {
310 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
311 }
312 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700313 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700314 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700315 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700316
317 if (mWakeLock.isHeld()) {
318 if (mReleaseWakeLockCountdown > 0) {
319 --mReleaseWakeLockCountdown;
320 } else {
321 mWakeLock.release();
322 }
323 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700324 }
325
326 /**
327 * Ignore back events after the user has entered the decrypt screen and while the device is
328 * encrypting.
329 */
330 @Override
331 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800332 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700333 if (mIgnoreBack)
334 return;
335 super.onBackPressed();
336 }
337
Jason parks8fd5bc92011-01-12 16:03:31 -0600338 @Override
339 public void onCreate(Bundle savedInstanceState) {
340 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600341
Andy Stadler95974062011-02-01 17:35:20 -0800342 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700343 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700344 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600345 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600346 PackageManager pm = getPackageManager();
347 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700348 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
349 PackageManager.DONT_KILL_APP);
350 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700351 // want to be running, so need to finish this activity. We can count
352 // on the activity manager re-launching the new home app upon finishing
353 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700354 // NOTE: This is really grungy. I think it would be better for the
355 // activity manager to explicitly launch the crypt keeper instead of
356 // home in the situation where we need to decrypt the device
357 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600358 return;
359 }
Jason parks35933812011-01-21 15:48:20 -0600360
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700361 // Disable the status bar, but do NOT disable back because the user needs a way to go
362 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800363 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
364 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800365
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700366 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700367 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800368 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700369 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800370 if (lastInstance instanceof NonConfigurationInstanceState) {
371 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
372 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700373 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800374 }
Jason parksec5a45e2011-01-18 15:28:36 -0600375 }
Jason parks35933812011-01-21 15:48:20 -0600376
Andy Stadler95974062011-02-01 17:35:20 -0800377 /**
378 * Note, we defer the state check and screen setup to onStart() because this will be
379 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
380 * especially important if we were to lose the wakelock for any reason.
381 */
382 @Override
383 public void onStart() {
384 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700385 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700386 }
387
388 /**
389 * Initializes the UI based on the current state of encryption.
390 * This is idempotent - calling repeatedly will simply re-initialize the UI.
391 */
392 private void setupUi() {
393 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
394 setContentView(R.layout.crypt_keeper_progress);
395 showFactoryReset();
396 return;
397 }
398
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700399 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700400 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800401 setContentView(R.layout.crypt_keeper_progress);
402 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700403 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700404 new AsyncTask<Void, Void, Void>() {
405 int type = StorageManager.CRYPT_TYPE_PASSWORD;
406 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800407
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700408 @Override
409 public Void doInBackground(Void... v) {
410 try {
411 final IMountService service = getMountService();
412 type = service.getPasswordType();
413 owner_info = service.getField("OwnerInfo");
414 } catch (Exception e) {
415 Log.e(TAG, "Error calling mount service " + e);
416 }
417
418 return null;
419 }
420
421 @Override
422 public void onPostExecute(java.lang.Void v) {
423 if(type == StorageManager.CRYPT_TYPE_PIN) {
424 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700425 mStatusString = R.string.enter_pin;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700426 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
427 setContentView(R.layout.crypt_keeper_pattern_entry);
428 setBackFunctionality(false);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700429 mStatusString = R.string.enter_pattern;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700430 } else {
431 setContentView(R.layout.crypt_keeper_password_entry);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700432 mStatusString = R.string.enter_password;
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700433 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700434 final TextView status = (TextView) findViewById(R.id.status);
435 status.setText(mStatusString);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700436
Paul Lawrence5a70f052014-06-13 11:09:55 -0700437 final TextView ownerInfo = (TextView) findViewById(R.id.owner_info);
438 ownerInfo.setText(owner_info);
439 ownerInfo.setSelected(true); // Required for marquee'ing to work
440
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700441 passwordEntryInit();
Paul Lawrence5a70f052014-06-13 11:09:55 -0700442
443 if (mCooldown > 0) {
444 setBackFunctionality(false);
445 cooldown(); // in case we are cooling down and coming back from emergency dialler
446 }
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700447 }
448 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700449 } else if (!mValidationRequested) {
450 // We're supposed to be encrypted, but no validation has been done.
451 new ValidationTask().execute((Void[]) null);
452 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800453 }
454 }
455
Jason parksf8217302011-01-26 13:11:42 -0600456 @Override
457 public void onStop() {
458 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700459 mHandler.removeMessages(MESSAGE_COOLDOWN);
460 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
461 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800462 }
463
464 /**
465 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
466 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
467 * mWakeLock so the subsequent call to onDestroy does not release it.
468 */
469 @Override
470 public Object onRetainNonConfigurationInstance() {
471 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700472 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800473 mWakeLock = null;
474 return state;
475 }
476
477 @Override
478 public void onDestroy() {
479 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600480
481 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700482 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600483 mWakeLock.release();
484 mWakeLock = null;
485 }
486 }
487
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700488 /**
489 * Start encrypting the device.
490 */
Jason parksec5a45e2011-01-18 15:28:36 -0600491 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600492 // Accquire a partial wakelock to prevent the device from sleeping. Note
493 // we never release this wakelock as we will be restarted after the device
494 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700495 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700496 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700497 Log.d(TAG, "Acquiring wakelock.");
498 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
499 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
500 mWakeLock.acquire();
501 }
Jason parks35933812011-01-21 15:48:20 -0600502
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700503 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700504 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800505 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700506 // Start the first run of progress manually. This method sets up messages to occur at
507 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600508 updateProgress();
509 }
510
Andy Stadler13d62042011-01-31 19:21:37 -0800511 private void showFactoryReset() {
512 // Hide the encryption-bot to make room for the "factory reset" button
513 findViewById(R.id.encroid).setVisibility(View.GONE);
514
515 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700516 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800517 button.setVisibility(View.VISIBLE);
518 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700519 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800520 public void onClick(View v) {
521 // Factory reset the device.
522 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
523 }
524 });
525
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700526 // Alert the user of the failure.
527 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
528 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800529
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700530 final View view = findViewById(R.id.bottom_divider);
531 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700532 if (view != null) {
533 view.setVisibility(View.VISIBLE);
534 }
Andy Stadler13d62042011-01-31 19:21:37 -0800535 }
536
Jason parksf8217302011-01-26 13:11:42 -0600537 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700538 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600539
Ben Komalo0e666092011-09-01 15:42:00 -0700540 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800541 showFactoryReset();
542 return;
543 }
544
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700545 // Get status as percentage first
546 CharSequence status = getText(R.string.crypt_keeper_setup_description);
547 int percent = 0;
Jason parksf8217302011-01-26 13:11:42 -0600548 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700549 // Force a 50% progress state when debugging the view.
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700550 percent = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600551 } catch (Exception e) {
552 Log.w(TAG, "Error parsing progress: " + e.toString());
553 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700554 String progress = Integer.toString(percent);
Jason parksf8217302011-01-26 13:11:42 -0600555
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700556 // Now try to get status as time remaining and replace as appropriate
Ben Komalo04606752011-08-18 14:50:26 -0700557 Log.v(TAG, "Encryption progress: " + progress);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700558 try {
559 final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining");
560 int time = Integer.parseInt(timeProperty);
561 if (time >= 0) {
562 // Round up to multiple of 10 - this way display is less jerky
563 time = (time + 9) / 10 * 10;
564 progress = DateUtils.formatElapsedTime(time);
565 status = getText(R.string.crypt_keeper_setup_time_remaining);
566 }
567 } catch (Exception e) {
568 // Will happen if no time etc - show percentage
569 }
570
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700571 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700572 if (tv != null) {
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700573 tv.setText(TextUtils.expandTemplate(status, progress));
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700574 }
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700575
576 // Check the progress every 1 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700577 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
Paul Lawrenceb15c68f2014-06-05 07:24:23 -0700578 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000);
Jason parksf8217302011-01-26 13:11:42 -0600579 }
580
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700581 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600582 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700583 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800584
Jason parksf8217302011-01-26 13:11:42 -0600585 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700586 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700587 if (mPasswordEntry != null) {
588 mPasswordEntry.setEnabled(true);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700589 final InputMethodManager imm = (InputMethodManager) getSystemService(
590 Context.INPUT_METHOD_SERVICE);
591 imm.showSoftInput(mPasswordEntry, 0);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700592 setBackFunctionality(true);
593 }
594 if (mLockPatternView != null) {
595 mLockPatternView.setEnabled(true);
596 }
Paul Lawrence5a70f052014-06-13 11:09:55 -0700597 status.setText(mStatusString);
Jason parksf8217302011-01-26 13:11:42 -0600598 } else {
Paul Lawrence5a70f052014-06-13 11:09:55 -0700599 // Disable the password entry and back presses.
600 if (mPasswordEntry != null) {
601 mPasswordEntry.setEnabled(false);
602 }
603 if (mLockPatternView != null) {
604 mLockPatternView.setEnabled(false);
605 }
606
Andy Stadler13d62042011-01-31 19:21:37 -0800607 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700608 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800609
Jason parksf8217302011-01-26 13:11:42 -0600610 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700611 mHandler.removeMessages(MESSAGE_COOLDOWN);
612 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600613 }
Jason parksec5a45e2011-01-18 15:28:36 -0600614 }
Jason parks35933812011-01-21 15:48:20 -0600615
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800616 /**
617 * Sets the back status: enabled or disabled according to the parameter.
618 * @param isEnabled true if back is enabled, false otherwise.
619 */
620 private final void setBackFunctionality(boolean isEnabled) {
621 mIgnoreBack = !isEnabled;
622 if (isEnabled) {
623 mStatusBar.disable(sWidgetsToDisable);
624 } else {
625 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
626 }
627 }
628
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800629 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700630 new LockPatternView.OnPatternListener() {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800631
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700632 @Override
633 public void onPatternStart() {
634 mLockPatternView.removeCallbacks(mClearPatternRunnable);
635 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800636
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700637 @Override
638 public void onPatternCleared() {
639 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800640
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700641 @Override
642 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
643 mLockPatternView.setEnabled(false);
644 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
645 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800646
Paul Lawrencef6cda3b2014-06-12 08:26:08 -0700647 @Override
648 public void onPatternCellAdded(List<Cell> pattern) {
649 }
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800650 };
651
652 private void passwordEntryInit() {
653 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600654 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800655 if (mPasswordEntry != null){
656 mPasswordEntry.setOnEditorActionListener(this);
657 mPasswordEntry.requestFocus();
658 // Become quiet when the user interacts with the Edit text screen.
659 mPasswordEntry.setOnKeyListener(this);
660 mPasswordEntry.setOnTouchListener(this);
661 mPasswordEntry.addTextChangedListener(this);
662 }
663
664 // Pattern case
665 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
666 if (mLockPatternView != null) {
667 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
668 }
Jason parks35933812011-01-21 15:48:20 -0600669
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700670 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700671 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700672 final View emergencyCall = findViewById(R.id.emergencyCallButton);
673 if (emergencyCall != null) {
674 Log.d(TAG, "Removing the emergency Call button");
675 emergencyCall.setVisibility(View.GONE);
676 }
677 }
678
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700679 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700680 final InputMethodManager imm = (InputMethodManager) getSystemService(
681 Context.INPUT_METHOD_SERVICE);
682 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
683 imeSwitcher.setVisibility(View.VISIBLE);
684 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700685 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700686 public void onClick(View v) {
687 imm.showInputMethodPicker();
688 }
689 });
Jason parks00046d62011-06-13 17:38:45 -0500690 }
David Brown8373b452011-06-20 12:38:45 -0700691
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700692 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
693 // is completely non-functional, and we want the user to notice the device and enter a
694 // password.
695 if (mWakeLock == null) {
696 Log.d(TAG, "Acquiring wakelock.");
697 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
698 if (pm != null) {
699 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
700 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700701 // Keep awake for 10 minutes - if the user hasn't been alerted by then
702 // best not to just drain their battery
Paul Lawrence5a70f052014-06-13 11:09:55 -0700703 mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700704 }
705 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700706
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700707 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
708 // immediately.
Paul Lawrence5a70f052014-06-13 11:09:55 -0700709 if (mLockPatternView == null && mCooldown <= 0) {
Paul Lawrencee54e9322014-03-13 14:05:48 -0700710 mHandler.postDelayed(new Runnable() {
711 @Override public void run() {
712 imm.showSoftInputUnchecked(0, null);
713 }
714 }, 0);
715 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700716
David Brown8373b452011-06-20 12:38:45 -0700717 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700718 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700719 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700720 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800721
722 // Dismiss keyguard while this screen is showing.
723 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600724 }
725
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700726 /**
727 * Method adapted from com.android.inputmethod.latin.Utils
728 *
729 * @param imm The input method manager
730 * @param shouldIncludeAuxiliarySubtypes
731 * @return true if we have multiple IMEs to choose from
732 */
733 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
734 final boolean shouldIncludeAuxiliarySubtypes) {
735 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
736
737 // Number of the filtered IMEs
738 int filteredImisCount = 0;
739
740 for (InputMethodInfo imi : enabledImis) {
741 // We can return true immediately after we find two or more filtered IMEs.
742 if (filteredImisCount > 1) return true;
743 final List<InputMethodSubtype> subtypes =
744 imm.getEnabledInputMethodSubtypeList(imi, true);
745 // IMEs that have no subtypes should be counted.
746 if (subtypes.isEmpty()) {
747 ++filteredImisCount;
748 continue;
749 }
750
751 int auxCount = 0;
752 for (InputMethodSubtype subtype : subtypes) {
753 if (subtype.isAuxiliary()) {
754 ++auxCount;
755 }
756 }
757 final int nonAuxCount = subtypes.size() - auxCount;
758
759 // IMEs that have one or more non-auxiliary subtypes should be counted.
760 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
761 // subtypes should be counted as well.
762 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
763 ++filteredImisCount;
764 continue;
765 }
766 }
767
768 return filteredImisCount > 1
769 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
770 // input method subtype (The current IME should be LatinIME.)
771 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
772 }
773
Jason parks8fd5bc92011-01-12 16:03:31 -0600774 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700775 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600776 if (service != null) {
777 return IMountService.Stub.asInterface(service);
778 }
779 return null;
780 }
781
782 @Override
783 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500784 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600785 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700786 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600787
Jason parksec5a45e2011-01-18 15:28:36 -0600788 if (TextUtils.isEmpty(password)) {
789 return true;
790 }
Jason parks35933812011-01-21 15:48:20 -0600791
Jason parks8fd5bc92011-01-12 16:03:31 -0600792 // Now that we have the password clear the password field.
793 v.setText(null);
794
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700795 // Disable the password entry and back keypress while checking the password. These
796 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600797 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800798 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600799
Ben Komalo04606752011-08-18 14:50:26 -0700800 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600801 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600802
Jason parks8fd5bc92011-01-12 16:03:31 -0600803 return true;
804 }
805 return false;
806 }
David Brown8373b452011-06-20 12:38:45 -0700807
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700808 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700809 * Set airplane mode on the device if it isn't an LTE device.
810 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
811 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
812 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
813 * both these problems, we turn the radio off. However, on certain networks turning on and
814 * off the radio takes a long time. In such cases, we are better off leaving the radio
815 * running so the latency of an E911 call is short.
816 * The behavior after this is:
817 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
818 * airplane mode and restart the radio.
819 * 2. Full boot: we read the persistent settings from the previous boot and restore the
820 * radio to whatever it was before it restarted. This also happens when rebooting a
821 * phone that has no encryption.
822 */
823 private final void setAirplaneModeIfNecessary() {
824 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700825 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700826 if (!isLteDevice) {
827 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700828 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700829 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
830 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700831 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700832 }
833 }
834
835 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700836 * Code to update the state of, and handle clicks from, the "Emergency call" button.
837 *
838 * This code is mostly duplicated from the corresponding code in
839 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
840 */
David Brown8373b452011-06-20 12:38:45 -0700841 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700842 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700843 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700844 if (emergencyCall == null)
845 return;
David Brown8373b452011-06-20 12:38:45 -0700846
847 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700848 emergencyCall.setVisibility(View.VISIBLE);
849 emergencyCall.setOnClickListener(new View.OnClickListener() {
850 @Override
851
David Brown8373b452011-06-20 12:38:45 -0700852 public void onClick(View v) {
853 takeEmergencyCallAction();
854 }
855 });
856 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700857 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700858 return;
859 }
860
Santos Cordon3afbdf02014-05-29 21:48:16 -0700861 final int newState = getTelephonyManager().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700862 int textId;
863 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700864 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700865 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700866 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
867 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700868 } else {
869 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700870 final int emergencyIcon = R.drawable.ic_emergency;
871 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700872 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700873 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700874 }
875
876 private boolean isEmergencyCallCapable() {
877 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
878 }
879
880 private void takeEmergencyCallAction() {
Santos Cordon3afbdf02014-05-29 21:48:16 -0700881 TelephonyManager telephonyManager = getTelephonyManager();
882 if (telephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
883 telephonyManager.showCallScreen();
David Brown8373b452011-06-20 12:38:45 -0700884 } else {
885 launchEmergencyDialer();
886 }
887 }
888
David Brown8373b452011-06-20 12:38:45 -0700889
890 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700891 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
893 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Paul Lawrence5a70f052014-06-13 11:09:55 -0700894 setBackFunctionality(true);
David Brown8373b452011-06-20 12:38:45 -0700895 startActivity(intent);
896 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700897
Santos Cordon3afbdf02014-05-29 21:48:16 -0700898 private TelephonyManager getTelephonyManager() {
899 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
900 }
901
Vikram Aggarwald1147252012-05-08 13:52:30 -0700902 /**
903 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
904 */
905 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700906 mNotificationCountdown = 20;
907 }
908
909 @Override
910 public boolean onKey(View v, int keyCode, KeyEvent event) {
911 delayAudioNotification();
912 return false;
913 }
914
915 @Override
916 public boolean onTouch(View v, MotionEvent event) {
917 delayAudioNotification();
918 return false;
919 }
920
921 @Override
922 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
923 return;
924 }
925
926 @Override
927 public void onTextChanged(CharSequence s, int start, int before, int count) {
928 delayAudioNotification();
929 }
930
931 @Override
932 public void afterTextChanged(Editable s) {
933 return;
934 }
David Brown8373b452011-06-20 12:38:45 -0700935}