IconPalette: Don't crash when the app gives us an invalid color

For notification background. Telegram does it for example.
Just assume contrast is fine in this case.

Log:
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.android.launcher3, PID: 2885
E AndroidRuntime: java.lang.IllegalArgumentException: background can not be translucent: #0
E AndroidRuntime:        at androidx.core.graphics.ColorUtils.calculateContrast(ColorUtils.java:161)
E AndroidRuntime:        at com.android.launcher3.graphics.IconPalette.findContrastColor(IconPalette.java:126)
E AndroidRuntime:        at com.android.launcher3.graphics.IconPalette.ensureTextContrast(IconPalette.java:112)
E AndroidRuntime:        at com.android.launcher3.graphics.IconPalette.resolveContrastColor(IconPalette.java:71)
E AndroidRuntime:        at com.android.launcher3.notification.NotificationInfo.getIconForBackground(NotificationInfo.java:124)
E AndroidRuntime:        at com.android.launcher3.notification.NotificationMainView.applyNotificationInfo(NotificationMainView.java:207)
E AndroidRuntime:        at com.android.launcher3.notification.NotificationContainer.applyNotificationInfos(NotificationContainer.java:156)
E AndroidRuntime:        at com.android.launcher3.popup.PopupContainerWithArrow.applyNotificationInfos(PopupContainerWithArrow.java:353)
E AndroidRuntime:        at com.android.launcher3.popup.PopupPopulator.lambda$createUpdateRunnable$1(PopupPopulator.java:150)
E AndroidRuntime:        at com.android.launcher3.popup.PopupPopulator$$ExternalSyntheticLambda0.run(Unknown Source:4)
...

Signed-off-by: Omkar Chandorkar <gotenksIN@aosip.dev>
Change-Id: I15e61c0610d2676467b57e636454b36aa628007e
This commit is contained in:
Ido Ben-Hur 2021-12-24 20:48:09 +02:00 committed by Jake Weinstein
parent 2653a637c2
commit 4c32facc03
1 changed files with 11 additions and 1 deletions

View File

@ -26,6 +26,8 @@ import androidx.core.graphics.ColorUtils;
import com.android.launcher3.R;
import com.android.launcher3.util.Themes;
import java.lang.IllegalArgumentException;
/**
* Contains colors based on the dominant color of an icon.
*/
@ -107,7 +109,15 @@ public class IconPalette {
* This was copied from com.android.internal.util.NotificationColorUtil.
*/
private static int ensureTextContrast(int color, int bg) {
return findContrastColor(color, bg, 4.5);
int res = color;
try {
res = findContrastColor(color, bg, 4.5);
} catch (IllegalArgumentException e) {
// Just returning the same color in this case
Log.e(TAG, "ensureTextContrast: Invalid fg/bg color int."
+ " fg=" + color + " bg=" + bg);
}
return res;
}
/**
* Finds a suitable color such that there's enough contrast.