diff options
| author | Zeapo <mohamed@zenadi.com> | 2014-08-01 00:17:05 +0100 |
|---|---|---|
| committer | Zeapo <mohamed@zenadi.com> | 2014-08-01 00:17:05 +0100 |
| commit | a0263aabadf04869da704a0075f01bfde98f279a (patch) | |
| tree | f2c95abaca52becba061761b4b163a188eaf921a /app/src/main/java/com/zeapo/pwdstore/utils | |
| parent | 4fe2baccf1b6cb6fe944fa49e34ab794ee53e5c5 (diff) | |
| download | HardenedPass-a0263aabadf04869da704a0075f01bfde98f279a.tar.gz HardenedPass-a0263aabadf04869da704a0075f01bfde98f279a.zip | |
Better distinction between categories and passwords, multilevel categories
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils')
3 files changed, 254 insertions, 0 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java new file mode 100644 index 00000000..b0cdb5a5 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java @@ -0,0 +1,63 @@ +package com.zeapo.pwdstore.utils; + +import android.content.Context; +import android.graphics.Typeface; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import com.zeapo.pwdstore.R; + +import java.util.ArrayList; + +public class PasswordAdapter extends ArrayAdapter<PasswordItem> { + private final Context context; + private final ArrayList<PasswordItem> values; + + static class ViewHolder { + public TextView name; + public TextView type; + } + + public PasswordAdapter(Context context, ArrayList<PasswordItem> values) { + super(context, R.layout.password_row_layout, values); + this.context = context; + this.values = values; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View rowView = convertView; + PasswordItem pass = values.get(position); + + // reuse for performance, holder pattern! + if (rowView == null) { + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + rowView = inflater.inflate(R.layout.password_row_layout, null); + + ViewHolder viewHolder = new ViewHolder(); + viewHolder.name = (TextView) rowView.findViewById(R.id.label); + viewHolder.type = (TextView) rowView.findViewById(R.id.type); + rowView.setTag(viewHolder); + } + + ViewHolder holder = (ViewHolder) rowView.getTag(); + + holder.name.setText(pass.toString()); + + if (pass.getType() == PasswordItem.TYPE_CATEGORY) { + holder.name.setTextColor(this.context.getResources().getColor(android.R.color.holo_blue_dark)); + holder.name.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); + holder.type.setText("Category: "); + } else { + holder.type.setText("Password: "); + holder.name.setTextColor(this.context.getResources().getColor(android.R.color.holo_orange_dark)); + holder.name.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); + } + + return rowView; + } +}
\ No newline at end of file diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java new file mode 100644 index 00000000..4403d16b --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java @@ -0,0 +1,103 @@ +package com.zeapo.pwdstore.utils; + +import java.io.File; + +public class PasswordItem implements Comparable{ + + public final static char TYPE_CATEGORY = 'c'; + public final static char TYPE_PASSWORD = 'p'; + + private char type; + private String name; + private PasswordItem parent; + private File file; + + /** Create a password item + * + * Make it protected so that we use a builder + * @param name + * @param parent + * @param type + */ + protected PasswordItem(String name, PasswordItem parent, char type, File file) { + this.name = name; + this.parent = parent; + this.type = type; + this.file = file; + } + + /** Create a new Category item + * + * @param name + * @param parent + * @return + */ + public static PasswordItem newCategory(String name, File file, PasswordItem parent) { + return new PasswordItem(name, parent, TYPE_CATEGORY, file); + } + + /** Create a new parentless category item + * + * @param name + * @return + */ + public static PasswordItem newCategory(String name, File file) { + return new PasswordItem(name, null, TYPE_CATEGORY, file); + } + + /** Create a new password item + * + * @param name + * @param parent + * @return + */ + public static PasswordItem newPassword(String name, File file, PasswordItem parent) { + return new PasswordItem(name, parent, TYPE_PASSWORD, file); + } + + /** Create a new parentless password item + * + * @param name + * @return + */ + public static PasswordItem newPassword(String name, File file) { + return new PasswordItem(name, null, TYPE_PASSWORD, file); + } + + public char getType(){ + return this.type; + } + + public String getName(){ + return this.name; + } + + public PasswordItem getParent() { + return this.parent; + } + + public File getFile() { + return this.file; + } + + @Override + public String toString(){ + return this.getName().replace(".gpg", ""); + } + + @Override + public boolean equals(Object o){ + PasswordItem other = (PasswordItem) o; + // Makes it possible to have a category and a password with the same name + return (other.getType() + other.getName()) + .equals(this.getType() + this.getName()); + } + + @Override + public int compareTo(Object o) { + PasswordItem other = (PasswordItem) o; + // Appending the type will make the sort type dependent + return (this.getType() + this.getName()) + .compareTo(other.getType() + other.getName()); + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java new file mode 100644 index 00000000..5f42b956 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java @@ -0,0 +1,88 @@ +package com.zeapo.pwdstore.utils; + +import org.apache.commons.io.FileUtils; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static java.util.Collections.sort; + +public class PasswordRepository { + + private static Repository repository; + + protected PasswordRepository(){ } + + public static Repository getRepository(File localDir) { + if (repository == null) { + FileRepositoryBuilder builder = new FileRepositoryBuilder(); + try { + repository = builder.setGitDir(localDir) + .readEnvironment() + .findGitDir() + .build(); + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + return repository; + } + + public static void closeRepository() { + repository.close(); + } + + public static ArrayList<File> getFilesList(){ + return getFilesList(repository.getWorkTree()); + } + + public static ArrayList<PasswordItem> getPasswords() { + return getPasswords(repository.getWorkTree()); + } + + public static File getWorkTree() { + return repository.getWorkTree(); + } + + public static File getFile(String name) { + return new File(repository.getWorkTree() + "/" + name); + } + + public static ArrayList<File> getFilesList(File path){ + if (!path.exists()) return new ArrayList<File>(); + + List<File> files = (List<File>) FileUtils.listFiles(path, new String[] {"gpg"}, true); + return new ArrayList<File>(files); + } + + public static ArrayList<PasswordItem> getPasswords(File path) { + //We need to recover the passwords then parse the files + ArrayList<File> passList = getFilesList(path); + + if (passList.size() == 0) return new ArrayList<PasswordItem>(); + + // TODO replace with a set + ArrayList<PasswordItem> passwordList = new ArrayList<PasswordItem>(); + + for (File file : passList) { + String fileName = file.getAbsolutePath().replace(path.getAbsolutePath() + "/", ""); + + String[] parts = fileName.split("/"); + if (parts.length == 1) { + passwordList.add(PasswordItem.newPassword(parts[0], file.getParentFile())); + } else { + if (!passwordList.contains(PasswordItem.newCategory(parts[0], file.getParentFile()))) { + passwordList.add(PasswordItem.newCategory(parts[0], file.getParentFile())); + } + } + } + sort(passwordList); + return passwordList; + } +} |
