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
|
|
|
|
2011-12-15 13:18:29 -08:00
|
|
|
#filter substitution
|
|
|
|
package @ANDROID_PACKAGE_NAME@.db;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
import java.io.File;
|
2012-01-04 17:28:04 -08:00
|
|
|
import java.io.IOException;
|
2012-02-20 11:28:27 -08:00
|
|
|
import java.util.ArrayList;
|
2012-02-27 10:51:28 -08:00
|
|
|
import java.util.Collections;
|
2011-12-08 08:37:21 -08:00
|
|
|
import java.util.HashMap;
|
2012-02-20 11:28:27 -08:00
|
|
|
import java.util.List;
|
2012-02-27 10:51:28 -08:00
|
|
|
import java.util.Map;
|
2011-12-13 06:46:58 -08:00
|
|
|
import java.util.Random;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-02-26 22:34:23 -08:00
|
|
|
import org.mozilla.gecko.GeckoBackgroundThread;
|
2012-02-25 20:22:40 -08:00
|
|
|
import org.mozilla.gecko.GeckoProfile;
|
2012-02-03 13:41:22 -08:00
|
|
|
import org.mozilla.gecko.R;
|
2011-12-08 08:37:21 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
2011-12-13 06:46:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.CommonColumns;
|
2011-12-08 08:37:21 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.History;
|
|
|
|
import org.mozilla.gecko.db.BrowserContract.Images;
|
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;
|
2011-12-13 06:46:58 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract.URLColumns;
|
2011-12-15 13:18:29 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserContract;
|
2011-12-16 15:11:09 -08:00
|
|
|
import org.mozilla.gecko.db.DBUtils;
|
|
|
|
import org.mozilla.gecko.sync.Utils;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
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;
|
2011-12-08 08:37:21 -08:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.database.sqlite.SQLiteQueryBuilder;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class BrowserProvider extends ContentProvider {
|
|
|
|
private static final String LOGTAG = "GeckoBrowserProvider";
|
2012-01-04 17:28:04 -08:00
|
|
|
private Context mContext;
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
static final String DATABASE_NAME = "browser.db";
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
static final int DATABASE_VERSION = 2;
|
2011-12-13 06:12:01 -08:00
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
// Maximum age of deleted records to be cleaned up (20 days in ms)
|
|
|
|
static final long MAX_AGE_OF_DELETED_RECORDS = 86400000 * 20;
|
|
|
|
|
|
|
|
// Number of records marked as deleted to be removed
|
|
|
|
static final long DELETED_RECORDS_PURGE_LIMIT = 5;
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
static final String TABLE_BOOKMARKS = "bookmarks";
|
|
|
|
static final String TABLE_HISTORY = "history";
|
|
|
|
static final String TABLE_IMAGES = "images";
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
static final String TABLE_BOOKMARKS_TMP = TABLE_BOOKMARKS + "_tmp";
|
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
static final String VIEW_BOOKMARKS_WITH_IMAGES = "bookmarks_with_images";
|
|
|
|
static final String VIEW_HISTORY_WITH_IMAGES = "history_with_images";
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Image matches
|
|
|
|
static final int IMAGES = 300;
|
2011-12-13 06:46:58 -08:00
|
|
|
static final int IMAGES_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;
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
static final String DEFAULT_BOOKMARKS_SORT_ORDER = Bookmarks.IS_FOLDER
|
|
|
|
+ " DESC, " + Bookmarks.POSITION + " ASC, " + Bookmarks._ID
|
|
|
|
+ " ASC";
|
|
|
|
|
|
|
|
static final String DEFAULT_HISTORY_SORT_ORDER = History.DATE_LAST_VISITED + " DESC";
|
|
|
|
|
|
|
|
static final String TABLE_BOOKMARKS_JOIN_IMAGES = TABLE_BOOKMARKS + " LEFT OUTER JOIN " +
|
2012-02-22 03:47:13 -08:00
|
|
|
TABLE_IMAGES + " ON " + qualifyColumn(TABLE_BOOKMARKS, Bookmarks.URL) + " = " +
|
|
|
|
qualifyColumn(TABLE_IMAGES, Images.URL);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
static final String TABLE_HISTORY_JOIN_IMAGES = TABLE_HISTORY + " LEFT OUTER JOIN " +
|
2012-02-22 03:47:13 -08:00
|
|
|
TABLE_IMAGES + " ON " + qualifyColumn(TABLE_HISTORY, History.URL) + " = " +
|
|
|
|
qualifyColumn(TABLE_IMAGES, Images.URL);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
static final Map<String, String> IMAGES_PROJECTION_MAP;
|
|
|
|
static final Map<String, String> SCHEMA_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);
|
|
|
|
map.put(Bookmarks.THUMBNAIL, Bookmarks.THUMBNAIL);
|
|
|
|
map.put(Bookmarks.IS_FOLDER, Bookmarks.IS_FOLDER);
|
|
|
|
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-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);
|
|
|
|
map.put(History.THUMBNAIL, History.THUMBNAIL);
|
|
|
|
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
|
|
|
|
|
|
|
// Images
|
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "images", IMAGES);
|
2012-01-25 03:18:33 -08:00
|
|
|
URI_MATCHER.addURI(BrowserContract.AUTHORITY, "images/#", IMAGES_ID);
|
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(Images._ID, Images._ID);
|
2011-12-08 08:37:21 -08:00
|
|
|
map.put(Images.URL, Images.URL);
|
|
|
|
map.put(Images.FAVICON, Images.FAVICON);
|
|
|
|
map.put(Images.FAVICON_URL, Images.FAVICON_URL);
|
|
|
|
map.put(Images.THUMBNAIL, Images.THUMBNAIL);
|
2011-12-13 11:47:40 -08:00
|
|
|
map.put(Images.DATE_CREATED, Images.DATE_CREATED);
|
|
|
|
map.put(Images.DATE_MODIFIED, Images.DATE_MODIFIED);
|
|
|
|
map.put(Images.GUID, Images.GUID);
|
|
|
|
map.put(Images.IS_DELETED, Images.IS_DELETED);
|
2012-02-27 10:51:28 -08:00
|
|
|
IMAGES_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);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-01-02 08:37:13 -08:00
|
|
|
private HashMap<String, DatabaseHelper> mDatabasePerProfile;
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
static final String qualifyColumn(String table, String column) {
|
|
|
|
return table + "." + column;
|
|
|
|
}
|
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
private static boolean hasImagesInProjection(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) {
|
|
|
|
if (projection[i].equals(Images.FAVICON) ||
|
|
|
|
projection[i].equals(Images.THUMBNAIL))
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
final class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
public DatabaseHelper(Context context, String databasePath) {
|
|
|
|
super(context, databasePath, null, DATABASE_VERSION);
|
|
|
|
}
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private void createBookmarksTable(SQLiteDatabase db) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Creating " + TABLE_BOOKMARKS + " table");
|
2012-02-20 11:28:27 -08:00
|
|
|
|
|
|
|
// Android versions older than Froyo ship with an sqlite
|
|
|
|
// that doesn't support foreign keys.
|
|
|
|
String foreignKeyOnParent = null;
|
|
|
|
if (Build.VERSION.SDK_INT >= 8) {
|
|
|
|
foreignKeyOnParent = ", FOREIGN KEY (" + Bookmarks.PARENT +
|
|
|
|
") REFERENCES " + TABLE_BOOKMARKS + "(" + Bookmarks._ID + ")";
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
db.execSQL("CREATE TABLE " + TABLE_BOOKMARKS + "(" +
|
|
|
|
Bookmarks._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
|
|
Bookmarks.TITLE + " TEXT," +
|
|
|
|
Bookmarks.URL + " TEXT," +
|
|
|
|
Bookmarks.IS_FOLDER + " INTEGER NOT NULL DEFAULT 0," +
|
|
|
|
Bookmarks.PARENT + " INTEGER," +
|
|
|
|
Bookmarks.POSITION + " INTEGER NOT NULL," +
|
2011-12-13 06:46:58 -08:00
|
|
|
Bookmarks.KEYWORD + " TEXT," +
|
|
|
|
Bookmarks.DESCRIPTION + " TEXT," +
|
|
|
|
Bookmarks.TAGS + " TEXT," +
|
2011-12-08 08:37:21 -08:00
|
|
|
Bookmarks.DATE_CREATED + " INTEGER," +
|
|
|
|
Bookmarks.DATE_MODIFIED + " INTEGER," +
|
2011-12-13 06:46:58 -08:00
|
|
|
Bookmarks.GUID + " TEXT," +
|
|
|
|
Bookmarks.IS_DELETED + " INTEGER NOT NULL DEFAULT 0" +
|
2012-02-20 11:28:27 -08:00
|
|
|
(foreignKeyOnParent != null ? foreignKeyOnParent : "") +
|
2011-12-08 08:37:21 -08:00
|
|
|
");");
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
db.execSQL("CREATE INDEX bookmarks_url_index ON " + TABLE_BOOKMARKS + "("
|
|
|
|
+ Bookmarks.URL + ")");
|
|
|
|
db.execSQL("CREATE UNIQUE INDEX bookmarks_guid_index ON " + TABLE_BOOKMARKS + "("
|
|
|
|
+ Bookmarks.GUID + ")");
|
|
|
|
db.execSQL("CREATE INDEX bookmarks_modified_index ON " + TABLE_BOOKMARKS + "("
|
|
|
|
+ Bookmarks.DATE_MODIFIED + ")");
|
2012-02-20 11:28:27 -08:00
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private void createHistoryTable(SQLiteDatabase db) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Creating " + TABLE_HISTORY + " table");
|
2011-12-08 08:37:21 -08:00
|
|
|
db.execSQL("CREATE TABLE " + TABLE_HISTORY + "(" +
|
|
|
|
History._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
|
|
History.TITLE + " TEXT," +
|
|
|
|
History.URL + " TEXT NOT NULL," +
|
|
|
|
History.VISITS + " INTEGER NOT NULL DEFAULT 0," +
|
|
|
|
History.DATE_LAST_VISITED + " INTEGER," +
|
|
|
|
History.DATE_CREATED + " INTEGER," +
|
|
|
|
History.DATE_MODIFIED + " INTEGER," +
|
2011-12-13 06:46:58 -08:00
|
|
|
History.GUID + " TEXT," +
|
|
|
|
History.IS_DELETED + " INTEGER NOT NULL DEFAULT 0" +
|
2011-12-08 08:37:21 -08:00
|
|
|
");");
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
db.execSQL("CREATE INDEX history_url_index ON " + TABLE_HISTORY + "("
|
|
|
|
+ History.URL + ")");
|
|
|
|
db.execSQL("CREATE UNIQUE INDEX history_guid_index ON " + TABLE_HISTORY + "("
|
|
|
|
+ History.GUID + ")");
|
|
|
|
db.execSQL("CREATE INDEX history_modified_index ON " + TABLE_HISTORY + "("
|
|
|
|
+ History.DATE_MODIFIED + ")");
|
|
|
|
db.execSQL("CREATE INDEX history_visited_index ON " + TABLE_HISTORY + "("
|
|
|
|
+ History.DATE_LAST_VISITED + ")");
|
2012-02-20 11:28:27 -08:00
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private void createImagesTable(SQLiteDatabase db) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Creating " + TABLE_IMAGES + " table");
|
2011-12-08 08:37:21 -08:00
|
|
|
db.execSQL("CREATE TABLE " + TABLE_IMAGES + " (" +
|
2011-12-13 06:46:58 -08:00
|
|
|
Images._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
2011-12-08 08:37:21 -08:00
|
|
|
Images.URL + " TEXT UNIQUE NOT NULL," +
|
|
|
|
Images.FAVICON + " BLOB," +
|
|
|
|
Images.FAVICON_URL + " TEXT," +
|
|
|
|
Images.THUMBNAIL + " BLOB," +
|
|
|
|
Images.DATE_CREATED + " INTEGER," +
|
|
|
|
Images.DATE_MODIFIED + " INTEGER," +
|
2011-12-13 06:46:58 -08:00
|
|
|
Images.GUID + " TEXT," +
|
|
|
|
Images.IS_DELETED + " INTEGER NOT NULL DEFAULT 0" +
|
2011-12-08 08:37:21 -08:00
|
|
|
");");
|
|
|
|
|
|
|
|
db.execSQL("CREATE INDEX images_url_index ON " + TABLE_IMAGES + "("
|
|
|
|
+ Images.URL + ")");
|
2011-12-13 06:46:58 -08:00
|
|
|
db.execSQL("CREATE UNIQUE INDEX images_guid_index ON " + TABLE_IMAGES + "("
|
|
|
|
+ Images.GUID + ")");
|
|
|
|
db.execSQL("CREATE INDEX images_modified_index ON " + TABLE_IMAGES + "("
|
|
|
|
+ Images.DATE_MODIFIED + ")");
|
2012-02-20 11:28:27 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private void createBookmarksWithImagesView(SQLiteDatabase db) {
|
2012-02-22 03:47:13 -08:00
|
|
|
debug("Creating " + VIEW_BOOKMARKS_WITH_IMAGES + " view");
|
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
db.execSQL("CREATE VIEW IF NOT EXISTS " + VIEW_BOOKMARKS_WITH_IMAGES + " AS " +
|
|
|
|
"SELECT " + qualifyColumn(TABLE_BOOKMARKS, "*") +
|
|
|
|
", " + Images.FAVICON + ", " + Images.THUMBNAIL + " FROM " +
|
|
|
|
TABLE_BOOKMARKS_JOIN_IMAGES);
|
2012-02-20 11:28:27 -08:00
|
|
|
}
|
2011-12-13 11:47:40 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private void createHistoryWithImagesView(SQLiteDatabase db) {
|
2012-02-22 03:47:13 -08:00
|
|
|
debug("Creating " + VIEW_HISTORY_WITH_IMAGES + " view");
|
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
db.execSQL("CREATE VIEW IF NOT EXISTS " + VIEW_HISTORY_WITH_IMAGES + " AS " +
|
|
|
|
"SELECT " + qualifyColumn(TABLE_HISTORY, "*") +
|
|
|
|
", " + Images.FAVICON + ", " + Images.THUMBNAIL + " FROM " +
|
|
|
|
TABLE_HISTORY_JOIN_IMAGES);
|
2012-02-20 11:28:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
debug("Creating browser.db: " + db.getPath());
|
|
|
|
|
|
|
|
createBookmarksTable(db);
|
|
|
|
createHistoryTable(db);
|
|
|
|
createImagesTable(db);
|
|
|
|
|
|
|
|
createBookmarksWithImagesView(db);
|
|
|
|
createHistoryWithImagesView(db);
|
2011-12-13 11:47:40 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.PLACES_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_places, 0);
|
|
|
|
|
|
|
|
createOrUpdateAllSpecialFolders(db);
|
|
|
|
|
|
|
|
// FIXME: Create default bookmarks here (bug 728224)
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createOrUpdateAllSpecialFolders(SQLiteDatabase db) {
|
2012-02-20 11:28:27 -08:00
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.MOBILE_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_mobile, 0);
|
2012-02-20 11:28:27 -08:00
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.TOOLBAR_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_toolbar, 1);
|
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.MENU_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_menu, 2);
|
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.TAGS_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_tags, 3);
|
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.UNFILED_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_unfiled, 4);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private void createOrUpdateSpecialFolder(SQLiteDatabase db,
|
|
|
|
String guid, int titleId, int position) {
|
2011-12-13 11:47:40 -08:00
|
|
|
ContentValues values = new ContentValues();
|
2012-02-20 11:28:27 -08:00
|
|
|
values.put(Bookmarks.GUID, guid);
|
2011-12-13 11:47:40 -08:00
|
|
|
values.put(Bookmarks.IS_FOLDER, 1);
|
2012-02-20 11:28:27 -08:00
|
|
|
values.put(Bookmarks.POSITION, position);
|
2011-12-13 11:47:40 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
if (guid.equals(Bookmarks.PLACES_FOLDER_GUID))
|
|
|
|
values.put(Bookmarks._ID, Bookmarks.FIXED_ROOT_ID);
|
|
|
|
|
2012-02-03 13:41:22 -08:00
|
|
|
// Set the parent to 0, which sync assumes is the root
|
2012-02-20 11:28:27 -08:00
|
|
|
values.put(Bookmarks.PARENT, Bookmarks.FIXED_ROOT_ID);
|
2012-02-03 13:41:22 -08:00
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
String title = mContext.getResources().getString(titleId);
|
2012-02-03 13:41:22 -08:00
|
|
|
values.put(Bookmarks.TITLE, title);
|
|
|
|
|
2011-12-13 11:47:40 -08:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
values.put(Bookmarks.DATE_CREATED, now);
|
|
|
|
values.put(Bookmarks.DATE_MODIFIED, now);
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
int updated = db.update(TABLE_BOOKMARKS, values,
|
|
|
|
Bookmarks.GUID + " = ?",
|
|
|
|
new String[] { guid });
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
if (updated == 0) {
|
2012-02-20 11:28:27 -08:00
|
|
|
db.insert(TABLE_BOOKMARKS, Bookmarks.GUID, values);
|
2012-02-20 11:28:27 -08:00
|
|
|
debug("Inserted special folder: " + guid);
|
|
|
|
} else {
|
|
|
|
debug("Updated special folder: " + guid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isSpecialFolder(ContentValues values) {
|
|
|
|
String guid = values.getAsString(Bookmarks.GUID);
|
|
|
|
if (guid == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return guid.equals(Bookmarks.MOBILE_FOLDER_GUID) ||
|
|
|
|
guid.equals(Bookmarks.MENU_FOLDER_GUID) ||
|
|
|
|
guid.equals(Bookmarks.TOOLBAR_FOLDER_GUID) ||
|
|
|
|
guid.equals(Bookmarks.UNFILED_FOLDER_GUID) ||
|
|
|
|
guid.equals(Bookmarks.TAGS_FOLDER_GUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void migrateBookmarkFolder(SQLiteDatabase db, int folderId) {
|
|
|
|
Cursor c = null;
|
|
|
|
|
|
|
|
debug("Migrating bookmark folder with id = " + folderId);
|
|
|
|
|
|
|
|
String selection = Bookmarks.PARENT + " = " + folderId;
|
|
|
|
String[] selectionArgs = null;
|
|
|
|
|
|
|
|
boolean isRootFolder = (folderId == Bookmarks.FIXED_ROOT_ID);
|
|
|
|
|
|
|
|
// If we're loading the root folder, we have to account for
|
|
|
|
// any previously created special folder that was created without
|
|
|
|
// setting a parent id (e.g. mobile folder) and making sure we're
|
|
|
|
// not adding any infinite recursion as root's parent is root itself.
|
|
|
|
if (isRootFolder) {
|
|
|
|
selection = Bookmarks.GUID + " != ?" + " AND (" +
|
|
|
|
selection + " OR " + Bookmarks.PARENT + " = NULL)";
|
|
|
|
selectionArgs = new String[] { Bookmarks.PLACES_FOLDER_GUID };
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Integer> subFolders = new ArrayList<Integer>();
|
|
|
|
List<ContentValues> invalidSpecialEntries = new ArrayList<ContentValues>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
c = db.query(TABLE_BOOKMARKS_TMP,
|
|
|
|
null,
|
|
|
|
selection,
|
|
|
|
selectionArgs,
|
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
// The key point here is that bookmarks should be added in
|
|
|
|
// parent order to avoid any problems with the foreign key
|
|
|
|
// in Bookmarks.PARENT.
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
|
|
|
// We're using a null projection in the query which
|
|
|
|
// means we're getting all columns from the table.
|
|
|
|
// It's safe to simply transform the row into the
|
|
|
|
// values to be inserted on the new table.
|
|
|
|
DatabaseUtils.cursorRowToContentValues(c, values);
|
|
|
|
|
|
|
|
boolean isSpecialFolder = isSpecialFolder(values);
|
|
|
|
|
|
|
|
// The mobile folder used to be created with PARENT = NULL.
|
|
|
|
// We want fix that here.
|
|
|
|
if (values.getAsLong(Bookmarks.PARENT) == null && isSpecialFolder)
|
|
|
|
values.put(Bookmarks.PARENT, Bookmarks.FIXED_ROOT_ID);
|
|
|
|
|
|
|
|
if (isRootFolder && !isSpecialFolder) {
|
|
|
|
invalidSpecialEntries.add(values);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Migrating bookmark: " + values.getAsString(Bookmarks.TITLE));
|
|
|
|
db.insert(TABLE_BOOKMARKS, Bookmarks.URL, values);
|
|
|
|
|
|
|
|
Integer isFolder = values.getAsInteger(Bookmarks.IS_FOLDER);
|
|
|
|
if (isFolder != null && isFolder == 1)
|
|
|
|
subFolders.add(values.getAsInteger(Bookmarks._ID));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (c != null)
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point is safe to assume that the mobile folder is
|
|
|
|
// in the new table given that we've always created it on
|
|
|
|
// database creation time.
|
|
|
|
final int nInvalidSpecialEntries = invalidSpecialEntries.size();
|
|
|
|
if (nInvalidSpecialEntries > 0) {
|
|
|
|
Long mobileFolderId = guidToID(db, Bookmarks.MOBILE_FOLDER_GUID);
|
|
|
|
|
|
|
|
debug("Found " + nInvalidSpecialEntries + " invalid special folder entries");
|
|
|
|
for (int i = 0; i < nInvalidSpecialEntries; i++) {
|
|
|
|
ContentValues values = invalidSpecialEntries.get(i);
|
|
|
|
values.put(Bookmarks.PARENT, mobileFolderId);
|
|
|
|
|
|
|
|
db.insert(TABLE_BOOKMARKS, Bookmarks.URL, values);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final int nSubFolders = subFolders.size();
|
|
|
|
for (int i = 0; i < nSubFolders; i++) {
|
|
|
|
int subFolderId = subFolders.get(i);
|
|
|
|
migrateBookmarkFolder(db, subFolderId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void upgradeDatabaseFrom1to2(SQLiteDatabase db) {
|
|
|
|
debug("Renaming bookmarks table to " + TABLE_BOOKMARKS_TMP);
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE_BOOKMARKS +
|
|
|
|
" RENAME TO " + TABLE_BOOKMARKS_TMP);
|
|
|
|
|
|
|
|
debug("Dropping views and indexes related to " + TABLE_BOOKMARKS);
|
|
|
|
db.execSQL("DROP VIEW IF EXISTS " + VIEW_BOOKMARKS_WITH_IMAGES);
|
|
|
|
|
|
|
|
db.execSQL("DROP INDEX IF EXISTS bookmarks_url_index");
|
|
|
|
db.execSQL("DROP INDEX IF EXISTS bookmarks_guid_index");
|
|
|
|
db.execSQL("DROP INDEX IF EXISTS bookmarks_modified_index");
|
|
|
|
|
|
|
|
createBookmarksTable(db);
|
|
|
|
createBookmarksWithImagesView(db);
|
|
|
|
|
|
|
|
createOrUpdateSpecialFolder(db, Bookmarks.PLACES_FOLDER_GUID,
|
|
|
|
R.string.bookmarks_folder_places, 0);
|
|
|
|
|
|
|
|
migrateBookmarkFolder(db, Bookmarks.FIXED_ROOT_ID);
|
|
|
|
|
|
|
|
// Ensure all special folders exist and have the
|
|
|
|
// right folder hierarchy.
|
|
|
|
createOrUpdateAllSpecialFolders(db);
|
|
|
|
|
|
|
|
debug("Dropping bookmarks temporary table");
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKMARKS_TMP);
|
2011-12-13 11:47:40 -08:00
|
|
|
}
|
|
|
|
|
2012-02-22 03:47:13 -08:00
|
|
|
private void upgradeDatabaseFrom2to3(SQLiteDatabase db) {
|
|
|
|
debug("Dropping view: " + VIEW_BOOKMARKS_WITH_IMAGES);
|
|
|
|
db.execSQL("DROP VIEW IF EXISTS " + VIEW_BOOKMARKS_WITH_IMAGES);
|
|
|
|
|
|
|
|
createBookmarksWithImagesView(db);
|
|
|
|
|
|
|
|
debug("Dropping view: " + VIEW_HISTORY_WITH_IMAGES);
|
|
|
|
db.execSQL("DROP VIEW IF EXISTS " + VIEW_HISTORY_WITH_IMAGES);
|
|
|
|
|
|
|
|
createHistoryWithImagesView(db);
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Upgrading browser.db: " + db.getPath() + " from " +
|
2011-12-08 08:37:21 -08:00
|
|
|
oldVersion + " to " + newVersion);
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
// We have to do incremental upgrades until we reach the current
|
|
|
|
// database schema version.
|
|
|
|
for (int v = oldVersion + 1; v <= newVersion; v++) {
|
|
|
|
switch(v) {
|
|
|
|
case 2:
|
|
|
|
upgradeDatabaseFrom1to2(db);
|
|
|
|
break;
|
2012-02-22 03:47:13 -08:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
upgradeDatabaseFrom2to3(db);
|
|
|
|
break;
|
2012-02-20 11:28:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.endTransaction();
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onOpen(SQLiteDatabase db) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Opening browser.db: " + db.getPath());
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
// From Honeycomb on, it's possible to run several db
|
|
|
|
// commands in parallel using multiple connections.
|
2012-01-26 07:30:21 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
2011-12-08 08:37:21 -08:00
|
|
|
db.enableWriteAheadLogging();
|
2012-01-26 07:30:21 -08:00
|
|
|
} else {
|
|
|
|
// Pre-Honeycomb, we can do some lesser optimizations.
|
|
|
|
Cursor cursor = null;
|
|
|
|
try {
|
|
|
|
cursor = db.rawQuery("PRAGMA synchronous=NORMAL", null);
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
cursor = null;
|
|
|
|
try {
|
|
|
|
cursor = db.rawQuery("PRAGMA journal_mode=PERSIST", null);
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 11:28:27 -08:00
|
|
|
private Long guidToID(SQLiteDatabase db, String guid) {
|
|
|
|
Cursor c = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
c = db.query(TABLE_BOOKMARKS,
|
|
|
|
new String[] { Bookmarks._ID },
|
|
|
|
Bookmarks.GUID + " = ?",
|
|
|
|
new String[] { guid },
|
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
if (c == null || !c.moveToFirst())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return c.getLong(c.getColumnIndex(Bookmarks._ID));
|
|
|
|
} finally {
|
|
|
|
if (c != null)
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
private DatabaseHelper getDatabaseHelperForProfile(String profile) {
|
|
|
|
// Each profile has a separate browser.db database. The target
|
|
|
|
// profile is provided using a URI query argument in each request
|
|
|
|
// to our content provider.
|
|
|
|
|
2012-01-02 08:37:14 -08:00
|
|
|
// Always fallback to default profile if none has been provided.
|
2011-12-08 08:37:21 -08:00
|
|
|
if (TextUtils.isEmpty(profile)) {
|
|
|
|
profile = BrowserContract.DEFAULT_PROFILE;
|
|
|
|
}
|
|
|
|
|
2012-01-02 08:37:13 -08:00
|
|
|
DatabaseHelper dbHelper;
|
|
|
|
synchronized (this) {
|
|
|
|
dbHelper = mDatabasePerProfile.get(profile);
|
|
|
|
if (dbHelper != null) {
|
|
|
|
return dbHelper;
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
2012-01-02 08:37:13 -08:00
|
|
|
dbHelper = new DatabaseHelper(getContext(), getDatabasePath(profile));
|
|
|
|
mDatabasePerProfile.put(profile, dbHelper);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Created database helper for profile: " + profile);
|
2011-12-08 08:37:21 -08:00
|
|
|
return dbHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getDatabasePath(String profile) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Getting database path for profile: " + profile);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
// On Android releases older than 2.3, it's not possible to use
|
|
|
|
// SQLiteOpenHelper with a full path. Fallback to using separate
|
|
|
|
// db files per profile in the app directory.
|
|
|
|
if (Build.VERSION.SDK_INT <= 8) {
|
|
|
|
return "browser-" + profile + ".db";
|
|
|
|
}
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
File profileDir = GeckoProfile.get(mContext, profile).getDir();
|
2011-12-08 08:37:21 -08:00
|
|
|
if (profileDir == null) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Couldn't find directory for profile: " + profile);
|
2011-12-08 08:37:21 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String databasePath = new File(profileDir, DATABASE_NAME).getAbsolutePath();
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Successfully created database path for profile: " + databasePath);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
return databasePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SQLiteDatabase getReadableDatabase(Uri uri) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Getting readable database for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
String profile = null;
|
|
|
|
|
|
|
|
if (uri != null)
|
|
|
|
profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
|
|
|
|
|
|
|
|
return getDatabaseHelperForProfile(profile).getReadableDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
private SQLiteDatabase getWritableDatabase(Uri uri) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Getting writable database for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
String profile = null;
|
|
|
|
|
|
|
|
if (uri != null)
|
|
|
|
profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
|
|
|
|
|
|
|
|
return getDatabaseHelperForProfile(profile).getWritableDatabase();
|
|
|
|
}
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
private void cleanupSomeDeletedRecords(Uri fromUri, Uri targetUri, String tableName) {
|
|
|
|
// we cleanup records marked as deleted that are older than a
|
|
|
|
// predefined max age. It's important not be too greedy here and
|
|
|
|
// remove only a few old deleted records at a time.
|
|
|
|
|
|
|
|
String profile = fromUri.getQueryParameter(BrowserContract.PARAM_PROFILE);
|
|
|
|
|
|
|
|
// The PARAM_SHOW_DELETED argument is necessary to return the records
|
|
|
|
// that were marked as deleted. We use PARAM_IS_SYNC here to ensure
|
|
|
|
// that we'll be actually deleting records instead of flagging them.
|
|
|
|
Uri uriWithArgs = targetUri.buildUpon()
|
|
|
|
.appendQueryParameter(BrowserContract.PARAM_PROFILE, profile)
|
|
|
|
.appendQueryParameter(BrowserContract.PARAM_LIMIT, String.valueOf(DELETED_RECORDS_PURGE_LIMIT))
|
|
|
|
.appendQueryParameter(BrowserContract.PARAM_SHOW_DELETED, "1")
|
|
|
|
.appendQueryParameter(BrowserContract.PARAM_IS_SYNC, "1")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
long now = System.currentTimeMillis();
|
2011-12-13 11:47:40 -08:00
|
|
|
String selection = SyncColumns.IS_DELETED + " = 1 AND " +
|
|
|
|
SyncColumns.DATE_MODIFIED + " <= " + (now - MAX_AGE_OF_DELETED_RECORDS);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
|
|
|
cursor = query(uriWithArgs,
|
|
|
|
new String[] { CommonColumns._ID },
|
|
|
|
selection,
|
|
|
|
null,
|
|
|
|
null);
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
Uri uriWithId = ContentUris.withAppendedId(uriWithArgs, cursor.getLong(0));
|
|
|
|
delete(uriWithId, null, null);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Removed old deleted item with URI: " + uriWithId);
|
2011-12-13 06:46:58 -08:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isCallerSync(Uri uri) {
|
|
|
|
String isSync = uri.getQueryParameter(BrowserContract.PARAM_IS_SYNC);
|
|
|
|
return !TextUtils.isEmpty(isSync);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean shouldShowDeleted(Uri uri) {
|
|
|
|
String showDeleted = uri.getQueryParameter(BrowserContract.PARAM_SHOW_DELETED);
|
|
|
|
return !TextUtils.isEmpty(showDeleted);
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Creating BrowserProvider");
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-02-26 22:34:23 -08:00
|
|
|
GeckoBackgroundThread.post(new Runnable() {
|
|
|
|
@Override
|
2012-01-04 17:28:04 -08:00
|
|
|
public void run() {
|
|
|
|
// Kick this off early. It is synchronized so that other callers will wait
|
|
|
|
try {
|
2012-02-25 20:22:40 -08:00
|
|
|
GeckoProfile.get(getContext()).getDir();
|
2012-01-04 17:28:04 -08:00
|
|
|
} catch (Exception ex) {
|
|
|
|
Log.e(LOGTAG, "Error getting profile dir", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-01-02 08:37:13 -08:00
|
|
|
synchronized (this) {
|
2012-01-04 17:28:04 -08:00
|
|
|
mContext = getContext();
|
2012-01-02 08:37:13 -08:00
|
|
|
mDatabasePerProfile = new HashMap<String, DatabaseHelper>();
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-02-09 18:39:11 -08:00
|
|
|
debug("URI has unrecognized type: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Calling delete on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
int deleted = 0;
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Beginning delete transaction: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
deleted = deleteInTransaction(uri, selection, selectionArgs);
|
|
|
|
db.setTransactionSuccessful();
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Successful delete transaction: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deleted = deleteInTransaction(uri, selection, selectionArgs);
|
|
|
|
}
|
2012-02-14 16:23:06 -08:00
|
|
|
|
|
|
|
if (deleted > 0)
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
return deleted;
|
|
|
|
}
|
|
|
|
|
2012-01-05 10:49:33 -08:00
|
|
|
@SuppressWarnings("fallthrough")
|
2011-12-08 08:37:21 -08:00
|
|
|
public int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Calling delete in transaction on URI: " + uri);
|
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);
|
2011-12-13 06:46:58 -08:00
|
|
|
deleted = deleteHistory(uri, selection, selectionArgs);
|
|
|
|
deleteUnusedImages(uri);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IMAGES_ID:
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Delete on IMAGES_ID: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, TABLE_IMAGES + "._id = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-13 06:46:58 -08:00
|
|
|
new String[] { Long.toString(ContentUris.parseId(uri)) });
|
|
|
|
// fall through
|
|
|
|
case IMAGES: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Deleting images: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
deleted = deleteImages(uri, selection, selectionArgs);
|
|
|
|
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 insert(Uri uri, ContentValues values) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Calling insert on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
Uri result = null;
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Beginning insert transaction: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
result = insertInTransaction(uri, values);
|
|
|
|
db.setTransactionSuccessful();
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Successful insert transaction: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = insertInTransaction(uri, values);
|
|
|
|
}
|
|
|
|
|
2012-02-14 16:23:06 -08:00
|
|
|
if (result != null)
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
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);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-02-21 05:17:18 -08:00
|
|
|
// Generate values if not specified. Don't overwrite
|
|
|
|
// if specified by caller.
|
2011-12-08 08:37:21 -08:00
|
|
|
long now = System.currentTimeMillis();
|
2012-02-21 05:17:18 -08:00
|
|
|
if (!values.containsKey(Bookmarks.DATE_CREATED)) {
|
|
|
|
values.put(Bookmarks.DATE_CREATED, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!values.containsKey(Bookmarks.DATE_MODIFIED)) {
|
|
|
|
values.put(Bookmarks.DATE_MODIFIED, now);
|
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-11 22:24:40 -08:00
|
|
|
if (!values.containsKey(Bookmarks.GUID)) {
|
2011-12-16 15:11:09 -08:00
|
|
|
values.put(Bookmarks.GUID, Utils.generateGuid());
|
2011-12-11 22:24:40 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
if (!values.containsKey(Bookmarks.POSITION)) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserting bookmark with no position for URI");
|
2011-12-08 08:37:21 -08:00
|
|
|
values.put(Bookmarks.POSITION, Long.toString(Long.MIN_VALUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
String url = values.getAsString(Bookmarks.URL);
|
|
|
|
ContentValues imageValues = extractImageValues(values, url);
|
2012-01-11 11:47:19 -08:00
|
|
|
Integer isFolder = values.getAsInteger(Bookmarks.IS_FOLDER);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2012-01-11 11:47:19 -08:00
|
|
|
if ((isFolder == null || isFolder != 1) && imageValues != null
|
2011-12-08 08:37:21 -08:00
|
|
|
&& !TextUtils.isEmpty(url)) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserting bookmark image for URL: " + url);
|
2011-12-13 06:46:58 -08:00
|
|
|
updateOrInsertImage(uri, imageValues, Images.URL + " = ?",
|
|
|
|
new String[] { url });
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserting bookmark in database with URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
id = db.insertOrThrow(TABLE_BOOKMARKS, Bookmarks.TITLE, values);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case HISTORY: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Insert on HISTORY: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
values.put(History.DATE_CREATED, now);
|
|
|
|
values.put(History.DATE_MODIFIED, now);
|
|
|
|
|
2011-12-11 22:24:40 -08:00
|
|
|
// Generate GUID for new history entry. Don't override specified GUIDs.
|
|
|
|
if (!values.containsKey(History.GUID)) {
|
2011-12-16 15:11:09 -08:00
|
|
|
values.put(History.GUID, Utils.generateGuid());
|
2011-12-11 22:24:40 -08:00
|
|
|
}
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
String url = values.getAsString(History.URL);
|
|
|
|
|
|
|
|
ContentValues imageValues = extractImageValues(values,
|
|
|
|
values.getAsString(History.URL));
|
|
|
|
|
|
|
|
if (imageValues != null) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserting history image for URL: " + url);
|
2011-12-13 06:46:58 -08:00
|
|
|
updateOrInsertImage(uri, imageValues, Images.URL + " = ?",
|
|
|
|
new String[] { url });
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserting history in database with URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
id = db.insertOrThrow(TABLE_HISTORY, History.VISITS, values);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
case IMAGES: {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Insert on IMAGES: " + uri);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
values.put(History.DATE_CREATED, now);
|
|
|
|
values.put(History.DATE_MODIFIED, now);
|
|
|
|
|
|
|
|
// Generate GUID for new history entry
|
2011-12-16 15:11:09 -08:00
|
|
|
values.put(History.GUID, Utils.generateGuid());
|
2011-12-13 06:46:58 -08:00
|
|
|
|
|
|
|
String url = values.getAsString(Images.URL);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Inserting image in database with URL: " + url);
|
2011-12-13 06:46:58 -08:00
|
|
|
id = db.insertOrThrow(TABLE_IMAGES, Images.URL, values);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int update(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Calling update on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
int updated = 0;
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Beginning update transaction: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
updated = updateInTransaction(uri, values, selection, selectionArgs);
|
|
|
|
db.setTransactionSuccessful();
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Successful update transaction: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updated = updateInTransaction(uri, values, selection, selectionArgs);
|
|
|
|
}
|
|
|
|
|
2012-02-14 16:23:06 -08:00
|
|
|
if (updated > 0)
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
2012-01-05 10:49:33 -08:00
|
|
|
@SuppressWarnings("fallthrough")
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
updated = updateBookmarkPositions(uri, selectionArgs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:10:18 -08:00
|
|
|
case BOOKMARKS_PARENT: {
|
|
|
|
debug("Update on BOOKMARKS_PARENT: " + uri);
|
|
|
|
updated = updateBookmarkParents(uri, values, selection, selectionArgs);
|
|
|
|
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);
|
2011-12-08 08:37:21 -08:00
|
|
|
updated = updateBookmarks(uri, values, selection, selectionArgs);
|
|
|
|
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);
|
2011-12-08 08:37:21 -08:00
|
|
|
updated = updateHistory(uri, values, selection, selectionArgs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IMAGES: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Update on IMAGES: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
String url = values.getAsString(Images.URL);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(url))
|
|
|
|
throw new IllegalArgumentException("Images.URL is required");
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
updated = updateExistingImage(uri, values, Images.URL + " = ?",
|
|
|
|
new String[] { url });
|
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);
|
|
|
|
|
|
|
|
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
|
|
|
String limit = uri.getQueryParameter(BrowserContract.PARAM_LIMIT);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (hasImagesInProjection(projection))
|
|
|
|
qb.setTables(VIEW_BOOKMARKS_WITH_IMAGES);
|
|
|
|
else
|
|
|
|
qb.setTables(TABLE_BOOKMARKS);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case HISTORY_ID:
|
|
|
|
case HISTORY: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Query is on history: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
if (match == HISTORY_ID) {
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, History._ID + " = ?");
|
|
|
|
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(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
|
|
|
|
|
|
|
if (hasImagesInProjection(projection))
|
|
|
|
qb.setTables(VIEW_HISTORY_WITH_IMAGES);
|
|
|
|
else
|
|
|
|
qb.setTables(TABLE_HISTORY);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
case IMAGES_ID:
|
2011-12-08 08:37:21 -08:00
|
|
|
case IMAGES: {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Query is on images: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
if (match == IMAGES_ID) {
|
2011-12-16 15:11:09 -08:00
|
|
|
selection = DBUtils.concatenateWhere(selection, Images._ID + " = ?");
|
|
|
|
selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
|
2011-12-13 06:46:58 -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(Images.IS_DELETED + " = 0", selection);
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
qb.setProjectionMap(IMAGES_PROJECTION_MAP);
|
|
|
|
qb.setTables(TABLE_IMAGES);
|
|
|
|
|
|
|
|
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 });
|
|
|
|
schemaCursor.newRow().add(DATABASE_VERSION);
|
|
|
|
|
|
|
|
return schemaCursor;
|
|
|
|
}
|
|
|
|
|
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.");
|
2011-12-08 08:37:21 -08:00
|
|
|
Cursor cursor = qb.query(db, projection, selection, selectionArgs, null,
|
|
|
|
null, sortOrder, limit);
|
|
|
|
cursor.setNotificationUri(getContext().getContentResolver(),
|
|
|
|
BrowserContract.AUTHORITY_URI);
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentValues extractImageValues(ContentValues values, String url) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Extracting image values for URI: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
ContentValues imageValues = null;
|
|
|
|
|
|
|
|
if (values.containsKey(Bookmarks.FAVICON)) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Has favicon value on URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
imageValues = new ContentValues();
|
|
|
|
imageValues.put(Images.FAVICON,
|
|
|
|
values.getAsByteArray(Bookmarks.FAVICON));
|
|
|
|
values.remove(Bookmarks.FAVICON);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (values.containsKey(Bookmarks.THUMBNAIL)) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Has favicon value on URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
if (imageValues == null)
|
|
|
|
imageValues = new ContentValues();
|
|
|
|
|
|
|
|
imageValues.put(Images.THUMBNAIL,
|
|
|
|
values.getAsByteArray(Bookmarks.THUMBNAIL));
|
|
|
|
values.remove(Bookmarks.THUMBNAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageValues != null && url != null) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Has URL value");
|
2011-12-08 08:37:21 -08:00
|
|
|
imageValues.put(Images.URL, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getUrlCount(SQLiteDatabase db, String table, String url) {
|
|
|
|
Cursor c = db.query(table, new String[] { "COUNT(*)" },
|
2011-12-13 06:46:58 -08:00
|
|
|
URLColumns.URL + " = ?", new String[] { url }, null, null,
|
2011-12-08 08:37:21 -08:00
|
|
|
null);
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (c.moveToFirst())
|
|
|
|
count = c.getInt(0);
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-02-22 12:37:34 -08:00
|
|
|
/**
|
|
|
|
* Construct an update expression that will modify the positions of
|
|
|
|
* records in-place.
|
|
|
|
*
|
|
|
|
* The provided selectionArgs is expected to be an implicit mapping from
|
|
|
|
* GUID to new position.
|
|
|
|
*/
|
|
|
|
int updateBookmarkPositions(Uri uri, String[] guids) {
|
|
|
|
if (guids == null)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int guidsCount = guids.length;
|
|
|
|
if (guidsCount == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Each must appear twice: once in a CASE, and once in the IN clause.
|
|
|
|
String[] args = new String[guidsCount * 2];
|
|
|
|
System.arraycopy(guids, 0, args, 0, guidsCount);
|
|
|
|
System.arraycopy(guids, 0, args, guidsCount, guidsCount);
|
|
|
|
|
|
|
|
StringBuilder b = new StringBuilder("UPDATE " + TABLE_BOOKMARKS +
|
|
|
|
" SET " + Bookmarks.POSITION + " = CASE guid");
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < guids.length; ++i) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
b.append(" END WHERE " + Bookmarks.GUID + " IN (");
|
|
|
|
i = 1;
|
|
|
|
while (i++ < guidsCount) {
|
|
|
|
b.append("?, ");
|
|
|
|
}
|
|
|
|
b.append("?)");
|
|
|
|
getWritableDatabase(uri).execSQL(b.toString(), args);
|
|
|
|
|
|
|
|
// We can't easily get a modified count without calling something like changes().
|
|
|
|
return guidsCount;
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:10:18 -08:00
|
|
|
/**
|
|
|
|
* Construct an update expression that will modify the parents of any records
|
|
|
|
* that match.
|
|
|
|
*/
|
|
|
|
int updateBookmarkParents(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
trace("Updating bookmark parents of " + selection + " (" + selectionArgs[0] + ")");
|
|
|
|
String where = Bookmarks._ID + " IN (" +
|
|
|
|
" SELECT DISTINCT " + Bookmarks.PARENT +
|
|
|
|
" FROM " + TABLE_BOOKMARKS +
|
|
|
|
" WHERE " + selection + " )";
|
|
|
|
return getWritableDatabase(uri).update(TABLE_BOOKMARKS, values, where, selectionArgs);
|
|
|
|
}
|
|
|
|
|
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 SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
int updated = 0;
|
|
|
|
|
|
|
|
final String[] bookmarksProjection = new String[] {
|
|
|
|
Bookmarks._ID, // 0
|
|
|
|
Bookmarks.URL, // 1
|
|
|
|
};
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Quering bookmarks to update on URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
Cursor cursor = db.query(TABLE_BOOKMARKS, bookmarksProjection,
|
|
|
|
selection, selectionArgs, null, null, null);
|
|
|
|
|
|
|
|
try {
|
2012-02-14 17:10:18 -08:00
|
|
|
if (!values.containsKey(Bookmarks.DATE_MODIFIED))
|
|
|
|
values.put(Bookmarks.DATE_MODIFIED, System.currentTimeMillis());
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
boolean updatingUrl = values.containsKey(Bookmarks.URL);
|
|
|
|
String url = null;
|
|
|
|
|
|
|
|
if (updatingUrl)
|
|
|
|
url = values.getAsString(Bookmarks.URL);
|
|
|
|
|
|
|
|
ContentValues imageValues = extractImageValues(values, url);
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
long id = cursor.getLong(0);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating bookmark with ID: " + id);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
updated += db.update(TABLE_BOOKMARKS, values, "_id = ?",
|
|
|
|
new String[] { Long.toString(id) });
|
|
|
|
|
|
|
|
if (imageValues == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!updatingUrl) {
|
|
|
|
url = cursor.getString(1);
|
|
|
|
imageValues.put(Images.URL, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(url)) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating bookmark image for URL: " + url);
|
2011-12-13 06:46:58 -08:00
|
|
|
updateOrInsertImage(uri, imageValues, Images.URL + " = ?",
|
|
|
|
new String[] { url });
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
int updated = 0;
|
|
|
|
|
|
|
|
final String[] historyProjection = new String[] { History._ID, // 0
|
|
|
|
History.URL, // 1
|
|
|
|
};
|
|
|
|
|
|
|
|
Cursor cursor = db.query(TABLE_HISTORY, historyProjection, selection,
|
|
|
|
selectionArgs, null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
values.put(History.DATE_MODIFIED, System.currentTimeMillis());
|
|
|
|
|
|
|
|
boolean updatingUrl = values.containsKey(History.URL);
|
|
|
|
String url = null;
|
|
|
|
|
|
|
|
if (updatingUrl)
|
|
|
|
url = values.getAsString(History.URL);
|
|
|
|
|
|
|
|
ContentValues imageValues = extractImageValues(values, url);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
updated += db.update(TABLE_HISTORY, values, "_id = ?",
|
|
|
|
new String[] { Long.toString(id) });
|
|
|
|
|
|
|
|
if (imageValues == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!updatingUrl) {
|
|
|
|
url = cursor.getString(1);
|
|
|
|
imageValues.put(Images.URL, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(url)) {
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating history image for URL: " + url);
|
2011-12-13 06:46:58 -08:00
|
|
|
updateOrInsertImage(uri, imageValues, Images.URL + " = ?",
|
|
|
|
new String[] { url });
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
int updateOrInsertImage(Uri uri, ContentValues values, String selection,
|
2011-12-08 08:37:21 -08:00
|
|
|
String[] selectionArgs) {
|
2011-12-13 06:46:58 -08:00
|
|
|
return updateImage(uri, values, selection, selectionArgs,
|
|
|
|
true /* insert if needed */);
|
|
|
|
}
|
|
|
|
|
|
|
|
int updateExistingImage(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs) {
|
|
|
|
return updateImage(uri, values, selection, selectionArgs,
|
|
|
|
false /* only update, no insert */);
|
|
|
|
}
|
|
|
|
|
|
|
|
int updateImage(Uri uri, ContentValues values, String selection,
|
|
|
|
String[] selectionArgs, boolean insertIfNeeded) {
|
2011-12-08 08:37:21 -08:00
|
|
|
String url = values.getAsString(Images.URL);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("Updating image for URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
final SQLiteDatabase db = getWritableDatabase(uri);
|
|
|
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
|
|
|
// Thumbnails update on every page load. We don't want to flood
|
|
|
|
// sync with meaningless last modified date. Only update modified
|
|
|
|
// date when favicons bits change.
|
|
|
|
if (values.containsKey(Images.FAVICON) || values.containsKey(Images.FAVICON_URL))
|
|
|
|
values.put(Images.DATE_MODIFIED, now);
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
// Restore and update an existing image record marked as
|
|
|
|
// deleted if possible.
|
|
|
|
if (insertIfNeeded)
|
|
|
|
values.put(Images.IS_DELETED, 0);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Trying to update image for URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
int updated = db.update(TABLE_IMAGES, values, selection, selectionArgs);
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
if (updated == 0 && insertIfNeeded) {
|
2011-12-11 22:24:40 -08:00
|
|
|
// Generate GUID for new image, if one is not already provided.
|
|
|
|
if (!values.containsKey(Images.GUID)) {
|
2011-12-16 15:11:09 -08:00
|
|
|
values.put(Images.GUID, Utils.generateGuid());
|
2011-12-11 22:24:40 -08:00
|
|
|
}
|
2011-12-13 06:46:58 -08:00
|
|
|
|
2011-12-08 08:37:21 -08:00
|
|
|
values.put(Images.DATE_CREATED, now);
|
|
|
|
values.put(Images.DATE_MODIFIED, now);
|
|
|
|
|
2012-02-09 18:39:11 -08:00
|
|
|
trace("No update, inserting image for URL: " + url);
|
2011-12-08 08:37:21 -08:00
|
|
|
db.insert(TABLE_IMAGES, Images.FAVICON, values);
|
|
|
|
updated = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-27 10:46:35 -08:00
|
|
|
cleanupSomeDeletedRecords(uri, History.CONTENT_URI, TABLE_HISTORY);
|
|
|
|
return updateHistory(uri, values, selection, selectionArgs);
|
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)) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
cleanupSomeDeletedRecords(uri, Bookmarks.CONTENT_URI, TABLE_BOOKMARKS);
|
|
|
|
return updateBookmarks(uri, values, selection, selectionArgs);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
int deleteImages(Uri uri, String selection, String[] selectionArgs) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Deleting images 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_IMAGES, selection, null);
|
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 images 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);
|
|
|
|
|
|
|
|
cleanupSomeDeletedRecords(uri, Images.CONTENT_URI, TABLE_IMAGES);
|
|
|
|
return updateExistingImage(uri, values, selection, null);
|
2011-12-13 06:46:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int deleteUnusedImages(Uri uri) {
|
2012-02-09 18:39:11 -08:00
|
|
|
debug("Deleting all unused images for URI: " + uri);
|
2011-12-08 08:37:21 -08:00
|
|
|
|
|
|
|
String selection = Images.URL + " NOT IN (SELECT " + Bookmarks.URL +
|
2011-12-13 06:46:58 -08:00
|
|
|
" FROM " + TABLE_BOOKMARKS + " WHERE " + Bookmarks.URL + " IS NOT NULL AND " +
|
2011-12-13 11:47:40 -08:00
|
|
|
qualifyColumn(TABLE_BOOKMARKS, Bookmarks.IS_DELETED) + " = 0) AND " +
|
2011-12-13 06:46:58 -08:00
|
|
|
Images.URL + " NOT IN (SELECT " + History.URL + " FROM " + TABLE_HISTORY +
|
|
|
|
" WHERE " + History.URL + " IS NOT NULL AND " +
|
2011-12-13 11:47:40 -08:00
|
|
|
qualifyColumn(TABLE_HISTORY, History.IS_DELETED) + " = 0)";
|
2011-12-08 08:37:21 -08:00
|
|
|
|
2011-12-13 06:46:58 -08:00
|
|
|
return deleteImages(uri, selection, null);
|
2011-12-08 08:37:21 -08:00
|
|
|
}
|
2011-12-11 22:24:40 -08:00
|
|
|
}
|