Skip to content

Commit

Permalink
Update rxjava to v1.2.1 (#12)
Browse files Browse the repository at this point in the history
* fix method name in exception message

* update rxjava
  • Loading branch information
Mauin authored Oct 13, 2016
1 parent 371a3d7 commit 022e8cc
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 118 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ext.versions = [

// Dependency Versions
supportLibrary : '24.2.0',
rxJava : '1.1.9',
rxJava : '1.2.1',

// Testing dependencies
jUnit : '4.12',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import com.mtramin.rxfingerprint.data.FingerprintAuthenticationResult;
import com.mtramin.rxfingerprint.data.FingerprintResult;

import rx.AsyncEmitter;
import rx.Emitter;
import rx.Observable;

import static rx.AsyncEmitter.BackpressureMode.LATEST;
import static rx.Emitter.BackpressureMode.LATEST;

/**
* Authenticates the user with his fingerprint.
Expand All @@ -45,29 +45,29 @@ private FingerprintAuthenticationObservable(Context context) {
* @return Observable {@link FingerprintAuthenticationResult}
*/
static Observable<FingerprintAuthenticationResult> create(Context context) {
return Observable.fromAsync(new FingerprintAuthenticationObservable(context), LATEST);
return Observable.fromEmitter(new FingerprintAuthenticationObservable(context), LATEST);
}

@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(AsyncEmitter<FingerprintAuthenticationResult> subscriber) {
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Emitter<FingerprintAuthenticationResult> subscriber) {
// Simple authentication does not need CryptoObject
return null;
}

@Override
protected void onAuthenticationSucceeded(AsyncEmitter<FingerprintAuthenticationResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
protected void onAuthenticationSucceeded(Emitter<FingerprintAuthenticationResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
emitter.onNext(new FingerprintAuthenticationResult(FingerprintResult.AUTHENTICATED, null));
emitter.onCompleted();
}

@Override
protected void onAuthenticationHelp(AsyncEmitter<FingerprintAuthenticationResult> emitter, int helpMessageId, String helpString) {
protected void onAuthenticationHelp(Emitter<FingerprintAuthenticationResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintAuthenticationResult(FingerprintResult.HELP, helpString));
}

@Override
protected void onAuthenticationFailed(AsyncEmitter<FingerprintAuthenticationResult> emitter) {
protected void onAuthenticationFailed(Emitter<FingerprintAuthenticationResult> emitter) {
emitter.onNext(new FingerprintAuthenticationResult(FingerprintResult.FAILED, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import rx.AsyncEmitter;
import rx.Emitter;
import rx.Observable;

import static rx.AsyncEmitter.BackpressureMode.LATEST;
import static rx.Emitter.BackpressureMode.LATEST;

/**
* Decrypts data with fingerprint authentication. Initializes a {@link Cipher} for decryption which
Expand Down Expand Up @@ -70,7 +70,7 @@ private FingerprintDecryptionObservable(Context context, String keyName, String
* @return Observable result of the decryption
*/
static Observable<FingerprintDecryptionResult> create(Context context, String keyName, String encrypted) {
return Observable.fromAsync(new FingerprintDecryptionObservable(context, keyName, encrypted), LATEST);
return Observable.fromEmitter(new FingerprintDecryptionObservable(context, keyName, encrypted), LATEST);
}

/**
Expand All @@ -82,12 +82,12 @@ static Observable<FingerprintDecryptionResult> create(Context context, String ke
* @return Observable result of the decryption
*/
static Observable<FingerprintDecryptionResult> create(Context context, String encrypted) {
return Observable.fromAsync(new FingerprintDecryptionObservable(context, null, encrypted), LATEST);
return Observable.fromEmitter(new FingerprintDecryptionObservable(context, null, encrypted), LATEST);
}

@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(AsyncEmitter<FingerprintDecryptionResult> subscriber) {
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Emitter<FingerprintDecryptionResult> subscriber) {
CryptoProvider cryptoProvider = new CryptoProvider(this.context, this.keyName);
try {
Cipher cipher = cryptoProvider.initDecryptionCipher(encryptedData.getIv());
Expand All @@ -99,7 +99,7 @@ protected FingerprintManagerCompat.CryptoObject initCryptoObject(AsyncEmitter<Fi
}

@Override
protected void onAuthenticationSucceeded(AsyncEmitter<FingerprintDecryptionResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
protected void onAuthenticationSucceeded(Emitter<FingerprintDecryptionResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
try {
Cipher cipher = result.getCryptoObject().getCipher();
String decrypted = new String(cipher.doFinal(encryptedData.getMessage()));
Expand All @@ -113,12 +113,12 @@ protected void onAuthenticationSucceeded(AsyncEmitter<FingerprintDecryptionResul
}

@Override
protected void onAuthenticationHelp(AsyncEmitter<FingerprintDecryptionResult> emitter, int helpMessageId, String helpString) {
protected void onAuthenticationHelp(Emitter<FingerprintDecryptionResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.HELP, helpString, null));
}

@Override
protected void onAuthenticationFailed(AsyncEmitter<FingerprintDecryptionResult> emitter) {
protected void onAuthenticationFailed(Emitter<FingerprintDecryptionResult> emitter) {
emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.FAILED, null, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;

import rx.AsyncEmitter;
import rx.Emitter;
import rx.Observable;

import static rx.AsyncEmitter.BackpressureMode.LATEST;
import static rx.Emitter.BackpressureMode.LATEST;

/**
* Encrypts data with fingerprint authentication. Initializes a {@link Cipher} for encryption which
Expand Down Expand Up @@ -74,7 +74,7 @@ private FingerprintEncryptionObservable(Context context, String keyName, String
* @param toEncrypt data to encrypt @return Observable {@link FingerprintEncryptionResult}
*/
static Observable<FingerprintEncryptionResult> create(Context context, String keyName, String toEncrypt) {
return Observable.fromAsync(new FingerprintEncryptionObservable(context, keyName, toEncrypt), LATEST);
return Observable.fromEmitter(new FingerprintEncryptionObservable(context, keyName, toEncrypt), LATEST);
}

/**
Expand All @@ -85,12 +85,12 @@ static Observable<FingerprintEncryptionResult> create(Context context, String ke
* @param toEncrypt data to encrypt @return Observable {@link FingerprintEncryptionResult}
*/
static Observable<FingerprintEncryptionResult> create(Context context, String toEncrypt) {
return Observable.fromAsync(new FingerprintEncryptionObservable(context, null, toEncrypt), LATEST);
return Observable.fromEmitter(new FingerprintEncryptionObservable(context, null, toEncrypt), LATEST);
}

@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(AsyncEmitter<FingerprintEncryptionResult> emitter) {
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Emitter<FingerprintEncryptionResult> emitter) {
CryptoProvider cryptoProvider = new CryptoProvider(this.context, this.keyName);
try {
Cipher cipher = cryptoProvider.initEncryptionCipher();
Expand All @@ -103,7 +103,7 @@ protected FingerprintManagerCompat.CryptoObject initCryptoObject(AsyncEmitter<Fi
}

@Override
protected void onAuthenticationSucceeded(AsyncEmitter<FingerprintEncryptionResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
protected void onAuthenticationSucceeded(Emitter<FingerprintEncryptionResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
try {
Cipher cipher = result.getCryptoObject().getCipher();
byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes("UTF-8"));
Expand All @@ -119,12 +119,12 @@ protected void onAuthenticationSucceeded(AsyncEmitter<FingerprintEncryptionResul
}

@Override
protected void onAuthenticationHelp(AsyncEmitter<FingerprintEncryptionResult> emitter, int helpMessageId, String helpString) {
protected void onAuthenticationHelp(Emitter<FingerprintEncryptionResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.HELP, helpString, null));
}

@Override
protected void onAuthenticationFailed(AsyncEmitter<FingerprintEncryptionResult> emitter) {
protected void onAuthenticationFailed(Emitter<FingerprintEncryptionResult> emitter) {
emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.FAILED, null, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import com.mtramin.rxfingerprint.data.FingerprintAuthenticationException;

import rx.AsyncEmitter;
import rx.Emitter;
import rx.Subscriber;
import rx.functions.Action1;
import rx.subscriptions.Subscriptions;
Expand All @@ -36,10 +36,10 @@
* Base observable for Fingerprint authentication. Provides abstract methods that allow
* to alter the input and result of the authentication.
*/
abstract class FingerprintObservable<T> implements Action1<AsyncEmitter<T>> {
abstract class FingerprintObservable<T> implements Action1<Emitter<T>> {

protected final Context context;
private CancellationSignal cancellationSignal;
protected final Context context;
private CancellationSignal cancellationSignal;

/**
* Default constructor for fingerprint authentication
Expand All @@ -57,94 +57,96 @@ abstract class FingerprintObservable<T> implements Action1<AsyncEmitter<T>> {
this.context = context;
}

@Override
public void call(AsyncEmitter<T> asyncEmitter) {
if (!RxFingerprint.isAvailable(context)) {
asyncEmitter.onError(new IllegalAccessException("Fingerprint authentication is not available on this device! Ensure that the device has a Fingerprint sensor and enrolled Fingerprints by calling RxFingerprint#available(Context) first"));
}

AuthenticationCallback callback = createAuthenticationCallback(asyncEmitter);
cancellationSignal = new CancellationSignal();
FingerprintManagerCompat.CryptoObject cryptoObject = initCryptoObject(asyncEmitter);
FingerprintManagerCompat.from(context).authenticate(cryptoObject, 0, cancellationSignal, callback, null);

asyncEmitter.setSubscription(Subscriptions.create(() -> {
if (cancellationSignal != null && !cancellationSignal.isCanceled()) {
cancellationSignal.cancel();
}
}));

}

@NonNull
private AuthenticationCallback createAuthenticationCallback(final AsyncEmitter<T> emitter) {
return new AuthenticationCallback() {
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
super.onAuthenticationError(errMsgId, errString);
emitter.onError(new FingerprintAuthenticationException(errString));
}

@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
FingerprintObservable.this.onAuthenticationFailed(emitter);
}

@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
super.onAuthenticationHelp(helpMsgId, helpString);
FingerprintObservable.this.onAuthenticationHelp(emitter, helpMsgId, helpString.toString());
}

@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
FingerprintObservable.this.onAuthenticationSucceeded(emitter, result);
}
};
}

/**
* Method to initialize the {@link android.support.v4.hardware.fingerprint.FingerprintManagerCompat.CryptoObject}
* used for the fingerprint authentication.
*
* @param subscriber current subscriber
* @return a {@link android.support.v4.hardware.fingerprint.FingerprintManagerCompat.CryptoObject}
* that is to be used in the authentication. May be {@code null}.
*/
@Nullable
protected abstract FingerprintManagerCompat.CryptoObject initCryptoObject(AsyncEmitter<T> subscriber);

/**
* Action to execute when fingerprint authentication was successful.
* Should return the needed result via the given {@link Subscriber}.
* <p/>
* Should call {@link Subscriber#onCompleted()}.
* @param subscriber current subscriber
* @param result result of the successful fingerprint authentication
*/
protected abstract void onAuthenticationSucceeded(AsyncEmitter<T> subscriber, FingerprintManagerCompat.AuthenticationResult result);

/**
* Action to execute when the fingerprint authentication returned a help result.
* Should return the needed actions to the subscriber via the given {@link Subscriber}.
* <p/>
* Should <b>not</b> {@link Subscriber#onCompleted()}.
* @param subscriber current subscriber
* @param helpMessageId ID of the help message returned from the {@link FingerprintManagerCompat}
* @param helpString Help message string returned by the {@link FingerprintManagerCompat}
*/
protected abstract void onAuthenticationHelp(AsyncEmitter<T> subscriber, int helpMessageId, String helpString);

/**
* Action to execute when the fingerprint authentication failed.
* Should return the needed action to the given {@link Subscriber}
* <p/>
* Should only call {@link Subscriber#onCompleted()} when fingerprint authentication should be
* canceled due to the failed event.
*
* @param subscriber current subscriber
*/
protected abstract void onAuthenticationFailed(AsyncEmitter<T> subscriber);
@Override
public void call(Emitter<T> emitter) {
if (!RxFingerprint.isAvailable(context)) {
emitter.onError(new IllegalAccessException("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"));
}

AuthenticationCallback callback = createAuthenticationCallback(emitter);
cancellationSignal = new CancellationSignal();
FingerprintManagerCompat.CryptoObject cryptoObject = initCryptoObject(emitter);
FingerprintManagerCompat.from(context).authenticate(cryptoObject, 0, cancellationSignal, callback, null);

emitter.setSubscription(Subscriptions.create(() -> {
if (cancellationSignal != null && !cancellationSignal.isCanceled()) {
cancellationSignal.cancel();
}
}));

}

@NonNull
private AuthenticationCallback createAuthenticationCallback(Emitter<T> emitter) {
return new AuthenticationCallback() {
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
super.onAuthenticationError(errMsgId, errString);
emitter.onError(new FingerprintAuthenticationException(errString));
}

@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
FingerprintObservable.this.onAuthenticationFailed(emitter);
}

@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
super.onAuthenticationHelp(helpMsgId, helpString);
FingerprintObservable.this.onAuthenticationHelp(emitter, helpMsgId, helpString.toString());
}

@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
FingerprintObservable.this.onAuthenticationSucceeded(emitter, result);
}
};
}

/**
* Method to initialize the {@link FingerprintManagerCompat.CryptoObject}
* used for the fingerprint authentication.
*
* @param emitter current emitter
* @return a {@link FingerprintManagerCompat.CryptoObject}
* that is to be used in the authentication. May be {@code null}.
*/
@Nullable
protected abstract FingerprintManagerCompat.CryptoObject initCryptoObject(Emitter<T> emitter);

/**
* Action to execute when fingerprint authentication was successful.
* Should return the needed result via the given {@link Subscriber}.
* <p/>
* Should call {@link Subscriber#onCompleted()}.
*
* @param emitter current emitter
* @param result result of the successful fingerprint authentication
*/
protected abstract void onAuthenticationSucceeded(Emitter<T> emitter, FingerprintManagerCompat.AuthenticationResult result);

/**
* Action to execute when the fingerprint authentication returned a help result.
* Should return the needed actions to the subscriber via the given {@link Subscriber}.
* <p/>
* Should <b>not</b> {@link Subscriber#onCompleted()}.
*
* @param emitter current emitter
* @param helpMessageId ID of the help message returned from the {@link FingerprintManagerCompat}
* @param helpString Help message string returned by the {@link FingerprintManagerCompat}
*/
protected abstract void onAuthenticationHelp(Emitter<T> emitter, int helpMessageId, String helpString);

/**
* Action to execute when the fingerprint authentication failed.
* Should return the needed action to the given {@link Subscriber}
* <p/>
* Should only call {@link Subscriber#onCompleted()} when fingerprint authentication should be
* canceled due to the failed event.
*
* @param emitter current emitter
*/
protected abstract void onAuthenticationFailed(Emitter<T> emitter);
}

0 comments on commit 022e8cc

Please sign in to comment.