FingerprintManagerCompatApi23.java revision d019e1cc3c5cbd5af21cd6bbf4e6d4593a5a6c65
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.getCipher() != null) { 52 return new FingerprintManager.CryptoObject(cryptoObject.getCipher()); 53 } else { 54 return new FingerprintManager.CryptoObject(cryptoObject.getSignature()); 55 } 56 } 57 58 private static CryptoObject unwrapCryptoObject(FingerprintManager.CryptoObject cryptoObject) { 59 if (cryptoObject.getCipher() != null) { 60 return new CryptoObject(cryptoObject.getCipher()); 61 } else { 62 return new CryptoObject(cryptoObject.getSignature()); 63 } 64 } 65 66 private static FingerprintManager.AuthenticationCallback wrapCallback( 67 final AuthenticationCallback callback) { 68 return new FingerprintManager.AuthenticationCallback() { 69 @Override 70 public void onAuthenticationError(int errMsgId, CharSequence errString) { 71 callback.onAuthenticationError(errMsgId, errString); 72 } 73 74 @Override 75 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { 76 callback.onAuthenticationHelp(helpMsgId, helpString); 77 } 78 79 @Override 80 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { 81 callback.onAuthenticationSucceeded(new AuthenticationResultInternal( 82 unwrapCryptoObject(result.getCryptoObject()))); 83 } 84 85 @Override 86 public void onAuthenticationFailed() { 87 callback.onAuthenticationFailed(); 88 } 89 }; 90 } 91 92 public static class CryptoObject { 93 94 private final Signature mSignature; 95 private final Cipher mCipher; 96 97 public CryptoObject(Signature signature) { 98 mSignature = signature; 99 mCipher = null; 100 } 101 102 public CryptoObject(Cipher cipher) { 103 mCipher = cipher; 104 mSignature = null; 105 } 106 107 public Signature getSignature() { return mSignature; } 108 public Cipher getCipher() { return mCipher; } 109 } 110 111 public static final class AuthenticationResultInternal { 112 private CryptoObject mCryptoObject; 113 114 public AuthenticationResultInternal(CryptoObject crypto) { 115 mCryptoObject = crypto; 116 } 117 118 public CryptoObject getCryptoObject() { return mCryptoObject; } 119 } 120 121 public static abstract class AuthenticationCallback { 122 123 public void onAuthenticationError(int errMsgId, CharSequence errString) { } 124 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { } 125 public void onAuthenticationSucceeded(AuthenticationResultInternal result) { } 126 public void onAuthenticationFailed() { } 127 } 128} 129