FingerprintManagerCompatApi23.java revision 50931369bc45988cfb44760dc3b52012bef56d3a
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;
21
22import java.security.Signature;
23
24import javax.crypto.Cipher;
25
26/**
27 * Actual FingerprintManagerCompat implementation for API level 23 and later.
28 */
29public final class FingerprintManagerCompatApi23 {
30
31    private static FingerprintManager getFingerprintManager(Context ctx) {
32        return ctx.getSystemService(FingerprintManager.class);
33    }
34
35    public static boolean hasEnrolledFingerprints(Context context) {
36        return getFingerprintManager(context).hasEnrolledFingerprints();
37    }
38
39    public static boolean isHardwareDetected(Context context) {
40        return getFingerprintManager(context).isHardwareDetected();
41    }
42
43    public static void authenticate(Context context, CryptoObject crypto, Object cancel,
44            AuthenticationCallback callback, int flags) {
45        getFingerprintManager(context).authenticate(wrapCryptoObject(crypto),
46                (android.os.CancellationSignal) cancel,
47                wrapCallback(callback), flags);
48    }
49
50    private static FingerprintManager.CryptoObject wrapCryptoObject(CryptoObject cryptoObject) {
51        if (cryptoObject == null) {
52            return null;
53        } else if (cryptoObject.getCipher() != null) {
54            return new FingerprintManager.CryptoObject(cryptoObject.getCipher());
55        } else {
56            return new FingerprintManager.CryptoObject(cryptoObject.getSignature());
57        }
58    }
59
60    private static CryptoObject unwrapCryptoObject(FingerprintManager.CryptoObject cryptoObject) {
61        if (cryptoObject == null) {
62            return null;
63        } else if (cryptoObject.getCipher() != null) {
64            return new CryptoObject(cryptoObject.getCipher());
65        } else {
66            return new CryptoObject(cryptoObject.getSignature());
67        }
68    }
69
70    private static FingerprintManager.AuthenticationCallback wrapCallback(
71            final AuthenticationCallback callback) {
72        return new FingerprintManager.AuthenticationCallback() {
73            @Override
74            public void onAuthenticationError(int errMsgId, CharSequence errString) {
75                callback.onAuthenticationError(errMsgId, errString);
76            }
77
78            @Override
79            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
80                callback.onAuthenticationHelp(helpMsgId, helpString);
81            }
82
83            @Override
84            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
85                callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
86                        unwrapCryptoObject(result.getCryptoObject())));
87            }
88
89            @Override
90            public void onAuthenticationFailed() {
91                callback.onAuthenticationFailed();
92            }
93        };
94    }
95
96    public static class CryptoObject {
97
98        private final Signature mSignature;
99        private final Cipher mCipher;
100
101        public CryptoObject(Signature signature) {
102            mSignature = signature;
103            mCipher = null;
104        }
105
106        public CryptoObject(Cipher cipher) {
107            mCipher = cipher;
108            mSignature = null;
109        }
110
111        public Signature getSignature() { return mSignature; }
112        public Cipher getCipher() { return mCipher; }
113    }
114
115    public static final class AuthenticationResultInternal {
116        private CryptoObject mCryptoObject;
117
118        public AuthenticationResultInternal(CryptoObject crypto) {
119            mCryptoObject = crypto;
120        }
121
122        public CryptoObject getCryptoObject() { return mCryptoObject; }
123    }
124
125    public static abstract class AuthenticationCallback {
126
127        public void onAuthenticationError(int errMsgId, CharSequence errString) { }
128        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { }
129        public void onAuthenticationSucceeded(AuthenticationResultInternal result) { }
130        public void onAuthenticationFailed() { }
131    }
132}
133