blob: 1efe98f79b874922299ba46dad8c4b44b93546ea [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 Lawrence9ac2d812014-03-18 10:59:18 -0700370 new AsyncTask<Void, Void, Void>() {
371 int type = StorageManager.CRYPT_TYPE_PASSWORD;
372 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800373
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700374 @Override
375 public Void doInBackground(Void... v) {
376 try {
377 final IMountService service = getMountService();
378 type = service.getPasswordType();
379 owner_info = service.getField("OwnerInfo");
380 } catch (Exception e) {
381 Log.e(TAG, "Error calling mount service " + e);
382 }
383
384 return null;
385 }
386
387 @Override
388 public void onPostExecute(java.lang.Void v) {
389 if(type == StorageManager.CRYPT_TYPE_PIN) {
390 setContentView(R.layout.crypt_keeper_pin_entry);
391 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
392 setContentView(R.layout.crypt_keeper_pattern_entry);
393 setBackFunctionality(false);
394 } else {
395 setContentView(R.layout.crypt_keeper_password_entry);
396 }
397
398 final TextView status = (TextView) findViewById(R.id.owner_info);
399 status.setText(owner_info);
400 passwordEntryInit();
401 }
402 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700403 } else if (!mValidationRequested) {
404 // We're supposed to be encrypted, but no validation has been done.
405 new ValidationTask().execute((Void[]) null);
406 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800407 }
408 }
409
Jason parksf8217302011-01-26 13:11:42 -0600410 @Override
411 public void onStop() {
412 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700413 mHandler.removeMessages(MESSAGE_COOLDOWN);
414 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
415 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800416 }
417
418 /**
419 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
420 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
421 * mWakeLock so the subsequent call to onDestroy does not release it.
422 */
423 @Override
424 public Object onRetainNonConfigurationInstance() {
425 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700426 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800427 mWakeLock = null;
428 return state;
429 }
430
431 @Override
432 public void onDestroy() {
433 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600434
435 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700436 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600437 mWakeLock.release();
438 mWakeLock = null;
439 }
440 }
441
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700442 /**
443 * Start encrypting the device.
444 */
Jason parksec5a45e2011-01-18 15:28:36 -0600445 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600446 // Accquire a partial wakelock to prevent the device from sleeping. Note
447 // we never release this wakelock as we will be restarted after the device
448 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700449 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700450 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700451 Log.d(TAG, "Acquiring wakelock.");
452 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
453 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
454 mWakeLock.acquire();
455 }
Jason parks35933812011-01-21 15:48:20 -0600456
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700457 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700458 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800459 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700460 // Start the first run of progress manually. This method sets up messages to occur at
461 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600462 updateProgress();
463 }
464
Andy Stadler13d62042011-01-31 19:21:37 -0800465 private void showFactoryReset() {
466 // Hide the encryption-bot to make room for the "factory reset" button
467 findViewById(R.id.encroid).setVisibility(View.GONE);
468
469 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700470 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800471 button.setVisibility(View.VISIBLE);
472 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700473 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800474 public void onClick(View v) {
475 // Factory reset the device.
476 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
477 }
478 });
479
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700480 // Alert the user of the failure.
481 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
482 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800483
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700484 final View view = findViewById(R.id.bottom_divider);
485 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700486 if (view != null) {
487 view.setVisibility(View.VISIBLE);
488 }
Andy Stadler13d62042011-01-31 19:21:37 -0800489 }
490
Jason parksf8217302011-01-26 13:11:42 -0600491 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700492 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600493
Ben Komalo0e666092011-09-01 15:42:00 -0700494 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800495 showFactoryReset();
496 return;
497 }
498
Jason parksf8217302011-01-26 13:11:42 -0600499 int progress = 0;
500 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700501 // Force a 50% progress state when debugging the view.
502 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600503 } catch (Exception e) {
504 Log.w(TAG, "Error parsing progress: " + e.toString());
505 }
506
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700507 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700508 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700509 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700510 if (tv != null) {
511 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
512 }
Jason parksf8217302011-01-26 13:11:42 -0600513 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700514 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
515 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600516 }
517
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700518 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600519 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700520 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800521
Jason parksf8217302011-01-26 13:11:42 -0600522 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700523 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700524 if (mPasswordEntry != null) {
525 mPasswordEntry.setEnabled(true);
526 setBackFunctionality(true);
527 }
528 if (mLockPatternView != null) {
529 mLockPatternView.setEnabled(true);
530 }
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700531 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600532 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800533 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700534 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800535
Jason parksf8217302011-01-26 13:11:42 -0600536 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700537 mHandler.removeMessages(MESSAGE_COOLDOWN);
538 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600539 }
Jason parksec5a45e2011-01-18 15:28:36 -0600540 }
Jason parks35933812011-01-21 15:48:20 -0600541
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800542 /**
543 * Sets the back status: enabled or disabled according to the parameter.
544 * @param isEnabled true if back is enabled, false otherwise.
545 */
546 private final void setBackFunctionality(boolean isEnabled) {
547 mIgnoreBack = !isEnabled;
548 if (isEnabled) {
549 mStatusBar.disable(sWidgetsToDisable);
550 } else {
551 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
552 }
553 }
554
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800555 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
556 new LockPatternView.OnPatternListener() {
557
558 @Override
559 public void onPatternStart() {
560 }
561
562 @Override
563 public void onPatternCleared() {
564 }
565
566 @Override
567 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700568 mLockPatternView.setEnabled(false);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800569 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
570 }
571
572 @Override
573 public void onPatternCellAdded(List<Cell> pattern) {
574 }
575 };
576
577 private void passwordEntryInit() {
578 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600579 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800580 if (mPasswordEntry != null){
581 mPasswordEntry.setOnEditorActionListener(this);
582 mPasswordEntry.requestFocus();
583 // Become quiet when the user interacts with the Edit text screen.
584 mPasswordEntry.setOnKeyListener(this);
585 mPasswordEntry.setOnTouchListener(this);
586 mPasswordEntry.addTextChangedListener(this);
587 }
588
589 // Pattern case
590 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
591 if (mLockPatternView != null) {
592 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
593 }
Jason parks35933812011-01-21 15:48:20 -0600594
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700595 // Disable the Emergency call button if the device has no voice telephone capability
596 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
597 if (!tm.isVoiceCapable()) {
598 final View emergencyCall = findViewById(R.id.emergencyCallButton);
599 if (emergencyCall != null) {
600 Log.d(TAG, "Removing the emergency Call button");
601 emergencyCall.setVisibility(View.GONE);
602 }
603 }
604
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700605 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700606 final InputMethodManager imm = (InputMethodManager) getSystemService(
607 Context.INPUT_METHOD_SERVICE);
608 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
609 imeSwitcher.setVisibility(View.VISIBLE);
610 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700611 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700612 public void onClick(View v) {
613 imm.showInputMethodPicker();
614 }
615 });
Jason parks00046d62011-06-13 17:38:45 -0500616 }
David Brown8373b452011-06-20 12:38:45 -0700617
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700618 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
619 // is completely non-functional, and we want the user to notice the device and enter a
620 // password.
621 if (mWakeLock == null) {
622 Log.d(TAG, "Acquiring wakelock.");
623 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
624 if (pm != null) {
625 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
626 mWakeLock.acquire();
627 }
628 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700629 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
630 // immediately.
Paul Lawrencee54e9322014-03-13 14:05:48 -0700631 if (mLockPatternView == null) {
632 mHandler.postDelayed(new Runnable() {
633 @Override public void run() {
634 imm.showSoftInputUnchecked(0, null);
635 }
636 }, 0);
637 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700638
David Brown8373b452011-06-20 12:38:45 -0700639 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700640 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700641 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700642 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800643
644 // Dismiss keyguard while this screen is showing.
645 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600646 }
647
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700648 /**
649 * Method adapted from com.android.inputmethod.latin.Utils
650 *
651 * @param imm The input method manager
652 * @param shouldIncludeAuxiliarySubtypes
653 * @return true if we have multiple IMEs to choose from
654 */
655 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
656 final boolean shouldIncludeAuxiliarySubtypes) {
657 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
658
659 // Number of the filtered IMEs
660 int filteredImisCount = 0;
661
662 for (InputMethodInfo imi : enabledImis) {
663 // We can return true immediately after we find two or more filtered IMEs.
664 if (filteredImisCount > 1) return true;
665 final List<InputMethodSubtype> subtypes =
666 imm.getEnabledInputMethodSubtypeList(imi, true);
667 // IMEs that have no subtypes should be counted.
668 if (subtypes.isEmpty()) {
669 ++filteredImisCount;
670 continue;
671 }
672
673 int auxCount = 0;
674 for (InputMethodSubtype subtype : subtypes) {
675 if (subtype.isAuxiliary()) {
676 ++auxCount;
677 }
678 }
679 final int nonAuxCount = subtypes.size() - auxCount;
680
681 // IMEs that have one or more non-auxiliary subtypes should be counted.
682 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
683 // subtypes should be counted as well.
684 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
685 ++filteredImisCount;
686 continue;
687 }
688 }
689
690 return filteredImisCount > 1
691 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
692 // input method subtype (The current IME should be LatinIME.)
693 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
694 }
695
Jason parks8fd5bc92011-01-12 16:03:31 -0600696 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700697 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600698 if (service != null) {
699 return IMountService.Stub.asInterface(service);
700 }
701 return null;
702 }
703
704 @Override
705 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500706 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600707 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700708 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600709
Jason parksec5a45e2011-01-18 15:28:36 -0600710 if (TextUtils.isEmpty(password)) {
711 return true;
712 }
Jason parks35933812011-01-21 15:48:20 -0600713
Jason parks8fd5bc92011-01-12 16:03:31 -0600714 // Now that we have the password clear the password field.
715 v.setText(null);
716
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700717 // Disable the password entry and back keypress while checking the password. These
718 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600719 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800720 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600721
Ben Komalo04606752011-08-18 14:50:26 -0700722 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600723 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600724
Jason parks8fd5bc92011-01-12 16:03:31 -0600725 return true;
726 }
727 return false;
728 }
David Brown8373b452011-06-20 12:38:45 -0700729
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700730 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700731 * Set airplane mode on the device if it isn't an LTE device.
732 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
733 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
734 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
735 * both these problems, we turn the radio off. However, on certain networks turning on and
736 * off the radio takes a long time. In such cases, we are better off leaving the radio
737 * running so the latency of an E911 call is short.
738 * The behavior after this is:
739 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
740 * airplane mode and restart the radio.
741 * 2. Full boot: we read the persistent settings from the previous boot and restore the
742 * radio to whatever it was before it restarted. This also happens when rebooting a
743 * phone that has no encryption.
744 */
745 private final void setAirplaneModeIfNecessary() {
746 final boolean isLteDevice =
Wink Saville55434042012-06-14 12:33:43 -0700747 TelephonyManager.getDefault().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700748 if (!isLteDevice) {
749 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700750 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700751 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
752 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700753 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700754 }
755 }
756
757 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700758 * Code to update the state of, and handle clicks from, the "Emergency call" button.
759 *
760 * This code is mostly duplicated from the corresponding code in
761 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
762 */
David Brown8373b452011-06-20 12:38:45 -0700763 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700764 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700765 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700766 if (emergencyCall == null)
767 return;
David Brown8373b452011-06-20 12:38:45 -0700768
769 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700770 emergencyCall.setVisibility(View.VISIBLE);
771 emergencyCall.setOnClickListener(new View.OnClickListener() {
772 @Override
773
David Brown8373b452011-06-20 12:38:45 -0700774 public void onClick(View v) {
775 takeEmergencyCallAction();
776 }
777 });
778 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700779 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700780 return;
781 }
782
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700783 final int newState = TelephonyManager.getDefault().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700784 int textId;
785 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700786 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700787 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700788 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
789 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700790 } else {
791 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700792 final int emergencyIcon = R.drawable.ic_emergency;
793 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700794 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700795 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700796 }
797
798 private boolean isEmergencyCallCapable() {
799 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
800 }
801
802 private void takeEmergencyCallAction() {
803 if (TelephonyManager.getDefault().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
804 resumeCall();
805 } else {
806 launchEmergencyDialer();
807 }
808 }
809
810 private void resumeCall() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700811 final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
David Brown8373b452011-06-20 12:38:45 -0700812 if (phone != null) {
813 try {
814 phone.showCallScreen();
815 } catch (RemoteException e) {
816 Log.e(TAG, "Error calling ITelephony service: " + e);
817 }
818 }
819 }
820
821 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700822 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700823 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
824 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
825 startActivity(intent);
826 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700827
828 /**
829 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
830 */
831 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700832 mNotificationCountdown = 20;
833 }
834
835 @Override
836 public boolean onKey(View v, int keyCode, KeyEvent event) {
837 delayAudioNotification();
838 return false;
839 }
840
841 @Override
842 public boolean onTouch(View v, MotionEvent event) {
843 delayAudioNotification();
844 return false;
845 }
846
847 @Override
848 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
849 return;
850 }
851
852 @Override
853 public void onTextChanged(CharSequence s, int start, int before, int count) {
854 delayAudioNotification();
855 }
856
857 @Override
858 public void afterTextChanged(Editable s) {
859 return;
860 }
David Brown8373b452011-06-20 12:38:45 -0700861}