Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 1 | package com.android.internal.widget; |
| 2 | |
| 3 | import android.os.AsyncTask; |
| 4 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 5 | import com.android.internal.widget.LockPatternUtils.RequestThrottledException; |
| 6 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 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 { |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 17 | |
| 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Invoked when a security check is finished. |
| 26 | * |
| 27 | * @param matched Whether the PIN/Password/Pattern matches the stored one. |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 28 | * @param throttleTimeoutMs The amount of time in ms to wait before reattempting |
| 29 | * the call. Only non-0 if matched is false. |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 30 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 31 | void onChecked(boolean matched, int throttleTimeoutMs); |
Jorim Jaggi | ed3032b | 2016-10-07 16:49:34 +0200 | [diff] [blame^] | 32 | |
| 33 | /** |
| 34 | * Called when the underlying AsyncTask was cancelled. |
| 35 | */ |
| 36 | default void onCancelled() {} |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 37 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 47 | * @param throttleTimeoutMs The amount of time in ms to wait before reattempting |
| 48 | * the call. Only non-0 if attestation is null. |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 49 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 50 | void onVerified(byte[] attestation, int throttleTimeoutMs); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 51 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 68 | private int mThrottleTimeout; |
| 69 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 70 | @Override |
| 71 | protected byte[] doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 72 | try { |
| 73 | return utils.verifyPattern(pattern, challenge, userId); |
| 74 | } catch (RequestThrottledException ex) { |
| 75 | mThrottleTimeout = ex.getTimeoutMs(); |
| 76 | return null; |
| 77 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void onPostExecute(byte[] result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 82 | callback.onVerified(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 83 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 102 | private int mThrottleTimeout; |
| 103 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 104 | @Override |
| 105 | protected Boolean doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 106 | try { |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 107 | return utils.checkPattern(pattern, userId, callback::onEarlyMatched); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 108 | } catch (RequestThrottledException ex) { |
| 109 | mThrottleTimeout = ex.getTimeoutMs(); |
| 110 | return false; |
| 111 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void onPostExecute(Boolean result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 116 | callback.onChecked(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 117 | } |
Jorim Jaggi | ed3032b | 2016-10-07 16:49:34 +0200 | [diff] [blame^] | 118 | |
| 119 | @Override |
| 120 | protected void onCancelled() { |
| 121 | callback.onCancelled(); |
| 122 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 123 | }; |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 143 | private int mThrottleTimeout; |
| 144 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 145 | @Override |
| 146 | protected byte[] doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 147 | try { |
| 148 | return utils.verifyPassword(password, challenge, userId); |
| 149 | } catch (RequestThrottledException ex) { |
| 150 | mThrottleTimeout = ex.getTimeoutMs(); |
| 151 | return null; |
| 152 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void onPostExecute(byte[] result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 157 | callback.onVerified(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 158 | } |
| 159 | }; |
| 160 | task.execute(); |
| 161 | return task; |
| 162 | } |
| 163 | |
| 164 | /** |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 165 | * 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 202 | * 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 214 | private int mThrottleTimeout; |
| 215 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 216 | @Override |
| 217 | protected Boolean doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 218 | try { |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 219 | return utils.checkPassword(password, userId, callback::onEarlyMatched); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 220 | } catch (RequestThrottledException ex) { |
| 221 | mThrottleTimeout = ex.getTimeoutMs(); |
| 222 | return false; |
| 223 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected void onPostExecute(Boolean result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 228 | callback.onChecked(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 229 | } |
Jorim Jaggi | ed3032b | 2016-10-07 16:49:34 +0200 | [diff] [blame^] | 230 | |
| 231 | @Override |
| 232 | protected void onCancelled() { |
| 233 | callback.onCancelled(); |
| 234 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 235 | }; |
| 236 | task.execute(); |
| 237 | return task; |
| 238 | } |
| 239 | } |