diff --git a/src/com/keepassdroid/PwListAdapter.java b/src/com/keepassdroid/PwListAdapter.java index 0520aa0df..cbe1780a7 100644 --- a/src/com/keepassdroid/PwListAdapter.java +++ b/src/com/keepassdroid/PwListAdapter.java @@ -19,7 +19,8 @@ */ package com.keepassdroid; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; import android.view.View; import android.view.ViewGroup; @@ -37,6 +38,7 @@ public class PwListAdapter extends BaseAdapter { super.notifyDataSetChanged(); filter(); + sort(); } @Override @@ -44,22 +46,29 @@ public class PwListAdapter extends BaseAdapter { super.notifyDataSetInvalidated(); filter(); + sort(); } private GroupBaseActivity mAct; private PwGroup mGroup; - private Vector filteredEntries; + private List filteredEntries; public PwListAdapter(GroupBaseActivity act, PwGroup group) { mAct = act; mGroup = group; filter(); + sort(); } + private void sort() { + // TODO Auto-generated method stub + + } + private void filter() { - filteredEntries = new Vector(); + filteredEntries = new ArrayList(); for (int i = 0; i < mGroup.childEntries.size(); i++) { PwEntry entry = mGroup.childEntries.get(i); @@ -108,7 +117,7 @@ public class PwListAdapter extends BaseAdapter { private PwEntryView createEntryView(int position, View convertView) { PwEntryView ev; - ev = PwEntryView.getInstance(mAct, filteredEntries.elementAt(position), position); + ev = PwEntryView.getInstance(mAct, filteredEntries.get(position), position); return ev; } diff --git a/src/com/keepassdroid/database/PwDatabase.java b/src/com/keepassdroid/database/PwDatabase.java index 158ae4ac1..cc55aeddd 100644 --- a/src/com/keepassdroid/database/PwDatabase.java +++ b/src/com/keepassdroid/database/PwDatabase.java @@ -27,7 +27,7 @@ import java.io.UnsupportedEncodingException; import java.security.DigestOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.Vector; +import java.util.List; import com.keepassdroid.crypto.finalkey.FinalKey; import com.keepassdroid.crypto.finalkey.FinalKeyFactory; @@ -206,11 +206,11 @@ public abstract class PwDatabase { public abstract byte[] getPasswordKey(String key) throws IOException; - public abstract Vector getGrpRoots(); + public abstract List getGrpRoots(); - public abstract Vector getGroups(); + public abstract List getGroups(); - public abstract Vector getEntries(); + public abstract List getEntries(); public abstract long getNumRounds(); diff --git a/src/com/keepassdroid/database/PwDatabaseV3.java b/src/com/keepassdroid/database/PwDatabaseV3.java index 97d31e122..8f8945ac2 100644 --- a/src/com/keepassdroid/database/PwDatabaseV3.java +++ b/src/com/keepassdroid/database/PwDatabaseV3.java @@ -21,21 +21,20 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + */ package com.keepassdroid.database; // Java import java.io.IOException; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; import java.util.Random; -import java.util.Vector; import com.keepassdroid.database.exception.InvalidKeyFileException; - - /** * @author Naomaru Itoi * @author Bill Zwicky @@ -44,235 +43,233 @@ import com.keepassdroid.database.exception.InvalidKeyFileException; public class PwDatabaseV3 extends PwDatabase { // TODO: delete ME public byte[] postHeader; - - // Constants - // private static final int PWM_SESSION_KEY_SIZE = 12; - // Special entry for settings - public PwEntry metaInfo; + // Constants + // private static final int PWM_SESSION_KEY_SIZE = 12; - // all entries - public Vector entries = new Vector(); - // all groups - private Vector groups = new Vector(); - // Algorithm used to encrypt the database - public int algorithm; - public int numKeyEncRounds; - - // Debugging entries - public PwDbHeaderV3 dbHeader; - - public int getAlgorithm() { - return algorithm; - } - - public int getNumKeyEncRecords() { - return numKeyEncRounds; - } + // Special entry for settings + public PwEntry metaInfo; + + // all entries + public List entries = new ArrayList(); + // all groups + private List groups = new ArrayList(); + // Algorithm used to encrypt the database + public int algorithm; + public int numKeyEncRounds; + + // Debugging entries + public PwDbHeaderV3 dbHeader; + + public int getAlgorithm() { + return algorithm; + } + + public int getNumKeyEncRecords() { + return numKeyEncRounds; + } - @Override - public Vector getGroups() { - return groups; - } - @Override - public Vector getEntries() { + public List getGroups() { + return groups; + } + + @Override + public List getEntries() { return entries; } - - - public void setGroups(Vector grp) { - groups = grp; - } - - @Override - public Vector getGrpRoots() { - int target = 0; - Vector kids = new Vector(); - for( int i=0; i < groups.size(); i++ ) { - PwGroupV3 grp = (PwGroupV3) groups.elementAt( i ); - if( grp.level == target ) - kids.addElement( grp ); - } - return kids; - } - - public int getRootGroupId() { - for ( int i = 0; i < groups.size(); i++ ) { - PwGroupV3 grp = (PwGroupV3) groups.elementAt(i); - if ( grp.level == 0 ) { - return grp.groupId; - } - } - - return -1; - } - public Vector getGrpChildren(PwGroupV3 parent) { - int idx = groups.indexOf(parent); - int target = parent.level + 1; - Vector kids = new Vector(); - while (++idx < groups.size()) { - PwGroupV3 grp = (PwGroupV3) groups.elementAt(idx); - if (grp.level < target) - break; - else if (grp.level == target) - kids.addElement(grp); + public void setGroups(List grp) { + groups = grp; + } + + @Override + public List getGrpRoots() { + int target = 0; + List kids = new ArrayList(); + for (int i = 0; i < groups.size(); i++) { + PwGroupV3 grp = (PwGroupV3) groups.get(i); + if (grp.level == target) + kids.add(grp); } return kids; } - public Vector getEntries(PwGroupV3 parent) { - Vector kids = new Vector(); + public int getRootGroupId() { + for (int i = 0; i < groups.size(); i++) { + PwGroupV3 grp = (PwGroupV3) groups.get(i); + if (grp.level == 0) { + return grp.groupId; + } + } + + return -1; + } + + public List getGrpChildren(PwGroupV3 parent) { + int idx = groups.indexOf(parent); + int target = parent.level + 1; + List kids = new ArrayList(); + while (++idx < groups.size()) { + PwGroupV3 grp = (PwGroupV3) groups.get(idx); + if (grp.level < target) + break; + else if (grp.level == target) + kids.add(grp); + } + return kids; + } + + public List getEntries(PwGroupV3 parent) { + List kids = new ArrayList(); /* * for( Iterator i = entries.iterator(); i.hasNext(); ) { PwEntryV3 ent * = (PwEntryV3)i.next(); if( ent.groupId == parent.groupId ) kids.add( * ent ); } */ for (int i = 0; i < entries.size(); i++) { - PwEntryV3 ent = (PwEntryV3) entries.elementAt(i); + PwEntryV3 ent = (PwEntryV3) entries.get(i); if (ent.groupId == parent.groupId) - kids.addElement(ent); + kids.add(ent); } return kids; } - public String toString() { - return name; - } - - - - public void addGroup(PwGroupV3 group) - { - groups.addElement(group); - } - - public void addEntry(PwEntry entry) - { - entries.addElement(entry); - } - - public void constructTree(PwGroupV3 currentGroup) - { - // I'm in root - if (currentGroup == null) { - PwGroupV3 root = new PwGroupV3(); - rootGroup = root; - - - Vector rootChildGroups = getGrpRoots(); - root.setGroups(rootChildGroups); - root.childEntries = new Vector(); - root.level = -1; - for (int i=0; i rootChildGroups = getGrpRoots(); + root.setGroups(rootChildGroups); + root.childEntries = new ArrayList(); + root.level = -1; + for (int i = 0; i < rootChildGroups.size(); i++) { + PwGroupV3 grp = (PwGroupV3) rootChildGroups.get(i); + grp.parent = root; + constructTree(grp); + } + return; + } + + // I'm in non-root + // get child groups + currentGroup.setGroups(getGrpChildren(currentGroup)); + currentGroup.childEntries = getEntries(currentGroup); + + // set parent in child entries + for (int i = 0; i < currentGroup.childEntries.size(); i++) { + PwEntryV3 entry = (PwEntryV3) currentGroup.childEntries.get(i); + entry.parent = currentGroup; + } + // recursively construct child groups + for (int i = 0; i < currentGroup.childGroups.size(); i++) { + PwGroupV3 grp = (PwGroupV3) currentGroup.childGroups.get(i); + grp.parent = currentGroup; + constructTree((PwGroupV3) currentGroup.childGroups.get(i)); + } + return; + } + + public PwGroupV3 newGroup(String name, PwGroupV3 parent) { + // Initialize group + PwGroupV3 group = new PwGroupV3(); + + group.parent = parent; + group.groupId = newGroupId(); + group.imageId = 0; + group.name = name; + + Date now = Calendar.getInstance().getTime(); + group.tCreation = new PwDate(now); + group.tLastAccess = new PwDate(now); + group.tLastMod = new PwDate(now); + group.tExpire = new PwDate(PwGroupV3.NEVER_EXPIRE); + + group.level = parent.level + 1; + + group.childEntries = new ArrayList(); + group.setGroups(new ArrayList()); + + // Add group PwDatabaseV3 and Parent + parent.childGroups.add(group); + groups.add(group); + + return group; + } + + public void removeGroup(PwGroupV3 group) { + group.parent.childGroups.remove(group); + groups.remove(group); + } + + /** + * Generates an unused random group id + * + * @return new group id + */ + private int newGroupId() { + boolean foundUnusedId = false; + int newId = 0; + + Random random = new Random(); + + while (!foundUnusedId) { + newId = random.nextInt(); + + if (!isGroupIdUsed(newId)) { + foundUnusedId = true; + } + } + + return newId; + } + + /** + * Determine if an id number is already in use + * + * @param id + * ID number to check for + * @return True if the ID is used, false otherwise + */ + private boolean isGroupIdUsed(int id) { + for (int i = 0; i < groups.size(); i++) { + PwGroupV3 group = (PwGroupV3) groups.get(i); + if (group.groupId == id) { + return true; + } + } + + return false; + } - group.childEntries = new Vector(); - group.setGroups(new Vector()); - - // Add group PwDatabaseV3 and Parent - parent.childGroups.add(group); - groups.add(group); - - return group; - } - - public void removeGroup(PwGroupV3 group) { - group.parent.childGroups.remove(group); - groups.remove(group); - } - - /** Generates an unused random group id - * @return new group id - */ - private int newGroupId() { - boolean foundUnusedId = false; - int newId = 0; - - Random random = new Random(); - - while ( ! foundUnusedId ) { - newId = random.nextInt(); - - if ( ! isGroupIdUsed(newId) ) { - foundUnusedId = true; - } - } - - return newId; - } - - /** Determine if an id number is already in use - * @param id ID number to check for - * @return True if the ID is used, false otherwise - */ - private boolean isGroupIdUsed(int id) { - for ( int i = 0; i < groups.size(); i++ ) { - PwGroupV3 group = (PwGroupV3) groups.get(i); - if ( group.groupId == id ) { - return true; - } - } - - return false; - } - public byte[] getMasterKey(String key, String keyFileName) - throws InvalidKeyFileException, IOException { - assert( key != null && keyFileName != null ); - - if ( key.length() > 0 && keyFileName.length() > 0 ) { + throws InvalidKeyFileException, IOException { + assert (key != null && keyFileName != null); + + if (key.length() > 0 && keyFileName.length() > 0) { return getCompositeKey(key, keyFileName); - } else if ( key.length() > 0 ) { + } else if (key.length() > 0) { return getPasswordKey(key); - } else if ( keyFileName.length() > 0 ) { + } else if (keyFileName.length() > 0) { return getFileKey(keyFileName); } else { - throw new IllegalArgumentException( "Key cannot be empty." ); + throw new IllegalArgumentException("Key cannot be empty."); } - + } public byte[] getPasswordKey(String key) throws IOException { @@ -286,10 +283,10 @@ public class PwDatabaseV3 extends PwDatabase { @Override public void setNumRounds(long rounds) throws NumberFormatException { - if ( rounds > Integer.MAX_VALUE || rounds < Integer.MIN_VALUE ) { + if (rounds > Integer.MAX_VALUE || rounds < Integer.MIN_VALUE) { throw new NumberFormatException(); } - + numKeyEncRounds = (int) rounds; } @@ -298,5 +295,4 @@ public class PwDatabaseV3 extends PwDatabase { return true; } - } diff --git a/src/com/keepassdroid/database/PwDatabaseV4.java b/src/com/keepassdroid/database/PwDatabaseV4.java index 9cb2056db..97b4e6454 100644 --- a/src/com/keepassdroid/database/PwDatabaseV4.java +++ b/src/com/keepassdroid/database/PwDatabaseV4.java @@ -28,7 +28,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; -import java.util.Vector; import com.keepassdroid.database.exception.InvalidKeyFileException; @@ -111,8 +110,8 @@ public class PwDatabaseV4 extends PwDatabase { } @Override - public Vector getGroups() { - Vector list = new Vector(); + public List getGroups() { + List list = new ArrayList(); PwGroupV4 root = (PwGroupV4) rootGroup; root.buildChildGroupsRecursive(list); @@ -120,13 +119,13 @@ public class PwDatabaseV4 extends PwDatabase { } @Override - public Vector getGrpRoots() { + public List getGrpRoots() { return rootGroup.childGroups; } @Override - public Vector getEntries() { - Vector list = new Vector(); + public List getEntries() { + List list = new ArrayList(); PwGroupV4 root = (PwGroupV4) rootGroup; root.buildChildEntriesRecursive(list); diff --git a/src/com/keepassdroid/database/PwEntryV4.java b/src/com/keepassdroid/database/PwEntryV4.java index 80d8b410f..052df5d1f 100644 --- a/src/com/keepassdroid/database/PwEntryV4.java +++ b/src/com/keepassdroid/database/PwEntryV4.java @@ -65,12 +65,6 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger { windowSeqPairs.put(key, value); } - private PwEntryV4 getOuterType() { - return PwEntryV4.this; - } - - - } public PwEntryV4() { diff --git a/src/com/keepassdroid/database/PwGroup.java b/src/com/keepassdroid/database/PwGroup.java index 18a98e428..938f81791 100644 --- a/src/com/keepassdroid/database/PwGroup.java +++ b/src/com/keepassdroid/database/PwGroup.java @@ -19,14 +19,15 @@ */ package com.keepassdroid.database; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; -import java.util.Vector; +import java.util.List; public abstract class PwGroup { - public Vector childGroups = new Vector(); - public Vector childEntries = new Vector(); + public List childGroups = new ArrayList(); + public List childEntries = new ArrayList(); public abstract PwGroup getParent(); diff --git a/src/com/keepassdroid/database/PwGroupV3.java b/src/com/keepassdroid/database/PwGroupV3.java index fb939f04b..ba53ecab4 100644 --- a/src/com/keepassdroid/database/PwGroupV3.java +++ b/src/com/keepassdroid/database/PwGroupV3.java @@ -31,7 +31,7 @@ Copyright 2006 Bill Zwicky package com.keepassdroid.database; import java.util.Date; -import java.util.Vector; +import java.util.List; @@ -71,7 +71,7 @@ public class PwGroupV3 extends PwGroup { /** Used by KeePass internally, don't use */ public int flags; - public void setGroups(Vector groups) { + public void setGroups(List groups) { childGroups = groups; } diff --git a/src/com/keepassdroid/database/PwGroupV4.java b/src/com/keepassdroid/database/PwGroupV4.java index a92f6a43f..694361751 100644 --- a/src/com/keepassdroid/database/PwGroupV4.java +++ b/src/com/keepassdroid/database/PwGroupV4.java @@ -20,8 +20,8 @@ package com.keepassdroid.database; import java.util.Date; +import java.util.List; import java.util.UUID; -import java.util.Vector; public class PwGroupV4 extends PwGroup implements ITimeLogger { @@ -82,7 +82,7 @@ public class PwGroupV4 extends PwGroup implements ITimeLogger { return parent; } - public void buildChildGroupsRecursive(Vector list) { + public void buildChildGroupsRecursive(List list) { list.add(this); for ( int i = 0; i < childGroups.size(); i++) { @@ -92,7 +92,7 @@ public class PwGroupV4 extends PwGroup implements ITimeLogger { } } - public void buildChildEntriesRecursive(Vector list) { + public void buildChildEntriesRecursive(List list) { for ( int i = 0; i < childEntries.size(); i++ ) { list.add(childEntries.get(i)); } diff --git a/src/com/keepassdroid/database/edit/AddEntry.java b/src/com/keepassdroid/database/edit/AddEntry.java index bdca221f7..131aafb3e 100644 --- a/src/com/keepassdroid/database/edit/AddEntry.java +++ b/src/com/keepassdroid/database/edit/AddEntry.java @@ -89,7 +89,7 @@ public abstract class AddEntry extends RunnableOnFinish { mEntry.getParent().childEntries.remove(mEntry); // Remove from manager - mDb.pm.getEntries().removeElement(mEntry); + mDb.pm.getEntries().remove(mEntry); } diff --git a/src/com/keepassdroid/database/edit/DeleteGroup.java b/src/com/keepassdroid/database/edit/DeleteGroup.java index 4ac4b24f1..443cfe88c 100644 --- a/src/com/keepassdroid/database/edit/DeleteGroup.java +++ b/src/com/keepassdroid/database/edit/DeleteGroup.java @@ -20,7 +20,8 @@ package com.keepassdroid.database.edit; import java.lang.ref.WeakReference; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; import com.keepassdroid.Database; import com.keepassdroid.GroupBaseActivity; @@ -62,19 +63,18 @@ public class DeleteGroup extends RunnableOnFinish { - @SuppressWarnings("unchecked") @Override public void run() { // Remove child entries - Vector childEnt = (Vector) mGroup.childEntries.clone(); + List childEnt = new ArrayList(mGroup.childEntries); for ( int i = 0; i < childEnt.size(); i++ ) { DeleteEntry task = new DeleteEntry(mDb, childEnt.get(i), null, true); task.run(); } // Remove child groups - Vector childGrp = (Vector) mGroup.childGroups.clone(); + List childGrp = new ArrayList(mGroup.childGroups); for ( int i = 0; i < childGrp.size(); i++ ) { DeleteGroup task = new DeleteGroup(mDb, childGrp.get(i), mAct, null, true); task.run(); diff --git a/src/com/keepassdroid/database/save/PwDbV3Output.java b/src/com/keepassdroid/database/save/PwDbV3Output.java index 8661ad550..66950b724 100644 --- a/src/com/keepassdroid/database/save/PwDbV3Output.java +++ b/src/com/keepassdroid/database/save/PwDbV3Output.java @@ -28,7 +28,8 @@ import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; @@ -180,7 +181,7 @@ public class PwDbV3Output extends PwDbOutput { //long size = 0; // Groups - Vector groups = mPM.getGroups(); + List groups = mPM.getGroups(); for ( int i = 0; i < groups.size(); i++ ) { PwGroupV3 pg = (PwGroupV3) groups.get(i); PwGroupOutputV3 pgo = new PwGroupOutputV3(pg, os); @@ -204,10 +205,10 @@ public class PwDbV3Output extends PwDbOutput { } private void sortGroupsForOutput() { - Vector groupList = new Vector(); + List groupList = new ArrayList(); // Rebuild list according to coalation sorting order removing any orphaned groups - Vector roots = mPM.getGrpRoots(); + List roots = mPM.getGrpRoots(); for ( int i = 0; i < roots.size(); i++ ) { sortGroup((PwGroupV3) roots.get(i), groupList); } @@ -215,7 +216,7 @@ public class PwDbV3Output extends PwDbOutput { mPM.setGroups(groupList); } - private void sortGroup(PwGroupV3 group, Vector groupList) { + private void sortGroup(PwGroupV3 group, List groupList) { // Add current group groupList.add(group); diff --git a/src/com/keepassdroid/search/SearchDbHelper.java b/src/com/keepassdroid/search/SearchDbHelper.java index 378676c30..b999e6cb7 100644 --- a/src/com/keepassdroid/search/SearchDbHelper.java +++ b/src/com/keepassdroid/search/SearchDbHelper.java @@ -19,8 +19,9 @@ */ package com.keepassdroid.search; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; -import java.util.Vector; import android.content.ContentValues; import android.content.Context; @@ -114,7 +115,7 @@ public class SearchDbHelper { mDb.insert(SEARCH_TABLE, null, cv); } - public void insertEntry(Vector entries) { + public void insertEntry(List entries) { mDb.beginTransaction(); try { @@ -147,8 +148,8 @@ public class SearchDbHelper { PwGroupV3 group = new PwGroupV3(); group.name = "Search results"; - group.childEntries = new Vector(); - group.setGroups(new Vector()); + group.childEntries = new ArrayList(); + group.setGroups(new ArrayList()); cursor.moveToFirst(); while ( ! cursor.isAfterLast() ) { diff --git a/tests/src/com/keepassdroid/tests/database/DeleteEntry.java b/tests/src/com/keepassdroid/tests/database/DeleteEntry.java index 60255f97a..f2d758573 100644 --- a/tests/src/com/keepassdroid/tests/database/DeleteEntry.java +++ b/tests/src/com/keepassdroid/tests/database/DeleteEntry.java @@ -19,7 +19,7 @@ */ package com.keepassdroid.tests.database; -import java.util.Vector; +import java.util.List; import android.content.Context; import android.test.AndroidTestCase; @@ -89,7 +89,7 @@ public class DeleteEntry extends AndroidTestCase { } private PwEntry getEntry(PwDatabaseV3 pm, String name) { - Vector entries = pm.entries; + List entries = pm.entries; for ( int i = 0; i < entries.size(); i++ ) { PwEntry entry = entries.get(i); if ( entry.getTitle().equals(name) ) { @@ -102,7 +102,7 @@ public class DeleteEntry extends AndroidTestCase { } private PwGroup getGroup(PwDatabase pm, String name) { - Vector groups = pm.getGroups(); + List groups = pm.getGroups(); for ( int i = 0; i < groups.size(); i++ ) { PwGroup group = groups.get(i); if ( group.getName().equals(name) ) {