|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.db; |
|
7 |
|
8 import java.util.List; |
|
9 |
|
10 import org.mozilla.gecko.db.BrowserContract.ExpirePriority; |
|
11 import org.mozilla.gecko.favicons.decoders.LoadFaviconResult; |
|
12 import org.mozilla.gecko.mozglue.RobocopTarget; |
|
13 |
|
14 import android.content.ContentResolver; |
|
15 import android.content.ContentValues; |
|
16 import android.database.ContentObserver; |
|
17 import android.database.Cursor; |
|
18 import android.graphics.drawable.BitmapDrawable; |
|
19 |
|
20 public class BrowserDB { |
|
21 private static boolean sAreContentProvidersEnabled = true; |
|
22 |
|
23 public static interface URLColumns { |
|
24 public static String URL = "url"; |
|
25 public static String TITLE = "title"; |
|
26 public static String FAVICON = "favicon"; |
|
27 public static String THUMBNAIL = "thumbnail"; |
|
28 public static String DATE_LAST_VISITED = "date-last-visited"; |
|
29 public static String VISITS = "visits"; |
|
30 public static String KEYWORD = "keyword"; |
|
31 } |
|
32 |
|
33 private static BrowserDBIface sDb = null; |
|
34 |
|
35 public interface BrowserDBIface { |
|
36 public void invalidateCachedState(); |
|
37 |
|
38 @RobocopTarget |
|
39 public Cursor filter(ContentResolver cr, CharSequence constraint, int limit); |
|
40 |
|
41 // This should only return frecent sites. BrowserDB.getTopSites will do the |
|
42 // work to combine that list with the pinned sites list. |
|
43 public Cursor getTopSites(ContentResolver cr, int limit); |
|
44 |
|
45 public void updateVisitedHistory(ContentResolver cr, String uri); |
|
46 |
|
47 public void updateHistoryTitle(ContentResolver cr, String uri, String title); |
|
48 |
|
49 public void updateHistoryEntry(ContentResolver cr, String uri, String title, |
|
50 long date, int visits); |
|
51 |
|
52 @RobocopTarget |
|
53 public Cursor getAllVisitedHistory(ContentResolver cr); |
|
54 |
|
55 public Cursor getRecentHistory(ContentResolver cr, int limit); |
|
56 |
|
57 public void expireHistory(ContentResolver cr, ExpirePriority priority); |
|
58 |
|
59 public void removeHistoryEntry(ContentResolver cr, int id); |
|
60 |
|
61 @RobocopTarget |
|
62 public void removeHistoryEntry(ContentResolver cr, String url); |
|
63 |
|
64 public void clearHistory(ContentResolver cr); |
|
65 |
|
66 @RobocopTarget |
|
67 public Cursor getBookmarksInFolder(ContentResolver cr, long folderId); |
|
68 |
|
69 public Cursor getReadingList(ContentResolver cr); |
|
70 |
|
71 public boolean isVisited(ContentResolver cr, String uri); |
|
72 |
|
73 public int getReadingListCount(ContentResolver cr); |
|
74 |
|
75 @RobocopTarget |
|
76 public boolean isBookmark(ContentResolver cr, String uri); |
|
77 |
|
78 public boolean isReadingListItem(ContentResolver cr, String uri); |
|
79 |
|
80 /** |
|
81 * Return a combination of fields about the provided URI |
|
82 * in a single hit on the DB. |
|
83 */ |
|
84 public int getItemFlags(ContentResolver cr, String uri); |
|
85 |
|
86 public String getUrlForKeyword(ContentResolver cr, String keyword); |
|
87 |
|
88 @RobocopTarget |
|
89 public void addBookmark(ContentResolver cr, String title, String uri); |
|
90 |
|
91 public void removeBookmark(ContentResolver cr, int id); |
|
92 |
|
93 @RobocopTarget |
|
94 public void removeBookmarksWithURL(ContentResolver cr, String uri); |
|
95 |
|
96 @RobocopTarget |
|
97 public void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword); |
|
98 |
|
99 public void addReadingListItem(ContentResolver cr, ContentValues values); |
|
100 |
|
101 public void removeReadingListItemWithURL(ContentResolver cr, String uri); |
|
102 |
|
103 public void removeReadingListItem(ContentResolver cr, int id); |
|
104 |
|
105 public LoadFaviconResult getFaviconForUrl(ContentResolver cr, String uri); |
|
106 |
|
107 public String getFaviconUrlForHistoryUrl(ContentResolver cr, String url); |
|
108 |
|
109 public void updateFaviconForUrl(ContentResolver cr, String pageUri, byte[] encodedFavicon, String faviconUri); |
|
110 |
|
111 public void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail); |
|
112 |
|
113 @RobocopTarget |
|
114 public byte[] getThumbnailForUrl(ContentResolver cr, String uri); |
|
115 |
|
116 public Cursor getThumbnailsForUrls(ContentResolver cr, List<String> urls); |
|
117 |
|
118 @RobocopTarget |
|
119 public void removeThumbnails(ContentResolver cr); |
|
120 |
|
121 public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer); |
|
122 |
|
123 public void registerHistoryObserver(ContentResolver cr, ContentObserver observer); |
|
124 |
|
125 public int getCount(ContentResolver cr, String database); |
|
126 |
|
127 public void pinSite(ContentResolver cr, String url, String title, int position); |
|
128 |
|
129 public void unpinSite(ContentResolver cr, int position); |
|
130 |
|
131 public void unpinAllSites(ContentResolver cr); |
|
132 |
|
133 public Cursor getPinnedSites(ContentResolver cr, int limit); |
|
134 |
|
135 @RobocopTarget |
|
136 public Cursor getBookmarkForUrl(ContentResolver cr, String url); |
|
137 } |
|
138 |
|
139 static { |
|
140 // Forcing local DB no option to switch to Android DB for now |
|
141 sDb = null; |
|
142 } |
|
143 |
|
144 public static void initialize(String profile) { |
|
145 sDb = new LocalBrowserDB(profile); |
|
146 } |
|
147 |
|
148 public static void invalidateCachedState() { |
|
149 sDb.invalidateCachedState(); |
|
150 } |
|
151 |
|
152 @RobocopTarget |
|
153 public static Cursor filter(ContentResolver cr, CharSequence constraint, int limit) { |
|
154 return sDb.filter(cr, constraint, limit); |
|
155 } |
|
156 |
|
157 public static Cursor getTopSites(ContentResolver cr, int minLimit, int maxLimit) { |
|
158 // Note this is not a single query anymore, but actually returns a mixture |
|
159 // of two queries, one for topSites and one for pinned sites. |
|
160 Cursor pinnedSites = sDb.getPinnedSites(cr, minLimit); |
|
161 Cursor topSites = sDb.getTopSites(cr, maxLimit - pinnedSites.getCount()); |
|
162 return new TopSitesCursorWrapper(pinnedSites, topSites, minLimit); |
|
163 } |
|
164 |
|
165 public static void updateVisitedHistory(ContentResolver cr, String uri) { |
|
166 if (sAreContentProvidersEnabled) { |
|
167 sDb.updateVisitedHistory(cr, uri); |
|
168 } |
|
169 } |
|
170 |
|
171 public static void updateHistoryTitle(ContentResolver cr, String uri, String title) { |
|
172 if (sAreContentProvidersEnabled) { |
|
173 sDb.updateHistoryTitle(cr, uri, title); |
|
174 } |
|
175 } |
|
176 |
|
177 public static void updateHistoryEntry(ContentResolver cr, String uri, String title, |
|
178 long date, int visits) { |
|
179 if (sAreContentProvidersEnabled) { |
|
180 sDb.updateHistoryEntry(cr, uri, title, date, visits); |
|
181 } |
|
182 } |
|
183 |
|
184 @RobocopTarget |
|
185 public static Cursor getAllVisitedHistory(ContentResolver cr) { |
|
186 return (sAreContentProvidersEnabled ? sDb.getAllVisitedHistory(cr) : null); |
|
187 } |
|
188 |
|
189 public static Cursor getRecentHistory(ContentResolver cr, int limit) { |
|
190 return sDb.getRecentHistory(cr, limit); |
|
191 } |
|
192 |
|
193 public static void expireHistory(ContentResolver cr, ExpirePriority priority) { |
|
194 if (sDb == null) |
|
195 return; |
|
196 |
|
197 if (priority == null) |
|
198 priority = ExpirePriority.NORMAL; |
|
199 sDb.expireHistory(cr, priority); |
|
200 } |
|
201 |
|
202 public static void removeHistoryEntry(ContentResolver cr, int id) { |
|
203 sDb.removeHistoryEntry(cr, id); |
|
204 } |
|
205 |
|
206 @RobocopTarget |
|
207 public static void removeHistoryEntry(ContentResolver cr, String url) { |
|
208 sDb.removeHistoryEntry(cr, url); |
|
209 } |
|
210 |
|
211 @RobocopTarget |
|
212 public static void clearHistory(ContentResolver cr) { |
|
213 sDb.clearHistory(cr); |
|
214 } |
|
215 |
|
216 @RobocopTarget |
|
217 public static Cursor getBookmarksInFolder(ContentResolver cr, long folderId) { |
|
218 return sDb.getBookmarksInFolder(cr, folderId); |
|
219 } |
|
220 |
|
221 @RobocopTarget |
|
222 public static Cursor getReadingList(ContentResolver cr) { |
|
223 return sDb.getReadingList(cr); |
|
224 } |
|
225 |
|
226 public static String getUrlForKeyword(ContentResolver cr, String keyword) { |
|
227 return sDb.getUrlForKeyword(cr, keyword); |
|
228 } |
|
229 |
|
230 public static boolean isVisited(ContentResolver cr, String uri) { |
|
231 return sDb.isVisited(cr, uri); |
|
232 } |
|
233 |
|
234 public static int getReadingListCount(ContentResolver cr) { |
|
235 return sDb.getReadingListCount(cr); |
|
236 } |
|
237 |
|
238 @RobocopTarget |
|
239 public static boolean isBookmark(ContentResolver cr, String uri) { |
|
240 return (sAreContentProvidersEnabled && sDb.isBookmark(cr, uri)); |
|
241 } |
|
242 |
|
243 public static boolean isReadingListItem(ContentResolver cr, String uri) { |
|
244 return (sAreContentProvidersEnabled && sDb.isReadingListItem(cr, uri)); |
|
245 } |
|
246 |
|
247 public static int getItemFlags(ContentResolver cr, String uri) { |
|
248 if (!sAreContentProvidersEnabled) { |
|
249 return 0; |
|
250 } |
|
251 return sDb.getItemFlags(cr, uri); |
|
252 } |
|
253 |
|
254 public static void addBookmark(ContentResolver cr, String title, String uri) { |
|
255 sDb.addBookmark(cr, title, uri); |
|
256 } |
|
257 |
|
258 public static void removeBookmark(ContentResolver cr, int id) { |
|
259 sDb.removeBookmark(cr, id); |
|
260 } |
|
261 |
|
262 @RobocopTarget |
|
263 public static void removeBookmarksWithURL(ContentResolver cr, String uri) { |
|
264 sDb.removeBookmarksWithURL(cr, uri); |
|
265 } |
|
266 |
|
267 @RobocopTarget |
|
268 public static void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword) { |
|
269 sDb.updateBookmark(cr, id, uri, title, keyword); |
|
270 } |
|
271 |
|
272 public static void addReadingListItem(ContentResolver cr, ContentValues values) { |
|
273 sDb.addReadingListItem(cr, values); |
|
274 } |
|
275 |
|
276 public static void removeReadingListItemWithURL(ContentResolver cr, String uri) { |
|
277 sDb.removeReadingListItemWithURL(cr, uri); |
|
278 } |
|
279 |
|
280 public static void removeReadingListItem(ContentResolver cr, int id) { |
|
281 sDb.removeReadingListItem(cr, id); |
|
282 } |
|
283 |
|
284 public static LoadFaviconResult getFaviconForFaviconUrl(ContentResolver cr, String faviconURL) { |
|
285 return sDb.getFaviconForUrl(cr, faviconURL); |
|
286 } |
|
287 |
|
288 public static String getFaviconUrlForHistoryUrl(ContentResolver cr, String url) { |
|
289 return sDb.getFaviconUrlForHistoryUrl(cr, url); |
|
290 } |
|
291 |
|
292 public static void updateFaviconForUrl(ContentResolver cr, String pageUri, byte[] encodedFavicon, String faviconUri) { |
|
293 sDb.updateFaviconForUrl(cr, pageUri, encodedFavicon, faviconUri); |
|
294 } |
|
295 |
|
296 public static void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail) { |
|
297 sDb.updateThumbnailForUrl(cr, uri, thumbnail); |
|
298 } |
|
299 |
|
300 @RobocopTarget |
|
301 public static byte[] getThumbnailForUrl(ContentResolver cr, String uri) { |
|
302 return sDb.getThumbnailForUrl(cr, uri); |
|
303 } |
|
304 |
|
305 public static Cursor getThumbnailsForUrls(ContentResolver cr, List<String> urls) { |
|
306 return sDb.getThumbnailsForUrls(cr, urls); |
|
307 } |
|
308 |
|
309 @RobocopTarget |
|
310 public static void removeThumbnails(ContentResolver cr) { |
|
311 sDb.removeThumbnails(cr); |
|
312 } |
|
313 |
|
314 public static void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) { |
|
315 sDb.registerBookmarkObserver(cr, observer); |
|
316 } |
|
317 |
|
318 public static void registerHistoryObserver(ContentResolver cr, ContentObserver observer) { |
|
319 sDb.registerHistoryObserver(cr, observer); |
|
320 } |
|
321 |
|
322 public static void unregisterContentObserver(ContentResolver cr, ContentObserver observer) { |
|
323 cr.unregisterContentObserver(observer); |
|
324 } |
|
325 |
|
326 public static int getCount(ContentResolver cr, String database) { |
|
327 return sDb.getCount(cr, database); |
|
328 } |
|
329 |
|
330 public static void pinSite(ContentResolver cr, String url, String title, int position) { |
|
331 sDb.pinSite(cr, url, title, position); |
|
332 } |
|
333 |
|
334 public static void unpinSite(ContentResolver cr, int position) { |
|
335 sDb.unpinSite(cr, position); |
|
336 } |
|
337 |
|
338 public static void unpinAllSites(ContentResolver cr) { |
|
339 sDb.unpinAllSites(cr); |
|
340 } |
|
341 |
|
342 public static Cursor getPinnedSites(ContentResolver cr, int limit) { |
|
343 return sDb.getPinnedSites(cr, limit); |
|
344 } |
|
345 |
|
346 @RobocopTarget |
|
347 public static Cursor getBookmarkForUrl(ContentResolver cr, String url) { |
|
348 return sDb.getBookmarkForUrl(cr, url); |
|
349 } |
|
350 |
|
351 public static boolean areContentProvidersDisabled() { |
|
352 return sAreContentProvidersEnabled; |
|
353 } |
|
354 |
|
355 public static void setEnableContentProviders(boolean enableContentProviders) { |
|
356 sAreContentProvidersEnabled = enableContentProviders; |
|
357 } |
|
358 } |