diff options
| author | Mohamed Zenadi <zeapo@users.noreply.github.com> | 2014-12-06 18:11:42 +0100 |
|---|---|---|
| committer | Mohamed Zenadi <zeapo@users.noreply.github.com> | 2014-12-06 18:11:42 +0100 |
| commit | ed26a526d340ce8fa6aff9915b6acbb73ff802b9 (patch) | |
| tree | c2d6c1323af7b6802ac5bdabe2c552fef95e4f94 /app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java | |
| parent | 1d2398ef85a475d50939b891b8b66bfda8d7237b (diff) | |
| parent | ad35b453f0a356af6ff6ac2028507d194f09e954 (diff) | |
| download | HardenedPass-ed26a526d340ce8fa6aff9915b6acbb73ff802b9.tar.gz HardenedPass-ed26a526d340ce8fa6aff9915b6acbb73ff802b9.zip | |
Merge pull request #53 from zeapo/feature/recursive-filtering
Filter the passwords recursively
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index 2262b7d3..b2e06a1b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -2,9 +2,11 @@ package com.zeapo.pwdstore; import android.app.Activity; import android.content.Intent; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; +import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; import android.support.v7.internal.widget.AdapterViewCompat; import android.support.v7.widget.LinearLayoutManager; @@ -47,6 +49,7 @@ public class PasswordFragment extends Fragment{ private RecyclerView recyclerView; private RecyclerView.LayoutManager mLayoutManager; private OnFragmentInteractionListener mListener; + private SharedPreferences settings; /** * Mandatory empty constructor for the fragment manager to instantiate the @@ -60,6 +63,7 @@ public class PasswordFragment extends Fragment{ String path = getArguments().getString("Path"); + settings = PreferenceManager.getDefaultSharedPreferences(getActivity()); passListStack = new Stack<ArrayList<PasswordItem>>(); scrollPosition = new Stack<Integer>(); pathStack = new Stack<File>(); @@ -150,25 +154,42 @@ public class PasswordFragment extends Fragment{ PasswordRepository.getPasswords(pathStack.peek())); } + /** + * filters the list adapter + * @param filter the filter to apply + */ public void filterAdapter(String filter) { Log.d("FRAG", "filter: " + filter); if (filter.isEmpty()) { refreshAdapter(); } else { - // on the root the pathStack is empty - List<PasswordItem> passwordItems = pathStack.isEmpty() ? - PasswordRepository.getPasswords() : - PasswordRepository.getPasswords(pathStack.peek()); - - for (PasswordItem item : passwordItems) { - boolean matches = item.toString().toLowerCase().contains(filter.toLowerCase()); - boolean inAdapter = recyclerAdapter.getValues().contains(item); - if (matches && !inAdapter) { - recyclerAdapter.add(item); - } else if (!matches && inAdapter) { - recyclerAdapter.remove(recyclerAdapter.getValues().indexOf(item)); - } + recursiveFilter(filter, pathStack.isEmpty() ? null : pathStack.peek()); + } + } + + /** + * recursively filters a directory and extract all the matching items + * @param filter the filter to apply + * @param dir the directory to filter + */ + private void recursiveFilter(String filter, File dir) { + // on the root the pathStack is empty + ArrayList<PasswordItem> passwordItems = dir == null ? + PasswordRepository.getPasswords() : + PasswordRepository.getPasswords(dir); + + boolean rec = settings.getBoolean("filter_recursively", true); + for (PasswordItem item : passwordItems) { + if (item.getType() == PasswordItem.TYPE_CATEGORY && rec) { + recursiveFilter(filter, item.getFile()); + } + boolean matches = item.toString().toLowerCase().contains(filter.toLowerCase()); + boolean inAdapter = recyclerAdapter.getValues().contains(item); + if (matches && !inAdapter) { + recyclerAdapter.add(item); + } else if (!matches && inAdapter) { + recyclerAdapter.remove(recyclerAdapter.getValues().indexOf(item)); } } } |
