Commit 5e62781d authored by twellington's avatar twellington Committed by Commit bot

Fix ChromeSwitchCompat thumb drawable and tint color

This is a temporary workaround until we can roll the support library with
their fixes for these issues.

BUG=455327

Review URL: https://codereview.chromium.org/912533003

Cr-Commit-Position: refs/heads/master@{#315434}
parent aae1c580
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<!-- TODO(twellington): remove after support library is rolled to pick up their fix for
the thumb drawable not always being visible (crbug.com/440601). -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_checked="true"
android:color="@color/pref_accent_color" />
<item android:state_enabled="true" android:state_checked="false"
android:color="#ececec" />
<item android:color="#b9b9b9" />
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<!-- TODO(twellington): remove after support library is rolled to pick up their fix for
the thumb drawable not always being visible (crbug.com/440601). -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/btn_switch_to_on_mtrl_00012" />
<item android:drawable="@drawable/btn_switch_to_on_mtrl_00001" />
</selector>
\ No newline at end of file
...@@ -5,12 +5,17 @@ ...@@ -5,12 +5,17 @@
package org.chromium.chrome.browser.widget; package org.chromium.chrome.browser.widget;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.SwitchCompat; import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo; import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Switch; import android.widget.Switch;
import org.chromium.chrome.R;
/** /**
* This class fixes the accessibility of SwitchCompat so it's read as "on switch" instead of * This class fixes the accessibility of SwitchCompat so it's read as "on switch" instead of
* "checkbox checked". http://crbug.com/441702 * "checkbox checked". http://crbug.com/441702
...@@ -19,15 +24,24 @@ import android.widget.Switch; ...@@ -19,15 +24,24 @@ import android.widget.Switch;
* "Checkbox". This works around the bug by marking accessibility events from SwitchCompat with the * "Checkbox". This works around the bug by marking accessibility events from SwitchCompat with the
* Switch class name, which TalkBack recognizes. * Switch class name, which TalkBack recognizes.
* *
* It also fixes the thumb drawable being too small and disappearing on certain devices
* and the disabled tint color being incorrect. http://crbug.com/455327
*
* TODO(newt): Delete this class once the support library is fixed. http://b/19110477 * TODO(newt): Delete this class once the support library is fixed. http://b/19110477
*/ */
public class ChromeSwitchCompat extends SwitchCompat { public class ChromeSwitchCompat extends SwitchCompat {
private Drawable mThumbDrawable;
private ColorStateList mTint;
/** /**
* Constructor for inflating from XML. * Constructor for inflating from XML.
*/ */
public ChromeSwitchCompat(Context context, AttributeSet attrs) { public ChromeSwitchCompat(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
setThumbResource(R.drawable.switch_thumb);
mThumbDrawable = getThumbDrawable();
mThumbDrawable.mutate();
mTint = getResources().getColorStateList(R.color.switch_thumb_tint);
} }
@Override @Override
...@@ -41,4 +55,17 @@ public class ChromeSwitchCompat extends SwitchCompat { ...@@ -41,4 +55,17 @@ public class ChromeSwitchCompat extends SwitchCompat {
super.onInitializeAccessibilityNodeInfo(info); super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(Switch.class.getName()); info.setClassName(Switch.class.getName());
} }
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
updateThumbTintColor();
}
private boolean updateThumbTintColor() {
if (mTint == null) return false;
mThumbDrawable.setColorFilter(mTint.getColorForState(mThumbDrawable.getState(), 0),
PorterDuff.Mode.SRC_IN);
return true;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment