2012-02-27 10:51:28 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2013-04-09 17:10:48 -07:00
|
|
|
package org.mozilla.gecko.db;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
2012-03-23 15:52:42 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Combined;
|
2011-12-13 06:46:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.CommonColumns;
|
2012-11-13 10:56:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.FaviconColumns;
|
2013-05-07 18:30:47 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Favicons;
|
2011-12-08 08:37:21 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.History;
|
2011-12-13 06:12:01 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Schema;
|
2011-12-13 06:46:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.SyncColumns;
|
2012-11-13 10:56:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
|
2011-12-13 06:46:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.URLColumns;
|
2011-12-16 15:11:09 -08:00
|
|
|
import org.mozilla.gecko.sync.Utils;
|
2013-05-07 18:30:47 -07:00
|
|
|
|
2012-10-04 16:57:39 -07:00
|
|
|
import android.app.SearchManager;
|
2013-05-07 18:30:47 -07:00
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
import android.content.ContentProviderResult;
|
2011-12-08 08:37:21 -08:00
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
2013-02-13 19:25:38 -08:00
|
|
|
import android.content.Context;
|
2013-02-14 15:35:39 -08:00
|
|
|
import android.content.OperationApplicationException;
|
2011-12-08 08:37:21 -08:00
|
|
|
import android.content.UriMatcher;
|
|
|
|
import android.database.Cursor;
|
2012-02-20 11:28:27 -08:00
|
|
|
import android.database.DatabaseUtils;
|
2011-12-13 06:12:01 -08:00
|
|
|
import android.database.MatrixCursor;
|
2012-03-12 14:48:15 -07:00
|
|
|
import android.database.SQLException;
|
2011-12-08 08:37:21 -08:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteQueryBuilder;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
public class BrowserProvider extends TransactionalProvider<BrowserDatabaseHelper> {
|
2011-12-08 08:37:21 -08:00
|
|
|
private static final String LOGTAG = "GeckoBrowserProvider";
|
2011-12-13 06:12:01 -08:00
|
|
|
|
2012-04-05 19:25:34 -07:00
|
|
|
// How many records to reposition in a single query.
|
|
|
|
// This should be less than the SQLite maximum number of query variables
|
|
|
|
// (currently 999) divided by the number of variables used per positioning
|
|
|
|
// query (currently 3).
|
|
|
|
static final int MAX_POSITION_UPDATES_PER_QUERY = 100;
|
|
|
|
|
2012-10-19 17:35:37 -07:00
|
|
|
// Minimum number of records to keep when expiring history.
|
|
|
|
static final int DEFAULT_EXPIRY_RETAIN_COUNT = 2000;
|
|
|
|
static final int AGGRESSIVE_EXPIRY_RETAIN_COUNT = 500;
|
|
|
|
|
|
|
|
// Minimum duration to keep when expiring.
|
|
|
|
static final long DEFAULT_EXPIRY_PRESERVE_WINDOW = 1000L * 60L * 60L * 24L * 28L; // Four weeks.
|
2012-11-07 09:39:55 -08:00
|
|
|
// Minimum number of thumbnails to keep around.
|
|
|
|
static final int DEFAULT_EXPIRY_THUMBNAIL_COUNT = 15;
|
2012-10-19 17:35:37 -07:00
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
static final String TABLE_BOOKMARKS = Bookmarks.TABLE_NAME;
|
|
|
|
static final String TABLE_HISTORY = History.TABLE_NAME;
|
|
|
|
static final String TABLE_FAVICONS = Favicons.TABLE_NAME;
|
|
|
|
static final String TABLE_THUMBNAILS = Thumbnails.TABLE_NAME;
|
2012-10-31 05:34:31 -07:00
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
static final String VIEW_COMBINED = Combined.VIEW_NAME;
|
|
|
|
static final String VIEW_BOOKMARKS_WITH_FAVICONS = Bookmarks.VIEW_WITH_FAVICONS;
|
|
|
|
static final String VIEW_HISTORY_WITH_FAVICONS = History.VIEW_WITH_FAVICONS;
|
|
|
|
static final String VIEW_COMBINED_WITH_FAVICONS = Combined.VIEW_WITH_FAVICONS;
|
2011-12-13 11:47:40 -08:00
|
|
|
|
2014-02-12 11:50:35 -08:00
|
|
|
static final String VIEW_FLAGS = "flags";
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
// Bookmark matches
|
|
|
|
static final int BOOKMARKS = 100;
|
|
|
|
static final int BOOKMARKS_ID = 101;
|
|
|
|
static final int BOOKMARKS_FOLDER_ID = 102;
|
2012-02-14 17:10:18 -08:00
|
|
|
static final int BOOKMARKS_PARENT = 103;
|
2012-02-22 12:37:34 -08:00
|
|
|
static final int BOOKMARKS_POSITIONS = 104;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
// History matches
|
|
|
|
static final int HISTORY = 200;
|
|
|
|
static final int HISTORY_ID = 201;
|
2012-10-19 17:35:37 -07:00
|
|
|
static final int HISTORY_OLD = 202;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
// Favicon matches
|
|
|
|
static final int FAVICONS = 300;
|
|
|
|
static final int FAVICON_ID = 301;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-13 06:12:01 -08:00
|
|
|
// Schema matches
|
|
|
|
static final int SCHEMA = 400;
|
|
|
|
|
2012-03-23 15:52:42 -07:00
|
|
|
// Combined bookmarks and history matches
|
|
|
|
static final int COMBINED = 500;
|
|
|
|
|
2012-04-04 12:48:46 -07:00
|
|
|
// Control matches
|
|
|
|
static final int CONTROL = 600;
|
|
|
|
|
2012-10-04 16:57:39 -07:00
|
|
|
// Search Suggest matches
|
|
|
|
static final int SEARCH_SUGGEST = 700;
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
// Thumbnail matches
|
|
|
|
static final int THUMBNAILS = 800;
|
|
|
|
static final int THUMBNAIL_ID = 801;
|
|
|
|
|
2014-02-12 11:50:35 -08:00
|
|
|
static final int FLAGS = 900;
|
|
|
|
|
2012-03-20 04:44:32 -07:00
|
|
|
static final String DEFAULT_BOOKMARKS_SORT_ORDER = Bookmarks.TYPE
|
|
|
|
+ " ASC, " + Bookmarks.POSITION + " ASC, " + Bookmarks._ID
|
2011-12-08 08:37:21 -08:00
|
|
|
+ " ASC";
|
|
|
|
|
|
|
|
static final String DEFAULT_HISTORY_SORT_ORDER = History.DATE_LAST_VISITED + " DESC";
|
|
|
|
|
|
|
|
static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
2012-02-27 10:51:28 -08:00
|
|
|
static final Map<String, String> BOOKMARKS_PROJECTION_MAP;
|
|
|
|
static final Map<String, String> HISTORY_PROJECTION_MAP;
|
2012-03-23 15:52:42 -07:00
|
|
|
static final Map<String, String> COMBINED_PROJECTION_MAP;
|
2012-02-27 10:51:28 -08:00
|
|
|
static final Map<String, String> SCHEMA_PROJECTION_MAP;
|
2012-10-04 16:57:39 -07:00
|
|
|
static final Map<String, String> SEARCH_SUGGEST_PROJECTION_MAP;
|
2012-11-13 10:56:58 -08:00
|
|
|
static final Map<String, String> FAVICONS_PROJECTION_MAP;
|
|
|
|
static final Map<String, String> THUMBNAILS_PROJECTION_MAP;
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
static {
|
2012-02-27 10:51:28 -08:00
|
|
|
// We will reuse this.
|
2011-12-08 08:37:21 -08:00
|
|
|
HashMap<String, String> map;
|
|
|
|
|
|
|
|
// Bookmarks
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "bookmarks", BOOKMARKS);
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "bookmarks/#", BOOKMARKS_ID);
|
2012-02-14 17:10:18 -08:00
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "bookmarks/parents", BOOKMARKS_PARENT);
|
2012-02-22 12:37:34 -08:00
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "bookmarks/positions", BOOKMARKS_POSITIONS);
|
2011-12-08 08:37:21 -08:00
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "bookmarks/folder/#", BOOKMARKS_FOLDER_ID);
|
|
|
|
|
2012-02-27 10:51:28 -08:00
|
|
|
map = new HashMap<String, String>();
|
2011-12-13 11:47:40 -08:00
|
|
|
map.put(Bookmarks._ID, Bookmarks._ID);
|
2011-12-08 08:37:21 -08:00
|
|
|
map.put(Bookmarks.TITLE, Bookmarks.TITLE);
|
|
|
|
map.put(Bookmarks.URL, Bookmarks.URL);
|
|
|
|
map.put(Bookmarks.FAVICON, Bookmarks.FAVICON);
|
2012-11-13 10:56:58 -08:00
|
|
|
map.put(Bookmarks.FAVICON_ID, Bookmarks.FAVICON_ID);
|
|
|
|
map.put(Bookmarks.FAVICON_URL, Bookmarks.FAVICON_URL);
|
2012-03-20 04:44:32 -07:00
|
|
|
map.put(Bookmarks.TYPE, Bookmarks.TYPE);
|
2011-12-08 08:37:21 -08:00
|
|
|
map.put(Bookmarks.PARENT, Bookmarks.PARENT);
|
|
|
|
map.put(Bookmarks.POSITION, Bookmarks.POSITION);
|
2011-12-13 06:46:58 -08:00
|
|
|
map.put(Bookmarks.TAGS, Bookmarks.TAGS);
|
|
|
|
map.put(Bookmarks.DESCRIPTION, Bookmarks.DESCRIPTION);
|
|
|
|
map.put(Bookmarks.KEYWORD, Bookmarks.KEYWORD);
|
2011-12-13 11:47:40 -08:00
|
|
|
map.put(Bookmarks.DATE_CREATED, Bookmarks.DATE_CREATED);
|
|
|
|
map.put(Bookmarks.DATE_MODIFIED, Bookmarks.DATE_MODIFIED);
|
|
|
|
map.put(Bookmarks.GUID, Bookmarks.GUID);
|
|
|
|
map.put(Bookmarks.IS_DELETED, Bookmarks.IS_DELETED);
|
2012-02-27 10:51:28 -08:00
|
|
|
BOOKMARKS_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
// History
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "history", HISTORY);
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "history/#", HISTORY_ID);
|
2012-10-19 17:35:37 -07:00
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "history/old", HISTORY_OLD);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-02-27 10:51:28 -08:00
|
|
|
map = new HashMap<String, String>();
|
2011-12-13 11:47:40 -08:00
|
|
|
map.put(History._ID, History._ID);
|
2011-12-08 08:37:21 -08:00
|
|
|
map.put(History.TITLE, History.TITLE);
|
|
|
|
map.put(History.URL, History.URL);
|
|
|
|
map.put(History.FAVICON, History.FAVICON);
|
2012-11-13 10:56:58 -08:00
|
|
|
map.put(History.FAVICON_ID, History.FAVICON_ID);
|
|
|
|
map.put(History.FAVICON_URL, History.FAVICON_URL);
|
2011-12-08 08:37:21 -08:00
|
|
|
map.put(History.VISITS, History.VISITS);
|
|
|
|
map.put(History.DATE_LAST_VISITED, History.DATE_LAST_VISITED);
|
2011-12-13 11:47:40 -08:00
|
|
|
map.put(History.DATE_CREATED, History.DATE_CREATED);
|
|
|
|
map.put(History.DATE_MODIFIED, History.DATE_MODIFIED);
|
|
|
|
map.put(History.GUID, History.GUID);
|
|
|
|
map.put(History.IS_DELETED, History.IS_DELETED);
|
2012-02-27 10:51:28 -08:00
|
|
|
HISTORY_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
// Favicons
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "favicons", FAVICONS);
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "favicons/#", FAVICON_ID);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-02-27 10:51:28 -08:00
|
|
|
map = new HashMap<String, String>();
|
2012-11-13 10:56:58 -08:00
|
|
|
map.put(Favicons._ID, Favicons._ID);
|
|
|
|
map.put(Favicons.URL, Favicons.URL);
|
|
|
|
map.put(Favicons.DATA, Favicons.DATA);
|
|
|
|
map.put(Favicons.DATE_CREATED, Favicons.DATE_CREATED);
|
|
|
|
map.put(Favicons.DATE_MODIFIED, Favicons.DATE_MODIFIED);
|
|
|
|
FAVICONS_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
|
|
|
|
|
|
|
// Thumbnails
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "thumbnails", THUMBNAILS);
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "thumbnails/#", THUMBNAIL_ID);
|
|
|
|
|
|
|
|
map = new HashMap<String, String>();
|
|
|
|
map.put(Thumbnails._ID, Thumbnails._ID);
|
|
|
|
map.put(Thumbnails.URL, Thumbnails.URL);
|
|
|
|
map.put(Thumbnails.DATA, Thumbnails.DATA);
|
|
|
|
THUMBNAILS_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
2011-12-13 06:12:01 -08:00
|
|
|
|
2012-03-23 15:52:42 -07:00
|
|
|
// Combined bookmarks and history
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "combined", COMBINED);
|
|
|
|
|
|
|
|
map = new HashMap<String, String>();
|
|
|
|
map.put(Combined._ID, Combined._ID);
|
|
|
|
map.put(Combined.BOOKMARK_ID, Combined.BOOKMARK_ID);
|
|
|
|
map.put(Combined.HISTORY_ID, Combined.HISTORY_ID);
|
2012-09-10 10:24:25 -07:00
|
|
|
map.put(Combined.DISPLAY, "MAX(" + Combined.DISPLAY + ") AS " + Combined.DISPLAY);
|
2012-03-23 15:52:42 -07:00
|
|
|
map.put(Combined.URL, Combined.URL);
|
|
|
|
map.put(Combined.TITLE, Combined.TITLE);
|
|
|
|
map.put(Combined.VISITS, Combined.VISITS);
|
|
|
|
map.put(Combined.DATE_LAST_VISITED, Combined.DATE_LAST_VISITED);
|
|
|
|
map.put(Combined.FAVICON, Combined.FAVICON);
|
2012-11-13 10:56:58 -08:00
|
|
|
map.put(Combined.FAVICON_ID, Combined.FAVICON_ID);
|
|
|
|
map.put(Combined.FAVICON_URL, Combined.FAVICON_URL);
|
2012-03-23 15:52:42 -07:00
|
|
|
COMBINED_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
|
|
|
|
2011-12-13 06:12:01 -08:00
|
|
|
// Schema
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "schema", SCHEMA);
|
|
|
|
|
2012-02-27 10:51:28 -08:00
|
|
|
map = new HashMap<String, String>();
|
2011-12-13 06:12:01 -08:00
|
|
|
map.put(Schema.VERSION, Schema.VERSION);
|
2012-02-27 10:51:28 -08:00
|
|
|
SCHEMA_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
2012-04-04 12:48:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Control
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "control", CONTROL);
|
2012-10-04 16:57:39 -07:00
|
|
|
|
|
|
|
// Search Suggest
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
|
|
|
|
|
2014-02-12 11:50:35 -08:00
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "flags", FLAGS);
|
|
|
|
|
2012-10-04 16:57:39 -07:00
|
|
|
map = new HashMap<String, String>();
|
|
|
|
map.put(SearchManager.SUGGEST_COLUMN_TEXT_1,
|
|
|
|
Combined.TITLE + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_1);
|
|
|
|
map.put(SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
|
|
|
|
Combined.URL + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
|
|
|
|
map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA,
|
|
|
|
Combined.URL + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA);
|
|
|
|
SEARCH_SUGGEST_PROJECTION_MAP = Collections.unmodifiableMap(map);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static final String qualifyColumn(String table, String column) {
|
|
|
|
return table + "." + column;
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
private static boolean hasFaviconsInProjection(String[] projection) {
|
2011-12-14 21:37:11 -08:00
|
|
|
if (projection == null) return true;
|
2011-12-13 11:47:40 -08:00
|
|
|
for (int i = 0; i < projection.length; ++i) {
|
2012-11-13 10:56:58 -08:00
|
|
|
if (projection[i].equals(FaviconColumns.FAVICON) ||
|
|
|
|
projection[i].equals(FaviconColumns.FAVICON_URL))
|
2011-12-13 11:47:40 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
// Calculate these once, at initialization. isLoggable is too expensive to
|
|
|
|
// have in-line in each log call.
|
|
|
|
private static boolean logDebug = Log.isLoggable(LOGTAG, Log.DEBUG);
|
|
|
|
private static boolean logVerbose = Log.isLoggable(LOGTAG, Log.VERBOSE);
|
|
|
|
protected static void trace(String message) {
|
|
|
|
if (logVerbose) {
|
|
|
|
Log.v(LOGTAG, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static void debug(String message) {
|
|
|
|
if (logDebug) {
|
|
|
|
Log.d(LOGTAG, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 17:35:37 -07:00
|
|
|
/**
|
|
|
|
* Remove enough history items to bring the database count below <code>retain</code>,
|
|
|
|
* removing no items with a modified time after <code>keepAfter</code>.
|
|
|
|
*
|
|
|
|
* Provide <code>keepAfter</code> less than or equal to zero to skip that check.
|
|
|
|
*
|
|
|
|
* Items will be removed according to an approximate frecency calculation.
|
|
|
|
*/
|
2012-11-13 10:56:58 -08:00
|
|
|
private void expireHistory(final SQLiteDatabase db, final int retain, final long keepAfter) {
|
2013-10-31 10:35:16 -07:00
|
|
|
Log.d(LOGTAG, "Expiring history.");
|
2012-10-19 17:35:37 -07:00
|
|
|
final long rows = DatabaseUtils.queryNumEntries(db, TABLE_HISTORY);
|
2012-11-07 09:39:55 -08:00
|
|
|
|
2012-10-19 17:35:37 -07:00
|
|
|
if (retain >= rows) {
|
|
|
|
debug("Not expiring history: only have " + rows + " rows.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-07 09:39:55 -08:00
|
|
|
final String sortOrder = BrowserContract.getFrecencySortOrder(false, true);
|
2012-10-19 17:35:37 -07:00
|
|
|
final long toRemove = rows - retain;
|
|
|
|
debug("Expiring at most " + toRemove + " rows earlier than " + keepAfter + ".");
|
|
|
|
|
|
|
|
final String sql;
|
|
|
|
if (keepAfter > 0) {
|
2012-11-07 09:39:55 -08:00
|
|
|
sql = "DELETE FROM " + TABLE_HISTORY + " " +
|
2012-10-19 17:35:37 -07:00
|
|
|
"WHERE MAX(" + History.DATE_LAST_VISITED + ", " + History.DATE_MODIFIED +") < " + keepAfter + " " +
|
2012-11-07 09:39:55 -08:00
|
|
|
" AND " + History._ID + " IN ( SELECT " +
|
2012-10-19 17:35:37 -07:00
|
|
|
History._ID + " FROM " + TABLE_HISTORY + " " +
|
|
|
|
"ORDER BY " + sortOrder + " LIMIT " + toRemove +
|
|
|
|
")";
|
|
|
|
} else {
|
|
|
|
sql = "DELETE FROM " + TABLE_HISTORY + " WHERE " + History._ID + " " +
|
|
|
|
"IN ( SELECT " + History._ID + " FROM " + TABLE_HISTORY + " " +
|
|
|
|
"ORDER BY " + sortOrder + " LIMIT " + toRemove + ")";
|
|
|
|
}
|
|
|
|
trace("Deleting using query: " + sql);
|
2014-02-25 12:27:54 -08:00
|
|
|
|
|
|
|
beginWrite(db);
|
2012-10-19 17:35:37 -07:00
|
|
|
db.execSQL(sql);
|
|
|
|
}
|
|
|
|
|
2012-11-07 09:39:55 -08:00
|
|
|
/**
|
|
|
|
* Remove any thumbnails that for sites that aren't likely to be ever shown.
|
2012-12-28 13:46:12 -08:00
|
|
|
* Items will be removed according to a frecency calculation and only if they are not pinned
|
2013-04-09 17:10:48 -07:00
|
|
|
*
|
2012-11-07 09:39:55 -08:00
|
|
|
* Call this method within a transaction.
|
|
|
|
*/
|
2012-11-13 10:56:58 -08:00
|
|
|
private void expireThumbnails(final SQLiteDatabase db) {
|
2013-10-31 10:35:16 -07:00
|
|
|
Log.d(LOGTAG, "Expiring thumbnails.");
|
2012-11-07 09:39:55 -08:00
|
|
|
final String sortOrder = BrowserContract.getFrecencySortOrder(true, false);
|
2012-11-13 10:56:58 -08:00
|
|
|
final String sql = "DELETE FROM " + TABLE_THUMBNAILS +
|
|
|
|
" WHERE " + Thumbnails.URL + " NOT IN ( " +
|
2012-11-07 09:39:55 -08:00
|
|
|
" SELECT " + Combined.URL +
|
2014-01-20 07:38:18 -08:00
|
|
|
" FROM " + Combined.VIEW_NAME +
|
2012-11-07 09:39:55 -08:00
|
|
|
" ORDER BY " + sortOrder +
|
|
|
|
" LIMIT " + DEFAULT_EXPIRY_THUMBNAIL_COUNT +
|
2012-12-28 13:46:12 -08:00
|
|
|
") AND " + Thumbnails.URL + " NOT IN ( " +
|
|
|
|
" SELECT " + Bookmarks.URL +
|
|
|
|
" FROM " + TABLE_BOOKMARKS +
|
|
|
|
" WHERE " + Bookmarks.PARENT + " = " + Bookmarks.FIXED_PINNED_LIST_ID +
|
2012-11-07 09:39:55 -08:00
|
|
|
")";
|
|
|
|
trace("Clear thumbs using query: " + sql);
|
|
|
|
db.execSQL(sql);
|
|
|
|
}
|
2012-04-09 17:28:59 -07:00
|
|
|
|
|
|
|
private boolean shouldIncrementVisits(Uri uri) {
|
|
|
|
String incrementVisits = uri.getQueryParameter(BrowserContract.PARAM_INCREMENT_VISITS);
|
|
|
|
return Boolean.parseBoolean(incrementVisits);
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
@Override
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
final int match = URI_MATCHER.match(uri);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Getting URI type: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
switch (match) {
|
|
|
|
case BOOKMARKS:
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("URI is BOOKMARKS: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
return Bookmarks.CONTENT_TYPE;
|
|
|
|
case BOOKMARKS_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("URI is BOOKMARKS_ID: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
return Bookmarks.CONTENT_ITEM_TYPE;
|
|
|
|
case HISTORY:
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("URI is HISTORY: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
return History.CONTENT_TYPE;
|
|
|
|
case HISTORY_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("URI is HISTORY_ID: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
return History.CONTENT_ITEM_TYPE;
|
2012-10-04 16:57:39 -07:00
|
|
|
case SEARCH_SUGGEST:
|
|
|
|
trace("URI is SEARCH_SUGGEST: " + uri);
|
|
|
|
return SearchManager.SUGGEST_MIME_TYPE;
|
2014-02-12 11:50:35 -08:00
|
|
|
case FLAGS:
|
|
|
|
trace("URI is FLAGS.");
|
|
|
|
return Bookmarks.CONTENT_ITEM_TYPE;
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("URI has unrecognized type: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-12 06:02:46 -08:00
|
|
|
@SuppressWarnings("fallthrough")
|
2014-01-20 07:38:18 -08:00
|
|
|
@Override
|
|
|
|
public int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
|
2014-02-12 06:02:46 -08:00
|
|
|
trace("Calling delete in transaction on URI: " + uri);
|
2014-01-20 07:38:18 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
final int match = URI_MATCHER.match(uri);
|
|
|
|
int deleted = 0;
|
|
|
|
|
|
|
|
switch (match) {
|
|
|
|
case BOOKMARKS_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Delete on BOOKMARKS_ID: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_BOOKMARKS + "._id = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-08 08:37:21 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case BOOKMARKS: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Deleting bookmarks: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
deleted = deleteBookmarks(uri, selection, selectionArgs);
|
|
|
|
deleteUnusedImages(uri);
|
|
|
|
break;
|
2011-12-13 06:46:58 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
case HISTORY_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Delete on HISTORY_ID: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_HISTORY + "._id = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-13 06:46:58 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case HISTORY: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Deleting history: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2011-12-13 06:46:58 -08:00
|
|
|
deleted = deleteHistory(uri, selection, selectionArgs);
|
|
|
|
deleteUnusedImages(uri);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-19 17:35:37 -07:00
|
|
|
case HISTORY_OLD: {
|
|
|
|
String priority = uri.getQueryParameter(BrowserContract.PARAM_EXPIRE_PRIORITY);
|
|
|
|
long keepAfter = System.currentTimeMillis() - DEFAULT_EXPIRY_PRESERVE_WINDOW;
|
|
|
|
int retainCount = DEFAULT_EXPIRY_RETAIN_COUNT;
|
|
|
|
|
|
|
|
if (BrowserContract.ExpirePriority.AGGRESSIVE.toString().equals(priority)) {
|
|
|
|
keepAfter = 0;
|
|
|
|
retainCount = AGGRESSIVE_EXPIRY_RETAIN_COUNT;
|
|
|
|
}
|
|
|
|
expireHistory(db, retainCount, keepAfter);
|
2012-11-07 09:39:55 -08:00
|
|
|
expireThumbnails(db);
|
|
|
|
deleteUnusedImages(uri);
|
2012-10-19 17:35:37 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
case FAVICON_ID:
|
|
|
|
debug("Delete on FAVICON_ID: " + uri);
|
|
|
|
|
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_FAVICONS + "._id = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case FAVICONS: {
|
|
|
|
trace("Deleting favicons: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2012-11-13 10:56:58 -08:00
|
|
|
deleted = deleteFavicons(uri, selection, selectionArgs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case THUMBNAIL_ID:
|
|
|
|
debug("Delete on THUMBNAIL_ID: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_THUMBNAILS + "._id = ?");
|
2011-12-16 15:11:09 -08:00
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-13 06:46:58 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
2012-11-13 10:56:58 -08:00
|
|
|
case THUMBNAILS: {
|
|
|
|
trace("Deleting thumbnails: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2012-11-13 10:56:58 -08:00
|
|
|
deleted = deleteThumbnails(uri, selection, selectionArgs);
|
2011-12-13 06:46:58 -08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationException("Unknown delete URI " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Deleted " + deleted + " rows for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
return deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Uri insertInTransaction(Uri uri, ContentValues values) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Calling insert in transaction on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
int match = URI_MATCHER.match(uri);
|
|
|
|
long id = -1;
|
|
|
|
|
|
|
|
switch (match) {
|
|
|
|
case BOOKMARKS: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Insert on BOOKMARKS: " + uri);
|
2012-06-25 15:30:36 -07:00
|
|
|
id = insertBookmark(uri, values);
|
2011-12-08 08:37:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case HISTORY: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Insert on HISTORY: " + uri);
|
2012-04-09 17:28:59 -07:00
|
|
|
id = insertHistory(uri, values);
|
2011-12-08 08:37:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
case FAVICONS: {
|
|
|
|
trace("Insert on FAVICONS: " + uri);
|
|
|
|
id = insertFavicon(uri, values);
|
|
|
|
break;
|
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
case THUMBNAILS: {
|
|
|
|
trace("Insert on THUMBNAILS: " + uri);
|
|
|
|
id = insertThumbnail(uri, values);
|
2011-12-13 06:46:58 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationException("Unknown insert URI " + uri);
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserted ID in database: " + id);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
if (id >= 0)
|
|
|
|
return ContentUris.withAppendedId(uri, id);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-12 06:02:46 -08:00
|
|
|
@SuppressWarnings("fallthrough")
|
2014-01-20 07:38:18 -08:00
|
|
|
@Override
|
2011-12-08 08:37:21 -08:00
|
|
|
public int updateInTransaction(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Calling update in transaction on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
int match = URI_MATCHER.match(uri);
|
|
|
|
int updated = 0;
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
switch (match) {
|
2012-02-22 12:37:34 -08:00
|
|
|
// We provide a dedicated (hacky) API for callers to bulk-update the positions of
|
|
|
|
// folder children by passing an array of GUID strings as `selectionArgs`.
|
|
|
|
// Each child will have its position column set to its index in the provided array.
|
|
|
|
//
|
|
|
|
// This avoids callers having to issue a large number of UPDATE queries through
|
|
|
|
// the usual channels. See Bug 728783.
|
|
|
|
//
|
|
|
|
// Note that this is decidedly not a general-purpose API; use at your own risk.
|
|
|
|
// `values` and `selection` are ignored.
|
|
|
|
case BOOKMARKS_POSITIONS: {
|
|
|
|
debug("Update on BOOKMARKS_POSITIONS: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
|
|
|
|
// This already starts and finishes its own transaction.
|
2012-02-22 12:37:34 -08:00
|
|
|
updated = updateBookmarkPositions(uri, selectionArgs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:10:18 -08:00
|
|
|
case BOOKMARKS_PARENT: {
|
|
|
|
debug("Update on BOOKMARKS_PARENT: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
|
|
|
updated = updateBookmarkParents(db, values, selection, selectionArgs);
|
2012-02-14 17:10:18 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
case BOOKMARKS_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Update on BOOKMARKS_ID: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_BOOKMARKS + "._id = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-08 08:37:21 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case BOOKMARKS: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Updating bookmark: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
if (shouldUpdateOrInsert(uri)) {
|
2012-06-25 15:30:36 -07:00
|
|
|
updated = updateOrInsertBookmark(uri, values, selection, selectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
} else {
|
2012-06-25 15:30:36 -07:00
|
|
|
updated = updateBookmarks(uri, values, selection, selectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case HISTORY_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Update on HISTORY_ID: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_HISTORY + "._id = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-08 08:37:21 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case HISTORY: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Updating history: " + uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
if (shouldUpdateOrInsert(uri)) {
|
2012-04-09 17:28:59 -07:00
|
|
|
updated = updateOrInsertHistory(uri, values, selection, selectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
} else {
|
2012-04-09 17:28:59 -07:00
|
|
|
updated = updateHistory(uri, values, selection, selectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
case FAVICONS: {
|
|
|
|
debug("Update on FAVICONS: " + uri);
|
|
|
|
|
|
|
|
String url = values.getAsString(Favicons.URL);
|
|
|
|
String faviconSelection = null;
|
|
|
|
String[] faviconSelectionArgs = null;
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(url)) {
|
|
|
|
faviconSelection = Favicons.URL + " = ?";
|
|
|
|
faviconSelectionArgs = new String[] { url };
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
if (shouldUpdateOrInsert(uri)) {
|
2012-11-13 10:56:58 -08:00
|
|
|
updated = updateOrInsertFavicon(uri, values, faviconSelection, faviconSelectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
} else {
|
2012-11-13 10:56:58 -08:00
|
|
|
updated = updateExistingFavicon(uri, values, faviconSelection, faviconSelectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
}
|
2012-11-13 10:56:58 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case THUMBNAILS: {
|
|
|
|
debug("Update on THUMBNAILS: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
String url = values.getAsString(Thumbnails.URL);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-10-16 07:07:06 -07:00
|
|
|
// if no URL is provided, update all of the entries
|
2014-02-25 12:27:54 -08:00
|
|
|
if (TextUtils.isEmpty(values.getAsString(Thumbnails.URL))) {
|
2012-11-13 10:56:58 -08:00
|
|
|
updated = updateExistingThumbnail(uri, values, null, null);
|
2014-02-25 12:27:54 -08:00
|
|
|
} else if (shouldUpdateOrInsert(uri)) {
|
2012-11-13 10:56:58 -08:00
|
|
|
updated = updateOrInsertThumbnail(uri, values, Thumbnails.URL + " = ?",
|
|
|
|
new String[] { url });
|
2014-02-25 12:27:54 -08:00
|
|
|
} else {
|
2012-11-13 10:56:58 -08:00
|
|
|
updated = updateExistingThumbnail(uri, values, Thumbnails.URL + " = ?",
|
|
|
|
new String[] { url });
|
2014-02-25 12:27:54 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationException("Unknown update URI " + uri);
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Updated " + updated + " rows for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection,
|
|
|
|
String[] selectionArgs, String sortOrder) {
|
|
|
|
SQLiteDatabase db = getReadableDatabase(uri);
|
|
|
|
final int match = URI_MATCHER.match(uri);
|
|
|
|
|
2014-02-12 11:50:35 -08:00
|
|
|
// The first selectionArgs value is the URI for which to query.
|
|
|
|
if (match == FLAGS) {
|
|
|
|
// We don't need the QB below for this.
|
|
|
|
//
|
|
|
|
// There are three possible kinds of bookmarks:
|
|
|
|
// * Regular bookmarks
|
|
|
|
// * Bookmarks whose parent is FIXED_READING_LIST_ID (reading list items)
|
|
|
|
// * Bookmarks whose parent is FIXED_PINNED_LIST_ID (pinned items).
|
|
|
|
//
|
|
|
|
// Although SQLite doesn't have an aggregate operator for bitwise-OR, we're
|
|
|
|
// using disjoint flags, so we can simply use SUM and DISTINCT to get the
|
|
|
|
// flags we need.
|
|
|
|
// We turn parents into flags according to the three kinds, above.
|
|
|
|
//
|
|
|
|
// When this query is extended to support queries across multiple tables, simply
|
|
|
|
// extend it to look like
|
|
|
|
//
|
|
|
|
// SELECT COALESCE((SELECT ...), 0) | COALESCE(...) | ...
|
2014-02-19 10:53:24 -08:00
|
|
|
|
|
|
|
final boolean includeDeleted = shouldShowDeleted(uri);
|
2014-02-12 11:50:35 -08:00
|
|
|
final String query = "SELECT COALESCE(SUM(flag), 0) AS flags " +
|
|
|
|
"FROM ( SELECT DISTINCT CASE" +
|
|
|
|
" WHEN " + Bookmarks.PARENT + " = " + Bookmarks.FIXED_READING_LIST_ID +
|
|
|
|
" THEN " + Bookmarks.FLAG_READING +
|
|
|
|
|
|
|
|
" WHEN " + Bookmarks.PARENT + " = " + Bookmarks.FIXED_PINNED_LIST_ID +
|
|
|
|
" THEN " + Bookmarks.FLAG_PINNED +
|
|
|
|
|
|
|
|
" ELSE " + Bookmarks.FLAG_BOOKMARK +
|
|
|
|
" END flag " +
|
2014-02-19 10:53:24 -08:00
|
|
|
"FROM " + TABLE_BOOKMARKS + " WHERE " +
|
|
|
|
Bookmarks.URL + " = ? " +
|
|
|
|
(includeDeleted ? "" : ("AND " + Bookmarks.IS_DELETED + " = 0")) +
|
2014-02-12 11:50:35 -08:00
|
|
|
")";
|
|
|
|
|
|
|
|
return db.rawQuery(query, selectionArgs);
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
|
|
|
String limit = uri.getQueryParameter(BrowserContract.PARAM_LIMIT);
|
2012-09-10 10:24:25 -07:00
|
|
|
String groupBy = null;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
switch (match) {
|
|
|
|
case BOOKMARKS_FOLDER_ID:
|
|
|
|
case BOOKMARKS_ID:
|
|
|
|
case BOOKMARKS: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Query is on bookmarks: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
if (match == BOOKMARKS_ID) {
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, Bookmarks._ID + " = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-08 08:37:21 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
} else if (match == BOOKMARKS_FOLDER_ID) {
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, Bookmarks.PARENT + " = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-08 08:37:21 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
}
|
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
if (!shouldShowDeleted(uri))
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(Bookmarks.IS_DELETED + " = 0", selection);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
if (TextUtils.isEmpty(sortOrder)) {
|
|
|
|
sortOrder = DEFAULT_BOOKMARKS_SORT_ORDER;
|
2012-01-02 08:37:14 -08:00
|
|
|
} else {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Using sort order " + sortOrder + ".");
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
qb.setProjectionMap(BOOKMARKS_PROJECTION_MAP);
|
2011-12-13 11:47:40 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
if (hasFaviconsInProjection(projection))
|
|
|
|
qb.setTables(VIEW_BOOKMARKS_WITH_FAVICONS);
|
2011-12-13 11:47:40 -08:00
|
|
|
else
|
|
|
|
qb.setTables(TABLE_BOOKMARKS);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case HISTORY_ID:
|
2012-11-13 10:56:58 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, History._ID + " = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
2011-12-08 08:37:21 -08:00
|
|
|
case HISTORY: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Query is on history: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
if (!shouldShowDeleted(uri))
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(History.IS_DELETED + " = 0", selection);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
if (TextUtils.isEmpty(sortOrder))
|
|
|
|
sortOrder = DEFAULT_HISTORY_SORT_ORDER;
|
|
|
|
|
|
|
|
qb.setProjectionMap(HISTORY_PROJECTION_MAP);
|
2011-12-13 11:47:40 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
if (hasFaviconsInProjection(projection))
|
|
|
|
qb.setTables(VIEW_HISTORY_WITH_FAVICONS);
|
2011-12-13 11:47:40 -08:00
|
|
|
else
|
|
|
|
qb.setTables(TABLE_HISTORY);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
case FAVICON_ID:
|
|
|
|
selection = DBUtils.concatenateWhere(selection, Favicons._ID + " = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case FAVICONS: {
|
|
|
|
debug("Query is on favicons: " + uri);
|
|
|
|
|
|
|
|
qb.setProjectionMap(FAVICONS_PROJECTION_MAP);
|
|
|
|
qb.setTables(TABLE_FAVICONS);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
case THUMBNAIL_ID:
|
|
|
|
selection = DBUtils.concatenateWhere(selection, Thumbnails._ID + " = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case THUMBNAILS: {
|
|
|
|
debug("Query is on thumbnails: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
qb.setProjectionMap(THUMBNAILS_PROJECTION_MAP);
|
|
|
|
qb.setTables(TABLE_THUMBNAILS);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-13 06:12:01 -08:00
|
|
|
case SCHEMA: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Query is on schema.");
|
2011-12-13 06:12:01 -08:00
|
|
|
MatrixCursor schemaCursor = new MatrixCursor(new String[] { Schema.VERSION });
|
2014-01-20 07:38:18 -08:00
|
|
|
schemaCursor.newRow().add(BrowserDatabaseHelper.DATABASE_VERSION);
|
2011-12-13 06:12:01 -08:00
|
|
|
|
|
|
|
return schemaCursor;
|
|
|
|
}
|
|
|
|
|
2012-03-23 15:52:42 -07:00
|
|
|
case COMBINED: {
|
|
|
|
debug("Query is on combined: " + uri);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(sortOrder))
|
|
|
|
sortOrder = DEFAULT_HISTORY_SORT_ORDER;
|
|
|
|
|
2012-09-10 10:24:25 -07:00
|
|
|
// This will avoid duplicate entries in the awesomebar
|
|
|
|
// results when a history entry has multiple bookmarks.
|
|
|
|
groupBy = Combined.URL;
|
|
|
|
|
2012-03-23 15:52:42 -07:00
|
|
|
qb.setProjectionMap(COMBINED_PROJECTION_MAP);
|
2012-10-31 05:34:31 -07:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
if (hasFaviconsInProjection(projection))
|
2014-02-10 15:52:52 -08:00
|
|
|
qb.setTables(VIEW_COMBINED_WITH_FAVICONS);
|
2012-10-31 05:34:31 -07:00
|
|
|
else
|
2014-01-20 07:38:18 -08:00
|
|
|
qb.setTables(Combined.VIEW_NAME);
|
2012-03-23 15:52:42 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-04 16:57:39 -07:00
|
|
|
case SEARCH_SUGGEST: {
|
|
|
|
debug("Query is on search suggest: " + uri);
|
|
|
|
selection = DBUtils.concatenateWhere(selection, "(" + Combined.URL + " LIKE ? OR " +
|
|
|
|
Combined.TITLE + " LIKE ?)");
|
|
|
|
|
|
|
|
String keyword = uri.getLastPathSegment();
|
|
|
|
if (keyword == null)
|
|
|
|
keyword = "";
|
|
|
|
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
|
|
|
new String[] { "%" + keyword + "%",
|
|
|
|
"%" + keyword + "%" });
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(sortOrder))
|
|
|
|
sortOrder = DEFAULT_HISTORY_SORT_ORDER;
|
|
|
|
|
|
|
|
qb.setProjectionMap(SEARCH_SUGGEST_PROJECTION_MAP);
|
2014-02-10 15:52:52 -08:00
|
|
|
qb.setTables(VIEW_COMBINED_WITH_FAVICONS);
|
2012-10-04 16:57:39 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationException("Unknown query URI " + uri);
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Running built query.");
|
2012-09-10 10:24:25 -07:00
|
|
|
Cursor cursor = qb.query(db, projection, selection, selectionArgs, groupBy,
|
2011-12-08 08:37:21 -08:00
|
|
|
null, sortOrder, limit);
|
|
|
|
cursor.setNotificationUri(getContext().getContentResolver(),
|
|
|
|
BrowserContract.AUTHORITY_URI);
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
private static int getUrlCount(SQLiteDatabase db, String table, String url) {
|
|
|
|
final Cursor c = db.query(table, new String[] { "COUNT(*)" },
|
|
|
|
URLColumns.URL + " = ?", new String[] { url },
|
|
|
|
null, null, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
try {
|
2014-02-25 12:27:53 -08:00
|
|
|
if (c.moveToFirst()) {
|
|
|
|
return c.getInt(0);
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
return 0;
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-22 12:37:34 -08:00
|
|
|
/**
|
2012-04-05 19:25:34 -07:00
|
|
|
* Update the positions of bookmarks in batches.
|
2012-02-22 12:37:34 -08:00
|
|
|
*
|
2014-02-25 12:27:54 -08:00
|
|
|
* Begins and ends its own transactions.
|
|
|
|
*
|
2012-04-05 19:25:34 -07:00
|
|
|
* @see #updateBookmarkPositionsInTransaction(SQLiteDatabase, String[], int, int)
|
2012-02-22 12:37:34 -08:00
|
|
|
*/
|
|
|
|
int updateBookmarkPositions(Uri uri, String[] guids) {
|
2014-02-25 12:27:53 -08:00
|
|
|
if (guids == null) {
|
2012-02-22 12:37:34 -08:00
|
|
|
return 0;
|
2014-02-25 12:27:53 -08:00
|
|
|
}
|
2012-02-22 12:37:34 -08:00
|
|
|
|
|
|
|
int guidsCount = guids.length;
|
2014-02-25 12:27:53 -08:00
|
|
|
if (guidsCount == 0) {
|
2012-02-22 12:37:34 -08:00
|
|
|
return 0;
|
2014-02-25 12:27:53 -08:00
|
|
|
}
|
2012-02-22 12:37:34 -08:00
|
|
|
|
2012-04-05 19:25:34 -07:00
|
|
|
int offset = 0;
|
|
|
|
int updated = 0;
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2012-04-05 19:25:34 -07:00
|
|
|
db.beginTransaction();
|
2014-02-25 12:27:54 -08:00
|
|
|
|
2012-04-05 19:25:34 -07:00
|
|
|
while (offset < guidsCount) {
|
|
|
|
try {
|
|
|
|
updated += updateBookmarkPositionsInTransaction(db, guids, offset,
|
|
|
|
MAX_POSITION_UPDATES_PER_QUERY);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
Log.e(LOGTAG, "Got SQLite exception updating bookmark positions at offset " + offset, e);
|
|
|
|
|
|
|
|
// Need to restart the transaction.
|
|
|
|
// The only way a caller knows that anything failed is that the
|
|
|
|
// returned update count will be smaller than the requested
|
|
|
|
// number of records.
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
db.endTransaction();
|
|
|
|
|
|
|
|
db.beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += MAX_POSITION_UPDATES_PER_QUERY;
|
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
db.endTransaction();
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct and execute an update expression that will modify the positions
|
|
|
|
* of records in-place.
|
|
|
|
*/
|
2014-02-25 12:27:54 -08:00
|
|
|
private static int updateBookmarkPositionsInTransaction(final SQLiteDatabase db, final String[] guids,
|
|
|
|
final int offset, final int max) {
|
2012-04-05 19:25:34 -07:00
|
|
|
int guidsCount = guids.length;
|
|
|
|
int processCount = Math.min(max, guidsCount - offset);
|
|
|
|
|
2012-02-22 12:37:34 -08:00
|
|
|
// Each must appear twice: once in a CASE, and once in the IN clause.
|
2012-04-05 19:25:34 -07:00
|
|
|
String[] args = new String[processCount * 2];
|
|
|
|
System.arraycopy(guids, offset, args, 0, processCount);
|
|
|
|
System.arraycopy(guids, offset, args, processCount, processCount);
|
2012-02-22 12:37:34 -08:00
|
|
|
|
|
|
|
StringBuilder b = new StringBuilder("UPDATE " + TABLE_BOOKMARKS +
|
2012-04-05 19:25:34 -07:00
|
|
|
" SET " + Bookmarks.POSITION +
|
|
|
|
" = CASE guid");
|
|
|
|
|
|
|
|
// Build the CASE statement body for GUID/index pairs from offset up to
|
|
|
|
// the computed limit.
|
|
|
|
final int end = offset + processCount;
|
|
|
|
int i = offset;
|
|
|
|
for (; i < end; ++i) {
|
2012-02-22 12:37:34 -08:00
|
|
|
if (guids[i] == null) {
|
|
|
|
// We don't want to issue the query if not every GUID is specified.
|
|
|
|
debug("updateBookmarkPositions called with null GUID at index " + i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
b.append(" WHEN ? THEN " + i);
|
|
|
|
}
|
2012-04-05 19:25:34 -07:00
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
// TODO: use computeSQLInClause
|
2012-02-22 12:37:34 -08:00
|
|
|
b.append(" END WHERE " + Bookmarks.GUID + " IN (");
|
|
|
|
i = 1;
|
2012-04-05 19:25:34 -07:00
|
|
|
while (i++ < processCount) {
|
2012-02-22 12:37:34 -08:00
|
|
|
b.append("?, ");
|
|
|
|
}
|
|
|
|
b.append("?)");
|
2012-04-05 19:25:34 -07:00
|
|
|
db.execSQL(b.toString(), args);
|
2012-02-22 12:37:34 -08:00
|
|
|
|
|
|
|
// We can't easily get a modified count without calling something like changes().
|
2012-04-05 19:25:34 -07:00
|
|
|
return processCount;
|
2012-02-22 12:37:34 -08:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:10:18 -08:00
|
|
|
/**
|
|
|
|
* Construct an update expression that will modify the parents of any records
|
|
|
|
* that match.
|
|
|
|
*/
|
2014-02-25 12:27:54 -08:00
|
|
|
private int updateBookmarkParents(SQLiteDatabase db, ContentValues values, String selection, String[] selectionArgs) {
|
2012-02-14 17:10:18 -08:00
|
|
|
trace("Updating bookmark parents of " + selection + " (" + selectionArgs[0] + ")");
|
|
|
|
String where = Bookmarks._ID + " IN (" +
|
|
|
|
" SELECT DISTINCT " + Bookmarks.PARENT +
|
|
|
|
" FROM " + TABLE_BOOKMARKS +
|
|
|
|
" WHERE " + selection + " )";
|
2014-02-25 12:27:54 -08:00
|
|
|
return db.update(TABLE_BOOKMARKS, values, where, selectionArgs);
|
2012-02-14 17:10:18 -08:00
|
|
|
}
|
|
|
|
|
2012-06-25 15:30:36 -07:00
|
|
|
long insertBookmark(Uri uri, ContentValues values) {
|
|
|
|
// Generate values if not specified. Don't overwrite
|
|
|
|
// if specified by caller.
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
if (!values.containsKey(Bookmarks.DATE_CREATED)) {
|
|
|
|
values.put(Bookmarks.DATE_CREATED, now);
|
|
|
|
}
|
|
|
|
|
2012-10-20 09:09:45 -07:00
|
|
|
if (!values.containsKey(Bookmarks.DATE_MODIFIED)) {
|
|
|
|
values.put(Bookmarks.DATE_MODIFIED, now);
|
|
|
|
}
|
|
|
|
|
2012-06-25 15:30:36 -07:00
|
|
|
if (!values.containsKey(Bookmarks.GUID)) {
|
|
|
|
values.put(Bookmarks.GUID, Utils.generateGuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!values.containsKey(Bookmarks.POSITION)) {
|
|
|
|
debug("Inserting bookmark with no position for URI");
|
|
|
|
values.put(Bookmarks.POSITION,
|
|
|
|
Long.toString(BrowserContract.Bookmarks.DEFAULT_POSITION));
|
|
|
|
}
|
|
|
|
|
|
|
|
String url = values.getAsString(Bookmarks.URL);
|
|
|
|
|
|
|
|
debug("Inserting bookmark in database with URL: " + url);
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2012-06-25 15:30:36 -07:00
|
|
|
return db.insertOrThrow(TABLE_BOOKMARKS, Bookmarks.TITLE, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int updateOrInsertBookmark(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
|
|
|
int updated = updateBookmarks(uri, values, selection, selectionArgs);
|
2014-02-25 12:27:54 -08:00
|
|
|
if (updated > 0) {
|
2012-06-25 15:30:36 -07:00
|
|
|
return updated;
|
2014-02-25 12:27:54 -08:00
|
|
|
}
|
2012-06-25 15:30:36 -07:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
// Transaction already begun by updateBookmarks.
|
2013-10-31 10:35:17 -07:00
|
|
|
if (0 <= insertBookmark(uri, values)) {
|
|
|
|
// We 'updated' one row.
|
|
|
|
return 1;
|
|
|
|
}
|
2012-06-25 15:30:36 -07:00
|
|
|
|
2013-10-31 10:35:17 -07:00
|
|
|
// If something went wrong, then we updated zero rows.
|
2012-06-25 15:30:36 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
int updateBookmarks(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating bookmarks on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final String[] bookmarksProjection = new String[] {
|
|
|
|
Bookmarks._ID, // 0
|
|
|
|
};
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
if (!values.containsKey(Bookmarks.DATE_MODIFIED)) {
|
|
|
|
values.put(Bookmarks.DATE_MODIFIED, System.currentTimeMillis());
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
trace("Querying bookmarks to update on URI: " + uri);
|
2014-02-25 12:27:53 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
|
|
|
|
// Compute matching IDs.
|
2014-02-25 12:27:53 -08:00
|
|
|
final Cursor cursor = db.query(TABLE_BOOKMARKS, bookmarksProjection,
|
|
|
|
selection, selectionArgs, null, null, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
// Now that we're done reading, open a transaction.
|
|
|
|
final String inClause;
|
2011-12-08 08:37:21 -08:00
|
|
|
try {
|
2014-02-25 12:27:54 -08:00
|
|
|
inClause = computeSQLInClauseFromLongs(cursor, Bookmarks._ID);
|
2011-12-08 08:37:21 -08:00
|
|
|
} finally {
|
2014-02-25 12:27:53 -08:00
|
|
|
cursor.close();
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
|
|
|
return db.update(TABLE_BOOKMARKS, values, inClause, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-04-09 17:28:59 -07:00
|
|
|
long insertHistory(Uri uri, ContentValues values) {
|
2014-02-25 12:27:53 -08:00
|
|
|
final long now = System.currentTimeMillis();
|
2012-04-09 17:28:59 -07:00
|
|
|
values.put(History.DATE_CREATED, now);
|
|
|
|
values.put(History.DATE_MODIFIED, now);
|
|
|
|
|
|
|
|
// Generate GUID for new history entry. Don't override specified GUIDs.
|
|
|
|
if (!values.containsKey(History.GUID)) {
|
|
|
|
values.put(History.GUID, Utils.generateGuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
String url = values.getAsString(History.URL);
|
|
|
|
|
|
|
|
debug("Inserting history in database with URL: " + url);
|
2014-02-25 12:27:53 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2012-04-09 17:28:59 -07:00
|
|
|
return db.insertOrThrow(TABLE_HISTORY, History.VISITS, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
int updateOrInsertHistory(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
2014-02-25 12:27:53 -08:00
|
|
|
final int updated = updateHistory(uri, values, selection, selectionArgs);
|
|
|
|
if (updated > 0) {
|
2012-04-09 17:28:59 -07:00
|
|
|
return updated;
|
2014-02-25 12:27:53 -08:00
|
|
|
}
|
2012-04-09 17:28:59 -07:00
|
|
|
|
|
|
|
// Insert a new entry if necessary
|
2014-02-25 12:27:53 -08:00
|
|
|
if (!values.containsKey(History.VISITS)) {
|
2012-04-09 17:28:59 -07:00
|
|
|
values.put(History.VISITS, 1);
|
2014-02-25 12:27:53 -08:00
|
|
|
}
|
|
|
|
if (!values.containsKey(History.TITLE)) {
|
2012-04-09 17:28:59 -07:00
|
|
|
values.put(History.TITLE, values.getAsString(History.URL));
|
2014-02-25 12:27:53 -08:00
|
|
|
}
|
2012-04-09 17:28:59 -07:00
|
|
|
|
2013-10-31 10:35:17 -07:00
|
|
|
if (0 <= insertHistory(uri, values)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2012-04-09 17:28:59 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
int updateHistory(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating history on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
int updated = 0;
|
|
|
|
|
2012-04-09 17:28:59 -07:00
|
|
|
final String[] historyProjection = new String[] {
|
|
|
|
History._ID, // 0
|
|
|
|
History.URL, // 1
|
|
|
|
History.VISITS // 2
|
2011-12-08 08:37:21 -08:00
|
|
|
};
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
final Cursor cursor = db.query(TABLE_HISTORY, historyProjection, selection,
|
|
|
|
selectionArgs, null, null, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
try {
|
2012-10-19 17:35:45 -07:00
|
|
|
if (!values.containsKey(Bookmarks.DATE_MODIFIED)) {
|
|
|
|
values.put(Bookmarks.DATE_MODIFIED, System.currentTimeMillis());
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
long id = cursor.getLong(0);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating history entry with ID: " + id);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-04-09 17:28:59 -07:00
|
|
|
if (shouldIncrementVisits(uri)) {
|
|
|
|
long existing = cursor.getLong(2);
|
|
|
|
Long additional = values.getAsLong(History.VISITS);
|
|
|
|
|
|
|
|
// Increment visit count by a specified amount, or default to increment by 1
|
|
|
|
values.put(History.VISITS, existing + ((additional != null) ? additional.longValue() : 1));
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
updated += db.update(TABLE_HISTORY, values, "_id = ?",
|
2014-02-25 12:27:53 -08:00
|
|
|
new String[] { Long.toString(id) });
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
} finally {
|
2014-02-25 12:27:53 -08:00
|
|
|
cursor.close();
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
2012-11-19 10:39:43 -08:00
|
|
|
private void updateFaviconIdsForUrl(SQLiteDatabase db, String pageUrl, Long faviconId) {
|
2012-11-13 10:56:58 -08:00
|
|
|
ContentValues updateValues = new ContentValues(1);
|
|
|
|
updateValues.put(FaviconColumns.FAVICON_ID, faviconId);
|
|
|
|
db.update(TABLE_HISTORY,
|
|
|
|
updateValues,
|
|
|
|
History.URL + " = ?",
|
|
|
|
new String[] { pageUrl });
|
|
|
|
db.update(TABLE_BOOKMARKS,
|
|
|
|
updateValues,
|
|
|
|
Bookmarks.URL + " = ?",
|
|
|
|
new String[] { pageUrl });
|
|
|
|
}
|
|
|
|
|
|
|
|
long insertFavicon(Uri uri, ContentValues values) {
|
|
|
|
return insertFavicon(getWritableDatabase(uri), values);
|
|
|
|
}
|
|
|
|
|
|
|
|
long insertFavicon(SQLiteDatabase db, ContentValues values) {
|
2014-01-20 07:38:18 -08:00
|
|
|
// This method is a dupicate of BrowserDatabaseHelper.insertFavicon.
|
|
|
|
// If changes are needed, please update both
|
2012-11-13 10:56:58 -08:00
|
|
|
String faviconUrl = values.getAsString(Favicons.URL);
|
|
|
|
String pageUrl = null;
|
|
|
|
|
|
|
|
trace("Inserting favicon for URL: " + faviconUrl);
|
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
DBUtils.stripEmptyByteArray(values, Favicons.DATA);
|
2013-07-24 22:08:42 -07:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
// Extract the page URL from the ContentValues
|
|
|
|
if (values.containsKey(Favicons.PAGE_URL)) {
|
|
|
|
pageUrl = values.getAsString(Favicons.PAGE_URL);
|
|
|
|
values.remove(Favicons.PAGE_URL);
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:42:39 -07:00
|
|
|
// If no URL is provided, insert using the default one.
|
|
|
|
if (TextUtils.isEmpty(faviconUrl) && !TextUtils.isEmpty(pageUrl)) {
|
2014-01-13 15:08:58 -08:00
|
|
|
values.put(Favicons.URL, org.mozilla.gecko.favicons.Favicons.guessDefaultFaviconURL(pageUrl));
|
2013-10-11 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
final long now = System.currentTimeMillis();
|
2012-11-13 10:56:58 -08:00
|
|
|
values.put(Favicons.DATE_CREATED, now);
|
|
|
|
values.put(Favicons.DATE_MODIFIED, now);
|
2014-02-25 12:27:54 -08:00
|
|
|
|
|
|
|
beginWrite(db);
|
|
|
|
final long faviconId = db.insertOrThrow(TABLE_FAVICONS, null, values);
|
2012-11-13 10:56:58 -08:00
|
|
|
|
|
|
|
if (pageUrl != null) {
|
|
|
|
updateFaviconIdsForUrl(db, pageUrl, faviconId);
|
|
|
|
}
|
|
|
|
return faviconId;
|
|
|
|
}
|
|
|
|
|
|
|
|
int updateOrInsertFavicon(Uri uri, ContentValues values, String selection,
|
2011-12-08 08:37:21 -08:00
|
|
|
String[] selectionArgs) {
|
2012-11-13 10:56:58 -08:00
|
|
|
return updateFavicon(uri, values, selection, selectionArgs,
|
2011-12-13 06:46:58 -08:00
|
|
|
true /* insert if needed */);
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
int updateExistingFavicon(Uri uri, ContentValues values, String selection,
|
2011-12-13 06:46:58 -08:00
|
|
|
String[] selectionArgs) {
|
2012-11-13 10:56:58 -08:00
|
|
|
return updateFavicon(uri, values, selection, selectionArgs,
|
2011-12-13 06:46:58 -08:00
|
|
|
false /* only update, no insert */);
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
int updateFavicon(Uri uri, ContentValues values, String selection,
|
2011-12-13 06:46:58 -08:00
|
|
|
String[] selectionArgs, boolean insertIfNeeded) {
|
2012-11-13 10:56:58 -08:00
|
|
|
String faviconUrl = values.getAsString(Favicons.URL);
|
|
|
|
String pageUrl = null;
|
|
|
|
int updated = 0;
|
|
|
|
Long faviconId = null;
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
|
|
|
trace("Updating favicon for URL: " + faviconUrl);
|
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
DBUtils.stripEmptyByteArray(values, Favicons.DATA);
|
2013-07-24 22:08:42 -07:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
// Extract the page URL from the ContentValues
|
|
|
|
if (values.containsKey(Favicons.PAGE_URL)) {
|
|
|
|
pageUrl = values.getAsString(Favicons.PAGE_URL);
|
|
|
|
values.remove(Favicons.PAGE_URL);
|
|
|
|
}
|
|
|
|
|
|
|
|
values.put(Favicons.DATE_MODIFIED, now);
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
// If there's no favicon URL given and we're inserting if needed, skip
|
|
|
|
// the update and only do an insert (otherwise all rows would be
|
2014-02-25 12:27:53 -08:00
|
|
|
// updated).
|
2012-11-13 10:56:58 -08:00
|
|
|
if (!(insertIfNeeded && (faviconUrl == null))) {
|
|
|
|
updated = db.update(TABLE_FAVICONS, values, selection, selectionArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updated > 0) {
|
|
|
|
if ((faviconUrl != null) && (pageUrl != null)) {
|
2014-02-25 12:27:53 -08:00
|
|
|
final Cursor cursor = db.query(TABLE_FAVICONS,
|
|
|
|
new String[] { Favicons._ID },
|
|
|
|
Favicons.URL + " = ?",
|
|
|
|
new String[] { faviconUrl },
|
|
|
|
null, null, null);
|
2012-11-13 10:56:58 -08:00
|
|
|
try {
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
faviconId = cursor.getLong(cursor.getColumnIndexOrThrow(Favicons._ID));
|
|
|
|
}
|
|
|
|
} finally {
|
2014-02-25 12:27:53 -08:00
|
|
|
cursor.close();
|
2012-11-13 10:56:58 -08:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 12:27:54 -08:00
|
|
|
if (pageUrl != null) {
|
|
|
|
beginWrite(db);
|
|
|
|
}
|
2012-11-13 10:56:58 -08:00
|
|
|
} else if (insertIfNeeded) {
|
|
|
|
values.put(Favicons.DATE_CREATED, now);
|
|
|
|
|
|
|
|
trace("No update, inserting favicon for URL: " + faviconUrl);
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2012-11-13 10:56:58 -08:00
|
|
|
faviconId = db.insert(TABLE_FAVICONS, null, values);
|
|
|
|
updated = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageUrl != null) {
|
|
|
|
updateFaviconIdsForUrl(db, pageUrl, faviconId);
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
return updated;
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
private long insertThumbnail(Uri uri, ContentValues values) {
|
|
|
|
final String url = values.getAsString(Thumbnails.URL);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
trace("Inserting thumbnail for URL: " + url);
|
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
DBUtils.stripEmptyByteArray(values, Thumbnails.DATA);
|
2013-07-24 22:08:42 -07:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
beginWrite(db);
|
2012-11-13 10:56:58 -08:00
|
|
|
return db.insertOrThrow(TABLE_THUMBNAILS, null, values);
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
private int updateOrInsertThumbnail(Uri uri, ContentValues values, String selection,
|
2012-11-13 10:56:58 -08:00
|
|
|
String[] selectionArgs) {
|
|
|
|
return updateThumbnail(uri, values, selection, selectionArgs,
|
|
|
|
true /* insert if needed */);
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
private int updateExistingThumbnail(Uri uri, ContentValues values, String selection,
|
2012-11-13 10:56:58 -08:00
|
|
|
String[] selectionArgs) {
|
|
|
|
return updateThumbnail(uri, values, selection, selectionArgs,
|
|
|
|
false /* only update, no insert */);
|
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
private int updateThumbnail(Uri uri, ContentValues values, String selection,
|
2012-11-13 10:56:58 -08:00
|
|
|
String[] selectionArgs, boolean insertIfNeeded) {
|
2014-02-25 12:27:54 -08:00
|
|
|
final String url = values.getAsString(Thumbnails.URL);
|
2014-01-20 07:38:18 -08:00
|
|
|
DBUtils.stripEmptyByteArray(values, Thumbnails.DATA);
|
2013-07-24 22:08:42 -07:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
trace("Updating thumbnail for URL: " + url);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
beginWrite(db);
|
|
|
|
int updated = db.update(TABLE_THUMBNAILS, values, selection, selectionArgs);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
if (updated == 0 && insertIfNeeded) {
|
|
|
|
trace("No update, inserting thumbnail for URL: " + url);
|
|
|
|
db.insert(TABLE_THUMBNAILS, null, values);
|
2011-12-08 08:37:21 -08:00
|
|
|
updated = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
/**
|
|
|
|
* This method does not create a new transaction. Its first operation is
|
|
|
|
* guaranteed to be a write, which in the case of a new enclosing
|
|
|
|
* transaction will guarantee that a read does not need to be upgraded to
|
|
|
|
* a write.
|
|
|
|
*/
|
2011-12-08 08:37:21 -08:00
|
|
|
int deleteHistory(Uri uri, String selection, String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Deleting history entry for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
|
|
|
if (isCallerSync(uri)) {
|
|
|
|
return db.delete(TABLE_HISTORY, selection, selectionArgs);
|
2012-02-27 10:46:35 -08:00
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-02-27 10:46:35 -08:00
|
|
|
debug("Marking history entry as deleted for URI: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-02-27 10:46:35 -08:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(History.IS_DELETED, 1);
|
|
|
|
|
2012-02-27 10:46:36 -08:00
|
|
|
// Wipe sensitive data.
|
|
|
|
values.putNull(History.TITLE);
|
|
|
|
values.put(History.URL, ""); // Column is NOT NULL.
|
|
|
|
values.put(History.DATE_CREATED, 0);
|
|
|
|
values.put(History.DATE_LAST_VISITED, 0);
|
|
|
|
values.put(History.VISITS, 0);
|
2012-04-26 12:40:07 -07:00
|
|
|
values.put(History.DATE_MODIFIED, System.currentTimeMillis());
|
2012-02-27 10:46:36 -08:00
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
// Doing this UPDATE (or the DELETE above) first ensures that the
|
|
|
|
// first operation within a new enclosing transaction is a write.
|
|
|
|
// The cleanup call below will do a SELECT first, and thus would
|
|
|
|
// require the transaction to be upgraded from a reader to a writer.
|
|
|
|
// In some cases that upgrade can fail (SQLITE_BUSY), so we avoid
|
|
|
|
// it if we can.
|
|
|
|
final int updated = db.update(TABLE_HISTORY, values, selection, selectionArgs);
|
|
|
|
try {
|
|
|
|
cleanupSomeDeletedRecords(uri, History.CONTENT_URI, TABLE_HISTORY);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// We don't care.
|
|
|
|
Log.e(LOGTAG, "Unable to clean up deleted history records: ", e);
|
|
|
|
}
|
|
|
|
return updated;
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int deleteBookmarks(Uri uri, String selection, String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Deleting bookmarks for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
|
|
|
if (isCallerSync(uri)) {
|
2014-02-25 12:27:54 -08:00
|
|
|
beginWrite(db);
|
2011-12-13 06:46:58 -08:00
|
|
|
return db.delete(TABLE_BOOKMARKS, selection, selectionArgs);
|
2012-02-27 10:46:35 -08:00
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-02-27 10:46:35 -08:00
|
|
|
debug("Marking bookmarks as deleted for URI: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-02-27 10:46:35 -08:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Bookmarks.IS_DELETED, 1);
|
|
|
|
|
2014-02-25 12:27:53 -08:00
|
|
|
// Doing this UPDATE (or the DELETE above) first ensures that the
|
|
|
|
// first operation within this transaction is a write.
|
|
|
|
// The cleanup call below will do a SELECT first, and thus would
|
|
|
|
// require the transaction to be upgraded from a reader to a writer.
|
|
|
|
final int updated = updateBookmarks(uri, values, selection, selectionArgs);
|
|
|
|
try {
|
|
|
|
cleanupSomeDeletedRecords(uri, Bookmarks.CONTENT_URI, TABLE_BOOKMARKS);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// We don't care.
|
|
|
|
Log.e(LOGTAG, "Unable to clean up deleted bookmark records: ", e);
|
|
|
|
}
|
|
|
|
return updated;
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
int deleteFavicons(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
debug("Deleting favicons for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
return db.delete(TABLE_FAVICONS, selection, selectionArgs);
|
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
int deleteThumbnails(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
debug("Deleting thumbnails for URI: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
2012-02-27 10:46:35 -08:00
|
|
|
|
2012-11-13 10:56:58 -08:00
|
|
|
return db.delete(TABLE_THUMBNAILS, selection, selectionArgs);
|
2011-12-13 06:46:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int deleteUnusedImages(Uri uri) {
|
2012-11-13 10:56:58 -08:00
|
|
|
debug("Deleting all unused favicons and thumbnails for URI: " + uri);
|
|
|
|
|
|
|
|
String faviconSelection = Favicons._ID + " NOT IN "
|
|
|
|
+ "(SELECT " + History.FAVICON_ID
|
|
|
|
+ " FROM " + TABLE_HISTORY
|
|
|
|
+ " WHERE " + History.IS_DELETED + " = 0"
|
|
|
|
+ " AND " + History.FAVICON_ID + " IS NOT NULL"
|
|
|
|
+ " UNION ALL SELECT " + Bookmarks.FAVICON_ID
|
|
|
|
+ " FROM " + TABLE_BOOKMARKS
|
|
|
|
+ " WHERE " + Bookmarks.IS_DELETED + " = 0"
|
|
|
|
+ " AND " + Bookmarks.FAVICON_ID + " IS NOT NULL)";
|
|
|
|
|
|
|
|
String thumbnailSelection = Thumbnails.URL + " NOT IN "
|
|
|
|
+ "(SELECT " + History.URL
|
|
|
|
+ " FROM " + TABLE_HISTORY
|
|
|
|
+ " WHERE " + History.IS_DELETED + " = 0"
|
|
|
|
+ " AND " + History.URL + " IS NOT NULL"
|
|
|
|
+ " UNION ALL SELECT " + Bookmarks.URL
|
|
|
|
+ " FROM " + TABLE_BOOKMARKS
|
|
|
|
+ " WHERE " + Bookmarks.IS_DELETED + " = 0"
|
|
|
|
+ " AND " + Bookmarks.URL + " IS NOT NULL)";
|
|
|
|
|
2013-04-09 17:10:48 -07:00
|
|
|
return deleteFavicons(uri, faviconSelection, null) +
|
2012-11-13 10:56:58 -08:00
|
|
|
deleteThumbnails(uri, thumbnailSelection, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
2012-03-12 14:48:15 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations)
|
|
|
|
throws OperationApplicationException {
|
|
|
|
final int numOperations = operations.size();
|
|
|
|
final ContentProviderResult[] results = new ContentProviderResult[numOperations];
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
if (numOperations < 1) {
|
|
|
|
debug("applyBatch: no operations; returning immediately.");
|
2012-03-12 14:48:15 -07:00
|
|
|
// The original Android implementation returns a zero-length
|
2014-02-25 12:27:54 -08:00
|
|
|
// array in this case. We do the same.
|
2012-03-12 14:48:15 -07:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:27:54 -08:00
|
|
|
boolean failures = false;
|
|
|
|
|
|
|
|
// We only have 1 database for all Uris that we can get.
|
|
|
|
SQLiteDatabase db = getWritableDatabase(operations.get(0).getUri());
|
|
|
|
|
2012-03-12 14:48:15 -07:00
|
|
|
// Note that the apply() call may cause us to generate
|
2014-02-25 12:27:54 -08:00
|
|
|
// additional transactions for the individual operations.
|
2012-03-12 14:48:15 -07:00
|
|
|
// But Android's wrapper for SQLite supports nested transactions,
|
|
|
|
// so this will do the right thing.
|
2014-02-25 12:27:54 -08:00
|
|
|
//
|
|
|
|
// Note further that in some circumstances this can result in
|
|
|
|
// exceptions: if this transaction is first involved in reading,
|
|
|
|
// and then (naturally) tries to perform writes, SQLITE_BUSY can
|
|
|
|
// be raised. See Bug 947939 and friends.
|
|
|
|
beginBatch(db);
|
2012-03-12 14:48:15 -07:00
|
|
|
|
|
|
|
for (int i = 0; i < numOperations; i++) {
|
|
|
|
try {
|
2014-02-25 12:27:54 -08:00
|
|
|
final ContentProviderOperation operation = operations.get(i);
|
|
|
|
results[i] = operation.apply(this, results, i);
|
2012-03-12 14:48:15 -07:00
|
|
|
} catch (SQLException e) {
|
2014-02-25 12:27:54 -08:00
|
|
|
Log.w(LOGTAG, "SQLite Exception during applyBatch.", e);
|
2012-03-12 14:48:15 -07:00
|
|
|
// The Android API makes it implementation-defined whether
|
|
|
|
// the failure of a single operation makes all others abort
|
|
|
|
// or not. For our use cases, best-effort operation makes
|
|
|
|
// more sense. Rolling back and forcing the caller to retry
|
|
|
|
// after it figures out what went wrong isn't very convenient
|
|
|
|
// anyway.
|
|
|
|
// Signal failed operation back, so the caller knows what
|
|
|
|
// went through and what didn't.
|
|
|
|
results[i] = new ContentProviderResult(0);
|
|
|
|
failures = true;
|
|
|
|
// http://www.sqlite.org/lang_conflict.html
|
|
|
|
// Note that we need a new transaction, subsequent operations
|
|
|
|
// on this one will fail (we're in ABORT by default, which
|
|
|
|
// isn't IGNORE). We still need to set it as successful to let
|
|
|
|
// everything before the failed op go through.
|
|
|
|
// We can't set conflict resolution on API level < 8, and even
|
|
|
|
// above 8 it requires splitting the call per operation
|
|
|
|
// (insert/update/delete).
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
db.endTransaction();
|
|
|
|
db.beginTransaction();
|
2012-05-14 03:22:10 -07:00
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
// Repeat of above.
|
|
|
|
results[i] = new ContentProviderResult(0);
|
|
|
|
failures = true;
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
db.endTransaction();
|
|
|
|
db.beginTransaction();
|
2012-03-12 14:48:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trace("Flushing DB applyBatch...");
|
2014-02-25 12:27:54 -08:00
|
|
|
markBatchSuccessful(db);
|
|
|
|
endBatch(db);
|
2012-03-12 14:48:15 -07:00
|
|
|
|
|
|
|
if (failures) {
|
|
|
|
throw new OperationApplicationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-01-20 07:38:18 -08:00
|
|
|
protected BrowserDatabaseHelper createDatabaseHelper(
|
|
|
|
Context context, String databasePath) {
|
|
|
|
return new BrowserDatabaseHelper(context, databasePath);
|
|
|
|
}
|
2014-02-12 06:02:46 -08:00
|
|
|
|
2014-01-20 07:38:18 -08:00
|
|
|
@Override
|
|
|
|
protected String getDatabaseName() {
|
|
|
|
return BrowserDatabaseHelper.DATABASE_NAME;
|
2012-03-12 14:48:15 -07:00
|
|
|
}
|
2011-12-11 22:24:40 -08:00
|
|
|
}
|