1 package com.android.internal.widget;
2 
3 import android.os.AsyncTask;
4 
5 import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
6 
7 import java.util.List;
8 
9 /**
10  * Helper class to check/verify PIN/Password/Pattern asynchronously.
11  */
12 public final class LockPatternChecker {
13     /**
14      * Interface for a callback to be invoked after security check.
15      */
16     public interface OnCheckCallback {
17         /**
18          * Invoked when a security check is finished.
19          *
20          * @param matched Whether the PIN/Password/Pattern matches the stored one.
21          * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
22          * the call. Only non-0 if matched is false.
23          */
onChecked(boolean matched, int throttleTimeoutMs)24         void onChecked(boolean matched, int throttleTimeoutMs);
25     }
26 
27     /**
28      * Interface for a callback to be invoked after security verification.
29      */
30     public interface OnVerifyCallback {
31         /**
32          * Invoked when a security verification is finished.
33          *
34          * @param attestation The attestation that the challenge was verified, or null.
35          * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
36          * the call. Only non-0 if attestation is null.
37          */
onVerified(byte[] attestation, int throttleTimeoutMs)38         void onVerified(byte[] attestation, int throttleTimeoutMs);
39     }
40 
41     /**
42      * Verify a pattern asynchronously.
43      *
44      * @param utils The LockPatternUtils instance to use.
45      * @param pattern The pattern to check.
46      * @param challenge The challenge to verify against the pattern.
47      * @param userId The user to check against the pattern.
48      * @param callback The callback to be invoked with the verification result.
49      */
verifyPattern(final LockPatternUtils utils, final List<LockPatternView.Cell> pattern, final long challenge, final int userId, final OnVerifyCallback callback)50     public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils,
51             final List<LockPatternView.Cell> pattern,
52             final long challenge,
53             final int userId,
54             final OnVerifyCallback callback) {
55         AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
56             private int mThrottleTimeout;
57 
58             @Override
59             protected byte[] doInBackground(Void... args) {
60                 try {
61                     return utils.verifyPattern(pattern, challenge, userId);
62                 } catch (RequestThrottledException ex) {
63                     mThrottleTimeout = ex.getTimeoutMs();
64                     return null;
65                 }
66             }
67 
68             @Override
69             protected void onPostExecute(byte[] result) {
70                 callback.onVerified(result, mThrottleTimeout);
71             }
72         };
73         task.execute();
74         return task;
75     }
76 
77     /**
78      * Checks a pattern asynchronously.
79      *
80      * @param utils The LockPatternUtils instance to use.
81      * @param pattern The pattern to check.
82      * @param userId The user to check against the pattern.
83      * @param callback The callback to be invoked with the check result.
84      */
checkPattern(final LockPatternUtils utils, final List<LockPatternView.Cell> pattern, final int userId, final OnCheckCallback callback)85     public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils,
86             final List<LockPatternView.Cell> pattern,
87             final int userId,
88             final OnCheckCallback callback) {
89         AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
90             private int mThrottleTimeout;
91 
92             @Override
93             protected Boolean doInBackground(Void... args) {
94                 try {
95                     return utils.checkPattern(pattern, userId);
96                 } catch (RequestThrottledException ex) {
97                     mThrottleTimeout = ex.getTimeoutMs();
98                     return false;
99                 }
100             }
101 
102             @Override
103             protected void onPostExecute(Boolean result) {
104                 callback.onChecked(result, mThrottleTimeout);
105             }
106         };
107         task.execute();
108         return task;
109     }
110 
111     /**
112      * Verify a password asynchronously.
113      *
114      * @param utils The LockPatternUtils instance to use.
115      * @param password The password to check.
116      * @param challenge The challenge to verify against the pattern.
117      * @param userId The user to check against the pattern.
118      * @param callback The callback to be invoked with the verification result.
119      */
verifyPassword(final LockPatternUtils utils, final String password, final long challenge, final int userId, final OnVerifyCallback callback)120     public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
121             final String password,
122             final long challenge,
123             final int userId,
124             final OnVerifyCallback callback) {
125         AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
126             private int mThrottleTimeout;
127 
128             @Override
129             protected byte[] doInBackground(Void... args) {
130                 try {
131                     return utils.verifyPassword(password, challenge, userId);
132                 } catch (RequestThrottledException ex) {
133                     mThrottleTimeout = ex.getTimeoutMs();
134                     return null;
135                 }
136             }
137 
138             @Override
139             protected void onPostExecute(byte[] result) {
140                 callback.onVerified(result, mThrottleTimeout);
141             }
142         };
143         task.execute();
144         return task;
145     }
146 
147     /**
148      * Checks a password asynchronously.
149      *
150      * @param utils The LockPatternUtils instance to use.
151      * @param password The password to check.
152      * @param userId The user to check against the pattern.
153      * @param callback The callback to be invoked with the check result.
154      */
checkPassword(final LockPatternUtils utils, final String password, final int userId, final OnCheckCallback callback)155     public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
156             final String password,
157             final int userId,
158             final OnCheckCallback callback) {
159         AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
160             private int mThrottleTimeout;
161 
162             @Override
163             protected Boolean doInBackground(Void... args) {
164                 try {
165                     return utils.checkPassword(password, userId);
166                 } catch (RequestThrottledException ex) {
167                     mThrottleTimeout = ex.getTimeoutMs();
168                     return false;
169                 }
170             }
171 
172             @Override
173             protected void onPostExecute(Boolean result) {
174                 callback.onChecked(result, mThrottleTimeout);
175             }
176         };
177         task.execute();
178         return task;
179     }
180 }
181