1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-30 22:12:55 +01:00

Xiaomi: Fix notification for apps in work profile

This commit is contained in:
José Rebelo 2024-05-28 22:45:57 +01:00
parent cdf21ecdb4
commit 72162469a1
5 changed files with 38 additions and 33 deletions

View File

@ -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)) {

View File

@ -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;
}

View File

@ -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) {

View File

@ -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<LauncherActivityInfo> 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;
}
}

View File

@ -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) {