Search code examples
androidandroid-contentproviderbookmarksandroid-browser

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


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)


Solution

  • 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