From a77ff03ca54033be7cc47b2c62239bf2584c82ae Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 9 Apr 2017 00:21:43 +0200 Subject: [PATCH] Add filter functionality to the app blacklist activity --- CHANGELOG.md | 1 + .../activities/AppBlacklistActivity.java | 17 ++++++ .../adapter/AppBlacklistAdapter.java | 60 ++++++++++++++++++- .../main/res/layout/activity_appblacklist.xml | 12 +++- .../main/res/layout/item_with_checkbox.xml | 4 +- app/src/main/res/xml/changelog_master.xml | 1 + 6 files changed, 90 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9ade88a3..730debff7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Changed colours: deep sleep is now dark blue, light sleep is now light blue * Support for exporting and importing of preferences in addition to the database * Visual improvements of the pie charts +* Add filter by name in the App blacklist activity * Pebble: improve compatibility with watch app configuration pages * HPlus: users can now decide whether they want to pair the device or not, hopefully fixing some connection problems (#642) * HPlus: display battery state and warn on low battery diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java index bf4632686..b83956cd6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java @@ -27,6 +27,7 @@ import android.support.v4.app.NavUtils; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.SearchView; import android.view.MenuItem; import org.slf4j.Logger; @@ -67,6 +68,22 @@ public class AppBlacklistActivity extends GBActivity { appListView.setAdapter(appBlacklistAdapter); + SearchView searchView = (SearchView) findViewById(R.id.appListViewSearch); + searchView.setIconifiedByDefault(false); + searchView.setIconified(false); + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + appBlacklistAdapter.getFilter().filter(newText); + return true; + } + }); + IntentFilter filter = new IntentFilter(); filter.addAction(GBApplication.ACTION_QUIT); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AppBlacklistAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AppBlacklistAdapter.java index 813e1e32a..b2b2a3c12 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AppBlacklistAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AppBlacklistAdapter.java @@ -8,9 +8,12 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; +import android.widget.Filter; +import android.widget.Filterable; import android.widget.ImageView; import android.widget.TextView; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.IdentityHashMap; @@ -19,14 +22,15 @@ import java.util.List; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; -public class AppBlacklistAdapter extends RecyclerView.Adapter { +public class AppBlacklistAdapter extends RecyclerView.Adapter implements Filterable { - private final List applicationInfoList; + private List applicationInfoList; private final int mLayoutId; private final Context mContext; private final PackageManager mPm; private final IdentityHashMap mNameMap; + private ApplicationFilter applicationFilter; public AppBlacklistAdapter(int layoutId, Context context) { mLayoutId = layoutId; @@ -96,6 +100,13 @@ public class AppBlacklistAdapter extends RecyclerView.Adapter originalList; + private final List filteredList; + + private ApplicationFilter(AppBlacklistAdapter adapter, List originalList) { + super(); + this.originalList = new ArrayList<>(originalList); + this.filteredList = new ArrayList<>(); + this.adapter = adapter; + } + + @Override + protected Filter.FilterResults performFiltering(CharSequence filter) { + filteredList.clear(); + final Filter.FilterResults results = new Filter.FilterResults(); + + if (filter == null || filter.length() == 0) + filteredList.addAll(originalList); + else { + final String filterPattern = filter.toString().toLowerCase().trim(); + + for (ApplicationInfo ai : originalList) { + CharSequence name = mPm.getApplicationLabel(ai); + if (((String) name).contains(filterPattern) || + (ai.packageName.contains(filterPattern))) { + filteredList.add(ai); + } + } + } + + results.values = filteredList; + results.count = filteredList.size(); + return results; + } + + @Override + protected void publishResults(CharSequence charSequence, Filter.FilterResults filterResults) { + adapter.applicationInfoList.clear(); + adapter.applicationInfoList.addAll((List) filterResults.values); + adapter.notifyDataSetChanged(); + } + } + } diff --git a/app/src/main/res/layout/activity_appblacklist.xml b/app/src/main/res/layout/activity_appblacklist.xml index 08f0445b4..d6010770f 100644 --- a/app/src/main/res/layout/activity_appblacklist.xml +++ b/app/src/main/res/layout/activity_appblacklist.xml @@ -4,11 +4,21 @@ android:layout_height="match_parent" tools:context="nodomain.freeyourgadget.gadgetbridge.activities.AppBlacklistActivity"> + + + + diff --git a/app/src/main/res/layout/item_with_checkbox.xml b/app/src/main/res/layout/item_with_checkbox.xml index 45ffc8ff2..b204546dc 100644 --- a/app/src/main/res/layout/item_with_checkbox.xml +++ b/app/src/main/res/layout/item_with_checkbox.xml @@ -47,10 +47,10 @@ + android:text="Item Description" + android:textAppearance="@style/TextAppearance.AppCompat.Small" /> \ No newline at end of file diff --git a/app/src/main/res/xml/changelog_master.xml b/app/src/main/res/xml/changelog_master.xml index f15918ae0..2365e27e8 100644 --- a/app/src/main/res/xml/changelog_master.xml +++ b/app/src/main/res/xml/changelog_master.xml @@ -5,6 +5,7 @@ Changed colours: deep sleep is now dark blue, light sleep is now light blue Support for exporting and importing of preferences in addition to the database Visual improvements of the pie charts + Add filter by name in the App blacklist activity Pebble: improve compatibility with watch app configuration pages HPlus: users can now decide whether they want to pair the device or not, hopefully fixing some connection problems (#642) HPlus: display battery state and warn on low battery