1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 10:56:50 +01:00

Zepp OS: Fix notification icons for work profile apps

This commit is contained in:
José Rebelo 2022-12-08 18:14:07 +00:00
parent f2aaf5aecf
commit 21f217b698
2 changed files with 58 additions and 8 deletions

View File

@ -37,7 +37,6 @@ import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.
import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsConfigService.ConfigArg.SLEEP_HIGH_ACCURACY_MONITORING;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsConfigService.ConfigArg.TEMPERATURE_UNIT;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsConfigService.ConfigArg.TIME_FORMAT;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsConfigService.ConfigGroup;
import android.Manifest;
import android.content.Intent;
@ -73,7 +72,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
@ -107,7 +105,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
import nodomain.freeyourgadget.gadgetbridge.model.Reminder;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.model.WorldClock;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@ -126,6 +123,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue;
import nodomain.freeyourgadget.gadgetbridge.util.MapUtils;
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
@ -2110,11 +2108,9 @@ public abstract class Huami2021Support extends HuamiSupport {
return;
}
final Drawable icon;
try {
icon = getContext().getPackageManager().getApplicationIcon(packageName);
} catch (final PackageManager.NameNotFoundException e) {
LOG.error("Failed to get icon for {}", packageName, e);
final Drawable icon = NotificationUtils.getAppIcon(getContext(), packageName);
if (icon == null) {
LOG.warn("Failed to get icon for {}", packageName);
return;
}

View File

@ -18,13 +18,28 @@
package nodomain.freeyourgadget.gadgetbridge.util;
import android.content.Context;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
public class NotificationUtils {
private static final Logger LOG = LoggerFactory.getLogger(NotificationUtils.class);
@NonNull
public static String getPreferredTextFor(NotificationSpec notificationSpec, int lengthBody, int lengthSubject, Context context) {
switch (notificationSpec.type) {
@ -82,4 +97,43 @@ public class NotificationUtils {
public static String getPreferredTextFor(CallSpec callSpec) {
return StringUtils.getFirstOf(callSpec.name, callSpec.number);
}
@Nullable
public static Drawable getAppIcon(final Context context, final String packageName) {
try {
return context.getPackageManager().getApplicationIcon(packageName);
} catch (final PackageManager.NameNotFoundException ignored) {
LOG.warn("Failed to find icon for {}, attempting fallback", packageName);
return getAppIconFallback(context, packageName);
}
}
/**
* Fallback method to get an app icon - 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) {
try {
final LauncherApps launcherAppsService = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
final List<UserHandle> userProfiles = userManager.getUserProfiles();
// Skip user 0 (current, main user)
for (int i = 1; i < userProfiles.size(); i++) {
final UserHandle userHandle = userProfiles.get(i);
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.warn("Failed to find fallback icon for {}", packageName);
} catch (final Exception e) {
LOG.error("Error during fallback icon search", e);
}
return null;
}
}