FileDocCategorySizeDatePackage
Browser.javaAPI DocAndroid 1.5 API18221Wed May 06 22:41:56 BST 2009android.provider

Browser

public class Browser extends Object

Fields Summary
private static final String
LOGTAG
public static final android.net.Uri
BOOKMARKS_URI
public static final String
INITIAL_ZOOM_LEVEL
The name of extra data when starting Browser with ACTION_VIEW or ACTION_SEARCH intent.

The value should be an integer between 0 and 1000. If not set or set to 0, the Browser will use default. If set to 100, the Browser will start with 100%.

public static final String
EXTRA_APPLICATION_ID
The name of the extra data when starting the Browser from another application.

The value is a unique identification string that will be used to indentify the calling application. The Browser will attempt to reuse the same window each time the application launches the Browser with the same identifier.

public static final String[]
HISTORY_PROJECTION
public static final int
HISTORY_PROJECTION_ID_INDEX
public static final int
HISTORY_PROJECTION_URL_INDEX
public static final int
HISTORY_PROJECTION_VISITS_INDEX
public static final int
HISTORY_PROJECTION_DATE_INDEX
public static final int
HISTORY_PROJECTION_BOOKMARK_INDEX
public static final int
HISTORY_PROJECTION_TITLE_INDEX
public static final int
HISTORY_PROJECTION_FAVICON_INDEX
public static final String[]
TRUNCATE_HISTORY_PROJECTION
public static final int
TRUNCATE_HISTORY_PROJECTION_ID_INDEX
public static final int
TRUNCATE_N_OLDEST
public static final android.net.Uri
SEARCHES_URI
public static final String[]
SEARCHES_PROJECTION
public static final int
SEARCHES_PROJECTION_SEARCH_INDEX
public static final int
SEARCHES_PROJECTION_DATE_INDEX
private static final String
SEARCHES_WHERE_CLAUSE
private static final int
MAX_HISTORY_COUNT
Constructors Summary
Methods Summary
public static final voidaddSearchUrl(android.content.ContentResolver cr, java.lang.String search)
Add a search string to the searches database.

param
cr The ContentResolver used to access the database.
param
search The string to add to the searches database.

        long now = new Date().getTime();
        try {
            Cursor c = cr.query(
                SEARCHES_URI,
                SEARCHES_PROJECTION,
                SEARCHES_WHERE_CLAUSE,
                new String [] { search },
                null);
            ContentValues map = new ContentValues();
            map.put(SearchColumns.SEARCH, search);
            map.put(SearchColumns.DATE, now);
            /* We should only get one answer that is exactly the same. */
            if (c.moveToFirst()) {
                cr.update(SEARCHES_URI, map, "_id = " + c.getInt(0), null);
            } else {
                cr.insert(SEARCHES_URI, map);
            }
            c.deactivate();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "addSearchUrl", e);
            return;
        }
    
public static final booleancanClearHistory(android.content.ContentResolver cr)
Returns whether there is any history to clear.

param
cr The ContentResolver used to access the database.
return
boolean True if the history can be cleared.

        try {
            Cursor c = cr.query(
                BOOKMARKS_URI,
                new String [] { BookmarkColumns._ID, 
                                BookmarkColumns.BOOKMARK,
                                BookmarkColumns.VISITS },
                "bookmark = 0 OR visits > 0", 
                null,
                null
                );
            boolean ret = c.moveToFirst();
            c.deactivate();
            return ret;
        } catch (IllegalStateException e) {
            return false;
        }
    
public static final voidclearHistory(android.content.ContentResolver cr)
Delete all entries from the bookmarks/history table which are not bookmarks. Also set all visited bookmarks to unvisited.

param
cr The ContentResolver used to access the database.

        deleteHistoryWhere(cr, null);
    
public static final voidclearSearches(android.content.ContentResolver cr)
Remove all searches from the search database.

param
cr The ContentResolver used to access the database.

        // FIXME: Should this clear the urls to which these searches lead?
        // (i.e. remove google.com/query= blah blah blah)
        try {
            cr.delete(SEARCHES_URI, null, null);
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "clearSearches", e);
        }
    
public static final voiddeleteFromHistory(android.content.ContentResolver cr, java.lang.String url)
Remove a specific url from the history database.

param
cr The ContentResolver used to access the database.
param
url url to remove.

        StringBuilder sb = new StringBuilder(BookmarkColumns.URL + " = ");
        DatabaseUtils.appendEscapedSQLString(sb, url);
        String matchesUrl = sb.toString();
        deleteHistoryWhere(cr, matchesUrl);
    
public static final voiddeleteHistoryTimeFrame(android.content.ContentResolver cr, long begin, long end)
Delete all history items from begin to end.

param
cr The ContentResolver used to access the database.
param
begin First date to remove. If -1, all dates before end. Inclusive.
param
end Last date to remove. If -1, all dates after begin. Non-inclusive.

        String whereClause;
        String date = BookmarkColumns.DATE;
        if (-1 == begin) {
            if (-1 == end) {
                clearHistory(cr);
                return;
            }
            whereClause = date + " < " + Long.toString(end);
        } else if (-1 == end) {
            whereClause = date + " >= " + Long.toString(begin);
        } else {
            whereClause = date + " >= " + Long.toString(begin) + " AND " + date
                    + " < " + Long.toString(end);
        }
        deleteHistoryWhere(cr, whereClause);
    
private static final voiddeleteHistoryWhere(android.content.ContentResolver cr, java.lang.String whereClause)
Helper function to delete all history items and revert all bookmarks to zero visits which meet the criteria provided.

param
cr The ContentResolver used to access the database.
param
whereClause String to limit the items affected. null means all items.

        try {
            Cursor c = cr.query(BOOKMARKS_URI,
                HISTORY_PROJECTION,
                whereClause,
                null,
                null);
            if (!c.moveToFirst()) {
                c.deactivate();
                return;
            }

            final WebIconDatabase iconDb = WebIconDatabase.getInstance();
            /* Delete favicons, and revert bookmarks which have been visited
             * to simply bookmarks.
             */
            StringBuffer sb = new StringBuffer();
            boolean firstTime = true;
            do {
                String url = c.getString(HISTORY_PROJECTION_URL_INDEX);
                boolean isBookmark = 
                    c.getInt(HISTORY_PROJECTION_BOOKMARK_INDEX) == 1;
                if (isBookmark) {
                    if (firstTime) {
                        firstTime = false;
                    } else {
                        sb.append(" OR ");
                    }
                    sb.append("( _id = ");
                    sb.append(c.getInt(0));
                    sb.append(" )");
                } else {
                    iconDb.releaseIconForPageUrl(url);
                }
            } while (c.moveToNext());
            c.deactivate();

            if (!firstTime) {
                ContentValues map = new ContentValues();
                map.put(BookmarkColumns.VISITS, 0);
                map.put(BookmarkColumns.DATE, 0);
                /* FIXME: Should I also remove the title? */
                cr.update(BOOKMARKS_URI, map, sb.toString(), null);
            }

            String deleteWhereClause = BookmarkColumns.BOOKMARK + " = 0";
            if (whereClause != null) {
                deleteWhereClause += " AND " + whereClause;
            }
            cr.delete(BOOKMARKS_URI, deleteWhereClause, null);
        } catch (IllegalStateException e) {
            return;
        }
    
public static final android.database.CursorgetAllBookmarks(android.content.ContentResolver cr)
Return a cursor pointing to a list of all the bookmarks.

param
cr The ContentResolver used to access the database.

        return cr.query(BOOKMARKS_URI,
                new String[] { BookmarkColumns.URL }, 
                "bookmark = 1", null, null);
    
public static final android.database.CursorgetAllVisitedUrls(android.content.ContentResolver cr)
Return a cursor pointing to a list of all visited site urls.

param
cr The ContentResolver used to access the database.

        return cr.query(BOOKMARKS_URI,
                new String[] { BookmarkColumns.URL }, null, null, null);
    
public static final voidrequestAllIcons(android.content.ContentResolver cr, java.lang.String where, WebIconDatabase.IconListener listener)
Request all icons from the database.

param
cr The ContentResolver used to access the database.
param
where Clause to be used to limit the query from the database. Must be an allowable string to be passed into a database query.
param
listener IconListener that gets the icons once they are retrieved.

        try {
            final Cursor c = cr.query(
                    BOOKMARKS_URI,
                    HISTORY_PROJECTION,
                    where, null, null);
            if (c.moveToFirst()) {
                final WebIconDatabase db = WebIconDatabase.getInstance();
                do {
                    db.requestIconForPageUrl(
                            c.getString(HISTORY_PROJECTION_URL_INDEX), 
                            listener);
                } while (c.moveToNext());
            }
            c.deactivate();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "requestAllIcons", e);
        }
    
public static final voidsaveBookmark(android.content.Context c, java.lang.String title, java.lang.String url)
Open the AddBookmark activity to save a bookmark. Launch with and/or url, which can be edited by the user before saving.

param
c Context used to launch the AddBookmark activity.
param
title Title for the bookmark. Can be null or empty string.
param
url Url for the bookmark. Can be null or empty string.


                                                                                    
          
                                            
                                            
        Intent i = new Intent(Intent.ACTION_INSERT, Browser.BOOKMARKS_URI);
        i.putExtra("title", title);
        i.putExtra("url", url);
        c.startActivity(i);
    
public static final voidsendString(android.content.Context c, java.lang.String s)

        Intent send = new Intent(Intent.ACTION_SEND);
        send.setType("text/plain");
        send.putExtra(Intent.EXTRA_TEXT, s);
        
        try {
            c.startActivity(Intent.createChooser(send,
                    c.getText(com.android.internal.R.string.sendText)));
        } catch(android.content.ActivityNotFoundException ex) {
            // if no app handles it, do nothing
        }
    
public static final voidtruncateHistory(android.content.ContentResolver cr)
If there are more than MAX_HISTORY_COUNT non-bookmark history items in the bookmark/history table, delete TRUNCATE_N_OLDEST of them. This is used to keep our history table to a reasonable size. Note: it does not prune bookmarks. If the user wants 1000 bookmarks, the user gets 1000 bookmarks.

param
cr The ContentResolver used to access the database.

        try {
            // Select non-bookmark history, ordered by date
            Cursor c = cr.query(
                    BOOKMARKS_URI,
                    TRUNCATE_HISTORY_PROJECTION,
                    "bookmark = 0",
                    null,
                    BookmarkColumns.DATE);
            // Log.v(LOGTAG, "history count " + c.count());
            if (c.moveToFirst() && c.getCount() >= MAX_HISTORY_COUNT) {
                /* eliminate oldest history items */
                for (int i = 0; i < TRUNCATE_N_OLDEST; i++) {
                    // Log.v(LOGTAG, "truncate history " +
                    // c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX));
                    deleteHistoryWhere(
                            cr, "_id = " +
                            c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX));
                    if (!c.moveToNext()) break;
                }
            }
            c.deactivate();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "truncateHistory", e);
            return;
        }
    
public static final voidupdateVisitedHistory(android.content.ContentResolver cr, java.lang.String url, boolean real)
Update the visited history to acknowledge that a site has been visited.

param
cr The ContentResolver used to access the database.
param
url The site being visited.
param
real Whether this is an actual visit, and should be added to the number of visits.

        long now = new Date().getTime();
        try {
            StringBuilder sb = new StringBuilder(BookmarkColumns.URL + " = ");
            DatabaseUtils.appendEscapedSQLString(sb, url);
            Cursor c = cr.query(
                    BOOKMARKS_URI,
                    HISTORY_PROJECTION,
                    sb.toString(),
                    null,
                    null);
            /* We should only get one answer that is exactly the same. */
            if (c.moveToFirst()) {
                ContentValues map = new ContentValues();
                if (real) {
                    map.put(BookmarkColumns.VISITS, c
                            .getInt(HISTORY_PROJECTION_VISITS_INDEX) + 1);
                }
                map.put(BookmarkColumns.DATE, now);
                cr.update(BOOKMARKS_URI, map, "_id = " + c.getInt(0), null);
            } else {
                truncateHistory(cr);
                ContentValues map = new ContentValues();
                map.put(BookmarkColumns.URL, url);
                map.put(BookmarkColumns.VISITS, real ? 1 : 0);
                map.put(BookmarkColumns.DATE, now);
                map.put(BookmarkColumns.BOOKMARK, 0);
                map.put(BookmarkColumns.TITLE, url);
                map.put(BookmarkColumns.CREATED, 0);
                cr.insert(BOOKMARKS_URI, map);
            }
            c.deactivate();
        } catch (IllegalStateException e) {
            return;
        }