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.
209 lines
5.6 KiB
209 lines
5.6 KiB
![]()
11 years ago
|
/*
|
||
|
* Copyright 2014 A.C.R. Development
|
||
|
*/
|
||
|
package acr.browser.lightning;
|
||
|
|
||
|
import android.content.ContentValues;
|
||
|
import android.content.Context;
|
||
|
import android.database.Cursor;
|
||
|
import android.database.sqlite.SQLiteDatabase;
|
||
|
import android.database.sqlite.SQLiteOpenHelper;
|
||
|
|
||
![]()
10 years ago
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
![]()
10 years ago
|
public class HistoryDatabaseHandler extends SQLiteOpenHelper {
|
||
![]()
11 years ago
|
|
||
|
// All Static variables
|
||
|
// Database Version
|
||
|
private static final int DATABASE_VERSION = 1;
|
||
|
|
||
|
// Database Name
|
||
|
public static final String DATABASE_NAME = "historyManager";
|
||
|
|
||
|
// HistoryItems table name
|
||
|
public static final String TABLE_HISTORY = "history";
|
||
|
|
||
|
// HistoryItems Table Columns names
|
||
|
public static final String KEY_ID = "id";
|
||
![]()
10 years ago
|
|
||
![]()
11 years ago
|
public static final String KEY_URL = "url";
|
||
![]()
10 years ago
|
|
||
![]()
11 years ago
|
public static final String KEY_TITLE = "title";
|
||
![]()
10 years ago
|
|
||
![]()
11 years ago
|
public static SQLiteDatabase mDatabase;
|
||
|
|
||
![]()
10 years ago
|
public HistoryDatabaseHandler(Context context) {
|
||
![]()
10 years ago
|
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
|
||
![]()
11 years ago
|
mDatabase = this.getWritableDatabase();
|
||
|
}
|
||
|
|
||
|
// Creating Tables
|
||
|
@Override
|
||
|
public void onCreate(SQLiteDatabase db) {
|
||
![]()
10 years ago
|
String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "(" + KEY_ID
|
||
|
+ " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT" + ")";
|
||
![]()
11 years ago
|
db.execSQL(CREATE_HISTORY_TABLE);
|
||
|
}
|
||
|
|
||
|
// Upgrading database
|
||
|
@Override
|
||
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||
|
// Drop older table if existed
|
||
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
|
||
|
|
||
|
// Create tables again
|
||
|
onCreate(db);
|
||
|
}
|
||
![]()
10 years ago
|
|
||
|
public boolean isOpen() {
|
||
|
if (mDatabase != null) {
|
||
![]()
11 years ago
|
return mDatabase.isOpen();
|
||
![]()
10 years ago
|
} else {
|
||
![]()
11 years ago
|
return false;
|
||
![]()
10 years ago
|
}
|
||
![]()
11 years ago
|
}
|
||
|
|
||
|
@Override
|
||
|
public synchronized void close() {
|
||
![]()
10 years ago
|
if (mDatabase != null) {
|
||
![]()
11 years ago
|
mDatabase.close();
|
||
![]()
10 years ago
|
}
|
||
![]()
11 years ago
|
super.close();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* All CRUD(Create, Read, Update, Delete) Operations
|
||
|
*/
|
||
|
|
||
|
public synchronized void delete(String url) {
|
||
|
String n = getHistoryItem(url);
|
||
![]()
10 years ago
|
if (n != null) {
|
||
![]()
11 years ago
|
deleteHistoryItem(n);
|
||
![]()
10 years ago
|
}
|
||
![]()
11 years ago
|
}
|
||
![]()
10 years ago
|
|
||
|
public synchronized void visitHistoryItem(String url, String title){
|
||
|
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[] { url });
|
||
|
ContentValues values = new ContentValues();
|
||
|
values.put(KEY_URL, url);
|
||
|
values.put(KEY_TITLE, title);
|
||
|
mDatabase.insert(TABLE_HISTORY, null, values);
|
||
|
}
|
||
![]()
11 years ago
|
|
||
|
// Adding new item
|
||
|
public synchronized void addHistoryItem(HistoryItem item) {
|
||
|
ContentValues values = new ContentValues();
|
||
![]()
10 years ago
|
values.put(KEY_URL, item.getUrl());
|
||
|
values.put(KEY_TITLE, item.getTitle());
|
||
![]()
11 years ago
|
mDatabase.insert(TABLE_HISTORY, null, values);
|
||
|
}
|
||
|
|
||
|
// Getting single item
|
||
|
String getHistoryItem(String url) {
|
||
![]()
10 years ago
|
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[] { KEY_ID, KEY_URL, KEY_TITLE },
|
||
|
KEY_URL + "=?", new String[] { url }, null, null, null, null);
|
||
![]()
11 years ago
|
String m = null;
|
||
|
if (cursor != null) {
|
||
|
cursor.moveToFirst();
|
||
|
m = cursor.getString(0);
|
||
|
|
||
|
cursor.close();
|
||
|
}
|
||
|
// return item
|
||
|
return m;
|
||
|
}
|
||
![]()
10 years ago
|
|
||
![]()
11 years ago
|
public List<HistoryItem> findItemsContaining(String search) {
|
||
|
List<HistoryItem> itemList = new ArrayList<HistoryItem>();
|
||
![]()
10 years ago
|
// select query
|
||
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%"
|
||
![]()
10 years ago
|
+ search + "%' OR " + KEY_URL + " LIKE '%" + search + "%'";
|
||
![]()
11 years ago
|
Cursor cursor = mDatabase.rawQuery(selectQuery, null);
|
||
|
|
||
|
// looping through all rows and adding to list
|
||
|
if (cursor.moveToLast()) {
|
||
|
do {
|
||
|
HistoryItem item = new HistoryItem();
|
||
|
item.setID(Integer.parseInt(cursor.getString(0)));
|
||
|
item.setUrl(cursor.getString(1));
|
||
|
item.setTitle(cursor.getString(2));
|
||
|
item.setImageId(R.drawable.ic_history);
|
||
|
// Adding item to list
|
||
|
itemList.add(item);
|
||
|
} while (cursor.moveToPrevious());
|
||
|
}
|
||
|
cursor.close();
|
||
|
// return item list
|
||
|
return itemList;
|
||
|
}
|
||
![]()
10 years ago
|
|
||
![]()
11 years ago
|
public List<HistoryItem> getLastHundredItems() {
|
||
|
List<HistoryItem> itemList = new ArrayList<HistoryItem>();
|
||
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY;
|
||
|
|
||
|
Cursor cursor = mDatabase.rawQuery(selectQuery, null);
|
||
|
int counter = 0;
|
||
|
if (cursor.moveToLast()) {
|
||
|
do {
|
||
|
HistoryItem item = new HistoryItem();
|
||
|
item.setID(Integer.parseInt(cursor.getString(0)));
|
||
|
item.setUrl(cursor.getString(1));
|
||
|
item.setTitle(cursor.getString(2));
|
||
|
item.setImageId(R.drawable.ic_history);
|
||
|
itemList.add(item);
|
||
|
counter++;
|
||
|
} while (cursor.moveToPrevious() && counter < 100);
|
||
|
}
|
||
|
cursor.close();
|
||
|
return itemList;
|
||
|
}
|
||
|
|
||
|
public List<HistoryItem> getAllHistoryItems() {
|
||
|
List<HistoryItem> itemList = new ArrayList<HistoryItem>();
|
||
|
String selectQuery = "SELECT * FROM " + TABLE_HISTORY;
|
||
|
|
||
|
Cursor cursor = mDatabase.rawQuery(selectQuery, null);
|
||
|
|
||
|
if (cursor.moveToFirst()) {
|
||
|
do {
|
||
|
HistoryItem item = new HistoryItem();
|
||
|
item.setID(Integer.parseInt(cursor.getString(0)));
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
// Updating single item
|
||
|
public synchronized int updateHistoryItem(HistoryItem item) {
|
||
|
|
||
|
ContentValues values = new ContentValues();
|
||
|
values.put(KEY_URL, item.getUrl());
|
||
|
values.put(KEY_TITLE, item.getTitle());
|
||
|
int n = mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?",
|
||
![]()
10 years ago
|
new String[] { String.valueOf(item.getId()) });
|
||
![]()
11 years ago
|
// updating row
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
// Deleting single item
|
||
|
public synchronized void deleteHistoryItem(String id) {
|
||
![]()
10 years ago
|
mDatabase.delete(TABLE_HISTORY, KEY_ID + " = ?", new String[] { String.valueOf(id) });
|
||
![]()
11 years ago
|
}
|
||
|
|
||
|
// Getting items Count
|
||
|
public int getHistoryItemsCount() {
|
||
|
String countQuery = "SELECT * FROM " + TABLE_HISTORY;
|
||
|
Cursor cursor = mDatabase.rawQuery(countQuery, null);
|
||
|
cursor.close();
|
||
|
|
||
|
// return count
|
||
|
return cursor.getCount();
|
||
|
}
|
||
|
}
|