Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.provider;
     18 
     19 import android.content.ContentResolver;
     20 import android.content.ContentUris;
     21 import android.content.ContentValues;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.database.Cursor;
     25 import android.database.DatabaseUtils;
     26 import android.database.MatrixCursor;
     27 import android.graphics.BitmapFactory;
     28 import android.net.Uri;
     29 import android.provider.BrowserContract.Bookmarks;
     30 import android.provider.BrowserContract.Combined;
     31 import android.provider.BrowserContract.History;
     32 import android.provider.BrowserContract.Searches;
     33 import android.util.Log;
     34 import android.webkit.WebIconDatabase;
     35 
     36 public class Browser {
     37     private static final String LOGTAG = "browser";
     38 
     39     /**
     40      * A table containing both bookmarks and history items. The columns of the table are defined in
     41      * {@link BookmarkColumns}.
     42      * @removed
     43      */
     44     public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
     45 
     46     /**
     47      * The name of extra data when starting Browser with ACTION_VIEW or
     48      * ACTION_SEARCH intent.
     49      * <p>
     50      * The value should be an integer between 0 and 1000. If not set or set to
     51      * 0, the Browser will use default. If set to 100, the Browser will start
     52      * with 100%.
     53      */
     54     public static final String INITIAL_ZOOM_LEVEL = "browser.initialZoomLevel";
     55 
     56     /**
     57      * The name of the extra data when starting the Browser from another
     58      * application.
     59      * <p>
     60      * The value is a unique identification string that will be used to
     61      * identify the calling application. The Browser will attempt to reuse the
     62      * same window each time the application launches the Browser with the same
     63      * identifier.
     64      */
     65     public static final String EXTRA_APPLICATION_ID = "com.android.browser.application_id";
     66 
     67     /**
     68      * The name of the extra data in the VIEW intent. The data are key/value
     69      * pairs in the format of Bundle. They will be sent in the HTTP request
     70      * headers for the provided url. The keys can't be the standard HTTP headers
     71      * as they are set by the WebView. The url's schema must be http(s).
     72      * <p>
     73      */
     74     public static final String EXTRA_HEADERS = "com.android.browser.headers";
     75 
     76     /** @removed if you change column order you must also change indices below */
     77     public static final String[] HISTORY_PROJECTION = new String[] {
     78             BookmarkColumns._ID, // 0
     79             BookmarkColumns.URL, // 1
     80             BookmarkColumns.VISITS, // 2
     81             BookmarkColumns.DATE, // 3
     82             BookmarkColumns.BOOKMARK, // 4
     83             BookmarkColumns.TITLE, // 5
     84             BookmarkColumns.FAVICON, // 6
     85             BookmarkColumns.THUMBNAIL, // 7
     86             BookmarkColumns.TOUCH_ICON, // 8
     87             BookmarkColumns.USER_ENTERED, // 9
     88     };
     89 
     90     /** @removed these indices dependent on HISTORY_PROJECTION */
     91     public static final int HISTORY_PROJECTION_ID_INDEX = 0;
     92 
     93     /** @removed */
     94     public static final int HISTORY_PROJECTION_URL_INDEX = 1;
     95 
     96     /** @removed */
     97     public static final int HISTORY_PROJECTION_VISITS_INDEX = 2;
     98 
     99     /** @removed */
    100     public static final int HISTORY_PROJECTION_DATE_INDEX = 3;
    101 
    102     /** @removed */
    103     public static final int HISTORY_PROJECTION_BOOKMARK_INDEX = 4;
    104 
    105     /** @removed */
    106     public static final int HISTORY_PROJECTION_TITLE_INDEX = 5;
    107 
    108     /** @removed */
    109     public static final int HISTORY_PROJECTION_FAVICON_INDEX = 6;
    110     /**
    111      * @hide
    112      */
    113     public static final int HISTORY_PROJECTION_THUMBNAIL_INDEX = 7;
    114     /**
    115      * @hide
    116      */
    117     public static final int HISTORY_PROJECTION_TOUCH_ICON_INDEX = 8;
    118 
    119     /** @removed columns needed to determine whether to truncate history @removed */
    120     public static final String[] TRUNCATE_HISTORY_PROJECTION = new String[] {
    121             BookmarkColumns._ID,
    122             BookmarkColumns.DATE,
    123     };
    124 
    125     /** @removed */
    126     public static final int TRUNCATE_HISTORY_PROJECTION_ID_INDEX = 0;
    127 
    128     /** @removed truncate this many history items at a time */
    129     public static final int TRUNCATE_N_OLDEST = 5;
    130 
    131     /**
    132      * A table containing a log of browser searches. The columns of the table are defined in
    133      * {@link SearchColumns}.
    134      * @removed
    135      */
    136     public static final Uri SEARCHES_URI = Uri.parse("content://browser/searches");
    137 
    138     /**
    139      * A projection of {@link #SEARCHES_URI} that contains {@link SearchColumns#_ID},
    140      * {@link SearchColumns#SEARCH}, and {@link SearchColumns#DATE}.
    141      * @removed
    142      */
    143     public static final String[] SEARCHES_PROJECTION = new String[] {
    144             // if you change column order you must also change indices below
    145             SearchColumns._ID, // 0
    146             SearchColumns.SEARCH, // 1
    147             SearchColumns.DATE, // 2
    148     };
    149 
    150     /** @removed these indices dependent on SEARCHES_PROJECTION */
    151     public static final int SEARCHES_PROJECTION_SEARCH_INDEX = 1;
    152     /** @removed */
    153     public static final int SEARCHES_PROJECTION_DATE_INDEX = 2;
    154 
    155     /* Set a cap on the count of history items in the history/bookmark
    156        table, to prevent db and layout operations from dragging to a
    157        crawl.  Revisit this cap when/if db/layout performance
    158        improvements are made.  Note: this does not affect bookmark
    159        entries -- if the user wants more bookmarks than the cap, they
    160        get them. */
    161     private static final int MAX_HISTORY_COUNT = 250;
    162 
    163     /**
    164      *  Open an activity to save a bookmark. Launch with a title
    165      *  and/or a url, both of which can be edited by the user before saving.
    166      *
    167      *  @param c        Context used to launch the activity to add a bookmark.
    168      *  @param title    Title for the bookmark. Can be null or empty string.
    169      *  @param url      Url for the bookmark. Can be null or empty string.
    170      *  @removed
    171      */
    172     public static final void saveBookmark(Context c,
    173                                           String title,
    174                                           String url) {
    175     }
    176 
    177     /**
    178      * Boolean extra passed along with an Intent to a browser, specifying that
    179      * a new tab be created.  Overrides EXTRA_APPLICATION_ID; if both are set,
    180      * a new tab will be used, rather than using the same one.
    181      */
    182     public static final String EXTRA_CREATE_NEW_TAB = "create_new_tab";
    183 
    184     /**
    185      * Stores a Bitmap extra in an {@link Intent} representing the screenshot of
    186      * a page to share.  When receiving an {@link Intent#ACTION_SEND} from the
    187      * Browser, use this to access the screenshot.
    188      * @hide
    189      */
    190     public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
    191 
    192     /**
    193      * Stores a Bitmap extra in an {@link Intent} representing the favicon of a
    194      * page to share.  When receiving an {@link Intent#ACTION_SEND} from the
    195      * Browser, use this to access the favicon.
    196      * @hide
    197      */
    198     public final static String EXTRA_SHARE_FAVICON = "share_favicon";
    199 
    200     /**
    201      * Sends the given string using an Intent with {@link Intent#ACTION_SEND} and a mime type
    202      * of text/plain. The string is put into {@link Intent#EXTRA_TEXT}.
    203      *
    204      * @param context the context used to start the activity
    205      * @param string the string to send
    206      */
    207     public static final void sendString(Context context, String string) {
    208         sendString(context, string, context.getString(com.android.internal.R.string.sendText));
    209     }
    210 
    211     /**
    212      *  Find an application to handle the given string and, if found, invoke
    213      *  it with the given string as a parameter.
    214      *  @param c Context used to launch the new activity.
    215      *  @param stringToSend The string to be handled.
    216      *  @param chooserDialogTitle The title of the dialog that allows the user
    217      *  to select between multiple applications that are all capable of handling
    218      *  the string.
    219      *  @hide pending API council approval
    220      */
    221     public static final void sendString(Context c,
    222                                         String stringToSend,
    223                                         String chooserDialogTitle) {
    224         Intent send = new Intent(Intent.ACTION_SEND);
    225         send.setType("text/plain");
    226         send.putExtra(Intent.EXTRA_TEXT, stringToSend);
    227 
    228         try {
    229             Intent i = Intent.createChooser(send, chooserDialogTitle);
    230             // In case this is called from outside an Activity
    231             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    232             c.startActivity(i);
    233         } catch(android.content.ActivityNotFoundException ex) {
    234             // if no app handles it, do nothing
    235         }
    236     }
    237 
    238     /**
    239      *  Return a cursor pointing to a list of all the bookmarks. The cursor will have a single
    240      *  column, {@link BookmarkColumns#URL}.
    241      *
    242      *  @param cr   The ContentResolver used to access the database.
    243      *  @removed
    244      */
    245     public static final Cursor getAllBookmarks(ContentResolver cr) throws
    246             IllegalStateException {
    247         return new MatrixCursor(new String[]{Bookmarks.URL}, 0);
    248     }
    249 
    250     /**
    251      *  Return a cursor pointing to a list of all visited site urls. The cursor will
    252      *  have a single column, {@link BookmarkColumns#URL}.
    253      *
    254      *  @param cr   The ContentResolver used to access the database.
    255      *  @removed
    256      */
    257     public static final Cursor getAllVisitedUrls(ContentResolver cr) throws
    258             IllegalStateException {
    259         return new MatrixCursor(new String[]{Combined.URL}, 0);
    260     }
    261 
    262     private static final void addOrUrlEquals(StringBuilder sb) {
    263         sb.append(" OR " + BookmarkColumns.URL + " = ");
    264     }
    265 
    266     private static final Cursor getVisitedLike(ContentResolver cr, String url) {
    267         boolean secure = false;
    268         String compareString = url;
    269         if (compareString.startsWith("http://")) {
    270             compareString = compareString.substring(7);
    271         } else if (compareString.startsWith("https://")) {
    272             compareString = compareString.substring(8);
    273             secure = true;
    274         }
    275         if (compareString.startsWith("www.")) {
    276             compareString = compareString.substring(4);
    277         }
    278         StringBuilder whereClause = null;
    279         if (secure) {
    280             whereClause = new StringBuilder(Bookmarks.URL + " = ");
    281             DatabaseUtils.appendEscapedSQLString(whereClause,
    282                     "https://" + compareString);
    283             addOrUrlEquals(whereClause);
    284             DatabaseUtils.appendEscapedSQLString(whereClause,
    285                     "https://www." + compareString);
    286         } else {
    287             whereClause = new StringBuilder(Bookmarks.URL + " = ");
    288             DatabaseUtils.appendEscapedSQLString(whereClause,
    289                     compareString);
    290             addOrUrlEquals(whereClause);
    291             String wwwString = "www." + compareString;
    292             DatabaseUtils.appendEscapedSQLString(whereClause,
    293                     wwwString);
    294             addOrUrlEquals(whereClause);
    295             DatabaseUtils.appendEscapedSQLString(whereClause,
    296                     "http://" + compareString);
    297             addOrUrlEquals(whereClause);
    298             DatabaseUtils.appendEscapedSQLString(whereClause,
    299                     "http://" + wwwString);
    300         }
    301         return cr.query(History.CONTENT_URI, new String[] { History._ID, History.VISITS },
    302                 whereClause.toString(), null, null);
    303     }
    304 
    305     /**
    306      *  Update the visited history to acknowledge that a site has been
    307      *  visited.
    308      *
    309      *  @param cr   The ContentResolver used to access the database.
    310      *  @param url  The site being visited.
    311      *  @param real If true, this is an actual visit, and should add to the
    312      *              number of visits.  If false, the user entered it manually.
    313      *  @removed
    314      */
    315     public static final void updateVisitedHistory(ContentResolver cr,
    316                                                   String url, boolean real) {
    317     }
    318 
    319     /**
    320      *  Returns all the URLs in the history.
    321      *
    322      *  @param cr   The ContentResolver used to access the database.
    323      *  @hide pending API council approval
    324      */
    325     @Deprecated
    326     public static final String[] getVisitedHistory(ContentResolver cr) {
    327         return new String[0];
    328     }
    329 
    330     /**
    331      * If there are more than MAX_HISTORY_COUNT non-bookmark history
    332      * items in the bookmark/history table, delete TRUNCATE_N_OLDEST
    333      * of them.  This is used to keep our history table to a
    334      * reasonable size.  Note: it does not prune bookmarks.  If the
    335      * user wants 1000 bookmarks, the user gets 1000 bookmarks.
    336      *
    337      * @param cr The ContentResolver used to access the database.
    338      * @removed
    339      */
    340     public static final void truncateHistory(ContentResolver cr) {
    341     }
    342 
    343     /**
    344      * Returns whether there is any history to clear.
    345      *
    346      * @param cr   The ContentResolver used to access the database.
    347      * @return boolean  True if the history can be cleared.
    348      * @removed
    349      */
    350     public static final boolean canClearHistory(ContentResolver cr) {
    351         return false;
    352     }
    353 
    354     /**
    355      *  Delete all entries from the bookmarks/history table which are
    356      *  not bookmarks.  Also set all visited bookmarks to unvisited.
    357      *
    358      *  @param cr   The ContentResolver used to access the database.
    359      *  @removed
    360      */
    361     public static final void clearHistory(ContentResolver cr) {
    362 
    363     }
    364 
    365     /**
    366      * Delete all history items from begin to end.
    367      *
    368      * @param cr    The ContentResolver used to access the database.
    369      * @param begin First date to remove.  If -1, all dates before end.
    370      *              Inclusive.
    371      * @param end   Last date to remove. If -1, all dates after begin.
    372      *              Non-inclusive.
    373      * @removed
    374      */
    375     public static final void deleteHistoryTimeFrame(ContentResolver cr,
    376             long begin, long end) {
    377     }
    378 
    379     /**
    380      * Remove a specific url from the history database.
    381      *
    382      * @param cr    The ContentResolver used to access the database.
    383      * @param url   url to remove.
    384      * @removed
    385      */
    386     public static final void deleteFromHistory(ContentResolver cr,
    387                                                String url) {
    388     }
    389 
    390     /**
    391      * Add a search string to the searches database.
    392      *
    393      * @param cr   The ContentResolver used to access the database.
    394      * @param search    The string to add to the searches database.
    395      * @removed
    396      */
    397     public static final void addSearchUrl(ContentResolver cr, String search) {
    398     }
    399 
    400     /**
    401      * Remove all searches from the search database.
    402      *
    403      * @param cr   The ContentResolver used to access the database.
    404      * @removed
    405      */
    406     public static final void clearSearches(ContentResolver cr) {
    407     }
    408 
    409     /**
    410      *  Request all icons from the database.  This call must either be called
    411      *  in the main thread or have had Looper.prepare() invoked in the calling
    412      *  thread.
    413      *
    414      *  @param  cr The ContentResolver used to access the database.
    415      *  @param  where Clause to be used to limit the query from the database.
    416      *          Must be an allowable string to be passed into a database query.
    417      *  @param  listener IconListener that gets the icons once they are
    418      *          retrieved.
    419      *  @removed
    420      */
    421     public static final void requestAllIcons(ContentResolver cr, String where,
    422             WebIconDatabase.IconListener listener) {
    423         // Do nothing: this is no longer used.
    424     }
    425 
    426     /**
    427      * Column definitions for the mixed bookmark and history items available
    428      * at {@link #BOOKMARKS_URI}.
    429      * @removed
    430      */
    431     public static class BookmarkColumns implements BaseColumns {
    432         /**
    433          * The URL of the bookmark or history item.
    434          * <p>Type: TEXT (URL)</p>
    435          */
    436         public static final String URL = "url";
    437 
    438         /**
    439          * The number of time the item has been visited.
    440          * <p>Type: NUMBER</p>
    441          */
    442         public static final String VISITS = "visits";
    443 
    444         /**
    445          * The date the item was last visited, in milliseconds since the epoch.
    446          * <p>Type: NUMBER (date in milliseconds since January 1, 1970)</p>
    447          */
    448         public static final String DATE = "date";
    449 
    450         /**
    451          * Flag indicating that an item is a bookmark. A value of 1 indicates a bookmark, a value
    452          * of 0 indicates a history item.
    453          * <p>Type: INTEGER (boolean)</p>
    454          */
    455         public static final String BOOKMARK = "bookmark";
    456 
    457         /**
    458          * The user visible title of the bookmark or history item.
    459          * <p>Type: TEXT</p>
    460          */
    461         public static final String TITLE = "title";
    462 
    463         /**
    464          * The date the item created, in milliseconds since the epoch.
    465          * <p>Type: NUMBER (date in milliseconds since January 1, 1970)</p>
    466          */
    467         public static final String CREATED = "created";
    468 
    469         /**
    470          * The favicon of the bookmark. Must decode via {@link BitmapFactory#decodeByteArray}.
    471          * <p>Type: BLOB (image)</p>
    472          */
    473         public static final String FAVICON = "favicon";
    474 
    475         /**
    476          * @hide
    477          */
    478         public static final String THUMBNAIL = "thumbnail";
    479 
    480         /**
    481          * @hide
    482          */
    483         public static final String TOUCH_ICON = "touch_icon";
    484 
    485         /**
    486          * @hide
    487          */
    488         public static final String USER_ENTERED = "user_entered";
    489     }
    490 
    491     /**
    492      * Column definitions for the search history table, available at {@link #SEARCHES_URI}.
    493      * @removed
    494      */
    495     public static class SearchColumns implements BaseColumns {
    496         /**
    497          * @deprecated Not used.
    498          */
    499         @Deprecated
    500         public static final String URL = "url";
    501 
    502         /**
    503          * The user entered search term.
    504          */
    505         public static final String SEARCH = "search";
    506 
    507         /**
    508          * The date the search was performed, in milliseconds since the epoch.
    509          * <p>Type: NUMBER (date in milliseconds since January 1, 1970)</p>
    510          */
    511         public static final String DATE = "date";
    512     }
    513 }
    514