Fix contrast ratio issue when wallpaper support dark text.

Currently, the background color generated by calculating a proper
contrast ratio between text and background and we use white as text
color to do this calculation. For some wallpaper that support dark text,
EmergencyDialer wil set the text in that dialer to black.
If the text is black, we try to make the background color lighter and
lighter, until the contrast ratio follows the accessibility rules.

Bug: 154290456
Test: Manually set wallpaper to dark-text-support wallpapers
Change-Id: I696e3f80c17bdc5ebaa960bc9a5b04f2218039fa
diff --git a/src/com/android/phone/EmergencyDialer.java b/src/com/android/phone/EmergencyDialer.java
index 5d630af..42bfb24 100644
--- a/src/com/android/phone/EmergencyDialer.java
+++ b/src/com/android/phone/EmergencyDialer.java
@@ -1204,7 +1204,7 @@
         return false;
     }
 
-    private static int getPrimaryColor(WallpaperColors colors) {
+    private int getPrimaryColor(WallpaperColors colors) {
         if (colors != null) {
             // Android accessibility scanner
             // (https://support.google.com/accessibility/android/answer/7158390)
@@ -1212,15 +1212,22 @@
             // 4.5 with background color. The color generated from wallpaper may not
             // follow this rule. Calculate a proper color here.
             Color primary = colors.getPrimaryColor();
-            Color text = Color.valueOf(Color.WHITE);
+            Color text;
+            if (mSupportsDarkText) {
+                text = Color.valueOf(Color.BLACK);
+            } else {
+                text = Color.valueOf(Color.WHITE);
+            }
             Color dial = Color.valueOf(DIALER_GREEN);
             // If current primary color can't follow the contrast ratio rule, make it
-            // deeper and try again.
+            // deeper/lighter and try again.
             while (!checkContrastRatio(primary, text)) {
-                primary = getDeeper(primary);
+                primary = getNextColor(primary, mSupportsDarkText);
             }
-            while (!checkContrastRatio(primary, dial)) {
-                primary = getDeeper(primary);
+            if (!mSupportsDarkText) {
+                while (!checkContrastRatio(primary, dial)) {
+                    primary = getNextColor(primary, mSupportsDarkText);
+                }
             }
             return primary.toArgb();
         }
@@ -1230,17 +1237,21 @@
         return Color.BLACK;
     }
 
-    private static Color getDeeper(Color color) {
-        float r = color.red() - COLOR_DELTA;
-        float g = color.green() - COLOR_DELTA;
-        float b = color.blue() - COLOR_DELTA;
+    private Color getNextColor(Color color, boolean darkText) {
+        float sign = darkText ? 1.f : -1.f;
+        float r = color.red() + sign * COLOR_DELTA;
+        float g = color.green() + sign * COLOR_DELTA;
+        float b = color.blue() + sign * COLOR_DELTA;
         if (r < 0f) r = 0f;
         if (g < 0f) g = 0f;
         if (b < 0f) b = 0f;
+        if (r > 1f) r = 1f;
+        if (g > 1f) g = 1f;
+        if (b > 1f) b = 1f;
         return Color.valueOf(r, g, b);
     }
 
-    private static boolean checkContrastRatio(Color color1, Color color2) {
+    private boolean checkContrastRatio(Color color1, Color color2) {
         float lum1 = color1.luminance();
         float lum2 = color2.luminance();
         double cr;