Delay search index build.

This commit is contained in:
Brian Pellin
2009-12-18 22:06:02 -06:00
parent 2873385c8b
commit 245ec9ffbe
9 changed files with 163 additions and 62 deletions

View File

@@ -38,7 +38,6 @@ import org.phoneid.keepassj2me.Types;
import android.content.Context;
import com.android.keepass.R;
import com.keepassdroid.app.App;
import com.keepassdroid.keepasslib.InvalidKeyFileException;
import com.keepassdroid.keepasslib.InvalidPasswordException;
@@ -48,8 +47,6 @@ import com.keepassdroid.search.SearchDbHelper;
/**
* @author bpellin
* TODO: Only one instance of the search DB can currently exist. So, we cannot support multiple instances of the Database class.
* I should either emit that this class should be static, or resolve that.
*/
public class Database {
public HashMap<Integer, WeakReference<PwGroup>> gGroups = new HashMap<Integer, WeakReference<PwGroup>>();
@@ -59,6 +56,8 @@ public class Database {
public PwManager mPM;
public String mFilename;
public SearchDbHelper searchHelper;
public boolean indexBuilt = false;
private boolean loaded = false;
public boolean Loaded() {
@@ -104,9 +103,8 @@ public class Database {
populateGlobals(null);
}
status.updateMessage(R.string.building_search_idx);
searchHelper = new SearchDbHelper(ctx);
buildSearchIndex();
//status.updateMessage(R.string.building_search_idx);
//buildSearchIndex();
//Debug.stopMethodTracing();
loaded = true;
@@ -114,9 +112,11 @@ public class Database {
/** Build the search index from the current database
* @param ctx
* @param ctx (this should be an App context not an activity constant to avoid leaks)
*/
private void buildSearchIndex() {
public void buildSearchIndex(Context ctx) {
searchHelper = new SearchDbHelper(ctx);
initSearch();
@@ -128,6 +128,8 @@ public class Database {
}
}
searchHelper.close();
indexBuilt = true;
}
public PwGroup Search(String str) {

View File

@@ -75,7 +75,7 @@ public class PwEntryView extends ClickView {
private void deleteEntry() {
Handler handler = new Handler();
DeleteEntry task = new DeleteEntry(App.getDB(), mPw, mAct, mAct.new RefreshTask(handler));
DeleteEntry task = new DeleteEntry(App.getDB(), mPw, mAct.new RefreshTask(handler));
ProgressTask pt = new ProgressTask(mAct, task, R.string.saving_database);
pt.run();

View File

@@ -76,11 +76,13 @@ public class AddEntry extends RunnableOnFinish {
// Add entry to global
mDb.gEntries.put(Types.bytestoUUID(mEntry.uuid), new WeakReference<PwEntry>(mEntry));
if ( mDb.indexBuilt ) {
// Add entry to search index
SearchDbHelper helper = mDb.searchHelper;
helper.open();
helper.insertEntry(mEntry);
helper.close();
}
} else {
// Remove from group
mEntry.parent.childEntries.removeElement(mEntry);

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2009 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.database;
import android.content.Context;
import com.keepassdroid.Database;
public class BuildIndex extends RunnableOnFinish {
private Database mDb;
private Context mCtx;
public BuildIndex(Database db, Context ctx, OnFinish finish) {
super(finish);
mDb = db;
mCtx = ctx.getApplicationContext();
}
@Override
public void run() {
mDb.buildSearchIndex(mCtx);
finish(true);
}
}

View File

@@ -24,8 +24,6 @@ import java.lang.ref.WeakReference;
import org.phoneid.keepassj2me.PwEntry;
import org.phoneid.keepassj2me.PwGroup;
import android.content.Context;
import com.keepassdroid.Database;
import com.keepassdroid.search.SearchDbHelper;
@@ -37,34 +35,28 @@ public class DeleteEntry extends RunnableOnFinish {
private Database mDb;
private PwEntry mEntry;
private Context mCtx;
private boolean mDontSave;
public DeleteEntry(Database db, PwEntry entry, Context ctx, OnFinish finish) {
public DeleteEntry(Database db, PwEntry entry, OnFinish finish) {
super(finish);
mDb = db;
mEntry = entry;
mCtx = ctx;
mDontSave = false;
}
public DeleteEntry(Database db, PwEntry entry, Context ctx, OnFinish finish, boolean dontSave) {
public DeleteEntry(Database db, PwEntry entry, OnFinish finish, boolean dontSave) {
super(finish);
mDb = db;
mEntry = entry;
mCtx = ctx;
mDontSave = dontSave;
}
@Override
public void run() {
SearchDbHelper dbHelper = new SearchDbHelper(mCtx);
dbHelper.open();
// Remove Entry from parent
PwGroup parent = mEntry.parent;
@@ -74,7 +66,7 @@ public class DeleteEntry extends RunnableOnFinish {
mDb.mPM.entries.remove(mEntry);
// Save
mFinish = new AfterDelete(mFinish, dbHelper, parent, mEntry);
mFinish = new AfterDelete(mFinish, parent, mEntry);
// Commit database
SaveDB save = new SaveDB(mDb, mFinish, mDontSave);
@@ -86,14 +78,12 @@ public class DeleteEntry extends RunnableOnFinish {
private class AfterDelete extends OnFinish {
private SearchDbHelper mDbHelper;
private PwGroup mParent;
private PwEntry mEntry;
public AfterDelete(OnFinish finish, SearchDbHelper helper, PwGroup parent, PwEntry entry) {
public AfterDelete(OnFinish finish, PwGroup parent, PwEntry entry) {
super(finish);
mDbHelper = helper;
mParent = parent;
mEntry = entry;
}
@@ -101,11 +91,17 @@ public class DeleteEntry extends RunnableOnFinish {
@Override
public void run() {
if ( mSuccess ) {
if ( mDb.indexBuilt ) {
SearchDbHelper dbHelper = mDb.searchHelper;
dbHelper.open();
// Remove from entry global
mDb.gEntries.remove(mEntry);
// Remove from search db
mDbHelper.deleteEntry(mEntry);
dbHelper.deleteEntry(mEntry);
dbHelper.close();
}
// Mark parent dirty
if ( mParent != null ) {
@@ -122,8 +118,6 @@ public class DeleteEntry extends RunnableOnFinish {
}
mDbHelper.close();
super.run();
}

View File

@@ -25,8 +25,6 @@ import java.util.Vector;
import org.phoneid.keepassj2me.PwEntry;
import org.phoneid.keepassj2me.PwGroup;
import android.content.Context;
import com.keepassdroid.Database;
import com.keepassdroid.GroupBaseActivity;
@@ -35,35 +33,28 @@ public class DeleteGroup extends RunnableOnFinish {
private Database mDb;
private PwGroup mGroup;
private GroupBaseActivity mAct;
private Context mCtx;
private boolean mDontSave;
public DeleteGroup(Database db, PwGroup group, GroupBaseActivity act, OnFinish finish) {
super(finish);
setMembers(db, group, act, act, false);
setMembers(db, group, act, false);
}
public DeleteGroup(Database db, PwGroup group, GroupBaseActivity act, OnFinish finish, boolean dontSave) {
super(finish);
setMembers(db, group, act, act, dontSave);
setMembers(db, group, act, dontSave);
}
public DeleteGroup(Database db, PwGroup group, GroupBaseActivity act, Context ctx, OnFinish finish, boolean dontSave) {
public DeleteGroup(Database db, PwGroup group, OnFinish finish, boolean dontSave) {
super(finish);
setMembers(db, group, act, ctx, dontSave);
setMembers(db, group, null, dontSave);
}
public DeleteGroup(Database db, PwGroup group, Context ctx, OnFinish finish, boolean dontSave) {
super(finish);
setMembers(db, group, null, ctx, dontSave);
}
private void setMembers(Database db, PwGroup group, GroupBaseActivity act, Context ctx, boolean dontSave) {
private void setMembers(Database db, PwGroup group, GroupBaseActivity act, boolean dontSave) {
mDb = db;
mGroup = group;
mAct = act;
mCtx = ctx;
mDontSave = dontSave;
mFinish = new AfterDelete(mFinish);
@@ -78,14 +69,14 @@ public class DeleteGroup extends RunnableOnFinish {
// Remove child entries
Vector<PwEntry> childEnt = (Vector<PwEntry>) mGroup.childEntries.clone();
for ( int i = 0; i < childEnt.size(); i++ ) {
DeleteEntry task = new DeleteEntry(mDb, childEnt.get(i), mCtx, null, true);
DeleteEntry task = new DeleteEntry(mDb, childEnt.get(i), null, true);
task.run();
}
// Remove child groups
Vector<PwGroup> childGrp = (Vector<PwGroup>) mGroup.childGroups.clone();
for ( int i = 0; i < childGrp.size(); i++ ) {
DeleteGroup task = new DeleteGroup(mDb, childGrp.get(i), mAct, mCtx, null, true);
DeleteGroup task = new DeleteGroup(mDb, childGrp.get(i), mAct, null, true);
task.run();
}

View File

@@ -78,12 +78,14 @@ public class UpdateEntry extends RunnableOnFinish {
}
if ( mDb.indexBuilt ) {
// Update search index
SearchDbHelper helper = mDb.searchHelper;
helper.open();
helper.updateEntry(mOldE);
helper.close();
}
}
} else {
// If we fail to save, back out changes to global structure
mOldE.assign(mBackup);

View File

@@ -19,22 +19,25 @@
*/
package com.keepassdroid.search;
import org.phoneid.keepassj2me.PwGroup;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import com.android.keepass.KeePass;
import com.android.keepass.R;
import com.keepassdroid.Database;
import com.keepassdroid.GroupBaseActivity;
import com.keepassdroid.ProgressTask;
import com.keepassdroid.PwListAdapter;
import com.keepassdroid.app.App;
import com.keepassdroid.database.BuildIndex;
import com.keepassdroid.database.OnFinish;
public class SearchResults extends GroupBaseActivity {
private Database mDb;
//private String mQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -53,6 +56,8 @@ public class SearchResults extends GroupBaseActivity {
finish();
}
performSearch(getSearchStr(getIntent()));
/*
mGroup = processSearchIntent(getIntent());
assert(mGroup != null);
@@ -65,16 +70,59 @@ public class SearchResults extends GroupBaseActivity {
setGroupTitle();
setListAdapter(new PwListAdapter(this, mGroup));
*/
}
private void performSearch(String query) {
if ( mDb.indexBuilt ) {
query(query);
} else {
PerformSearch task = new PerformSearch(query, new Handler());
ProgressTask pt = new ProgressTask(this, new BuildIndex(mDb, this, task), R.string.building_search_idx);
pt.run();
}
}
private void query(String query) {
mGroup = mDb.Search(query);
if ( mGroup == null || mGroup.childEntries.size() < 1 ) {
setContentView(R.layout.group_empty);
} else {
setContentView(R.layout.group_view_only);
}
setGroupTitle();
setListAdapter(new PwListAdapter(this, mGroup));
}
/*
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mGroup = processSearchIntent(intent);
assert(mGroup != null);
mQuery = getSearchStr(intent);
performSearch();
//mGroup = processSearchIntent(intent);
//assert(mGroup != null);
}
*/
private String getSearchStr(Intent queryIntent) {
// get and process search query here
final String queryAction = queryIntent.getAction();
if ( Intent.ACTION_SEARCH.equals(queryAction) ) {
return queryIntent.getStringExtra(SearchManager.QUERY);
}
return "";
}
/*
private PwGroup processSearchIntent(Intent queryIntent) {
// get and process search query here
final String queryAction = queryIntent.getAction();
@@ -87,5 +135,22 @@ public class SearchResults extends GroupBaseActivity {
return null;
}
*/
private class PerformSearch extends OnFinish {
private String mQuery;
public PerformSearch(String query, Handler handler) {
super(handler);
mQuery = query;
}
@Override
public void run() {
query(mQuery);
}
}
}

View File

@@ -59,7 +59,7 @@ public class DeleteEntry extends AndroidTestCase {
assertNotNull("Could not find group1", group1);
// Delete the group
DeleteGroup task = new DeleteGroup(db, group1, ctx, null, true);
DeleteGroup task = new DeleteGroup(db, group1, null, true);
task.run();
// Verify the entries were deleted