1 /* 2 * Copyright (C) 2018 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.biometrics; 18 19 import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME; 20 21 import android.annotation.Nullable; 22 import android.annotation.SuppressLint; 23 import android.content.Intent; 24 import android.content.res.ColorStateList; 25 import android.graphics.Color; 26 import android.os.Bundle; 27 import android.os.UserHandle; 28 import android.text.TextUtils; 29 import android.util.Log; 30 import android.view.View; 31 import android.widget.LinearLayout; 32 import android.widget.TextView; 33 34 import androidx.annotation.ColorInt; 35 36 import com.android.settings.R; 37 import com.android.settings.SetupWizardUtils; 38 import com.android.settings.Utils; 39 import com.android.settings.biometrics.fingerprint.FingerprintEnrollEnrolling; 40 import com.android.settings.core.InstrumentedActivity; 41 import com.android.settings.overlay.FeatureFactory; 42 import com.android.settings.password.ChooseLockSettingsHelper; 43 import com.android.systemui.unfold.compat.ScreenSizeFoldProvider; 44 import com.android.systemui.unfold.updates.FoldProvider; 45 46 import com.google.android.setupcompat.template.FooterBarMixin; 47 import com.google.android.setupcompat.template.FooterButton; 48 import com.google.android.setupcompat.util.WizardManagerHelper; 49 import com.google.android.setupdesign.GlifLayout; 50 import com.google.android.setupdesign.util.ThemeHelper; 51 52 /** 53 * Base activity for all biometric enrollment steps. 54 */ 55 public abstract class BiometricEnrollBase extends InstrumentedActivity { 56 57 private static final String TAG = "BiometricEnrollBase"; 58 59 public static final String EXTRA_FROM_SETTINGS_SUMMARY = "from_settings_summary"; 60 public static final String EXTRA_KEY_LAUNCHED_CONFIRM = "launched_confirm_lock"; 61 public static final String EXTRA_KEY_REQUIRE_VISION = "accessibility_vision"; 62 public static final String EXTRA_KEY_REQUIRE_DIVERSITY = "accessibility_diversity"; 63 public static final String EXTRA_KEY_SENSOR_ID = "sensor_id"; 64 public static final String EXTRA_KEY_CHALLENGE = "challenge"; 65 public static final String EXTRA_KEY_MODALITY = "sensor_modality"; 66 public static final String EXTRA_KEY_NEXT_LAUNCHED = "next_launched"; 67 public static final String EXTRA_FINISHED_ENROLL_FACE = "finished_enrolling_face"; 68 public static final String EXTRA_FINISHED_ENROLL_FINGERPRINT = "finished_enrolling_fingerprint"; 69 public static final String EXTRA_LAUNCHED_POSTURE_GUIDANCE = "launched_posture_guidance"; 70 71 /** 72 * Used by the choose fingerprint wizard to indicate the wizard is 73 * finished, and each activity in the wizard should finish. 74 * <p> 75 * Previously, each activity in the wizard would finish itself after 76 * starting the next activity. However, this leads to broken 'Back' 77 * behavior. So, now an activity does not finish itself until it gets this 78 * result. 79 * 80 * This must be the same as 81 * {@link com.android.settings.password.ChooseLockPattern#RESULT_FINISHED} 82 */ 83 public static final int RESULT_FINISHED = RESULT_FIRST_USER; 84 85 /** 86 * Used by the enrolling screen during setup wizard to skip over setting up fingerprint, which 87 * will be useful if the user accidentally entered this flow. 88 */ 89 public static final int RESULT_SKIP = RESULT_FIRST_USER + 1; 90 91 /** 92 * Like {@link #RESULT_FINISHED} except this one indicates enrollment failed because the 93 * device was left idle. This is used to clear the credential token to require the user to 94 * re-enter their pin/pattern/password before continuing. 95 */ 96 public static final int RESULT_TIMEOUT = RESULT_FIRST_USER + 2; 97 98 /** 99 * Used by consent screens to indicate that consent was granted. Extras, such as 100 * EXTRA_KEY_MODALITY, will be included in the result to provide details about the 101 * consent that was granted. 102 */ 103 public static final int RESULT_CONSENT_GRANTED = RESULT_FIRST_USER + 3; 104 105 /** 106 * Used by consent screens to indicate that consent was denied. Extras, such as 107 * EXTRA_KEY_MODALITY, will be included in the result to provide details about the 108 * consent that was not granted. 109 */ 110 public static final int RESULT_CONSENT_DENIED = RESULT_FIRST_USER + 4; 111 112 public static final int CHOOSE_LOCK_GENERIC_REQUEST = 1; 113 public static final int BIOMETRIC_FIND_SENSOR_REQUEST = 2; 114 public static final int LEARN_MORE_REQUEST = 3; 115 public static final int CONFIRM_REQUEST = 4; 116 public static final int ENROLL_REQUEST = 5; 117 118 /** 119 * Request code when starting another biometric enrollment from within a biometric flow. For 120 * example, when starting fingerprint enroll after face enroll. 121 */ 122 public static final int ENROLL_NEXT_BIOMETRIC_REQUEST = 6; 123 public static final int REQUEST_POSTURE_GUIDANCE = 7; 124 125 protected boolean mLaunchedConfirmLock; 126 protected boolean mLaunchedPostureGuidance; 127 protected boolean mNextLaunched; 128 protected byte[] mToken; 129 protected int mUserId; 130 protected int mSensorId; 131 @BiometricUtils.DevicePostureInt 132 protected int mDevicePostureState; 133 protected long mChallenge; 134 protected boolean mFromSettingsSummary; 135 protected FooterBarMixin mFooterBarMixin; 136 protected boolean mShouldSetFooterBarBackground = true; 137 @Nullable 138 protected ScreenSizeFoldProvider mScreenSizeFoldProvider; 139 @Nullable 140 protected Intent mPostureGuidanceIntent = null; 141 @Nullable 142 protected FoldProvider.FoldCallback mFoldCallback = null; 143 144 @Override onCreate(Bundle savedInstanceState)145 protected void onCreate(Bundle savedInstanceState) { 146 super.onCreate(savedInstanceState); 147 setTheme(SetupWizardUtils.getTheme(this, getIntent())); 148 ThemeHelper.trySetDynamicColor(this); 149 mChallenge = getIntent().getLongExtra(EXTRA_KEY_CHALLENGE, -1L); 150 mSensorId = getIntent().getIntExtra(EXTRA_KEY_SENSOR_ID, -1); 151 // Don't need to retrieve the HAT if it already exists. In some cases, the extras do not 152 // contain EXTRA_KEY_CHALLENGE_TOKEN but contain EXTRA_KEY_GK_PW, in which case enrollment 153 // classes may request a HAT to be created (as opposed to being passed in) 154 if (mToken == null) { 155 mToken = getIntent().getByteArrayExtra( 156 ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN); 157 } 158 mFromSettingsSummary = getIntent().getBooleanExtra(EXTRA_FROM_SETTINGS_SUMMARY, false); 159 if (savedInstanceState != null) { 160 if (mToken == null) { 161 mLaunchedConfirmLock = savedInstanceState.getBoolean(EXTRA_KEY_LAUNCHED_CONFIRM); 162 mToken = savedInstanceState.getByteArray( 163 ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN); 164 mFromSettingsSummary = 165 savedInstanceState.getBoolean(EXTRA_FROM_SETTINGS_SUMMARY, false); 166 mChallenge = savedInstanceState.getLong(EXTRA_KEY_CHALLENGE); 167 mSensorId = savedInstanceState.getInt(EXTRA_KEY_SENSOR_ID); 168 } 169 mLaunchedPostureGuidance = savedInstanceState.getBoolean( 170 EXTRA_LAUNCHED_POSTURE_GUIDANCE); 171 mNextLaunched = savedInstanceState.getBoolean(EXTRA_KEY_NEXT_LAUNCHED); 172 } 173 mUserId = getIntent().getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId()); 174 mPostureGuidanceIntent = FeatureFactory.getFactory(getApplicationContext()) 175 .getFaceFeatureProvider().getPostureGuidanceIntent(getApplicationContext()); 176 } 177 178 @Override onSaveInstanceState(Bundle outState)179 protected void onSaveInstanceState(Bundle outState) { 180 super.onSaveInstanceState(outState); 181 outState.putBoolean(EXTRA_KEY_LAUNCHED_CONFIRM, mLaunchedConfirmLock); 182 outState.putByteArray(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, mToken); 183 outState.putBoolean(EXTRA_FROM_SETTINGS_SUMMARY, mFromSettingsSummary); 184 outState.putLong(EXTRA_KEY_CHALLENGE, mChallenge); 185 outState.putInt(EXTRA_KEY_SENSOR_ID, mSensorId); 186 outState.putBoolean(EXTRA_LAUNCHED_POSTURE_GUIDANCE, mLaunchedPostureGuidance); 187 outState.putBoolean(EXTRA_KEY_NEXT_LAUNCHED, mNextLaunched); 188 } 189 190 @Override onPostCreate(@ullable Bundle savedInstanceState)191 protected void onPostCreate(@Nullable Bundle savedInstanceState) { 192 super.onPostCreate(savedInstanceState); 193 initViews(); 194 195 if (mShouldSetFooterBarBackground) { 196 @SuppressLint("VisibleForTests") 197 final LinearLayout buttonContainer = mFooterBarMixin != null 198 ? mFooterBarMixin.getButtonContainer() 199 : null; 200 if (buttonContainer != null) { 201 buttonContainer.setBackgroundColor(getBackgroundColor()); 202 } 203 } 204 } 205 206 @Override onAttachedToWindow()207 public void onAttachedToWindow() { 208 super.onAttachedToWindow(); 209 getWindow().setStatusBarColor(getBackgroundColor()); 210 } 211 212 @Override onStop()213 protected void onStop() { 214 super.onStop(); 215 if (mScreenSizeFoldProvider != null && mFoldCallback != null) { 216 mScreenSizeFoldProvider.unregisterCallback(mFoldCallback); 217 } 218 mScreenSizeFoldProvider = null; 219 mFoldCallback = null; 220 221 if (!isChangingConfigurations() && shouldFinishWhenBackgrounded() 222 && !BiometricUtils.isAnyMultiBiometricFlow(this)) { 223 setResult(RESULT_TIMEOUT); 224 finish(); 225 } 226 } 227 launchPostureGuidance()228 protected boolean launchPostureGuidance() { 229 if (mPostureGuidanceIntent == null || mLaunchedPostureGuidance) { 230 return false; 231 } 232 BiometricUtils.copyMultiBiometricExtras(getIntent(), mPostureGuidanceIntent); 233 startActivityForResult(mPostureGuidanceIntent, REQUEST_POSTURE_GUIDANCE); 234 mLaunchedPostureGuidance = true; 235 overridePendingTransition(0 /* no enter anim */, 0 /* no exit anim */); 236 return mLaunchedPostureGuidance; 237 } 238 shouldFinishWhenBackgrounded()239 protected boolean shouldFinishWhenBackgrounded() { 240 return !WizardManagerHelper.isAnySetupWizard(getIntent()); 241 } 242 initViews()243 protected void initViews() { 244 getWindow().setStatusBarColor(Color.TRANSPARENT); 245 } 246 getLayout()247 protected GlifLayout getLayout() { 248 return (GlifLayout) findViewById(R.id.setup_wizard_layout); 249 } 250 setHeaderText(int resId, boolean force)251 protected void setHeaderText(int resId, boolean force) { 252 TextView layoutTitle = getLayout().getHeaderTextView(); 253 CharSequence previousTitle = layoutTitle.getText(); 254 CharSequence title = getText(resId); 255 if (previousTitle != title || force) { 256 if (!TextUtils.isEmpty(previousTitle)) { 257 layoutTitle.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); 258 } 259 getLayout().setHeaderText(title); 260 getLayout().getHeaderTextView().setContentDescription(title); 261 setTitle(title); 262 } 263 } 264 setHeaderText(int resId)265 protected void setHeaderText(int resId) { 266 setHeaderText(resId, false /* force */); 267 getLayout().getHeaderTextView().setContentDescription(getText(resId)); 268 } 269 setHeaderText(CharSequence title)270 protected void setHeaderText(CharSequence title) { 271 getLayout().setHeaderText(title); 272 getLayout().getHeaderTextView().setContentDescription(title); 273 } 274 setDescriptionText(int resId)275 protected void setDescriptionText(int resId) { 276 CharSequence previousDescription = getLayout().getDescriptionText(); 277 CharSequence description = getString(resId); 278 // Prevent a11y for re-reading the same string 279 if (!TextUtils.equals(previousDescription, description)) { 280 getLayout().setDescriptionText(resId); 281 } 282 } 283 setDescriptionText(CharSequence descriptionText)284 protected void setDescriptionText(CharSequence descriptionText) { 285 getLayout().setDescriptionText(descriptionText); 286 } 287 getNextButton()288 protected FooterButton getNextButton() { 289 if (mFooterBarMixin != null) { 290 return mFooterBarMixin.getPrimaryButton(); 291 } 292 return null; 293 } 294 onNextButtonClick(View view)295 protected void onNextButtonClick(View view) { 296 } 297 getFingerprintEnrollingIntent()298 protected Intent getFingerprintEnrollingIntent() { 299 Intent intent = new Intent(); 300 intent.setClassName(SETTINGS_PACKAGE_NAME, FingerprintEnrollEnrolling.class.getName()); 301 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, mToken); 302 intent.putExtra(EXTRA_FROM_SETTINGS_SUMMARY, mFromSettingsSummary); 303 intent.putExtra(EXTRA_KEY_CHALLENGE, mChallenge); 304 intent.putExtra(EXTRA_KEY_SENSOR_ID, mSensorId); 305 BiometricUtils.copyMultiBiometricExtras(getIntent(), intent); 306 if (mUserId != UserHandle.USER_NULL) { 307 intent.putExtra(Intent.EXTRA_USER_ID, mUserId); 308 } 309 return intent; 310 } 311 launchConfirmLock(int titleResId)312 protected void launchConfirmLock(int titleResId) { 313 Log.d(TAG, "launchConfirmLock"); 314 315 final ChooseLockSettingsHelper.Builder builder = new ChooseLockSettingsHelper.Builder(this); 316 builder.setRequestCode(CONFIRM_REQUEST) 317 .setTitle(getString(titleResId)) 318 .setRequestGatekeeperPasswordHandle(true) 319 .setForegroundOnly(true) 320 .setReturnCredentials(true); 321 322 if (mUserId != UserHandle.USER_NULL) { 323 builder.setUserId(mUserId); 324 } 325 326 final boolean launched = builder.show(); 327 if (!launched) { 328 // This shouldn't happen, as we should only end up at this step if a lock thingy is 329 // already set. 330 finish(); 331 } else { 332 mLaunchedConfirmLock = true; 333 } 334 } 335 336 @ColorInt getBackgroundColor()337 public int getBackgroundColor() { 338 final ColorStateList stateList = Utils.getColorAttr(this, android.R.attr.windowBackground); 339 return stateList != null ? stateList.getDefaultColor() : Color.TRANSPARENT; 340 } 341 } 342