|
|
@ -8,6 +8,8 @@ import android.content.Context; |
|
|
|
import android.database.Cursor; |
|
|
|
import android.database.Cursor; |
|
|
|
import android.database.sqlite.SQLiteDatabase; |
|
|
|
import android.database.sqlite.SQLiteDatabase; |
|
|
|
import android.database.sqlite.SQLiteOpenHelper; |
|
|
|
import android.database.sqlite.SQLiteOpenHelper; |
|
|
|
|
|
|
|
import android.support.annotation.NonNull; |
|
|
|
|
|
|
|
import android.support.annotation.Nullable; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
@ -38,10 +40,6 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
private SQLiteDatabase mDatabase; |
|
|
|
private SQLiteDatabase mDatabase; |
|
|
|
|
|
|
|
|
|
|
|
private static HistoryDatabase mInstance; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mLock; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Inject |
|
|
|
@Inject |
|
|
|
public HistoryDatabase(Context context) { |
|
|
|
public HistoryDatabase(Context context) { |
|
|
|
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); |
|
|
|
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); |
|
|
@ -66,23 +64,21 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
onCreate(db); |
|
|
|
onCreate(db); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void deleteHistory() { |
|
|
|
public synchronized void deleteHistory() { |
|
|
|
mDatabase.delete(TABLE_HISTORY, null, null); |
|
|
|
mDatabase.delete(TABLE_HISTORY, null, null); |
|
|
|
mDatabase.close(); |
|
|
|
mDatabase.close(); |
|
|
|
mDatabase = this.getWritableDatabase(); |
|
|
|
mDatabase = this.getWritableDatabase(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean isClosed() { |
|
|
|
private synchronized boolean isClosed() { |
|
|
|
return mDatabase == null || !mDatabase.isOpen(); |
|
|
|
return mDatabase == null || !mDatabase.isOpen(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public synchronized void close() { |
|
|
|
public synchronized void close() { |
|
|
|
if (!mLock) { |
|
|
|
if (mDatabase != null) { |
|
|
|
if (mDatabase != null) { |
|
|
|
mDatabase.close(); |
|
|
|
mDatabase.close(); |
|
|
|
mDatabase = null; |
|
|
|
mDatabase = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
super.close(); |
|
|
|
super.close(); |
|
|
|
} |
|
|
|
} |
|
|
@ -94,42 +90,35 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public synchronized void deleteHistoryItem(String url) { |
|
|
|
public synchronized void deleteHistoryItem(String url) { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url}); |
|
|
|
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url}); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public synchronized void visitHistoryItem(String url, String title) { |
|
|
|
public synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
ContentValues values = new ContentValues(); |
|
|
|
ContentValues values = new ContentValues(); |
|
|
|
values.put(KEY_TITLE, title); |
|
|
|
values.put(KEY_TITLE, title == null ? "" : title); |
|
|
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
|
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
|
|
Cursor q = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL}, |
|
|
|
Cursor q = mDatabase.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 (q.getCount() > 0) { |
|
|
|
if (q.getCount() > 0) { |
|
|
|
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); |
|
|
|
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
addHistoryItem(new HistoryItem(url, title)); |
|
|
|
addHistoryItem(new HistoryItem(url, title == null ? "" : title)); |
|
|
|
} |
|
|
|
} |
|
|
|
q.close(); |
|
|
|
q.close(); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private synchronized void addHistoryItem(HistoryItem item) { |
|
|
|
private synchronized void addHistoryItem(@NonNull HistoryItem item) { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
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); |
|
|
|
mDatabase.insert(TABLE_HISTORY, null, values); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String getHistoryItem(String url) { |
|
|
|
synchronized String getHistoryItem(String url) { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
Cursor cursor = mDatabase.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, null); |
|
|
|
KEY_URL + " = ?", new String[]{url}, null, null, null, null); |
|
|
@ -140,14 +129,15 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
cursor.close(); |
|
|
|
cursor.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
return m; |
|
|
|
return m; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<HistoryItem> findItemsContaining(String search) { |
|
|
|
public synchronized List<HistoryItem> findItemsContaining(@Nullable String search) { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(5); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(5); |
|
|
|
|
|
|
|
if (search == null) { |
|
|
|
|
|
|
|
return itemList; |
|
|
|
|
|
|
|
} |
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%" |
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%" |
|
|
|
+ search + "%' OR " + KEY_URL + " LIKE '%" + search + "%' " + "ORDER BY " |
|
|
|
+ search + "%' OR " + KEY_URL + " LIKE '%" + search + "%' " + "ORDER BY " |
|
|
|
+ KEY_TIME_VISITED + " DESC LIMIT 5"; |
|
|
|
+ KEY_TIME_VISITED + " DESC LIMIT 5"; |
|
|
@ -165,12 +155,10 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
} while (cursor.moveToNext() && n < 5); |
|
|
|
} while (cursor.moveToNext() && n < 5); |
|
|
|
} |
|
|
|
} |
|
|
|
cursor.close(); |
|
|
|
cursor.close(); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
return itemList; |
|
|
|
return itemList; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<HistoryItem> getLastHundredItems() { |
|
|
|
public synchronized List<HistoryItem> getLastHundredItems() { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(100); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(100); |
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED |
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED |
|
|
@ -189,12 +177,10 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
} while (cursor.moveToNext() && counter < 100); |
|
|
|
} while (cursor.moveToNext() && counter < 100); |
|
|
|
} |
|
|
|
} |
|
|
|
cursor.close(); |
|
|
|
cursor.close(); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
return itemList; |
|
|
|
return itemList; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<HistoryItem> getAllHistoryItems() { |
|
|
|
public synchronized List<HistoryItem> getAllHistoryItems() { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(); |
|
|
|
List<HistoryItem> itemList = new ArrayList<>(); |
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED |
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED |
|
|
@ -212,18 +198,15 @@ public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
} while (cursor.moveToNext()); |
|
|
|
} while (cursor.moveToNext()); |
|
|
|
} |
|
|
|
} |
|
|
|
cursor.close(); |
|
|
|
cursor.close(); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
return itemList; |
|
|
|
return itemList; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getHistoryItemsCount() { |
|
|
|
public synchronized int getHistoryItemsCount() { |
|
|
|
mLock = true; |
|
|
|
|
|
|
|
openIfNecessary(); |
|
|
|
openIfNecessary(); |
|
|
|
String countQuery = "SELECT * FROM " + TABLE_HISTORY; |
|
|
|
String countQuery = "SELECT * FROM " + TABLE_HISTORY; |
|
|
|
Cursor cursor = mDatabase.rawQuery(countQuery, null); |
|
|
|
Cursor cursor = mDatabase.rawQuery(countQuery, null); |
|
|
|
int n = cursor.getCount(); |
|
|
|
int n = cursor.getCount(); |
|
|
|
cursor.close(); |
|
|
|
cursor.close(); |
|
|
|
mLock = false; |
|
|
|
|
|
|
|
return n; |
|
|
|
return n; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|