1 package com.android.internal.widget;
3 import android.os.AsyncTask;
5 import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
10 * Helper class to check/verify PIN/Password/Pattern asynchronously.
12 public final class LockPatternChecker {
14 * Interface for a callback to be invoked after security check.
16 public interface OnCheckCallback {
19 * Invoked as soon as possible we know that the credentials match. This will be called
20 * earlier than {@link #onChecked} but only if the credentials match.
22 default void onEarlyMatched() {}
25 * Invoked when a security check is finished.
27 * @param matched Whether the PIN/Password/Pattern matches the stored one.
28 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
29 * the call. Only non-0 if matched is false.
31 void onChecked(boolean matched, int throttleTimeoutMs);
35 * Interface for a callback to be invoked after security verification.
37 public interface OnVerifyCallback {
39 * Invoked when a security verification is finished.
41 * @param attestation The attestation that the challenge was verified, or null.
42 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
43 * the call. Only non-0 if attestation is null.
45 void onVerified(byte[] attestation, int throttleTimeoutMs);
49 * Verify a pattern asynchronously.
51 * @param utils The LockPatternUtils instance to use.
52 * @param pattern The pattern to check.
53 * @param challenge The challenge to verify against the pattern.
54 * @param userId The user to check against the pattern.
55 * @param callback The callback to be invoked with the verification result.
57 public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils,
58 final List<LockPatternView.Cell> pattern,
61 final OnVerifyCallback callback) {
62 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
63 private int mThrottleTimeout;
66 protected byte[] doInBackground(Void... args) {
68 return utils.verifyPattern(pattern, challenge, userId);
69 } catch (RequestThrottledException ex) {
70 mThrottleTimeout = ex.getTimeoutMs();
76 protected void onPostExecute(byte[] result) {
77 callback.onVerified(result, mThrottleTimeout);
85 * Checks a pattern asynchronously.
87 * @param utils The LockPatternUtils instance to use.
88 * @param pattern The pattern to check.
89 * @param userId The user to check against the pattern.
90 * @param callback The callback to be invoked with the check result.
92 public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils,
93 final List<LockPatternView.Cell> pattern,
95 final OnCheckCallback callback) {
96 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
97 private int mThrottleTimeout;
100 protected Boolean doInBackground(Void... args) {
102 return utils.checkPattern(pattern, userId, callback::onEarlyMatched);
103 } catch (RequestThrottledException ex) {
104 mThrottleTimeout = ex.getTimeoutMs();
110 protected void onPostExecute(Boolean result) {
111 callback.onChecked(result, mThrottleTimeout);
119 * Verify a password asynchronously.
121 * @param utils The LockPatternUtils instance to use.
122 * @param password The password to check.
123 * @param challenge The challenge to verify against the pattern.
124 * @param userId The user to check against the pattern.
125 * @param callback The callback to be invoked with the verification result.
127 public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
128 final String password,
129 final long challenge,
131 final OnVerifyCallback callback) {
132 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
133 private int mThrottleTimeout;
136 protected byte[] doInBackground(Void... args) {
138 return utils.verifyPassword(password, challenge, userId);
139 } catch (RequestThrottledException ex) {
140 mThrottleTimeout = ex.getTimeoutMs();
146 protected void onPostExecute(byte[] result) {
147 callback.onVerified(result, mThrottleTimeout);
155 * Verify a password asynchronously.
157 * @param utils The LockPatternUtils instance to use.
158 * @param password The password to check.
159 * @param challenge The challenge to verify against the pattern.
160 * @param userId The user to check against the pattern.
161 * @param callback The callback to be invoked with the verification result.
163 public static AsyncTask<?, ?, ?> verifyTiedProfileChallenge(final LockPatternUtils utils,
164 final String password,
165 final boolean isPattern,
166 final long challenge,
168 final OnVerifyCallback callback) {
169 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
170 private int mThrottleTimeout;
173 protected byte[] doInBackground(Void... args) {
175 return utils.verifyTiedProfileChallenge(password, isPattern, challenge, userId);
176 } catch (RequestThrottledException ex) {
177 mThrottleTimeout = ex.getTimeoutMs();
183 protected void onPostExecute(byte[] result) {
184 callback.onVerified(result, mThrottleTimeout);
192 * Checks a password asynchronously.
194 * @param utils The LockPatternUtils instance to use.
195 * @param password The password to check.
196 * @param userId The user to check against the pattern.
197 * @param callback The callback to be invoked with the check result.
199 public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
200 final String password,
202 final OnCheckCallback callback) {
203 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
204 private int mThrottleTimeout;
207 protected Boolean doInBackground(Void... args) {
209 return utils.checkPassword(password, userId, callback::onEarlyMatched);
210 } catch (RequestThrottledException ex) {
211 mThrottleTimeout = ex.getTimeoutMs();
217 protected void onPostExecute(Boolean result) {
218 callback.onChecked(result, mThrottleTimeout);