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