gecko/mobile/android/base/db/FormHistoryProvider.java.in

172 lines
5.7 KiB
Java

/* 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/. */
#filter substitution
package @ANDROID_PACKAGE_NAME@.db;
import java.io.File;
import java.io.IOException;
import java.lang.IllegalArgumentException;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Random;
import org.mozilla.gecko.GeckoApp;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.GeckoEvent;
import org.mozilla.gecko.GeckoEventListener;
import org.mozilla.gecko.db.BrowserContract.CommonColumns;
import org.mozilla.gecko.db.DBUtils;
import org.mozilla.gecko.db.BrowserContract.FormHistory;
import org.mozilla.gecko.db.BrowserContract.DeletedFormHistory;
import org.mozilla.gecko.db.BrowserContract.SyncColumns;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.sqlite.SQLiteBridge;
import org.mozilla.gecko.sqlite.SQLiteBridgeException;
import org.mozilla.gecko.sync.Utils;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
public class FormHistoryProvider extends GeckoProvider {
static final String TABLE_FORM_HISTORY = "moz_formhistory";
static final String TABLE_DELETED_FORM_HISTORY = "moz_deleted_formhistory";
private static final int FORM_HISTORY = 100;
private static final int DELETED_FORM_HISTORY = 101;
private static final UriMatcher URI_MATCHER;
private static HashMap<String, String> FORM_HISTORY_PROJECTION_MAP;
private static HashMap<String, String> DELETED_FORM_HISTORY_PROJECTION_MAP;
// This should be kept in sync with the db version in toolkit/components/satchel/nsFormHistory.js
private static int DB_VERSION = 4;
private static String DB_FILENAME = "formhistory.sqlite";
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "formhistory", FORM_HISTORY);
URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "deleted-formhistory", DELETED_FORM_HISTORY);
FORM_HISTORY_PROJECTION_MAP = new HashMap<String, String>();
DELETED_FORM_HISTORY_PROJECTION_MAP = new HashMap<String, String>();
}
@Override
public boolean onCreate() {
setLogTag("FormHistoryProvider");
setDBName(DB_FILENAME);
setDBVersion(DB_VERSION);
return super.onCreate();
}
@Override
public String getType(Uri uri) {
final int match = URI_MATCHER.match(uri);
switch (match) {
case FORM_HISTORY:
return FormHistory.CONTENT_TYPE;
case DELETED_FORM_HISTORY:
return DeletedFormHistory.CONTENT_TYPE;
default:
throw new UnsupportedOperationException("Unknown type " + uri);
}
}
public String getTable(Uri uri) {
String table = null;
final int match = URI_MATCHER.match(uri);
switch (match) {
case DELETED_FORM_HISTORY:
table = TABLE_DELETED_FORM_HISTORY;
break;
case FORM_HISTORY:
table = TABLE_FORM_HISTORY;
break;
default:
throw new UnsupportedOperationException("Unknown table " + uri);
}
return table;
}
@Override
public String getSortOrder(Uri uri, String aRequested) {
if (!TextUtils.isEmpty(aRequested))
return aRequested;
return null;
}
public void setupDefaults(Uri uri, ContentValues values) {
int match = URI_MATCHER.match(uri);
long now = System.currentTimeMillis();
switch (match) {
case DELETED_FORM_HISTORY:
values.put(DeletedFormHistory.TIME_DELETED, now);
// Deleted entries must contain a guid
if (!values.containsKey(FormHistory.GUID)) {
throw new IllegalArgumentException("Must provide a GUID for a deleted form history");
}
break;
case FORM_HISTORY:
// Generate GUID for new entry. Don't override specified GUIDs.
if (!values.containsKey(FormHistory.GUID)) {
String guid = Utils.generateGuid();
values.put(FormHistory.GUID, guid);
}
break;
default:
throw new UnsupportedOperationException("Unknown insert URI " + uri);
}
}
public void initGecko() {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormHistory:Init", null));
}
@Override
public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) {
if (!values.containsKey(FormHistory.GUID)) {
return;
}
String guid = values.getAsString(FormHistory.GUID);
String where = BrowserContract.DeletedFormHistory.GUID + " = ?";
String[] args = new String[] { guid };
if (guid == null) {
where = BrowserContract.DeletedFormHistory.GUID + " IS NULL";
args = null;
}
try {
db.delete(TABLE_DELETED_FORM_HISTORY, where, args);
} catch(SQLiteBridgeException ex) {
Log.w(getLogTag(), "Error removing entry with GUID " + guid, ex);
}
}
@Override
public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { }
@Override
public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { }
}