Java tutorial
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.nyelito.fingerprintcam; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.fingerprint.FingerprintManager; import android.os.CancellationSignal; import android.support.v4.content.ContextCompat; import android.widget.ImageView; import android.widget.TextView; import com.example.android.camera2basic.R; /** * Small helper class to manage text/icon around fingerprint authentication UI. */ public class FingerprintUiHelper extends FingerprintManager.AuthenticationCallback { static final long ERROR_TIMEOUT_MILLIS = 100; static final long SUCCESS_DELAY_MILLIS = 100; private final FingerprintManager mFingerprintManager; private final Callback mCallback; private CancellationSignal mCancellationSignal; boolean mSelfCancelled; public FingerprintUiHelper(FingerprintManager fingerprintManager, Callback callback) { mFingerprintManager = fingerprintManager; mCallback = callback; } public boolean isFingerprintAuthAvailable(Context context) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { } return mFingerprintManager.isHardwareDetected() && mFingerprintManager.hasEnrolledFingerprints(); } public void startListening(Context context, FingerprintManager.CryptoObject cryptoObject) { if (!isFingerprintAuthAvailable(context)) { return; } mCancellationSignal = new CancellationSignal(); mSelfCancelled = false; if (ContextCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // public void requestPermissions(@NonNull String[] permissions, int requestCode) // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for Activity#requestPermissions for more details. return; } mFingerprintManager.authenticate(cryptoObject, mCancellationSignal, 0 /* flags */, this, null); // mIcon.setImageResource(R.drawable.ic_fp_40px); } public void stopListening() { if (mCancellationSignal != null) { mSelfCancelled = true; mCancellationSignal.cancel(); mCancellationSignal = null; } } @Override public void onAuthenticationError(int errMsgId, CharSequence errString) { } @Override public void onAuthenticationFailed() { if (!mSelfCancelled) { mCallback.onError(); } } @Override public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { mCallback.onAuthenticated(); } public interface Callback { void onAuthenticated(); void onError(); } }