blob: 97e6b208610f0dd07334a0f550817586bbb3f2e7 [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;
24import android.net.Uri;
25import android.provider.Browser;
26import android.util.Log;
27import android.webkit.WebIconDatabase;
28import android.widget.Toast;
29
30import java.util.Date;
31
32/**
33 * This class is purely to have a common place for adding/deleting bookmarks.
34 */
35/* package */ class Bookmarks {
36 private static final String WHERE_CLAUSE
37 = "url = ? OR url = ? OR url = ? OR url = ?";
38 private static final String WHERE_CLAUSE_SECURE = "url = ? OR url = ?";
39
40 private static String[] SELECTION_ARGS;
41
42 /**
43 * Add a bookmark to the database.
44 * @param context Context of the calling Activity. This is used to make
45 * Toast confirming that the bookmark has been added. If the
46 * caller provides null, the Toast will not be shown.
47 * @param cr The ContentResolver being used to add the bookmark to the db.
48 * @param url URL of the website to be bookmarked.
49 * @param name Provided name for the bookmark.
50 */
51 /* package */ static void addBookmark(Context context,
52 ContentResolver cr, String url, String name) {
53 // Want to append to the beginning of the list
54 long creationTime = new Date().getTime();
55 // First we check to see if the user has already visited this
56 // site. They may have bookmarked it in a different way from
57 // how it's stored in the database, so allow different combos
58 // to map to the same url.
59 boolean secure = false;
60 String compareString = url;
61 if (compareString.startsWith("http://")) {
62 compareString = compareString.substring(7);
63 } else if (compareString.startsWith("https://")) {
64 compareString = compareString.substring(8);
65 secure = true;
66 }
67 if (compareString.startsWith("www.")) {
68 compareString = compareString.substring(4);
69 }
70 if (secure) {
71 SELECTION_ARGS = new String[2];
72 SELECTION_ARGS[0] = "https://" + compareString;
73 SELECTION_ARGS[1] = "https://www." + compareString;
74 } else {
75 SELECTION_ARGS = new String[4];
76 SELECTION_ARGS[0] = compareString;
77 SELECTION_ARGS[1] = "www." + compareString;
78 SELECTION_ARGS[2] = "http://" + compareString;
79 SELECTION_ARGS[3] = "http://" + SELECTION_ARGS[1];
80 }
81 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
82 Browser.HISTORY_PROJECTION,
83 secure ? WHERE_CLAUSE_SECURE : WHERE_CLAUSE,
84 SELECTION_ARGS,
85 null);
86 ContentValues map = new ContentValues();
87 if (cursor.moveToFirst() && cursor.getInt(
88 Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) {
89 // This means we have been to this site but not bookmarked
90 // it, so convert the history item to a bookmark
91 map.put(Browser.BookmarkColumns.CREATED, creationTime);
92 map.put(Browser.BookmarkColumns.TITLE, name);
93 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
94 cr.update(Browser.BOOKMARKS_URI, map,
95 "_id = " + cursor.getInt(0), null);
96 } else {
97 int count = cursor.getCount();
98 boolean matchedTitle = false;
99 for (int i = 0; i < count; i++) {
100 // One or more bookmarks already exist for this site.
101 // Check the names of each
102 cursor.moveToPosition(i);
103 if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX)
104 .equals(name)) {
105 // The old bookmark has the same name.
106 // Update its creation time.
107 map.put(Browser.BookmarkColumns.CREATED,
108 creationTime);
109 cr.update(Browser.BOOKMARKS_URI, map,
110 "_id = " + cursor.getInt(0), null);
111 matchedTitle = true;
112 break;
113 }
114 }
115 if (!matchedTitle) {
116 // Adding a bookmark for a site the user has visited,
117 // or a new bookmark (with a different name) for a site
118 // the user has visited
119 map.put(Browser.BookmarkColumns.TITLE, name);
120 map.put(Browser.BookmarkColumns.URL, url);
121 map.put(Browser.BookmarkColumns.CREATED, creationTime);
122 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
123 map.put(Browser.BookmarkColumns.DATE, 0);
124 int visits = 0;
125 if (count > 0) {
126 // The user has already bookmarked, and possibly
127 // visited this site. However, they are creating
128 // a new bookmark with the same url but a different
129 // name. The new bookmark should have the same
130 // number of visits as the already created bookmark.
131 visits = cursor.getInt(
132 Browser.HISTORY_PROJECTION_VISITS_INDEX);
133 }
134 // Bookmark starts with 3 extra visits so that it will
135 // bubble up in the most visited and goto search box
136 map.put(Browser.BookmarkColumns.VISITS, visits + 3);
137 cr.insert(Browser.BOOKMARKS_URI, map);
138 }
139 }
140 WebIconDatabase.getInstance().retainIconForPageUrl(url);
141 cursor.deactivate();
142 if (context != null) {
143 Toast.makeText(context, R.string.added_to_bookmarks,
144 Toast.LENGTH_LONG).show();
145 }
146 }
147
148 /**
149 * Remove a bookmark from the database. If the url is a visited site, it
150 * will remain in the database, but only as a history item, and not as a
151 * bookmarked site.
152 * @param context Context of the calling Activity. This is used to make
153 * Toast confirming that the bookmark has been removed. If the
154 * caller provides null, the Toast will not be shown.
155 * @param cr The ContentResolver being used to remove the bookmark.
156 * @param url URL of the website to be removed.
157 */
158 /* package */ static void removeFromBookmarks(Context context,
159 ContentResolver cr, String url) {
160 Cursor cursor = cr.query(
161 Browser.BOOKMARKS_URI,
162 Browser.HISTORY_PROJECTION,
163 "url = ?",
164 new String[] { url },
165 null);
166 boolean first = cursor.moveToFirst();
167 // Should be in the database no matter what
168 if (!first) {
169 throw new AssertionError("URL is not in the database!");
170 }
171 // Remove from bookmarks
172 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
173 Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
174 cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
175 int numVisits = cursor.getInt(
176 Browser.HISTORY_PROJECTION_VISITS_INDEX);
177 if (0 == numVisits) {
178 cr.delete(uri, null, null);
179 } else {
180 // It is no longer a bookmark, but it is still a visited
181 // site.
182 ContentValues values = new ContentValues();
183 values.put(Browser.BookmarkColumns.BOOKMARK, 0);
184 try {
185 cr.update(uri, values, null, null);
186 } catch (IllegalStateException e) {
187 Log.e("removeFromBookmarks", "no database!");
188 }
189 }
190 if (context != null) {
191 Toast.makeText(context, R.string.removed_from_bookmarks,
192 Toast.LENGTH_LONG).show();
193 }
194 cursor.deactivate();
195 }
196}