Search in sources :

Example 1 with PermissionsResultAction

use of com.anthonycr.grant.PermissionsResultAction 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)

Aggregations

HistoryItem (acr.browser.lightning.database.HistoryItem)1 Source (acr.browser.lightning.database.bookmark.BookmarkLocalSync.Source)1 Activity (android.app.Activity)1 Nullable (android.support.annotation.Nullable)1 CompletableOnSubscribe (com.anthonycr.bonsai.CompletableOnSubscribe)1 SingleOnSubscribe (com.anthonycr.bonsai.SingleOnSubscribe)1 PermissionsResultAction (com.anthonycr.grant.PermissionsResultAction)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1