blob: df9b0ddc180473c114e5be41120ed27da783e9f1 [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);
Xiyuan Xia6e380582015-05-05 15:16:08 -070032 }
33
34 /**
35 * Interface for a callback to be invoked after security verification.
36 */
37 public interface OnVerifyCallback {
38 /**
39 * Invoked when a security verification is finished.
40 *
41 * @param attestation The attestation that the challenge was verified, or null.
Andres Morales23974272015-05-14 22:42:26 -070042 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
43 * the call. Only non-0 if attestation is null.
Xiyuan Xia6e380582015-05-05 15:16:08 -070044 */
Andres Morales23974272015-05-14 22:42:26 -070045 void onVerified(byte[] attestation, int throttleTimeoutMs);
Xiyuan Xia6e380582015-05-05 15:16:08 -070046 }
47
48 /**
49 * Verify a pattern asynchronously.
50 *
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.
56 */
57 public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils,
58 final List<LockPatternView.Cell> pattern,
59 final long challenge,
60 final int userId,
61 final OnVerifyCallback callback) {
62 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
Andres Morales23974272015-05-14 22:42:26 -070063 private int mThrottleTimeout;
64
Xiyuan Xia6e380582015-05-05 15:16:08 -070065 @Override
66 protected byte[] doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -070067 try {
68 return utils.verifyPattern(pattern, challenge, userId);
69 } catch (RequestThrottledException ex) {
70 mThrottleTimeout = ex.getTimeoutMs();
71 return null;
72 }
Xiyuan Xia6e380582015-05-05 15:16:08 -070073 }
74
75 @Override
76 protected void onPostExecute(byte[] result) {
Andres Morales23974272015-05-14 22:42:26 -070077 callback.onVerified(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -070078 }
79 };
80 task.execute();
81 return task;
82 }
83
84 /**
85 * Checks a pattern asynchronously.
86 *
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.
91 */
92 public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils,
93 final List<LockPatternView.Cell> pattern,
94 final int userId,
95 final OnCheckCallback callback) {
96 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
Andres Morales23974272015-05-14 22:42:26 -070097 private int mThrottleTimeout;
98
Xiyuan Xia6e380582015-05-05 15:16:08 -070099 @Override
100 protected Boolean doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700101 try {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700102 return utils.checkPattern(pattern, userId, callback::onEarlyMatched);
Andres Morales23974272015-05-14 22:42:26 -0700103 } catch (RequestThrottledException ex) {
104 mThrottleTimeout = ex.getTimeoutMs();
105 return false;
106 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700107 }
108
109 @Override
110 protected void onPostExecute(Boolean result) {
Andres Morales23974272015-05-14 22:42:26 -0700111 callback.onChecked(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700112 }
113 };
114 task.execute();
115 return task;
116 }
117
118 /**
119 * Verify a password asynchronously.
120 *
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.
126 */
127 public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
128 final String password,
129 final long challenge,
130 final int userId,
131 final OnVerifyCallback callback) {
132 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
Andres Morales23974272015-05-14 22:42:26 -0700133 private int mThrottleTimeout;
134
Xiyuan Xia6e380582015-05-05 15:16:08 -0700135 @Override
136 protected byte[] doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700137 try {
138 return utils.verifyPassword(password, challenge, userId);
139 } catch (RequestThrottledException ex) {
140 mThrottleTimeout = ex.getTimeoutMs();
141 return null;
142 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700143 }
144
145 @Override
146 protected void onPostExecute(byte[] result) {
Andres Morales23974272015-05-14 22:42:26 -0700147 callback.onVerified(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700148 }
149 };
150 task.execute();
151 return task;
152 }
153
154 /**
Ricky Wai53940d42016-04-05 15:29:24 +0100155 * Verify a password asynchronously.
156 *
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.
162 */
163 public static AsyncTask<?, ?, ?> verifyTiedProfileChallenge(final LockPatternUtils utils,
164 final String password,
165 final boolean isPattern,
166 final long challenge,
167 final int userId,
168 final OnVerifyCallback callback) {
169 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
170 private int mThrottleTimeout;
171
172 @Override
173 protected byte[] doInBackground(Void... args) {
174 try {
175 return utils.verifyTiedProfileChallenge(password, isPattern, challenge, userId);
176 } catch (RequestThrottledException ex) {
177 mThrottleTimeout = ex.getTimeoutMs();
178 return null;
179 }
180 }
181
182 @Override
183 protected void onPostExecute(byte[] result) {
184 callback.onVerified(result, mThrottleTimeout);
185 }
186 };
187 task.execute();
188 return task;
189 }
190
191 /**
Xiyuan Xia6e380582015-05-05 15:16:08 -0700192 * Checks a password asynchronously.
193 *
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.
198 */
199 public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
200 final String password,
201 final int userId,
202 final OnCheckCallback callback) {
203 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
Andres Morales23974272015-05-14 22:42:26 -0700204 private int mThrottleTimeout;
205
Xiyuan Xia6e380582015-05-05 15:16:08 -0700206 @Override
207 protected Boolean doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700208 try {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700209 return utils.checkPassword(password, userId, callback::onEarlyMatched);
Andres Morales23974272015-05-14 22:42:26 -0700210 } catch (RequestThrottledException ex) {
211 mThrottleTimeout = ex.getTimeoutMs();
212 return false;
213 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700214 }
215
216 @Override
217 protected void onPostExecute(Boolean result) {
Andres Morales23974272015-05-14 22:42:26 -0700218 callback.onChecked(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700219 }
220 };
221 task.execute();
222 return task;
223 }
224}