Search in sources :

Example 16 with HistoryItem

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

the class LegacyBookmarkManager method destructiveGetBookmarks.

/**
     * Gets all bookmarks from the old bookmark file
     * and then deletes the file.
     *
     * @param application the context needed to open the file.
     * @return a list of bookmarks from the old bookmark file.
     */
@WorkerThread
@NonNull
public static List<HistoryItem> destructiveGetBookmarks(@NonNull Application application) {
    File filesDir = application.getFilesDir();
    List<HistoryItem> bookmarks = new ArrayList<>();
    final File bookmarksFile = new File(filesDir, FILE_BOOKMARKS);
    BufferedReader bookmarksReader = null;
    InputStream inputStream = null;
    try {
        if (bookmarksFile.exists() && bookmarksFile.isFile()) {
            //noinspection IOResourceOpenedButNotSafelyClosed
            inputStream = new FileInputStream(bookmarksFile);
        } else {
            return 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(TITLE));
                item.setUrl(object.getString(URL));
                item.setFolder(object.getString(FOLDER));
                item.setPosition(object.getInt(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);
    }
    bookmarksFile.delete();
    return bookmarks;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) FileInputStream(java.io.FileInputStream) 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) File(java.io.File) FileInputStream(java.io.FileInputStream) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 17 with HistoryItem

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

the class HistoryDatabase method visitHistoryItem.

@WorkerThread
synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) {
    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, title == null ? "" : title);
    values.put(KEY_TIME_VISITED, System.currentTimeMillis());
    Cursor cursor = lazyDatabase().query(false, TABLE_HISTORY, new String[] { KEY_URL }, KEY_URL + " = ?", new String[] { url }, null, null, null, "1");
    if (cursor.getCount() > 0) {
        lazyDatabase().update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[] { url });
    } else {
        addHistoryItem(new HistoryItem(url, title == null ? "" : title));
    }
    cursor.close();
}
Also used : ContentValues(android.content.ContentValues) HistoryItem(acr.browser.lightning.database.HistoryItem) Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread)

Example 18 with HistoryItem

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

the class HistoryDatabase method fromCursor.

@NonNull
private static HistoryItem fromCursor(@NonNull Cursor cursor) {
    HistoryItem historyItem = new HistoryItem();
    historyItem.setUrl(cursor.getString(1));
    historyItem.setTitle(cursor.getString(2));
    historyItem.setImageId(R.drawable.ic_history);
    return historyItem;
}
Also used : HistoryItem(acr.browser.lightning.database.HistoryItem) NonNull(android.support.annotation.NonNull)

Example 19 with HistoryItem

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

the class HistoryDatabase method getAllHistoryItems.

@WorkerThread
@NonNull
synchronized List<HistoryItem> getAllHistoryItems() {
    List<HistoryItem> itemList = new ArrayList<>();
    Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC");
    while (cursor.moveToNext()) {
        itemList.add(fromCursor(cursor));
    }
    cursor.close();
    return itemList;
}
Also used : HistoryItem(acr.browser.lightning.database.HistoryItem) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 20 with HistoryItem

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

the class LightningDialogBuilder method showLongPressedDialogForBookmarkUrl.

/**
     * Show the appropriated dialog for the long pressed link. It means that we try to understand
     * if the link is relative to a bookmark or is just a folder.
     *
     * @param activity used to show the dialog
     * @param url      the long pressed url
     */
public void showLongPressedDialogForBookmarkUrl(@NonNull final Activity activity, @NonNull final UIController uiController, @NonNull final String url) {
    final HistoryItem item;
    if (url.startsWith(Constants.FILE) && url.endsWith(BookmarkPage.FILENAME)) {
        // TODO hacky, make a better bookmark mechanism in the future
        final Uri uri = Uri.parse(url);
        final String filename = uri.getLastPathSegment();
        final String folderTitle = filename.substring(0, filename.length() - BookmarkPage.FILENAME.length() - 1);
        item = new HistoryItem();
        item.setIsFolder(true);
        item.setTitle(folderTitle);
        item.setImageId(R.drawable.ic_folder);
        item.setUrl(Constants.FOLDER + folderTitle);
        showBookmarkFolderLongPressedDialog(activity, uiController, item);
    } else {
        mBookmarkManager.findBookmarkForUrl(url).subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<HistoryItem>() {

            @Override
            public void onItem(@Nullable HistoryItem historyItem) {
                if (historyItem != null) {
                    showLongPressedDialogForBookmarkUrl(activity, uiController, historyItem);
                }
            }
        });
    }
}
Also used : HistoryItem(acr.browser.lightning.database.HistoryItem) Uri(android.net.Uri)

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