3

I want to create a bookmark in the stock android-browser from my own application. How can I do this?

I only found the Browser.saveBookmark-Method (api-doc) but this displays a new window where the user can change the data. As I want to import bookmarks from a external data-source I want to save the bookmark directly and not ask the user for input.

theomega
  • 31,591
  • 21
  • 89
  • 127

2 Answers2

4

If you just want to allow the user to add a bookmark, android.provider.Browser.saveBookmark() is what you want. But it looks like you're wanting to do mass updates, so that's probably not enough since it just opens the browser's bookmarks page.

AFAIK there is no open API that ties directly into the browser's bookmarks. However, there is a content resolver for it which can be accessed android.provider.Browser.BOOKMARKS_URI. Once you've resolved the provider you can manipulate bookmarks by running queries, provided you have the com.android.browser.permission.READ_HISTORY_BOOKMARKS and com.android.browser.permission.WRITE_HISTORY_BOOKMARKS permissions.

If you're unfamiliar with content providers, they can get kinda hairy (doubly so if you're unfamiliar with SQL). However, the knowledge base has some good articles on them, and a quick google for "android content provider tutorial" should get you well on your way.

Ginger McMurray
  • 1,275
  • 2
  • 20
  • 42
  • Also, the code for the browser itself can be read here: http://android.git.kernel.org/?p=platform/packages/apps/Browser.git;a=log;h=refs/tags/android-cts-2.2_r4. I recommend anyone wanting to do serious android work browse through the OS code for the things they'll be interacting with. – Ginger McMurray Jan 03 '11 at 23:29
  • could you please help me on that? http://stackoverflow.com/questions/13725192/how-can-i-get-set-bookmarks-per-account-on-android – Pascal Dec 10 '12 at 10:34
  • What help do you need specifically? Have you checked out the links? – Ginger McMurray Dec 11 '12 at 18:00
  • James,I would like to know How can I get/set bookmarks per account on Android. see my question http://stackoverflow.com/questions/13725192/how-can-i-get-set-bookmarks-per-account-on-android for more details. – Pascal Dec 12 '12 at 09:01
  • 1
    I'm not sure how the default browser does it, but google's source code is freely available. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.1.1_r1/com/android/browser/BrowserActivity.java?av=f – Ginger McMurray Dec 13 '12 at 17:35
  • James, I think I found the code thank to you: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.1.1_r1/com/android/browser/AddBookmarkPage.java?av=f Thank a lot. – Pascal Dec 15 '12 at 09:45
3

I took the following code from Android implementation (and disabled the "bookmark added" toast):

/**
 * Add a bookmark to the database.
 * 
 * @param context
 *            Context of the calling Activity. This is used to make Toast confirming that the bookmark has been added. If the caller provides null, the Toast will not be shown.
 * @param cr
 *            The ContentResolver being used to add the bookmark to the db.
 * @param url
 *            URL of the website to be bookmarked.
 * @param name
 *            Provided name for the bookmark.
 * @param thumbnail
 *            A thumbnail for the bookmark.
 * @param retainIcon
 *            Whether to retain the page's icon in the icon database. This will usually be <code>true</code> except when bookmarks are added by a settings restore agent.
 */
static void addBookmark(Context context, ContentResolver cr, String url, String name, Bitmap thumbnail, boolean retainIcon) {

    final String WHERE_CLAUSE = "url = ? OR url = ? OR url = ? OR url = ?";
    final String WHERE_CLAUSE_SECURE = "url = ? OR url = ?";
    String[] SELECTION_ARGS;

    // Want to append to the beginning of the list
    long creationTime = new Date().getTime();
    // First we check to see if the user has already visited this
    // site. They may have bookmarked it in a different way from
    // how it's stored in the database, so allow different combos
    // to map to the same url.
    boolean secure = false;
    String compareString = url;
    if (compareString.startsWith("http://")) {
        compareString = compareString.substring(7);
    } else if (compareString.startsWith("https://")) {
        compareString = compareString.substring(8);
        secure = true;
    }
    if (compareString.startsWith("www.")) {
        compareString = compareString.substring(4);
    }
    if (secure) {
        SELECTION_ARGS = new String[2];
        SELECTION_ARGS[0] = "https://" + compareString;
        SELECTION_ARGS[1] = "https://www." + compareString;
    } else {
        SELECTION_ARGS = new String[4];
        SELECTION_ARGS[0] = compareString;
        SELECTION_ARGS[1] = "www." + compareString;
        SELECTION_ARGS[2] = "http://" + compareString;
        SELECTION_ARGS[3] = "http://" + SELECTION_ARGS[1];
    }
    Cursor cursor = cr.query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, secure ? WHERE_CLAUSE_SECURE : WHERE_CLAUSE, SELECTION_ARGS, null);
    ContentValues map = new ContentValues();
    if (cursor.moveToFirst() && cursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) {
        // This means we have been to this site but not bookmarked
        // it, so convert the history item to a bookmark
        map.put(Browser.BookmarkColumns.CREATED, creationTime);
        map.put(Browser.BookmarkColumns.TITLE, name);
        map.put(Browser.BookmarkColumns.BOOKMARK, 1);
        // map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
        cr.update(Browser.BOOKMARKS_URI, map, "_id = " + cursor.getInt(0), null);
    } else {
        int count = cursor.getCount();
        boolean matchedTitle = false;
        for (int i = 0; i < count; i++) {
            // One or more bookmarks already exist for this site.
            // Check the names of each
            cursor.moveToPosition(i);
            if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX).equals(name)) {
                // The old bookmark has the same name.
                // Update its creation time.
                map.put(Browser.BookmarkColumns.CREATED, creationTime);
                cr.update(Browser.BOOKMARKS_URI, map, "_id = " + cursor.getInt(0), null);
                matchedTitle = true;
                break;
            }
        }
        if (!matchedTitle) {
            // Adding a bookmark for a site the user has visited,
            // or a new bookmark (with a different name) for a site
            // the user has visited
            map.put(Browser.BookmarkColumns.TITLE, name);
            map.put(Browser.BookmarkColumns.URL, url);
            map.put(Browser.BookmarkColumns.CREATED, creationTime);
            map.put(Browser.BookmarkColumns.BOOKMARK, 1);
            map.put(Browser.BookmarkColumns.DATE, 0);
            // map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
            int visits = 0;
            if (count > 0) {
                // The user has already bookmarked, and possibly
                // visited this site. However, they are creating
                // a new bookmark with the same url but a different
                // name. The new bookmark should have the same
                // number of visits as the already created bookmark.
                visits = cursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
            }
            // Bookmark starts with 3 extra visits so that it will
            // bubble up in the most visited and goto search box
            map.put(Browser.BookmarkColumns.VISITS, visits + 3);
            cr.insert(Browser.BOOKMARKS_URI, map);
        }
    }
    if (retainIcon) {
        WebIconDatabase.getInstance().retainIconForPageUrl(url);
    }
    cursor.close();

}
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
  • I was looking for an answer for saving bookmarks and I think I found the answer. But I do have a question, just to explain you what I am trying to achieve. I have created a webview something like google and the user can there search for urls and thing, I am trying to make the same with bookmarks like google to save the link. Do you think your answer will be enough or do I need to write a DB for that ? I have a recyclerview list which all the saved bookmarks the user is going to see there. – TheCoderGuy Feb 08 '19 at 09:11
  • It was 4 years ago, I'm no longer develop for android - sorry but I can't remember... – Asaf Pinhassi Feb 10 '19 at 09:21