FileDocCategorySizeDatePackage
Browser.javaAPI DocAndroid 5.1 API26128Thu Mar 12 22:22:10 GMT 2015android.provider

Browser

public class Browser extends Object

Fields Summary
private static final String
LOGTAG
public static final android.net.Uri
BOOKMARKS_URI
A table containing both bookmarks and history items. The columns of the table are defined in {@link BookmarkColumns}. Reading this table requires the {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} permission and writing to it requires the {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} permission.
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 identify 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
EXTRA_HEADERS
The name of the extra data in the VIEW intent. The data are key/value pairs in the format of Bundle. They will be sent in the HTTP request headers for the provided url. The keys can't be the standard HTTP headers as they are set by the WebView. The url's schema must be http(s).

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 int
HISTORY_PROJECTION_THUMBNAIL_INDEX
public static final int
HISTORY_PROJECTION_TOUCH_ICON_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
A table containing a log of browser searches. The columns of the table are defined in {@link SearchColumns}. Reading this table requires the {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} permission and writing to it requires the {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} permission.
public static final String[]
SEARCHES_PROJECTION
A projection of {@link #SEARCHES_URI} that contains {@link SearchColumns#_ID}, {@link SearchColumns#SEARCH}, and {@link SearchColumns#DATE}.
public static final int
SEARCHES_PROJECTION_SEARCH_INDEX
public static final int
SEARCHES_PROJECTION_DATE_INDEX
private static final int
MAX_HISTORY_COUNT
public static final String
EXTRA_CREATE_NEW_TAB
Boolean extra passed along with an Intent to a browser, specifying that a new tab be created. Overrides EXTRA_APPLICATION_ID; if both are set, a new tab will be used, rather than using the same one.
public static final String
EXTRA_SHARE_SCREENSHOT
Stores a Bitmap extra in an {@link Intent} representing the screenshot of a page to share. When receiving an {@link Intent#ACTION_SEND} from the Browser, use this to access the screenshot.
public static final String
EXTRA_SHARE_FAVICON
Stores a Bitmap extra in an {@link Intent} representing the favicon of a page to share. When receiving an {@link Intent#ACTION_SEND} from the Browser, use this to access the favicon.
Constructors Summary
Methods Summary
private static final voidaddOrUrlEquals(java.lang.StringBuilder sb)

        sb.append(" OR " + BookmarkColumns.URL + " = ");
    
public static final voidaddSearchUrl(android.content.ContentResolver cr, java.lang.String search)
Add a search string to the searches database. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

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

        // The content provider will take care of updating existing searches instead of duplicating
        ContentValues values = new ContentValues();
        values.put(Searches.SEARCH, search);
        values.put(Searches.DATE, System.currentTimeMillis());
        cr.insert(Searches.CONTENT_URI, values);
    
public static final booleancanClearHistory(android.content.ContentResolver cr)
Returns whether there is any history to clear. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}

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

        Cursor cursor = null;
        boolean ret = false;
        try {
            cursor = cr.query(History.CONTENT_URI,
                new String [] { History._ID, History.VISITS },
                null, null, null);
            ret = cursor.getCount() > 0;
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "canClearHistory", e);
        } finally {
            if (cursor != null) cursor.close();
        }
        return ret;
    
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. Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

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. Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

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.CONTENT_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. Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

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

        cr.delete(History.CONTENT_URI, History.URL + "=?", new String[] { url });
    
public static final voiddeleteHistoryTimeFrame(android.content.ContentResolver cr, long begin, long end)
Delete all history items from begin to end. Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

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 release the icons for them in the {@link WebIconDatabase}. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

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

        Cursor cursor = null;
        try {
            cursor = cr.query(History.CONTENT_URI, new String[] { History.URL }, whereClause,
                    null, null);
            if (cursor.moveToFirst()) {
                cr.delete(History.CONTENT_URI, whereClause, null);
            }
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "deleteHistoryWhere", e);
            return;
        } finally {
            if (cursor != null) cursor.close();
        }
    
public static final android.database.CursorgetAllBookmarks(android.content.ContentResolver cr)
Return a cursor pointing to a list of all the bookmarks. The cursor will have a single column, {@link BookmarkColumns#URL}.

Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}

param
cr The ContentResolver used to access the database.

        return cr.query(Bookmarks.CONTENT_URI,
                new String[] { Bookmarks.URL },
                Bookmarks.IS_FOLDER + " = 0", null, null);
    
public static final android.database.CursorgetAllVisitedUrls(android.content.ContentResolver cr)
Return a cursor pointing to a list of all visited site urls. The cursor will have a single column, {@link BookmarkColumns#URL}.

Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}

param
cr The ContentResolver used to access the database.

        return cr.query(Combined.CONTENT_URI,
                new String[] { Combined.URL }, null, null,
                Combined.DATE_CREATED + " ASC");
    
public static final java.lang.String[]getVisitedHistory(android.content.ContentResolver cr)
Returns all the URLs in the history. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}

param
cr The ContentResolver used to access the database.
hide
pending API council approval

        Cursor c = null;
        String[] str = null;
        try {
            String[] projection = new String[] {
                    History.URL,
            };
            c = cr.query(History.CONTENT_URI, projection, History.VISITS + " > 0", null, null);
            if (c == null) return new String[0];
            str = new String[c.getCount()];
            int i = 0;
            while (c.moveToNext()) {
                str[i] = c.getString(0);
                i++;
            }
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "getVisitedHistory", e);
            str = new String[0];
        } finally {
            if (c != null) c.close();
        }
        return str;
    
private static final android.database.CursorgetVisitedLike(android.content.ContentResolver cr, java.lang.String url)

        boolean secure = false;
        String compareString = url;
        if (compareString.startsWith("http://")) {
            compareString = compareString.substring(7);
        } else if (compareString.startsWith("https://")) {
            compareString = compareString.substring(8);
            secure = true;
        }
        if (compareString.startsWith("www.")) {
            compareString = compareString.substring(4);
        }
        StringBuilder whereClause = null;
        if (secure) {
            whereClause = new StringBuilder(Bookmarks.URL + " = ");
            DatabaseUtils.appendEscapedSQLString(whereClause,
                    "https://" + compareString);
            addOrUrlEquals(whereClause);
            DatabaseUtils.appendEscapedSQLString(whereClause,
                    "https://www." + compareString);
        } else {
            whereClause = new StringBuilder(Bookmarks.URL + " = ");
            DatabaseUtils.appendEscapedSQLString(whereClause,
                    compareString);
            addOrUrlEquals(whereClause);
            String wwwString = "www." + compareString;
            DatabaseUtils.appendEscapedSQLString(whereClause,
                    wwwString);
            addOrUrlEquals(whereClause);
            DatabaseUtils.appendEscapedSQLString(whereClause,
                    "http://" + compareString);
            addOrUrlEquals(whereClause);
            DatabaseUtils.appendEscapedSQLString(whereClause,
                    "http://" + wwwString);
        }
        return cr.query(History.CONTENT_URI, new String[] { History._ID, History.VISITS },
                whereClause.toString(), null, null);
    
public static final voidrequestAllIcons(android.content.ContentResolver cr, java.lang.String where, WebIconDatabase.IconListener listener)
Request all icons from the database. This call must either be called in the main thread or have had Looper.prepare() invoked in the calling thread. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}

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.

        // Do nothing: this is no longer used.
    
public static final voidsaveBookmark(android.content.Context c, java.lang.String title, java.lang.String url)
Open an activity to save a bookmark. Launch with a title and/or a url, both of which can be edited by the user before saving.

param
c Context used to launch the activity to add a bookmark.
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 context, java.lang.String string)
Sends the given string using an Intent with {@link Intent#ACTION_SEND} and a mime type of text/plain. The string is put into {@link Intent#EXTRA_TEXT}.

param
context the context used to start the activity
param
string the string to send


                                               
            
        sendString(context, string, context.getString(com.android.internal.R.string.sendText));
    
public static final voidsendString(android.content.Context c, java.lang.String stringToSend, java.lang.String chooserDialogTitle)
Find an application to handle the given string and, if found, invoke it with the given string as a parameter.

param
c Context used to launch the new activity.
param
stringToSend The string to be handled.
param
chooserDialogTitle The title of the dialog that allows the user to select between multiple applications that are all capable of handling the string.
hide
pending API council approval

        Intent send = new Intent(Intent.ACTION_SEND);
        send.setType("text/plain");
        send.putExtra(Intent.EXTRA_TEXT, stringToSend);

        try {
            Intent i = Intent.createChooser(send, chooserDialogTitle);
            // In case this is called from outside an Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            c.startActivity(i);
        } 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. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

param
cr The ContentResolver used to access the database.

        // TODO make a single request to the provider to do this in a single transaction
        Cursor cursor = null;
        try {

            // Select non-bookmark history, ordered by date
            cursor = cr.query(History.CONTENT_URI,
                    new String[] { History._ID, History.URL, History.DATE_LAST_VISITED },
                    null, null, History.DATE_LAST_VISITED + " ASC");

            if (cursor.moveToFirst() && cursor.getCount() >= MAX_HISTORY_COUNT) {
                /* eliminate oldest history items */
                for (int i = 0; i < TRUNCATE_N_OLDEST; i++) {
                    cr.delete(ContentUris.withAppendedId(History.CONTENT_URI, cursor.getLong(0)),
                        null, null);
                    if (!cursor.moveToNext()) break;
                }
            }
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "truncateHistory", e);
        } finally {
            if (cursor != null) cursor.close();
        }
    
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. Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}

param
cr The ContentResolver used to access the database.
param
url The site being visited.
param
real If true, this is an actual visit, and should add to the number of visits. If false, the user entered it manually.

        long now = System.currentTimeMillis();
        Cursor c = null;
        try {
            c = getVisitedLike(cr, url);
            /* We should only get one answer that is exactly the same. */
            if (c.moveToFirst()) {
                ContentValues values = new ContentValues();
                if (real) {
                    values.put(History.VISITS, c.getInt(1) + 1);
                } else {
                    values.put(History.USER_ENTERED, 1);
                }
                values.put(History.DATE_LAST_VISITED, now);
                cr.update(ContentUris.withAppendedId(History.CONTENT_URI, c.getLong(0)),
                        values, null, null);
            } else {
                truncateHistory(cr);
                ContentValues values = new ContentValues();
                int visits;
                int user_entered;
                if (real) {
                    visits = 1;
                    user_entered = 0;
                } else {
                    visits = 0;
                    user_entered = 1;
                }
                values.put(History.URL, url);
                values.put(History.VISITS, visits);
                values.put(History.DATE_LAST_VISITED, now);
                values.put(History.TITLE, url);
                values.put(History.DATE_CREATED, 0);
                values.put(History.USER_ENTERED, user_entered);
                cr.insert(History.CONTENT_URI, values);
            }
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "updateVisitedHistory", e);
        } finally {
            if (c != null) c.close();
        }