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

The following examples show how to use io.reactivex.ObservableEmitter#onError() . 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: RxCache.java    From RxEasyHttp with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(@NonNull ObservableEmitter<T> subscriber) throws Exception {
    try {
        T data = execute();
        if (!subscriber.isDisposed()) {
            subscriber.onNext(data);
        }
    } catch (Throwable e) {
        HttpLog.e(e.getMessage());
        if (!subscriber.isDisposed()) {
            subscriber.onError(e);
        }
        Exceptions.throwIfFatal(e);
        //RxJavaPlugins.onError(e);
        return;
    }

    if (!subscriber.isDisposed()) {
        subscriber.onComplete();
    }
}
 
Example 2
Source File: FileTailer.java    From websockets-log-tail with Apache License 2.0 6 votes vote down vote up
private TailerListenerAdapter createListener(final ObservableEmitter<String> emitter) {
    return new TailerListenerAdapter() {

        @Override
        public void fileRotated() {
            // ignore, just keep tailing
        }

        @Override
        public void handle(String line) {
            emitter.onNext(line);
        }

        @Override
        public void fileNotFound() {
            emitter.onError(new FileNotFoundException(file.toString()));
        }

        @Override
        public void handle(Exception ex) {
            emitter.onError(ex);
        }
    };
}
 
Example 3
Source File: FingerprintObservable.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
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));
			}
		}

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

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

		@Override
		public void onAuthenticationSucceeded(AuthenticationResult result) {
			FingerprintObservable.this.onAuthenticationSucceeded(emitter, result);
		}
	};
}
 
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: RsaEncryptionObservable.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(ObservableEmitter<FingerprintEncryptionResult> 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;
	}

	try {
		Cipher cipher = cipherProvider.getCipherForEncryption();
		byte[] encryptedBytes = cipher.doFinal(ConversionUtils.toBytes(toEncrypt));

		String encryptedString = encodingProvider.encode(encryptedBytes);
		emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.AUTHENTICATED, null, encryptedString));
		emitter.onComplete();
	} catch (Exception e) {
		Logger.error(String.format("Error writing value for key: %s", cipherProvider.keyName), e);
		emitter.onError(e);
	}
}
 
Example 6
Source File: AesEncryptionObservable.java    From RxFingerprint with Apache License 2.0 6 votes vote down vote up
@Override
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintEncryptionResult> emitter, AuthenticationResult result) {
	try {
		Cipher cipher = result.getCryptoObject().getCipher();
		byte[] encryptedBytes = cipher.doFinal(ConversionUtils.toBytes(toEncrypt));
		byte[] ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();

		String encryptedString = CryptoData.fromBytes(encodingProvider, encryptedBytes, ivBytes).toString();
		CryptoData.verifyCryptoDataString(encryptedString);

		emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.AUTHENTICATED, null, encryptedString));
		emitter.onComplete();
	} catch (Exception e) {
		emitter.onError(cipherProvider.mapCipherFinalOperationException(e));
	}
}
 
Example 7
Source File: SubscriptionProxy.java    From RxGroups with Apache License 2.0 6 votes vote down vote up
DisposableObserver<? super T> disposableWrapper(final ObservableEmitter<? super T> emitter) {
  return new DisposableObserver<T>() {
    @Override public void onNext(@NonNull T t) {
      if (!emitter.isDisposed()) {
        emitter.onNext(t);
      }
    }

    @Override public void onError(@NonNull Throwable e) {
      if (!emitter.isDisposed()) {
        emitter.onError(e);
      }
    }

    @Override public void onComplete() {
      if (!emitter.isDisposed()) {
        emitter.onComplete();
      }
    }
  };
}
 
Example 8
Source File: ReadTadPagePresenter.java    From GankGirl with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void subscribe(ObservableEmitter<List<ReadTypeBean>> subscriber) throws Exception {
    List<ReadTypeBean> datas = new ArrayList<>();
    try {
        Document doc = Jsoup.connect(Constants.API_URL_READ).get();
        Elements tads = doc.select("div#xiandu_cat").select("a");
        for (Element tad : tads) {
            ReadTypeBean bean = new ReadTypeBean();
            bean.setTitle(tad.text());//获取标题
            bean.setUrl(tad.absUrl("href"));//absUrl可以获取地址的绝对路径
            datas.add(bean);
            Log.v("Jsoup","title= "+bean.getTitle()+"   url= "+bean.getUrl());
        }
    } catch (IOException e) {
        subscriber.onError(e);
    }

    subscriber.onNext(datas);
    subscriber.onComplete();
}
 
Example 9
Source File: AesDecryptionObservable.java    From RxFingerprint with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected CryptoObject initCryptoObject(ObservableEmitter<FingerprintDecryptionResult> subscriber) {
	try {
		CryptoData cryptoData = CryptoData.fromString(encodingProvider, encryptedString);
		Cipher cipher = cipherProvider.getCipherForDecryption(cryptoData.getIv());
		return new CryptoObject(cipher);
	} catch (Exception e) {
		subscriber.onError(e);
		return null;
	}
}
 
Example 10
Source File: AesDecryptionObservable.java    From RxFingerprint with Apache License 2.0 5 votes vote down vote up
@Override
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintDecryptionResult> emitter, AuthenticationResult result) {
	try {
		CryptoData cryptoData = CryptoData.fromString(encodingProvider, encryptedString);
		Cipher cipher = result.getCryptoObject().getCipher();
		byte[] bytes = cipher.doFinal(cryptoData.getMessage());

		emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.AUTHENTICATED, null, ConversionUtils.toChars(bytes)));
		emitter.onComplete();
	} catch (Exception e) {
		emitter.onError(cipherProvider.mapCipherFinalOperationException(e));
	}

}
 
Example 11
Source File: RsaDecryptionObservable.java    From RxFingerprint with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected CryptoObject initCryptoObject(ObservableEmitter<FingerprintDecryptionResult> subscriber) {
	try {
		Cipher cipher = cipherProvider.getCipherForDecryption();
		return new CryptoObject(cipher);
	} catch (Exception e) {
		subscriber.onError(e);
		return null;
	}
}
 
Example 12
Source File: RsaDecryptionObservable.java    From RxFingerprint with Apache License 2.0 5 votes vote down vote up
@Override
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintDecryptionResult> emitter, AuthenticationResult result) {
	try {
		Cipher cipher = result.getCryptoObject().getCipher();
		byte[] bytes = cipher.doFinal(encodingProvider.decode(encryptedString));

		emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.AUTHENTICATED, null, ConversionUtils.toChars(bytes)));
		emitter.onComplete();
	} catch (Exception e) {
		Logger.error("Unable to decrypt given value. RxFingerprint is only able to decrypt values previously encrypted by RxFingerprint with the same encryption mode.", e);
		emitter.onError(cipherProvider.mapCipherFinalOperationException(e));
	}

}
 
Example 13
Source File: AesEncryptionObservable.java    From RxFingerprint with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected CryptoObject initCryptoObject(ObservableEmitter<FingerprintEncryptionResult> emitter) {
	try {
		Cipher cipher = cipherProvider.getCipherForEncryption();
		return new CryptoObject(cipher);
	} catch (Exception e) {
		emitter.onError(e);
		return null;
	}
}
 
Example 14
Source File: ReadMorePresenter.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void subscribe(ObservableEmitter<ReadTypeBean> subscriber) throws Exception {
    ReadTypeBean readTypeBean = new ReadTypeBean();
    readTypeBean.setTitle(requestContext.getType());
    readTypeBean.setUrl(requestContext.getUrl());
    List<ReadListBean> readListBeanList = new ArrayList<>();
    try {
        Document doc = Jsoup.connect(requestContext.getUrl()).get();
        Elements items = doc.select("div.xiandu_item");
        for (Element item : items) {
            ReadListBean bean = new ReadListBean();
            Elements aLeft = item.select("div.xiandu_left").select("a");
            bean.setTitle(aLeft.text());
            bean.setLink(aLeft.attr("href"));
            bean.setTime(item.select("small").text());
            Elements aRight = item.select("div.xiandu_right").select("a");
            bean.setSource(aRight.attr("title"));
            bean.setLogo(aRight.select("img").attr("src"));
            readListBeanList.add(bean);
        }
        Element button = doc.select("a.button").last();
        readTypeBean.setPage(button.absUrl("href"));
    } catch (IOException e) {
        subscriber.onError(e);
    }
    readTypeBean.setReadListBeanList(readListBeanList);
    subscriber.onNext(readTypeBean);
    subscriber.onComplete();
}
 
Example 15
Source File: NewArticleActivity.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 显示数据
 */
private void showEditData(ObservableEmitter<String> emitter, String html) {
    try {
        List<String> textList = HyperLibUtils.cutStringByImgTag(html);
        for (int i = 0; i < textList.size(); i++) {
            String text = textList.get(i);
            emitter.onNext(text);
        }
        emitter.onComplete();
    } catch (Exception e){
        e.printStackTrace();
        emitter.onError(e);
    }
}
 
Example 16
Source File: RimetLocalSource.java    From xposed-rimet with Apache License 2.0 5 votes vote down vote up
private <T> void subscribe(ObservableEmitter<T> emitter, OnHandler<T> handler) {

        try {
            // 开始处理
            T value = handler.onHandler();

            if (value != null) {
                // 返回结果
                emitter.onNext(value);
            }
            emitter.onComplete();
        } catch (Throwable tr) {
            emitter.onError(tr);
        }
    }
 
Example 17
Source File: SmsRepositoryImpl.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CyclomaticComplexity"})
private void executeSmsSending(ObservableEmitter<SmsSendingState> e, String number,
                               List<String> smsParts, int timeoutSeconds) {
    final long timeStarted = System.currentTimeMillis();
    SendingStateReceiver stateReceiver = new SendingStateReceiver(timeStarted,
            timeoutSeconds, sendSmsAction);
    context.registerReceiver(stateReceiver, new IntentFilter(sendSmsAction));
    int sentNumber = 0;
    sendSmsToOS(stateReceiver, number, smsParts);
    int totalMessages = smsParts.size();
    e.onNext(new SmsSendingState(0, totalMessages));

    while (stateReceiver.smsResultsWaiting() > 0 && !stateReceiver.isError() &&
            Utility.timeLeft(timeStarted, timeoutSeconds) > 0 && !e.isDisposed()) {
        // wait until timeout passes, response comes, or request disposed
        try {
            Thread.sleep(500);
        } catch (InterruptedException ie) {
            if (!e.isDisposed()) {
                e.onError(ie);
            }
            Utility.unregisterReceiver(context, stateReceiver);
            return;
        }
        int currentSentNumber = totalMessages - stateReceiver.smsResultsWaiting();
        if (currentSentNumber != sentNumber) {
            sentNumber = currentSentNumber;
            e.onNext(new SmsSendingState(sentNumber, totalMessages));
        }
    }
    Utility.unregisterReceiver(context, stateReceiver);

    if (e.isDisposed()) {
        return;
    }
    if (stateReceiver.smsResultsWaiting() == 0 && !stateReceiver.isError()) {
        e.onNext(new SmsSendingState(totalMessages, totalMessages));
        e.onComplete();
    } else if (stateReceiver.isError()) {
        e.onError(new ReceivedErrorException(stateReceiver.getErrorCode()));
    } else {
        e.onError(new ResultResponseException(ResultResponseIssue.TIMEOUT));
    }
}