MediaRouter: Use default text color for button when contrast is low

Bug: 24336361
Change-Id: Ie763e040e72c63869f11e4fc622a9ca8853c26b7
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index b3a35c4..da85124 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -41,7 +41,6 @@
 import android.support.v7.mediarouter.R;
 import android.text.TextUtils;
 import android.util.Log;
-import android.util.TypedValue;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -276,20 +275,17 @@
 
         ClickListener listener = new ClickListener();
 
+        int color = MediaRouterThemeHelper.getButtonTextColor(mContext);
         mDisconnectButton = (Button) findViewById(BUTTON_DISCONNECT_RES_ID);
         mDisconnectButton.setText(R.string.mr_controller_disconnect);
+        mDisconnectButton.setTextColor(color);
         mDisconnectButton.setOnClickListener(listener);
 
         mStopCastingButton = (Button) findViewById(BUTTON_STOP_RES_ID);
         mStopCastingButton.setText(R.string.mr_controller_stop);
+        mStopCastingButton.setTextColor(color);
         mStopCastingButton.setOnClickListener(listener);
 
-        TypedValue value = new TypedValue();
-        if (mContext.getTheme().resolveAttribute(R.attr.colorPrimary, value, true)) {
-            mDisconnectButton.setTextColor(value.data);
-            mStopCastingButton.setTextColor(value.data);
-        }
-
         mRouteNameTextView = (TextView) findViewById(R.id.mr_name);
         mCloseButton = (ImageButton) findViewById(R.id.mr_close);
         mCloseButton.setOnClickListener(listener);
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java b/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java
index aa0ac32..5d86448 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java
@@ -19,11 +19,16 @@
 import android.content.Context;
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
+import android.support.v4.graphics.ColorUtils;
 import android.support.v7.mediarouter.R;
 import android.util.TypedValue;
 import android.view.ContextThemeWrapper;
 
 final class MediaRouterThemeHelper {
+
+    // http://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html
+    private static final float MIN_CONTRAST_BUTTON_TEXT = 4.5f;
+
     private MediaRouterThemeHelper() {
     }
 
@@ -59,6 +64,22 @@
         return isPrimaryColorLight(context) ? Color.BLACK : Color.WHITE;
     }
 
+    public static int getButtonTextColor(Context context) {
+        int primaryColor = getPrimaryColor(context);
+
+        TypedValue value = new TypedValue();
+        context.getTheme().resolveAttribute(android.R.attr.colorBackground, value, true);
+        int backgroundColor = context.getResources().getColor(value.resourceId);
+
+        double contrast = ColorUtils.calculateContrast(primaryColor, backgroundColor);
+        if (contrast < MIN_CONTRAST_BUTTON_TEXT) {
+            // Default to textColorPrimary if the contrast ratio is low.
+            context.getTheme().resolveAttribute(android.R.attr.textColorPrimary, value, true);
+            return context.getResources().getColor(value.resourceId);
+        }
+        return primaryColor;
+    }
+
     private static boolean isLightTheme(Context context) {
         TypedValue value = new TypedValue();
         return context.getTheme().resolveAttribute(R.attr.isLightTheme, value, true)
@@ -66,18 +87,13 @@
     }
 
     private static boolean isPrimaryColorLight(Context context) {
-        TypedValue value = new TypedValue();
-        context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
-        return luminance(value.data) >= 0.5;
+        int primaryColor = getPrimaryColor(context);
+        return ColorUtils.calculateLuminance(primaryColor) >= 0.5;
     }
 
-    private static float luminance(int color) {
-        double red = Color.red(color) / 255.0;
-        red = red < 0.03928 ? red / 12.92 : Math.pow((red + 0.055) / 1.055, 2.4);
-        double green = Color.green(color) / 255.0;
-        green = green < 0.03928 ? green / 12.92 : Math.pow((green + 0.055) / 1.055, 2.4);
-        double blue = Color.blue(color) / 255.0;
-        blue = blue < 0.03928 ? blue / 12.92 : Math.pow((blue + 0.055) / 1.055, 2.4);
-        return (float) ((0.2126 * red) + (0.7152 * green) + (0.0722 * blue));
+    private static int getPrimaryColor(Context context) {
+        TypedValue value = new TypedValue();
+        context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
+        return value.data;
     }
 }