Search in sources :

Example 11 with HistoryItem

use of acr.browser.lightning.database.HistoryItem 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 12 with HistoryItem

use of acr.browser.lightning.database.HistoryItem 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 13 with HistoryItem

use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.

the class BrowserActivity method addBookmark.

// By using a manager, adds a bookmark and notifies third parties about that
private void addBookmark(final String title, final String url) {
    final HistoryItem item = new HistoryItem(url, title);
    mBookmarkManager.addBookmarkIfNotExists(item).subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<Boolean>() {

        @Override
        public void onItem(@Nullable Boolean item) {
            if (Boolean.TRUE.equals(item)) {
                mSuggestionsAdapter.refreshBookmarks();
                mBookmarksView.handleUpdatedUrl(url);
            }
        }
    });
}
Also used : HistoryItem(acr.browser.lightning.database.HistoryItem)

Example 14 with HistoryItem

use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.

the class BrowserActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final LightningView currentView = mTabsManager.getCurrentTab();
    final String currentUrl = currentView != null ? currentView.getUrl() : null;
    // Handle action buttons
    switch(item.getItemId()) {
        case android.R.id.home:
            if (mDrawerLayout.isDrawerOpen(getBookmarkDrawer())) {
                mDrawerLayout.closeDrawer(getBookmarkDrawer());
            }
            return true;
        case R.id.action_back:
            if (currentView != null && currentView.canGoBack()) {
                currentView.goBack();
            }
            return true;
        case R.id.action_forward:
            if (currentView != null && currentView.canGoForward()) {
                currentView.goForward();
            }
            return true;
        case R.id.action_add_to_homescreen:
            if (currentView != null) {
                HistoryItem shortcut = new HistoryItem(currentView.getUrl(), currentView.getTitle());
                shortcut.setBitmap(currentView.getFavicon());
                Utils.createShortcut(this, shortcut);
            }
            return true;
        case R.id.action_new_tab:
            newTab(null, true);
            return true;
        case R.id.action_incognito:
            startActivity(new Intent(this, IncognitoActivity.class));
            overridePendingTransition(R.anim.slide_up_in, R.anim.fade_out_scale);
            return true;
        case R.id.action_share:
            new IntentUtils(this).shareUrl(currentUrl, currentView != null ? currentView.getTitle() : null);
            return true;
        case R.id.action_bookmarks:
            openBookmarks();
            return true;
        case R.id.action_copy:
            if (currentUrl != null && !UrlUtils.isSpecialUrl(currentUrl)) {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("label", currentUrl);
                clipboard.setPrimaryClip(clip);
                Utils.showSnackbar(this, R.string.message_link_copied);
            }
            return true;
        case R.id.action_settings:
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        case R.id.action_history:
            openHistory();
            return true;
        case R.id.action_downloads:
            openDownloads();
            return true;
        case R.id.action_add_bookmark:
            if (currentUrl != null && !UrlUtils.isSpecialUrl(currentUrl)) {
                addBookmark(currentView.getTitle(), currentUrl);
            }
            return true;
        case R.id.action_find:
            findInPage();
            return true;
        case R.id.action_reading_mode:
            if (currentUrl != null) {
                Intent read = new Intent(this, ReadingActivity.class);
                read.putExtra(Constants.LOAD_READING_URL, currentUrl);
                startActivity(read);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Also used : ClipboardManager(android.content.ClipboardManager) HistoryItem(acr.browser.lightning.database.HistoryItem) IntentUtils(acr.browser.lightning.utils.IntentUtils) LightningView(acr.browser.lightning.view.LightningView) Intent(android.content.Intent) ClipData(android.content.ClipData)

Example 15 with HistoryItem

use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.

the class BookmarkExporter method importBookmarksFromAssets.

/**
     * Retrieves all the default bookmarks stored
     * in the raw file within assets.
     *
     * @param context the context necessary to open assets.
     * @return a non null list of the bookmarks stored in assets.
     */
@NonNull
public static List<HistoryItem> importBookmarksFromAssets(@NonNull Context context) {
    List<HistoryItem> bookmarks = new ArrayList<>();
    BufferedReader bookmarksReader = null;
    InputStream inputStream = null;
    try {
        inputStream = context.getResources().openRawResource(R.raw.default_bookmarks);
        //noinspection IOResourceOpenedButNotSafelyClosed
        bookmarksReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = bookmarksReader.readLine()) != null) {
            try {
                JSONObject object = new JSONObject(line);
                HistoryItem item = new HistoryItem();
                item.setTitle(object.getString(KEY_TITLE));
                final String url = object.getString(KEY_URL);
                item.setUrl(url);
                item.setFolder(object.getString(KEY_FOLDER));
                item.setPosition(object.getInt(KEY_ORDER));
                item.setImageId(R.drawable.ic_bookmark);
                bookmarks.add(item);
            } catch (JSONException e) {
                Log.e(TAG, "Can't parse line " + line, e);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "Error reading the bookmarks file", e);
    } finally {
        Utils.close(bookmarksReader);
        Utils.close(inputStream);
    }
    return bookmarks;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) HistoryItem(acr.browser.lightning.database.HistoryItem) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) NonNull(android.support.annotation.NonNull)

Aggregations

HistoryItem (acr.browser.lightning.database.HistoryItem)22 NonNull (android.support.annotation.NonNull)11 ArrayList (java.util.ArrayList)9 Cursor (android.database.Cursor)5 WorkerThread (android.support.annotation.WorkerThread)5 Nullable (android.support.annotation.Nullable)4 IOException (java.io.IOException)4 List (java.util.List)4 CompletableOnSubscribe (com.anthonycr.bonsai.CompletableOnSubscribe)3 SingleOnSubscribe (com.anthonycr.bonsai.SingleOnSubscribe)3 File (java.io.File)3 InputStream (java.io.InputStream)3 JSONObject (org.json.JSONObject)3 Activity (android.app.Activity)2 Dialog (android.app.Dialog)2 DialogInterface (android.content.DialogInterface)2 AlertDialog (android.support.v7.app.AlertDialog)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 JSONArray (org.json.JSONArray)2