blob: 3d43f5d2d41905e722f46759893aa3835475c395 [file] [log] [blame]
Xiyuan Xia6e380582015-05-05 15:16:08 -07001package com.android.internal.widget;
2
3import android.os.AsyncTask;
4
Andres Morales23974272015-05-14 22:42:26 -07005import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
6
Xiyuan Xia6e380582015-05-05 15:16:08 -07007import java.util.List;
8
9/**
10 * Helper class to check/verify PIN/Password/Pattern asynchronously.
11 */
12public final class LockPatternChecker {
13 /**
14 * Interface for a callback to be invoked after security check.
15 */
16 public interface OnCheckCallback {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070017
18 /**
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.
21 */
22 default void onEarlyMatched() {}
23
Xiyuan Xia6e380582015-05-05 15:16:08 -070024 /**
25 * Invoked when a security check is finished.
26 *
27 * @param matched Whether the PIN/Password/Pattern matches the stored one.
Andres Morales23974272015-05-14 22:42:26 -070028 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
29 * the call. Only non-0 if matched is false.
Xiyuan Xia6e380582015-05-05 15:16:08 -070030 */
Andres Morales23974272015-05-14 22:42:26 -070031 void onChecked(boolean matched, int throttleTimeoutMs);
Jorim Jaggied3032b2016-10-07 16:49:34 +020032
33 /**
34 * Called when the underlying AsyncTask was cancelled.
35 */
36 default void onCancelled() {}
Xiyuan Xia6e380582015-05-05 15:16:08 -070037 }
38
39 /**
40 * Interface for a callback to be invoked after security verification.
41 */
42 public interface OnVerifyCallback {
43 /**
44 * Invoked when a security verification is finished.
45 *
46 * @param attestation The attestation that the challenge was verified, or null.
Andres Morales23974272015-05-14 22:42:26 -070047 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
48 * the call. Only non-0 if attestation is null.
Xiyuan Xia6e380582015-05-05 15:16:08 -070049 */
Andres Morales23974272015-05-14 22:42:26 -070050 void onVerified(byte[] attestation, int throttleTimeoutMs);
Xiyuan Xia6e380582015-05-05 15:16:08 -070051 }
52
53 /**
54 * Verify a pattern asynchronously.
55 *
56 * @param utils The LockPatternUtils instance to use.
57 * @param pattern The pattern to check.
58 * @param challenge The challenge to verify against the pattern.
59 * @param userId The user to check against the pattern.
60 * @param callback The callback to be invoked with the verification result.
61 */
62 public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils,
63 final List<LockPatternView.Cell> pattern,
64 final long challenge,
65 final int userId,
66 final OnVerifyCallback callback) {
67 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
Andres Morales23974272015-05-14 22:42:26 -070068 private int mThrottleTimeout;
69
Xiyuan Xia6e380582015-05-05 15:16:08 -070070 @Override
71 protected byte[] doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -070072 try {
73 return utils.verifyPattern(pattern, challenge, userId);
74 } catch (RequestThrottledException ex) {
75 mThrottleTimeout = ex.getTimeoutMs();
76 return null;
77 }
Xiyuan Xia6e380582015-05-05 15:16:08 -070078 }
79
80 @Override
81 protected void onPostExecute(byte[] result) {
Andres Morales23974272015-05-14 22:42:26 -070082 callback.onVerified(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -070083 }
84 };
85 task.execute();
86 return task;
87 }
88
89 /**
90 * Checks a pattern asynchronously.
91 *
92 * @param utils The LockPatternUtils instance to use.
93 * @param pattern The pattern to check.
94 * @param userId The user to check against the pattern.
95 * @param callback The callback to be invoked with the check result.
96 */
97 public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils,
98 final List<LockPatternView.Cell> pattern,
99 final int userId,
100 final OnCheckCallback callback) {
101 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
Andres Morales23974272015-05-14 22:42:26 -0700102 private int mThrottleTimeout;
103
Xiyuan Xia6e380582015-05-05 15:16:08 -0700104 @Override
105 protected Boolean doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700106 try {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700107 return utils.checkPattern(pattern, userId, callback::onEarlyMatched);
Andres Morales23974272015-05-14 22:42:26 -0700108 } catch (RequestThrottledException ex) {
109 mThrottleTimeout = ex.getTimeoutMs();
110 return false;
111 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700112 }
113
114 @Override
115 protected void onPostExecute(Boolean result) {
Andres Morales23974272015-05-14 22:42:26 -0700116 callback.onChecked(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700117 }
Jorim Jaggied3032b2016-10-07 16:49:34 +0200118
119 @Override
120 protected void onCancelled() {
121 callback.onCancelled();
122 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700123 };
124 task.execute();
125 return task;
126 }
127
128 /**
129 * Verify a password asynchronously.
130 *
131 * @param utils The LockPatternUtils instance to use.
132 * @param password The password to check.
133 * @param challenge The challenge to verify against the pattern.
134 * @param userId The user to check against the pattern.
135 * @param callback The callback to be invoked with the verification result.
136 */
137 public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
138 final String password,
139 final long challenge,
140 final int userId,
141 final OnVerifyCallback callback) {
142 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
Andres Morales23974272015-05-14 22:42:26 -0700143 private int mThrottleTimeout;
144
Xiyuan Xia6e380582015-05-05 15:16:08 -0700145 @Override
146 protected byte[] doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700147 try {
148 return utils.verifyPassword(password, challenge, userId);
149 } catch (RequestThrottledException ex) {
150 mThrottleTimeout = ex.getTimeoutMs();
151 return null;
152 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700153 }
154
155 @Override
156 protected void onPostExecute(byte[] result) {
Andres Morales23974272015-05-14 22:42:26 -0700157 callback.onVerified(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700158 }
159 };
160 task.execute();
161 return task;
162 }
163
164 /**
Ricky Wai53940d42016-04-05 15:29:24 +0100165 * Verify a password asynchronously.
166 *
167 * @param utils The LockPatternUtils instance to use.
168 * @param password The password to check.
169 * @param challenge The challenge to verify against the pattern.
170 * @param userId The user to check against the pattern.
171 * @param callback The callback to be invoked with the verification result.
172 */
173 public static AsyncTask<?, ?, ?> verifyTiedProfileChallenge(final LockPatternUtils utils,
174 final String password,
175 final boolean isPattern,
176 final long challenge,
177 final int userId,
178 final OnVerifyCallback callback) {
179 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
180 private int mThrottleTimeout;
181
182 @Override
183 protected byte[] doInBackground(Void... args) {
184 try {
185 return utils.verifyTiedProfileChallenge(password, isPattern, challenge, userId);
186 } catch (RequestThrottledException ex) {
187 mThrottleTimeout = ex.getTimeoutMs();
188 return null;
189 }
190 }
191
192 @Override
193 protected void onPostExecute(byte[] result) {
194 callback.onVerified(result, mThrottleTimeout);
195 }
196 };
197 task.execute();
198 return task;
199 }
200
201 /**
Xiyuan Xia6e380582015-05-05 15:16:08 -0700202 * Checks a password asynchronously.
203 *
204 * @param utils The LockPatternUtils instance to use.
205 * @param password The password to check.
206 * @param userId The user to check against the pattern.
207 * @param callback The callback to be invoked with the check result.
208 */
209 public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
210 final String password,
211 final int userId,
212 final OnCheckCallback callback) {
213 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
Andres Morales23974272015-05-14 22:42:26 -0700214 private int mThrottleTimeout;
215
Xiyuan Xia6e380582015-05-05 15:16:08 -0700216 @Override
217 protected Boolean doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700218 try {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700219 return utils.checkPassword(password, userId, callback::onEarlyMatched);
Andres Morales23974272015-05-14 22:42:26 -0700220 } catch (RequestThrottledException ex) {
221 mThrottleTimeout = ex.getTimeoutMs();
222 return false;
223 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700224 }
225
226 @Override
227 protected void onPostExecute(Boolean result) {
Andres Morales23974272015-05-14 22:42:26 -0700228 callback.onChecked(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700229 }
Jorim Jaggied3032b2016-10-07 16:49:34 +0200230
231 @Override
232 protected void onCancelled() {
233 callback.onCancelled();
234 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700235 };
236 task.execute();
237 return task;
238 }
239}