Browse Source

Rename HistoryDatabase and convert it to a singleton for easier usage. Improved database structure.

master
Anthony Restaino 10 years ago
parent
commit
8b3da70d92
  1. 32
      src/acr/browser/lightning/BrowserActivity.java
  2. 80
      src/acr/browser/lightning/HistoryDatabase.java
  3. 2
      src/acr/browser/lightning/HistoryPage.java
  4. 2
      src/acr/browser/lightning/PrivacySettingsActivity.java
  5. 8
      src/acr/browser/lightning/SearchAdapter.java

32
src/acr/browser/lightning/BrowserActivity.java

@ -108,7 +108,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
private Bitmap mDefaultVideoPoster; private Bitmap mDefaultVideoPoster;
private View mVideoProgressView; private View mVideoProgressView;
private LinearLayout mToolbarLayout; private LinearLayout mToolbarLayout;
private HistoryDatabaseHandler mHistoryHandler; private HistoryDatabase mHistoryDatabase;
private SharedPreferences mPreferences; private SharedPreferences mPreferences;
private Context mContext; private Context mContext;
private Bitmap mWebpageBitmap; private Bitmap mWebpageBitmap;
@ -196,9 +196,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
mDrawerListRight.setOnItemClickListener(new BookmarkItemClickListener()); mDrawerListRight.setOnItemClickListener(new BookmarkItemClickListener());
mDrawerListRight.setOnItemLongClickListener(new BookmarkItemLongClickListener()); mDrawerListRight.setOnItemLongClickListener(new BookmarkItemLongClickListener());
if (mHistoryHandler == null || !mHistoryHandler.isOpen()) { mHistoryDatabase = HistoryDatabase.getInstance(this);
mHistoryHandler = new HistoryDatabaseHandler(this);
}
// set display options of the ActionBar // set display options of the ActionBar
@ -1407,7 +1405,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void clearHistory() { public void clearHistory() {
this.deleteDatabase(HistoryDatabaseHandler.DATABASE_NAME); this.deleteDatabase(HistoryDatabase.DATABASE_NAME);
WebViewDatabase m = WebViewDatabase.getInstance(this); WebViewDatabase m = WebViewDatabase.getInstance(this);
m.clearFormData(); m.clearFormData();
m.clearHttpAuthUsernamePassword(); m.clearHttpAuthUsernamePassword();
@ -1470,12 +1468,6 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
mCurrentView.pauseTimers(); mCurrentView.pauseTimers();
mCurrentView.onPause(); mCurrentView.onPause();
} }
if (mHistoryHandler != null) {
if (mHistoryHandler.isOpen()) {
mHistoryHandler.close();
}
}
} }
public void saveOpenTabs() { public void saveOpenTabs() {
@ -1493,10 +1485,8 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
@Override @Override
protected void onDestroy() { protected void onDestroy() {
Log.d(Constants.TAG, "onDestroy"); Log.d(Constants.TAG, "onDestroy");
if (mHistoryHandler != null) { if (mHistoryDatabase != null) {
if (mHistoryHandler.isOpen()) { mHistoryDatabase.close();
mHistoryHandler.close();
}
} }
super.onDestroy(); super.onDestroy();
} }
@ -1513,11 +1503,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
mCurrentView.resumeTimers(); mCurrentView.resumeTimers();
mCurrentView.onResume(); mCurrentView.onResume();
if (mHistoryHandler == null) { mHistoryDatabase = HistoryDatabase.getInstance(this);
mHistoryHandler = new HistoryDatabaseHandler(this);
} else if (!mHistoryHandler.isOpen()) {
mHistoryHandler = new HistoryDatabaseHandler(this);
}
mBookmarkList = mBookmarkManager.getBookmarks(true); mBookmarkList = mBookmarkManager.getBookmarks(true);
notifyBookmarkDataSetChanged(); notifyBookmarkDataSetChanged();
} }
@ -1970,10 +1956,10 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
} }
} }
try { try {
if (mHistoryHandler == null || !mHistoryHandler.isOpen()) { if (mHistoryDatabase == null) {
mHistoryHandler = new HistoryDatabaseHandler(mContext); mHistoryDatabase = HistoryDatabase.getInstance(mContext);
} }
mHistoryHandler.visitHistoryItem(url, title); mHistoryDatabase.visitHistoryItem(url, title);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
Log.e(Constants.TAG, "IllegalStateException in updateHistory"); Log.e(Constants.TAG, "IllegalStateException in updateHistory");
} catch (NullPointerException e) { } catch (NullPointerException e) {

80
src/acr/browser/lightning/HistoryDatabaseHandler.java → src/acr/browser/lightning/HistoryDatabase.java

@ -8,15 +8,14 @@ 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 java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class HistoryDatabaseHandler extends SQLiteOpenHelper { public class HistoryDatabase extends SQLiteOpenHelper {
// All Static variables // All Static variables
// Database Version // Database Version
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 2;
// Database Name // Database Name
public static final String DATABASE_NAME = "historyManager"; public static final String DATABASE_NAME = "historyManager";
@ -26,14 +25,22 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
// HistoryItems Table Columns names // HistoryItems Table Columns names
public static final String KEY_ID = "id"; public static final String KEY_ID = "id";
public static final String KEY_URL = "url"; public static final String KEY_URL = "url";
public static final String KEY_TITLE = "title"; public static final String KEY_TITLE = "title";
public static final String KEY_TIME_VISITED = "time";
public static SQLiteDatabase mDatabase; public static SQLiteDatabase mDatabase;
public HistoryDatabaseHandler(Context context) { private static HistoryDatabase mInstance;
public static HistoryDatabase getInstance(Context context) {
if (mInstance == null || mInstance.isClosed()) {
mInstance = new HistoryDatabase(context);
}
return mInstance;
}
private HistoryDatabase(Context context) {
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
mDatabase = this.getWritableDatabase(); mDatabase = this.getWritableDatabase();
} }
@ -42,22 +49,22 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "(" + KEY_ID String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "(" + KEY_ID
+ " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT" + ")"; + " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT,"
+ KEY_TIME_VISITED + " INTEGER" + ")";
db.execSQL(CREATE_HISTORY_TABLE); db.execSQL(CREATE_HISTORY_TABLE);
} }
// Upgrading database // Upgrading database
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed // Drop older table if it exists
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY); db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
// Create tables again // Create tables again
onCreate(db); onCreate(db);
} }
public boolean isOpen() { public boolean isClosed() {
return mDatabase != null && mDatabase.isOpen(); return mDatabase == null || !mDatabase.isOpen();
} }
@Override @Override
@ -68,34 +75,34 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
super.close(); super.close();
} }
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
public synchronized void deleteHistoryItem(String url) { public synchronized void deleteHistoryItem(String url) {
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[] { url }); mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[] { url });
} }
public synchronized void visitHistoryItem(String url, String title) { public synchronized void visitHistoryItem(String url, String title) {
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[] { url });
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_URL, url);
values.put(KEY_TITLE, title); values.put(KEY_TITLE, title);
mDatabase.insert(TABLE_HISTORY, null, values); 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));
}
} }
// Adding new item
public synchronized void addHistoryItem(HistoryItem item) { public synchronized void addHistoryItem(HistoryItem item) {
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());
mDatabase.insert(TABLE_HISTORY, null, values); mDatabase.insert(TABLE_HISTORY, null, values);
} }
// Getting single item
String getHistoryItem(String url) { String getHistoryItem(String url) {
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);
String m = null; String m = null;
if (cursor != null) { if (cursor != null) {
cursor.moveToFirst(); cursor.moveToFirst();
@ -103,43 +110,40 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
cursor.close(); cursor.close();
} }
// return item
return m; return m;
} }
public List<HistoryItem> findItemsContaining(String search) { public List<HistoryItem> findItemsContaining(String search) {
List<HistoryItem> itemList = new ArrayList<HistoryItem>(); List<HistoryItem> itemList = new ArrayList<HistoryItem>();
// select query
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 + "%' LIMIT 5"; + search + "%' OR " + KEY_URL + " LIKE '%" + search + "%' " + "ORDER BY "
+ KEY_TIME_VISITED + " DESC LIMIT 5";
Cursor cursor = mDatabase.rawQuery(selectQuery, null); Cursor cursor = mDatabase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
int n = 0; int n = 0;
if (cursor.moveToLast()) { if (cursor.moveToFirst()) {
do { do {
HistoryItem item = new HistoryItem(); HistoryItem item = new HistoryItem();
item.setID(Integer.parseInt(cursor.getString(0))); item.setID(Integer.parseInt(cursor.getString(0)));
item.setUrl(cursor.getString(1)); item.setUrl(cursor.getString(1));
item.setTitle(cursor.getString(2)); item.setTitle(cursor.getString(2));
item.setImageId(R.drawable.ic_history); item.setImageId(R.drawable.ic_history);
// Adding item to list
itemList.add(item); itemList.add(item);
n++; n++;
} while (cursor.moveToPrevious() && n < 5); } while (cursor.moveToNext() && n < 5);
} }
cursor.close(); cursor.close();
// return item list
return itemList; return itemList;
} }
public List<HistoryItem> getLastHundredItems() { public List<HistoryItem> getLastHundredItems() {
List<HistoryItem> itemList = new ArrayList<HistoryItem>(); List<HistoryItem> itemList = new ArrayList<HistoryItem>();
String selectQuery = "SELECT * FROM " + TABLE_HISTORY; String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED
+ " DESC";
Cursor cursor = mDatabase.rawQuery(selectQuery, null); Cursor cursor = mDatabase.rawQuery(selectQuery, null);
int counter = 0; int counter = 0;
if (cursor.moveToLast()) { if (cursor.moveToFirst()) {
do { do {
HistoryItem item = new HistoryItem(); HistoryItem item = new HistoryItem();
item.setID(Integer.parseInt(cursor.getString(0))); item.setID(Integer.parseInt(cursor.getString(0)));
@ -148,7 +152,7 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
item.setImageId(R.drawable.ic_history); item.setImageId(R.drawable.ic_history);
itemList.add(item); itemList.add(item);
counter++; counter++;
} while (cursor.moveToPrevious() && counter < 100); } while (cursor.moveToNext() && counter < 100);
} }
cursor.close(); cursor.close();
return itemList; return itemList;
@ -156,7 +160,8 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
public List<HistoryItem> getAllHistoryItems() { public List<HistoryItem> getAllHistoryItems() {
List<HistoryItem> itemList = new ArrayList<HistoryItem>(); List<HistoryItem> itemList = new ArrayList<HistoryItem>();
String selectQuery = "SELECT * FROM " + TABLE_HISTORY; String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED
+ " DESC";
Cursor cursor = mDatabase.rawQuery(selectQuery, null); Cursor cursor = mDatabase.rawQuery(selectQuery, null);
@ -174,23 +179,22 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper {
return itemList; return itemList;
} }
// Updating single item
public synchronized int updateHistoryItem(HistoryItem item) { public synchronized int updateHistoryItem(HistoryItem item) {
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());
return mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?", return mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?",
new String[] { String.valueOf(item.getId()) }); new String[] { String.valueOf(item.getId()) });
} }
// Getting items Count
public int getHistoryItemsCount() { public int getHistoryItemsCount() {
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();
cursor.close(); cursor.close();
// return count return n;
return cursor.getCount();
} }
} }

2
src/acr/browser/lightning/HistoryPage.java

@ -53,7 +53,7 @@ public class HistoryPage {
} }
private static List<HistoryItem> getWebHistory(Context context) { private static List<HistoryItem> getWebHistory(Context context) {
HistoryDatabaseHandler databaseHandler = new HistoryDatabaseHandler(context); HistoryDatabase databaseHandler = HistoryDatabase.getInstance(context);
return databaseHandler.getLastHundredItems(); return databaseHandler.getLastHundredItems();
} }
} }

2
src/acr/browser/lightning/PrivacySettingsActivity.java

@ -431,7 +431,7 @@ public class PrivacySettingsActivity extends ActionBarActivity {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void clearHistory() { public void clearHistory() {
deleteDatabase(HistoryDatabaseHandler.DATABASE_NAME); deleteDatabase(HistoryDatabase.DATABASE_NAME);
WebViewDatabase m = WebViewDatabase.getInstance(this); WebViewDatabase m = WebViewDatabase.getInstance(this);
m.clearFormData(); m.clearFormData();
m.clearHttpAuthUsernamePassword(); m.clearHttpAuthUsernamePassword();

8
src/acr/browser/lightning/SearchAdapter.java

@ -29,7 +29,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private List<HistoryItem> mSuggestions; private List<HistoryItem> mSuggestions;
private List<HistoryItem> mFilteredList; private List<HistoryItem> mFilteredList;
private List<HistoryItem> mAllBookmarks; private List<HistoryItem> mAllBookmarks;
private HistoryDatabaseHandler mDatabaseHandler; private HistoryDatabase mDatabaseHandler;
private SharedPreferences mPreferences; private SharedPreferences mPreferences;
private boolean mUseGoogle = true; private boolean mUseGoogle = true;
private Context mContext; private Context mContext;
@ -42,7 +42,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private String mSearchSubtitle; private String mSearchSubtitle;
public SearchAdapter(Context context, boolean incognito) { public SearchAdapter(Context context, boolean incognito) {
mDatabaseHandler = new HistoryDatabaseHandler(context); mDatabaseHandler = HistoryDatabase.getInstance(context);
mFilteredList = new ArrayList<HistoryItem>(); mFilteredList = new ArrayList<HistoryItem>();
mHistory = new ArrayList<HistoryItem>(); mHistory = new ArrayList<HistoryItem>();
mBookmarks = new ArrayList<HistoryItem>(); mBookmarks = new ArrayList<HistoryItem>();
@ -186,8 +186,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
} }
if (mDatabaseHandler == null || !mDatabaseHandler.isOpen()) { if (mDatabaseHandler == null) {
mDatabaseHandler = new HistoryDatabaseHandler(mContext); mDatabaseHandler = HistoryDatabase.getInstance(mContext);
} }
mHistory = mDatabaseHandler.findItemsContaining(constraint.toString()); mHistory = mDatabaseHandler.findItemsContaining(constraint.toString());

Loading…
Cancel
Save