Java Code Examples for io.reactivex.ObservableEmitter#setCancellable()

The following examples show how to use io.reactivex.ObservableEmitter#setCancellable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompositeBurstNodeService.java    From burstkit4j with Apache License 2.0 6 votes vote down vote up
private synchronized <T> void doIfUsedObservable(ObservableEmitter<T> emitter, AtomicInteger usedObservable, AtomicReferenceArray<Disposable> disposables, int myI, Runnable runnable) {
    int used = usedObservable.get();
    if (used == -1) {
        // We are the first!
        usedObservable.set(myI);
        runnable.run();
        // Kill all of the others.
        Disposable myDisposable = disposables.get(myI);
        disposables.set(myI, null);
        emitter.setCancellable(() -> {
            if (myDisposable != null) {
                myDisposable.dispose();
            }
        }); // Calling this calls the previous one, so all of the others get disposed.
    } else if (used == myI) {
        // We are the used observable.
        runnable.run();
    }
}
 
Example 2
Source File: ScanOperation.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@Override
final protected void protectedRun(final ObservableEmitter<SCAN_RESULT_TYPE> emitter, QueueReleaseInterface queueReleaseInterface) {

    final SCAN_CALLBACK_TYPE scanCallback = createScanCallback(emitter);

    try {
        emitter.setCancellable(new Cancellable() {
            @Override
            public void cancel() {
                RxBleLog.i("Scan operation is requested to stop.");
                stopScan(rxBleAdapterWrapper, scanCallback);
            }
        });
        RxBleLog.i("Scan operation is requested to start.");
        boolean startLeScanStatus = startScan(rxBleAdapterWrapper, scanCallback);

        if (!startLeScanStatus) {
            emitter.tryOnError(new BleScanException(BleScanException.BLUETOOTH_CANNOT_START));
        }
    } catch (Throwable throwable) {
        RxBleLog.w(throwable, "Error while calling the start scan function");
        emitter.tryOnError(new BleScanException(BleScanException.BLUETOOTH_CANNOT_START, throwable));
    } finally {
        queueReleaseInterface.release();
    }
}
 
Example 3
Source File: AppStateObservableOnSubscribe.java    From RxAppState with MIT License 6 votes vote down vote up
@Override
public void subscribe(@NonNull final ObservableEmitter<AppState> appStateEmitter) throws Exception {
  final AppStateListener appStateListener = new AppStateListener() {
    @Override
    public void onAppDidEnterForeground() {
      appStateEmitter.onNext(FOREGROUND);
    }

    @Override
    public void onAppDidEnterBackground() {
      appStateEmitter.onNext(BACKGROUND);
    }
  };

  appStateEmitter.setCancellable(new Cancellable() {
    @Override public void cancel() throws Exception {
      recognizer.removeListener(appStateListener);
      recognizer.stop();
    }
  });

  recognizer.addListener(appStateListener);
  recognizer.start();
}
 
Example 4
Source File: FingerprintObservable.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@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();
			}
		}
	});
}
 
Example 5
Source File: FeatureControllerOnSubscribe.java    From FeatureAdapter with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(final ObservableEmitter<FeatureEvent> subscriber) {
  verifyMainThread();

  FeatureEventListener listener =
      event -> {
        if (!subscriber.isDisposed()) {
          subscriber.onNext(event);
        }
      };

  subscriber.setCancellable(() -> featureController.removeFeatureEventListener(listener));

  featureController.addFeatureEventListener(listener);
}
 
Example 6
Source File: StoreOnSubscribe.java    From grox with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(ObservableEmitter<STATE> emitter) throws Exception {

  //the internal listener to the store.
  Store.StateChangeListener<STATE> listener =
      emitter::onNext;
  emitter.setCancellable(() -> store.unsubscribe(listener));
  store.subscribe(listener);
}
 
Example 7
Source File: RxKeyboard.java    From JianshuApp with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
    unregistrar = KeyboardVisibilityEvent.registerEventListener(activity, isOpen -> {
        if (!emitter.isDisposed()) {
            emitter.onNext(isOpen);
        }
    });
    emitter.setCancellable(() -> {
        activity = null;
        unregistrar.unregister();
        unregistrar = null;
    });
}
 
Example 8
Source File: QueueReleasingEmitterWrapper.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
public QueueReleasingEmitterWrapper(ObservableEmitter<T> emitter, QueueReleaseInterface queueReleaseInterface) {
    this.emitter = emitter;
    this.queueReleaseInterface = queueReleaseInterface;
    emitter.setCancellable(this);
}