diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index 47d9e2d25..4764b1df4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -250,17 +250,6 @@ public class NotificationListener extends NotificationListenerService { super.onDestroy(); } - public String getAppName(String pkg) { - // determinate Source App Name ("Label") - PackageManager pm = getPackageManager(); - try { - return (String) pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)); - } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); - } - return null; - } - @Override public void onNotificationPosted(StatusBarNotification sbn) { onNotificationPosted(sbn, null); @@ -352,7 +341,7 @@ public class NotificationListener extends NotificationListenerService { notificationSpec.when = notification.when; // determinate Source App Name ("Label") - String name = getAppName(source); + String name = NotificationUtils.getApplicationLabel(this, source); if (name != null) { notificationSpec.sourceName = name; } @@ -546,7 +535,7 @@ public class NotificationListener extends NotificationListenerService { // figure out sender String number; - String appName = getAppName(app); + String appName = NotificationUtils.getApplicationLabel(this, app); if (noti.extras.containsKey(Notification.EXTRA_PEOPLE)) { number = noti.extras.getString(Notification.EXTRA_PEOPLE); } else if (noti.extras.containsKey(Notification.EXTRA_TITLE)) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index 2a81233f3..682ee6c3e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -953,20 +953,9 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements int suffixlength = appSuffix.length; if (alertCategory == AlertCategory.CustomHuami) { - String appName; + String appName = "\0" + StringUtils.getFirstOf(notificationSpec.sourceName, "UNKNOWN") + "\0"; prefixlength = 3; - final PackageManager pm = getContext().getPackageManager(); - ApplicationInfo ai = null; - try { - ai = pm.getApplicationInfo(notificationSpec.sourceAppId, 0); - } catch (PackageManager.NameNotFoundException ignored) { - } - if (ai != null) { - appName = "\0" + pm.getApplicationLabel(ai) + "\0"; - } else { - appName = "\0" + "UNKNOWN" + "\0"; - } appSuffix = appName.getBytes(); suffixlength = appSuffix.length; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiNotificationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiNotificationService.java index 81d946e78..ca1b89c2f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiNotificationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiNotificationService.java @@ -35,6 +35,7 @@ import java.util.Locale; import java.util.Queue; import nodomain.freeyourgadget.gadgetbridge.BuildConfig; +import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl; @@ -198,6 +199,9 @@ public class XiaomiNotificationService extends AbstractXiaomiService implements if (notificationSpec.sourceName != null) { notification3.setAppName(notificationSpec.sourceName); + } else { + // Should never happen, but notification is not shown otherwise + notification3.setAppName("UNKNOWN"); } if (notificationSpec.key != null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/NotificationUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/NotificationUtils.java index 9d2f29815..797ba7556 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/NotificationUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/NotificationUtils.java @@ -104,16 +104,37 @@ public class NotificationUtils { return context.getPackageManager().getApplicationIcon(packageName); } catch (final PackageManager.NameNotFoundException ignored) { LOG.warn("Failed to find icon for {}, attempting fallback", packageName); - return getAppIconFallback(context, packageName); + final LauncherActivityInfo launcherActivityInfo = getLauncherActivityInfo(context, packageName); + if (launcherActivityInfo != null) { + return launcherActivityInfo.getIcon(0); + } + + return null; + } + } + + @Nullable + public static String getApplicationLabel(final Context context, final String packageName) { + final PackageManager pm = context.getPackageManager(); + try { + return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0)).toString(); + } catch (final PackageManager.NameNotFoundException ignored) { + LOG.warn("Failed to find application label for {}, attempting fallback", packageName); + final LauncherActivityInfo launcherActivityInfo = getLauncherActivityInfo(context, packageName); + if (launcherActivityInfo != null) { + return launcherActivityInfo.getLabel().toString(); + } + + return null; } } /** - * Fallback method to get an app icon - iterate through all the users and attempt to find the + * Fallback method to get an app info - iterate through all the users and attempt to find the * app. This includes work profiles. */ @Nullable - private static Drawable getAppIconFallback(final Context context, final String packageName) { + private static LauncherActivityInfo getLauncherActivityInfo(final Context context, final String packageName) { try { final LauncherApps launcherAppsService = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); @@ -124,16 +145,18 @@ public class NotificationUtils { final List activityList = launcherAppsService.getActivityList(packageName, userHandle); if (!activityList.isEmpty()) { - LOG.info("Found {} icons for {} in user {}", activityList.size(), packageName, userHandle); - return activityList.get(0).getIcon(0); + LOG.debug("Found {} launcher activity infos for {} in user {}", activityList.size(), packageName, userHandle); + return activityList.get(0); } } - LOG.warn("Failed to find fallback icon for {}", packageName); + LOG.warn("Failed to find launcher activity info for {}", packageName); } catch (final Exception e) { - LOG.error("Error during fallback icon search", e); + LOG.error("Error during launcher activity info search", e); } return null; } + + } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java index 882cff7a0..9e7296ce4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java @@ -119,7 +119,7 @@ public class StringUtils { @NonNull public static String getFirstOf(String first, String second) { - if (first != null && first.length() > 0) { + if (first != null && !first.isEmpty()) { return first; } if (second != null) {