blob: c4e3e668247f96ea96190e055ffdd0477fb8d307 [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;
Jason parks8fd5bc92011-01-12 16:03:31 -060043import android.util.Log;
44import android.view.KeyEvent;
Vikram Aggarwald1147252012-05-08 13:52:30 -070045import android.view.MotionEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080046import android.view.View;
Jim Millerfb3d5ca2013-11-14 14:40:22 -080047import android.view.WindowManager;
Andy Stadler13d62042011-01-31 19:21:37 -080048import android.view.View.OnClickListener;
Vikram Aggarwald1147252012-05-08 13:52:30 -070049import android.view.View.OnKeyListener;
50import android.view.View.OnTouchListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060051import android.view.inputmethod.EditorInfo;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070052import android.view.inputmethod.InputMethodInfo;
Jason parks75c085e2011-02-10 14:03:47 -060053import android.view.inputmethod.InputMethodManager;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070054import android.view.inputmethod.InputMethodSubtype;
Andy Stadler13d62042011-01-31 19:21:37 -080055import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060056import android.widget.EditText;
57import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060058import android.widget.TextView;
59
Vikram Aggarwalbf459da2012-11-08 16:54:59 -080060import com.android.internal.statusbar.StatusBarIcon;
Ben Komalo91a2f052011-08-16 17:48:25 -070061import com.android.internal.telephony.ITelephony;
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
68import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070069
70/**
71 * Settings screens to show the UI flows for encrypting/decrypting the device.
72 *
73 * This may be started via adb for debugging the UI layout, without having to go through
74 * encryption flows everytime. It should be noted that starting the activity in this manner
75 * is only useful for verifying UI-correctness - the behavior will not be identical.
76 * <pre>
77 * $ adb shell pm enable com.android.settings/.CryptKeeper
78 * $ adb shell am start \
79 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
80 * -n com.android.settings/.CryptKeeper
81 * </pre>
82 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070083public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
84 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060085 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060086
Jason parks8fd5bc92011-01-12 16:03:31 -060087 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070088 /** Message sent to us to indicate encryption update progress. */
89 private static final int MESSAGE_UPDATE_PROGRESS = 1;
90 /** Message sent to us to cool-down (waste user's time between password attempts) */
91 private static final int MESSAGE_COOLDOWN = 2;
92 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
93 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060094
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070095 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060096 private static final int MAX_FAILED_ATTEMPTS = 30;
97 private static final int COOL_DOWN_ATTEMPTS = 10;
98 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
99
David Brown8373b452011-06-20 12:38:45 -0700100 // Intent action for launching the Emergency Dialer activity.
101 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
102
Ben Komalo91a2f052011-08-16 17:48:25 -0700103 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
104 private static final String EXTRA_FORCE_VIEW =
105 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
106 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700107 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700108 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700109
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700110 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700111 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700112 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700113 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
114 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700115 /** A flag to indicate when the back event should be ignored */
116 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800117 private int mCooldown;
118 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600119 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800120 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700121 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
122 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700123 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
124 private int mReleaseWakeLockCountdown = 0;
Andy Stadler14997402011-02-01 15:34:59 -0800125
126 /**
127 * Used to propagate state through configuration changes (e.g. screen rotation)
128 */
129 private static class NonConfigurationInstanceState {
130 final PowerManager.WakeLock wakelock;
131
132 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
133 wakelock = _wakelock;
134 }
135 }
136
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700137 /**
138 * Activity used to fade the screen to black after the password is entered.
139 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700140 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800141 @Override
142 public void onCreate(Bundle savedInstanceState) {
143 super.onCreate(savedInstanceState);
144 setContentView(R.layout.crypt_keeper_blank);
145 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700146 /** Ignore all back events. */
147 @Override
148 public void onBackPressed() {
149 return;
150 }
Jason parksf1dbf552011-01-24 16:19:28 -0600151 }
Jason parksec5a45e2011-01-18 15:28:36 -0600152
Jason parks06c5ff42011-03-01 10:17:40 -0600153 private class DecryptTask extends AsyncTask<String, Void, Integer> {
154 @Override
155 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700156 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600157 try {
158 return service.decryptStorage(params[0]);
159 } catch (Exception e) {
160 Log.e(TAG, "Error while decrypting...", e);
161 return -1;
162 }
163 }
164
165 @Override
166 protected void onPostExecute(Integer failedAttempts) {
167 if (failedAttempts == 0) {
168 // The password was entered successfully. Start the Blank activity
169 // so this activity animates to black before the devices starts. Note
170 // It has 1 second to complete the animation or it will be frozen
171 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700172 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600173 finish();
174 startActivity(intent);
175 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
176 // Factory reset the device.
177 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
178 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700179 if (mLockPatternView != null) {
180 mLockPatternView.clearPattern();
181 }
Jason parks06c5ff42011-03-01 10:17:40 -0600182 mCooldown = COOL_DOWN_INTERVAL;
183 cooldown();
184 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700185 final TextView status = (TextView) findViewById(R.id.status);
186 status.setText(R.string.try_again);
Jason parks06c5ff42011-03-01 10:17:40 -0600187 // Reenable the password entry
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800188 if (mPasswordEntry != null) {
189 mPasswordEntry.setEnabled(true);
190 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700191 if (mLockPatternView != null) {
192 mLockPatternView.setEnabled(true);
193 }
Jason parks06c5ff42011-03-01 10:17:40 -0600194 }
195 }
196 }
Jason parks75c085e2011-02-10 14:03:47 -0600197
Ben Komalo0e666092011-09-01 15:42:00 -0700198 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
199 @Override
200 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700201 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700202 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700203 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700204 int state = service.getEncryptionState();
205 if (state == IMountService.ENCRYPTION_STATE_NONE) {
206 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
207 return true; // Unexpected, but fine, I guess...
208 }
209 return state == IMountService.ENCRYPTION_STATE_OK;
210 } catch (RemoteException e) {
211 Log.w(TAG, "Unable to get encryption state properly");
212 return true;
213 }
214 }
215
216 @Override
217 protected void onPostExecute(Boolean result) {
218 mValidationComplete = true;
219 if (Boolean.FALSE.equals(result)) {
220 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
221 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700222 } else {
223 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700224 }
225 setupUi();
226 }
227 }
228
Ben Komalo91a2f052011-08-16 17:48:25 -0700229 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600230 @Override
231 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600232 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700233 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600234 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600235 break;
Jason parks35933812011-01-21 15:48:20 -0600236
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700237 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600238 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600239 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700240
241 case MESSAGE_NOTIFY:
242 notifyUser();
243 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600244 }
245 }
246 };
Jason parks35933812011-01-21 15:48:20 -0600247
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700248 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800249 /** The status bar where back/home/recent buttons are shown. */
250 private StatusBarManager mStatusBar;
251
252 /** All the widgets to disable in the status bar */
253 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
254 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
255 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
256 | StatusBarManager.DISABLE_SYSTEM_INFO
257 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700258 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800259 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700260
Ben Komalo91a2f052011-08-16 17:48:25 -0700261 /** @return whether or not this Activity was started for debugging the UI only. */
262 private boolean isDebugView() {
263 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
264 }
265
266 /** @return whether or not this Activity was started for debugging the specific UI view only. */
267 private boolean isDebugView(String viewType /* non-nullable */) {
268 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
269 }
270
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700271 /**
272 * Notify the user that we are awaiting input. Currently this sends an audio alert.
273 */
274 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700275 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700276 --mNotificationCountdown;
277 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700278 try {
279 // Play the standard keypress sound at full volume. This should be available on
280 // every device. We cannot play a ringtone here because media services aren't
281 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
282 // on tablet devices. The idea is to alert the user that something is needed: this
283 // does not have to be pleasing.
284 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
285 } catch (Exception e) {
286 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
287 }
288 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700289 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700290 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700291 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700292
293 if (mWakeLock.isHeld()) {
294 if (mReleaseWakeLockCountdown > 0) {
295 --mReleaseWakeLockCountdown;
296 } else {
297 mWakeLock.release();
298 }
299 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700300 }
301
302 /**
303 * Ignore back events after the user has entered the decrypt screen and while the device is
304 * encrypting.
305 */
306 @Override
307 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800308 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700309 if (mIgnoreBack)
310 return;
311 super.onBackPressed();
312 }
313
Jason parks8fd5bc92011-01-12 16:03:31 -0600314 @Override
315 public void onCreate(Bundle savedInstanceState) {
316 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600317
Andy Stadler95974062011-02-01 17:35:20 -0800318 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700319 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700320 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600321 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600322 PackageManager pm = getPackageManager();
323 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700324 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
325 PackageManager.DONT_KILL_APP);
326 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700327 // want to be running, so need to finish this activity. We can count
328 // on the activity manager re-launching the new home app upon finishing
329 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700330 // NOTE: This is really grungy. I think it would be better for the
331 // activity manager to explicitly launch the crypt keeper instead of
332 // home in the situation where we need to decrypt the device
333 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600334 return;
335 }
Jason parks35933812011-01-21 15:48:20 -0600336
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700337 // Disable the status bar, but do NOT disable back because the user needs a way to go
338 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800339 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
340 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800341
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700342 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700343 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800344 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700345 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800346 if (lastInstance instanceof NonConfigurationInstanceState) {
347 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
348 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700349 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800350 }
Jason parksec5a45e2011-01-18 15:28:36 -0600351 }
Jason parks35933812011-01-21 15:48:20 -0600352
Andy Stadler95974062011-02-01 17:35:20 -0800353 /**
354 * Note, we defer the state check and screen setup to onStart() because this will be
355 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
356 * especially important if we were to lose the wakelock for any reason.
357 */
358 @Override
359 public void onStart() {
360 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700361 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700362 }
363
364 /**
365 * Initializes the UI based on the current state of encryption.
366 * This is idempotent - calling repeatedly will simply re-initialize the UI.
367 */
368 private void setupUi() {
369 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
370 setContentView(R.layout.crypt_keeper_progress);
371 showFactoryReset();
372 return;
373 }
374
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700375 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700376 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800377 setContentView(R.layout.crypt_keeper_progress);
378 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700379 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700380 new AsyncTask<Void, Void, Void>() {
381 int type = StorageManager.CRYPT_TYPE_PASSWORD;
382 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800383
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700384 @Override
385 public Void doInBackground(Void... v) {
386 try {
387 final IMountService service = getMountService();
388 type = service.getPasswordType();
389 owner_info = service.getField("OwnerInfo");
390 } catch (Exception e) {
391 Log.e(TAG, "Error calling mount service " + e);
392 }
393
394 return null;
395 }
396
397 @Override
398 public void onPostExecute(java.lang.Void v) {
399 if(type == StorageManager.CRYPT_TYPE_PIN) {
400 setContentView(R.layout.crypt_keeper_pin_entry);
401 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
402 setContentView(R.layout.crypt_keeper_pattern_entry);
403 setBackFunctionality(false);
404 } else {
405 setContentView(R.layout.crypt_keeper_password_entry);
406 }
407
408 final TextView status = (TextView) findViewById(R.id.owner_info);
409 status.setText(owner_info);
410 passwordEntryInit();
411 }
412 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700413 } else if (!mValidationRequested) {
414 // We're supposed to be encrypted, but no validation has been done.
415 new ValidationTask().execute((Void[]) null);
416 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800417 }
418 }
419
Jason parksf8217302011-01-26 13:11:42 -0600420 @Override
421 public void onStop() {
422 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700423 mHandler.removeMessages(MESSAGE_COOLDOWN);
424 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
425 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800426 }
427
428 /**
429 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
430 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
431 * mWakeLock so the subsequent call to onDestroy does not release it.
432 */
433 @Override
434 public Object onRetainNonConfigurationInstance() {
435 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700436 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800437 mWakeLock = null;
438 return state;
439 }
440
441 @Override
442 public void onDestroy() {
443 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600444
445 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700446 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600447 mWakeLock.release();
448 mWakeLock = null;
449 }
450 }
451
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700452 /**
453 * Start encrypting the device.
454 */
Jason parksec5a45e2011-01-18 15:28:36 -0600455 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600456 // Accquire a partial wakelock to prevent the device from sleeping. Note
457 // we never release this wakelock as we will be restarted after the device
458 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700459 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700460 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700461 Log.d(TAG, "Acquiring wakelock.");
462 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
463 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
464 mWakeLock.acquire();
465 }
Jason parks35933812011-01-21 15:48:20 -0600466
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700467 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700468 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800469 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700470 // Start the first run of progress manually. This method sets up messages to occur at
471 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600472 updateProgress();
473 }
474
Andy Stadler13d62042011-01-31 19:21:37 -0800475 private void showFactoryReset() {
476 // Hide the encryption-bot to make room for the "factory reset" button
477 findViewById(R.id.encroid).setVisibility(View.GONE);
478
479 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700480 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800481 button.setVisibility(View.VISIBLE);
482 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700483 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800484 public void onClick(View v) {
485 // Factory reset the device.
486 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
487 }
488 });
489
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700490 // Alert the user of the failure.
491 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
492 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800493
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700494 final View view = findViewById(R.id.bottom_divider);
495 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700496 if (view != null) {
497 view.setVisibility(View.VISIBLE);
498 }
Andy Stadler13d62042011-01-31 19:21:37 -0800499 }
500
Jason parksf8217302011-01-26 13:11:42 -0600501 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700502 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600503
Ben Komalo0e666092011-09-01 15:42:00 -0700504 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800505 showFactoryReset();
506 return;
507 }
508
Jason parksf8217302011-01-26 13:11:42 -0600509 int progress = 0;
510 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700511 // Force a 50% progress state when debugging the view.
512 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600513 } catch (Exception e) {
514 Log.w(TAG, "Error parsing progress: " + e.toString());
515 }
516
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700517 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700518 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700519 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700520 if (tv != null) {
521 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
522 }
Jason parksf8217302011-01-26 13:11:42 -0600523 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700524 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
525 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600526 }
527
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700528 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600529 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700530 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800531
Jason parksf8217302011-01-26 13:11:42 -0600532 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700533 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700534 if (mPasswordEntry != null) {
535 mPasswordEntry.setEnabled(true);
536 setBackFunctionality(true);
537 }
538 if (mLockPatternView != null) {
539 mLockPatternView.setEnabled(true);
540 }
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700541 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600542 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800543 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700544 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800545
Jason parksf8217302011-01-26 13:11:42 -0600546 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700547 mHandler.removeMessages(MESSAGE_COOLDOWN);
548 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600549 }
Jason parksec5a45e2011-01-18 15:28:36 -0600550 }
Jason parks35933812011-01-21 15:48:20 -0600551
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800552 /**
553 * Sets the back status: enabled or disabled according to the parameter.
554 * @param isEnabled true if back is enabled, false otherwise.
555 */
556 private final void setBackFunctionality(boolean isEnabled) {
557 mIgnoreBack = !isEnabled;
558 if (isEnabled) {
559 mStatusBar.disable(sWidgetsToDisable);
560 } else {
561 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
562 }
563 }
564
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800565 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
566 new LockPatternView.OnPatternListener() {
567
568 @Override
569 public void onPatternStart() {
570 }
571
572 @Override
573 public void onPatternCleared() {
574 }
575
576 @Override
577 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700578 mLockPatternView.setEnabled(false);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800579 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
580 }
581
582 @Override
583 public void onPatternCellAdded(List<Cell> pattern) {
584 }
585 };
586
587 private void passwordEntryInit() {
588 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600589 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800590 if (mPasswordEntry != null){
591 mPasswordEntry.setOnEditorActionListener(this);
592 mPasswordEntry.requestFocus();
593 // Become quiet when the user interacts with the Edit text screen.
594 mPasswordEntry.setOnKeyListener(this);
595 mPasswordEntry.setOnTouchListener(this);
596 mPasswordEntry.addTextChangedListener(this);
597 }
598
599 // Pattern case
600 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
601 if (mLockPatternView != null) {
602 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
603 }
Jason parks35933812011-01-21 15:48:20 -0600604
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700605 // Disable the Emergency call button if the device has no voice telephone capability
606 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
607 if (!tm.isVoiceCapable()) {
608 final View emergencyCall = findViewById(R.id.emergencyCallButton);
609 if (emergencyCall != null) {
610 Log.d(TAG, "Removing the emergency Call button");
611 emergencyCall.setVisibility(View.GONE);
612 }
613 }
614
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700615 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700616 final InputMethodManager imm = (InputMethodManager) getSystemService(
617 Context.INPUT_METHOD_SERVICE);
618 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
619 imeSwitcher.setVisibility(View.VISIBLE);
620 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700621 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700622 public void onClick(View v) {
623 imm.showInputMethodPicker();
624 }
625 });
Jason parks00046d62011-06-13 17:38:45 -0500626 }
David Brown8373b452011-06-20 12:38:45 -0700627
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700628 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
629 // is completely non-functional, and we want the user to notice the device and enter a
630 // password.
631 if (mWakeLock == null) {
632 Log.d(TAG, "Acquiring wakelock.");
633 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
634 if (pm != null) {
635 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
636 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700637 // Keep awake for 10 minutes - if the user hasn't been alerted by then
638 // best not to just drain their battery
639 mReleaseWakeLockCountdown = 96; // 96 * 5 + 120 = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700640 }
641 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700642
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700643 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
644 // immediately.
Paul Lawrencee54e9322014-03-13 14:05:48 -0700645 if (mLockPatternView == null) {
646 mHandler.postDelayed(new Runnable() {
647 @Override public void run() {
648 imm.showSoftInputUnchecked(0, null);
649 }
650 }, 0);
651 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700652
David Brown8373b452011-06-20 12:38:45 -0700653 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700654 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700655 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700656 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800657
658 // Dismiss keyguard while this screen is showing.
659 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600660 }
661
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700662 /**
663 * Method adapted from com.android.inputmethod.latin.Utils
664 *
665 * @param imm The input method manager
666 * @param shouldIncludeAuxiliarySubtypes
667 * @return true if we have multiple IMEs to choose from
668 */
669 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
670 final boolean shouldIncludeAuxiliarySubtypes) {
671 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
672
673 // Number of the filtered IMEs
674 int filteredImisCount = 0;
675
676 for (InputMethodInfo imi : enabledImis) {
677 // We can return true immediately after we find two or more filtered IMEs.
678 if (filteredImisCount > 1) return true;
679 final List<InputMethodSubtype> subtypes =
680 imm.getEnabledInputMethodSubtypeList(imi, true);
681 // IMEs that have no subtypes should be counted.
682 if (subtypes.isEmpty()) {
683 ++filteredImisCount;
684 continue;
685 }
686
687 int auxCount = 0;
688 for (InputMethodSubtype subtype : subtypes) {
689 if (subtype.isAuxiliary()) {
690 ++auxCount;
691 }
692 }
693 final int nonAuxCount = subtypes.size() - auxCount;
694
695 // IMEs that have one or more non-auxiliary subtypes should be counted.
696 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
697 // subtypes should be counted as well.
698 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
699 ++filteredImisCount;
700 continue;
701 }
702 }
703
704 return filteredImisCount > 1
705 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
706 // input method subtype (The current IME should be LatinIME.)
707 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
708 }
709
Jason parks8fd5bc92011-01-12 16:03:31 -0600710 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700711 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600712 if (service != null) {
713 return IMountService.Stub.asInterface(service);
714 }
715 return null;
716 }
717
718 @Override
719 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500720 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600721 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700722 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600723
Jason parksec5a45e2011-01-18 15:28:36 -0600724 if (TextUtils.isEmpty(password)) {
725 return true;
726 }
Jason parks35933812011-01-21 15:48:20 -0600727
Jason parks8fd5bc92011-01-12 16:03:31 -0600728 // Now that we have the password clear the password field.
729 v.setText(null);
730
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700731 // Disable the password entry and back keypress while checking the password. These
732 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600733 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800734 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600735
Ben Komalo04606752011-08-18 14:50:26 -0700736 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600737 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600738
Jason parks8fd5bc92011-01-12 16:03:31 -0600739 return true;
740 }
741 return false;
742 }
David Brown8373b452011-06-20 12:38:45 -0700743
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700744 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700745 * Set airplane mode on the device if it isn't an LTE device.
746 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
747 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
748 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
749 * both these problems, we turn the radio off. However, on certain networks turning on and
750 * off the radio takes a long time. In such cases, we are better off leaving the radio
751 * running so the latency of an E911 call is short.
752 * The behavior after this is:
753 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
754 * airplane mode and restart the radio.
755 * 2. Full boot: we read the persistent settings from the previous boot and restore the
756 * radio to whatever it was before it restarted. This also happens when rebooting a
757 * phone that has no encryption.
758 */
759 private final void setAirplaneModeIfNecessary() {
760 final boolean isLteDevice =
Wink Saville55434042012-06-14 12:33:43 -0700761 TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700762 if (!isLteDevice) {
763 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700764 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700765 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
766 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700767 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700768 }
769 }
770
771 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700772 * Code to update the state of, and handle clicks from, the "Emergency call" button.
773 *
774 * This code is mostly duplicated from the corresponding code in
775 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
776 */
David Brown8373b452011-06-20 12:38:45 -0700777 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700778 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700779 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700780 if (emergencyCall == null)
781 return;
David Brown8373b452011-06-20 12:38:45 -0700782
783 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700784 emergencyCall.setVisibility(View.VISIBLE);
785 emergencyCall.setOnClickListener(new View.OnClickListener() {
786 @Override
787
David Brown8373b452011-06-20 12:38:45 -0700788 public void onClick(View v) {
789 takeEmergencyCallAction();
790 }
791 });
792 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700793 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700794 return;
795 }
796
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700797 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700798 int textId;
799 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700800 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700801 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700802 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
803 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700804 } else {
805 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700806 final int emergencyIcon = R.drawable.ic_emergency;
807 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700808 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700809 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700810 }
811
812 private boolean isEmergencyCallCapable() {
813 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
814 }
815
816 private void takeEmergencyCallAction() {
817 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
818 resumeCall();
819 } else {
820 launchEmergencyDialer();
821 }
822 }
823
824 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700825 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700826 if (phone != null) {
827 try {
828 phone.showCallScreen();
829 } catch (RemoteException e) {
830 Log.e(TAG, "Error calling ITelephony service: " + e);
831 }
832 }
833 }
834
835 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700836 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700837 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
838 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
839 startActivity(intent);
840 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700841
842 /**
843 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
844 */
845 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700846 mNotificationCountdown = 20;
847 }
848
849 @Override
850 public boolean onKey(View v, int keyCode, KeyEvent event) {
851 delayAudioNotification();
852 return false;
853 }
854
855 @Override
856 public boolean onTouch(View v, MotionEvent event) {
857 delayAudioNotification();
858 return false;
859 }
860
861 @Override
862 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
863 return;
864 }
865
866 @Override
867 public void onTextChanged(CharSequence s, int start, int before, int count) {
868 delayAudioNotification();
869 }
870
871 @Override
872 public void afterTextChanged(Editable s) {
873 return;
874 }
David Brown8373b452011-06-20 12:38:45 -0700875}