From ccce2924981e38358e710359fee0a9a132eb6047 Mon Sep 17 00:00:00 2001 From: Steve Kondik Date: Fri, 19 Feb 2016 19:12:03 +0100 Subject: [PATCH] cmsdk: Add sanity checks in ColorUtils * Protect against nulls when generating colors from icons. Change-Id: I0ba9540848f7be485e713301f2c6c804bd4522e1 --- src/java/cyanogenmod/util/ColorUtils.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/java/cyanogenmod/util/ColorUtils.java b/src/java/cyanogenmod/util/ColorUtils.java index 345a739..37b4e5d 100644 --- a/src/java/cyanogenmod/util/ColorUtils.java +++ b/src/java/cyanogenmod/util/ColorUtils.java @@ -237,6 +237,9 @@ public class ColorUtils { * @return the dominant Swatch */ public static Palette.Swatch getDominantSwatch(Palette palette) { + if (palette == null || palette.getSwatches().size() == 0) { + return null; + } // find most-represented swatch based on population return Collections.max(palette.getSwatches(), new Comparator() { @Override @@ -273,10 +276,17 @@ public class ColorUtils { if (bitmap != null) { Palette p = Palette.from(bitmap).generate(); + if (p == null) { + return alertColor; + } // First try the dominant color - int iconColor = getDominantSwatch(p).getRgb(); - alertColor = findPerceptuallyNearestSolidColor(iconColor); + final Palette.Swatch dominantSwatch = getDominantSwatch(p); + int iconColor = alertColor; + if (dominantSwatch != null) { + iconColor = dominantSwatch.getRgb(); + alertColor = findPerceptuallyNearestSolidColor(iconColor); + } // Try the most saturated color if we got white or black (boring) if (alertColor == Color.BLACK || alertColor == Color.WHITE) {