From 4d0020ae1d0533984cae124c284724dfd502511a Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Mon, 18 Jun 2018 20:38:37 +0200 Subject: [PATCH] Add the option the blacklist applications also for Pebble Messages Fixes #996 --- .../gadgetbridge/GBApplication.java | 122 +++++++++++++----- .../adapter/AppBlacklistAdapter.java | 32 ++++- .../externalevents/NotificationListener.java | 4 +- .../externalevents/PebbleReceiver.java | 5 + .../gadgetbridge/util/GBPrefs.java | 1 + .../util/ImportExportSharedPreferences.java | 9 +- .../main/res/color/blacklist_checkboxes.xml | 6 + .../main/res/layout/item_app_blacklist.xml | 23 +++- 8 files changed, 158 insertions(+), 44 deletions(-) create mode 100644 app/src/main/res/color/blacklist_checkboxes.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 48a848a49..0d8a374fb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -176,7 +176,8 @@ public class GBApplication extends Application { setLanguage(language); deviceService = createDeviceService(); - loadAppsBlackList(); + loadAppsNotifBlackList(); + loadAppsPebbleBlackList(); loadCalendarsBlackList(); if (isRunningMarshmallowOrLater()) { @@ -372,58 +373,119 @@ public class GBApplication extends Application { return NotificationManager.INTERRUPTION_FILTER_ALL; } - private static HashSet apps_blacklist = null; + private static HashSet apps_notification_blacklist = null; - public static boolean appIsBlacklisted(String packageName) { - if (apps_blacklist == null) { - GB.log("appIsBlacklisted: apps_blacklist is null!", GB.INFO, null); + public static boolean appIsNotifBlacklisted(String packageName) { + if (apps_notification_blacklist == null) { + GB.log("appIsNotifBlacklisted: apps_notification_blacklist is null!", GB.INFO, null); } - return apps_blacklist != null && apps_blacklist.contains(packageName); + return apps_notification_blacklist != null && apps_notification_blacklist.contains(packageName); } - public static void setAppsBlackList(Set packageNames) { + public static void setAppsNotifBlackList(Set packageNames) { if (packageNames == null) { - GB.log("Set null apps_blacklist", GB.INFO, null); - apps_blacklist = new HashSet<>(); + GB.log("Set null apps_notification_blacklist", GB.INFO, null); + apps_notification_blacklist = new HashSet<>(); } else { - apps_blacklist = new HashSet<>(packageNames); + apps_notification_blacklist = new HashSet<>(packageNames); } - GB.log("New apps_blacklist has " + apps_blacklist.size() + " entries", GB.INFO, null); - saveAppsBlackList(); + GB.log("New apps_notification_blacklist has " + apps_notification_blacklist.size() + " entries", GB.INFO, null); + saveAppsNotifBlackList(); } - private static void loadAppsBlackList() { - GB.log("Loading apps_blacklist", GB.INFO, null); - apps_blacklist = (HashSet) sharedPrefs.getStringSet(GBPrefs.PACKAGE_BLACKLIST, null); - if (apps_blacklist == null) { - apps_blacklist = new HashSet<>(); + private static void loadAppsNotifBlackList() { + GB.log("Loading apps_notification_blacklist", GB.INFO, null); + apps_notification_blacklist = (HashSet) sharedPrefs.getStringSet(GBPrefs.PACKAGE_BLACKLIST, null); + if (apps_notification_blacklist == null) { + apps_notification_blacklist = new HashSet<>(); } - GB.log("Loaded apps_blacklist has " + apps_blacklist.size() + " entries", GB.INFO, null); + GB.log("Loaded apps_notification_blacklist has " + apps_notification_blacklist.size() + " entries", GB.INFO, null); } - private static void saveAppsBlackList() { - GB.log("Saving apps_blacklist with " + apps_blacklist.size() + " entries", GB.INFO, null); + private static void saveAppsNotifBlackList() { + GB.log("Saving apps_notification_blacklist with " + apps_notification_blacklist.size() + " entries", GB.INFO, null); SharedPreferences.Editor editor = sharedPrefs.edit(); - if (apps_blacklist.isEmpty()) { + if (apps_notification_blacklist.isEmpty()) { editor.putStringSet(GBPrefs.PACKAGE_BLACKLIST, null); } else { - Prefs.putStringSet(editor, GBPrefs.PACKAGE_BLACKLIST, apps_blacklist); + Prefs.putStringSet(editor, GBPrefs.PACKAGE_BLACKLIST, apps_notification_blacklist); } editor.apply(); } - public static void addAppToBlacklist(String packageName) { - if (apps_blacklist.add(packageName)) { - saveAppsBlackList(); + public static void addAppToNotifBlacklist(String packageName) { + if (apps_notification_blacklist.add(packageName)) { + saveAppsNotifBlackList(); } } - public static synchronized void removeFromAppsBlacklist(String packageName) { - GB.log("Removing from apps_blacklist: " + packageName, GB.INFO, null); - apps_blacklist.remove(packageName); - saveAppsBlackList(); + public static synchronized void removeFromAppsNotifBlacklist(String packageName) { + GB.log("Removing from apps_notification_blacklist: " + packageName, GB.INFO, null); + apps_notification_blacklist.remove(packageName); + saveAppsNotifBlackList(); } + private static HashSet apps_pebblemsg_blacklist = null; + + public static boolean appIsPebbleBlacklisted(String sender) { + if (apps_pebblemsg_blacklist == null) { + GB.log("appIsPebbleBlacklisted: apps_pebblemsg_blacklist is null!", GB.INFO, null); + } + return apps_pebblemsg_blacklist != null && apps_pebblemsg_blacklist.contains(sender); + } + + public static void setAppsPebbleBlackList(Set packageNames) { + if (packageNames == null) { + GB.log("Set null apps_pebblemsg_blacklist", GB.INFO, null); + apps_pebblemsg_blacklist = new HashSet<>(); + } else { + apps_pebblemsg_blacklist = new HashSet<>(packageNames); + } + GB.log("New apps_pebblemsg_blacklist has " + apps_pebblemsg_blacklist.size() + " entries", GB.INFO, null); + saveAppsPebbleBlackList(); + } + + private static void loadAppsPebbleBlackList() { + GB.log("Loading apps_pebblemsg_blacklist", GB.INFO, null); + apps_pebblemsg_blacklist = (HashSet) sharedPrefs.getStringSet(GBPrefs.PACKAGE_PEBBLEMSG_BLACKLIST, null); + if (apps_pebblemsg_blacklist == null) { + apps_pebblemsg_blacklist = new HashSet<>(); + } + GB.log("Loaded apps_pebblemsg_blacklist has " + apps_pebblemsg_blacklist.size() + " entries", GB.INFO, null); + } + + private static void saveAppsPebbleBlackList() { + GB.log("Saving apps_pebblemsg_blacklist with " + apps_pebblemsg_blacklist.size() + " entries", GB.INFO, null); + SharedPreferences.Editor editor = sharedPrefs.edit(); + if (apps_pebblemsg_blacklist.isEmpty()) { + editor.putStringSet(GBPrefs.PACKAGE_PEBBLEMSG_BLACKLIST, null); + } else { + Prefs.putStringSet(editor, GBPrefs.PACKAGE_PEBBLEMSG_BLACKLIST, apps_pebblemsg_blacklist); + } + editor.apply(); + } + + public static void addAppToPebbleBlacklist(String packageName) { + if (apps_pebblemsg_blacklist.add(packageNameToPebbleMsgSender(packageName))) { + saveAppsPebbleBlackList(); + } + } + + public static synchronized void removeFromAppsPebbleBlacklist(String packageName) { + GB.log("Removing from apps_pebblemsg_blacklist: " + packageName, GB.INFO, null); + apps_pebblemsg_blacklist.remove(packageNameToPebbleMsgSender(packageName)); + saveAppsPebbleBlackList(); + } + +public static String packageNameToPebbleMsgSender(String packageName) { + if ("eu.siacs.conversations".equals(packageName)){ + return("Conversations"); + } else if ("net.osmand.plus".equals(packageName)) { + return("OsmAnd"); + } + return packageName; +} + private static HashSet calendars_blacklist = null; public static boolean calendarIsBlacklisted(String calendarDisplayName) { @@ -435,7 +497,7 @@ public class GBApplication extends Application { public static void setCalendarsBlackList(Set calendarNames) { if (calendarNames == null) { - GB.log("Set null apps_blacklist", GB.INFO, null); + GB.log("Set null apps_notification_blacklist", GB.INFO, null); calendars_blacklist = new HashSet<>(); } else { calendars_blacklist = new HashSet<>(calendarNames); 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 6f064168d..a29a65d36 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AppBlacklistAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AppBlacklistAdapter.java @@ -38,6 +38,8 @@ import java.util.List; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import static nodomain.freeyourgadget.gadgetbridge.GBApplication.packageNameToPebbleMsgSender; + public class AppBlacklistAdapter extends RecyclerView.Adapter implements Filterable { private List applicationInfoList; @@ -62,7 +64,7 @@ public class AppBlacklistAdapter extends RecyclerView.Adapter apps_pebble_blacklist = new HashSet<>(); + text=text.replace("[","").replace("]",""); + for (int z=0;z calendars_blacklist = new HashSet<>(); text = text.replace("[", "").replace("]", ""); diff --git a/app/src/main/res/color/blacklist_checkboxes.xml b/app/src/main/res/color/blacklist_checkboxes.xml new file mode 100644 index 000000000..c87828168 --- /dev/null +++ b/app/src/main/res/color/blacklist_checkboxes.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_app_blacklist.xml b/app/src/main/res/layout/item_app_blacklist.xml index b204546dc..e967b54c0 100644 --- a/app/src/main/res/layout/item_app_blacklist.xml +++ b/app/src/main/res/layout/item_app_blacklist.xml @@ -12,19 +12,34 @@ android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" - android:layout_marginStart="16dp" + android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:clickable="false" - android:focusable="false" /> + android:focusable="false" + android:button="@null" + android:text="NOTIF"/> + +