use of com.anthonycr.bonsai.SingleOnSubscribe in project Lightning-Browser by anthonycr.
the class HistoryPage method getHistoryPage.
@NonNull
public Single<String> getHistoryPage() {
return Single.create(new SingleAction<String>() {
@Override
public void onSubscribe(@NonNull final SingleSubscriber<String> subscriber) {
final StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
HistoryModel.lastHundredVisitedHistoryItems().subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable List<HistoryItem> item) {
Preconditions.checkNonNull(item);
Iterator<HistoryItem> it = item.iterator();
HistoryItem helper;
while (it.hasNext()) {
helper = it.next();
historyBuilder.append(PART1);
historyBuilder.append(helper.getUrl());
historyBuilder.append(PART2);
historyBuilder.append(helper.getTitle());
historyBuilder.append(PART3);
historyBuilder.append(helper.getUrl());
historyBuilder.append(PART4);
}
historyBuilder.append(END);
File historyWebPage = new File(mApp.getFilesDir(), FILENAME);
FileWriter historyWriter = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
historyWriter = new FileWriter(historyWebPage, false);
historyWriter.write(historyBuilder.toString());
} catch (IOException e) {
Log.e(TAG, "Unable to write history page to disk", e);
} finally {
Utils.close(historyWriter);
}
subscriber.onItem(Constants.FILE + historyWebPage);
subscriber.onComplete();
}
});
}
});
}
use of com.anthonycr.bonsai.SingleOnSubscribe in project Lightning-Browser by anthonycr.
the class BookmarkSettingsFragment method createDialog.
private void createDialog() {
if (mActivity == null) {
return;
}
final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
final String title = getString(R.string.title_chooser);
builder.setTitle(title + ": " + Environment.getExternalStorageDirectory());
if (mFileList == null) {
Dialog dialog = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog);
return;
}
builder.setItems(mFileNameList, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mFileList[which].isDirectory()) {
builder.setTitle(title + ": " + mFileList[which]);
loadFileList(mFileList[which]);
builder.setItems(mFileNameList, this);
Dialog dialog1 = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog1);
} else {
BookmarkExporter.importBookmarksFromFile(mFileList[which]).subscribeOn(Schedulers.io()).subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable final List<HistoryItem> importList) {
Preconditions.checkNonNull(importList);
mBookmarkManager.addBookmarkList(importList).observeOn(Schedulers.main()).subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
Activity activity = getActivity();
if (activity != null) {
String message = activity.getResources().getString(R.string.message_import);
Utils.showSnackbar(activity, importList.size() + " " + message);
}
}
});
}
@Override
public void onError(@NonNull Throwable throwable) {
Log.e(TAG, "onError: importing bookmarks", throwable);
Utils.createInformativeDialog(getActivity(), R.string.title_error, R.string.import_bookmark_error);
}
});
}
}
});
Dialog dialog = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog);
}
use of com.anthonycr.bonsai.SingleOnSubscribe in project Lightning-Browser by anthonycr.
the class BookmarkSettingsFragment method onPreferenceClick.
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
switch(preference.getKey()) {
case SETTINGS_EXPORT:
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(getActivity(), REQUIRED_PERMISSIONS, new PermissionsResultAction() {
@Override
public void onGranted() {
mBookmarkManager.getAllBookmarks().subscribeOn(Schedulers.io()).subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable List<HistoryItem> item) {
Preconditions.checkNonNull(item);
final File exportFile = BookmarkExporter.createNewExportFile();
BookmarkExporter.exportBookmarksToFile(item, exportFile).subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
Activity activity = getActivity();
if (activity != null) {
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path) + ' ' + exportFile.getPath());
}
}
});
}
});
}
@Override
public void onDenied(String permission) {
//TODO Show message
}
});
return true;
case SETTINGS_IMPORT:
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(getActivity(), REQUIRED_PERMISSIONS, new PermissionsResultAction() {
@Override
public void onGranted() {
loadFileList(null);
createDialog();
}
@Override
public void onDenied(String permission) {
//TODO Show message
}
});
return true;
case SETTINGS_IMPORT_BROWSER:
getSync().getSupportedBrowsers().subscribeOn(Schedulers.worker()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<List<Source>>() {
@Override
public void onItem(@Nullable List<Source> item) {
Activity activity = getActivity();
if (item == null || activity == null) {
return;
}
List<String> titles = buildTitleList(activity, item);
showChooserDialog(activity, titles);
}
});
return true;
case SETTINGS_DELETE_BOOKMARKS:
showDeleteBookmarksDialog();
return true;
default:
return false;
}
}
use of com.anthonycr.bonsai.SingleOnSubscribe in project Lightning-Browser by anthonycr.
the class TabsManager method restoreLostTabs.
private void restoreLostTabs(@Nullable final String url, @NonNull final Activity activity, @NonNull final CompletableSubscriber subscriber) {
restoreState().subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new StreamOnSubscribe<Bundle>() {
@Override
public void onNext(@Nullable Bundle item) {
final LightningView tab = newTab(activity, "", false);
Preconditions.checkNonNull(item);
String url = item.getString(URL_KEY);
if (url != null && tab.getWebView() != null) {
if (UrlUtils.isBookmarkUrl(url)) {
new BookmarkPage(activity).getBookmarkPage().subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
} else if (UrlUtils.isDownloadsUrl(url)) {
new DownloadsPage().getDownloadsPage().subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
} else if (UrlUtils.isStartPageUrl(url)) {
new StartPage().getHomepage().subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
} else if (UrlUtils.isHistoryUrl(url)) {
new HistoryPage().getHistoryPage().subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
}
} else if (tab.getWebView() != null) {
tab.getWebView().restoreState(item);
}
}
@Override
public void onComplete() {
if (url != null) {
if (url.startsWith(Constants.FILE)) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
Dialog dialog = builder.setCancelable(true).setTitle(R.string.title_warning).setMessage(R.string.message_blocked_local).setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (mTabList.isEmpty()) {
newTab(activity, null, false);
}
finishInitialization();
subscriber.onComplete();
}
}).setNegativeButton(android.R.string.cancel, null).setPositiveButton(R.string.action_open, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
newTab(activity, url, false);
}
}).show();
BrowserDialog.setDialogSize(activity, dialog);
} else {
newTab(activity, url, false);
if (mTabList.isEmpty()) {
newTab(activity, null, false);
}
finishInitialization();
subscriber.onComplete();
}
} else {
if (mTabList.isEmpty()) {
newTab(activity, null, false);
}
finishInitialization();
subscriber.onComplete();
}
}
});
}
use of com.anthonycr.bonsai.SingleOnSubscribe in project Lightning-Browser by anthonycr.
the class ReadingActivity method loadPage.
private boolean loadPage(Intent intent) {
if (intent == null) {
return false;
}
mUrl = intent.getStringExtra(Constants.LOAD_READING_URL);
if (mUrl == null) {
return false;
}
if (getSupportActionBar() != null)
getSupportActionBar().setTitle(Utils.getDomainName(mUrl));
mPageLoaderSubscription = loadPage(mUrl).subscribeOn(Schedulers.worker()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<ReaderInfo>() {
@Override
public void onStart() {
mProgressDialog = new ProgressDialog(ReadingActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.show();
BrowserDialog.setDialogSize(ReadingActivity.this, mProgressDialog);
}
@Override
public void onItem(@Nullable ReaderInfo item) {
if (item == null || item.getTitle().isEmpty() || item.getBody().isEmpty()) {
setText(getString(R.string.untitled), getString(R.string.loading_failed));
} else {
setText(item.getTitle(), item.getBody());
}
}
@Override
public void onError(@NonNull Throwable throwable) {
setText(getString(R.string.untitled), getString(R.string.loading_failed));
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
@Override
public void onComplete() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
});
return true;
}
Aggregations