// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintAuthenticationException.java
// public class FingerprintAuthenticationException extends Exception {
// 
//     private final String message;
// 
//     /**
//      * Creates exception that occurs during fingerprint authentication with message
//      *
//      * @param errString message of exception
//      */
//     public FingerprintAuthenticationException(CharSequence errString) {
//         message = errString.toString();
//     }
// 
//     @Override
//     public String getMessage() {
//         return message;
//     }
// }
// 
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintUnavailableException.java
// public class FingerprintUnavailableException extends Exception {
// 
// 	public FingerprintUnavailableException(String s) {
// 		super(s);
// 	}
// }


import io.reactivex.Emitter;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;
import static android.Manifest.permission.USE_FINGERPRINT;
import android.annotation.SuppressLint;
import android.hardware.fingerprint.FingerprintManager;
import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
import android.hardware.fingerprint.FingerprintManager.CryptoObject;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.RequiresPermission;
import com.mtramin.rxfingerprint.data.FingerprintAuthenticationException;
import com.mtramin.rxfingerprint.data.FingerprintUnavailableException;
	@Override
	@RequiresPermission(USE_FINGERPRINT)
	@RequiresApi(Build.VERSION_CODES.M)
	public void subscribe(ObservableEmitter<T> emitter) throws Exception {
		if (fingerprintApiWrapper.isUnavailable()) {
			emitter.onError(new FingerprintUnavailableException("Fingerprint authentication is not available on this device! Ensure that the device has a Fingerprint sensor and enrolled Fingerprints by calling RxFingerprint#isAvailable(Context) first"));
			return;
		}

		AuthenticationCallback callback = createAuthenticationCallback(emitter);
		cancellationSignal = fingerprintApiWrapper.createCancellationSignal();
		CryptoObject cryptoObject = initCryptoObject(emitter);
		//noinspection MissingPermission
		fingerprintApiWrapper.getFingerprintManager().authenticate(cryptoObject, cancellationSignal, 0, callback, null);

		emitter.setCancellable(new Cancellable() {
			@Override
			public void cancel() throws Exception {
				if (cancellationSignal != null && !cancellationSignal.isCanceled()) {
					cancellationSignal.cancel();
				}
			}
		});
	}

	private AuthenticationCallback createAuthenticationCallback(final ObservableEmitter<T> emitter) {
		return new AuthenticationCallback() {
			@Override
			public void onAuthenticationError(int errMsgId, CharSequence errString) {
				if (!emitter.isDisposed()) {
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintAuthenticationException.java
// public class FingerprintAuthenticationException extends Exception {
// 
//     private final String message;
// 
//     /**
//      * Creates exception that occurs during fingerprint authentication with message
//      *
//      * @param errString message of exception
//      */
//     public FingerprintAuthenticationException(CharSequence errString) {
//         message = errString.toString();
//     }
// 
//     @Override
//     public String getMessage() {
//         return message;
//     }
// }
// 
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintUnavailableException.java
// public class FingerprintUnavailableException extends Exception {
// 
// 	public FingerprintUnavailableException(String s) {
// 		super(s);
// 	}
// }


// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/FingerprintObservable.java
import io.reactivex.Emitter;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;
import static android.Manifest.permission.USE_FINGERPRINT;
import android.annotation.SuppressLint;
import android.hardware.fingerprint.FingerprintManager;
import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
import android.hardware.fingerprint.FingerprintManager.CryptoObject;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.RequiresPermission;
import com.mtramin.rxfingerprint.data.FingerprintAuthenticationException;
import com.mtramin.rxfingerprint.data.FingerprintUnavailableException;

	@Override
	@RequiresPermission(USE_FINGERPRINT)
	@RequiresApi(Build.VERSION_CODES.M)
	public void subscribe(ObservableEmitter<T> emitter) throws Exception {
		if (fingerprintApiWrapper.isUnavailable()) {
			emitter.onError(new FingerprintUnavailableException("Fingerprint authentication is not available on this device! Ensure that the device has a Fingerprint sensor and enrolled Fingerprints by calling RxFingerprint#isAvailable(Context) first"));
			return;
		}

		AuthenticationCallback callback = createAuthenticationCallback(emitter);
		cancellationSignal = fingerprintApiWrapper.createCancellationSignal();
		CryptoObject cryptoObject = initCryptoObject(emitter);
		//noinspection MissingPermission
		fingerprintApiWrapper.getFingerprintManager().authenticate(cryptoObject, cancellationSignal, 0, callback, null);

		emitter.setCancellable(new Cancellable() {
			@Override
			public void cancel() throws Exception {
				if (cancellationSignal != null && !cancellationSignal.isCanceled()) {
					cancellationSignal.cancel();
				}
			}
		});
	}

	private AuthenticationCallback createAuthenticationCallback(final ObservableEmitter<T> emitter) {
		return new AuthenticationCallback() {
			@Override
			public void onAuthenticationError(int errMsgId, CharSequence errString) {
				if (!emitter.isDisposed()) {
					emitter.onError(new FingerprintAuthenticationException(errString));