use of rx.Subscriber in project wh-app-android by WhiteHouse.
the class FeedManager method updateFavorites.
public static void updateFavorites(Context ctx) {
sFavoritesSubject.onNext(Observable.create((Subscriber<? super List<FeedItem>> op) -> {
final FavoritesMap map;
final InputStreamReader isr;
final List<FeedItem> favorites = new ArrayList<>();
final Gson gson = GsonUtils.createGsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
try {
isr = new InputStreamReader(FavoritesUtils.getInputStream(ctx));
map = gson.fromJson(isr, FavoritesMap.class);
if (map.articles() != null) {
processAndStoreFavorites(map.articles(), favorites, FeedType.TYPE_ARTICLE);
}
if (map.photos() != null) {
processAndStoreFavorites(map.photos(), favorites, FeedType.TYPE_PHOTO);
}
if (map.videos() != null) {
processAndStoreFavorites(map.videos(), favorites, FeedType.TYPE_VIDEO);
}
Collections.sort(favorites, (lhs, rhs) -> {
if (lhs.feedTitle() != null && rhs.feedTitle() != null) {
return lhs.feedTitle().compareTo(rhs.feedTitle());
}
return 1;
});
} catch (FileNotFoundException ignored) {
}
op.onNext(favorites);
op.onCompleted();
}).subscribeOn(Schedulers.io()));
}
use of rx.Subscriber in project AntennaPod by AntennaPod.
the class OnlineFeedViewActivity method parseFeed.
private void parseFeed() {
if (feed == null || feed.getFile_url() == null && feed.isDownloaded()) {
throw new IllegalStateException("feed must be non-null and downloaded when parseFeed is called");
}
Log.d(TAG, "Parsing feed");
parser = Observable.create(new Observable.OnSubscribe<FeedHandlerResult>() {
@Override
public void call(Subscriber<? super FeedHandlerResult> subscriber) {
FeedHandler handler = new FeedHandler();
try {
FeedHandlerResult result = handler.parseFeed(feed);
subscriber.onNext(result);
} catch (UnsupportedFeedtypeException e) {
Log.d(TAG, "Unsupported feed type detected");
if (TextUtils.equals("html", e.getRootElement().toLowerCase())) {
showFeedDiscoveryDialog(new File(feed.getFile_url()), feed.getDownload_url());
} else {
subscriber.onError(e);
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
subscriber.onError(e);
} finally {
boolean rc = new File(feed.getFile_url()).delete();
Log.d(TAG, "Deleted feed source file. Result: " + rc);
subscriber.onCompleted();
}
}
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
beforeShowFeedInformation(result.feed);
showFeedInformation(result.feed, result.alternateFeedUrls);
}, error -> {
String errorMsg = DownloadError.ERROR_PARSER_EXCEPTION.getErrorString(OnlineFeedViewActivity.this) + " (" + error.getMessage() + ")";
showErrorDialog(errorMsg);
});
}
use of rx.Subscriber in project AntennaPod by AntennaPod.
the class OnlineFeedViewActivity method startFeedDownload.
private void startFeedDownload(String url, String username, String password) {
Log.d(TAG, "Starting feed download");
url = URLChecker.prepareURL(url);
feed = new Feed(url, null);
if (username != null && password != null) {
feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, username, password));
}
String fileUrl = new File(getExternalCacheDir(), FileNameGenerator.generateFileName(feed.getDownload_url())).toString();
feed.setFile_url(fileUrl);
final DownloadRequest request = new DownloadRequest(feed.getFile_url(), feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED, username, password, true, null);
download = Observable.create(new Observable.OnSubscribe<DownloadStatus>() {
@Override
public void call(Subscriber<? super DownloadStatus> subscriber) {
feeds = DBReader.getFeedList();
downloader = new HttpDownloader(request);
downloader.call();
Log.d(TAG, "Download was completed");
subscriber.onNext(downloader.getResult());
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(this::checkDownloadResult, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
use of rx.Subscriber in project AntennaPod by AntennaPod.
the class ProxyDialog method test.
private void test() {
if (subscription != null) {
subscription.unsubscribe();
}
if (!checkValidity()) {
setTestRequired(true);
return;
}
TypedArray res = context.getTheme().obtainStyledAttributes(new int[] { android.R.attr.textColorPrimary });
int textColorPrimary = res.getColor(0, 0);
res.recycle();
String checking = context.getString(R.string.proxy_checking);
txtvMessage.setTextColor(textColorPrimary);
txtvMessage.setText("{fa-circle-o-notch spin} " + checking);
txtvMessage.setVisibility(View.VISIBLE);
subscription = Observable.create(new Observable.OnSubscribe<Response>() {
@Override
public void call(Subscriber<? super Response> subscriber) {
String type = (String) spType.getSelectedItem();
String host = etHost.getText().toString();
String port = etPort.getText().toString();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
int portValue = 8080;
if (!TextUtils.isEmpty(port)) {
portValue = Integer.valueOf(port);
}
SocketAddress address = InetSocketAddress.createUnresolved(host, portValue);
Proxy.Type proxyType = Proxy.Type.valueOf(type.toUpperCase());
Proxy proxy = new Proxy(proxyType, address);
OkHttpClient.Builder builder = AntennapodHttpClient.newBuilder().connectTimeout(10, TimeUnit.SECONDS).proxy(proxy);
builder.interceptors().clear();
OkHttpClient client = builder.build();
if (!TextUtils.isEmpty(username)) {
String credentials = Credentials.basic(username, password);
client.interceptors().add(chain -> {
Request request = chain.request().newBuilder().header("Proxy-Authorization", credentials).build();
return chain.proceed(request);
});
}
Request request = new Request.Builder().url("http://www.google.com").head().build();
try {
Response response = client.newCall(request).execute();
subscriber.onNext(response);
} catch (IOException e) {
subscriber.onError(e);
}
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(response -> {
int colorId;
String icon;
String result;
if (response.isSuccessful()) {
colorId = R.color.download_success_green;
icon = "{fa-check}";
result = context.getString(R.string.proxy_test_successful);
} else {
colorId = R.color.download_failed_red;
icon = "{fa-close}";
result = context.getString(R.string.proxy_test_failed);
}
int color = ContextCompat.getColor(context, colorId);
txtvMessage.setTextColor(color);
String message = String.format("%s %s: %s", icon, result, response.message());
txtvMessage.setText(message);
setTestRequired(!response.isSuccessful());
}, error -> {
String icon = "{fa-close}";
String result = context.getString(R.string.proxy_test_failed);
int color = ContextCompat.getColor(context, R.color.download_failed_red);
txtvMessage.setTextColor(color);
String message = String.format("%s %s: %s", icon, result, error.getMessage());
txtvMessage.setText(message);
setTestRequired(true);
});
}
use of rx.Subscriber in project AndroidDevelop by 7449.
the class MainActivity method onPictureTaken.
@Override
public void onPictureTaken(CameraView cameraView, final byte[] data) {
subscribe = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "picture.jpg");
OutputStream os = new FileOutputStream(file);
os.write(data);
subscriber.onNext(getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath());
subscriber.onCompleted();
os.flush();
os.close();
} catch (IOException ignored) {
subscriber.onError(ignored);
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
Log.i(getClass().getSimpleName(), e.toString());
Toast.makeText(getApplicationContext(), "保存失败——> " + e.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNext(String s) {
Toast.makeText(getApplicationContext(), "保存路径为:--> " + s, Toast.LENGTH_SHORT).show();
}
});
}
Aggregations