diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-01 04:16:45 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-01 14:57:25 +0200 |
commit | 918076a6703268307e7856dcd5f25c4f3dc870ab (patch) | |
tree | 2eb957c32b249401b717afc8f8adaba611af2767 /app/src/main/java | |
parent | 752e61d1c74ac12d59d55c623804fe39187179c0 (diff) |
global: fix theme situation and clean up cruft while adding more cruft
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java')
3 files changed, 41 insertions, 21 deletions
diff --git a/app/src/main/java/com/wireguard/android/widget/fab/AddFloatingActionButton.java b/app/src/main/java/com/wireguard/android/widget/fab/AddFloatingActionButton.java index d83050d9..e688a11a 100644 --- a/app/src/main/java/com/wireguard/android/widget/fab/AddFloatingActionButton.java +++ b/app/src/main/java/com/wireguard/android/widget/fab/AddFloatingActionButton.java @@ -16,6 +16,7 @@ import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.Shape; import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; +import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import com.wireguard.android.R; @@ -38,7 +39,7 @@ public class AddFloatingActionButton extends FloatingActionButton { @Override void init(final Context context, final AttributeSet attributeSet) { final TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.AddFloatingActionButton, 0, 0); - mPlusColor = attr.getColor(R.styleable.AddFloatingActionButton_fab_plusIconColor, getColor(android.R.color.white)); + mPlusColor = attr.getColor(R.styleable.AddFloatingActionButton_fab_plusIconColor, FloatingActionButton.getColorFromTheme(context, android.R.attr.colorBackground, android.R.color.white)); attr.recycle(); super.init(context, attributeSet); @@ -59,7 +60,7 @@ public class AddFloatingActionButton extends FloatingActionButton { } public void setPlusColorResId(@ColorRes final int plusColor) { - setPlusColor(getColor(plusColor)); + setPlusColor(ContextCompat.getColor(getContext(), plusColor)); } @Override diff --git a/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionButton.java b/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionButton.java index 3395ae44..a8d937ef 100644 --- a/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionButton.java +++ b/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionButton.java @@ -15,6 +15,8 @@ import android.graphics.drawable.*; import android.graphics.drawable.ShapeDrawable.ShaderFactory; import android.graphics.drawable.shapes.OvalShape; import android.support.annotation.*; +import android.support.v4.content.ContextCompat; +import android.support.v7.app.AppCompatDelegate; import android.support.v7.widget.AppCompatImageButton; import android.util.AttributeSet; import android.widget.TextView; @@ -56,15 +58,25 @@ public class FloatingActionButton extends AppCompatImageButton { init(context, attrs); } + //TODO(msf): make not terrible + public static int getColorFromTheme(final Context context, final int themeResource, @ColorRes final int fallback) { + TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{themeResource}); + try { + return a.getColor(0, ContextCompat.getColor(context, fallback)); + } finally { + a.recycle(); + } + } + void init(final Context context, final AttributeSet attributeSet) { final TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FloatingActionButton, 0, 0); mColorNormal = attr.getColor(R.styleable.FloatingActionButton_fab_colorNormal, - getColor(R.color.color_accent)); + getColorFromTheme(context, android.R.attr.colorAccent, android.R.color.holo_blue_bright)); mColorPressed = attr.getColor(R.styleable.FloatingActionButton_fab_colorPressed, - getColor(R.color.color_accent_dark)); + darkenOrLightenColor(mColorNormal)); //TODO(msf): use getColorForState on the accent color from theme instead to get darker states mColorDisabled = attr.getColor(R.styleable.FloatingActionButton_fab_colorDisabled, - getColor(android.R.color.darker_gray)); + ContextCompat.getColor(context, android.R.color.darker_gray)); //TODO(msf): load from theme mSize = attr.getInt(R.styleable.FloatingActionButton_fab_size, SIZE_NORMAL); mIcon = attr.getResourceId(R.styleable.FloatingActionButton_fab_icon, 0); mTitle = attr.getString(R.styleable.FloatingActionButton_fab_title); @@ -128,7 +140,7 @@ public class FloatingActionButton extends AppCompatImageButton { } public void setColorNormalResId(@ColorRes final int colorNormal) { - setColorNormal(getColor(colorNormal)); + setColorNormal(ContextCompat.getColor(getContext(), colorNormal)); } /** @@ -146,7 +158,7 @@ public class FloatingActionButton extends AppCompatImageButton { } public void setColorPressedResId(@ColorRes final int colorPressed) { - setColorPressed(getColor(colorPressed)); + setColorPressed(ContextCompat.getColor(getContext(), colorPressed)); } /** @@ -164,7 +176,7 @@ public class FloatingActionButton extends AppCompatImageButton { } public void setColorDisabledResId(@ColorRes final int colorDisabled) { - setColorDisabled(getColor(colorDisabled)); + setColorDisabled(ContextCompat.getColor(getContext(), colorDisabled)); } public boolean isStrokeVisible() { @@ -178,10 +190,6 @@ public class FloatingActionButton extends AppCompatImageButton { } } - int getColor(@ColorRes final int id) { - return getResources().getColor(id); - } - float getDimension(@DimenRes final int id) { return getResources().getDimension(id); } @@ -251,7 +259,7 @@ public class FloatingActionButton extends AppCompatImageButton { if (mIconDrawable != null) { return mIconDrawable; } else if (mIcon != 0) { - return getResources().getDrawable(mIcon, null); + return ContextCompat.getDrawable(getContext(), mIcon); } else { return new ColorDrawable(Color.TRANSPARENT); } @@ -311,7 +319,7 @@ public class FloatingActionButton extends AppCompatImageButton { return shapeDrawable; } - private int opacityToAlpha(final float opacity) { + private static int opacityToAlpha(final float opacity) { return (int) (255f * opacity); } @@ -323,6 +331,19 @@ public class FloatingActionButton extends AppCompatImageButton { return adjustColorBrightness(argb, 1.1f); } + public static int darkenOrLightenColor(final int argb) { + final float[] hsv = new float[3]; + Color.colorToHSV(argb, hsv); + final float factor; + if (hsv[2] < 0.2) + factor = 1.2f; + else + factor = 0.8f; + + hsv[2] = Math.min(hsv[2] * factor, 1f); + return Color.HSVToColor(Color.alpha(argb), hsv); + } + private static int adjustColorBrightness(final int argb, final float factor) { final float[] hsv = new float[3]; Color.colorToHSV(argb, hsv); diff --git a/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionsMenu.java b/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionsMenu.java index 02546e13..2733e77e 100644 --- a/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionsMenu.java +++ b/app/src/main/java/com/wireguard/android/widget/fab/FloatingActionsMenu.java @@ -14,6 +14,7 @@ import android.animation.TimeInterpolator; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; @@ -22,6 +23,7 @@ import android.os.Parcelable; import android.support.annotation.ColorRes; import android.support.annotation.Keep; import android.support.annotation.NonNull; +import android.support.v4.content.ContextCompat; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; import android.view.ContextThemeWrapper; @@ -97,11 +99,11 @@ public class FloatingActionsMenu extends ViewGroup { final TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FloatingActionsMenu, 0, 0); mAddButtonPlusColor = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonPlusIconColor, - getColor(android.R.color.white)); + FloatingActionButton.getColorFromTheme(context, android.R.attr.colorBackground, android.R.color.white)); mAddButtonColorNormal = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorNormal, - getColor(R.color.color_accent)); + FloatingActionButton.getColorFromTheme(context, android.R.attr.colorAccent, android.R.color.holo_blue_bright)); mAddButtonColorPressed = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorPressed, - getColor(R.color.color_accent_dark)); + FloatingActionButton.darkenOrLightenColor(mAddButtonColorNormal)); //TODO(msf): use getColorForState on the accent color from theme instead to get darker states mAddButtonSize = attr.getInt(R.styleable.FloatingActionsMenu_fab_addButtonSize, FloatingActionButton.SIZE_NORMAL); mAddButtonStrokeVisible = attr.getBoolean(R.styleable.FloatingActionsMenu_fab_addButtonStrokeVisible, true); mExpandDirection = attr.getInt(R.styleable.FloatingActionsMenu_fab_expandDirection, EXPAND_UP); @@ -179,10 +181,6 @@ public class FloatingActionsMenu extends ViewGroup { mButtonsCount--; } - private int getColor(@ColorRes final int id) { - return getResources().getColor(id); - } - @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { measureChildren(widthMeasureSpec, heightMeasureSpec); |