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