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