From 30d4d5342ff6cdcc7d1adbd0be34bea4b1aa1a96 Mon Sep 17 00:00:00 2001 From: حسين Date: Mon, 20 Aug 2018 10:13:38 +0100 Subject: change the sort order of password items (#421) --- .../java/com/zeapo/pwdstore/PasswordFragment.java | 22 ++++++---- .../java/com/zeapo/pwdstore/PasswordStore.java | 10 ++++- .../com/zeapo/pwdstore/SelectFolderFragment.java | 10 ++++- .../com/zeapo/pwdstore/utils/PasswordItem.java | 2 +- .../zeapo/pwdstore/utils/PasswordRepository.java | 48 +++++++++++++++++++--- app/src/main/res/values/arrays.xml | 10 +++++ app/src/main/res/values/strings.xml | 4 ++ app/src/main/res/xml/preference.xml | 8 ++++ build.gradle | 2 +- 9 files changed, 96 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index 2b0fa7c1..58af4a60 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -60,7 +60,7 @@ public class PasswordFragment extends Fragment{ scrollPosition = new Stack<>(); pathStack = new Stack<>(); recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) getActivity(), mListener, - PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()))); + PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder())); } @Override @@ -101,15 +101,15 @@ public class PasswordFragment extends Fragment{ if (item.getType() == PasswordItem.TYPE_CATEGORY) { // push the current password list (non filtered plz!) passListStack.push(pathStack.isEmpty() ? - PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context)) : - PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context))); + PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context), getSortOrder()) : + PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context), getSortOrder())); //push the category were we're going pathStack.push(item.getFile()); scrollPosition.push(recyclerView.getVerticalScrollbarPosition()); recyclerView.scrollToPosition(0); recyclerAdapter.clear(); - recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context))); + recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context), getSortOrder())); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } else { @@ -135,7 +135,7 @@ public class PasswordFragment extends Fragment{ pathStack.clear(); scrollPosition.clear(); recyclerAdapter.clear(); - recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()))); + recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder())); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); } @@ -146,8 +146,8 @@ public class PasswordFragment extends Fragment{ public void refreshAdapter() { recyclerAdapter.clear(); recyclerAdapter.addAll(pathStack.isEmpty() ? - PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())) : - PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(getActivity()))); + PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()) : + PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder())); } /** @@ -172,8 +172,8 @@ public class PasswordFragment extends Fragment{ private void recursiveFilter(String filter, File dir) { // on the root the pathStack is empty ArrayList passwordItems = dir == null ? - PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())) : - PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(getActivity())); + PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()) : + PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()); boolean rec = settings.getBoolean("filter_recursively", true); for (PasswordItem item : passwordItems) { @@ -223,4 +223,8 @@ public class PasswordFragment extends Fragment{ recyclerAdapter.mActionMode.finish(); } } + + private PasswordRepository.PasswordSortOrder getSortOrder() { + return PasswordRepository.PasswordSortOrder.getSortOrder(settings); + } } diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 890ab3e8..b4029ba4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -313,7 +313,9 @@ public class PasswordStore extends AppCompatActivity { if (settings.getBoolean("git_external", false) && externalRepoPath != null) { File dir = new File(externalRepoPath); - if (dir.exists() && dir.isDirectory() && !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)).isEmpty()) { + PasswordRepository.PasswordSortOrder sortOrder = PasswordRepository.PasswordSortOrder.valueOf(settings.getString("sort_order", null)); + + if (dir.exists() && dir.isDirectory() && !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this), getSortOrder()).isEmpty()) { PasswordRepository.closeRepository(); checkLocalRepository(); return; // if not empty, just show me the passwords! @@ -648,7 +650,7 @@ public class PasswordStore extends AppCompatActivity { dir.exists() && dir.isDirectory() && !FileUtils.listFiles(dir, null, true).isEmpty() && - !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)).isEmpty()) { + !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this), getSortOrder()).isEmpty()) { PasswordRepository.closeRepository(); checkLocalRepository(); return; // if not empty, just show me the passwords! @@ -769,4 +771,8 @@ public class PasswordStore extends AppCompatActivity { setResult(RESULT_OK, data); finish(); } + + private PasswordRepository.PasswordSortOrder getSortOrder() { + return PasswordRepository.PasswordSortOrder.getSortOrder(settings); + } } diff --git a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java index 8f583a8a..d8c5ff16 100644 --- a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java @@ -1,7 +1,9 @@ package com.zeapo.pwdstore; import android.content.Context; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; @@ -51,7 +53,7 @@ public class SelectFolderFragment extends Fragment{ pathStack = new Stack<>(); recyclerAdapter = new FolderRecyclerAdapter((SelectFolderActivity) getActivity(), mListener, - PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()))); + PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder())); } @Override @@ -87,7 +89,7 @@ public class SelectFolderFragment extends Fragment{ recyclerView.scrollToPosition(0); recyclerAdapter.clear(); - recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context))); + recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context), getSortOrder())); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @@ -109,4 +111,8 @@ public class SelectFolderFragment extends Fragment{ else return pathStack.peek(); } + + private PasswordRepository.PasswordSortOrder getSortOrder() { + return PasswordRepository.PasswordSortOrder.getSortOrder(PreferenceManager.getDefaultSharedPreferences(getActivity())); + } } diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java index f45abc38..3261cc58 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java @@ -55,7 +55,7 @@ public class PasswordItem implements Comparable{ return this.type; } - private String getName(){ + String getName(){ return this.name; } diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java index e232b8a0..d724f090 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java @@ -5,7 +5,6 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.FileFilterUtils; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.Repository; @@ -19,6 +18,7 @@ import java.io.File; import java.io.FileFilter; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Set; @@ -155,8 +155,8 @@ public class PasswordRepository { * * @return a list of passwords in the root direcotyr */ - public static ArrayList getPasswords(File rootDir) { - return getPasswords(rootDir, rootDir); + public static ArrayList getPasswords(File rootDir, PasswordSortOrder sortOrder) { + return getPasswords(rootDir, rootDir, sortOrder); } /** @@ -185,7 +185,7 @@ public class PasswordRepository { * @param path the directory path * @return a list of password items */ - public static ArrayList getPasswords(File path, File rootDir) { + public static ArrayList getPasswords(File path, File rootDir, PasswordSortOrder sortOrder) { //We need to recover the passwords then parse the files ArrayList passList = getFilesList(path); @@ -203,7 +203,7 @@ public class PasswordRepository { passwordList.add(PasswordItem.newCategory(file.getName(), file, rootDir)); } } - sort(passwordList); + sort(passwordList, sortOrder.comparator); return passwordList; } @@ -244,4 +244,42 @@ public class PasswordRepository { } } } + + public enum PasswordSortOrder { + + FOLDER_FIRST(new Comparator() { + @Override + public int compare(PasswordItem p1, PasswordItem p2) { + return (p1.getType() + p1.getName()) + .compareToIgnoreCase(p2.getType() + p2.getName()); + } + }), + + INDEPENDENT(new Comparator() { + @Override + public int compare(PasswordItem p1, PasswordItem p2) { + return p1.getName().compareToIgnoreCase(p2.getName()); + } + }), + + FILE_FIRST(new Comparator() { + @Override + public int compare(PasswordItem p1, PasswordItem p2) { + return (p2.getType() + p1.getName()) + .compareToIgnoreCase(p1.getType() + p2.getName()); + } + }) + + ; + + private Comparator comparator; + + PasswordSortOrder(Comparator comparator) { + this.comparator = comparator; + } + + public static PasswordSortOrder getSortOrder(SharedPreferences settings) { + return valueOf(settings.getString("sort_order", null)); + } + } } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 36037083..44698c41 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -8,4 +8,14 @@ ssh:// https:// + + @string/pref_folder_first_sort_order + @string/pref_file_first_sort_order + @string/pref_type_independent_sort_order + + + FOLDER_FIRST + FILE_FIRST + INDEPENDENT + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1d9422bb..96e028e5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,6 +123,10 @@ Message : \n Recursive filtering Recursively find passwords of the current directory. + Password sort order + Folders First + Files First + Type Independent Autofill Enable autofill Tap OK to go to Accessibility settings. There, tap Password Store under Services then tap the switch in the top right to turn it on or off. diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 23e68577..e232c6e1 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -76,6 +76,14 @@ android:key="filter_recursively" android:summary="@string/pref_recursive_filter_hint" android:title="@string/pref_recursive_filter" /> + diff --git a/build.gradle b/build.gradle index 75a44a72..d5d573be 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.0' + classpath 'com.android.tools.build:gradle:3.1.4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong -- cgit v1.2.3