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 com.android.settings.R;
24 import com.android.settings.password.ChooseLockSettingsHelper;
25 
26 import com.google.android.setupcompat.util.WizardManagerHelper;
27 
28 /**
29  * Abstract base activity which handles the actual enrolling for biometrics.
30  */
31 public abstract class BiometricsEnrollEnrolling extends BiometricEnrollBase
32         implements BiometricEnrollSidecar.Listener {
33 
34     private static final String TAG_SIDECAR = "sidecar";
35 
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         super.onStop();
64 
65         if (mSidecar != null) {
66             mSidecar.setListener(null);
67         }
68 
69         if (!isChangingConfigurations()) {
70             if (mSidecar != null) {
71                 mSidecar.cancelEnrollment();
72                 getSupportFragmentManager()
73                         .beginTransaction().remove(mSidecar).commitAllowingStateLoss();
74             }
75             if (!WizardManagerHelper.isAnySetupWizard(getIntent())) {
76                 setResult(RESULT_TIMEOUT);
77             }
78             finish();
79         }
80     }
81 
82     @Override
shouldFinishWhenBackgrounded()83     protected boolean shouldFinishWhenBackgrounded() {
84         // Prevent super.onStop() from finishing, since we handle this in our onStop().
85         return false;
86     }
87 
88     @Override
onBackPressed()89     public void onBackPressed() {
90         if (mSidecar != null) {
91             mSidecar.setListener(null);
92             mSidecar.cancelEnrollment();
93             getSupportFragmentManager()
94                     .beginTransaction().remove(mSidecar).commitAllowingStateLoss();
95             mSidecar = null;
96         }
97         super.onBackPressed();
98     }
99 
onSkipButtonClick(View view)100     protected void onSkipButtonClick(View view) {
101         setResult(RESULT_SKIP);
102         finish();
103     }
104 
startEnrollment()105     public void startEnrollment() {
106         mSidecar = (BiometricEnrollSidecar) getSupportFragmentManager()
107                 .findFragmentByTag(TAG_SIDECAR);
108         if (mSidecar == null) {
109             mSidecar = getSidecar();
110             getSupportFragmentManager().beginTransaction().add(mSidecar, TAG_SIDECAR)
111                     .commitAllowingStateLoss();
112         }
113         mSidecar.setListener(this);
114     }
115 
launchFinish(byte[] token)116     protected void launchFinish(byte[] token) {
117         Intent intent = getFinishIntent();
118         intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
119                 | Intent.FLAG_ACTIVITY_CLEAR_TOP
120                 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
121         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, token);
122         intent.putExtra(EXTRA_FROM_SETTINGS_SUMMARY, mFromSettingsSummary);
123         if (mUserId != UserHandle.USER_NULL) {
124             intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
125         }
126         startActivity(intent);
127         overridePendingTransition(R.anim.sud_slide_next_in, R.anim.sud_slide_next_out);
128         finish();
129     }
130 
131 }
132