1package com.android.internal.widget; 2 3import android.os.AsyncTask; 4 5import com.android.internal.widget.LockPatternUtils.RequestThrottledException; 6 7import 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 { 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 24 /** 25 * Invoked when a security check is finished. 26 * 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. 30 */ 31 void onChecked(boolean matched, int throttleTimeoutMs); 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. 42 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting 43 * the call. Only non-0 if attestation is null. 44 */ 45 void onVerified(byte[] attestation, int throttleTimeoutMs); 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[]>() { 63 private int mThrottleTimeout; 64 65 @Override 66 protected byte[] doInBackground(Void... args) { 67 try { 68 return utils.verifyPattern(pattern, challenge, userId); 69 } catch (RequestThrottledException ex) { 70 mThrottleTimeout = ex.getTimeoutMs(); 71 return null; 72 } 73 } 74 75 @Override 76 protected void onPostExecute(byte[] result) { 77 callback.onVerified(result, mThrottleTimeout); 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>() { 97 private int mThrottleTimeout; 98 99 @Override 100 protected Boolean doInBackground(Void... args) { 101 try { 102 return utils.checkPattern(pattern, userId, callback::onEarlyMatched); 103 } catch (RequestThrottledException ex) { 104 mThrottleTimeout = ex.getTimeoutMs(); 105 return false; 106 } 107 } 108 109 @Override 110 protected void onPostExecute(Boolean result) { 111 callback.onChecked(result, mThrottleTimeout); 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[]>() { 133 private int mThrottleTimeout; 134 135 @Override 136 protected byte[] doInBackground(Void... args) { 137 try { 138 return utils.verifyPassword(password, challenge, userId); 139 } catch (RequestThrottledException ex) { 140 mThrottleTimeout = ex.getTimeoutMs(); 141 return null; 142 } 143 } 144 145 @Override 146 protected void onPostExecute(byte[] result) { 147 callback.onVerified(result, mThrottleTimeout); 148 } 149 }; 150 task.execute(); 151 return task; 152 } 153 154 /** 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 /** 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>() { 204 private int mThrottleTimeout; 205 206 @Override 207 protected Boolean doInBackground(Void... args) { 208 try { 209 return utils.checkPassword(password, userId, callback::onEarlyMatched); 210 } catch (RequestThrottledException ex) { 211 mThrottleTimeout = ex.getTimeoutMs(); 212 return false; 213 } 214 } 215 216 @Override 217 protected void onPostExecute(Boolean result) { 218 callback.onChecked(result, mThrottleTimeout); 219 } 220 }; 221 task.execute(); 222 return task; 223 } 224} 225