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