mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Improve handling of global state.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Android Library"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Android with Source"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
||||
@@ -13,4 +13,7 @@
|
||||
<activity android:name=".GroupActivity"></activity>
|
||||
<activity android:name=".EntryActivity"></activity>
|
||||
</application>
|
||||
|
||||
|
||||
|
||||
</manifest>
|
||||
96
src/com/android/keepass/Database.java
Normal file
96
src/com/android/keepass/Database.java
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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 3 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.android.keepass;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.bouncycastle1.crypto.InvalidCipherTextException;
|
||||
import org.phoneid.keepassj2me.ImporterV3;
|
||||
import org.phoneid.keepassj2me.PwEntry;
|
||||
import org.phoneid.keepassj2me.PwGroup;
|
||||
import org.phoneid.keepassj2me.PwManager;
|
||||
|
||||
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 PwGroup gRoot;
|
||||
private static PwManager mPM;
|
||||
|
||||
public static int LoadData(String filename, String password) {
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(filename);
|
||||
} catch (FileNotFoundException e) {
|
||||
return R.string.FileNotFound;
|
||||
}
|
||||
|
||||
ImporterV3 Importer = new ImporterV3();
|
||||
|
||||
try {
|
||||
mPM = Importer.openDatabase(fis, password);
|
||||
if ( mPM != null ) {
|
||||
mPM.constructTree(null);
|
||||
populateGlobals(null);
|
||||
}
|
||||
} catch (InvalidCipherTextException e) {
|
||||
return R.string.InvalidPassword;
|
||||
} catch (IOException e) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
private static void populateGlobals(PwGroup currentGroup) {
|
||||
if (currentGroup == null) {
|
||||
Vector rootChildGroups = mPM.getGrpRoots();
|
||||
for (int i = 0; i < rootChildGroups.size(); i++ ){
|
||||
PwGroup cur = (PwGroup) rootChildGroups.elementAt(i);
|
||||
gRoot = cur.parent;
|
||||
gGroups.put(cur.groupId, new WeakReference<PwGroup>(cur));
|
||||
populateGlobals(cur);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Vector childGroups = currentGroup.childGroups;
|
||||
Vector childEntries = currentGroup.childEntries;
|
||||
|
||||
for (int i = 0; i < childEntries.size(); i++ ) {
|
||||
PwEntry cur = (PwEntry) childEntries.elementAt(i);
|
||||
gEntries.put(UUID.nameUUIDFromBytes(cur.uuid), new WeakReference<PwEntry>(cur));
|
||||
}
|
||||
|
||||
for (int i = 0; i < childGroups.size(); i++ ) {
|
||||
PwGroup cur = (PwGroup) childGroups.elementAt(i);
|
||||
gGroups.put(cur.groupId, new WeakReference<PwGroup>(cur));
|
||||
populateGlobals(cur);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -19,6 +19,8 @@
|
||||
*/
|
||||
package com.android.keepass;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.phoneid.keepassj2me.PwEntry;
|
||||
|
||||
import android.app.Activity;
|
||||
@@ -41,9 +43,7 @@ public class EntryActivity extends Activity {
|
||||
public static void Launch(Activity act, PwEntry pw) {
|
||||
Intent i = new Intent(act, EntryActivity.class);
|
||||
|
||||
KeePass.gPwEntry.put(KeePass.gNumPwEntry, pw);
|
||||
i.putExtra(KEY_ENTRY, KeePass.gNumPwEntry);
|
||||
KeePass.gNumPwEntry++;
|
||||
i.putExtra(KEY_ENTRY, pw.uuid);
|
||||
|
||||
act.startActivity(i);
|
||||
}
|
||||
@@ -58,11 +58,11 @@ public class EntryActivity extends Activity {
|
||||
setContentView(R.layout.entry_view);
|
||||
|
||||
Intent i = getIntent();
|
||||
mId = i.getIntExtra(KEY_ENTRY, -1);
|
||||
assert(mId < 0);
|
||||
|
||||
mEntry = KeePass.gPwEntry.get(mId);
|
||||
UUID uuid = UUID.nameUUIDFromBytes(i.getByteArrayExtra(KEY_ENTRY));
|
||||
assert(uuid != null);
|
||||
|
||||
mEntry = Database.gEntries.get(uuid).get();
|
||||
|
||||
fillData();
|
||||
}
|
||||
|
||||
|
||||
@@ -35,18 +35,15 @@ public class GroupActivity extends ListActivity {
|
||||
|
||||
public static final String KEY_ENTRY = "entry";
|
||||
|
||||
private Vector mGroups;
|
||||
private Vector mEntries;
|
||||
private PwGroup mGroup;
|
||||
|
||||
public static void Launch(Activity act, Vector groups, Vector entries) {
|
||||
public static void Launch(Activity act, PwGroup group) {
|
||||
Intent i = new Intent(act, GroupActivity.class);
|
||||
|
||||
KeePass.gGroups.put(KeePass.gNumEntries, groups);
|
||||
KeePass.gEntries.put(KeePass.gNumEntries, entries);
|
||||
if ( group != null ) {
|
||||
i.putExtra(KEY_ENTRY, group.groupId);
|
||||
}
|
||||
|
||||
i.putExtra(KEY_ENTRY, KeePass.gNumEntries);
|
||||
|
||||
KeePass.gNumEntries++;
|
||||
|
||||
act.startActivity(i);
|
||||
}
|
||||
@@ -55,15 +52,14 @@ public class GroupActivity extends ListActivity {
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||
// TODO Auto-generated method stub
|
||||
super.onListItemClick(l, v, position, id);
|
||||
int size = mGroups.size();
|
||||
int size = mGroup.childGroups.size();
|
||||
PwItemView iv;
|
||||
if (position < size ) {
|
||||
PwGroup group = (PwGroup) mGroups.elementAt(position);
|
||||
PwGroup group = (PwGroup) mGroup.childGroups.elementAt(position);
|
||||
iv = new PwGroupView(this, group);
|
||||
} else {
|
||||
PwEntry entry = (PwEntry) mEntries.elementAt(position - size);
|
||||
PwEntry entry = (PwEntry) mGroup.childEntries.elementAt(position - size);
|
||||
iv = new PwEntryView(this, entry);
|
||||
}
|
||||
iv.onClick();
|
||||
@@ -74,26 +70,20 @@ public class GroupActivity extends ListActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.list);
|
||||
|
||||
mId = getIntent().getIntExtra(KEY_ENTRY, -1);
|
||||
int id = getIntent().getIntExtra(KEY_ENTRY, -1);
|
||||
assert(mId >= 0);
|
||||
|
||||
mGroups = KeePass.gGroups.get(mId);
|
||||
assert(mGroups != null);
|
||||
mEntries = KeePass.gEntries.get(mId);
|
||||
assert(mEntries != null);
|
||||
if ( id == -1 ) {
|
||||
mGroup = Database.gRoot;
|
||||
} else {
|
||||
mGroup = Database.gGroups.get(id).get();
|
||||
}
|
||||
assert(mGroup != null);
|
||||
|
||||
|
||||
setListAdapter(new PwListAdapter(this, mGroups, mEntries));
|
||||
setListAdapter(new PwListAdapter(this, mGroup));
|
||||
getListView().setTextFilterEnabled(true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
KeePass.gGroups.remove(mId);
|
||||
KeePass.gEntries.remove(mId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ package com.android.keepass;
|
||||
import org.bouncycastle1.crypto.InvalidCipherTextException;
|
||||
import org.phoneid.keepassj2me.ImporterV3;
|
||||
import org.phoneid.keepassj2me.PwEntry;
|
||||
import org.phoneid.keepassj2me.PwGroup;
|
||||
import org.phoneid.keepassj2me.PwManager;
|
||||
|
||||
import android.app.Activity;
|
||||
@@ -34,19 +35,14 @@ import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.Vector;
|
||||
|
||||
public class KeePass extends Activity {
|
||||
|
||||
|
||||
private PwManager mPM;
|
||||
public static HashMap<Integer, Vector> gGroups = new HashMap<Integer, Vector>();
|
||||
public static HashMap<Integer, Vector> gEntries = new HashMap<Integer, Vector>();
|
||||
public static Integer gNumEntries = new Integer(0);
|
||||
|
||||
public static HashMap<Integer, PwEntry> gPwEntry = new HashMap<Integer, PwEntry>();
|
||||
public static Integer gNumPwEntry = new Integer(0);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -58,8 +54,6 @@ public class KeePass extends Activity {
|
||||
|
||||
loadDefaultPrefs();
|
||||
|
||||
//setEditText(R.id.pass_password, "12345");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,33 +88,6 @@ public class KeePass extends Activity {
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
private boolean fillData(String filename, String password) {
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(filename);
|
||||
} catch (FileNotFoundException e) {
|
||||
errorMessage(R.string.FileNotFound);
|
||||
return false;
|
||||
}
|
||||
|
||||
ImporterV3 Importer = new ImporterV3();
|
||||
|
||||
try {
|
||||
mPM = Importer.openDatabase(fis, password);
|
||||
if ( mPM != null ) {
|
||||
mPM.constructTree(null);
|
||||
}
|
||||
} catch (InvalidCipherTextException e) {
|
||||
errorMessage(R.string.InvalidPassword);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
errorMessage("IO Error");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private void errorMessage(CharSequence text)
|
||||
{
|
||||
@@ -132,14 +99,6 @@ public class KeePass extends Activity {
|
||||
Toast.makeText(this, resId, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
gGroups.remove(requestCode);
|
||||
gEntries.remove(requestCode);
|
||||
}
|
||||
|
||||
private class ClickHandler implements View.OnClickListener {
|
||||
private Activity mAct;
|
||||
|
||||
@@ -148,8 +107,18 @@ public class KeePass extends Activity {
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if ( fillData(getEditText(R.id.pass_filename),getEditText(R.id.pass_password)) ) {
|
||||
GroupActivity.Launch(mAct, mPM.getGrpRoots(), new Vector());
|
||||
int result = Database.LoadData(getEditText(R.id.pass_filename),getEditText(R.id.pass_password));
|
||||
|
||||
switch (result) {
|
||||
case 0:
|
||||
GroupActivity.Launch(mAct, null);
|
||||
break;
|
||||
case -1:
|
||||
errorMessage("Unknown error.");
|
||||
break;
|
||||
default:
|
||||
errorMessage(result);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class PwGroupView extends PwItemView {
|
||||
|
||||
@Override
|
||||
void onClick() {
|
||||
GroupActivity.Launch(mAct, mPw.childGroups, mPw.childEntries);
|
||||
GroupActivity.Launch(mAct, mPw);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package com.android.keepass;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.TypedValue;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -31,6 +32,7 @@ abstract public class PwItemView extends LinearLayout {
|
||||
|
||||
mTitle = new TextView(context);
|
||||
mTitle.setText(title);
|
||||
mTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
|
||||
addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
|
||||
}
|
||||
|
||||
|
||||
@@ -32,20 +32,18 @@ import org.phoneid.keepassj2me.PwGroup;
|
||||
public class PwListAdapter extends BaseAdapter {
|
||||
|
||||
private Activity mAct;
|
||||
private Vector mGroupList;
|
||||
private Vector mEntryList;
|
||||
private PwGroup mGroup;
|
||||
|
||||
PwListAdapter(Activity act, Vector gl, Vector el) {
|
||||
PwListAdapter(Activity act, PwGroup group) {
|
||||
mAct = act;
|
||||
mGroupList = gl;
|
||||
mEntryList = el;
|
||||
mGroup = group;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
|
||||
return mGroupList.size() + mEntryList.size();
|
||||
return mGroup.childGroups.size() + mGroup.childEntries.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +58,7 @@ public class PwListAdapter extends BaseAdapter {
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
int size = mGroupList.size();
|
||||
int size = mGroup.childGroups.size();
|
||||
|
||||
if ( position < size ) {
|
||||
return createGroupView(position, convertView);
|
||||
@@ -72,11 +70,11 @@ public class PwListAdapter extends BaseAdapter {
|
||||
private PwGroupView createGroupView(int position, View convertView) {
|
||||
PwGroupView gv;
|
||||
if (convertView == null || ! (convertView instanceof PwGroupView)) {
|
||||
PwGroup group = (PwGroup) mGroupList.elementAt(position);
|
||||
PwGroup group = (PwGroup) mGroup.childGroups.elementAt(position);
|
||||
gv = new PwGroupView(mAct, group);
|
||||
} else {
|
||||
gv = (PwGroupView) convertView;
|
||||
gv.setGroup((PwGroup) mGroupList.elementAt(position));
|
||||
gv.setGroup((PwGroup) mGroup.childGroups.elementAt(position));
|
||||
}
|
||||
return gv;
|
||||
}
|
||||
@@ -84,10 +82,10 @@ public class PwListAdapter extends BaseAdapter {
|
||||
private PwEntryView createEntryView(int position, View convertView) {
|
||||
PwEntryView ev;
|
||||
if (convertView == null || ! (convertView instanceof PwEntryView) ) {
|
||||
ev = new PwEntryView(mAct, (PwEntry) mEntryList.elementAt(position));
|
||||
ev = new PwEntryView(mAct, (PwEntry) mGroup.childEntries.elementAt(position));
|
||||
} else {
|
||||
ev = (PwEntryView) convertView;
|
||||
ev.setEntry((PwEntry) mEntryList.elementAt(position));
|
||||
ev.setEntry((PwEntry) mGroup.childEntries.elementAt(position));
|
||||
}
|
||||
return ev;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user