blob: 2cbd85122598794b408c9ca49d840d466d49a855 [file] [log] [blame]
Leon Scrogginse372c022009-06-12 17:07:29 -04001/*
2 * Copyright (C) 2009 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
17package com.android.browser;
18
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
23import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010024import android.graphics.Bitmap;
Leon Scrogginse372c022009-06-12 17:07:29 -040025import android.net.Uri;
26import android.provider.Browser;
27import android.util.Log;
28import android.webkit.WebIconDatabase;
29import android.widget.Toast;
30
Ben Murdochaac7aa62009-09-17 16:57:40 +010031import java.io.ByteArrayOutputStream;
Leon Scrogginse372c022009-06-12 17:07:29 -040032import java.util.Date;
33
34/**
35 * This class is purely to have a common place for adding/deleting bookmarks.
36 */
37/* package */ class Bookmarks {
Ben Murdochde353622009-10-12 10:29:00 +010038 // We only want the user to be able to bookmark content that
39 // the browser can handle directly.
40 private static final String acceptableBookmarkSchemes[] = {
41 "http:",
42 "https:",
43 "about:",
44 "data:",
45 "javascript:",
46 "file:",
47 "content:"
48 };
49
Leon Scrogginse372c022009-06-12 17:07:29 -040050 /**
51 * Add a bookmark to the database.
52 * @param context Context of the calling Activity. This is used to make
53 * Toast confirming that the bookmark has been added. If the
54 * caller provides null, the Toast will not be shown.
55 * @param cr The ContentResolver being used to add the bookmark to the db.
56 * @param url URL of the website to be bookmarked.
57 * @param name Provided name for the bookmark.
Ben Murdochaac7aa62009-09-17 16:57:40 +010058 * @param thumbnail A thumbnail for the bookmark.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070059 * @param retainIcon Whether to retain the page's icon in the icon database.
60 * This will usually be <code>true</code> except when bookmarks are
61 * added by a settings restore agent.
Leon Scrogginse372c022009-06-12 17:07:29 -040062 */
63 /* package */ static void addBookmark(Context context,
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070064 ContentResolver cr, String url, String name,
Ben Murdochaac7aa62009-09-17 16:57:40 +010065 Bitmap thumbnail, boolean retainIcon) {
Leon Scrogginse372c022009-06-12 17:07:29 -040066 // Want to append to the beginning of the list
67 long creationTime = new Date().getTime();
Leon Scrogginse372c022009-06-12 17:07:29 -040068 ContentValues map = new ContentValues();
Leon Scrogginsda3e0302010-03-02 16:01:49 -050069 Cursor cursor = Browser.getVisitedLike(cr, url);
Leon Scrogginse372c022009-06-12 17:07:29 -040070 if (cursor.moveToFirst() && cursor.getInt(
71 Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) {
72 // This means we have been to this site but not bookmarked
73 // it, so convert the history item to a bookmark
74 map.put(Browser.BookmarkColumns.CREATED, creationTime);
75 map.put(Browser.BookmarkColumns.TITLE, name);
76 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
Ben Murdochaac7aa62009-09-17 16:57:40 +010077 map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
Leon Scrogginse372c022009-06-12 17:07:29 -040078 cr.update(Browser.BOOKMARKS_URI, map,
79 "_id = " + cursor.getInt(0), null);
80 } else {
81 int count = cursor.getCount();
82 boolean matchedTitle = false;
83 for (int i = 0; i < count; i++) {
84 // One or more bookmarks already exist for this site.
85 // Check the names of each
86 cursor.moveToPosition(i);
87 if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX)
88 .equals(name)) {
89 // The old bookmark has the same name.
90 // Update its creation time.
91 map.put(Browser.BookmarkColumns.CREATED,
92 creationTime);
93 cr.update(Browser.BOOKMARKS_URI, map,
94 "_id = " + cursor.getInt(0), null);
95 matchedTitle = true;
96 break;
97 }
98 }
99 if (!matchedTitle) {
100 // Adding a bookmark for a site the user has visited,
101 // or a new bookmark (with a different name) for a site
102 // the user has visited
103 map.put(Browser.BookmarkColumns.TITLE, name);
104 map.put(Browser.BookmarkColumns.URL, url);
105 map.put(Browser.BookmarkColumns.CREATED, creationTime);
106 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
107 map.put(Browser.BookmarkColumns.DATE, 0);
Ben Murdochaac7aa62009-09-17 16:57:40 +0100108 map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
Leon Scrogginse372c022009-06-12 17:07:29 -0400109 int visits = 0;
110 if (count > 0) {
111 // The user has already bookmarked, and possibly
112 // visited this site. However, they are creating
113 // a new bookmark with the same url but a different
114 // name. The new bookmark should have the same
115 // number of visits as the already created bookmark.
116 visits = cursor.getInt(
117 Browser.HISTORY_PROJECTION_VISITS_INDEX);
118 }
119 // Bookmark starts with 3 extra visits so that it will
120 // bubble up in the most visited and goto search box
121 map.put(Browser.BookmarkColumns.VISITS, visits + 3);
122 cr.insert(Browser.BOOKMARKS_URI, map);
123 }
124 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700125 if (retainIcon) {
126 WebIconDatabase.getInstance().retainIconForPageUrl(url);
127 }
Leon Scrogginse372c022009-06-12 17:07:29 -0400128 cursor.deactivate();
129 if (context != null) {
130 Toast.makeText(context, R.string.added_to_bookmarks,
131 Toast.LENGTH_LONG).show();
132 }
133 }
134
135 /**
136 * Remove a bookmark from the database. If the url is a visited site, it
137 * will remain in the database, but only as a history item, and not as a
138 * bookmarked site.
139 * @param context Context of the calling Activity. This is used to make
140 * Toast confirming that the bookmark has been removed. If the
141 * caller provides null, the Toast will not be shown.
142 * @param cr The ContentResolver being used to remove the bookmark.
143 * @param url URL of the website to be removed.
144 */
145 /* package */ static void removeFromBookmarks(Context context,
Andrei Popescuc9526192009-09-23 15:52:16 +0100146 ContentResolver cr, String url, String title) {
Leon Scrogginse372c022009-06-12 17:07:29 -0400147 Cursor cursor = cr.query(
148 Browser.BOOKMARKS_URI,
149 Browser.HISTORY_PROJECTION,
Andrei Popescuc9526192009-09-23 15:52:16 +0100150 "url = ? AND title = ?",
151 new String[] { url, title },
Leon Scrogginse372c022009-06-12 17:07:29 -0400152 null);
153 boolean first = cursor.moveToFirst();
154 // Should be in the database no matter what
155 if (!first) {
Andrei Popescuc9526192009-09-23 15:52:16 +0100156 throw new AssertionError("URL is not in the database! " + url + " " + title);
Leon Scrogginse372c022009-06-12 17:07:29 -0400157 }
158 // Remove from bookmarks
159 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
160 Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
161 cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
162 int numVisits = cursor.getInt(
163 Browser.HISTORY_PROJECTION_VISITS_INDEX);
164 if (0 == numVisits) {
165 cr.delete(uri, null, null);
166 } else {
167 // It is no longer a bookmark, but it is still a visited
168 // site.
169 ContentValues values = new ContentValues();
170 values.put(Browser.BookmarkColumns.BOOKMARK, 0);
171 try {
172 cr.update(uri, values, null, null);
173 } catch (IllegalStateException e) {
174 Log.e("removeFromBookmarks", "no database!");
175 }
176 }
177 if (context != null) {
178 Toast.makeText(context, R.string.removed_from_bookmarks,
179 Toast.LENGTH_LONG).show();
180 }
181 cursor.deactivate();
182 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100183
184 private static byte[] bitmapToBytes(Bitmap bm) {
185 if (bm == null) {
186 return null;
187 }
188
189 final ByteArrayOutputStream os = new ByteArrayOutputStream();
190 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
191 return os.toByteArray();
192 }
Ben Murdochde353622009-10-12 10:29:00 +0100193
194 /* package */ static boolean urlHasAcceptableScheme(String url) {
195 if (url == null) {
196 return false;
197 }
198
199 for (int i = 0; i < acceptableBookmarkSchemes.length; i++) {
200 if (url.startsWith(acceptableBookmarkSchemes[i])) {
201 return true;
202 }
203 }
204 return false;
205 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100206}