You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
7.2 KiB
220 lines
7.2 KiB
/* |
|
* Copyright 2014 A.C.R. Development |
|
*/ |
|
package acr.browser.lightning.database; |
|
|
|
import android.content.ContentValues; |
|
import android.content.Context; |
|
import android.database.Cursor; |
|
import android.database.sqlite.SQLiteDatabase; |
|
import android.database.sqlite.SQLiteOpenHelper; |
|
import android.support.annotation.NonNull; |
|
import android.support.annotation.Nullable; |
|
|
|
import java.util.ArrayList; |
|
import java.util.List; |
|
import java.util.concurrent.Executor; |
|
import java.util.concurrent.Executors; |
|
|
|
import javax.inject.Inject; |
|
import javax.inject.Singleton; |
|
|
|
import acr.browser.lightning.R; |
|
|
|
@Singleton |
|
public class HistoryDatabase extends SQLiteOpenHelper { |
|
|
|
// All Static variables |
|
// Database Version |
|
private static final int DATABASE_VERSION = 2; |
|
|
|
// Database Name |
|
private static final String DATABASE_NAME = "historyManager"; |
|
|
|
// HistoryItems table name |
|
private static final String TABLE_HISTORY = "history"; |
|
|
|
// HistoryItems Table Columns names |
|
private static final String KEY_ID = "id"; |
|
private static final String KEY_URL = "url"; |
|
private static final String KEY_TITLE = "title"; |
|
private static final String KEY_TIME_VISITED = "time"; |
|
|
|
private SQLiteDatabase mDatabase; |
|
|
|
private Executor mIOThread = Executors.newSingleThreadExecutor(); |
|
|
|
@Inject |
|
public HistoryDatabase(Context context) { |
|
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); |
|
mDatabase = this.getWritableDatabase(); |
|
} |
|
|
|
// Creating Tables |
|
@Override |
|
public void onCreate(SQLiteDatabase db) { |
|
String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + '(' + KEY_ID |
|
+ " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT," |
|
+ KEY_TIME_VISITED + " INTEGER" + ')'; |
|
db.execSQL(CREATE_HISTORY_TABLE); |
|
} |
|
|
|
// Upgrading database |
|
@Override |
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
|
// Drop older table if it exists |
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY); |
|
// Create tables again |
|
onCreate(db); |
|
} |
|
|
|
public synchronized void deleteHistory() { |
|
mDatabase.delete(TABLE_HISTORY, null, null); |
|
mDatabase.close(); |
|
mDatabase = this.getWritableDatabase(); |
|
} |
|
|
|
private synchronized boolean isClosed() { |
|
return mDatabase == null || !mDatabase.isOpen(); |
|
} |
|
|
|
public Executor getIOThread() { |
|
return mIOThread; |
|
} |
|
|
|
@Override |
|
public synchronized void close() { |
|
if (mDatabase != null) { |
|
mDatabase.close(); |
|
mDatabase = null; |
|
} |
|
super.close(); |
|
} |
|
|
|
private void openIfNecessary() { |
|
if (isClosed()) { |
|
mDatabase = this.getWritableDatabase(); |
|
} |
|
} |
|
|
|
public synchronized void deleteHistoryItem(String url) { |
|
openIfNecessary(); |
|
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url}); |
|
} |
|
|
|
public synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) { |
|
openIfNecessary(); |
|
ContentValues values = new ContentValues(); |
|
values.put(KEY_TITLE, title == null ? "" : title); |
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
Cursor q = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL}, |
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); |
|
if (q.getCount() > 0) { |
|
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); |
|
} else { |
|
addHistoryItem(new HistoryItem(url, title == null ? "" : title)); |
|
} |
|
q.close(); |
|
} |
|
|
|
private synchronized void addHistoryItem(@NonNull HistoryItem item) { |
|
openIfNecessary(); |
|
ContentValues values = new ContentValues(); |
|
values.put(KEY_URL, item.getUrl()); |
|
values.put(KEY_TITLE, item.getTitle()); |
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); |
|
mDatabase.insert(TABLE_HISTORY, null, values); |
|
} |
|
|
|
synchronized String getHistoryItem(String url) { |
|
openIfNecessary(); |
|
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE}, |
|
KEY_URL + " = ?", new String[]{url}, null, null, null, null); |
|
String m = null; |
|
if (cursor != null) { |
|
cursor.moveToFirst(); |
|
m = cursor.getString(0); |
|
|
|
cursor.close(); |
|
} |
|
return m; |
|
} |
|
|
|
public synchronized List<HistoryItem> findItemsContaining(@Nullable String search) { |
|
openIfNecessary(); |
|
List<HistoryItem> itemList = new ArrayList<>(5); |
|
if (search == null) { |
|
return itemList; |
|
} |
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%" |
|
+ search + "%' OR " + KEY_URL + " LIKE '%" + search + "%' " + "ORDER BY " |
|
+ KEY_TIME_VISITED + " DESC LIMIT 5"; |
|
Cursor cursor = mDatabase.rawQuery(selectQuery, null); |
|
|
|
int n = 0; |
|
if (cursor.moveToFirst()) { |
|
do { |
|
HistoryItem item = new HistoryItem(); |
|
item.setUrl(cursor.getString(1)); |
|
item.setTitle(cursor.getString(2)); |
|
item.setImageId(R.drawable.ic_history); |
|
itemList.add(item); |
|
n++; |
|
} while (cursor.moveToNext() && n < 5); |
|
} |
|
cursor.close(); |
|
return itemList; |
|
} |
|
|
|
public synchronized List<HistoryItem> getLastHundredItems() { |
|
openIfNecessary(); |
|
List<HistoryItem> itemList = new ArrayList<>(100); |
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED |
|
+ " DESC"; |
|
|
|
Cursor cursor = mDatabase.rawQuery(selectQuery, null); |
|
int counter = 0; |
|
if (cursor.moveToFirst()) { |
|
do { |
|
HistoryItem item = new HistoryItem(); |
|
item.setUrl(cursor.getString(1)); |
|
item.setTitle(cursor.getString(2)); |
|
item.setImageId(R.drawable.ic_history); |
|
itemList.add(item); |
|
counter++; |
|
} while (cursor.moveToNext() && counter < 100); |
|
} |
|
cursor.close(); |
|
return itemList; |
|
} |
|
|
|
public synchronized List<HistoryItem> getAllHistoryItems() { |
|
openIfNecessary(); |
|
List<HistoryItem> itemList = new ArrayList<>(); |
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED |
|
+ " DESC"; |
|
|
|
Cursor cursor = mDatabase.rawQuery(selectQuery, null); |
|
|
|
if (cursor.moveToFirst()) { |
|
do { |
|
HistoryItem item = new HistoryItem(); |
|
item.setUrl(cursor.getString(1)); |
|
item.setTitle(cursor.getString(2)); |
|
item.setImageId(R.drawable.ic_history); |
|
itemList.add(item); |
|
} while (cursor.moveToNext()); |
|
} |
|
cursor.close(); |
|
return itemList; |
|
} |
|
|
|
public synchronized int getHistoryItemsCount() { |
|
openIfNecessary(); |
|
String countQuery = "SELECT * FROM " + TABLE_HISTORY; |
|
Cursor cursor = mDatabase.rawQuery(countQuery, null); |
|
int n = cursor.getCount(); |
|
cursor.close(); |
|
return n; |
|
} |
|
}
|
|
|