Android Browser.BOOKMARKS_URI does not work on all devices. How to find out the correct uri for a given device?
Asked Answered
A

2

9

I am trying to use Android Browser.BOOKMARKS_URI to CRUD device bookmarks from within my app ( https://play.google.com/store/apps/details?id=com.elementique.web )

It's working fine on most of the devices, but does not work on some :-(

On those devices, trying to use bookmarks leads to

java.lang.IllegalArgumentException: Unknown URL content://browser/bookmarks

I now understand that Boookmark Uri can be different than the AOSP default value (i.e. "content://browser/bookmarks").

Question:

How can I get the correct Bookmark Uri for a given device?

I already 'collected' the following URI

private static final Uri BOKKMARKS_DEFAULT = Browser.BOOKMARKS_URI; // = Uri.parse("content://browser/bookmarks")
private static final Uri BOKKMARKS_URI_CHROME = Uri.parse("content://com.android.chrome.browser/bookmarks");
private static final Uri BOKKMARKS_URI_SAMSUNG_S_ = Uri.parse("content://com.sec.android.app.sbrowser.browser/bookmarks");

Is it a way to 'list' all available content URI (content://...) on a devices? If yes, I could list them and search for occurrence of "/bookmarks" string and give a try with this URI.

Note:

I am currently in the process of creating a fallback mechanism if the app is not able to get a 'working' bookmark URI (i.e. my own Bookmark DB since I do need a bookmark feature in my app)

Argentine answered 20/1, 2015 at 8:14 Comment(0)
C
4

Is there a way to 'list' all available content URI (content://...) on a devices?

There's an opensource APP called "Content Provider Helper" in Play Store that can list all available content:// URI on the device. It uses PackageManager.GET_PROVIDER

Here's the respective class that searches all available content provider:

SearchProviderTask.java

package com.jensdriller.contentproviderhelper.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.net.Uri;

public class SearchProvidersTask extends DialogAsyncTask<Uri, Void, List<String>> {
    public SearchProvidersTask(Context context) {
        super(context);
    }

    @Override
    protected List<String> doInBackground(Uri... params) {
        List<String> contentProviders = new ArrayList<String>();

        try {
            PackageManager pm = mContext.getPackageManager();
            for (PackageInfo pack : pm.getInstalledPackages(PackageManager.GET_PROVIDERS)) {
                ProviderInfo[] providers = pack.providers;
                if (providers != null) {
                    for (ProviderInfo provider : providers) {
                        contentProviders.add("content://" + provider.authority);
                    }
                }
            }
        } catch (Exception e) {
            // PackageManager has died?
            mException = e;
        }

        // Sort alphabetically and ignore case sensitivity
        Collections.sort(contentProviders, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return lowerCase(lhs).compareTo(lowerCase(rhs));
            }

            private String lowerCase(String s) {
                return s.toLowerCase(Locale.getDefault());
            }
        });
        return contentProviders;
    }
}

Github: https://github.com/jenzz/ContentProviderHelper

Corruption answered 31/3, 2015 at 4:11 Comment(2)
Thanks! Seems to be almost what I am looking for. I'll just have to be able to filter contentProvider to keep bookmark. I don't think we can do better than that, so, from my point of view, it's the right answer. Thanks :-)Argentine
Hopefully it will help you. And if you managed to collects more browser URI, please share as well ;-) it will surely save time for all of us.Corruption
P
0

This is the query to search the bookmarks;

cursor = mContentResolver.query(Browser.BOOKMARKS_URI, BOOKMARKS_PROJECTION,
                Browser.BookmarkColumns.BOOKMARK + " == 1" + 
                    " AND LOWER(" + Browser.BookmarkColumns.TITLE + ") LIKE ?", 
                new String[] { searchText + "%" },
                Browser.BookmarkColumns.TITLE + " ASC");
Proletarian answered 9/2, 2015 at 6:14 Comment(1)
The question is "How to find out the correct uri for a given device?"Argentine

© 2022 - 2024 — McMap. All rights reserved.