Skip to content
Snippets Groups Projects
Commit 54ddce6f authored by Margaret Leibovic's avatar Margaret Leibovic
Browse files

Bug 741590 - Create updateOrInsertHistory method for BrowserProvider. r=rnewman

parent 21b12891
No related branches found
No related tags found
No related merge requests found
......@@ -60,6 +60,8 @@ public class BrowserContract {
public static final String PARAM_IS_SYNC = "sync";
public static final String PARAM_SHOW_DELETED = "show_deleted";
public static final String PARAM_IS_TEST = "test";
public static final String PARAM_INSERT_IF_NEEDED = "insert_if_needed";
public static final String PARAM_INCREMENT_VISITS = "increment_visits";
public interface CommonColumns {
public static final String _ID = "_id";
......
......@@ -1041,6 +1041,16 @@ public class BrowserProvider extends ContentProvider {
return !TextUtils.isEmpty(showDeleted);
}
private boolean shouldUpdateOrInsert(Uri uri) {
String insertIfNeeded = uri.getQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED);
return Boolean.parseBoolean(insertIfNeeded);
}
private boolean shouldIncrementVisits(Uri uri) {
String incrementVisits = uri.getQueryParameter(BrowserContract.PARAM_INCREMENT_VISITS);
return Boolean.parseBoolean(incrementVisits);
}
@Override
public boolean onCreate() {
debug("Creating BrowserProvider");
......@@ -1256,29 +1266,7 @@ public class BrowserProvider extends ContentProvider {
case HISTORY: {
trace("Insert on HISTORY: " + uri);
long now = System.currentTimeMillis();
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);
ContentValues imageValues = extractImageValues(values,
values.getAsString(History.URL));
if (imageValues != null) {
debug("Inserting history image for URL: " + url);
updateOrInsertImage(uri, imageValues, Images.URL + " = ?",
new String[] { url });
}
debug("Inserting history in database with URL: " + url);
id = db.insertOrThrow(TABLE_HISTORY, History.VISITS, values);
id = insertHistory(uri, values);
break;
}
......@@ -1391,7 +1379,10 @@ public class BrowserProvider extends ContentProvider {
// fall through
case HISTORY: {
debug("Updating history: " + uri);
updated = updateHistory(uri, values, selection, selectionArgs);
if (shouldUpdateOrInsert(uri))
updated = updateOrInsertHistory(uri, values, selection, selectionArgs);
else
updated = updateHistory(uri, values, selection, selectionArgs);
break;
}
......@@ -1825,6 +1816,49 @@ public class BrowserProvider extends ContentProvider {
return updated;
}
long insertHistory(Uri uri, ContentValues values) {
final SQLiteDatabase db = getWritableDatabase(uri);
long now = System.currentTimeMillis();
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);
ContentValues imageValues = extractImageValues(values, url);
if (imageValues != null) {
debug("Inserting history image for URL: " + url);
updateOrInsertImage(uri, imageValues, Images.URL + " = ?",
new String[] { url });
}
debug("Inserting history in database with URL: " + url);
return db.insertOrThrow(TABLE_HISTORY, History.VISITS, values);
}
int updateOrInsertHistory(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int updated = updateHistory(uri, values, selection, selectionArgs);
if (updated > 0)
return updated;
// Insert a new entry if necessary
if (!values.containsKey(History.VISITS))
values.put(History.VISITS, 1);
if (!values.containsKey(History.TITLE))
values.put(History.TITLE, values.getAsString(History.URL));
insertHistory(uri, values);
// Return 0 if we added a new row
return 0;
}
int updateHistory(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
trace("Updating history on URI: " + uri);
......@@ -1832,8 +1866,10 @@ public class BrowserProvider extends ContentProvider {
final SQLiteDatabase db = getWritableDatabase(uri);
int updated = 0;
final String[] historyProjection = new String[] { History._ID, // 0
History.URL, // 1
final String[] historyProjection = new String[] {
History._ID, // 0
History.URL, // 1
History.VISITS // 2
};
Cursor cursor = db.query(TABLE_HISTORY, historyProjection, selection,
......@@ -1855,6 +1891,14 @@ public class BrowserProvider extends ContentProvider {
trace("Updating history entry with ID: " + id);
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));
}
updated += db.update(TABLE_HISTORY, values, "_id = ?",
new String[] { Long.toString(id) });
......
......@@ -91,6 +91,7 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
private final Uri mImagesUriWithProfile;
private final Uri mCombinedUriWithProfile;
private final Uri mDeletedHistoryUriWithProfile;
private final Uri mUpdateHistoryUriWithProfile;
private static final String[] DEFAULT_BOOKMARK_COLUMNS =
new String[] { Bookmarks._ID,
......@@ -115,6 +116,10 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
mDeletedHistoryUriWithProfile = mHistoryUriWithProfile.buildUpon().
appendQueryParameter(BrowserContract.PARAM_SHOW_DELETED, "1").build();
mUpdateHistoryUriWithProfile = mHistoryUriWithProfile.buildUpon().
appendQueryParameter(BrowserContract.PARAM_INCREMENT_VISITS, "true").
appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true").build();
}
// Invalidate cached data
......@@ -231,50 +236,23 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
}
public void updateVisitedHistory(ContentResolver cr, String uri) {
long now = System.currentTimeMillis();
Cursor cursor = null;
try {
final String[] projection = new String[] {
History._ID, // 0
History.VISITS, // 1
};
cursor = cr.query(mDeletedHistoryUriWithProfile,
projection,
History.URL + " = ?",
new String[] { uri },
null);
if (cursor.moveToFirst()) {
ContentValues values = new ContentValues();
values.put(History.VISITS, cursor.getInt(1) + 1);
values.put(History.DATE_LAST_VISITED, now);
// Restore deleted record if possible
values.put(History.IS_DELETED, 0);
Uri historyUri = ContentUris.withAppendedId(History.CONTENT_URI, cursor.getLong(0));
cr.update(appendProfile(historyUri), values, null, null);
} else {
// Ensure we don't blow up our database with too
// many history items.
truncateHistory(cr);
ContentValues values = new ContentValues();
ContentValues values = new ContentValues();
values.put(History.URL, uri);
values.put(History.DATE_LAST_VISITED, System.currentTimeMillis());
values.put(History.IS_DELETED, 0);
values.put(History.URL, uri);
values.put(History.VISITS, 1);
values.put(History.DATE_LAST_VISITED, now);
values.put(History.TITLE, uri);
// This will insert a new history entry if one for this URL
// doesn't already exist
int updated = cr.update(mUpdateHistoryUriWithProfile,
values,
History.URL + " = ?",
new String[] { uri });
cr.insert(mHistoryUriWithProfile, values);
}
} finally {
if (cursor != null)
cursor.close();
}
// If we added a new row, ensure we don't blow up our database
// with too many history items.
if (updated == 0)
truncateHistory(cr);
}
public void updateHistoryTitle(ContentResolver cr, String uri, String title) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment