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      * Verify a password asynchronously.
149      *
150      * @param utils The LockPatternUtils instance to use.
151      * @param password The password to check.
152      * @param challenge The challenge to verify against the pattern.
153      * @param userId The user to check against the pattern.
154      * @param callback The callback to be invoked with the verification result.
155      */
verifyTiedProfileChallenge(final LockPatternUtils utils, final String password, final boolean isPattern, final long challenge, final int userId, final OnVerifyCallback callback)156     public static AsyncTask<?, ?, ?> verifyTiedProfileChallenge(final LockPatternUtils utils,
157             final String password,
158             final boolean isPattern,
159             final long challenge,
160             final int userId,
161             final OnVerifyCallback callback) {
162         AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
163             private int mThrottleTimeout;
164 
165             @Override
166             protected byte[] doInBackground(Void... args) {
167                 try {
168                     return utils.verifyTiedProfileChallenge(password, isPattern, challenge, userId);
169                 } catch (RequestThrottledException ex) {
170                     mThrottleTimeout = ex.getTimeoutMs();
171                     return null;
172                 }
173             }
174 
175             @Override
176             protected void onPostExecute(byte[] result) {
177                 callback.onVerified(result, mThrottleTimeout);
178             }
179         };
180         task.execute();
181         return task;
182     }
183 
184     /**
185      * Checks a password asynchronously.
186      *
187      * @param utils The LockPatternUtils instance to use.
188      * @param password The password to check.
189      * @param userId The user to check against the pattern.
190      * @param callback The callback to be invoked with the check result.
191      */
checkPassword(final LockPatternUtils utils, final String password, final int userId, final OnCheckCallback callback)192     public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
193             final String password,
194             final int userId,
195             final OnCheckCallback callback) {
196         AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
197             private int mThrottleTimeout;
198 
199             @Override
200             protected Boolean doInBackground(Void... args) {
201                 try {
202                     return utils.checkPassword(password, userId);
203                 } catch (RequestThrottledException ex) {
204                     mThrottleTimeout = ex.getTimeoutMs();
205                     return false;
206                 }
207             }
208 
209             @Override
210             protected void onPostExecute(Boolean result) {
211                 callback.onChecked(result, mThrottleTimeout);
212             }
213         };
214         task.execute();
215         return task;
216     }
217 }
218