1 /*
2  * Copyright (C) 2019 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.systemui.biometrics;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import com.android.internal.widget.LockPatternChecker;
23 import com.android.internal.widget.LockPatternUtils;
24 import com.android.internal.widget.LockPatternView;
25 import com.android.internal.widget.LockscreenCredential;
26 import com.android.systemui.R;
27 
28 import java.util.List;
29 
30 /**
31  * Pattern UI
32  */
33 public class AuthCredentialPatternView extends AuthCredentialView {
34 
35     private LockPatternView mLockPatternView;
36 
37     private class UnlockPatternListener implements LockPatternView.OnPatternListener {
38 
39         @Override
onPatternStart()40         public void onPatternStart() {
41 
42         }
43 
44         @Override
onPatternCleared()45         public void onPatternCleared() {
46 
47         }
48 
49         @Override
onPatternCellAdded(List<LockPatternView.Cell> pattern)50         public void onPatternCellAdded(List<LockPatternView.Cell> pattern) {
51 
52         }
53 
54         @Override
onPatternDetected(List<LockPatternView.Cell> pattern)55         public void onPatternDetected(List<LockPatternView.Cell> pattern) {
56             if (mPendingLockCheck != null) {
57                 mPendingLockCheck.cancel(false);
58             }
59 
60             mLockPatternView.setEnabled(false);
61 
62             if (pattern.size() < LockPatternUtils.MIN_PATTERN_REGISTER_FAIL) {
63                 // Pattern size is less than the minimum, do not count it as a failed attempt.
64                 onPatternVerified(null /* attestation */, 0 /* timeoutMs */);
65                 return;
66             }
67 
68             try (LockscreenCredential credential = LockscreenCredential.createPattern(pattern)) {
69                 mPendingLockCheck = LockPatternChecker.verifyCredential(
70                         mLockPatternUtils,
71                         credential,
72                         mOperationId,
73                         mEffectiveUserId,
74                         this::onPatternVerified);
75             }
76         }
77 
onPatternVerified(byte[] attestation, int timeoutMs)78         private void onPatternVerified(byte[] attestation, int timeoutMs) {
79             AuthCredentialPatternView.this.onCredentialVerified(attestation, timeoutMs);
80             if (timeoutMs > 0) {
81                 mLockPatternView.setEnabled(false);
82             } else {
83                 mLockPatternView.setEnabled(true);
84             }
85         }
86     }
87 
88     @Override
onErrorTimeoutFinish()89     protected void onErrorTimeoutFinish() {
90         super.onErrorTimeoutFinish();
91         mLockPatternView.setEnabled(true);
92     }
93 
AuthCredentialPatternView(Context context, AttributeSet attrs)94     public AuthCredentialPatternView(Context context, AttributeSet attrs) {
95         super(context, attrs);
96     }
97 
98     @Override
onAttachedToWindow()99     protected void onAttachedToWindow() {
100         super.onAttachedToWindow();
101         mLockPatternView = findViewById(R.id.lockPattern);
102         mLockPatternView.setOnPatternListener(new UnlockPatternListener());
103         mLockPatternView.setInStealthMode(
104                 !mLockPatternUtils.isVisiblePatternEnabled(mUserId));
105         mLockPatternView.setTactileFeedbackEnabled(mLockPatternUtils.isTactileFeedbackEnabled());
106     }
107 }
108