mobile/android/base/db/BrowserDB.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/db/BrowserDB.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,358 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.db;
    1.10 +
    1.11 +import java.util.List;
    1.12 +
    1.13 +import org.mozilla.gecko.db.BrowserContract.ExpirePriority;
    1.14 +import org.mozilla.gecko.favicons.decoders.LoadFaviconResult;
    1.15 +import org.mozilla.gecko.mozglue.RobocopTarget;
    1.16 +
    1.17 +import android.content.ContentResolver;
    1.18 +import android.content.ContentValues;
    1.19 +import android.database.ContentObserver;
    1.20 +import android.database.Cursor;
    1.21 +import android.graphics.drawable.BitmapDrawable;
    1.22 +
    1.23 +public class BrowserDB {
    1.24 +    private static boolean sAreContentProvidersEnabled = true;
    1.25 +
    1.26 +    public static interface URLColumns {
    1.27 +        public static String URL = "url";
    1.28 +        public static String TITLE = "title";
    1.29 +        public static String FAVICON = "favicon";
    1.30 +        public static String THUMBNAIL = "thumbnail";
    1.31 +        public static String DATE_LAST_VISITED = "date-last-visited";
    1.32 +        public static String VISITS = "visits";
    1.33 +        public static String KEYWORD = "keyword";
    1.34 +    }
    1.35 +
    1.36 +    private static BrowserDBIface sDb = null;
    1.37 +
    1.38 +    public interface BrowserDBIface {
    1.39 +        public void invalidateCachedState();
    1.40 +
    1.41 +        @RobocopTarget
    1.42 +        public Cursor filter(ContentResolver cr, CharSequence constraint, int limit);
    1.43 +
    1.44 +        // This should only return frecent sites. BrowserDB.getTopSites will do the
    1.45 +        // work to combine that list with the pinned sites list.
    1.46 +        public Cursor getTopSites(ContentResolver cr, int limit);
    1.47 +
    1.48 +        public void updateVisitedHistory(ContentResolver cr, String uri);
    1.49 +
    1.50 +        public void updateHistoryTitle(ContentResolver cr, String uri, String title);
    1.51 +
    1.52 +        public void updateHistoryEntry(ContentResolver cr, String uri, String title,
    1.53 +                                       long date, int visits);
    1.54 +
    1.55 +        @RobocopTarget
    1.56 +        public Cursor getAllVisitedHistory(ContentResolver cr);
    1.57 +
    1.58 +        public Cursor getRecentHistory(ContentResolver cr, int limit);
    1.59 +
    1.60 +        public void expireHistory(ContentResolver cr, ExpirePriority priority);
    1.61 +
    1.62 +        public void removeHistoryEntry(ContentResolver cr, int id);
    1.63 +
    1.64 +        @RobocopTarget
    1.65 +        public void removeHistoryEntry(ContentResolver cr, String url);
    1.66 +
    1.67 +        public void clearHistory(ContentResolver cr);
    1.68 +
    1.69 +        @RobocopTarget
    1.70 +        public Cursor getBookmarksInFolder(ContentResolver cr, long folderId);
    1.71 +
    1.72 +        public Cursor getReadingList(ContentResolver cr);
    1.73 +
    1.74 +        public boolean isVisited(ContentResolver cr, String uri);
    1.75 +
    1.76 +        public int getReadingListCount(ContentResolver cr);
    1.77 +
    1.78 +        @RobocopTarget
    1.79 +        public boolean isBookmark(ContentResolver cr, String uri);
    1.80 +
    1.81 +        public boolean isReadingListItem(ContentResolver cr, String uri);
    1.82 +
    1.83 +        /**
    1.84 +         * Return a combination of fields about the provided URI
    1.85 +         * in a single hit on the DB.
    1.86 +         */
    1.87 +        public int getItemFlags(ContentResolver cr, String uri);
    1.88 +
    1.89 +        public String getUrlForKeyword(ContentResolver cr, String keyword);
    1.90 +
    1.91 +        @RobocopTarget
    1.92 +        public void addBookmark(ContentResolver cr, String title, String uri);
    1.93 +
    1.94 +        public void removeBookmark(ContentResolver cr, int id);
    1.95 +
    1.96 +        @RobocopTarget
    1.97 +        public void removeBookmarksWithURL(ContentResolver cr, String uri);
    1.98 +
    1.99 +        @RobocopTarget
   1.100 +        public void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword);
   1.101 +
   1.102 +        public void addReadingListItem(ContentResolver cr, ContentValues values);
   1.103 +
   1.104 +        public void removeReadingListItemWithURL(ContentResolver cr, String uri);
   1.105 +
   1.106 +        public void removeReadingListItem(ContentResolver cr, int id);
   1.107 +
   1.108 +        public LoadFaviconResult getFaviconForUrl(ContentResolver cr, String uri);
   1.109 +
   1.110 +        public String getFaviconUrlForHistoryUrl(ContentResolver cr, String url);
   1.111 +
   1.112 +        public void updateFaviconForUrl(ContentResolver cr, String pageUri, byte[] encodedFavicon, String faviconUri);
   1.113 +
   1.114 +        public void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail);
   1.115 +
   1.116 +        @RobocopTarget
   1.117 +        public byte[] getThumbnailForUrl(ContentResolver cr, String uri);
   1.118 +
   1.119 +        public Cursor getThumbnailsForUrls(ContentResolver cr, List<String> urls);
   1.120 +
   1.121 +        @RobocopTarget
   1.122 +        public void removeThumbnails(ContentResolver cr);
   1.123 +
   1.124 +        public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer);
   1.125 +
   1.126 +        public void registerHistoryObserver(ContentResolver cr, ContentObserver observer);
   1.127 +
   1.128 +        public int getCount(ContentResolver cr, String database);
   1.129 +
   1.130 +        public void pinSite(ContentResolver cr, String url, String title, int position);
   1.131 +
   1.132 +        public void unpinSite(ContentResolver cr, int position);
   1.133 +
   1.134 +        public void unpinAllSites(ContentResolver cr);
   1.135 +
   1.136 +        public Cursor getPinnedSites(ContentResolver cr, int limit);
   1.137 +
   1.138 +        @RobocopTarget
   1.139 +        public Cursor getBookmarkForUrl(ContentResolver cr, String url);
   1.140 +    }
   1.141 +
   1.142 +    static {
   1.143 +        // Forcing local DB no option to switch to Android DB for now
   1.144 +        sDb = null;
   1.145 +    }
   1.146 +
   1.147 +    public static void initialize(String profile) {
   1.148 +        sDb = new LocalBrowserDB(profile);
   1.149 +    }
   1.150 +
   1.151 +    public static void invalidateCachedState() {
   1.152 +        sDb.invalidateCachedState();
   1.153 +    }
   1.154 +
   1.155 +    @RobocopTarget
   1.156 +    public static Cursor filter(ContentResolver cr, CharSequence constraint, int limit) {
   1.157 +        return sDb.filter(cr, constraint, limit);
   1.158 +    }
   1.159 +
   1.160 +    public static Cursor getTopSites(ContentResolver cr, int minLimit, int maxLimit) {
   1.161 +        // Note this is not a single query anymore, but actually returns a mixture
   1.162 +        // of two queries, one for topSites and one for pinned sites.
   1.163 +        Cursor pinnedSites = sDb.getPinnedSites(cr, minLimit);
   1.164 +        Cursor topSites = sDb.getTopSites(cr, maxLimit - pinnedSites.getCount());
   1.165 +        return new TopSitesCursorWrapper(pinnedSites, topSites, minLimit);
   1.166 +    }
   1.167 +
   1.168 +    public static void updateVisitedHistory(ContentResolver cr, String uri) {
   1.169 +        if (sAreContentProvidersEnabled) {
   1.170 +            sDb.updateVisitedHistory(cr, uri);
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    public static void updateHistoryTitle(ContentResolver cr, String uri, String title) {
   1.175 +        if (sAreContentProvidersEnabled) {
   1.176 +            sDb.updateHistoryTitle(cr, uri, title);
   1.177 +        }
   1.178 +    }
   1.179 +
   1.180 +    public static void updateHistoryEntry(ContentResolver cr, String uri, String title,
   1.181 +                                          long date, int visits) {
   1.182 +        if (sAreContentProvidersEnabled) {
   1.183 +            sDb.updateHistoryEntry(cr, uri, title, date, visits);
   1.184 +        }
   1.185 +    }
   1.186 +
   1.187 +    @RobocopTarget
   1.188 +    public static Cursor getAllVisitedHistory(ContentResolver cr) {
   1.189 +        return (sAreContentProvidersEnabled ? sDb.getAllVisitedHistory(cr) : null);
   1.190 +    }
   1.191 +
   1.192 +    public static Cursor getRecentHistory(ContentResolver cr, int limit) {
   1.193 +        return sDb.getRecentHistory(cr, limit);
   1.194 +    }
   1.195 +
   1.196 +    public static void expireHistory(ContentResolver cr, ExpirePriority priority) {
   1.197 +        if (sDb == null)
   1.198 +            return;
   1.199 +
   1.200 +        if (priority == null)
   1.201 +            priority = ExpirePriority.NORMAL;
   1.202 +        sDb.expireHistory(cr, priority);
   1.203 +    }
   1.204 +
   1.205 +    public static void removeHistoryEntry(ContentResolver cr, int id) {
   1.206 +        sDb.removeHistoryEntry(cr, id);
   1.207 +    }
   1.208 +
   1.209 +    @RobocopTarget
   1.210 +    public static void removeHistoryEntry(ContentResolver cr, String url) {
   1.211 +        sDb.removeHistoryEntry(cr, url);
   1.212 +    }
   1.213 +
   1.214 +    @RobocopTarget
   1.215 +    public static void clearHistory(ContentResolver cr) {
   1.216 +        sDb.clearHistory(cr);
   1.217 +    }
   1.218 +
   1.219 +    @RobocopTarget
   1.220 +    public static Cursor getBookmarksInFolder(ContentResolver cr, long folderId) {
   1.221 +        return sDb.getBookmarksInFolder(cr, folderId);
   1.222 +    }
   1.223 +
   1.224 +    @RobocopTarget
   1.225 +    public static Cursor getReadingList(ContentResolver cr) {
   1.226 +        return sDb.getReadingList(cr);
   1.227 +    }
   1.228 +
   1.229 +    public static String getUrlForKeyword(ContentResolver cr, String keyword) {
   1.230 +        return sDb.getUrlForKeyword(cr, keyword);
   1.231 +    }
   1.232 +
   1.233 +    public static boolean isVisited(ContentResolver cr, String uri) {
   1.234 +        return sDb.isVisited(cr, uri);
   1.235 +    }
   1.236 +
   1.237 +    public static int getReadingListCount(ContentResolver cr) {
   1.238 +        return sDb.getReadingListCount(cr);
   1.239 +    }
   1.240 +
   1.241 +    @RobocopTarget
   1.242 +    public static boolean isBookmark(ContentResolver cr, String uri) {
   1.243 +        return (sAreContentProvidersEnabled && sDb.isBookmark(cr, uri));
   1.244 +    }
   1.245 +
   1.246 +    public static boolean isReadingListItem(ContentResolver cr, String uri) {
   1.247 +        return (sAreContentProvidersEnabled && sDb.isReadingListItem(cr, uri));
   1.248 +    }
   1.249 +
   1.250 +    public static int getItemFlags(ContentResolver cr, String uri) {
   1.251 +        if (!sAreContentProvidersEnabled) {
   1.252 +            return 0;
   1.253 +        }
   1.254 +        return sDb.getItemFlags(cr, uri);
   1.255 +    }
   1.256 +
   1.257 +    public static void addBookmark(ContentResolver cr, String title, String uri) {
   1.258 +        sDb.addBookmark(cr, title, uri);
   1.259 +    }
   1.260 +
   1.261 +    public static void removeBookmark(ContentResolver cr, int id) {
   1.262 +        sDb.removeBookmark(cr, id);
   1.263 +    }
   1.264 +
   1.265 +    @RobocopTarget
   1.266 +    public static void removeBookmarksWithURL(ContentResolver cr, String uri) {
   1.267 +        sDb.removeBookmarksWithURL(cr, uri);
   1.268 +    }
   1.269 +
   1.270 +    @RobocopTarget
   1.271 +    public static void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword) {
   1.272 +        sDb.updateBookmark(cr, id, uri, title, keyword);
   1.273 +    }
   1.274 +
   1.275 +    public static void addReadingListItem(ContentResolver cr, ContentValues values) {
   1.276 +        sDb.addReadingListItem(cr, values);
   1.277 +    }
   1.278 +
   1.279 +    public static void removeReadingListItemWithURL(ContentResolver cr, String uri) {
   1.280 +        sDb.removeReadingListItemWithURL(cr, uri);
   1.281 +    }
   1.282 +
   1.283 +    public static void removeReadingListItem(ContentResolver cr, int id) {
   1.284 +        sDb.removeReadingListItem(cr, id);
   1.285 +    }
   1.286 +
   1.287 +    public static LoadFaviconResult getFaviconForFaviconUrl(ContentResolver cr, String faviconURL) {
   1.288 +        return sDb.getFaviconForUrl(cr, faviconURL);
   1.289 +    }
   1.290 +
   1.291 +    public static String getFaviconUrlForHistoryUrl(ContentResolver cr, String url) {
   1.292 +        return sDb.getFaviconUrlForHistoryUrl(cr, url);
   1.293 +    }
   1.294 +
   1.295 +    public static void updateFaviconForUrl(ContentResolver cr, String pageUri, byte[] encodedFavicon, String faviconUri) {
   1.296 +        sDb.updateFaviconForUrl(cr, pageUri, encodedFavicon, faviconUri);
   1.297 +    }
   1.298 +
   1.299 +    public static void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail) {
   1.300 +        sDb.updateThumbnailForUrl(cr, uri, thumbnail);
   1.301 +    }
   1.302 +
   1.303 +    @RobocopTarget
   1.304 +    public static byte[] getThumbnailForUrl(ContentResolver cr, String uri) {
   1.305 +        return sDb.getThumbnailForUrl(cr, uri);
   1.306 +    }
   1.307 +
   1.308 +    public static Cursor getThumbnailsForUrls(ContentResolver cr, List<String> urls) {
   1.309 +        return sDb.getThumbnailsForUrls(cr, urls);
   1.310 +    }
   1.311 +
   1.312 +    @RobocopTarget
   1.313 +    public static void removeThumbnails(ContentResolver cr) {
   1.314 +        sDb.removeThumbnails(cr);
   1.315 +    }
   1.316 +
   1.317 +    public static void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) {
   1.318 +        sDb.registerBookmarkObserver(cr, observer);
   1.319 +    }
   1.320 +
   1.321 +    public static void registerHistoryObserver(ContentResolver cr, ContentObserver observer) {
   1.322 +        sDb.registerHistoryObserver(cr, observer);
   1.323 +    }
   1.324 +
   1.325 +    public static void unregisterContentObserver(ContentResolver cr, ContentObserver observer) {
   1.326 +        cr.unregisterContentObserver(observer);
   1.327 +    }
   1.328 +
   1.329 +    public static int getCount(ContentResolver cr, String database) {
   1.330 +        return sDb.getCount(cr, database);
   1.331 +    }
   1.332 +
   1.333 +    public static void pinSite(ContentResolver cr, String url, String title, int position) {
   1.334 +        sDb.pinSite(cr, url, title, position);
   1.335 +    }
   1.336 +
   1.337 +    public static void unpinSite(ContentResolver cr, int position) {
   1.338 +        sDb.unpinSite(cr, position);
   1.339 +    }
   1.340 +
   1.341 +    public static void unpinAllSites(ContentResolver cr) {
   1.342 +        sDb.unpinAllSites(cr);
   1.343 +    }
   1.344 +
   1.345 +    public static Cursor getPinnedSites(ContentResolver cr, int limit) {
   1.346 +        return sDb.getPinnedSites(cr, limit);
   1.347 +    }
   1.348 +
   1.349 +    @RobocopTarget
   1.350 +    public static Cursor getBookmarkForUrl(ContentResolver cr, String url) {
   1.351 +        return sDb.getBookmarkForUrl(cr, url);
   1.352 +    }
   1.353 +
   1.354 +    public static boolean areContentProvidersDisabled() {
   1.355 +        return sAreContentProvidersEnabled;
   1.356 +    }
   1.357 +
   1.358 +    public static void setEnableContentProviders(boolean enableContentProviders) {
   1.359 +        sAreContentProvidersEnabled = enableContentProviders;
   1.360 +    }
   1.361 +}

mercurial