Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.StatusBarManager; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 23 | import android.content.Intent; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 25 | import android.media.AudioManager; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 27 | import android.os.Bundle; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 28 | import android.os.Handler; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 29 | import android.os.IBinder; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 30 | import android.os.Message; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 31 | import android.os.PowerManager; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 33 | import android.os.ServiceManager; |
| 34 | import android.os.SystemProperties; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 36 | import android.os.storage.IMountService; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 37 | import android.os.storage.StorageManager; |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame^] | 38 | |
| 39 | import android.phone.PhoneManager; |
Vikram Aggarwal | 6ebbd30 | 2012-05-04 14:09:52 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 41 | import android.telephony.TelephonyManager; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 42 | import android.text.Editable; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 43 | import android.text.TextUtils; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 44 | import android.text.TextWatcher; |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 45 | import android.text.format.DateUtils; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 46 | import android.util.Log; |
| 47 | import android.view.KeyEvent; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 48 | import android.view.MotionEvent; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 49 | import android.view.View; |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 50 | import android.view.WindowManager; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 51 | import android.view.View.OnClickListener; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 52 | import android.view.View.OnKeyListener; |
| 53 | import android.view.View.OnTouchListener; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 54 | import android.view.inputmethod.EditorInfo; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 55 | import android.view.inputmethod.InputMethodInfo; |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 56 | import android.view.inputmethod.InputMethodManager; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 57 | import android.view.inputmethod.InputMethodSubtype; |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 58 | import android.widget.Button; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 59 | import android.widget.EditText; |
| 60 | import android.widget.ProgressBar; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 61 | import android.widget.TextView; |
| 62 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 63 | import com.android.internal.statusbar.StatusBarIcon; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 64 | import com.android.internal.telephony.Phone; |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 65 | import com.android.internal.telephony.PhoneConstants; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 66 | import com.android.internal.widget.LockPatternUtils; |
| 67 | import com.android.internal.widget.LockPatternView; |
| 68 | import com.android.internal.widget.LockPatternView.Cell; |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 69 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 70 | import static com.android.internal.widget.LockPatternView.DisplayMode; |
| 71 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 72 | import java.util.List; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Settings screens to show the UI flows for encrypting/decrypting the device. |
| 76 | * |
| 77 | * This may be started via adb for debugging the UI layout, without having to go through |
| 78 | * encryption flows everytime. It should be noted that starting the activity in this manner |
| 79 | * is only useful for verifying UI-correctness - the behavior will not be identical. |
| 80 | * <pre> |
| 81 | * $ adb shell pm enable com.android.settings/.CryptKeeper |
| 82 | * $ adb shell am start \ |
| 83 | * -e "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW" "progress" \ |
| 84 | * -n com.android.settings/.CryptKeeper |
| 85 | * </pre> |
| 86 | */ |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 87 | public class CryptKeeper extends Activity implements TextView.OnEditorActionListener, |
| 88 | OnKeyListener, OnTouchListener, TextWatcher { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 89 | private static final String TAG = "CryptKeeper"; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 90 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 91 | private static final String DECRYPT_STATE = "trigger_restart_framework"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 92 | /** Message sent to us to indicate encryption update progress. */ |
| 93 | private static final int MESSAGE_UPDATE_PROGRESS = 1; |
| 94 | /** Message sent to us to cool-down (waste user's time between password attempts) */ |
| 95 | private static final int MESSAGE_COOLDOWN = 2; |
| 96 | /** Message sent to us to indicate alerting the user that we are waiting for password entry */ |
| 97 | private static final int MESSAGE_NOTIFY = 3; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 98 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 99 | // Constants used to control policy. |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 100 | private static final int MAX_FAILED_ATTEMPTS = 30; |
| 101 | private static final int COOL_DOWN_ATTEMPTS = 10; |
| 102 | private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds |
| 103 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 104 | // Intent action for launching the Emergency Dialer activity. |
| 105 | static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; |
| 106 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 107 | // Debug Intent extras so that this Activity may be started via adb for debugging UI layouts |
| 108 | private static final String EXTRA_FORCE_VIEW = |
| 109 | "com.android.settings.CryptKeeper.DEBUG_FORCE_VIEW"; |
| 110 | private static final String FORCE_VIEW_PROGRESS = "progress"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 111 | private static final String FORCE_VIEW_ERROR = "error"; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 112 | private static final String FORCE_VIEW_PASSWORD = "password"; |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 113 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 114 | /** When encryption is detected, this flag indicates whether or not we've checked for errors. */ |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 115 | private boolean mValidationComplete; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 116 | private boolean mValidationRequested; |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 117 | /** A flag to indicate that the volume is in a bad state (e.g. partially encrypted). */ |
| 118 | private boolean mEncryptionGoneBad; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 119 | /** A flag to indicate when the back event should be ignored */ |
| 120 | private boolean mIgnoreBack = false; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 121 | private int mCooldown; |
| 122 | PowerManager.WakeLock mWakeLock; |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 123 | private EditText mPasswordEntry; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 124 | private LockPatternView mLockPatternView; |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 125 | /** Number of calls to {@link #notifyUser()} to ignore before notifying. */ |
| 126 | private int mNotificationCountdown = 0; |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 127 | /** Number of calls to {@link #notifyUser()} before we release the wakelock */ |
| 128 | private int mReleaseWakeLockCountdown = 0; |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 129 | private int mStatusString = R.string.enter_password; |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 130 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 131 | // how long we wait to clear a wrong pattern |
| 132 | private static final int WRONG_PATTERN_CLEAR_TIMEOUT_MS = 1500; |
| 133 | |
| 134 | private Runnable mClearPatternRunnable = new Runnable() { |
| 135 | public void run() { |
| 136 | mLockPatternView.clearPattern(); |
| 137 | } |
| 138 | }; |
| 139 | |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Used to propagate state through configuration changes (e.g. screen rotation) |
| 142 | */ |
| 143 | private static class NonConfigurationInstanceState { |
| 144 | final PowerManager.WakeLock wakelock; |
| 145 | |
| 146 | NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) { |
| 147 | wakelock = _wakelock; |
| 148 | } |
| 149 | } |
| 150 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Activity used to fade the screen to black after the password is entered. |
| 153 | */ |
Vikram Aggarwal | 9f55ae2 | 2012-04-02 13:36:35 -0700 | [diff] [blame] | 154 | public static class FadeToBlack extends Activity { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 155 | @Override |
| 156 | public void onCreate(Bundle savedInstanceState) { |
| 157 | super.onCreate(savedInstanceState); |
| 158 | setContentView(R.layout.crypt_keeper_blank); |
| 159 | } |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 160 | /** Ignore all back events. */ |
| 161 | @Override |
| 162 | public void onBackPressed() { |
| 163 | return; |
| 164 | } |
Jason parks | f1dbf55 | 2011-01-24 16:19:28 -0600 | [diff] [blame] | 165 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 166 | |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 167 | private class DecryptTask extends AsyncTask<String, Void, Integer> { |
| 168 | @Override |
| 169 | protected Integer doInBackground(String... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 170 | final IMountService service = getMountService(); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 171 | try { |
| 172 | return service.decryptStorage(params[0]); |
| 173 | } catch (Exception e) { |
| 174 | Log.e(TAG, "Error while decrypting...", e); |
| 175 | return -1; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | protected void onPostExecute(Integer failedAttempts) { |
| 181 | if (failedAttempts == 0) { |
| 182 | // The password was entered successfully. Start the Blank activity |
| 183 | // so this activity animates to black before the devices starts. Note |
| 184 | // It has 1 second to complete the animation or it will be frozen |
| 185 | // until the boot animation comes back up. |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 186 | Intent intent = new Intent(CryptKeeper.this, FadeToBlack.class); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 187 | finish(); |
| 188 | startActivity(intent); |
| 189 | } else if (failedAttempts == MAX_FAILED_ATTEMPTS) { |
| 190 | // Factory reset the device. |
| 191 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 192 | } else { |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 193 | // Wrong entry. Handle pattern case. |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 194 | if (mLockPatternView != null) { |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 195 | mLockPatternView.setDisplayMode(DisplayMode.Wrong); |
| 196 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 197 | mLockPatternView.postDelayed(mClearPatternRunnable, WRONG_PATTERN_CLEAR_TIMEOUT_MS); |
| 198 | } |
| 199 | if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) { |
| 200 | mCooldown = COOL_DOWN_INTERVAL; |
| 201 | cooldown(); |
| 202 | } else { |
| 203 | final TextView status = (TextView) findViewById(R.id.status); |
| 204 | status.setText(R.string.try_again); |
| 205 | if (mLockPatternView != null) { |
| 206 | mLockPatternView.setDisplayMode(DisplayMode.Wrong); |
| 207 | } |
| 208 | // Reenable the password entry |
| 209 | if (mPasswordEntry != null) { |
| 210 | mPasswordEntry.setEnabled(true); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 211 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 212 | Context.INPUT_METHOD_SERVICE); |
| 213 | imm.showSoftInput(mPasswordEntry, 0); |
Paul Lawrence | 08c6ab0 | 2014-06-19 13:34:58 -0700 | [diff] [blame] | 214 | setBackFunctionality(true); |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 215 | } |
| 216 | if (mLockPatternView != null) { |
| 217 | mLockPatternView.setEnabled(true); |
| 218 | } |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 219 | } |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
Jason parks | 75c085e | 2011-02-10 14:03:47 -0600 | [diff] [blame] | 223 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 224 | private class ValidationTask extends AsyncTask<Void, Void, Boolean> { |
| 225 | @Override |
| 226 | protected Boolean doInBackground(Void... params) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 227 | final IMountService service = getMountService(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 228 | try { |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 229 | Log.d(TAG, "Validating encryption state."); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 230 | int state = service.getEncryptionState(); |
| 231 | if (state == IMountService.ENCRYPTION_STATE_NONE) { |
| 232 | Log.w(TAG, "Unexpectedly in CryptKeeper even though there is no encryption."); |
| 233 | return true; // Unexpected, but fine, I guess... |
| 234 | } |
| 235 | return state == IMountService.ENCRYPTION_STATE_OK; |
| 236 | } catch (RemoteException e) { |
| 237 | Log.w(TAG, "Unable to get encryption state properly"); |
| 238 | return true; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | protected void onPostExecute(Boolean result) { |
| 244 | mValidationComplete = true; |
| 245 | if (Boolean.FALSE.equals(result)) { |
| 246 | Log.w(TAG, "Incomplete, or corrupted encryption detected. Prompting user to wipe."); |
| 247 | mEncryptionGoneBad = true; |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 248 | } else { |
| 249 | Log.d(TAG, "Encryption state validated. Proceeding to configure UI"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 250 | } |
| 251 | setupUi(); |
| 252 | } |
| 253 | } |
| 254 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 255 | private final Handler mHandler = new Handler() { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 256 | @Override |
| 257 | public void handleMessage(Message msg) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 258 | switch (msg.what) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 259 | case MESSAGE_UPDATE_PROGRESS: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 260 | updateProgress(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 261 | break; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 262 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 263 | case MESSAGE_COOLDOWN: |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 264 | cooldown(); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 265 | break; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 266 | |
| 267 | case MESSAGE_NOTIFY: |
| 268 | notifyUser(); |
| 269 | break; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | }; |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 273 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 274 | private AudioManager mAudioManager; |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 275 | /** The status bar where back/home/recent buttons are shown. */ |
| 276 | private StatusBarManager mStatusBar; |
| 277 | |
| 278 | /** All the widgets to disable in the status bar */ |
| 279 | final private static int sWidgetsToDisable = StatusBarManager.DISABLE_EXPAND |
| 280 | | StatusBarManager.DISABLE_NOTIFICATION_ICONS |
| 281 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS |
| 282 | | StatusBarManager.DISABLE_SYSTEM_INFO |
| 283 | | StatusBarManager.DISABLE_HOME |
Alon Albert | 66d050b | 2013-09-30 17:08:12 -0700 | [diff] [blame] | 284 | | StatusBarManager.DISABLE_SEARCH |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 285 | | StatusBarManager.DISABLE_RECENT; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 286 | |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 287 | /** @return whether or not this Activity was started for debugging the UI only. */ |
| 288 | private boolean isDebugView() { |
| 289 | return getIntent().hasExtra(EXTRA_FORCE_VIEW); |
| 290 | } |
| 291 | |
| 292 | /** @return whether or not this Activity was started for debugging the specific UI view only. */ |
| 293 | private boolean isDebugView(String viewType /* non-nullable */) { |
| 294 | return viewType.equals(getIntent().getStringExtra(EXTRA_FORCE_VIEW)); |
| 295 | } |
| 296 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Notify the user that we are awaiting input. Currently this sends an audio alert. |
| 299 | */ |
| 300 | private void notifyUser() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 301 | if (mNotificationCountdown > 0) { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 302 | --mNotificationCountdown; |
| 303 | } else if (mAudioManager != null) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 304 | try { |
| 305 | // Play the standard keypress sound at full volume. This should be available on |
| 306 | // every device. We cannot play a ringtone here because media services aren't |
| 307 | // available yet. A DTMF-style tone is too soft to be noticed, and might not exist |
| 308 | // on tablet devices. The idea is to alert the user that something is needed: this |
| 309 | // does not have to be pleasing. |
| 310 | mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, 100); |
| 311 | } catch (Exception e) { |
| 312 | Log.w(TAG, "notifyUser: Exception while playing sound: " + e); |
| 313 | } |
| 314 | } |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 315 | // Notify the user again in 5 seconds. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 316 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 317 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 5 * 1000); |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 318 | |
| 319 | if (mWakeLock.isHeld()) { |
| 320 | if (mReleaseWakeLockCountdown > 0) { |
| 321 | --mReleaseWakeLockCountdown; |
| 322 | } else { |
| 323 | mWakeLock.release(); |
| 324 | } |
| 325 | } |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Ignore back events after the user has entered the decrypt screen and while the device is |
| 330 | * encrypting. |
| 331 | */ |
| 332 | @Override |
| 333 | public void onBackPressed() { |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 334 | // In the rare case that something pressed back even though we were disabled. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 335 | if (mIgnoreBack) |
| 336 | return; |
| 337 | super.onBackPressed(); |
| 338 | } |
| 339 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 340 | @Override |
| 341 | public void onCreate(Bundle savedInstanceState) { |
| 342 | super.onCreate(savedInstanceState); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 343 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 344 | // If we are not encrypted or encrypting, get out quickly. |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 345 | final String state = SystemProperties.get("vold.decrypt"); |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 346 | if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 347 | // Disable the crypt keeper. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 348 | PackageManager pm = getPackageManager(); |
| 349 | ComponentName name = new ComponentName(this, CryptKeeper.class); |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 350 | pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 351 | PackageManager.DONT_KILL_APP); |
| 352 | // Typically CryptKeeper is launched as the home app. We didn't |
Dianne Hackborn | 644fa42 | 2011-10-18 13:38:03 -0700 | [diff] [blame] | 353 | // want to be running, so need to finish this activity. We can count |
| 354 | // on the activity manager re-launching the new home app upon finishing |
| 355 | // this one, since this will leave the activity stack empty. |
Dianne Hackborn | 140f6c6 | 2011-10-16 11:49:00 -0700 | [diff] [blame] | 356 | // NOTE: This is really grungy. I think it would be better for the |
| 357 | // activity manager to explicitly launch the crypt keeper instead of |
| 358 | // home in the situation where we need to decrypt the device |
| 359 | finish(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 360 | return; |
| 361 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 362 | |
Vikram Aggarwal | b96b35a | 2012-05-01 11:09:39 -0700 | [diff] [blame] | 363 | // Disable the status bar, but do NOT disable back because the user needs a way to go |
| 364 | // from keyboard settings and back to the password screen. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 365 | mStatusBar = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
| 366 | mStatusBar.disable(sWidgetsToDisable); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 367 | |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 368 | setAirplaneModeIfNecessary(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 369 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 370 | // Check for (and recover) retained instance data |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 371 | final Object lastInstance = getLastNonConfigurationInstance(); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 372 | if (lastInstance instanceof NonConfigurationInstanceState) { |
| 373 | NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance; |
| 374 | mWakeLock = retained.wakelock; |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 375 | Log.d(TAG, "Restoring wakelock from NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 376 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 377 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 378 | |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 379 | /** |
| 380 | * Note, we defer the state check and screen setup to onStart() because this will be |
| 381 | * re-run if the user clicks the power button (sleeping/waking the screen), and this is |
| 382 | * especially important if we were to lose the wakelock for any reason. |
| 383 | */ |
| 384 | @Override |
| 385 | public void onStart() { |
| 386 | super.onStart(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 387 | setupUi(); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Initializes the UI based on the current state of encryption. |
| 392 | * This is idempotent - calling repeatedly will simply re-initialize the UI. |
| 393 | */ |
| 394 | private void setupUi() { |
| 395 | if (mEncryptionGoneBad || isDebugView(FORCE_VIEW_ERROR)) { |
| 396 | setContentView(R.layout.crypt_keeper_progress); |
| 397 | showFactoryReset(); |
| 398 | return; |
| 399 | } |
| 400 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 401 | final String progress = SystemProperties.get("vold.encrypt_progress"); |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 402 | if (!"".equals(progress) || isDebugView(FORCE_VIEW_PROGRESS)) { |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 403 | setContentView(R.layout.crypt_keeper_progress); |
| 404 | encryptionProgressInit(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 405 | } else if (mValidationComplete || isDebugView(FORCE_VIEW_PASSWORD)) { |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 406 | new AsyncTask<Void, Void, Void>() { |
| 407 | int type = StorageManager.CRYPT_TYPE_PASSWORD; |
| 408 | String owner_info; |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 409 | |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 410 | @Override |
| 411 | public Void doInBackground(Void... v) { |
| 412 | try { |
| 413 | final IMountService service = getMountService(); |
| 414 | type = service.getPasswordType(); |
| 415 | owner_info = service.getField("OwnerInfo"); |
| 416 | } catch (Exception e) { |
| 417 | Log.e(TAG, "Error calling mount service " + e); |
| 418 | } |
| 419 | |
| 420 | return null; |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public void onPostExecute(java.lang.Void v) { |
| 425 | if(type == StorageManager.CRYPT_TYPE_PIN) { |
| 426 | setContentView(R.layout.crypt_keeper_pin_entry); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 427 | mStatusString = R.string.enter_pin; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 428 | } else if (type == StorageManager.CRYPT_TYPE_PATTERN) { |
| 429 | setContentView(R.layout.crypt_keeper_pattern_entry); |
| 430 | setBackFunctionality(false); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 431 | mStatusString = R.string.enter_pattern; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 432 | } else { |
| 433 | setContentView(R.layout.crypt_keeper_password_entry); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 434 | mStatusString = R.string.enter_password; |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 435 | } |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 436 | final TextView status = (TextView) findViewById(R.id.status); |
| 437 | status.setText(mStatusString); |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 438 | |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 439 | final TextView ownerInfo = (TextView) findViewById(R.id.owner_info); |
| 440 | ownerInfo.setText(owner_info); |
| 441 | ownerInfo.setSelected(true); // Required for marquee'ing to work |
| 442 | |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 443 | passwordEntryInit(); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 444 | |
| 445 | if (mCooldown > 0) { |
| 446 | setBackFunctionality(false); |
| 447 | cooldown(); // in case we are cooling down and coming back from emergency dialler |
| 448 | } |
Paul Lawrence | 9ac2d81 | 2014-03-18 10:59:18 -0700 | [diff] [blame] | 449 | } |
| 450 | }.execute(); |
Ben Komalo | d4758ef | 2011-09-08 14:36:08 -0700 | [diff] [blame] | 451 | } else if (!mValidationRequested) { |
| 452 | // We're supposed to be encrypted, but no validation has been done. |
| 453 | new ValidationTask().execute((Void[]) null); |
| 454 | mValidationRequested = true; |
Andy Stadler | 9597406 | 2011-02-01 17:35:20 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 458 | @Override |
| 459 | public void onStop() { |
| 460 | super.onStop(); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 461 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 462 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
| 463 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop() |
| 468 | * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears |
| 469 | * mWakeLock so the subsequent call to onDestroy does not release it. |
| 470 | */ |
| 471 | @Override |
| 472 | public Object onRetainNonConfigurationInstance() { |
| 473 | NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock); |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 474 | Log.d(TAG, "Handing wakelock off to NonConfigurationInstanceState"); |
Andy Stadler | 1499740 | 2011-02-01 15:34:59 -0800 | [diff] [blame] | 475 | mWakeLock = null; |
| 476 | return state; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void onDestroy() { |
| 481 | super.onDestroy(); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 482 | |
| 483 | if (mWakeLock != null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 484 | Log.d(TAG, "Releasing and destroying wakelock"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 485 | mWakeLock.release(); |
| 486 | mWakeLock = null; |
| 487 | } |
| 488 | } |
| 489 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 490 | /** |
| 491 | * Start encrypting the device. |
| 492 | */ |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 493 | private void encryptionProgressInit() { |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 494 | // Accquire a partial wakelock to prevent the device from sleeping. Note |
| 495 | // we never release this wakelock as we will be restarted after the device |
| 496 | // is encrypted. |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 497 | Log.d(TAG, "Encryption progress screen initializing."); |
Ben Komalo | 9ee164f | 2011-09-21 10:40:50 -0700 | [diff] [blame] | 498 | if (mWakeLock == null) { |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 499 | Log.d(TAG, "Acquiring wakelock."); |
| 500 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 501 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 502 | mWakeLock.acquire(); |
| 503 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 504 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 505 | ((ProgressBar) findViewById(R.id.progress_bar)).setIndeterminate(true); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 506 | // Ignore all back presses from now, both hard and soft keys. |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 507 | setBackFunctionality(false); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 508 | // Start the first run of progress manually. This method sets up messages to occur at |
| 509 | // repeated intervals. |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 510 | updateProgress(); |
| 511 | } |
| 512 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 513 | private void showFactoryReset() { |
| 514 | // Hide the encryption-bot to make room for the "factory reset" button |
| 515 | findViewById(R.id.encroid).setVisibility(View.GONE); |
| 516 | |
| 517 | // Show the reset button, failure text, and a divider |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 518 | final Button button = (Button) findViewById(R.id.factory_reset); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 519 | button.setVisibility(View.VISIBLE); |
| 520 | button.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 521 | @Override |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 522 | public void onClick(View v) { |
| 523 | // Factory reset the device. |
| 524 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 525 | } |
| 526 | }); |
| 527 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 528 | // Alert the user of the failure. |
| 529 | ((TextView) findViewById(R.id.title)).setText(R.string.crypt_keeper_failed_title); |
| 530 | ((TextView) findViewById(R.id.status)).setText(R.string.crypt_keeper_failed_summary); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 531 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 532 | final View view = findViewById(R.id.bottom_divider); |
| 533 | // TODO(viki): Why would the bottom divider be missing in certain layouts? Investigate. |
Ben Komalo | f0104df | 2011-08-16 17:48:42 -0700 | [diff] [blame] | 534 | if (view != null) { |
| 535 | view.setVisibility(View.VISIBLE); |
| 536 | } |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 539 | private void updateProgress() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 540 | final String state = SystemProperties.get("vold.encrypt_progress"); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 541 | |
Ben Komalo | 0e66609 | 2011-09-01 15:42:00 -0700 | [diff] [blame] | 542 | if ("error_partially_encrypted".equals(state)) { |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 543 | showFactoryReset(); |
| 544 | return; |
| 545 | } |
| 546 | |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 547 | // Get status as percentage first |
| 548 | CharSequence status = getText(R.string.crypt_keeper_setup_description); |
| 549 | int percent = 0; |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 550 | try { |
Ben Komalo | 91a2f05 | 2011-08-16 17:48:25 -0700 | [diff] [blame] | 551 | // Force a 50% progress state when debugging the view. |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 552 | percent = isDebugView() ? 50 : Integer.parseInt(state); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 553 | } catch (Exception e) { |
| 554 | Log.w(TAG, "Error parsing progress: " + e.toString()); |
| 555 | } |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 556 | String progress = Integer.toString(percent); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 557 | |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 558 | // Now try to get status as time remaining and replace as appropriate |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 559 | Log.v(TAG, "Encryption progress: " + progress); |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 560 | try { |
| 561 | final String timeProperty = SystemProperties.get("vold.encrypt_time_remaining"); |
| 562 | int time = Integer.parseInt(timeProperty); |
| 563 | if (time >= 0) { |
| 564 | // Round up to multiple of 10 - this way display is less jerky |
| 565 | time = (time + 9) / 10 * 10; |
| 566 | progress = DateUtils.formatElapsedTime(time); |
| 567 | status = getText(R.string.crypt_keeper_setup_time_remaining); |
| 568 | } |
| 569 | } catch (Exception e) { |
| 570 | // Will happen if no time etc - show percentage |
| 571 | } |
| 572 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 573 | final TextView tv = (TextView) findViewById(R.id.status); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 574 | if (tv != null) { |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 575 | tv.setText(TextUtils.expandTemplate(status, progress)); |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 576 | } |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 577 | |
| 578 | // Check the progress every 1 seconds |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 579 | mHandler.removeMessages(MESSAGE_UPDATE_PROGRESS); |
Paul Lawrence | b15c68f | 2014-06-05 07:24:23 -0700 | [diff] [blame] | 580 | mHandler.sendEmptyMessageDelayed(MESSAGE_UPDATE_PROGRESS, 1000); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 581 | } |
| 582 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 583 | /** Disable password input for a while to force the user to waste time between retries */ |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 584 | private void cooldown() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 585 | final TextView status = (TextView) findViewById(R.id.status); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 586 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 587 | if (mCooldown <= 0) { |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 588 | // Re-enable the password entry and back presses. |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 589 | if (mPasswordEntry != null) { |
| 590 | mPasswordEntry.setEnabled(true); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 591 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 592 | Context.INPUT_METHOD_SERVICE); |
| 593 | imm.showSoftInput(mPasswordEntry, 0); |
Paul Lawrence | 2daf264 | 2014-03-14 09:20:24 -0700 | [diff] [blame] | 594 | setBackFunctionality(true); |
| 595 | } |
| 596 | if (mLockPatternView != null) { |
| 597 | mLockPatternView.setEnabled(true); |
| 598 | } |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 599 | status.setText(mStatusString); |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 600 | } else { |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 601 | // Disable the password entry and back presses. |
| 602 | if (mPasswordEntry != null) { |
| 603 | mPasswordEntry.setEnabled(false); |
| 604 | } |
| 605 | if (mLockPatternView != null) { |
| 606 | mLockPatternView.setEnabled(false); |
| 607 | } |
| 608 | |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 609 | CharSequence template = getText(R.string.crypt_keeper_cooldown); |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 610 | status.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown))); |
Andy Stadler | 13d6204 | 2011-01-31 19:21:37 -0800 | [diff] [blame] | 611 | |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 612 | mCooldown--; |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 613 | mHandler.removeMessages(MESSAGE_COOLDOWN); |
| 614 | mHandler.sendEmptyMessageDelayed(MESSAGE_COOLDOWN, 1000); // Tick every second |
Jason parks | f821730 | 2011-01-26 13:11:42 -0600 | [diff] [blame] | 615 | } |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 616 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 617 | |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 618 | /** |
| 619 | * Sets the back status: enabled or disabled according to the parameter. |
| 620 | * @param isEnabled true if back is enabled, false otherwise. |
| 621 | */ |
| 622 | private final void setBackFunctionality(boolean isEnabled) { |
| 623 | mIgnoreBack = !isEnabled; |
| 624 | if (isEnabled) { |
| 625 | mStatusBar.disable(sWidgetsToDisable); |
| 626 | } else { |
| 627 | mStatusBar.disable(sWidgetsToDisable | StatusBarManager.DISABLE_BACK); |
| 628 | } |
| 629 | } |
| 630 | |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 631 | protected LockPatternView.OnPatternListener mChooseNewLockPatternListener = |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 632 | new LockPatternView.OnPatternListener() { |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 633 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 634 | @Override |
| 635 | public void onPatternStart() { |
| 636 | mLockPatternView.removeCallbacks(mClearPatternRunnable); |
| 637 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 638 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 639 | @Override |
| 640 | public void onPatternCleared() { |
| 641 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 642 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 643 | @Override |
| 644 | public void onPatternDetected(List<LockPatternView.Cell> pattern) { |
| 645 | mLockPatternView.setEnabled(false); |
| 646 | new DecryptTask().execute(LockPatternUtils.patternToString(pattern)); |
| 647 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 648 | |
Paul Lawrence | f6cda3b | 2014-06-12 08:26:08 -0700 | [diff] [blame] | 649 | @Override |
| 650 | public void onPatternCellAdded(List<Cell> pattern) { |
| 651 | } |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | private void passwordEntryInit() { |
| 655 | // Password/pin case |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 656 | mPasswordEntry = (EditText) findViewById(R.id.passwordEntry); |
Paul Lawrence | b05f39d | 2014-02-04 10:22:19 -0800 | [diff] [blame] | 657 | if (mPasswordEntry != null){ |
| 658 | mPasswordEntry.setOnEditorActionListener(this); |
| 659 | mPasswordEntry.requestFocus(); |
| 660 | // Become quiet when the user interacts with the Edit text screen. |
| 661 | mPasswordEntry.setOnKeyListener(this); |
| 662 | mPasswordEntry.setOnTouchListener(this); |
| 663 | mPasswordEntry.addTextChangedListener(this); |
| 664 | } |
| 665 | |
| 666 | // Pattern case |
| 667 | mLockPatternView = (LockPatternView) findViewById(R.id.lockPattern); |
| 668 | if (mLockPatternView != null) { |
| 669 | mLockPatternView.setOnPatternListener(mChooseNewLockPatternListener); |
| 670 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 671 | |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 672 | // Disable the Emergency call button if the device has no voice telephone capability |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 673 | if (!getTelephonyManager().isVoiceCapable()) { |
Vikram Aggarwal | c62d321 | 2012-05-02 16:29:10 -0700 | [diff] [blame] | 674 | final View emergencyCall = findViewById(R.id.emergencyCallButton); |
| 675 | if (emergencyCall != null) { |
| 676 | Log.d(TAG, "Removing the emergency Call button"); |
| 677 | emergencyCall.setVisibility(View.GONE); |
| 678 | } |
| 679 | } |
| 680 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 681 | final View imeSwitcher = findViewById(R.id.switch_ime_button); |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 682 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 683 | Context.INPUT_METHOD_SERVICE); |
| 684 | if (imeSwitcher != null && hasMultipleEnabledIMEsOrSubtypes(imm, false)) { |
| 685 | imeSwitcher.setVisibility(View.VISIBLE); |
| 686 | imeSwitcher.setOnClickListener(new OnClickListener() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 687 | @Override |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 688 | public void onClick(View v) { |
| 689 | imm.showInputMethodPicker(); |
| 690 | } |
| 691 | }); |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 692 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 693 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 694 | // We want to keep the screen on while waiting for input. In minimal boot mode, the device |
| 695 | // is completely non-functional, and we want the user to notice the device and enter a |
| 696 | // password. |
| 697 | if (mWakeLock == null) { |
| 698 | Log.d(TAG, "Acquiring wakelock."); |
| 699 | final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 700 | if (pm != null) { |
| 701 | mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
| 702 | mWakeLock.acquire(); |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 703 | // Keep awake for 10 minutes - if the user hasn't been alerted by then |
| 704 | // best not to just drain their battery |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 705 | mReleaseWakeLockCountdown = 96; // 96 * 5 secs per click + 120 secs before we show this = 600 |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
Paul Lawrence | 73456ac | 2014-05-16 07:56:04 -0700 | [diff] [blame] | 708 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 709 | // Asynchronously throw up the IME, since there are issues with requesting it to be shown |
| 710 | // immediately. |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 711 | if (mLockPatternView == null && mCooldown <= 0) { |
Paul Lawrence | e54e932 | 2014-03-13 14:05:48 -0700 | [diff] [blame] | 712 | mHandler.postDelayed(new Runnable() { |
| 713 | @Override public void run() { |
| 714 | imm.showSoftInputUnchecked(0, null); |
| 715 | } |
| 716 | }, 0); |
| 717 | } |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 718 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 719 | updateEmergencyCallButtonState(); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 720 | // Notify the user in 120 seconds that we are waiting for him to enter the password. |
Vikram Aggarwal | 86b9393 | 2012-05-01 16:46:06 -0700 | [diff] [blame] | 721 | mHandler.removeMessages(MESSAGE_NOTIFY); |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 722 | mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY, 120 * 1000); |
Jim Miller | fb3d5ca | 2013-11-14 14:40:22 -0800 | [diff] [blame] | 723 | |
| 724 | // Dismiss keyguard while this screen is showing. |
| 725 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 726 | } |
| 727 | |
Ben Komalo | 9fcb6a7 | 2011-08-26 14:40:18 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Method adapted from com.android.inputmethod.latin.Utils |
| 730 | * |
| 731 | * @param imm The input method manager |
| 732 | * @param shouldIncludeAuxiliarySubtypes |
| 733 | * @return true if we have multiple IMEs to choose from |
| 734 | */ |
| 735 | private boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManager imm, |
| 736 | final boolean shouldIncludeAuxiliarySubtypes) { |
| 737 | final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); |
| 738 | |
| 739 | // Number of the filtered IMEs |
| 740 | int filteredImisCount = 0; |
| 741 | |
| 742 | for (InputMethodInfo imi : enabledImis) { |
| 743 | // We can return true immediately after we find two or more filtered IMEs. |
| 744 | if (filteredImisCount > 1) return true; |
| 745 | final List<InputMethodSubtype> subtypes = |
| 746 | imm.getEnabledInputMethodSubtypeList(imi, true); |
| 747 | // IMEs that have no subtypes should be counted. |
| 748 | if (subtypes.isEmpty()) { |
| 749 | ++filteredImisCount; |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | int auxCount = 0; |
| 754 | for (InputMethodSubtype subtype : subtypes) { |
| 755 | if (subtype.isAuxiliary()) { |
| 756 | ++auxCount; |
| 757 | } |
| 758 | } |
| 759 | final int nonAuxCount = subtypes.size() - auxCount; |
| 760 | |
| 761 | // IMEs that have one or more non-auxiliary subtypes should be counted. |
| 762 | // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary |
| 763 | // subtypes should be counted as well. |
| 764 | if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { |
| 765 | ++filteredImisCount; |
| 766 | continue; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | return filteredImisCount > 1 |
| 771 | // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled |
| 772 | // input method subtype (The current IME should be LatinIME.) |
| 773 | || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; |
| 774 | } |
| 775 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 776 | private IMountService getMountService() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 777 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 778 | if (service != null) { |
| 779 | return IMountService.Stub.asInterface(service); |
| 780 | } |
| 781 | return null; |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Jason parks | 00046d6 | 2011-06-13 17:38:45 -0500 | [diff] [blame] | 786 | if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) { |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 787 | // Get the password |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 788 | final String password = v.getText().toString(); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 789 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 790 | if (TextUtils.isEmpty(password)) { |
| 791 | return true; |
| 792 | } |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 793 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 794 | // Now that we have the password clear the password field. |
| 795 | v.setText(null); |
| 796 | |
Vikram Aggarwal | de3c9cb | 2012-05-01 15:37:30 -0700 | [diff] [blame] | 797 | // Disable the password entry and back keypress while checking the password. These |
| 798 | // we either be re-enabled if the password was wrong or after the cooldown period. |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 799 | mPasswordEntry.setEnabled(false); |
Vikram Aggarwal | bf459da | 2012-11-08 16:54:59 -0800 | [diff] [blame] | 800 | setBackFunctionality(false); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 801 | |
Ben Komalo | 0460675 | 2011-08-18 14:50:26 -0700 | [diff] [blame] | 802 | Log.d(TAG, "Attempting to send command to decrypt"); |
Jason parks | 06c5ff4 | 2011-03-01 10:17:40 -0600 | [diff] [blame] | 803 | new DecryptTask().execute(password); |
Jason parks | 3593381 | 2011-01-21 15:48:20 -0600 | [diff] [blame] | 804 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 805 | return true; |
| 806 | } |
| 807 | return false; |
| 808 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 809 | |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 810 | /** |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 811 | * Set airplane mode on the device if it isn't an LTE device. |
| 812 | * Full story: In minimal boot mode, we cannot save any state. In particular, we cannot save |
| 813 | * any incoming SMS's. So SMSs that are received here will be silently dropped to the floor. |
| 814 | * That is bad. Also, we cannot receive any telephone calls in this state. So to avoid |
| 815 | * both these problems, we turn the radio off. However, on certain networks turning on and |
| 816 | * off the radio takes a long time. In such cases, we are better off leaving the radio |
| 817 | * running so the latency of an E911 call is short. |
| 818 | * The behavior after this is: |
| 819 | * 1. Emergency dialing: the emergency dialer has logic to force the device out of |
| 820 | * airplane mode and restart the radio. |
| 821 | * 2. Full boot: we read the persistent settings from the previous boot and restore the |
| 822 | * radio to whatever it was before it restarted. This also happens when rebooting a |
| 823 | * phone that has no encryption. |
| 824 | */ |
| 825 | private final void setAirplaneModeIfNecessary() { |
| 826 | final boolean isLteDevice = |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 827 | getTelephonyManager().getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE; |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 828 | if (!isLteDevice) { |
| 829 | Log.d(TAG, "Going into airplane mode."); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 830 | Settings.Global.putInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 831 | final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 832 | intent.putExtra("state", true); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 833 | sendBroadcastAsUser(intent, UserHandle.ALL); |
Vikram Aggarwal | ea1186d | 2012-05-03 15:35:03 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | /** |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 838 | * Code to update the state of, and handle clicks from, the "Emergency call" button. |
| 839 | * |
| 840 | * This code is mostly duplicated from the corresponding code in |
| 841 | * LockPatternUtils and LockPatternKeyguardView under frameworks/base. |
| 842 | */ |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 843 | private void updateEmergencyCallButtonState() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 844 | final Button emergencyCall = (Button) findViewById(R.id.emergencyCallButton); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 845 | // The button isn't present at all in some configurations. |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 846 | if (emergencyCall == null) |
| 847 | return; |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 848 | |
| 849 | if (isEmergencyCallCapable()) { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 850 | emergencyCall.setVisibility(View.VISIBLE); |
| 851 | emergencyCall.setOnClickListener(new View.OnClickListener() { |
| 852 | @Override |
| 853 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 854 | public void onClick(View v) { |
| 855 | takeEmergencyCallAction(); |
| 856 | } |
| 857 | }); |
| 858 | } else { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 859 | emergencyCall.setVisibility(View.GONE); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 863 | int textId; |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame^] | 864 | if (getPhoneManager().isInAPhoneCall()) { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 865 | // Show "return to call" text and show phone icon |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 866 | textId = R.string.cryptkeeper_return_to_call; |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 867 | final int phoneCallIcon = R.drawable.stat_sys_phone_call; |
| 868 | emergencyCall.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 869 | } else { |
| 870 | textId = R.string.cryptkeeper_emergency_call; |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 871 | final int emergencyIcon = R.drawable.ic_emergency; |
| 872 | emergencyCall.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 873 | } |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 874 | emergencyCall.setText(textId); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | private boolean isEmergencyCallCapable() { |
| 878 | return getResources().getBoolean(com.android.internal.R.bool.config_voice_capable); |
| 879 | } |
| 880 | |
| 881 | private void takeEmergencyCallAction() { |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame^] | 882 | PhoneManager phoneManager = getPhoneManager(); |
| 883 | if (phoneManager.isInAPhoneCall()) { |
| 884 | phoneManager.showCallScreen(false /* showDialpad */); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 885 | } else { |
| 886 | launchEmergencyDialer(); |
| 887 | } |
| 888 | } |
| 889 | |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 890 | |
| 891 | private void launchEmergencyDialer() { |
Vikram Aggarwal | bfa3a64 | 2012-03-29 14:07:22 -0700 | [diff] [blame] | 892 | final Intent intent = new Intent(ACTION_EMERGENCY_DIAL); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 893 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 894 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Paul Lawrence | 5a70f05 | 2014-06-13 11:09:55 -0700 | [diff] [blame] | 895 | setBackFunctionality(true); |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 896 | startActivity(intent); |
| 897 | } |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 898 | |
Santos Cordon | 3afbdf0 | 2014-05-29 21:48:16 -0700 | [diff] [blame] | 899 | private TelephonyManager getTelephonyManager() { |
| 900 | return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
| 901 | } |
| 902 | |
Santos Cordon | 35230e9 | 2014-07-07 16:25:10 -0700 | [diff] [blame^] | 903 | private PhoneManager getPhoneManager() { |
| 904 | return (PhoneManager) getSystemService(Context.PHONE_SERVICE); |
| 905 | } |
| 906 | |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 907 | /** |
| 908 | * Listen to key events so we can disable sounds when we get a keyinput in EditText. |
| 909 | */ |
| 910 | private void delayAudioNotification() { |
Vikram Aggarwal | d114725 | 2012-05-08 13:52:30 -0700 | [diff] [blame] | 911 | mNotificationCountdown = 20; |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 916 | delayAudioNotification(); |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public boolean onTouch(View v, MotionEvent event) { |
| 922 | delayAudioNotification(); |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | @Override |
| 927 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | @Override |
| 932 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 933 | delayAudioNotification(); |
| 934 | } |
| 935 | |
| 936 | @Override |
| 937 | public void afterTextChanged(Editable s) { |
| 938 | return; |
| 939 | } |
David Brown | 8373b45 | 2011-06-20 12:38:45 -0700 | [diff] [blame] | 940 | } |