Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 17 | // TODO (b/35202196): move this class out of the root of the package. |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 18 | package com.android.settings.password; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 19 | |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 20 | import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME; |
| 21 | |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 23 | import android.app.Dialog; |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 24 | import android.app.KeyguardManager; |
Benjamin Franz | cc8d37a | 2016-01-18 17:31:26 +0000 | [diff] [blame] | 25 | import android.app.admin.DevicePolicyManager; |
Clara Bayarri | 86ebaa2 | 2015-12-10 12:35:27 +0000 | [diff] [blame] | 26 | import android.content.Context; |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 27 | import android.content.DialogInterface; |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 28 | import android.content.Intent; |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 29 | import android.content.pm.UserInfo; |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 30 | import android.graphics.Point; |
| 31 | import android.graphics.PorterDuff; |
Benjamin Franz | cc8d37a | 2016-01-18 17:31:26 +0000 | [diff] [blame] | 32 | import android.graphics.drawable.ColorDrawable; |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
Kevin Chyn | e56df88 | 2019-01-24 19:42:05 -0800 | [diff] [blame] | 34 | import android.hardware.biometrics.BiometricManager; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 36 | import android.os.Handler; |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 37 | import android.os.UserManager; |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 38 | import android.text.TextUtils; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 39 | import android.view.View; |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 40 | import android.view.ViewGroup; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 41 | import android.widget.Button; |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 42 | import android.widget.FrameLayout; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 43 | import android.widget.ImageView; |
| 44 | import android.widget.TextView; |
| 45 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 46 | import androidx.appcompat.app.AlertDialog; |
| 47 | import androidx.fragment.app.DialogFragment; |
| 48 | import androidx.fragment.app.FragmentManager; |
| 49 | |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 50 | import com.android.internal.widget.LockPatternUtils; |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 51 | import com.android.settings.R; |
| 52 | import com.android.settings.Utils; |
tmfang | 99cc23d | 2018-06-26 19:01:57 +0800 | [diff] [blame] | 53 | import com.android.settings.core.InstrumentedFragment; |
| 54 | |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Base fragment to be shared for PIN/Pattern/Password confirmation fragments. |
| 57 | */ |
Kevin Chyn | e5a016e | 2018-10-03 18:39:22 -0700 | [diff] [blame] | 58 | public abstract class ConfirmDeviceCredentialBaseFragment extends InstrumentedFragment { |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 59 | |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 60 | public static final String TITLE_TEXT = SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.title"; |
| 61 | public static final String HEADER_TEXT = SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.header"; |
| 62 | public static final String DETAILS_TEXT = SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.details"; |
| 63 | public static final String DARK_THEME = SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.darkTheme"; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 64 | public static final String SHOW_CANCEL_BUTTON = |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 65 | SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.showCancelButton"; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 66 | public static final String SHOW_WHEN_LOCKED = |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 67 | SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.showWhenLocked"; |
Kevin Chyn | b3ee231 | 2018-10-04 15:01:43 -0700 | [diff] [blame] | 68 | public static final String USE_FADE_ANIMATION = |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 69 | SETTINGS_PACKAGE_NAME + ".ConfirmCredentials.useFadeAnimation"; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 70 | |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 71 | protected static final int USER_TYPE_PRIMARY = 1; |
| 72 | protected static final int USER_TYPE_MANAGED_PROFILE = 2; |
| 73 | protected static final int USER_TYPE_SECONDARY = 3; |
| 74 | |
Charles He | caf9510 | 2017-08-18 17:35:27 +0100 | [diff] [blame] | 75 | /** Time we wait before clearing a wrong input attempt (e.g. pattern) and the error message. */ |
| 76 | protected static final long CLEAR_WRONG_ATTEMPT_TIMEOUT_MS = 3000; |
| 77 | |
Victor Chang | 5e0a46b | 2016-05-13 17:06:59 +0100 | [diff] [blame] | 78 | protected boolean mReturnCredentials = false; |
Jorim Jaggi | ff41a9a | 2015-06-09 15:31:28 -0700 | [diff] [blame] | 79 | protected Button mCancelButton; |
Clara Bayarri | 40db4bb | 2016-01-07 17:10:27 +0000 | [diff] [blame] | 80 | protected int mEffectiveUserId; |
Ricky Wai | 996d0df | 2016-04-05 16:33:47 +0100 | [diff] [blame] | 81 | protected int mUserId; |
Andrew Scull | 783870c | 2017-03-15 12:42:18 +0000 | [diff] [blame] | 82 | protected UserManager mUserManager; |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 83 | protected LockPatternUtils mLockPatternUtils; |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 84 | protected DevicePolicyManager mDevicePolicyManager; |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 85 | protected TextView mErrorTextView; |
| 86 | protected final Handler mHandler = new Handler(); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 87 | protected boolean mFrp; |
| 88 | private CharSequence mFrpAlternateButtonText; |
Kevin Chyn | e56df88 | 2019-01-24 19:42:05 -0800 | [diff] [blame] | 89 | protected BiometricManager mBiometricManager; |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 90 | |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 91 | private boolean isInternalActivity() { |
| 92 | return (getActivity() instanceof ConfirmLockPassword.InternalActivity) |
| 93 | || (getActivity() instanceof ConfirmLockPattern.InternalActivity); |
| 94 | } |
| 95 | |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public void onCreate(@Nullable Bundle savedInstanceState) { |
| 98 | super.onCreate(savedInstanceState); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 99 | mFrpAlternateButtonText = getActivity().getIntent().getCharSequenceExtra( |
| 100 | KeyguardManager.EXTRA_ALTERNATE_BUTTON_LABEL); |
Victor Chang | 5e0a46b | 2016-05-13 17:06:59 +0100 | [diff] [blame] | 101 | mReturnCredentials = getActivity().getIntent().getBooleanExtra( |
| 102 | ChooseLockSettingsHelper.EXTRA_KEY_RETURN_CREDENTIALS, false); |
Clara Bayarri | 40db4bb | 2016-01-07 17:10:27 +0000 | [diff] [blame] | 103 | // Only take this argument into account if it belongs to the current profile. |
| 104 | Intent intent = getActivity().getIntent(); |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 105 | mUserId = Utils.getUserIdFromBundle(getActivity(), intent.getExtras(), |
| 106 | isInternalActivity()); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 107 | mFrp = (mUserId == LockPatternUtils.USER_FRP); |
Andrew Scull | 783870c | 2017-03-15 12:42:18 +0000 | [diff] [blame] | 108 | mUserManager = UserManager.get(getActivity()); |
| 109 | mEffectiveUserId = mUserManager.getCredentialOwnerProfile(mUserId); |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 110 | mLockPatternUtils = new LockPatternUtils(getActivity()); |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 111 | mDevicePolicyManager = (DevicePolicyManager) getActivity().getSystemService( |
| 112 | Context.DEVICE_POLICY_SERVICE); |
Kevin Chyn | e56df88 | 2019-01-24 19:42:05 -0800 | [diff] [blame] | 113 | mBiometricManager = getActivity().getSystemService(BiometricManager.class); |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| 118 | super.onViewCreated(view, savedInstanceState); |
Jorim Jaggi | ff41a9a | 2015-06-09 15:31:28 -0700 | [diff] [blame] | 119 | mCancelButton = (Button) view.findViewById(R.id.cancelButton); |
Kevin Chyn | e5a016e | 2018-10-03 18:39:22 -0700 | [diff] [blame] | 120 | |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 121 | boolean showCancelButton = getActivity().getIntent().getBooleanExtra( |
| 122 | SHOW_CANCEL_BUTTON, false); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 123 | boolean hasAlternateButton = mFrp && !TextUtils.isEmpty(mFrpAlternateButtonText); |
| 124 | mCancelButton.setVisibility(showCancelButton || hasAlternateButton |
| 125 | ? View.VISIBLE : View.GONE); |
| 126 | if (hasAlternateButton) { |
| 127 | mCancelButton.setText(mFrpAlternateButtonText); |
| 128 | } |
Jorim Jaggi | ff41a9a | 2015-06-09 15:31:28 -0700 | [diff] [blame] | 129 | mCancelButton.setOnClickListener(new View.OnClickListener() { |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void onClick(View v) { |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 132 | if (hasAlternateButton) { |
| 133 | getActivity().setResult(KeyguardManager.RESULT_ALTERNATE); |
| 134 | } |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 135 | getActivity().finish(); |
| 136 | } |
| 137 | }); |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 138 | int credentialOwnerUserId = Utils.getCredentialOwnerUserId( |
| 139 | getActivity(), |
| 140 | Utils.getUserIdFromBundle( |
| 141 | getActivity(), |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 142 | getActivity().getIntent().getExtras(), isInternalActivity())); |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Victor Chang | 5e0a46b | 2016-05-13 17:06:59 +0100 | [diff] [blame] | 145 | // User could be locked while Effective user is unlocked even though the effective owns the |
| 146 | // credential. Otherwise, fingerprint can't unlock fbe/keystore through |
| 147 | // verifyTiedProfileChallenge. In such case, we also wanna show the user message that |
| 148 | // fingerprint is disabled due to device restart. |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 149 | protected boolean isStrongAuthRequired() { |
| 150 | return mFrp |
Kevin Chyn | e5a016e | 2018-10-03 18:39:22 -0700 | [diff] [blame] | 151 | || !mLockPatternUtils.isBiometricAllowedForUser(mEffectiveUserId) |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 152 | || !mUserManager.isUserUnlocked(mUserId); |
Daniel U | 6655e1b | 2016-04-07 19:18:26 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void onResume() { |
| 157 | super.onResume(); |
Ricky Wai | 9aa434c | 2016-06-27 11:10:09 +0100 | [diff] [blame] | 158 | refreshLockScreen(); |
| 159 | } |
| 160 | |
| 161 | protected void refreshLockScreen() { |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 162 | updateErrorMessage(mLockPatternUtils.getCurrentFailedPasswordAttempts(mEffectiveUserId)); |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Julia Reynolds | 017ae50 | 2016-03-16 11:40:54 -0400 | [diff] [blame] | 165 | protected void setAccessibilityTitle(CharSequence supplementalText) { |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 166 | Intent intent = getActivity().getIntent(); |
| 167 | if (intent != null) { |
| 168 | CharSequence titleText = intent.getCharSequenceExtra( |
| 169 | ConfirmDeviceCredentialBaseFragment.TITLE_TEXT); |
Doris Ling | 30e348b | 2016-08-03 17:26:34 -0700 | [diff] [blame] | 170 | if (supplementalText == null) { |
Julia Reynolds | ddd887a | 2015-07-10 10:07:21 -0400 | [diff] [blame] | 171 | return; |
| 172 | } |
Doris Ling | 30e348b | 2016-08-03 17:26:34 -0700 | [diff] [blame] | 173 | if (titleText == null) { |
| 174 | getActivity().setTitle(supplementalText); |
| 175 | } else { |
| 176 | String accessibilityTitle = |
| 177 | new StringBuilder(titleText).append(",").append(supplementalText).toString(); |
| 178 | getActivity().setTitle(Utils.createAccessibleSequence(titleText, accessibilityTitle)); |
| 179 | } |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public void onPause() { |
| 185 | super.onPause(); |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Andres Morales | 59f5976 | 2015-04-16 16:30:31 -0700 | [diff] [blame] | 188 | protected abstract void authenticationSucceeded(); |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 189 | |
Jorim Jaggi | ff41a9a | 2015-06-09 15:31:28 -0700 | [diff] [blame] | 190 | |
| 191 | public void prepareEnterAnimation() { |
| 192 | } |
| 193 | |
| 194 | public void startEnterAnimation() { |
| 195 | } |
Clara Bayarri | c5cde05 | 2015-10-22 17:24:43 +0100 | [diff] [blame] | 196 | |
Benjamin Franz | cc8d37a | 2016-01-18 17:31:26 +0000 | [diff] [blame] | 197 | private void setWorkChallengeBackground(View baseView, int userId) { |
Abodunrinwa Toki | 80c082f | 2016-03-08 18:07:35 +0000 | [diff] [blame] | 198 | View mainContent = getActivity().findViewById(com.android.settings.R.id.main_content); |
| 199 | if (mainContent != null) { |
| 200 | // Remove the main content padding so that the background image is full screen. |
| 201 | mainContent.setPadding(0, 0, 0, 0); |
| 202 | } |
| 203 | |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 204 | baseView.setBackground( |
| 205 | new ColorDrawable(mDevicePolicyManager.getOrganizationColorForUser(userId))); |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 206 | ImageView imageView = (ImageView) baseView.findViewById(R.id.background_image); |
| 207 | if (imageView != null) { |
| 208 | Drawable image = getResources().getDrawable(R.drawable.work_challenge_background); |
| 209 | image.setColorFilter( |
| 210 | getResources().getColor(R.color.confirm_device_credential_transparent_black), |
| 211 | PorterDuff.Mode.DARKEN); |
| 212 | imageView.setImageDrawable(image); |
| 213 | Point screenSize = new Point(); |
| 214 | getActivity().getWindowManager().getDefaultDisplay().getSize(screenSize); |
| 215 | imageView.setLayoutParams(new FrameLayout.LayoutParams( |
| 216 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 217 | screenSize.y)); |
| 218 | } |
| 219 | } |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 220 | |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 221 | protected void reportFailedAttempt() { |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 222 | updateErrorMessage( |
| 223 | mLockPatternUtils.getCurrentFailedPasswordAttempts(mEffectiveUserId) + 1); |
| 224 | mLockPatternUtils.reportFailedPasswordAttempt(mEffectiveUserId); |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | protected void updateErrorMessage(int numAttempts) { |
| 228 | final int maxAttempts = |
| 229 | mLockPatternUtils.getMaximumFailedPasswordsForWipe(mEffectiveUserId); |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 230 | if (maxAttempts <= 0 || numAttempts <= 0) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | // Update the on-screen error string |
| 235 | if (mErrorTextView != null) { |
| 236 | final String message = getActivity().getString( |
| 237 | R.string.lock_failed_attempts_before_wipe, numAttempts, maxAttempts); |
| 238 | showError(message, 0); |
| 239 | } |
| 240 | |
| 241 | // Only show popup dialog before the last attempt and before wipe |
| 242 | final int remainingAttempts = maxAttempts - numAttempts; |
| 243 | if (remainingAttempts > 1) { |
| 244 | return; |
| 245 | } |
| 246 | final FragmentManager fragmentManager = getChildFragmentManager(); |
| 247 | final int userType = getUserTypeForWipe(); |
| 248 | if (remainingAttempts == 1) { |
| 249 | // Last try |
| 250 | final String title = getActivity().getString( |
| 251 | R.string.lock_last_attempt_before_wipe_warning_title); |
| 252 | final int messageId = getLastTryErrorMessage(userType); |
| 253 | LastTryDialog.show(fragmentManager, title, messageId, |
| 254 | android.R.string.ok, false /* dismiss */); |
| 255 | } else { |
| 256 | // Device, profile, or secondary user is wiped |
| 257 | final int messageId = getWipeMessage(userType); |
| 258 | LastTryDialog.show(fragmentManager, null /* title */, messageId, |
| 259 | R.string.lock_failed_attempts_now_wiping_dialog_dismiss, true /* dismiss */); |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 263 | private int getUserTypeForWipe() { |
| 264 | final UserInfo userToBeWiped = mUserManager.getUserInfo( |
| 265 | mDevicePolicyManager.getProfileWithMinimumFailedPasswordsForWipe(mEffectiveUserId)); |
| 266 | if (userToBeWiped == null || userToBeWiped.isPrimary()) { |
| 267 | return USER_TYPE_PRIMARY; |
| 268 | } else if (userToBeWiped.isManagedProfile()) { |
| 269 | return USER_TYPE_MANAGED_PROFILE; |
| 270 | } else { |
| 271 | return USER_TYPE_SECONDARY; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | protected abstract int getLastTryErrorMessage(int userType); |
| 276 | |
| 277 | private int getWipeMessage(int userType) { |
| 278 | switch (userType) { |
| 279 | case USER_TYPE_PRIMARY: |
| 280 | return R.string.lock_failed_attempts_now_wiping_device; |
| 281 | case USER_TYPE_MANAGED_PROFILE: |
| 282 | return R.string.lock_failed_attempts_now_wiping_profile; |
| 283 | case USER_TYPE_SECONDARY: |
| 284 | return R.string.lock_failed_attempts_now_wiping_user; |
| 285 | default: |
| 286 | throw new IllegalArgumentException("Unrecognized user type:" + userType); |
| 287 | } |
| 288 | } |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 289 | |
| 290 | private final Runnable mResetErrorRunnable = new Runnable() { |
| 291 | @Override |
| 292 | public void run() { |
| 293 | mErrorTextView.setText(""); |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | protected void showError(CharSequence msg, long timeout) { |
| 298 | mErrorTextView.setText(msg); |
| 299 | onShowError(); |
| 300 | mHandler.removeCallbacks(mResetErrorRunnable); |
| 301 | if (timeout != 0) { |
| 302 | mHandler.postDelayed(mResetErrorRunnable, timeout); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | protected abstract void onShowError(); |
| 307 | |
| 308 | protected void showError(int msg, long timeout) { |
| 309 | showError(getText(msg), timeout); |
| 310 | } |
| 311 | |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 312 | public static class LastTryDialog extends DialogFragment { |
| 313 | private static final String TAG = LastTryDialog.class.getSimpleName(); |
| 314 | |
| 315 | private static final String ARG_TITLE = "title"; |
| 316 | private static final String ARG_MESSAGE = "message"; |
| 317 | private static final String ARG_BUTTON = "button"; |
| 318 | private static final String ARG_DISMISS = "dismiss"; |
| 319 | |
| 320 | static boolean show(FragmentManager from, String title, int message, int button, |
| 321 | boolean dismiss) { |
| 322 | LastTryDialog existent = (LastTryDialog) from.findFragmentByTag(TAG); |
| 323 | if (existent != null && !existent.isRemoving()) { |
| 324 | return false; |
| 325 | } |
| 326 | Bundle args = new Bundle(); |
| 327 | args.putString(ARG_TITLE, title); |
| 328 | args.putInt(ARG_MESSAGE, message); |
| 329 | args.putInt(ARG_BUTTON, button); |
| 330 | args.putBoolean(ARG_DISMISS, dismiss); |
| 331 | |
| 332 | DialogFragment dialog = new LastTryDialog(); |
| 333 | dialog.setArguments(args); |
| 334 | dialog.show(from, TAG); |
Charles He | 641c9fc | 2017-04-26 18:45:48 +0100 | [diff] [blame] | 335 | from.executePendingTransactions(); |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
| 339 | static void hide(FragmentManager from) { |
| 340 | LastTryDialog dialog = (LastTryDialog) from.findFragmentByTag(TAG); |
| 341 | if (dialog != null) { |
| 342 | dialog.dismissAllowingStateLoss(); |
| 343 | from.executePendingTransactions(); |
| 344 | } |
| 345 | } |
| 346 | |
Robin Lee | 11bf780 | 2017-02-10 16:12:20 +0000 | [diff] [blame] | 347 | /** |
| 348 | * Dialog setup. |
| 349 | * <p> |
| 350 | * To make it less likely that the dialog is dismissed accidentally, for example if the |
| 351 | * device is malfunctioning or if the device is in a pocket, we set |
| 352 | * {@code setCanceledOnTouchOutside(false)}. |
| 353 | */ |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 354 | @Override |
| 355 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Robin Lee | 11bf780 | 2017-02-10 16:12:20 +0000 | [diff] [blame] | 356 | Dialog dialog = new AlertDialog.Builder(getActivity()) |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 357 | .setTitle(getArguments().getString(ARG_TITLE)) |
| 358 | .setMessage(getArguments().getInt(ARG_MESSAGE)) |
| 359 | .setPositiveButton(getArguments().getInt(ARG_BUTTON), null) |
| 360 | .create(); |
Robin Lee | 11bf780 | 2017-02-10 16:12:20 +0000 | [diff] [blame] | 361 | dialog.setCanceledOnTouchOutside(false); |
| 362 | return dialog; |
Robin Lee | c1cb8a4 | 2017-02-06 18:19:40 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void onDismiss(final DialogInterface dialog) { |
| 367 | super.onDismiss(dialog); |
| 368 | if (getActivity() != null && getArguments().getBoolean(ARG_DISMISS)) { |
| 369 | getActivity().finish(); |
| 370 | } |
| 371 | } |
Clara Bayarri | 9d357ea | 2016-01-28 17:50:53 +0000 | [diff] [blame] | 372 | } |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 373 | } |