Android | Reading Browser Content Provider

Android Browser Content Provider

Android Browser Content Provider

A content provider presents data to external applications. Android comes with a number of content providers that store common data such as contact informations, calendar information, and media files, browser history and bookmarks. Here we will use Browser Provider to read default browser “Chrome” history and display the history on a list view.


( 1 ) Create a new Project

  • Application Name: Browser Bookmark
  • Project Name: android-read-browser-bookmark
  • Package Name: com.hmkcode.browser.bookmark

( 2 ) Main Activity Layout

The main layout “activity_main.xml” has one ListView

  • res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hmkcode.browser.bookmark.MainActivity" >

     <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
  		android:layout_height="fill_parent" />
     
</RelativeLayout>

( 3 ) ListView Items Layout

Each item in the list view will display “icon, date, title, type & url” about each link in the browser history.

  • /res/layout/link.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >
    
    <ImageView android:id="@+id/favicon"
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="20dp"
        android:minWidth="20dp"
        />
 
    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:text="Date:  "
        android:layout_below="@+id/favicon"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvDate"
        android:layout_below="@+id/tvDate"
        android:text="Title:  "
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
     <TextView
        android:id="@+id/tvBoV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvTitle"
        android:layout_below="@+id/tvTitle"
        android:text="Type: "
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/tvUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvTitle"
        android:layout_below="@+id/tvBoV"
        android:text="URL: "
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
    
    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/favicon"

        android:layout_toRightOf="@+id/tvDate"
        android:text="Date" />
 
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvDate"
        android:layout_toRightOf="@+id/tvTitle"
        android:text="Title" 
        android:ellipsize="end"
    	android:singleLine="true"/>
    
     <TextView
        android:id="@+id/bov"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"

        android:layout_toRightOf="@+id/tvBoV"
        android:text="Title" />
 
    <TextView
        android:id="@+id/url"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tvUrl"
                android:layout_below="@+id/tvBoV"
        android:text="URL" />
 
	
 
</RelativeLayout>

( 4 ) Cursor Adapter

The reuslt of querying the content provider “using contentResolver” is a cursor. We can map the results to the listview using CursorAdapter. CursorAdapter is a way to map each record in the cursor to a ListView item.

/src/com/hmkcode/browser/bookmark/MyCursorAdapter.java

package com.hmkcode.browser.bookmark;

import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.Browser;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyCursorAdapter extends CursorAdapter {

    private LayoutInflater cursorInflater;
    
    // Default constructor
    public MyCursorAdapter(Context context, Cursor cursor, int flags) {
    	super(context, cursor, flags);
        cursorInflater = (LayoutInflater) context.getSystemService(
                      Context.LAYOUT_INFLATER_SERVICE);
    }

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		
		TextView tvDate = (TextView) view.findViewById(R.id.date);
		TextView tvTitle = (TextView) view.findViewById(R.id.title);
		TextView tvBoV = (TextView) view.findViewById(R.id.bov);
		TextView tvURL = (TextView) view.findViewById(R.id.url);
		ImageView ivFavicon = (ImageView) view.findViewById(R.id.favicon);
		
	 //  long date = cursor.getLong(Browser.SEARCHES_PROJECTION_DATE_INDEX);
	 //  String title = cursor.getString(Browser.SEARCHES_PROJECTION_SEARCH_INDEX);
		
		long date = cursor.getLong(Browser.HISTORY_PROJECTION_DATE_INDEX);
		String title = cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
	   String url = cursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
	   byte[] icon = cursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
	   String bokmrk = cursor.getString(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX);
	   String visits = cursor.getString(Browser.HISTORY_PROJECTION_VISITS_INDEX);
	   String id = cursor.getString(Browser.HISTORY_PROJECTION_ID_INDEX);
	    
	 //  String names[] = cursor.getColumnNames();

	   
	   tvDate.setText(new Date(date).toString());
	   tvBoV.setText(bokmrk.equals("1")?"Bookmarked":"Visited");
	   tvTitle.setText(title);
	   tvURL.setText(url);
	   if(icon != null){
		   Bitmap bm = BitmapFactory.decodeByteArray(icon, 0, icon.length);
		   ivFavicon.setImageBitmap(bm);
	   }

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return cursorInflater.inflate(R.layout.link, parent, false);
	}

   
}

( 5 ) MainActivity

  • In the MainActivity we will use ContentResolver to query browser bookmarks as following.
  • Use BOOKMARKS_URI if you are looking for visited and bookmarked URLs.
  • Use SEARCHES_URI if you are looking for search terms.
  • All null parameters are optional, they can be used to filter the results.
Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, 
null, null, null);
  • The returned cursor will be used to create CursorAdapter
MyCursorAdapter myCursorAdapter = new MyCursorAdapter(this,cursor,0);
  • Then we will set the ListView adapter to myCursorAdapter
listView.setAdapter(myCursorAdapter);

/src/com/hmkcode/browser/bookmark/MainActivity.java

package com.hmkcode.browser.bookmark;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;

import android.widget.ListView;

public class MainActivity extends Activity {

	ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.listView1);
		getBrowserHist();
	}
  
	public void getBrowserHist()  {

        Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, null, null, null);
        
       // Cursor cursor = getContentResolver().query(Browser.SEARCHES_URI, null, null, null, null);
        	 
        MyCursorAdapter myCursorAdapter = new MyCursorAdapter(this,cursor,0);
		 listView.setAdapter(myCursorAdapter);      
      }
}

( 6 ) Permissions

In AndroidManifest.xml add the following permission.

     <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>

Source Code @ GitHub

Leave a Reply