blob: 322bda057a435a971e458e9677fd95f05ec16ac5 [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;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -070061import com.android.internal.telephony.Phone;
Wink Saville55434042012-06-14 12:33:43 -070062import com.android.internal.telephony.PhoneConstants;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -080063import com.android.internal.widget.LockPatternUtils;
64import com.android.internal.widget.LockPatternView;
65import com.android.internal.widget.LockPatternView.Cell;
Ben Komalo9fcb6a72011-08-26 14:40:18 -070066
67import java.util.List;
Ben Komalo91a2f052011-08-16 17:48:25 -070068
69/**
70 * Settings screens to show the UI flows for encrypting/decrypting the device.
71 *
72 * This may be started via adb for debugging the UI layout, without having to go through
73 * encryption flows everytime. It should be noted that starting the activity in this manner
74 * is only useful for verifying UI-correctness - the behavior will not be identical.
75 * <pre>
76 * $ adb shell pm enable com.android.settings/.CryptKeeper
77 * $ adb shell am start \
78 * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \
79 * -n com.android.settings/.CryptKeeper
80 * </pre>
81 */
Vikram Aggarwald1147252012-05-08 13:52:30 -070082public class CryptKeeper extends Activity implements TextView.OnEditorActionListener,
83 OnKeyListener, OnTouchListener, TextWatcher {
Jason parksec5a45e2011-01-18 15:28:36 -060084 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060085
Jason parks8fd5bc92011-01-12 16:03:31 -060086 private static final String DECRYPT_STATE = "trigger_restart_framework";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070087 /** Message sent to us to indicate encryption update progress. */
88 private static final int MESSAGE_UPDATE_PROGRESS = 1;
89 /** Message sent to us to cool-down (waste user's time between password attempts) */
90 private static final int MESSAGE_COOLDOWN = 2;
91 /** Message sent to us to indicate alerting the user that we are waiting for password entry */
92 private static final int MESSAGE_NOTIFY = 3;
Jason parksec5a45e2011-01-18 15:28:36 -060093
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -070094 // Constants used to control policy.
Jason parksec5a45e2011-01-18 15:28:36 -060095 private static final int MAX_FAILED_ATTEMPTS = 30;
96 private static final int COOL_DOWN_ATTEMPTS = 10;
97 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
98
David Brown8373b452011-06-20 12:38:45 -070099 // Intent action for launching the Emergency Dialer activity.
100 static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
101
Ben Komalo91a2f052011-08-16 17:48:25 -0700102 // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts
103 private static final String EXTRA_FORCE_VIEW =
104 "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW";
105 private static final String FORCE_VIEW_PROGRESS = "progress";
Ben Komalo91a2f052011-08-16 17:48:25 -0700106 private static final String FORCE_VIEW_ERROR = "error";
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700107 private static final String FORCE_VIEW_PASSWORD = "password";
Ben Komalo91a2f052011-08-16 17:48:25 -0700108
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700109 /** When encryption is detected, this flag indicates whether or not we've checked for errors. */
Ben Komalo0e666092011-09-01 15:42:00 -0700110 private boolean mValidationComplete;
Ben Komalod4758ef2011-09-08 14:36:08 -0700111 private boolean mValidationRequested;
Ben Komalo0e666092011-09-01 15:42:00 -0700112 /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */
113 private boolean mEncryptionGoneBad;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700114 /** A flag to indicate when the back event should be ignored */
115 private boolean mIgnoreBack = false;
Andy Stadler14997402011-02-01 15:34:59 -0800116 private int mCooldown;
117 PowerManager.WakeLock mWakeLock;
Jason parks06c5ff42011-03-01 10:17:40 -0600118 private EditText mPasswordEntry;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800119 private LockPatternView mLockPatternView;
Vikram Aggarwald1147252012-05-08 13:52:30 -0700120 /** Number of calls to {@link #notifyUser()} to ignore before notifying. */
121 private int mNotificationCountdown = 0;
Paul Lawrence73456ac2014-05-16 07:56:04 -0700122 /** Number of calls to {@link #notifyUser()} before we release the wakelock */
123 private int mReleaseWakeLockCountdown = 0;
Andy Stadler14997402011-02-01 15:34:59 -0800124
125 /**
126 * Used to propagate state through configuration changes (e.g. screen rotation)
127 */
128 private static class NonConfigurationInstanceState {
129 final PowerManager.WakeLock wakelock;
130
131 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
132 wakelock = _wakelock;
133 }
134 }
135
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700136 /**
137 * Activity used to fade the screen to black after the password is entered.
138 */
Vikram Aggarwal9f55ae22012-04-02 13:36:35 -0700139 public static class FadeToBlack extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -0800140 @Override
141 public void onCreate(Bundle savedInstanceState) {
142 super.onCreate(savedInstanceState);
143 setContentView(R.layout.crypt_keeper_blank);
144 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700145 /** Ignore all back events. */
146 @Override
147 public void onBackPressed() {
148 return;
149 }
Jason parksf1dbf552011-01-24 16:19:28 -0600150 }
Jason parksec5a45e2011-01-18 15:28:36 -0600151
Jason parks06c5ff42011-03-01 10:17:40 -0600152 private class DecryptTask extends AsyncTask<String, Void, Integer> {
153 @Override
154 protected Integer doInBackground(String... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700155 final IMountService service = getMountService();
Jason parks06c5ff42011-03-01 10:17:40 -0600156 try {
157 return service.decryptStorage(params[0]);
158 } catch (Exception e) {
159 Log.e(TAG, "Error while decrypting...", e);
160 return -1;
161 }
162 }
163
164 @Override
165 protected void onPostExecute(Integer failedAttempts) {
166 if (failedAttempts == 0) {
167 // The password was entered successfully. Start the Blank activity
168 // so this activity animates to black before the devices starts. Note
169 // It has 1 second to complete the animation or it will be frozen
170 // until the boot animation comes back up.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700171 Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class);
Jason parks06c5ff42011-03-01 10:17:40 -0600172 finish();
173 startActivity(intent);
174 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
175 // Factory reset the device.
176 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
177 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700178 if (mLockPatternView != null) {
179 mLockPatternView.clearPattern();
180 }
Jason parks06c5ff42011-03-01 10:17:40 -0600181 mCooldown = COOL_DOWN_INTERVAL;
182 cooldown();
183 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700184 final TextView status = (TextView) findViewById(R.id.status);
185 status.setText(R.string.try_again);
Jason parks06c5ff42011-03-01 10:17:40 -0600186 // Reenable the password entry
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800187 if (mPasswordEntry != null) {
188 mPasswordEntry.setEnabled(true);
189 }
Paul Lawrence2daf2642014-03-14 09:20:24 -0700190 if (mLockPatternView != null) {
191 mLockPatternView.setEnabled(true);
192 }
Jason parks06c5ff42011-03-01 10:17:40 -0600193 }
194 }
195 }
Jason parks75c085e2011-02-10 14:03:47 -0600196
Ben Komalo0e666092011-09-01 15:42:00 -0700197 private class ValidationTask extends AsyncTask<Void, Void, Boolean> {
198 @Override
199 protected Boolean doInBackground(Void... params) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700200 final IMountService service = getMountService();
Ben Komalo0e666092011-09-01 15:42:00 -0700201 try {
Ben Komalod4758ef2011-09-08 14:36:08 -0700202 Log.d(TAG, "Validating encryption state.");
Ben Komalo0e666092011-09-01 15:42:00 -0700203 int state = service.getEncryptionState();
204 if (state == IMountService.ENCRYPTION_STATE_NONE) {
205 Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption.");
206 return true; // Unexpected, but fine, I guess...
207 }
208 return state == IMountService.ENCRYPTION_STATE_OK;
209 } catch (RemoteException e) {
210 Log.w(TAG, "Unable to get encryption state properly");
211 return true;
212 }
213 }
214
215 @Override
216 protected void onPostExecute(Boolean result) {
217 mValidationComplete = true;
218 if (Boolean.FALSE.equals(result)) {
219 Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe.");
220 mEncryptionGoneBad = true;
Ben Komalod4758ef2011-09-08 14:36:08 -0700221 } else {
222 Log.d(TAG, "Encryption state validated. Proceeding to configure UI");
Ben Komalo0e666092011-09-01 15:42:00 -0700223 }
224 setupUi();
225 }
226 }
227
Ben Komalo91a2f052011-08-16 17:48:25 -0700228 private final Handler mHandler = new Handler() {
Jason parksec5a45e2011-01-18 15:28:36 -0600229 @Override
230 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -0600231 switch (msg.what) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700232 case MESSAGE_UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -0600233 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -0600234 break;
Jason parks35933812011-01-21 15:48:20 -0600235
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700236 case MESSAGE_COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -0600237 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600238 break;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700239
240 case MESSAGE_NOTIFY:
241 notifyUser();
242 break;
Jason parksec5a45e2011-01-18 15:28:36 -0600243 }
244 }
245 };
Jason parks35933812011-01-21 15:48:20 -0600246
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700247 private AudioManager mAudioManager;
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800248 /** The status bar where back/home/recent buttons are shown. */
249 private StatusBarManager mStatusBar;
250
251 /** All the widgets to disable in the status bar */
252 final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND
253 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
254 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
255 | StatusBarManager.DISABLE_SYSTEM_INFO
256 | StatusBarManager.DISABLE_HOME
Alon Albert66d050b2013-09-30 17:08:12 -0700257 | StatusBarManager.DISABLE_SEARCH
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800258 | StatusBarManager.DISABLE_RECENT;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700259
Ben Komalo91a2f052011-08-16 17:48:25 -0700260 /** @return whether or not this Activity was started for debugging the UI only. */
261 private boolean isDebugView() {
262 return getIntent().hasExtra(EXTRA_FORCE_VIEW);
263 }
264
265 /** @return whether or not this Activity was started for debugging the specific UI view only. */
266 private boolean isDebugView(String viewType /* non-nullable */) {
267 return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW));
268 }
269
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700270 /**
271 * Notify the user that we are awaiting input. Currently this sends an audio alert.
272 */
273 private void notifyUser() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700274 if (mNotificationCountdown > 0) {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700275 --mNotificationCountdown;
276 } else if (mAudioManager != null) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700277 try {
278 // Play the standard keypress sound at full volume. This should be available on
279 // every device. We cannot play a ringtone here because media services aren't
280 // available yet. A DTMF-style tone is too soft to be noticed, and might not exist
281 // on tablet devices. The idea is to alert the user that something is needed: this
282 // does not have to be pleasing.
283 mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100);
284 } catch (Exception e) {
285 Log.w(TAG, "notifyUser: Exception while playing sound: " + e);
286 }
287 }
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700288 // Notify the user again in 5 seconds.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700289 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700290 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000);
Paul Lawrence73456ac2014-05-16 07:56:04 -0700291
292 if (mWakeLock.isHeld()) {
293 if (mReleaseWakeLockCountdown > 0) {
294 --mReleaseWakeLockCountdown;
295 } else {
296 mWakeLock.release();
297 }
298 }
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700299 }
300
301 /**
302 * Ignore back events after the user has entered the decrypt screen and while the device is
303 * encrypting.
304 */
305 @Override
306 public void onBackPressed() {
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800307 // In the rare case that something pressed back even though we were disabled.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700308 if (mIgnoreBack)
309 return;
310 super.onBackPressed();
311 }
312
Jason parks8fd5bc92011-01-12 16:03:31 -0600313 @Override
314 public void onCreate(Bundle savedInstanceState) {
315 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600316
Andy Stadler95974062011-02-01 17:35:20 -0800317 // If we are not encrypted or encrypting, get out quickly.
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700318 final String state = SystemProperties.get("vold.decrypt");
Ben Komalo91a2f052011-08-16 17:48:25 -0700319 if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
Jason parks35933812011-01-21 15:48:20 -0600320 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600321 PackageManager pm = getPackageManager();
322 ComponentName name = new ComponentName(this, CryptKeeper.class);
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700323 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
324 PackageManager.DONT_KILL_APP);
325 // Typically CryptKeeper is launched as the home app. We didn't
Dianne Hackborn644fa422011-10-18 13:38:03 -0700326 // want to be running, so need to finish this activity. We can count
327 // on the activity manager re-launching the new home app upon finishing
328 // this one, since this will leave the activity stack empty.
Dianne Hackborn140f6c62011-10-16 11:49:00 -0700329 // NOTE: This is really grungy. I think it would be better for the
330 // activity manager to explicitly launch the crypt keeper instead of
331 // home in the situation where we need to decrypt the device
332 finish();
Jason parks8fd5bc92011-01-12 16:03:31 -0600333 return;
334 }
Jason parks35933812011-01-21 15:48:20 -0600335
Vikram Aggarwalb96b35a2012-05-01 11:09:39 -0700336 // Disable the status bar, but do NOT disable back because the user needs a way to go
337 // from keyboard settings and back to the password screen.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800338 mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
339 mStatusBar.disable(sWidgetsToDisable);
Andy Stadler14997402011-02-01 15:34:59 -0800340
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700341 setAirplaneModeIfNecessary();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700342 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andy Stadler14997402011-02-01 15:34:59 -0800343 // Check for (and recover) retained instance data
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700344 final Object lastInstance = getLastNonConfigurationInstance();
Andy Stadler14997402011-02-01 15:34:59 -0800345 if (lastInstance instanceof NonConfigurationInstanceState) {
346 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
347 mWakeLock = retained.wakelock;
Ben Komalo04606752011-08-18 14:50:26 -0700348 Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800349 }
Jason parksec5a45e2011-01-18 15:28:36 -0600350 }
Jason parks35933812011-01-21 15:48:20 -0600351
Andy Stadler95974062011-02-01 17:35:20 -0800352 /**
353 * Note, we defer the state check and screen setup to onStart() because this will be
354 * re-run if the user clicks the power button (sleeping/waking the screen), and this is
355 * especially important if we were to lose the wakelock for any reason.
356 */
357 @Override
358 public void onStart() {
359 super.onStart();
Ben Komalod4758ef2011-09-08 14:36:08 -0700360 setupUi();
Ben Komalo0e666092011-09-01 15:42:00 -0700361 }
362
363 /**
364 * Initializes the UI based on the current state of encryption.
365 * This is idempotent - calling repeatedly will simply re-initialize the UI.
366 */
367 private void setupUi() {
368 if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) {
369 setContentView(R.layout.crypt_keeper_progress);
370 showFactoryReset();
371 return;
372 }
373
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700374 final String progress = SystemProperties.get("vold.encrypt_progress");
Ben Komalo0e666092011-09-01 15:42:00 -0700375 if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) {
Andy Stadler95974062011-02-01 17:35:20 -0800376 setContentView(R.layout.crypt_keeper_progress);
377 encryptionProgressInit();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700378 } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) {
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700379 new AsyncTask<Void, Void, Void>() {
380 int type = StorageManager.CRYPT_TYPE_PASSWORD;
381 String owner_info;
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800382
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700383 @Override
384 public Void doInBackground(Void... v) {
385 try {
386 final IMountService service = getMountService();
387 type = service.getPasswordType();
388 owner_info = service.getField("OwnerInfo");
389 } catch (Exception e) {
390 Log.e(TAG, "Error calling mount service " + e);
391 }
392
393 return null;
394 }
395
396 @Override
397 public void onPostExecute(java.lang.Void v) {
398 if(type == StorageManager.CRYPT_TYPE_PIN) {
399 setContentView(R.layout.crypt_keeper_pin_entry);
Paul Lawrence04425f12014-05-15 09:37:56 -0700400 ((TextView)findViewById(R.id.status)).setText(R.string.enter_pin);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700401 } else if (type == StorageManager.CRYPT_TYPE_PATTERN) {
402 setContentView(R.layout.crypt_keeper_pattern_entry);
403 setBackFunctionality(false);
Paul Lawrence04425f12014-05-15 09:37:56 -0700404 ((TextView)findViewById(R.id.status)).setText(R.string.enter_pattern);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700405 } else {
406 setContentView(R.layout.crypt_keeper_password_entry);
407 }
408
409 final TextView status = (TextView) findViewById(R.id.owner_info);
410 status.setText(owner_info);
Paul Lawrence7836ae82014-05-29 10:19:08 -0700411 status.setSelected(true);
Paul Lawrence9ac2d812014-03-18 10:59:18 -0700412 passwordEntryInit();
413 }
414 }.execute();
Ben Komalod4758ef2011-09-08 14:36:08 -0700415 } else if (!mValidationRequested) {
416 // We're supposed to be encrypted, but no validation has been done.
417 new ValidationTask().execute((Void[]) null);
418 mValidationRequested = true;
Andy Stadler95974062011-02-01 17:35:20 -0800419 }
420 }
421
Jason parksf8217302011-01-26 13:11:42 -0600422 @Override
423 public void onStop() {
424 super.onStop();
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700425 mHandler.removeMessages(MESSAGE_COOLDOWN);
426 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
427 mHandler.removeMessages(MESSAGE_NOTIFY);
Andy Stadler14997402011-02-01 15:34:59 -0800428 }
429
430 /**
431 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
432 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
433 * mWakeLock so the subsequent call to onDestroy does not release it.
434 */
435 @Override
436 public Object onRetainNonConfigurationInstance() {
437 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
Ben Komalo04606752011-08-18 14:50:26 -0700438 Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState");
Andy Stadler14997402011-02-01 15:34:59 -0800439 mWakeLock = null;
440 return state;
441 }
442
443 @Override
444 public void onDestroy() {
445 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600446
447 if (mWakeLock != null) {
Ben Komalo04606752011-08-18 14:50:26 -0700448 Log.d(TAG, "Releasing and destroying wakelock");
Jason parksf8217302011-01-26 13:11:42 -0600449 mWakeLock.release();
450 mWakeLock = null;
451 }
452 }
453
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700454 /**
455 * Start encrypting the device.
456 */
Jason parksec5a45e2011-01-18 15:28:36 -0600457 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600458 // Accquire a partial wakelock to prevent the device from sleeping. Note
459 // we never release this wakelock as we will be restarted after the device
460 // is encrypted.
Ben Komalo04606752011-08-18 14:50:26 -0700461 Log.d(TAG, "Encryption progress screen initializing.");
Ben Komalo9ee164f2011-09-21 10:40:50 -0700462 if (mWakeLock == null) {
Ben Komalo04606752011-08-18 14:50:26 -0700463 Log.d(TAG, "Acquiring wakelock.");
464 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
465 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
466 mWakeLock.acquire();
467 }
Jason parks35933812011-01-21 15:48:20 -0600468
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700469 ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700470 // Ignore all back presses from now, both hard and soft keys.
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800471 setBackFunctionality(false);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700472 // Start the first run of progress manually. This method sets up messages to occur at
473 // repeated intervals.
Jason parksf8217302011-01-26 13:11:42 -0600474 updateProgress();
475 }
476
Andy Stadler13d62042011-01-31 19:21:37 -0800477 private void showFactoryReset() {
478 // Hide the encryption-bot to make room for the "factory reset" button
479 findViewById(R.id.encroid).setVisibility(View.GONE);
480
481 // Show the reset button, failure text, and a divider
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700482 final Button button = (Button) findViewById(R.id.factory_reset);
Andy Stadler13d62042011-01-31 19:21:37 -0800483 button.setVisibility(View.VISIBLE);
484 button.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700485 @Override
Andy Stadler13d62042011-01-31 19:21:37 -0800486 public void onClick(View v) {
487 // Factory reset the device.
488 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
489 }
490 });
491
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700492 // Alert the user of the failure.
493 ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title);
494 ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary);
Andy Stadler13d62042011-01-31 19:21:37 -0800495
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700496 final View view = findViewById(R.id.bottom_divider);
497 // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate.
Ben Komalof0104df2011-08-16 17:48:42 -0700498 if (view != null) {
499 view.setVisibility(View.VISIBLE);
500 }
Andy Stadler13d62042011-01-31 19:21:37 -0800501 }
502
Jason parksf8217302011-01-26 13:11:42 -0600503 private void updateProgress() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700504 final String state = SystemProperties.get("vold.encrypt_progress");
Jason parksf8217302011-01-26 13:11:42 -0600505
Ben Komalo0e666092011-09-01 15:42:00 -0700506 if ("error_partially_encrypted".equals(state)) {
Andy Stadler13d62042011-01-31 19:21:37 -0800507 showFactoryReset();
508 return;
509 }
510
Jason parksf8217302011-01-26 13:11:42 -0600511 int progress = 0;
512 try {
Ben Komalo91a2f052011-08-16 17:48:25 -0700513 // Force a 50% progress state when debugging the view.
514 progress = isDebugView() ? 50 : Integer.parseInt(state);
Jason parksf8217302011-01-26 13:11:42 -0600515 } catch (Exception e) {
516 Log.w(TAG, "Error parsing progress: " + e.toString());
517 }
518
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700519 final CharSequence status = getText(R.string.crypt_keeper_setup_description);
Ben Komalo04606752011-08-18 14:50:26 -0700520 Log.v(TAG, "Encryption progress: " + progress);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700521 final TextView tv = (TextView) findViewById(R.id.status);
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700522 if (tv != null) {
523 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
524 }
Jason parksf8217302011-01-26 13:11:42 -0600525 // Check the progress every 5 seconds
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700526 mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS);
527 mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 5000);
Jason parksf8217302011-01-26 13:11:42 -0600528 }
529
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700530 /** Disable password input for a while to force the user to waste time between retries */
Jason parksf8217302011-01-26 13:11:42 -0600531 private void cooldown() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700532 final TextView status = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800533
Jason parksf8217302011-01-26 13:11:42 -0600534 if (mCooldown <= 0) {
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700535 // Re-enable the password entry and back presses.
Paul Lawrence2daf2642014-03-14 09:20:24 -0700536 if (mPasswordEntry != null) {
537 mPasswordEntry.setEnabled(true);
538 setBackFunctionality(true);
539 }
540 if (mLockPatternView != null) {
541 mLockPatternView.setEnabled(true);
542 }
Vikram Aggarwalf576dd62012-05-22 10:58:10 -0700543 status.setText(R.string.enter_password);
Jason parksf8217302011-01-26 13:11:42 -0600544 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800545 CharSequence template = getText(R.string.crypt_keeper_cooldown);
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700546 status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
Andy Stadler13d62042011-01-31 19:21:37 -0800547
Jason parksf8217302011-01-26 13:11:42 -0600548 mCooldown--;
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700549 mHandler.removeMessages(MESSAGE_COOLDOWN);
550 mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second
Jason parksf8217302011-01-26 13:11:42 -0600551 }
Jason parksec5a45e2011-01-18 15:28:36 -0600552 }
Jason parks35933812011-01-21 15:48:20 -0600553
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800554 /**
555 * Sets the back status: enabled or disabled according to the parameter.
556 * @param isEnabled true if back is enabled, false otherwise.
557 */
558 private final void setBackFunctionality(boolean isEnabled) {
559 mIgnoreBack = !isEnabled;
560 if (isEnabled) {
561 mStatusBar.disable(sWidgetsToDisable);
562 } else {
563 mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK);
564 }
565 }
566
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800567 protected LockPatternView.OnPatternListener mChooseNewLockPatternListener =
568 new LockPatternView.OnPatternListener() {
569
570 @Override
571 public void onPatternStart() {
572 }
573
574 @Override
575 public void onPatternCleared() {
576 }
577
578 @Override
579 public void onPatternDetected(List<LockPatternView.Cell> pattern) {
Paul Lawrence2daf2642014-03-14 09:20:24 -0700580 mLockPatternView.setEnabled(false);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800581 new DecryptTask().execute(LockPatternUtils.patternToString(pattern));
582 }
583
584 @Override
585 public void onPatternCellAdded(List<Cell> pattern) {
586 }
587 };
588
589 private void passwordEntryInit() {
590 // Password/pin case
Jason parks06c5ff42011-03-01 10:17:40 -0600591 mPasswordEntry = (EditText) findViewById(R.id.passwordEntry);
Paul Lawrenceb05f39d2014-02-04 10:22:19 -0800592 if (mPasswordEntry != null){
593 mPasswordEntry.setOnEditorActionListener(this);
594 mPasswordEntry.requestFocus();
595 // Become quiet when the user interacts with the Edit text screen.
596 mPasswordEntry.setOnKeyListener(this);
597 mPasswordEntry.setOnTouchListener(this);
598 mPasswordEntry.addTextChangedListener(this);
599 }
600
601 // Pattern case
602 mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern);
603 if (mLockPatternView != null) {
604 mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener);
605 }
Jason parks35933812011-01-21 15:48:20 -0600606
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700607 // Disable the Emergency call button if the device has no voice telephone capability
Santos Cordon3afbdf02014-05-29 21:48:16 -0700608 if (!getTelephonyManager().isVoiceCapable()) {
Vikram Aggarwalc62d3212012-05-02 16:29:10 -0700609 final View emergencyCall = findViewById(R.id.emergencyCallButton);
610 if (emergencyCall != null) {
611 Log.d(TAG, "Removing the emergency Call button");
612 emergencyCall.setVisibility(View.GONE);
613 }
614 }
615
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700616 final View imeSwitcher = findViewById(R.id.switch_ime_button);
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700617 final InputMethodManager imm = (InputMethodManager) getSystemService(
618 Context.INPUT_METHOD_SERVICE);
619 if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) {
620 imeSwitcher.setVisibility(View.VISIBLE);
621 imeSwitcher.setOnClickListener(new OnClickListener() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700622 @Override
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700623 public void onClick(View v) {
624 imm.showInputMethodPicker();
625 }
626 });
Jason parks00046d62011-06-13 17:38:45 -0500627 }
David Brown8373b452011-06-20 12:38:45 -0700628
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700629 // We want to keep the screen on while waiting for input. In minimal boot mode, the device
630 // is completely non-functional, and we want the user to notice the device and enter a
631 // password.
632 if (mWakeLock == null) {
633 Log.d(TAG, "Acquiring wakelock.");
634 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
635 if (pm != null) {
636 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
637 mWakeLock.acquire();
Paul Lawrence73456ac2014-05-16 07:56:04 -0700638 // Keep awake for 10 minutes - if the user hasn't been alerted by then
639 // best not to just drain their battery
640 mReleaseWakeLockCountdown = 96; // 96 * 5 + 120 = 600
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700641 }
642 }
Paul Lawrence73456ac2014-05-16 07:56:04 -0700643
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700644 // Asynchronously throw up the IME, since there are issues with requesting it to be shown
645 // immediately.
Paul Lawrencee54e9322014-03-13 14:05:48 -0700646 if (mLockPatternView == null) {
647 mHandler.postDelayed(new Runnable() {
648 @Override public void run() {
649 imm.showSoftInputUnchecked(0, null);
650 }
651 }, 0);
652 }
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700653
David Brown8373b452011-06-20 12:38:45 -0700654 updateEmergencyCallButtonState();
Vikram Aggarwald1147252012-05-08 13:52:30 -0700655 // Notify the user in 120 seconds that we are waiting for him to enter the password.
Vikram Aggarwal86b93932012-05-01 16:46:06 -0700656 mHandler.removeMessages(MESSAGE_NOTIFY);
Vikram Aggarwald1147252012-05-08 13:52:30 -0700657 mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000);
Jim Millerfb3d5ca2013-11-14 14:40:22 -0800658
659 // Dismiss keyguard while this screen is showing.
660 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Jason parks8fd5bc92011-01-12 16:03:31 -0600661 }
662
Ben Komalo9fcb6a72011-08-26 14:40:18 -0700663 /**
664 * Method adapted from com.android.inputmethod.latin.Utils
665 *
666 * @param imm The input method manager
667 * @param shouldIncludeAuxiliarySubtypes
668 * @return true if we have multiple IMEs to choose from
669 */
670 private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm,
671 final boolean shouldIncludeAuxiliarySubtypes) {
672 final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
673
674 // Number of the filtered IMEs
675 int filteredImisCount = 0;
676
677 for (InputMethodInfo imi : enabledImis) {
678 // We can return true immediately after we find two or more filtered IMEs.
679 if (filteredImisCount > 1) return true;
680 final List<InputMethodSubtype> subtypes =
681 imm.getEnabledInputMethodSubtypeList(imi, true);
682 // IMEs that have no subtypes should be counted.
683 if (subtypes.isEmpty()) {
684 ++filteredImisCount;
685 continue;
686 }
687
688 int auxCount = 0;
689 for (InputMethodSubtype subtype : subtypes) {
690 if (subtype.isAuxiliary()) {
691 ++auxCount;
692 }
693 }
694 final int nonAuxCount = subtypes.size() - auxCount;
695
696 // IMEs that have one or more non-auxiliary subtypes should be counted.
697 // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
698 // subtypes should be counted as well.
699 if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
700 ++filteredImisCount;
701 continue;
702 }
703 }
704
705 return filteredImisCount > 1
706 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
707 // input method subtype (The current IME should be LatinIME.)
708 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
709 }
710
Jason parks8fd5bc92011-01-12 16:03:31 -0600711 private IMountService getMountService() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700712 final IBinder service = ServiceManager.getService("mount");
Jason parks8fd5bc92011-01-12 16:03:31 -0600713 if (service != null) {
714 return IMountService.Stub.asInterface(service);
715 }
716 return null;
717 }
718
719 @Override
720 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Jason parks00046d62011-06-13 17:38:45 -0500721 if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
Jason parks8fd5bc92011-01-12 16:03:31 -0600722 // Get the password
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700723 final String password = v.getText().toString();
Jason parks8fd5bc92011-01-12 16:03:31 -0600724
Jason parksec5a45e2011-01-18 15:28:36 -0600725 if (TextUtils.isEmpty(password)) {
726 return true;
727 }
Jason parks35933812011-01-21 15:48:20 -0600728
Jason parks8fd5bc92011-01-12 16:03:31 -0600729 // Now that we have the password clear the password field.
730 v.setText(null);
731
Vikram Aggarwalde3c9cb2012-05-01 15:37:30 -0700732 // Disable the password entry and back keypress while checking the password. These
733 // we either be re-enabled if the password was wrong or after the cooldown period.
Jason parks06c5ff42011-03-01 10:17:40 -0600734 mPasswordEntry.setEnabled(false);
Vikram Aggarwalbf459da2012-11-08 16:54:59 -0800735 setBackFunctionality(false);
Jason parks8fd5bc92011-01-12 16:03:31 -0600736
Ben Komalo04606752011-08-18 14:50:26 -0700737 Log.d(TAG, "Attempting to send command to decrypt");
Jason parks06c5ff42011-03-01 10:17:40 -0600738 new DecryptTask().execute(password);
Jason parks35933812011-01-21 15:48:20 -0600739
Jason parks8fd5bc92011-01-12 16:03:31 -0600740 return true;
741 }
742 return false;
743 }
David Brown8373b452011-06-20 12:38:45 -0700744
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700745 /**
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700746 * Set airplane mode on the device if it isn't an LTE device.
747 * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save
748 * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor.
749 * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid
750 * both these problems, we turn the radio off. However, on certain networks turning on and
751 * off the radio takes a long time. In such cases, we are better off leaving the radio
752 * running so the latency of an E911 call is short.
753 * The behavior after this is:
754 * 1. Emergency dialing: the emergency dialer has logic to force the device out of
755 * airplane mode and restart the radio.
756 * 2. Full boot: we read the persistent settings from the previous boot and restore the
757 * radio to whatever it was before it restarted. This also happens when rebooting a
758 * phone that has no encryption.
759 */
760 private final void setAirplaneModeIfNecessary() {
761 final boolean isLteDevice =
Santos Cordon3afbdf02014-05-29 21:48:16 -0700762 getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700763 if (!isLteDevice) {
764 Log.d(TAG, "Going into airplane mode.");
Christopher Tate6a5929b2012-09-10 15:39:05 -0700765 Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700766 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
767 intent.putExtra("state", true);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700768 sendBroadcastAsUser(intent, UserHandle.ALL);
Vikram Aggarwalea1186d2012-05-03 15:35:03 -0700769 }
770 }
771
772 /**
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700773 * Code to update the state of, and handle clicks from, the "Emergency call" button.
774 *
775 * This code is mostly duplicated from the corresponding code in
776 * LockPatternUtils and LockPatternKeyguardView under frameworks/base.
777 */
David Brown8373b452011-06-20 12:38:45 -0700778 private void updateEmergencyCallButtonState() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700779 final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton);
David Brown8373b452011-06-20 12:38:45 -0700780 // The button isn't present at all in some configurations.
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700781 if (emergencyCall == null)
782 return;
David Brown8373b452011-06-20 12:38:45 -0700783
784 if (isEmergencyCallCapable()) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700785 emergencyCall.setVisibility(View.VISIBLE);
786 emergencyCall.setOnClickListener(new View.OnClickListener() {
787 @Override
788
David Brown8373b452011-06-20 12:38:45 -0700789 public void onClick(View v) {
790 takeEmergencyCallAction();
791 }
792 });
793 } else {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700794 emergencyCall.setVisibility(View.GONE);
David Brown8373b452011-06-20 12:38:45 -0700795 return;
796 }
797
Santos Cordon3afbdf02014-05-29 21:48:16 -0700798 final int newState = getTelephonyManager().getCallState();
David Brown8373b452011-06-20 12:38:45 -0700799 int textId;
800 if (newState == TelephonyManager.CALL_STATE_OFFHOOK) {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700801 // Show "return to call" text and show phone icon
David Brown8373b452011-06-20 12:38:45 -0700802 textId = R.string.cryptkeeper_return_to_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700803 final int phoneCallIcon = R.drawable.stat_sys_phone_call;
804 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700805 } else {
806 textId = R.string.cryptkeeper_emergency_call;
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700807 final int emergencyIcon = R.drawable.ic_emergency;
808 emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
David Brown8373b452011-06-20 12:38:45 -0700809 }
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700810 emergencyCall.setText(textId);
David Brown8373b452011-06-20 12:38:45 -0700811 }
812
813 private boolean isEmergencyCallCapable() {
814 return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
815 }
816
817 private void takeEmergencyCallAction() {
Santos Cordon3afbdf02014-05-29 21:48:16 -0700818 TelephonyManager telephonyManager = getTelephonyManager();
819 if (telephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
820 telephonyManager.showCallScreen();
David Brown8373b452011-06-20 12:38:45 -0700821 } else {
822 launchEmergencyDialer();
823 }
824 }
825
David Brown8373b452011-06-20 12:38:45 -0700826
827 private void launchEmergencyDialer() {
Vikram Aggarwalbfa3a642012-03-29 14:07:22 -0700828 final Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
David Brown8373b452011-06-20 12:38:45 -0700829 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
830 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
831 startActivity(intent);
832 }
Vikram Aggarwald1147252012-05-08 13:52:30 -0700833
Santos Cordon3afbdf02014-05-29 21:48:16 -0700834 private TelephonyManager getTelephonyManager() {
835 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
836 }
837
Vikram Aggarwald1147252012-05-08 13:52:30 -0700838 /**
839 * Listen to key events so we can disable sounds when we get a keyinput in EditText.
840 */
841 private void delayAudioNotification() {
Vikram Aggarwald1147252012-05-08 13:52:30 -0700842 mNotificationCountdown = 20;
843 }
844
845 @Override
846 public boolean onKey(View v, int keyCode, KeyEvent event) {
847 delayAudioNotification();
848 return false;
849 }
850
851 @Override
852 public boolean onTouch(View v, MotionEvent event) {
853 delayAudioNotification();
854 return false;
855 }
856
857 @Override
858 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
859 return;
860 }
861
862 @Override
863 public void onTextChanged(CharSequence s, int start, int before, int count) {
864 delayAudioNotification();
865 }
866
867 @Override
868 public void afterTextChanged(Editable s) {
869 return;
870 }
David Brown8373b452011-06-20 12:38:45 -0700871}