More conservative dark text calculation

Refactored WallpaperColors to use constrast ratio instead of luminance
for detecting dark pixels. Also using a contrast more conservative than
what GAR requires while decreasing the dark area threshold.

Change-Id: I67b799be4b7ccd50bb3e63c6179d513b9b76446b
Fixes: 76435920
Test: manually set various wallpapers
Test: use new debug flag to verify which pixel is actually dark
diff --git a/core/java/android/app/WallpaperColors.java b/core/java/android/app/WallpaperColors.java
index 60e8a12..626b3be 100644
--- a/core/java/android/app/WallpaperColors.java
+++ b/core/java/android/app/WallpaperColors.java
@@ -25,12 +25,15 @@
 import android.graphics.drawable.Drawable;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.util.Log;
 import android.util.Size;
 
 import com.android.internal.graphics.ColorUtils;
 import com.android.internal.graphics.palette.Palette;
 import com.android.internal.graphics.palette.VariationalKMeansQuantizer;
+import com.android.internal.util.ContrastColorUtil;
 
+import java.io.FileOutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -44,6 +47,8 @@
  */
 public final class WallpaperColors implements Parcelable {
 
+    private static final boolean DEBUG_DARK_PIXELS = false;
+
     /**
      * Specifies that dark text is preferred over the current wallpaper for best presentation.
      * <p>
@@ -83,8 +88,8 @@
     private static final float BRIGHT_IMAGE_MEAN_LUMINANCE = 0.75f;
     // We also check if the image has dark pixels in it,
     // to avoid bright images with some dark spots.
-    private static final float DARK_PIXEL_LUMINANCE = 0.45f;
-    private static final float MAX_DARK_AREA = 0.05f;
+    private static final float DARK_PIXEL_CONTRAST = 6f;
+    private static final float MAX_DARK_AREA = 0.025f;
 
     private final ArrayList<Color> mMainColors;
     private int mColorHints;
@@ -382,8 +387,13 @@
             final int alpha = Color.alpha(pixels[i]);
             // Make sure we don't have a dark pixel mass that will
             // make text illegible.
-            if (luminance < DARK_PIXEL_LUMINANCE && alpha != 0) {
+            final boolean satisfiesTextContrast = ContrastColorUtil
+                    .calculateContrast(pixels[i], Color.BLACK) > DARK_PIXEL_CONTRAST;
+            if (!satisfiesTextContrast && alpha != 0) {
                 darkPixels++;
+                if (DEBUG_DARK_PIXELS) {
+                    pixels[i] = Color.RED;
+                }
             }
             totalLuminance += luminance;
         }
@@ -397,6 +407,18 @@
             hints |= HINT_SUPPORTS_DARK_THEME;
         }
 
+        if (DEBUG_DARK_PIXELS) {
+            try (FileOutputStream out = new FileOutputStream("/data/pixels.png")) {
+                source.setPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(),
+                        source.getHeight());
+                source.compress(Bitmap.CompressFormat.PNG, 100, out);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            Log.d("WallpaperColors", "l: " + meanLuminance + ", d: " + darkPixels +
+                    " maxD: " + maxDarkPixels + " numPixels: " + pixels.length);
+        }
+
         return hints;
     }