blob: 352fbd908095e9113c6f2b2993ef96bc07ecd1fd [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;
Andy Stadler14997402011-02-01 15:34:59 -0800123
124 /**
125 * Used to propagate state through configuration changes (e.g. screen rotation)
126 */
127 private static class NonConfigurationInstanceState {
128 final PowerManager.WakeLock wakelock;
129
130 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
131 wakelock = _wakelock;
132 }
133 }
134
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700135 /**
136 * Activity used to fade the screen to black after the password is entered.
137 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700138 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800139 @Override
140 public void onCreate(Bundle savedInstanceState) {
141 super.onCreate(savedInstanceState);
142 setContentView(R.layout.crypt_keeper_blank);
143 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700144 /** Ignore all back events. */
145 @Override
146 public void onBackPressed() {
147 return;
148 }
Jason parksf1dbf552011-01-24 16:19:28 -0600149 }
Jason parksec5a45e2011-01-18 15:28:36 -0600150
Jason parks06c5ff42011-03-01 10:17:40 -0600151 private class DecryptTask extends AsyncTask<String, Void, Integer> {
152 @Override
153 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700154 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600155 try {
156 return service.decryptStorage(params[0]);
157 } catch (Exception e) {
158 Log.e(TAG, "Error while decrypting...", e);
159 return -1;
160 }
161 }
162
163 @Override
164 protected void onPostExecute(Integer failedAttempts) {
165 if (failedAttempts == 0) {
166 // The password was entered successfully. Start the Blank activity
167 // so this activity animates to black before the devices starts. Note
168 // It has 1 second to complete the animation or it will be frozen
169 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700170 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600171 finish();
172 startActivity(intent);
173 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
174 // Factory reset the device.
175 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
176 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700177 if (mLockPatternView != null) {
178 mLockPatternView.clearPattern();
179 }
Jason parks06c5ff42011-03-01 10:17:40 -0600180 mCooldown = COOL_DOWN_INTERVAL;
181 cooldown();
182 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700183 final TextView status = (TextView) findViewById(R.id.status);
184 status.setText(R.string.try_again);
Jason parks06c5ff42011-03-01 10:17:40 -0600185 // Reenable the password entry
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800186 if (mPasswordEntry != null) {
187 mPasswordEntry.setEnabled(true);
188 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700189 if (mLockPatternView != null) {
190 mLockPatternView.setEnabled(true);
191 }
Jason parks06c5ff42011-03-01 10:17:40 -0600192 }
193 }
194 }
Jason parks75c085e2011-02-10 14:03:47 -0600195
Ben Komalo0e666092011-09-01 15:42:00 -0700196 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
197 @Override
198 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700199 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700200 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700201 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700202 int state = service.getEncryptionState();
203 if (state == IMountService.ENCRYPTION_STATE_NONE) {
204 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
205 return true; // Unexpected, but fine, I guess...
206 }
207 return state == IMountService.ENCRYPTION_STATE_OK;
208 } catch (RemoteException e) {
209 Log.w(TAG, "Unable to get encryption state properly");
210 return true;
211 }
212 }
213
214 @Override
215 protected void onPostExecute(Boolean result) {
216 mValidationComplete = true;
217 if (Boolean.FALSE.equals(result)) {
218 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
219 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700220 } else {
221 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700222 }
223 setupUi();
224 }
225 }
226
Ben Komalo91a2f052011-08-16 17:48:25 -0700227 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600228 @Override
229 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600230 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700231 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600232 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600233 break;
Jason parks35933812011-01-21 15:48:20 -0600234
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700235 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600236 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600237 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700238
239 case MESSAGE_NOTIFY:
240 notifyUser();
241 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600242 }
243 }
244 };
Jason parks35933812011-01-21 15:48:20 -0600245
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700246 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800247 /** The status bar where back/home/recent buttons are shown. */
248 private StatusBarManager mStatusBar;
249
250 /** All the widgets to disable in the status bar */
251 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
252 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
253 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
254 | StatusBarManager.DISABLE_SYSTEM_INFO
255 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700256 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800257 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700258
Ben Komalo91a2f052011-08-16 17:48:25 -0700259 /** @return whether or not this Activity was started for debugging the UI only. */
260 private boolean isDebugView() {
261 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
262 }
263
264 /** @return whether or not this Activity was started for debugging the specific UI view only. */
265 private boolean isDebugView(String viewType /* non-nullable */) {
266 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
267 }
268
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700269 /**
270 * Notify the user that we are awaiting input. Currently this sends an audio alert.
271 */
272 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700273 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700274 --mNotificationCountdown;
275 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700276 try {
277 // Play the standard keypress sound at full volume. This should be available on
278 // every device. We cannot play a ringtone here because media services aren't
279 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
280 // on tablet devices. The idea is to alert the user that something is needed: this
281 // does not have to be pleasing.
282 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
283 } catch (Exception e) {
284 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
285 }
286 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700287 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700288 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700289 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700290 }
291
292 /**
293 * Ignore back events after the user has entered the decrypt screen and while the device is
294 * encrypting.
295 */
296 @Override
297 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800298 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700299 if (mIgnoreBack)
300 return;
301 super.onBackPressed();
302 }
303
Jason parks8fd5bc92011-01-12 16:03:31 -0600304 @Override
305 public void onCreate(Bundle savedInstanceState) {
306 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600307
Andy Stadler95974062011-02-01 17:35:20 -0800308 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700309 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700310 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600311 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600312 PackageManager pm = getPackageManager();
313 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700314 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
315 PackageManager.DONT_KILL_APP);
316 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700317 // want to be running, so need to finish this activity. We can count
318 // on the activity manager re-launching the new home app upon finishing
319 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700320 // NOTE: This is really grungy. I think it would be better for the
321 // activity manager to explicitly launch the crypt keeper instead of
322 // home in the situation where we need to decrypt the device
323 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600324 return;
325 }
Jason parks35933812011-01-21 15:48:20 -0600326
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700327 // Disable the status bar, but do NOT disable back because the user needs a way to go
328 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800329 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
330 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800331
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700332 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700333 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800334 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700335 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800336 if (lastInstance instanceof NonConfigurationInstanceState) {
337 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
338 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700339 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800340 }
Jason parksec5a45e2011-01-18 15:28:36 -0600341 }
Jason parks35933812011-01-21 15:48:20 -0600342
Andy Stadler95974062011-02-01 17:35:20 -0800343 /**
344 * Note, we defer the state check and screen setup to onStart() because this will be
345 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
346 * especially important if we were to lose the wakelock for any reason.
347 */
348 @Override
349 public void onStart() {
350 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700351 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700352 }
353
354 /**
355 * Initializes the UI based on the current state of encryption.
356 * This is idempotent - calling repeatedly will simply re-initialize the UI.
357 */
358 private void setupUi() {
359 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
360 setContentView(R.layout.crypt_keeper_progress);
361 showFactoryReset();
362 return;
363 }
364
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700365 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700366 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800367 setContentView(R.layout.crypt_keeper_progress);
368 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700369 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800370 final IMountService service = getMountService();
371 int type = StorageManager.CRYPT_TYPE_PASSWORD;
372 try {
373 type = service.getPasswordType();
374 } catch (Exception e) {
375 Log.e(TAG, "Error while getting type - showing default dialog" + e);
376 }
377
378 if(type == StorageManager.CRYPT_TYPE_PIN) {
379 setContentView(R.layout.crypt_keeper_pin_entry);
380 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
381 setContentView(R.layout.crypt_keeper_pattern_entry);
Paul Lawrence2daf2642014-03-14 09:20:24 -0700382 setBackFunctionality(false);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800383 } else {
384 setContentView(R.layout.crypt_keeper_password_entry);
385 }
Andy Stadler95974062011-02-01 17:35:20 -0800386 passwordEntryInit();
Ben Komalod4758ef2011-09-08 14:36:08 -0700387 } else if (!mValidationRequested) {
388 // We're supposed to be encrypted, but no validation has been done.
389 new ValidationTask().execute((Void[]) null);
390 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800391 }
392 }
393
Jason parksf8217302011-01-26 13:11:42 -0600394 @Override
395 public void onStop() {
396 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700397 mHandler.removeMessages(MESSAGE_COOLDOWN);
398 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
399 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800400 }
401
402 /**
403 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
404 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
405 * mWakeLock so the subsequent call to onDestroy does not release it.
406 */
407 @Override
408 public Object onRetainNonConfigurationInstance() {
409 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700410 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800411 mWakeLock = null;
412 return state;
413 }
414
415 @Override
416 public void onDestroy() {
417 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600418
419 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700420 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600421 mWakeLock.release();
422 mWakeLock = null;
423 }
424 }
425
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700426 /**
427 * Start encrypting the device.
428 */
Jason parksec5a45e2011-01-18 15:28:36 -0600429 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600430 // Accquire a partial wakelock to prevent the device from sleeping. Note
431 // we never release this wakelock as we will be restarted after the device
432 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700433 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700434 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700435 Log.d(TAG, "Acquiring wakelock.");
436 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
437 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
438 mWakeLock.acquire();
439 }
Jason parks35933812011-01-21 15:48:20 -0600440
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700441 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700442 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800443 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700444 // Start the first run of progress manually. This method sets up messages to occur at
445 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600446 updateProgress();
447 }
448
Andy Stadler13d62042011-01-31 19:21:37 -0800449 private void showFactoryReset() {
450 // Hide the encryption-bot to make room for the "factory reset" button
451 findViewById(R.id.encroid).setVisibility(View.GONE);
452
453 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700454 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800455 button.setVisibility(View.VISIBLE);
456 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700457 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800458 public void onClick(View v) {
459 // Factory reset the device.
460 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
461 }
462 });
463
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700464 // Alert the user of the failure.
465 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
466 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800467
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700468 final View view = findViewById(R.id.bottom_divider);
469 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700470 if (view != null) {
471 view.setVisibility(View.VISIBLE);
472 }
Andy Stadler13d62042011-01-31 19:21:37 -0800473 }
474
Jason parksf8217302011-01-26 13:11:42 -0600475 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700476 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600477
Ben Komalo0e666092011-09-01 15:42:00 -0700478 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800479 showFactoryReset();
480 return;
481 }
482
Jason parksf8217302011-01-26 13:11:42 -0600483 int progress = 0;
484 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700485 // Force a 50% progress state when debugging the view.
486 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600487 } catch (Exception e) {
488 Log.w(TAG, "Error parsing progress: " + e.toString());
489 }
490
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700491 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700492 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700493 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700494 if (tv != null) {
495 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
496 }
Jason parksf8217302011-01-26 13:11:42 -0600497 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700498 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
499 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600500 }
501
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700502 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600503 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700504 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800505
Jason parksf8217302011-01-26 13:11:42 -0600506 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700507 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700508 if (mPasswordEntry != null) {
509 mPasswordEntry.setEnabled(true);
510 setBackFunctionality(true);
511 }
512 if (mLockPatternView != null) {
513 mLockPatternView.setEnabled(true);
514 }
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700515 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600516 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800517 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700518 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800519
Jason parksf8217302011-01-26 13:11:42 -0600520 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700521 mHandler.removeMessages(MESSAGE_COOLDOWN);
522 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600523 }
Jason parksec5a45e2011-01-18 15:28:36 -0600524 }
Jason parks35933812011-01-21 15:48:20 -0600525
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800526 /**
527 * Sets the back status: enabled or disabled according to the parameter.
528 * @param isEnabled true if back is enabled, false otherwise.
529 */
530 private final void setBackFunctionality(boolean isEnabled) {
531 mIgnoreBack = !isEnabled;
532 if (isEnabled) {
533 mStatusBar.disable(sWidgetsToDisable);
534 } else {
535 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
536 }
537 }
538
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800539 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
540 new LockPatternView.OnPatternListener() {
541
542 @Override
543 public void onPatternStart() {
544 }
545
546 @Override
547 public void onPatternCleared() {
548 }
549
550 @Override
551 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700552 mLockPatternView.setEnabled(false);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800553 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
554 }
555
556 @Override
557 public void onPatternCellAdded(List<Cell> pattern) {
558 }
559 };
560
561 private void passwordEntryInit() {
562 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600563 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800564 if (mPasswordEntry != null){
565 mPasswordEntry.setOnEditorActionListener(this);
566 mPasswordEntry.requestFocus();
567 // Become quiet when the user interacts with the Edit text screen.
568 mPasswordEntry.setOnKeyListener(this);
569 mPasswordEntry.setOnTouchListener(this);
570 mPasswordEntry.addTextChangedListener(this);
571 }
572
573 // Pattern case
574 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
575 if (mLockPatternView != null) {
576 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
577 }
Jason parks35933812011-01-21 15:48:20 -0600578
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700579 // Disable the Emergency call button if the device has no voice telephone capability
580 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
581 if (!tm.isVoiceCapable()) {
582 final View emergencyCall = findViewById(R.id.emergencyCallButton);
583 if (emergencyCall != null) {
584 Log.d(TAG, "Removing the emergency Call button");
585 emergencyCall.setVisibility(View.GONE);
586 }
587 }
588
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700589 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700590 final InputMethodManager imm = (InputMethodManager) getSystemService(
591 Context.INPUT_METHOD_SERVICE);
592 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
593 imeSwitcher.setVisibility(View.VISIBLE);
594 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700595 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700596 public void onClick(View v) {
597 imm.showInputMethodPicker();
598 }
599 });
Jason parks00046d62011-06-13 17:38:45 -0500600 }
David Brown8373b452011-06-20 12:38:45 -0700601
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700602 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
603 // is completely non-functional, and we want the user to notice the device and enter a
604 // password.
605 if (mWakeLock == null) {
606 Log.d(TAG, "Acquiring wakelock.");
607 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
608 if (pm != null) {
609 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
610 mWakeLock.acquire();
611 }
612 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700613 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
614 // immediately.
615 mHandler.postDelayed(new Runnable() {
616 @Override public void run() {
617 imm.showSoftInputUnchecked(0, null);
618 }
619 }, 0);
620
David Brown8373b452011-06-20 12:38:45 -0700621 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700622 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700623 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700624 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800625
626 // Dismiss keyguard while this screen is showing.
627 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600628 }
629
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700630 /**
631 * Method adapted from com.android.inputmethod.latin.Utils
632 *
633 * @param imm The input method manager
634 * @param shouldIncludeAuxiliarySubtypes
635 * @return true if we have multiple IMEs to choose from
636 */
637 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
638 final boolean shouldIncludeAuxiliarySubtypes) {
639 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
640
641 // Number of the filtered IMEs
642 int filteredImisCount = 0;
643
644 for (InputMethodInfo imi : enabledImis) {
645 // We can return true immediately after we find two or more filtered IMEs.
646 if (filteredImisCount > 1) return true;
647 final List<InputMethodSubtype> subtypes =
648 imm.getEnabledInputMethodSubtypeList(imi, true);
649 // IMEs that have no subtypes should be counted.
650 if (subtypes.isEmpty()) {
651 ++filteredImisCount;
652 continue;
653 }
654
655 int auxCount = 0;
656 for (InputMethodSubtype subtype : subtypes) {
657 if (subtype.isAuxiliary()) {
658 ++auxCount;
659 }
660 }
661 final int nonAuxCount = subtypes.size() - auxCount;
662
663 // IMEs that have one or more non-auxiliary subtypes should be counted.
664 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
665 // subtypes should be counted as well.
666 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
667 ++filteredImisCount;
668 continue;
669 }
670 }
671
672 return filteredImisCount > 1
673 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
674 // input method subtype (The current IME should be LatinIME.)
675 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
676 }
677
Jason parks8fd5bc92011-01-12 16:03:31 -0600678 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700679 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600680 if (service != null) {
681 return IMountService.Stub.asInterface(service);
682 }
683 return null;
684 }
685
686 @Override
687 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500688 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600689 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700690 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600691
Jason parksec5a45e2011-01-18 15:28:36 -0600692 if (TextUtils.isEmpty(password)) {
693 return true;
694 }
Jason parks35933812011-01-21 15:48:20 -0600695
Jason parks8fd5bc92011-01-12 16:03:31 -0600696 // Now that we have the password clear the password field.
697 v.setText(null);
698
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700699 // Disable the password entry and back keypress while checking the password. These
700 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600701 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800702 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600703
Ben Komalo04606752011-08-18 14:50:26 -0700704 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600705 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600706
Jason parks8fd5bc92011-01-12 16:03:31 -0600707 return true;
708 }
709 return false;
710 }
David Brown8373b452011-06-20 12:38:45 -0700711
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700712 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700713 * Set airplane mode on the device if it isn't an LTE device.
714 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
715 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
716 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
717 * both these problems, we turn the radio off. However, on certain networks turning on and
718 * off the radio takes a long time. In such cases, we are better off leaving the radio
719 * running so the latency of an E911 call is short.
720 * The behavior after this is:
721 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
722 * airplane mode and restart the radio.
723 * 2. Full boot: we read the persistent settings from the previous boot and restore the
724 * radio to whatever it was before it restarted. This also happens when rebooting a
725 * phone that has no encryption.
726 */
727 private final void setAirplaneModeIfNecessary() {
728 final boolean isLteDevice =
Wink Saville55434042012-06-14 12:33:43 -0700729 TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700730 if (!isLteDevice) {
731 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700732 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700733 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
734 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700735 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700736 }
737 }
738
739 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700740 * Code to update the state of, and handle clicks from, the "Emergency call" button.
741 *
742 * This code is mostly duplicated from the corresponding code in
743 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
744 */
David Brown8373b452011-06-20 12:38:45 -0700745 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700746 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700747 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700748 if (emergencyCall == null)
749 return;
David Brown8373b452011-06-20 12:38:45 -0700750
751 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700752 emergencyCall.setVisibility(View.VISIBLE);
753 emergencyCall.setOnClickListener(new View.OnClickListener() {
754 @Override
755
David Brown8373b452011-06-20 12:38:45 -0700756 public void onClick(View v) {
757 takeEmergencyCallAction();
758 }
759 });
760 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700761 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700762 return;
763 }
764
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700765 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700766 int textId;
767 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700768 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700769 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700770 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
771 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700772 } else {
773 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700774 final int emergencyIcon = R.drawable.ic_emergency;
775 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700776 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700777 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700778 }
779
780 private boolean isEmergencyCallCapable() {
781 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
782 }
783
784 private void takeEmergencyCallAction() {
785 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
786 resumeCall();
787 } else {
788 launchEmergencyDialer();
789 }
790 }
791
792 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700793 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700794 if (phone != null) {
795 try {
796 phone.showCallScreen();
797 } catch (RemoteException e) {
798 Log.e(TAG, "Error calling ITelephony service: " + e);
799 }
800 }
801 }
802
803 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700804 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700805 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
806 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
807 startActivity(intent);
808 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700809
810 /**
811 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
812 */
813 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700814 mNotificationCountdown = 20;
815 }
816
817 @Override
818 public boolean onKey(View v, int keyCode, KeyEvent event) {
819 delayAudioNotification();
820 return false;
821 }
822
823 @Override
824 public boolean onTouch(View v, MotionEvent event) {
825 delayAudioNotification();
826 return false;
827 }
828
829 @Override
830 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
831 return;
832 }
833
834 @Override
835 public void onTextChanged(CharSequence s, int start, int before, int count) {
836 delayAudioNotification();
837 }
838
839 @Override
840 public void afterTextChanged(Editable s) {
841 return;
842 }
David Brown8373b452011-06-20 12:38:45 -0700843}