• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     @Nullable
137     protected ScreenSizeFoldProvider mScreenSizeFoldProvider;
138     @Nullable
139     protected Intent mPostureGuidanceIntent = null;
140     @Nullable
141     protected FoldProvider.FoldCallback mFoldCallback = null;
142 
143     @Override
onCreate(Bundle savedInstanceState)144     protected void onCreate(Bundle savedInstanceState) {
145         super.onCreate(savedInstanceState);
146         setTheme(SetupWizardUtils.getTheme(this, getIntent()));
147         ThemeHelper.trySetDynamicColor(this);
148         mChallenge = getIntent().getLongExtra(EXTRA_KEY_CHALLENGE, -1L);
149         mSensorId = getIntent().getIntExtra(EXTRA_KEY_SENSOR_ID, -1);
150         // Don't need to retrieve the HAT if it already exists. In some cases, the extras do not
151         // contain EXTRA_KEY_CHALLENGE_TOKEN but contain EXTRA_KEY_GK_PW, in which case enrollment
152         // classes may request a HAT to be created (as opposed to being passed in)
153         if (mToken == null) {
154             mToken = getIntent().getByteArrayExtra(
155                     ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
156         }
157         mFromSettingsSummary = getIntent().getBooleanExtra(EXTRA_FROM_SETTINGS_SUMMARY, false);
158         if (savedInstanceState != null) {
159             if (mToken == null) {
160                 mLaunchedConfirmLock = savedInstanceState.getBoolean(EXTRA_KEY_LAUNCHED_CONFIRM);
161                 mToken = savedInstanceState.getByteArray(
162                         ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
163                 mFromSettingsSummary =
164                         savedInstanceState.getBoolean(EXTRA_FROM_SETTINGS_SUMMARY, false);
165                 mChallenge = savedInstanceState.getLong(EXTRA_KEY_CHALLENGE);
166                 mSensorId = savedInstanceState.getInt(EXTRA_KEY_SENSOR_ID);
167             }
168             mLaunchedPostureGuidance = savedInstanceState.getBoolean(
169                     EXTRA_LAUNCHED_POSTURE_GUIDANCE);
170             mNextLaunched = savedInstanceState.getBoolean(EXTRA_KEY_NEXT_LAUNCHED);
171         }
172         mUserId = getIntent().getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
173         mPostureGuidanceIntent = FeatureFactory.getFactory(getApplicationContext())
174                 .getFaceFeatureProvider().getPostureGuidanceIntent(getApplicationContext());
175     }
176 
177     @Override
onSaveInstanceState(Bundle outState)178     protected void onSaveInstanceState(Bundle outState) {
179         super.onSaveInstanceState(outState);
180         outState.putBoolean(EXTRA_KEY_LAUNCHED_CONFIRM, mLaunchedConfirmLock);
181         outState.putByteArray(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, mToken);
182         outState.putBoolean(EXTRA_FROM_SETTINGS_SUMMARY, mFromSettingsSummary);
183         outState.putLong(EXTRA_KEY_CHALLENGE, mChallenge);
184         outState.putInt(EXTRA_KEY_SENSOR_ID, mSensorId);
185         outState.putBoolean(EXTRA_LAUNCHED_POSTURE_GUIDANCE, mLaunchedPostureGuidance);
186         outState.putBoolean(EXTRA_KEY_NEXT_LAUNCHED, mNextLaunched);
187     }
188 
189     @Override
onPostCreate(@ullable Bundle savedInstanceState)190     protected void onPostCreate(@Nullable Bundle savedInstanceState) {
191         super.onPostCreate(savedInstanceState);
192         initViews();
193 
194         @SuppressLint("VisibleForTests")
195         final LinearLayout buttonContainer = mFooterBarMixin != null
196                 ? mFooterBarMixin.getButtonContainer()
197                 : null;
198         if (buttonContainer != null) {
199             buttonContainer.setBackgroundColor(getBackgroundColor());
200         }
201     }
202 
203     @Override
onAttachedToWindow()204     public void onAttachedToWindow() {
205         super.onAttachedToWindow();
206         getWindow().setStatusBarColor(getBackgroundColor());
207     }
208 
209     @Override
onStop()210     protected void onStop() {
211         super.onStop();
212         if (mScreenSizeFoldProvider != null && mFoldCallback != null) {
213             mScreenSizeFoldProvider.unregisterCallback(mFoldCallback);
214         }
215         mScreenSizeFoldProvider = null;
216         mFoldCallback = null;
217 
218         if (!isChangingConfigurations() && shouldFinishWhenBackgrounded()
219                 && !BiometricUtils.isAnyMultiBiometricFlow(this)) {
220             setResult(RESULT_TIMEOUT);
221             finish();
222         }
223     }
224 
launchPostureGuidance()225     protected boolean launchPostureGuidance() {
226         if (mPostureGuidanceIntent == null || mLaunchedPostureGuidance) {
227             return false;
228         }
229         BiometricUtils.copyMultiBiometricExtras(getIntent(), mPostureGuidanceIntent);
230         startActivityForResult(mPostureGuidanceIntent, REQUEST_POSTURE_GUIDANCE);
231         mLaunchedPostureGuidance = true;
232         overridePendingTransition(0 /* no enter anim */, 0 /* no exit anim */);
233         return mLaunchedPostureGuidance;
234     }
235 
shouldFinishWhenBackgrounded()236     protected boolean shouldFinishWhenBackgrounded() {
237         return !WizardManagerHelper.isAnySetupWizard(getIntent());
238     }
239 
initViews()240     protected void initViews() {
241         getWindow().setStatusBarColor(Color.TRANSPARENT);
242     }
243 
getLayout()244     protected GlifLayout getLayout() {
245         return (GlifLayout) findViewById(R.id.setup_wizard_layout);
246     }
247 
setHeaderText(int resId, boolean force)248     protected void setHeaderText(int resId, boolean force) {
249         TextView layoutTitle = getLayout().getHeaderTextView();
250         CharSequence previousTitle = layoutTitle.getText();
251         CharSequence title = getText(resId);
252         if (previousTitle != title || force) {
253             if (!TextUtils.isEmpty(previousTitle)) {
254                 layoutTitle.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE);
255             }
256             getLayout().setHeaderText(title);
257             getLayout().getHeaderTextView().setContentDescription(title);
258             setTitle(title);
259         }
260     }
261 
setHeaderText(int resId)262     protected void setHeaderText(int resId) {
263         setHeaderText(resId, false /* force */);
264         getLayout().getHeaderTextView().setContentDescription(getText(resId));
265     }
266 
setHeaderText(CharSequence title)267     protected void setHeaderText(CharSequence title) {
268         getLayout().setHeaderText(title);
269         getLayout().getHeaderTextView().setContentDescription(title);
270     }
271 
setDescriptionText(int resId)272     protected void setDescriptionText(int resId) {
273         CharSequence previousDescription = getLayout().getDescriptionText();
274         CharSequence description = getString(resId);
275         // Prevent a11y for re-reading the same string
276         if (!TextUtils.equals(previousDescription, description)) {
277             getLayout().setDescriptionText(resId);
278         }
279     }
280 
setDescriptionText(CharSequence descriptionText)281     protected void setDescriptionText(CharSequence descriptionText) {
282         getLayout().setDescriptionText(descriptionText);
283     }
284 
getNextButton()285     protected FooterButton getNextButton() {
286         if (mFooterBarMixin != null) {
287             return mFooterBarMixin.getPrimaryButton();
288         }
289         return null;
290     }
291 
onNextButtonClick(View view)292     protected void onNextButtonClick(View view) {
293     }
294 
getFingerprintEnrollingIntent()295     protected Intent getFingerprintEnrollingIntent() {
296         Intent intent = new Intent();
297         intent.setClassName(SETTINGS_PACKAGE_NAME, FingerprintEnrollEnrolling.class.getName());
298         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, mToken);
299         intent.putExtra(EXTRA_FROM_SETTINGS_SUMMARY, mFromSettingsSummary);
300         intent.putExtra(EXTRA_KEY_CHALLENGE, mChallenge);
301         intent.putExtra(EXTRA_KEY_SENSOR_ID, mSensorId);
302         BiometricUtils.copyMultiBiometricExtras(getIntent(), intent);
303         if (mUserId != UserHandle.USER_NULL) {
304             intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
305         }
306         return intent;
307     }
308 
launchConfirmLock(int titleResId)309     protected void launchConfirmLock(int titleResId) {
310         Log.d(TAG, "launchConfirmLock");
311 
312         final ChooseLockSettingsHelper.Builder builder = new ChooseLockSettingsHelper.Builder(this);
313         builder.setRequestCode(CONFIRM_REQUEST)
314                 .setTitle(getString(titleResId))
315                 .setRequestGatekeeperPasswordHandle(true)
316                 .setForegroundOnly(true)
317                 .setReturnCredentials(true);
318 
319         if (mUserId != UserHandle.USER_NULL) {
320             builder.setUserId(mUserId);
321         }
322 
323         final boolean launched = builder.show();
324         if (!launched) {
325             // This shouldn't happen, as we should only end up at this step if a lock thingy is
326             // already set.
327             finish();
328         } else {
329             mLaunchedConfirmLock = true;
330         }
331     }
332 
333     @ColorInt
getBackgroundColor()334     private int getBackgroundColor() {
335         final ColorStateList stateList = Utils.getColorAttr(this, android.R.attr.windowBackground);
336         return stateList != null ? stateList.getDefaultColor() : Color.TRANSPARENT;
337     }
338 }
339