LockPatternChecker.java revision 6e38058908d6e49a241e384cd7023d9ac0927afb
1package com.android.internal.widget;
2
3import android.os.AsyncTask;
4
5import java.util.List;
6
7/**
8 * Helper class to check/verify PIN/Password/Pattern asynchronously.
9 */
10public final class LockPatternChecker {
11    /**
12     * Interface for a callback to be invoked after security check.
13     */
14    public interface OnCheckCallback {
15        /**
16         * Invoked when a security check is finished.
17         *
18         * @param matched Whether the PIN/Password/Pattern matches the stored one.
19         */
20        void onChecked(boolean matched);
21    }
22
23    /**
24     * Interface for a callback to be invoked after security verification.
25     */
26    public interface OnVerifyCallback {
27        /**
28         * Invoked when a security verification is finished.
29         *
30         * @param attestation The attestation that the challenge was verified, or null.
31         */
32        void onVerified(byte[] attestation);
33    }
34
35    /**
36     * Verify a pattern asynchronously.
37     *
38     * @param utils The LockPatternUtils instance to use.
39     * @param pattern The pattern to check.
40     * @param challenge The challenge to verify against the pattern.
41     * @param userId The user to check against the pattern.
42     * @param callback The callback to be invoked with the verification result.
43     */
44    public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils,
45            final List<LockPatternView.Cell> pattern,
46            final long challenge,
47            final int userId,
48            final OnVerifyCallback callback) {
49        AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
50            @Override
51            protected byte[] doInBackground(Void... args) {
52                return utils.verifyPattern(pattern, challenge, userId);
53            }
54
55            @Override
56            protected void onPostExecute(byte[] result) {
57                callback.onVerified(result);
58            }
59        };
60        task.execute();
61        return task;
62    }
63
64    /**
65     * Checks a pattern asynchronously.
66     *
67     * @param utils The LockPatternUtils instance to use.
68     * @param pattern The pattern to check.
69     * @param userId The user to check against the pattern.
70     * @param callback The callback to be invoked with the check result.
71     */
72    public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils,
73            final List<LockPatternView.Cell> pattern,
74            final int userId,
75            final OnCheckCallback callback) {
76        AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
77            @Override
78            protected Boolean doInBackground(Void... args) {
79                return utils.checkPattern(pattern, userId);
80            }
81
82            @Override
83            protected void onPostExecute(Boolean result) {
84                callback.onChecked(result);
85            }
86        };
87        task.execute();
88        return task;
89    }
90
91    /**
92     * Verify a password asynchronously.
93     *
94     * @param utils The LockPatternUtils instance to use.
95     * @param password The password to check.
96     * @param challenge The challenge to verify against the pattern.
97     * @param userId The user to check against the pattern.
98     * @param callback The callback to be invoked with the verification result.
99     */
100    public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
101            final String password,
102            final long challenge,
103            final int userId,
104            final OnVerifyCallback callback) {
105        AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
106            @Override
107            protected byte[] doInBackground(Void... args) {
108                return utils.verifyPassword(password, challenge, userId);
109            }
110
111            @Override
112            protected void onPostExecute(byte[] result) {
113                callback.onVerified(result);
114            }
115        };
116        task.execute();
117        return task;
118    }
119
120    /**
121     * Checks a password asynchronously.
122     *
123     * @param utils The LockPatternUtils instance to use.
124     * @param password The password to check.
125     * @param userId The user to check against the pattern.
126     * @param callback The callback to be invoked with the check result.
127     */
128    public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
129            final String password,
130            final int userId,
131            final OnCheckCallback callback) {
132        AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
133            @Override
134            protected Boolean doInBackground(Void... args) {
135                return utils.checkPassword(password, userId);
136            }
137
138            @Override
139            protected void onPostExecute(Boolean result) {
140                callback.onChecked(result);
141            }
142        };
143        task.execute();
144        return task;
145    }
146}
147