Merge pull request #53 from zeapo/feature/recursive-filtering

Filter the passwords recursively
This commit is contained in:
Mohamed Zenadi 2014-12-06 18:11:42 +01:00
commit ed26a526d3
4 changed files with 42 additions and 19 deletions

View file

@ -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());
recursiveFilter(filter, pathStack.isEmpty() ? null : 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));
}
/**
* 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));
}
}
}

View file

@ -151,7 +151,7 @@ public class PgpHandler extends ActionBarActivity implements OpenPgpServiceConne
clipboard.setPrimaryClip(clip);
try {
showToast(this.getResources().getString(R.string.clipboard_beginning_toast_text)
+ Integer.parseInt(settings.getString("general_show_time", "45"))
+ " " + Integer.parseInt(settings.getString("general_show_time", "45")) + " "
+ this.getResources().getString(R.string.clipboard_ending_toast_text));
} catch (NumberFormatException e)
{
@ -322,9 +322,6 @@ public class PgpHandler extends ActionBarActivity implements OpenPgpServiceConne
// encrypt/decrypt/sign/verify
if (requestCode == REQUEST_CODE_DECRYPT_AND_VERIFY && os != null) {
try {
Log.d(OpenPgpApi.TAG, "result: " + os.toByteArray().length
+ " str=" + os.toString("UTF-8"));
if (returnToCiphertextField) {
findViewById(R.id.crypto_container).setVisibility(View.VISIBLE);
@ -380,7 +377,6 @@ public class PgpHandler extends ActionBarActivity implements OpenPgpServiceConne
String mKeys = keyIDs.split(",").length > 1 ? keyIDs : keyIDs.split(",")[0];
// ((TextView) findViewById(R.id.crypto_key_ids)).setText(mKeys);
settings.edit().putString("openpgp_key_ids", keyIDs).apply();
Log.i("PGP", mKeys);
}
setResult(RESULT_OK);
finish();

View file

@ -109,7 +109,9 @@
<string name="pref_copy_title">Automatically Copy Password</string>
<string name="pref_copy_dialog_title">Automatically copy the password to the clipboard after decryption was successful.</string>
<string name="ssh_key_error_dialog_title">Error while trying to import the ssh-key</string>
<string name="ssh_key_error_dialog_text">Message : /n</string>
<string name="ssh_key_error_dialog_text">Message : \n</string>
<string name="pref_recursive_filter">Recursive filtering</string>
<string name="pref_recursive_filter_hint">Recursively find passwords of the current directory.</string>
<!-- Misc -->
<string name="dialog_ok">OK</string>

View file

@ -22,10 +22,14 @@
<EditTextPreference android:title="@string/pref_password_title"
android:dialogTitle="@string/pref_password_dialog_title"
android:summary="@string/pref_password_dialog_title"
android:hint="45" android:key="general_show_time" />
android:defaultValue="45" android:key="general_show_time"
android:inputType="number" />
<CheckBoxPreference android:title="@string/pref_copy_title"
android:dialogTitle="@string/pref_copy_dialog_title"
android:summary="@string/pref_copy_dialog_title"
android:key="copy_on_decrypt" android:defaultValue="true" />
<CheckBoxPreference android:title="@string/pref_recursive_filter"
android:summary="@string/pref_recursive_filter_hint"
android:key="filter_recursively" android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>