Search in sources :

Example 1 with SingleOnSubscribe

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();
                }
            });
        }
    });
}
Also used : HistoryItem(acr.browser.lightning.database.HistoryItem) FileWriter(java.io.FileWriter) SingleOnSubscribe(com.anthonycr.bonsai.SingleOnSubscribe) IOException(java.io.IOException) List(java.util.List) File(java.io.File) Nullable(android.support.annotation.Nullable) NonNull(android.support.annotation.NonNull)

Example 2 with SingleOnSubscribe

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);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) HistoryItem(acr.browser.lightning.database.HistoryItem) SingleOnSubscribe(com.anthonycr.bonsai.SingleOnSubscribe) Activity(android.app.Activity) Dialog(android.app.Dialog) BrowserDialog(acr.browser.lightning.dialog.BrowserDialog) AlertDialog(android.support.v7.app.AlertDialog) NonNull(android.support.annotation.NonNull) ArrayList(java.util.ArrayList) List(java.util.List) CompletableOnSubscribe(com.anthonycr.bonsai.CompletableOnSubscribe) Nullable(android.support.annotation.Nullable)

Example 3 with SingleOnSubscribe

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;
    }
}
Also used : HistoryItem(acr.browser.lightning.database.HistoryItem) SingleOnSubscribe(com.anthonycr.bonsai.SingleOnSubscribe) Activity(android.app.Activity) Source(acr.browser.lightning.database.bookmark.BookmarkLocalSync.Source) PermissionsResultAction(com.anthonycr.grant.PermissionsResultAction) ArrayList(java.util.ArrayList) List(java.util.List) CompletableOnSubscribe(com.anthonycr.bonsai.CompletableOnSubscribe) File(java.io.File) Nullable(android.support.annotation.Nullable)

Example 4 with SingleOnSubscribe

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();
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) HistoryPage(acr.browser.lightning.constant.HistoryPage) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) SingleOnSubscribe(com.anthonycr.bonsai.SingleOnSubscribe) BookmarkPage(acr.browser.lightning.constant.BookmarkPage) DownloadsPage(acr.browser.lightning.constant.DownloadsPage) StartPage(acr.browser.lightning.constant.StartPage) Dialog(android.app.Dialog) BrowserDialog(acr.browser.lightning.dialog.BrowserDialog) AlertDialog(android.support.v7.app.AlertDialog) LightningView(acr.browser.lightning.view.LightningView) Nullable(android.support.annotation.Nullable)

Example 5 with SingleOnSubscribe

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;
}
Also used : NonNull(android.support.annotation.NonNull) SingleOnSubscribe(com.anthonycr.bonsai.SingleOnSubscribe) ProgressDialog(android.app.ProgressDialog) Nullable(android.support.annotation.Nullable)

Aggregations

Nullable (android.support.annotation.Nullable)5 SingleOnSubscribe (com.anthonycr.bonsai.SingleOnSubscribe)5 HistoryItem (acr.browser.lightning.database.HistoryItem)3 NonNull (android.support.annotation.NonNull)3 List (java.util.List)3 BrowserDialog (acr.browser.lightning.dialog.BrowserDialog)2 Activity (android.app.Activity)2 Dialog (android.app.Dialog)2 DialogInterface (android.content.DialogInterface)2 AlertDialog (android.support.v7.app.AlertDialog)2 CompletableOnSubscribe (com.anthonycr.bonsai.CompletableOnSubscribe)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 BookmarkPage (acr.browser.lightning.constant.BookmarkPage)1 DownloadsPage (acr.browser.lightning.constant.DownloadsPage)1 HistoryPage (acr.browser.lightning.constant.HistoryPage)1 StartPage (acr.browser.lightning.constant.StartPage)1 Source (acr.browser.lightning.database.bookmark.BookmarkLocalSync.Source)1 LightningView (acr.browser.lightning.view.LightningView)1 ProgressDialog (android.app.ProgressDialog)1