Skip to content

Commit eed869e

Browse files
committed
Using the filtering mechanism
1 parent b9773e2 commit eed869e

File tree

9 files changed

+228
-126
lines changed

9 files changed

+228
-126
lines changed

java/res/layout/fragment_list_entries.xml

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
android:drawableTop="@drawable/newimage"
2929
android:text="New" />
3030

31+
<EditText
32+
android:id="@+id/editFilter"
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:layout_columnSpan="2"
36+
android:hint="Filter the displayed passwords..."
37+
android:inputType="text" />
38+
3139
<ListView
3240
android:id="@android:id/list"
3341
android:layout_width="fill_parent"

java/src/main/java/org/astonbitecode/rustkeylock/api/InterfaceWithRust.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface EntryCallback extends Callback {
3030
* Callback containing a Set of Entries
3131
*/
3232
interface EntriesSetCallback extends Callback {
33-
void apply(JavaEntriesSet.ByValue entriesSet);
33+
void apply(JavaEntriesSet.ByReference entriesSet, String filter);
3434
}
3535

3636
/**
@@ -66,13 +66,15 @@ void execute(RustCallback showMenuCb, EntryCallback showEntryCb, EntriesSetCallb
6666
void go_to_menu(String menuName);
6767

6868
/**
69-
* Passes a Menu name to Rust and an int argument. Rust instructs the
70-
* callback to go to this menu and use the passed argument
69+
* Passes a Menu name to Rust and an int argument. Rust instructs the callback
70+
* to go to this menu and use the passed argument
7171
*
7272
* @param menuName
73-
* @param arg
73+
* @param argNum
74+
* A String representing an Integer
75+
* @param argStr
7476
*/
75-
void go_to_menu_plus_arg(String menuName, int arg);
77+
void go_to_menu_plus_arg(String menuName, String argNum, String argStr);
7678

7779
/**
7880
* Adds this JavaEntry to the list of Entries in memory. Note: The entry is

java/src/main/java/org/astonbitecode/rustkeylock/callbacks/ShowEntriesSetCb.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,46 @@
99
import org.astonbitecode.rustkeylock.api.JavaEntriesSet;
1010
import org.astonbitecode.rustkeylock.api.JavaEntry;
1111
import org.astonbitecode.rustkeylock.fragments.ListEntries;
12+
import org.astonbitecode.rustkeylock.utils.Defs;
1213

1314
import android.util.Log;
1415

1516
public class ShowEntriesSetCb implements InterfaceWithRust.EntriesSetCallback {
1617
private final String TAG = getClass().getName();
1718

1819
@Override
19-
public void apply(JavaEntriesSet.ByValue entriesSet) {
20+
public void apply(JavaEntriesSet.ByReference entriesSet, String filter) {
2021
Log.d(TAG, "Callback with JavaEntriesSet " + entriesSet.numberOfEntries);
2122
List<JavaEntry> entries;
2223
// Workaround for handling empty list from Rust
23-
if (entriesSet.numberOfEntries == 1 && entriesSet.getEntries().get(0).name.equals("null")
24-
&& entriesSet.getEntries().get(0).user.equals("null")
25-
&& entriesSet.getEntries().get(0).pass.equals("null")
26-
&& entriesSet.getEntries().get(0).desc.equals("null")) {
24+
if (entriesSet.numberOfEntries == 1 && entriesSet.getEntries().get(0).name.equals(Defs.EMPTY_ARG)
25+
&& entriesSet.getEntries().get(0).user.equals(Defs.EMPTY_ARG)
26+
&& entriesSet.getEntries().get(0).pass.equals(Defs.EMPTY_ARG)
27+
&& entriesSet.getEntries().get(0).desc.equals(Defs.EMPTY_ARG)) {
2728
entries = new ArrayList<>();
2829
} else {
2930
entries = entriesSet.getEntries();
3031
}
3132

3233
MainActivity mainActivity = MainActivity.getActiveActivity();
33-
Runnable uiRunnable = new UiThreadRunnable(entries, mainActivity);
34+
Runnable uiRunnable = new UiThreadRunnable(entries, filter, mainActivity);
3435
mainActivity.runOnUiThread(uiRunnable);
3536
}
3637

3738
private class UiThreadRunnable implements Runnable {
3839
private List<JavaEntry> entries = null;
3940
private MainActivity mainActivity = null;
41+
private String filter = null;
4042

41-
public UiThreadRunnable(List<JavaEntry> entries, MainActivity mainActivity) {
43+
public UiThreadRunnable(List<JavaEntry> entries, String filter, MainActivity mainActivity) {
4244
this.entries = entries;
4345
this.mainActivity = mainActivity;
46+
this.filter = filter.equals(Defs.EMPTY_ARG) ? "" : filter;
4447
}
4548

4649
@Override
4750
public void run() {
48-
ListEntries le = new ListEntries(entries);
51+
ListEntries le = new ListEntries(entries, filter);
4952
mainActivity.setBackButtonHandler(le);
5053
mainActivity.getFragmentManager().beginTransaction().replace(R.id.container, le).commitAllowingStateLoss();
5154
}

java/src/main/java/org/astonbitecode/rustkeylock/fragments/ListEntries.java

+57-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Timer;
6+
import java.util.TimerTask;
57

68
import org.astonbitecode.rustkeylock.R;
79
import org.astonbitecode.rustkeylock.adapters.EntriesAdapter;
@@ -13,38 +15,78 @@
1315
import android.app.ListFragment;
1416
import android.content.Context;
1517
import android.os.Bundle;
18+
import android.text.Editable;
19+
import android.text.TextWatcher;
1620
import android.util.Log;
1721
import android.view.LayoutInflater;
1822
import android.view.View;
1923
import android.view.View.OnClickListener;
2024
import android.view.ViewGroup;
2125
import android.view.inputmethod.InputMethodManager;
2226
import android.widget.Button;
27+
import android.widget.EditText;
2328
import android.widget.ListView;
2429

2530
public class ListEntries extends ListFragment implements OnClickListener, BackButtonHandler {
2631
private static final long serialVersionUID = 8765819759487480794L;
2732
private final String TAG = getClass().getName();
2833
private List<JavaEntry> entries;
2934
private EntriesAdapter entriesAdapter;
35+
private String filter;
3036

3137
public ListEntries() {
3238
this.entries = new ArrayList<>();
3339
}
3440

35-
public ListEntries(List<JavaEntry> entries) {
41+
public ListEntries(List<JavaEntry> entries, String filter) {
3642
this.entries = entries;
43+
this.filter = filter;
3744
}
3845

3946
@Override
4047
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
48+
restore(savedInstanceState);
4149
if (savedInstanceState != null) {
42-
InterfaceWithRust.INSTANCE.go_to_menu(Defs.MENU_ENTRIES_LIST);
50+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_ENTRIES_LIST, Defs.EMPTY_ARG, filter);
4351
}
4452
View rootView = inflater.inflate(R.layout.fragment_list_entries, container, false);
4553
Button nb = (Button) rootView.findViewById(R.id.addNewButton);
4654
nb.setOnClickListener(this);
4755

56+
EditText filterText = (EditText) rootView.findViewById(R.id.editFilter);
57+
filterText.setText(filter);
58+
filterText.addTextChangedListener(new TextWatcher() {
59+
private Timer timer = new Timer();
60+
private final long DELAY = 500;
61+
62+
@Override
63+
public void onTextChanged(CharSequence s, int start, int before, int count) {
64+
// ignore
65+
}
66+
67+
@Override
68+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
69+
// ignore
70+
}
71+
72+
@Override
73+
public void afterTextChanged(final Editable s) {
74+
timer.cancel();
75+
timer = new Timer();
76+
timer.schedule(new TimerTask() {
77+
@Override
78+
public void run() {
79+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_ENTRIES_LIST, Defs.EMPTY_ARG,
80+
s != null ? s.toString() : "");
81+
}
82+
}, DELAY);
83+
}
84+
});
85+
if (filter.length() > 0) {
86+
filterText.setFocusableInTouchMode(true);
87+
filterText.requestFocus();
88+
}
89+
4890
// Hide the soft keyboard
4991
final InputMethodManager imm = (InputMethodManager) getActivity()
5092
.getSystemService(Context.INPUT_METHOD_SERVICE);
@@ -64,7 +106,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
64106
public void onListItemClick(ListView l, View v, int pos, long id) {
65107
Log.d(TAG, "Clicked entry with index " + pos + " in the list of entries");
66108
super.onListItemClick(l, v, pos, id);
67-
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_SHOW_ENTRY, pos);
109+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_SHOW_ENTRY, pos + "", Defs.EMPTY_ARG);
68110
}
69111

70112
@Override
@@ -78,4 +120,16 @@ public void onBackButton() {
78120
Log.d(TAG, "Back button pressed");
79121
InterfaceWithRust.INSTANCE.go_to_menu(Defs.MENU_MAIN);
80122
}
123+
124+
@Override
125+
public void onSaveInstanceState(Bundle state) {
126+
state.putString("filter", filter);
127+
}
128+
129+
private void restore(Bundle state) {
130+
if (state != null) {
131+
filter = state.getString("filter");
132+
}
133+
}
134+
81135
}

java/src/main/java/org/astonbitecode/rustkeylock/fragments/MainMenu.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private void addButtonListeners(View rootView) {
5252
public void onClick(View view) {
5353
if (view.getId() == R.id.listButton) {
5454
Log.d(TAG, "The User Selected to List Entries");
55-
InterfaceWithRust.INSTANCE.go_to_menu(Defs.MENU_ENTRIES_LIST);
55+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_ENTRIES_LIST, Defs.EMPTY_ARG, "");
5656
} else if (view.getId() == R.id.saveButton) {
5757
Log.d(TAG, "The User Selected to Save Entries");
5858
InterfaceWithRust.INSTANCE.go_to_menu(Defs.MENU_SAVE);

java/src/main/java/org/astonbitecode/rustkeylock/fragments/ShowEntry.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void onClick(View view) {
7373
if (view.getId() == R.id.editButton) {
7474
Log.d(TAG, "Clicked edit on entry with id " + entryIndex);
7575

76-
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_EDIT_ENTRY, entryIndex);
76+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_EDIT_ENTRY, entryIndex + "", Defs.EMPTY_ARG);
7777
} else if (view.getId() == R.id.updateButton) {
7878
Log.d(TAG, "Clicked Update for entry with id " + entryIndex);
7979

@@ -106,7 +106,7 @@ public void onClick(View view) {
106106
} else if (view.getId() == R.id.deleteButton) {
107107
Log.d(TAG, "Clicked delete on entry with id " + entryIndex);
108108

109-
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_DELETE_ENTRY, entryIndex);
109+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_DELETE_ENTRY, entryIndex + "", Defs.EMPTY_ARG);
110110
} else if (view.getId() == R.id.areYouSureButton) {
111111
Log.d(TAG, "Clicked confirm deletion on entry with id " + entryIndex);
112112

@@ -155,7 +155,7 @@ private void prepareUiElements(View v) {
155155
@Override
156156
public void onBackButton() {
157157
Log.d(TAG, "Back button pressed");
158-
InterfaceWithRust.INSTANCE.go_to_menu(Defs.MENU_ENTRIES_LIST);
158+
InterfaceWithRust.INSTANCE.go_to_menu_plus_arg(Defs.MENU_ENTRIES_LIST, Defs.EMPTY_ARG, "");
159159
}
160160

161161
@Override

java/src/main/java/org/astonbitecode/rustkeylock/utils/Defs.java

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public class Defs {
1414
public static final String MENU_NEW_ENTRY = "NewEntry";
1515
public static final String MENU_EXPORT_ENTRIES = "ExportEntries";
1616
public static final String MENU_IMPORT_ENTRIES = "ImportEntries";
17+
public static final String EMPTY_ARG = "null";
1718
}

0 commit comments

Comments
 (0)