FingerprintManagerCompatApi23.java revision e70c5387272b23eb09b7324e88b8c5d8e4481b3f
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.support.v4.hardware.fingerprint;
18
19import android.content.Context;
20import android.hardware.fingerprint.FingerprintManager;
21import android.os.Handler;
22
23import java.security.Signature;
24
25import javax.crypto.Cipher;
26import javax.crypto.Mac;
27
28/**
29 * Actual FingerprintManagerCompat implementation for API level 23 and later.
30 */
31public final class FingerprintManagerCompatApi23 {
32
33    private static FingerprintManager getFingerprintManager(Context ctx) {
34        return ctx.getSystemService(FingerprintManager.class);
35    }
36
37    public static boolean hasEnrolledFingerprints(Context context) {
38        return getFingerprintManager(context).hasEnrolledFingerprints();
39    }
40
41    public static boolean isHardwareDetected(Context context) {
42        return getFingerprintManager(context).isHardwareDetected();
43    }
44
45    public static void authenticate(Context context, CryptoObject crypto, int flags, Object cancel,
46            AuthenticationCallback callback, Handler handler) {
47        getFingerprintManager(context).authenticate(wrapCryptoObject(crypto),
48                (android.os.CancellationSignal) cancel, flags,
49                wrapCallback(callback), handler);
50    }
51
52    private static FingerprintManager.CryptoObject wrapCryptoObject(CryptoObject cryptoObject) {
53        if (cryptoObject == null) {
54            return null;
55        } else if (cryptoObject.getCipher() != null) {
56            return new FingerprintManager.CryptoObject(cryptoObject.getCipher());
57        } else if (cryptoObject.getSignature() != null) {
58            return new FingerprintManager.CryptoObject(cryptoObject.getSignature());
59        } else if (cryptoObject.getMac() != null) {
60            return new FingerprintManager.CryptoObject(cryptoObject.getMac());
61        } else {
62            return null;
63        }
64    }
65
66    private static CryptoObject unwrapCryptoObject(FingerprintManager.CryptoObject cryptoObject) {
67        if (cryptoObject == null) {
68            return null;
69        } else if (cryptoObject.getCipher() != null) {
70            return new CryptoObject(cryptoObject.getCipher());
71        } else if (cryptoObject.getSignature() != null) {
72            return new CryptoObject(cryptoObject.getSignature());
73        } else if (cryptoObject.getMac() != null) {
74            return new CryptoObject(cryptoObject.getMac());
75        } else {
76            return null;
77        }
78    }
79
80    private static FingerprintManager.AuthenticationCallback wrapCallback(
81            final AuthenticationCallback callback) {
82        return new FingerprintManager.AuthenticationCallback() {
83            @Override
84            public void onAuthenticationError(int errMsgId, CharSequence errString) {
85                callback.onAuthenticationError(errMsgId, errString);
86            }
87
88            @Override
89            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
90                callback.onAuthenticationHelp(helpMsgId, helpString);
91            }
92
93            @Override
94            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
95                callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
96                        unwrapCryptoObject(result.getCryptoObject())));
97            }
98
99            @Override
100            public void onAuthenticationFailed() {
101                callback.onAuthenticationFailed();
102            }
103        };
104    }
105
106    public static class CryptoObject {
107
108        private final Signature mSignature;
109        private final Cipher mCipher;
110        private final Mac mMac;
111
112        public CryptoObject(Signature signature) {
113            mSignature = signature;
114            mCipher = null;
115            mMac = null;
116        }
117
118        public CryptoObject(Cipher cipher) {
119            mCipher = cipher;
120            mSignature = null;
121            mMac = null;
122        }
123
124        public CryptoObject(Mac mac) {
125            mMac = mac;
126            mCipher = null;
127            mSignature = null;
128        }
129
130        public Signature getSignature() { return mSignature; }
131        public Cipher getCipher() { return mCipher; }
132        public Mac getMac() { return mMac; }
133    }
134
135    public static final class AuthenticationResultInternal {
136        private CryptoObject mCryptoObject;
137
138        public AuthenticationResultInternal(CryptoObject crypto) {
139            mCryptoObject = crypto;
140        }
141
142        public CryptoObject getCryptoObject() { return mCryptoObject; }
143    }
144
145    public static abstract class AuthenticationCallback {
146
147        public void onAuthenticationError(int errMsgId, CharSequence errString) { }
148        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { }
149        public void onAuthenticationSucceeded(AuthenticationResultInternal result) { }
150        public void onAuthenticationFailed() { }
151    }
152}
153