Search code examples
javaandroidgoogle-chromebrowser-historyobservers

Browser ContentObserver not being called even using different URI's


My ContentObserver for observing the history in the browser is not being called. I don't understand why it isn't. I'm not doing anything different or bizarre, I'm following the API specs exactly, but to no avail! Below is my code:

In my service:

public class MonitorService extends Service {
    //some global variables declared here
    private ContentObserver historyObserver, searchObserver, chromeObserver;

    public void onCreate() {
        super.onCreate();
        isRunning = false;
        this.preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        //this.historyObserver = new HistoryObserver();
        this.historyObserver = new HistoryObserver(new Handler());
        this.searchObserver = new HistoryObserver(new Handler());
        this.chromeObserver = new HistoryObserver(new Handler());

        getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://com.android.chrome.browser/history"), false, this.chromeObserver);
        getApplicationContext().getContentResolver().registerContentObserver(android.provider.Browser.BOOKMARKS_URI, false, this.historyObserver);
        getApplicationContext().getContentResolver().registerContentObserver(android.provider.Browser.SEARCHES_URI, false, this.searchObserver);
    }
//Other required methods in class
}//end of class

Then in my HistoryObserver Class we have:

public class HistoryObserver extends ContentObserver {

    public final String TAG = "HistoryObserver";

    public HistoryObserver(Handler handler) {
        super(handler);
        Log.d(TAG, "Creating new HistoryObserver");
    }

    public HistoryObserver() {
        super(null);
        Log.d(TAG, "Creating a new HistoryObserver without a Handler");
    }

    @Override
    public boolean deliverSelfNotifications() {
        Log.d(TAG, "delivering self notifications");
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Log.d(TAG, "onChange without uri: " + selfChange);
        //onChange(selfChange, null);
    }

    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);
        Log.d(TAG, "onChange: " + selfChange + "\t " + uri.toString());
    }
}

Like I said there is nothing special or unique about this implementation. Yet, when I go go to a new website or search for something in Chrome, the onChange method is never fired.


Solution

  • I figured out the problem. The /history content provider isn't an observable. The observables come through the /bookmark uri. Once I discovered that, things got working very quickly and very well.