Make Database no longer static.

This commit is contained in:
Brian Pellin
2009-05-30 12:56:19 -05:00
parent 0a29a6801d
commit 1ddb716de6
9 changed files with 35 additions and 33 deletions

View File

@@ -48,15 +48,15 @@ import com.android.keepass.search.SearchDbHelper;
*
*/
public class Database {
public static HashMap<Integer, WeakReference<PwGroup>> gGroups = new HashMap<Integer, WeakReference<PwGroup>>();
public static HashMap<UUID, WeakReference<PwEntry>> gEntries = new HashMap<UUID, WeakReference<PwEntry>>();
public static HashMap<PwGroup, WeakReference<PwGroup>> gDirty = new HashMap<PwGroup, WeakReference<PwGroup>>();
public static PwGroup gRoot;
public static PwManager mPM;
public static String mFilename;
public static SearchDbHelper searchHelper;
public HashMap<Integer, WeakReference<PwGroup>> gGroups = new HashMap<Integer, WeakReference<PwGroup>>();
public HashMap<UUID, WeakReference<PwEntry>> gEntries = new HashMap<UUID, WeakReference<PwEntry>>();
public HashMap<PwGroup, WeakReference<PwGroup>> gDirty = new HashMap<PwGroup, WeakReference<PwGroup>>();
public PwGroup gRoot;
public PwManager mPM;
public String mFilename;
public SearchDbHelper searchHelper;
public static void LoadData(Context ctx, String filename, String password, String keyfile) throws InvalidCipherTextException, IOException, InvalidKeyFileException, FileNotFoundException {
public void LoadData(Context ctx, String filename, String password, String keyfile) throws InvalidCipherTextException, IOException, InvalidKeyFileException, FileNotFoundException {
FileInputStream fis;
fis = new FileInputStream(filename);
@@ -79,7 +79,7 @@ public class Database {
/** Build the search index from the current database
* @param ctx
*/
private static void buildSearchIndex(Context ctx) {
private void buildSearchIndex(Context ctx) {
for ( int i = 0; i < mPM.entries.size(); i++) {
@@ -88,7 +88,7 @@ public class Database {
}
}
public static void NewEntry(PwEntry entry) throws IOException, PwManagerOutputException {
public void NewEntry(PwEntry entry) throws IOException, PwManagerOutputException {
PwGroup parent = entry.parent;
// Add entry to group
@@ -118,7 +118,7 @@ public class Database {
searchHelper.insertEntry(entry);
}
public static void UndoNewEntry(PwEntry entry) {
public void UndoNewEntry(PwEntry entry) {
// Remove from group
entry.parent.childEntries.removeElement(entry);
@@ -126,7 +126,7 @@ public class Database {
mPM.entries.removeElement(entry);
}
public static void UpdateEntry(PwEntry oldE, PwEntry newE) throws IOException, PwManagerOutputException {
public void UpdateEntry(PwEntry oldE, PwEntry newE) throws IOException, PwManagerOutputException {
// Keep backup of original values in case save fails
PwEntry backup = new PwEntry(oldE);
@@ -158,16 +158,16 @@ public class Database {
}
public static void UndoUpdateEntry(PwEntry old, PwEntry backup) {
public void UndoUpdateEntry(PwEntry old, PwEntry backup) {
// If we fail to save, back out changes to global structure
old.assign(backup);
}
public static void SaveData() throws IOException, PwManagerOutputException {
public void SaveData() throws IOException, PwManagerOutputException {
SaveData(mFilename);
}
public static void SaveData(String filename) throws IOException, PwManagerOutputException {
public void SaveData(String filename) throws IOException, PwManagerOutputException {
File tempFile = new File(filename + ".tmp");
FileOutputStream fos = new FileOutputStream(tempFile);
PwManagerOutput pmo = new PwManagerOutput(mPM, fos);
@@ -185,7 +185,7 @@ public class Database {
}
private static void populateGlobals(PwGroup currentGroup) {
private void populateGlobals(PwGroup currentGroup) {
if (currentGroup == null) {
Vector<PwGroup> rootChildGroups = mPM.getGrpRoots();
for (int i = 0; i < rootChildGroups.size(); i++ ){
@@ -213,7 +213,7 @@ public class Database {
}
}
public static void clear() {
public void clear() {
if ( searchHelper != null ) {
searchHelper.close();
searchHelper = null;

View File

@@ -77,7 +77,7 @@ public class EntryActivity extends LockingActivity {
mPos = i.getIntExtra(KEY_REFRESH_POS, -1);
assert(uuid != null);
mEntry = Database.gEntries.get(uuid).get();
mEntry = KeePass.db.gEntries.get(uuid).get();
// Update last access time.
Calendar cal = Calendar.getInstance();

View File

@@ -81,14 +81,14 @@ public class EntryEditActivity extends LockingActivity {
if ( uuidBytes == null ) {
int groupId = i.getIntExtra(KEY_PARENT, -1);
mEntry = new PwEntry(groupId);
mEntry = new PwEntry(KeePass.db, groupId);
mIsNew = true;
} else {
UUID uuid = Types.bytestoUUID(uuidBytes);
assert(uuid != null);
mEntry = Database.gEntries.get(uuid).get();
mEntry = KeePass.db.gEntries.get(uuid).get();
mIsNew = false;
fillData();
@@ -264,9 +264,9 @@ public class EntryEditActivity extends LockingActivity {
public void run() {
try {
if ( mIsNew ) {
Database.NewEntry(mNew);
KeePass.db.NewEntry(mNew);
} else {
Database.UpdateEntry(mOld, mNew);
KeePass.db.UpdateEntry(mOld, mNew);
}
uiHandler.post(new AfterSave());

View File

@@ -56,8 +56,8 @@ public class GroupActivity extends LockingListActivity {
protected void onResume() {
super.onResume();
if ( Database.gDirty.get(mGroup) != null ) {
Database.gDirty.remove(mGroup);
if ( KeePass.db.gDirty.get(mGroup) != null ) {
KeePass.db.gDirty.remove(mGroup);
BaseAdapter adapter = (BaseAdapter) getListAdapter();
adapter.notifyDataSetChanged();
@@ -86,9 +86,9 @@ public class GroupActivity extends LockingListActivity {
assert(mId >= 0);
if ( id == -1 ) {
mGroup = Database.gRoot;
mGroup = KeePass.db.gRoot;
} else {
WeakReference<PwGroup> wPw = Database.gGroups.get(id);
WeakReference<PwGroup> wPw = KeePass.db.gGroups.get(id);
mGroup = wPw.get();
}
assert(mGroup != null);

View File

@@ -32,6 +32,8 @@ public class KeePass extends Activity {
public static final int EXIT_LOCK = 1;
public static final int EXIT_REFRESH = 2;
public static final int EXIT_REFRESH_TITLE = 3;
public static Database db = new Database();
@Override
protected void onCreate(Bundle savedInstanceState) {

View File

@@ -39,7 +39,7 @@ public class LockManager {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( action.equals(TimeoutIntents.LOCK) ) {
Database.clear();
KeePass.db.clear();
mAct.setResult(KeePass.EXIT_LOCK);
mAct.finish();
}

View File

@@ -80,7 +80,7 @@ public class PasswordActivity extends Activity {
finish();
}
Database.clear();
KeePass.db.clear();
}
@Override
@@ -255,7 +255,7 @@ public class PasswordActivity extends Activity {
@Override
public void run() {
try {
Database.LoadData(PasswordActivity.this, mFileName, mPass, mKey);
KeePass.db.LoadData(PasswordActivity.this, mFileName, mPass, mKey);
saveFileData(mFileName, mKey);
uiHandler.post(new AfterLoad());

View File

@@ -122,7 +122,7 @@ public class SearchDbHelper {
}
public PwGroup search(String qStr) {
public PwGroup search(Database db, String qStr) {
Cursor cursor;
cursor = mDb.query(true, SEARCH_TABLE, new String[] {KEY_UUID}, "match ?", new String[] {qStr}, null, null, null, null);
@@ -134,7 +134,7 @@ public class SearchDbHelper {
byte[] bUuid = cursor.getBlob(0);
UUID uuid = Types.bytestoUUID(bUuid);
PwEntry entry = Database.gEntries.get(uuid).get();
PwEntry entry = db.gEntries.get(uuid).get();
group.childEntries.add(entry);
cursor.moveToNext();

View File

@@ -81,9 +81,9 @@ public class PwEntry {
assign(source);
}
public PwEntry(int parentId) {
public PwEntry(Database db, int parentId) {
WeakReference<PwGroup> wPw = Database.gGroups.get(parentId);
WeakReference<PwGroup> wPw = db.gGroups.get(parentId);
parent = wPw.get();
groupId = parentId;