Pages

Showing posts with label bookmarks. Show all posts
Showing posts with label bookmarks. Show all posts

Thursday, November 14, 2013

Browser History and bookmarks

In this post, you will learn to create a simple Android app to view browser history and bookmarks. This app displays the browser history and bookmarks in a single list. It can also be used to clear the browser history from your Android device. In addition, the user is able to click a link on the list to open the page on the browser.
To develop this app, you will need to create an Android project in Eclipse. The name of the project or application will be BClean. The BClean app has the simple user interface. When the app firstly launches, it displays a list of browser history and bookmarks. You will need to edit the activity_main.xml file to include a ListView component. Here is the content of the activity_main.xml file.

activity_main.xml file

<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=".MainActivity" >

    <ListView
     
        android:id="@+id/hb_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     
    </ListView>

</RelativeLayout>


Each item of the list will display three fields of each browser history or bookmark. The first field is the icon. The second field is the title and the third field is the url.

browser history and bookmarks view and clean


To customize the ListView component to allow each row or item to show the data in that way, you will need the listlayout.xml and ListAdapterModel.java files. The listyout.xml file represents the layout of each row or item of the list. There are three components in the listlayout file. The first component is an ImageView. It is for displaying the icon. The second and third components are TextView compoennts for displaying the title and url.

listlayout.xml file

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >
<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:padding="5sp"
    android:contentDescription="icon_image"
 />
<LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
    >

<TextView
       android:id="@+id/hbtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1sp"
        android:textSize="20sp"
        android:scrollHorizontally="true"
    android:singleLine="true"
        android:textStyle="bold" >
</TextView>
<TextView
       android:id="@+id/hburl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1sp"
        android:textSize="15sp"
        android:scrollHorizontally="true"
    android:singleLine="true"
        android:textColor="#0000ff">
 </TextView>
</LinearLayout>
</LinearLayout>

The ListAdapterModel.java file defines a class called ListAdapterModel that extends ArrayAdapter class. The object of the ListAdapterModel class will be used as the data source of the list. Below is the content of the ListAdapterModel. java file.

ListAdapterModel. java file

package com.example.bclean;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ListAdapterModel extends ArrayAdapter<String>{
int groupid;
ArrayList<String> titles;
ArrayList<String> urls;
ArrayList<Bitmap> bitmaps;
Context context;
String path;
public ListAdapterModel(Context context, int vg, int id, ArrayList<String> titles,ArrayList<String> urls,ArrayList<Bitmap> bitmaps){
super(context,vg, id, titles);
this.context=context;
groupid=vg;
this.titles=titles;
this.urls=urls;
this.bitmaps=bitmaps;

}
public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(groupid, parent, false);
        ImageView imageView = (ImageView) itemView.findViewById(R.id.icon);
        imageView.setImageBitmap(bitmaps.get(position));
        TextView textTitle= (TextView) itemView.findViewById(R.id.hbtitle);
        String title=titles.get(position);
        textTitle.setText(title);
        TextView textURL = (TextView) itemView.findViewById(R.id.hburl);
        String url=urls.get(position);
        textURL.setText(url);
        //make the url clickable
        Linkify.addLinks(textURL, Linkify.ALL);
        return itemView;
}


}

The most important file in this application is MainActivity.java file. This file contains code that reads, display  the browser history and bookmarks, and clean the history data.
In the onCreate method of the MainActivity class, three ArrayList objects are created by calling the createLists method. One ArrayList object is to store the titles of the browser history and bookmarks. One is for storing the urls. And the last one will store the icons. The three ArrayList objects will be supplied to the ListAdapterModel class to show the browser history and bookmarks on the list.

MainActivity.java file

package com.example.bclean;
import java.util.ArrayList;
import android.os.Bundle;
import android.provider.Browser;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ArrayList<String> titles;
    private ArrayList<String> urls;
    private ArrayList<Bitmap> bitmaps;
    private ContentResolver cr;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createLists();
       }
    protected void onResume(){
    super.onResume();
    getBH();      
    showHistoryBookmarks();
   
    }
 
    public void createLists(){
    titles=new ArrayList<String>();
    urls=new ArrayList<String>();
    bitmaps=new ArrayList<Bitmap>();
 
    }
    public void getBH(){
    Bitmap icon;
    cr=getContentResolver();
    String order=Browser.BookmarkColumns.DATE+" DESC";
    String[] projection={Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL,Browser.BookmarkColumns.FAVICON};
    //String selection=projection[0]+"=?";
    //String args[]={"Google"};
    Cursor rows=cr.query(Browser.BOOKMARKS_URI,projection, null,null,order);
    if(rows.getCount()>0){
    while(rows.moveToNext()) {
    //read title
    String title=rows.getString(rows.getColumnIndex(projection[0]));
    //read url
    String url=rows.getString(rows.getColumnIndex(projection[1]));
    //read icon
    byte[] bicon=rows.getBlob(rows.getColumnIndex(projection[2]));
    if(bicon!=null){
    //convert blob image data to Bitmap
    icon=BitmapFactory.decodeByteArray(bicon,0,bicon.length);  
   
    }
   
    else{
    //default icon for history and bookmarks that do not icons
    icon=BitmapFactory.decodeResource(getResources(),R.drawable.noicon);
    }
    //add to lists
    addToList(title,url,icon);
    }
    //close the cursor
    rows.close();
    }    
   
   
    }  
 
 
    public void addToList(String title,String url, Bitmap bitmap){
   
    titles.add(title);
    urls.add(url);
    bitmaps.add(bitmap);
   
    }

 
    public void showHistoryBookmarks(){
    ListView l=(ListView) findViewById(R.id.hb_list);
if(l!=null){
if(titles.size()>0){
ListAdapterModel aa=new ListAdapterModel(this,R.layout.listlayout,R.id.hbtitle,titles,urls,bitmaps);  
l.setAdapter(aa);
}
else{
Toast.makeText(this, "This is no bookmark or history.", Toast.LENGTH_SHORT).show();
}
}

}
 
    public void cleanHB(){
    if(Browser.canClearHistory(cr)){
    Browser.clearHistory(cr); //clear history data
    createLists(); //recreate the lists
    onResume(); //update the list
    }
   
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    public boolean onOptionsItemSelected(MenuItem item){
    if(item.getItemId()==R.id.action_clean)
    {
    cleanHB();
   
    }
    return false;
    }

 
}

The getHB method reads the browser history and bookmarks from Android and add the data (titles, urls, and icons) on the list. To get the browser history and bookmarks from Android, you will use the ContentResolver class. The instance of this class can be obtained by invoking the getContentResolver method from the current activity. By using the query method of the ContentResolver class, you can read all browser history and bookmarks. These history and bookmarks are stored in a Cursor object. The query method has five arguments. The first argument accepts the url that the data will be read from. This app reads the browser history and bookmarks. Thus, the url is BOOKMARKS_URI from the Browser class. The second argument allows you to specifiy an array of fields or column names to be retrieved. In this app, their are only three fields that will be retrieved TITLE, URL, and FAVICON. You will need to use the BookmarkColumns class in Browser class to access the field names of the table that stores the browser history and bookmarks. The third and forth arguments specifies the selection (field and operator) and selection arguments (values) to retrieve the rows of the table. Since the app retrieves all rows, you will use null values for these arguments. The last argument of the query method allows you to specify the field that the rows will be ordered by.
The title, and urls are text (string). The icon field is of Blob type. The data in Blob type can be converted to the Bitmap by using the decodeArrayByte method of the BitmapFactory class.
The showHistoryBookmarks will be called after the getHB method to display the titles, urls, and icons of the browser history and bookmarks on the list. In this method, the ListAdapterModel object is created and the titles, urls, and icons are passed to it.
The cleanHB method is called when the user touches the clean menu item. It will remove all history data from Android. The list will be updated to reflect the change by calling the onResume method. You will need to edit the main.xml file (in menu folder) to add the clean item to the menu. Here is the content of the main.xml file.
main.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_clean"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_clean"/>


</menu>

Before running the BClean app, you also need to edit the AndroidManifest.xml file to allow the application to read and write browser history and bookmarks. Please add the following permissions to the file.

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


Download the apk file of the BClean app

Merge or Combine PDF, Txt, Images