|
|
@ -68,15 +68,6 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
onCreate(db); |
|
|
|
onCreate(db); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public synchronized void close() { |
|
|
|
|
|
|
|
if (mDatabase != null) { |
|
|
|
|
|
|
|
mDatabase.close(); |
|
|
|
|
|
|
|
mDatabase = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
super.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
private static HistoryItem fromCursor(@NonNull Cursor cursor) { |
|
|
|
private static HistoryItem fromCursor(@NonNull Cursor cursor) { |
|
|
|
HistoryItem historyItem = new HistoryItem(); |
|
|
|
HistoryItem historyItem = new HistoryItem(); |
|
|
@ -89,7 +80,7 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
private SQLiteDatabase openIfNecessary() { |
|
|
|
private SQLiteDatabase lazyDatabase() { |
|
|
|
if (mDatabase == null || !mDatabase.isOpen()) { |
|
|
|
if (mDatabase == null || !mDatabase.isOpen()) { |
|
|
|
mDatabase = this.getWritableDatabase(); |
|
|
|
mDatabase = this.getWritableDatabase(); |
|
|
|
} |
|
|
|
} |
|
|
@ -98,30 +89,26 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
synchronized void deleteHistory() { |
|
|
|
synchronized void deleteHistory() { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
lazyDatabase().delete(TABLE_HISTORY, null, null); |
|
|
|
mDatabase.delete(TABLE_HISTORY, null, null); |
|
|
|
lazyDatabase().close(); |
|
|
|
mDatabase.close(); |
|
|
|
|
|
|
|
mDatabase = this.getWritableDatabase(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
synchronized void deleteHistoryItem(@NonNull String url) { |
|
|
|
synchronized void deleteHistoryItem(@NonNull String url) { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
lazyDatabase().delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url}); |
|
|
|
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) { |
|
|
|
synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
|
|
|
|
ContentValues values = new ContentValues(); |
|
|
|
ContentValues values = new ContentValues(); |
|
|
|
values.put(KEY_TITLE, title == null ? "" : title); |
|
|
|
values.put(KEY_TITLE, title == null ? "" : title); |
|
|
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
|
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
|
|
|
|
|
|
|
|
|
|
Cursor cursor = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL}, |
|
|
|
Cursor cursor = lazyDatabase().query(false, TABLE_HISTORY, new String[]{KEY_URL}, |
|
|
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); |
|
|
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); |
|
|
|
|
|
|
|
|
|
|
|
if (cursor.getCount() > 0) { |
|
|
|
if (cursor.getCount() > 0) { |
|
|
|
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); |
|
|
|
lazyDatabase().update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
addHistoryItem(new HistoryItem(url, title == null ? "" : title)); |
|
|
|
addHistoryItem(new HistoryItem(url, title == null ? "" : title)); |
|
|
|
} |
|
|
|
} |
|
|
@ -131,19 +118,17 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
private synchronized void addHistoryItem(@NonNull HistoryItem item) { |
|
|
|
private synchronized void addHistoryItem(@NonNull HistoryItem item) { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
|
|
|
|
ContentValues values = new ContentValues(); |
|
|
|
ContentValues values = new ContentValues(); |
|
|
|
values.put(KEY_URL, item.getUrl()); |
|
|
|
values.put(KEY_URL, item.getUrl()); |
|
|
|
values.put(KEY_TITLE, item.getTitle()); |
|
|
|
values.put(KEY_TITLE, item.getTitle()); |
|
|
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
|
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
|
|
mDatabase.insert(TABLE_HISTORY, null, values); |
|
|
|
lazyDatabase().insert(TABLE_HISTORY, null, values); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
synchronized String getHistoryItem(@NonNull String url) { |
|
|
|
synchronized String getHistoryItem(@NonNull String url) { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE}, |
|
|
|
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE}, |
|
|
|
|
|
|
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); |
|
|
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); |
|
|
|
String m = null; |
|
|
|
String m = null; |
|
|
|
if (cursor != null) { |
|
|
|
if (cursor != null) { |
|
|
@ -158,7 +143,6 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
synchronized List<HistoryItem> findItemsContaining(@Nullable String search) { |
|
|
|
synchronized List<HistoryItem> findItemsContaining(@Nullable String search) { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
|
|
|
|
List<HistoryItem> itemList = new ArrayList<>(5); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(5); |
|
|
|
if (search == null) { |
|
|
|
if (search == null) { |
|
|
|
return itemList; |
|
|
|
return itemList; |
|
|
@ -166,7 +150,7 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
search = '%' + search + '%'; |
|
|
|
search = '%' + search + '%'; |
|
|
|
|
|
|
|
|
|
|
|
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?", |
|
|
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?", |
|
|
|
new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5"); |
|
|
|
new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5"); |
|
|
|
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) { |
|
|
|
while (cursor.moveToNext()) { |
|
|
@ -181,9 +165,8 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
synchronized List<HistoryItem> getLastHundredItems() { |
|
|
|
synchronized List<HistoryItem> getLastHundredItems() { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
|
|
|
|
List<HistoryItem> itemList = new ArrayList<>(100); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(100); |
|
|
|
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC", "100"); |
|
|
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC", "100"); |
|
|
|
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) { |
|
|
|
while (cursor.moveToNext()) { |
|
|
|
itemList.add(fromCursor(cursor)); |
|
|
|
itemList.add(fromCursor(cursor)); |
|
|
@ -197,10 +180,9 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
synchronized List<HistoryItem> getAllHistoryItems() { |
|
|
|
synchronized List<HistoryItem> getAllHistoryItems() { |
|
|
|
mDatabase = openIfNecessary(); |
|
|
|
|
|
|
|
List<HistoryItem> itemList = new ArrayList<>(); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC"); |
|
|
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC"); |
|
|
|
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) { |
|
|
|
while (cursor.moveToNext()) { |
|
|
|
itemList.add(fromCursor(cursor)); |
|
|
|
itemList.add(fromCursor(cursor)); |
|
|
|