• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.fingerprint;
18 
19 import android.annotation.Nullable;
20 import android.app.Activity;
21 import android.app.Fragment;
22 import android.hardware.fingerprint.FingerprintManager;
23 import android.os.Bundle;
24 import android.os.CancellationSignal;
25 import android.os.Handler;
26 
27 import com.android.internal.logging.MetricsLogger;
28 import com.android.settings.ChooseLockSettingsHelper;
29 import com.android.settings.InstrumentedFragment;
30 
31 /**
32  * Sidecar fragment to handle the state around fingerprint enrollment.
33  */
34 public class FingerprintEnrollSidecar extends InstrumentedFragment {
35 
36     private int mEnrollmentSteps = -1;
37     private int mEnrollmentRemaining = 0;
38     private Listener mListener;
39     private boolean mEnrolling;
40     private CancellationSignal mEnrollmentCancel;
41     private Handler mHandler = new Handler();
42     private byte[] mToken;
43     private boolean mDone;
44 
45     @Override
onCreate(@ullable Bundle savedInstanceState)46     public void onCreate(@Nullable Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         setRetainInstance(true);
49     }
50 
51     @Override
onAttach(Activity activity)52     public void onAttach(Activity activity) {
53         super.onAttach(activity);
54         mToken = activity.getIntent().getByteArrayExtra(
55                 ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
56     }
57 
58     @Override
onStart()59     public void onStart() {
60         super.onStart();
61         if (!mEnrolling) {
62             startEnrollment();
63         }
64     }
65 
66     @Override
onStop()67     public void onStop() {
68         super.onStop();
69         if (!getActivity().isChangingConfigurations()) {
70             cancelEnrollment();
71         }
72     }
73 
startEnrollment()74     private void startEnrollment() {
75         mHandler.removeCallbacks(mTimeoutRunnable);
76         mEnrollmentSteps = -1;
77         mEnrollmentCancel = new CancellationSignal();
78         getActivity().getSystemService(FingerprintManager.class).enroll(mToken, mEnrollmentCancel,
79                 0 /* flags */, mEnrollmentCallback);
80         mEnrolling = true;
81     }
82 
cancelEnrollment()83     private void cancelEnrollment() {
84         mHandler.removeCallbacks(mTimeoutRunnable);
85         if (mEnrolling) {
86             mEnrollmentCancel.cancel();
87             mEnrolling = false;
88             mEnrollmentSteps = -1;
89         }
90     }
91 
setListener(Listener listener)92     public void setListener(Listener listener) {
93         mListener = listener;
94     }
95 
getEnrollmentSteps()96     public int getEnrollmentSteps() {
97         return mEnrollmentSteps;
98     }
99 
getEnrollmentRemaining()100     public int getEnrollmentRemaining() {
101         return mEnrollmentRemaining;
102     }
103 
isDone()104     public boolean isDone() {
105         return mDone;
106     }
107 
108     private FingerprintManager.EnrollmentCallback mEnrollmentCallback
109             = new FingerprintManager.EnrollmentCallback() {
110 
111         @Override
112         public void onEnrollmentProgress(int remaining) {
113             if (mEnrollmentSteps == -1) {
114                 mEnrollmentSteps = remaining;
115             }
116             mEnrollmentRemaining = remaining;
117             mDone = remaining == 0;
118             if (mListener != null) {
119                 mListener.onEnrollmentProgressChange(mEnrollmentSteps, remaining);
120             }
121         }
122 
123         @Override
124         public void onEnrollmentHelp(int helpMsgId, CharSequence helpString) {
125             if (mListener != null) {
126                 mListener.onEnrollmentHelp(helpString);
127             }
128         }
129 
130         @Override
131         public void onEnrollmentError(int errMsgId, CharSequence errString) {
132             if (mListener != null) {
133                 mListener.onEnrollmentError(errMsgId, errString);
134             }
135         }
136     };
137 
138     private final Runnable mTimeoutRunnable = new Runnable() {
139         @Override
140         public void run() {
141             cancelEnrollment();
142         }
143     };
144 
145     @Override
getMetricsCategory()146     protected int getMetricsCategory() {
147         return MetricsLogger.FINGERPRINT_ENROLL_SIDECAR;
148     }
149 
150     public interface Listener {
onEnrollmentHelp(CharSequence helpString)151         void onEnrollmentHelp(CharSequence helpString);
onEnrollmentError(int errMsgId, CharSequence errString)152         void onEnrollmentError(int errMsgId, CharSequence errString);
onEnrollmentProgressChange(int steps, int remaining)153         void onEnrollmentProgressChange(int steps, int remaining);
154     }
155 }
156