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 * @hide
31 */
32public final class FingerprintManagerCompatApi23 {
33
34    private static FingerprintManager getFingerprintManager(Context ctx) {
35        return ctx.getSystemService(FingerprintManager.class);
36    }
37
38    public static boolean hasEnrolledFingerprints(Context context) {
39        return getFingerprintManager(context).hasEnrolledFingerprints();
40    }
41
42    public static boolean isHardwareDetected(Context context) {
43        return getFingerprintManager(context).isHardwareDetected();
44    }
45
46    public static void authenticate(Context context, CryptoObject crypto, int flags, Object cancel,
47            AuthenticationCallback callback, Handler handler) {
48        getFingerprintManager(context).authenticate(wrapCryptoObject(crypto),
49                (android.os.CancellationSignal) cancel, flags,
50                wrapCallback(callback), handler);
51    }
52
53    private static FingerprintManager.CryptoObject wrapCryptoObject(CryptoObject cryptoObject) {
54        if (cryptoObject == null) {
55            return null;
56        } else if (cryptoObject.getCipher() != null) {
57            return new FingerprintManager.CryptoObject(cryptoObject.getCipher());
58        } else if (cryptoObject.getSignature() != null) {
59            return new FingerprintManager.CryptoObject(cryptoObject.getSignature());
60        } else if (cryptoObject.getMac() != null) {
61            return new FingerprintManager.CryptoObject(cryptoObject.getMac());
62        } else {
63            return null;
64        }
65    }
66
67    private static CryptoObject unwrapCryptoObject(FingerprintManager.CryptoObject cryptoObject) {
68        if (cryptoObject == null) {
69            return null;
70        } else if (cryptoObject.getCipher() != null) {
71            return new CryptoObject(cryptoObject.getCipher());
72        } else if (cryptoObject.getSignature() != null) {
73            return new CryptoObject(cryptoObject.getSignature());
74        } else if (cryptoObject.getMac() != null) {
75            return new CryptoObject(cryptoObject.getMac());
76        } else {
77            return null;
78        }
79    }
80
81    private static FingerprintManager.AuthenticationCallback wrapCallback(
82            final AuthenticationCallback callback) {
83        return new FingerprintManager.AuthenticationCallback() {
84            @Override
85            public void onAuthenticationError(int errMsgId, CharSequence errString) {
86                callback.onAuthenticationError(errMsgId, errString);
87            }
88
89            @Override
90            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
91                callback.onAuthenticationHelp(helpMsgId, helpString);
92            }
93
94            @Override
95            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
96                callback.onAuthenticationSucceeded(new AuthenticationResultInternal(
97                        unwrapCryptoObject(result.getCryptoObject())));
98            }
99
100            @Override
101            public void onAuthenticationFailed() {
102                callback.onAuthenticationFailed();
103            }
104        };
105    }
106
107    public static class CryptoObject {
108
109        private final Signature mSignature;
110        private final Cipher mCipher;
111        private final Mac mMac;
112
113        public CryptoObject(Signature signature) {
114            mSignature = signature;
115            mCipher = null;
116            mMac = null;
117        }
118
119        public CryptoObject(Cipher cipher) {
120            mCipher = cipher;
121            mSignature = null;
122            mMac = null;
123        }
124
125        public CryptoObject(Mac mac) {
126            mMac = mac;
127            mCipher = null;
128            mSignature = null;
129        }
130
131        public Signature getSignature() { return mSignature; }
132        public Cipher getCipher() { return mCipher; }
133        public Mac getMac() { return mMac; }
134    }
135
136    public static final class AuthenticationResultInternal {
137        private CryptoObject mCryptoObject;
138
139        public AuthenticationResultInternal(CryptoObject crypto) {
140            mCryptoObject = crypto;
141        }
142
143        public CryptoObject getCryptoObject() { return mCryptoObject; }
144    }
145
146    public static abstract class AuthenticationCallback {
147
148        public void onAuthenticationError(int errMsgId, CharSequence errString) { }
149        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { }
150        public void onAuthenticationSucceeded(AuthenticationResultInternal result) { }
151        public void onAuthenticationFailed() { }
152    }
153}
154