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); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 32 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 42 | * @param throttleTimeoutMs The amount of time in ms to wait before reattempting |
| 43 | * the call. Only non-0 if attestation is null. |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 44 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 45 | void onVerified(byte[] attestation, int throttleTimeoutMs); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 46 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 63 | private int mThrottleTimeout; |
| 64 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 65 | @Override |
| 66 | protected byte[] doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 67 | try { |
| 68 | return utils.verifyPattern(pattern, challenge, userId); |
| 69 | } catch (RequestThrottledException ex) { |
| 70 | mThrottleTimeout = ex.getTimeoutMs(); |
| 71 | return null; |
| 72 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected void onPostExecute(byte[] result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 77 | callback.onVerified(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 78 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 97 | private int mThrottleTimeout; |
| 98 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 99 | @Override |
| 100 | protected Boolean doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 101 | try { |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 102 | return utils.checkPattern(pattern, userId, callback::onEarlyMatched); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 103 | } catch (RequestThrottledException ex) { |
| 104 | mThrottleTimeout = ex.getTimeoutMs(); |
| 105 | return false; |
| 106 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onPostExecute(Boolean result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 111 | callback.onChecked(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 112 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 133 | private int mThrottleTimeout; |
| 134 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 135 | @Override |
| 136 | protected byte[] doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 137 | try { |
| 138 | return utils.verifyPassword(password, challenge, userId); |
| 139 | } catch (RequestThrottledException ex) { |
| 140 | mThrottleTimeout = ex.getTimeoutMs(); |
| 141 | return null; |
| 142 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void onPostExecute(byte[] result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 147 | callback.onVerified(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 148 | } |
| 149 | }; |
| 150 | task.execute(); |
| 151 | return task; |
| 152 | } |
| 153 | |
| 154 | /** |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 155 | * 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 192 | * 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 204 | private int mThrottleTimeout; |
| 205 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 206 | @Override |
| 207 | protected Boolean doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 208 | try { |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 209 | return utils.checkPassword(password, userId, callback::onEarlyMatched); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 210 | } catch (RequestThrottledException ex) { |
| 211 | mThrottleTimeout = ex.getTimeoutMs(); |
| 212 | return false; |
| 213 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | protected void onPostExecute(Boolean result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 218 | callback.onChecked(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 219 | } |
| 220 | }; |
| 221 | task.execute(); |
| 222 | return task; |
| 223 | } |
| 224 | } |