Skip to content

Commit

Permalink
Fixed tinting on pre-lollipop devices for custom widgets (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
poisdeux authored and SyncedSynapse committed Nov 16, 2017
1 parent fa67964 commit ce664a0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
45 changes: 39 additions & 6 deletions app/src/main/java/org/xbmc/kore/ui/widgets/HighlightButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageButton;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;

import org.xbmc.kore.R;
import org.xbmc.kore.utils.Utils;

public class HighlightButton extends AppCompatImageButton {

private int colorFilter;
private int highlightColorFilter;

private boolean highlight;

Expand All @@ -36,16 +38,18 @@ public HighlightButton(Context context) {
public HighlightButton(Context context, AttributeSet attrs) {
super(context, attrs);
setStyle(context);
fixThemeColorForPreLollipop(context);
}

public HighlightButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setStyle(context);
fixThemeColorForPreLollipop(context);
}

public void setHighlight(boolean highlight) {
if (highlight) {
setColorFilter(colorFilter);
setColorFilter(highlightColorFilter);
} else {
clearColorFilter();
}
Expand All @@ -58,11 +62,40 @@ public boolean isHighlighted() {

private void setStyle(Context context) {
if (!this.isInEditMode()) {
TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
new int[]{R.attr.colorAccent});
colorFilter = styledAttributes.getColor(styledAttributes.getIndex(0),
context.getResources().getColor(R.color.accent_default));
TypedArray styledAttributes =
context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
highlightColorFilter = styledAttributes.getColor(styledAttributes.getIndex(0),
context.getResources().getColor(R.color.accent_default));
styledAttributes.recycle();
}
}

/**
* Hack!
* Tinting is not applied on pre-lollipop devices.
* As there is (AFAICT) no proper way to set this manually we simply
* apply the color filter each time the view has been laid out.
* @param context
*/
private void fixThemeColorForPreLollipop(Context context) {
if (Utils.isLollipopOrLater() || this.isInEditMode())
return;

TypedArray styledAttributes =
context.getTheme().obtainStyledAttributes(new int[]{R.attr.defaultButtonColorFilter});
final int colorFilter = styledAttributes.getColor(styledAttributes.getIndex(0),
context.getResources().getColor(R.color.white));
styledAttributes.recycle();

getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (highlight) {
setColorFilter(highlightColorFilter);
} else {
setColorFilter(colorFilter);
}
}
});
}
}
10 changes: 4 additions & 6 deletions app/src/main/java/org/xbmc/kore/ui/widgets/RepeatModeButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import org.xbmc.kore.R;

public class RepeatModeButton extends AppCompatImageButton {
public class RepeatModeButton extends HighlightButton {
public enum MODE {
OFF,
ONE,
Expand All @@ -32,7 +32,6 @@ public enum MODE {

private MODE mode;
private static TypedArray styledAttributes;
private static int accentDefaultColor;

public RepeatModeButton(Context context) {
super(context);
Expand All @@ -55,15 +54,15 @@ public void setMode(MODE mode) {
switch (mode) {
case OFF:
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_repeat_white_24dp));
clearColorFilter();
setHighlight(false);
break;
case ONE:
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(2), R.drawable.ic_repeat_one_white_24dp));
setColorFilter(styledAttributes.getColor(styledAttributes.getIndex(0), accentDefaultColor));
setHighlight(true);
break;
case ALL:
setImageResource(styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_repeat_white_24dp));
setColorFilter(styledAttributes.getColor(styledAttributes.getIndex(0), accentDefaultColor));
setHighlight(true);
break;
}
}
Expand All @@ -77,6 +76,5 @@ private void setStyle(Context context) {
R.attr.colorAccent,
R.attr.iconRepeat,
R.attr.iconRepeatOne});
accentDefaultColor = getContext().getResources().getColor(R.color.accent_default);
}
}

0 comments on commit ce664a0

Please sign in to comment.