blob: f07d6fae1f5399d47ff83d022afd507e6bac4894 [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;
Vikram Aggarwal6ebbd302012-05-04 14:09:52 -070037import android.provider.Settings;
David Brown8373b452011-06-20 12:38:45 -070038import android.telephony.TelephonyManager;
Vikram Aggarwald1147252012-05-08 13:52:30 -070039import android.text.Editable;
Jason parksec5a45e2011-01-18 15:28:36 -060040import android.text.TextUtils;
Vikram Aggarwald1147252012-05-08 13:52:30 -070041import android.text.TextWatcher;
Jason parks8fd5bc92011-01-12 16:03:31 -060042import android.util.Log;
43import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070044import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080045import android.view.View;
46import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070047import android.view.View.OnKeyListener;
48import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060049import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070050import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060051import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070052import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080053import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060054import android.widget.EditText;
55import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060056import android.widget.TextView;
57
Ben Komalo91a2f052011-08-16 17:48:25 -070058import com.android.internal.telephony.ITelephony;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070059import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070060import com.android.internal.telephony.PhoneConstants;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070061
62import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070063
64/**
65 * Settings screens to show the UI flows for encrypting/decrypting the device.
66 *
67 * This may be started via adb for debugging the UI layout, without having to go through
68 * encryption flows everytime. It should be noted that starting the activity in this manner
69 * is only useful for verifying UI-correctness - the behavior will not be identical.
70 * <pre>
71 * $ adb shell pm enable com.android.settings/.CryptKeeper
72 * $ adb shell am start \
73 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
74 * -n com.android.settings/.CryptKeeper
75 * </pre>
76 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070077public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
78 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060079 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060080
Jason parks8fd5bc92011-01-12 16:03:31 -060081 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070082 /** Message sent to us to indicate encryption update progress. */
83 private static final int MESSAGE_UPDATE_PROGRESS = 1;
84 /** Message sent to us to cool-down (waste user's time between password attempts) */
85 private static final int MESSAGE_COOLDOWN = 2;
86 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
87 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060088
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070089 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060090 private static final int MAX_FAILED_ATTEMPTS = 30;
91 private static final int COOL_DOWN_ATTEMPTS = 10;
92 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
93
David Brown8373b452011-06-20 12:38:45 -070094 // Intent action for launching the Emergency Dialer activity.
95 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
96
Ben Komalo91a2f052011-08-16 17:48:25 -070097 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
98 private static final String EXTRA_FORCE_VIEW =
99 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
100 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700101 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700102 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700103
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700104 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700105 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700106 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700107 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
108 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700109 /** A flag to indicate when the back event should be ignored */
110 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800111 private int mCooldown;
112 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600113 private EditText mPasswordEntry;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700114 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
115 private int mNotificationCountdown = 0;
Andy Stadler14997402011-02-01 15:34:59 -0800116
117 /**
118 * Used to propagate state through configuration changes (e.g. screen rotation)
119 */
120 private static class NonConfigurationInstanceState {
121 final PowerManager.WakeLock wakelock;
122
123 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
124 wakelock = _wakelock;
125 }
126 }
127
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700128 /**
129 * Activity used to fade the screen to black after the password is entered.
130 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700131 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800132 @Override
133 public void onCreate(Bundle savedInstanceState) {
134 super.onCreate(savedInstanceState);
135 setContentView(R.layout.crypt_keeper_blank);
136 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700137 /** Ignore all back events. */
138 @Override
139 public void onBackPressed() {
140 return;
141 }
Jason parksf1dbf552011-01-24 16:19:28 -0600142 }
Jason parksec5a45e2011-01-18 15:28:36 -0600143
Jason parks06c5ff42011-03-01 10:17:40 -0600144 private class DecryptTask extends AsyncTask<String, Void, Integer> {
145 @Override
146 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700147 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600148 try {
149 return service.decryptStorage(params[0]);
150 } catch (Exception e) {
151 Log.e(TAG, "Error while decrypting...", e);
152 return -1;
153 }
154 }
155
156 @Override
157 protected void onPostExecute(Integer failedAttempts) {
158 if (failedAttempts == 0) {
159 // The password was entered successfully. Start the Blank activity
160 // so this activity animates to black before the devices starts. Note
161 // It has 1 second to complete the animation or it will be frozen
162 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700163 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600164 finish();
165 startActivity(intent);
166 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
167 // Factory reset the device.
168 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
169 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
170 mCooldown = COOL_DOWN_INTERVAL;
171 cooldown();
172 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700173 final TextView status = (TextView) findViewById(R.id.status);
174 status.setText(R.string.try_again);
Jason parks06c5ff42011-03-01 10:17:40 -0600175 // Reenable the password entry
176 mPasswordEntry.setEnabled(true);
177 }
178 }
179 }
Jason parks75c085e2011-02-10 14:03:47 -0600180
Ben Komalo0e666092011-09-01 15:42:00 -0700181 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
182 @Override
183 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700184 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700185 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700186 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700187 int state = service.getEncryptionState();
188 if (state == IMountService.ENCRYPTION_STATE_NONE) {
189 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
190 return true; // Unexpected, but fine, I guess...
191 }
192 return state == IMountService.ENCRYPTION_STATE_OK;
193 } catch (RemoteException e) {
194 Log.w(TAG, "Unable to get encryption state properly");
195 return true;
196 }
197 }
198
199 @Override
200 protected void onPostExecute(Boolean result) {
201 mValidationComplete = true;
202 if (Boolean.FALSE.equals(result)) {
203 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
204 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700205 } else {
206 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700207 }
208 setupUi();
209 }
210 }
211
Ben Komalo91a2f052011-08-16 17:48:25 -0700212 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600213 @Override
214 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600215 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700216 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600217 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600218 break;
Jason parks35933812011-01-21 15:48:20 -0600219
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700220 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600221 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600222 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700223
224 case MESSAGE_NOTIFY:
225 notifyUser();
226 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600227 }
228 }
229 };
Jason parks35933812011-01-21 15:48:20 -0600230
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700231 private AudioManager mAudioManager;
232
Ben Komalo91a2f052011-08-16 17:48:25 -0700233 /** @return whether or not this Activity was started for debugging the UI only. */
234 private boolean isDebugView() {
235 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
236 }
237
238 /** @return whether or not this Activity was started for debugging the specific UI view only. */
239 private boolean isDebugView(String viewType /* non-nullable */) {
240 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
241 }
242
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700243 /**
244 * Notify the user that we are awaiting input. Currently this sends an audio alert.
245 */
246 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700247 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700248 --mNotificationCountdown;
249 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700250 try {
251 // Play the standard keypress sound at full volume. This should be available on
252 // every device. We cannot play a ringtone here because media services aren't
253 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
254 // on tablet devices. The idea is to alert the user that something is needed: this
255 // does not have to be pleasing.
256 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
257 } catch (Exception e) {
258 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
259 }
260 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700261 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700262 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700263 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700264 }
265
266 /**
267 * Ignore back events after the user has entered the decrypt screen and while the device is
268 * encrypting.
269 */
270 @Override
271 public void onBackPressed() {
272 if (mIgnoreBack)
273 return;
274 super.onBackPressed();
275 }
276
Jason parks8fd5bc92011-01-12 16:03:31 -0600277 @Override
278 public void onCreate(Bundle savedInstanceState) {
279 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600280
Andy Stadler95974062011-02-01 17:35:20 -0800281 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700282 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700283 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600284 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600285 PackageManager pm = getPackageManager();
286 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700287 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
288 PackageManager.DONT_KILL_APP);
289 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700290 // want to be running, so need to finish this activity. We can count
291 // on the activity manager re-launching the new home app upon finishing
292 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700293 // NOTE: This is really grungy. I think it would be better for the
294 // activity manager to explicitly launch the crypt keeper instead of
295 // home in the situation where we need to decrypt the device
296 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600297 return;
298 }
Jason parks35933812011-01-21 15:48:20 -0600299
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700300 // Disable the status bar, but do NOT disable back because the user needs a way to go
301 // from keyboard settings and back to the password screen.
Jason parks39f1e042011-01-20 23:29:28 -0600302 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Andy Stadler13d62042011-01-31 19:21:37 -0800303 sbm.disable(StatusBarManager.DISABLE_EXPAND
304 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
Jason parks39f1e042011-01-20 23:29:28 -0600305 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Andy Stadler13d62042011-01-31 19:21:37 -0800306 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -0400307 | StatusBarManager.DISABLE_HOME
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700308 | StatusBarManager.DISABLE_RECENT);
Andy Stadler14997402011-02-01 15:34:59 -0800309
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700310 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700311 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800312 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700313 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800314 if (lastInstance instanceof NonConfigurationInstanceState) {
315 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
316 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700317 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800318 }
Jason parksec5a45e2011-01-18 15:28:36 -0600319 }
Jason parks35933812011-01-21 15:48:20 -0600320
Andy Stadler95974062011-02-01 17:35:20 -0800321 /**
322 * Note, we defer the state check and screen setup to onStart() because this will be
323 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
324 * especially important if we were to lose the wakelock for any reason.
325 */
326 @Override
327 public void onStart() {
328 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700329 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700330 }
331
332 /**
333 * Initializes the UI based on the current state of encryption.
334 * This is idempotent - calling repeatedly will simply re-initialize the UI.
335 */
336 private void setupUi() {
337 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
338 setContentView(R.layout.crypt_keeper_progress);
339 showFactoryReset();
340 return;
341 }
342
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700343 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700344 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800345 setContentView(R.layout.crypt_keeper_progress);
346 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700347 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Andy Stadler95974062011-02-01 17:35:20 -0800348 setContentView(R.layout.crypt_keeper_password_entry);
349 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700350 } else if (!mValidationRequested) {
351 // We're supposed to be encrypted, but no validation has been done.
352 new ValidationTask().execute((Void[]) null);
353 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800354 }
355 }
356
Jason parksf8217302011-01-26 13:11:42 -0600357 @Override
358 public void onStop() {
359 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700360 mHandler.removeMessages(MESSAGE_COOLDOWN);
361 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
362 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800363 }
364
365 /**
366 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
367 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
368 * mWakeLock so the subsequent call to onDestroy does not release it.
369 */
370 @Override
371 public Object onRetainNonConfigurationInstance() {
372 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700373 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800374 mWakeLock = null;
375 return state;
376 }
377
378 @Override
379 public void onDestroy() {
380 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600381
382 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700383 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600384 mWakeLock.release();
385 mWakeLock = null;
386 }
387 }
388
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700389 /**
390 * Start encrypting the device.
391 */
Jason parksec5a45e2011-01-18 15:28:36 -0600392 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600393 // Accquire a partial wakelock to prevent the device from sleeping. Note
394 // we never release this wakelock as we will be restarted after the device
395 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700396 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700397 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700398 Log.d(TAG, "Acquiring wakelock.");
399 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
400 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
401 mWakeLock.acquire();
402 }
Jason parks35933812011-01-21 15:48:20 -0600403
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700404 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700405 // Ignore all back presses from now, both hard and soft keys.
406 mIgnoreBack = true;
407 // Start the first run of progress manually. This method sets up messages to occur at
408 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600409 updateProgress();
410 }
411
Andy Stadler13d62042011-01-31 19:21:37 -0800412 private void showFactoryReset() {
413 // Hide the encryption-bot to make room for the "factory reset" button
414 findViewById(R.id.encroid).setVisibility(View.GONE);
415
416 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700417 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800418 button.setVisibility(View.VISIBLE);
419 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700420 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800421 public void onClick(View v) {
422 // Factory reset the device.
423 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
424 }
425 });
426
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700427 // Alert the user of the failure.
428 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
429 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800430
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700431 final View view = findViewById(R.id.bottom_divider);
432 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700433 if (view != null) {
434 view.setVisibility(View.VISIBLE);
435 }
Andy Stadler13d62042011-01-31 19:21:37 -0800436 }
437
Jason parksf8217302011-01-26 13:11:42 -0600438 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700439 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600440
Ben Komalo0e666092011-09-01 15:42:00 -0700441 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800442 showFactoryReset();
443 return;
444 }
445
Jason parksf8217302011-01-26 13:11:42 -0600446 int progress = 0;
447 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700448 // Force a 50% progress state when debugging the view.
449 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600450 } catch (Exception e) {
451 Log.w(TAG, "Error parsing progress: " + e.toString());
452 }
453
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700454 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700455 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700456 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700457 if (tv != null) {
458 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
459 }
Jason parksf8217302011-01-26 13:11:42 -0600460 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700461 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
462 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600463 }
464
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700465 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600466 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700467 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800468
Jason parksf8217302011-01-26 13:11:42 -0600469 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700470 // Re-enable the password entry and back presses.
Jason parks06c5ff42011-03-01 10:17:40 -0600471 mPasswordEntry.setEnabled(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700472 mIgnoreBack = false;
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700473 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600474 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800475 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700476 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800477
Jason parksf8217302011-01-26 13:11:42 -0600478 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700479 mHandler.removeMessages(MESSAGE_COOLDOWN);
480 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600481 }
Jason parksec5a45e2011-01-18 15:28:36 -0600482 }
Jason parks35933812011-01-21 15:48:20 -0600483
Jason parksec5a45e2011-01-18 15:28:36 -0600484 private void passwordEntryInit() {
Jason parks06c5ff42011-03-01 10:17:40 -0600485 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
486 mPasswordEntry.setOnEditorActionListener(this);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700487 mPasswordEntry.requestFocus();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700488 // Become quiet when the user interacts with the Edit text screen.
489 mPasswordEntry.setOnKeyListener(this);
490 mPasswordEntry.setOnTouchListener(this);
491 mPasswordEntry.addTextChangedListener(this);
Jason parks35933812011-01-21 15:48:20 -0600492
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700493 // Disable the Emergency call button if the device has no voice telephone capability
494 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
495 if (!tm.isVoiceCapable()) {
496 final View emergencyCall = findViewById(R.id.emergencyCallButton);
497 if (emergencyCall != null) {
498 Log.d(TAG, "Removing the emergency Call button");
499 emergencyCall.setVisibility(View.GONE);
500 }
501 }
502
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700503 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700504 final InputMethodManager imm = (InputMethodManager) getSystemService(
505 Context.INPUT_METHOD_SERVICE);
506 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
507 imeSwitcher.setVisibility(View.VISIBLE);
508 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700509 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700510 public void onClick(View v) {
511 imm.showInputMethodPicker();
512 }
513 });
Jason parks00046d62011-06-13 17:38:45 -0500514 }
David Brown8373b452011-06-20 12:38:45 -0700515
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700516 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
517 // is completely non-functional, and we want the user to notice the device and enter a
518 // password.
519 if (mWakeLock == null) {
520 Log.d(TAG, "Acquiring wakelock.");
521 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
522 if (pm != null) {
523 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
524 mWakeLock.acquire();
525 }
526 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700527 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
528 // immediately.
529 mHandler.postDelayed(new Runnable() {
530 @Override public void run() {
531 imm.showSoftInputUnchecked(0, null);
532 }
533 }, 0);
534
David Brown8373b452011-06-20 12:38:45 -0700535 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700536 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700537 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700538 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jason parks8fd5bc92011-01-12 16:03:31 -0600539 }
540
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700541 /**
542 * Method adapted from com.android.inputmethod.latin.Utils
543 *
544 * @param imm The input method manager
545 * @param shouldIncludeAuxiliarySubtypes
546 * @return true if we have multiple IMEs to choose from
547 */
548 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
549 final boolean shouldIncludeAuxiliarySubtypes) {
550 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
551
552 // Number of the filtered IMEs
553 int filteredImisCount = 0;
554
555 for (InputMethodInfo imi : enabledImis) {
556 // We can return true immediately after we find two or more filtered IMEs.
557 if (filteredImisCount > 1) return true;
558 final List<InputMethodSubtype> subtypes =
559 imm.getEnabledInputMethodSubtypeList(imi, true);
560 // IMEs that have no subtypes should be counted.
561 if (subtypes.isEmpty()) {
562 ++filteredImisCount;
563 continue;
564 }
565
566 int auxCount = 0;
567 for (InputMethodSubtype subtype : subtypes) {
568 if (subtype.isAuxiliary()) {
569 ++auxCount;
570 }
571 }
572 final int nonAuxCount = subtypes.size() - auxCount;
573
574 // IMEs that have one or more non-auxiliary subtypes should be counted.
575 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
576 // subtypes should be counted as well.
577 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
578 ++filteredImisCount;
579 continue;
580 }
581 }
582
583 return filteredImisCount > 1
584 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
585 // input method subtype (The current IME should be LatinIME.)
586 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
587 }
588
Jason parks8fd5bc92011-01-12 16:03:31 -0600589 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700590 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600591 if (service != null) {
592 return IMountService.Stub.asInterface(service);
593 }
594 return null;
595 }
596
597 @Override
598 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500599 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600600 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700601 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600602
Jason parksec5a45e2011-01-18 15:28:36 -0600603 if (TextUtils.isEmpty(password)) {
604 return true;
605 }
Jason parks35933812011-01-21 15:48:20 -0600606
Jason parks8fd5bc92011-01-12 16:03:31 -0600607 // Now that we have the password clear the password field.
608 v.setText(null);
609
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700610 // Disable the password entry and back keypress while checking the password. These
611 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600612 mPasswordEntry.setEnabled(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700613 mIgnoreBack = true;
Jason parks8fd5bc92011-01-12 16:03:31 -0600614
Ben Komalo04606752011-08-18 14:50:26 -0700615 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600616 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600617
Jason parks8fd5bc92011-01-12 16:03:31 -0600618 return true;
619 }
620 return false;
621 }
David Brown8373b452011-06-20 12:38:45 -0700622
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700623 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700624 * Set airplane mode on the device if it isn't an LTE device.
625 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
626 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
627 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
628 * both these problems, we turn the radio off. However, on certain networks turning on and
629 * off the radio takes a long time. In such cases, we are better off leaving the radio
630 * running so the latency of an E911 call is short.
631 * The behavior after this is:
632 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
633 * airplane mode and restart the radio.
634 * 2. Full boot: we read the persistent settings from the previous boot and restore the
635 * radio to whatever it was before it restarted. This also happens when rebooting a
636 * phone that has no encryption.
637 */
638 private final void setAirplaneModeIfNecessary() {
639 final boolean isLteDevice =
Wink Saville55434042012-06-14 12:33:43 -0700640 TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700641 if (!isLteDevice) {
642 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700643 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700644 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
645 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700646 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700647 }
648 }
649
650 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700651 * Code to update the state of, and handle clicks from, the "Emergency call" button.
652 *
653 * This code is mostly duplicated from the corresponding code in
654 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
655 */
David Brown8373b452011-06-20 12:38:45 -0700656 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700657 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700658 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700659 if (emergencyCall == null)
660 return;
David Brown8373b452011-06-20 12:38:45 -0700661
662 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700663 emergencyCall.setVisibility(View.VISIBLE);
664 emergencyCall.setOnClickListener(new View.OnClickListener() {
665 @Override
666
David Brown8373b452011-06-20 12:38:45 -0700667 public void onClick(View v) {
668 takeEmergencyCallAction();
669 }
670 });
671 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700672 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700673 return;
674 }
675
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700676 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700677 int textId;
678 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700679 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700680 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700681 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
682 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700683 } else {
684 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700685 final int emergencyIcon = R.drawable.ic_emergency;
686 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700687 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700688 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700689 }
690
691 private boolean isEmergencyCallCapable() {
692 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
693 }
694
695 private void takeEmergencyCallAction() {
696 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
697 resumeCall();
698 } else {
699 launchEmergencyDialer();
700 }
701 }
702
703 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700704 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700705 if (phone != null) {
706 try {
707 phone.showCallScreen();
708 } catch (RemoteException e) {
709 Log.e(TAG, "Error calling ITelephony service: " + e);
710 }
711 }
712 }
713
714 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700715 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700716 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
717 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
718 startActivity(intent);
719 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700720
721 /**
722 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
723 */
724 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700725 mNotificationCountdown = 20;
726 }
727
728 @Override
729 public boolean onKey(View v, int keyCode, KeyEvent event) {
730 delayAudioNotification();
731 return false;
732 }
733
734 @Override
735 public boolean onTouch(View v, MotionEvent event) {
736 delayAudioNotification();
737 return false;
738 }
739
740 @Override
741 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
742 return;
743 }
744
745 @Override
746 public void onTextChanged(CharSequence s, int start, int before, int count) {
747 delayAudioNotification();
748 }
749
750 @Override
751 public void afterTextChanged(Editable s) {
752 return;
753 }
David Brown8373b452011-06-20 12:38:45 -0700754}