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 android.content.Intent;
20 import android.os.UserHandle;
21 import android.view.View;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.settings.password.ChooseLockSettingsHelper;
26 
27 /**
28  * Abstract base activity which handles the actual enrolling for biometrics.
29  */
30 public abstract class BiometricsEnrollEnrolling extends BiometricEnrollBase
31         implements BiometricEnrollSidecar.Listener {
32 
33     private static final String TAG_SIDECAR = "sidecar";
34 
35     @Nullable
36     protected BiometricEnrollSidecar mSidecar;
37 
38     /**
39      * @return the intent for the finish activity
40      */
getFinishIntent()41     protected abstract Intent getFinishIntent();
42 
43     /**
44      * @return an instance of the biometric enroll sidecar
45      */
getSidecar()46     protected abstract BiometricEnrollSidecar getSidecar();
47 
48     /**
49      * @return true if enrollment should start automatically.
50      */
shouldStartAutomatically()51     protected abstract boolean shouldStartAutomatically();
52 
53     @Override
onStart()54     protected void onStart() {
55         super.onStart();
56         if (shouldStartAutomatically()) {
57             startEnrollment();
58         }
59     }
60 
61     @Override
onStop()62     protected void onStop() {
63         if (mSidecar != null) {
64             mSidecar.setListener(null);
65         }
66         if (!isChangingConfigurations()) {
67             if (mSidecar != null) {
68                 mSidecar.cancelEnrollment();
69                 getSupportFragmentManager()
70                         .beginTransaction().remove(mSidecar).commitAllowingStateLoss();
71             }
72         }
73 
74         super.onStop();
75     }
76 
77     @Override
onBackPressed()78     public void onBackPressed() {
79         cancelEnrollment();
80         super.onBackPressed();
81     }
82 
onSkipButtonClick(View view)83     protected void onSkipButtonClick(View view) {
84         cancelEnrollment();
85         setResult(RESULT_SKIP);
86         finish();
87     }
88 
cancelEnrollment()89     public void cancelEnrollment() {
90         if (mSidecar != null) {
91             mSidecar.setListener(null);
92             mSidecar.cancelEnrollment();
93             getSupportFragmentManager()
94                     .beginTransaction().remove(mSidecar).commitAllowingStateLoss();
95             mSidecar = null;
96         }
97     }
98 
startEnrollment()99     public void startEnrollment() {
100         // If it's in multi window mode, dialog is shown, do not start enrollment.
101         if (shouldShowSplitScreenDialog()) {
102             return;
103         }
104         startEnrollmentInternal();
105     }
106 
startEnrollmentInternal()107     protected void startEnrollmentInternal() {
108         mSidecar = (BiometricEnrollSidecar) getSupportFragmentManager()
109                 .findFragmentByTag(TAG_SIDECAR);
110         if (mSidecar == null) {
111             mSidecar = getSidecar();
112             getSupportFragmentManager().beginTransaction().add(mSidecar, TAG_SIDECAR)
113                     .commitAllowingStateLoss();
114         }
115         mSidecar.setListener(this);
116     }
117 
launchFinish(byte[] token)118     protected void launchFinish(byte[] token) {
119         Intent intent = getFinishIntent();
120         intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
121                 | Intent.FLAG_ACTIVITY_CLEAR_TOP
122                 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
123         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, token);
124         intent.putExtra(BiometricEnrollBase.EXTRA_KEY_SENSOR_ID, mSensorId);
125         intent.putExtra(BiometricEnrollBase.EXTRA_KEY_CHALLENGE, mChallenge);
126         intent.putExtra(EXTRA_FROM_SETTINGS_SUMMARY, mFromSettingsSummary);
127         if (mUserId != UserHandle.USER_NULL) {
128             intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
129         }
130         startActivity(intent);
131         finish();
132     }
133 
134 }
135