From 056acee017ca6e923827fc9b6286a44a4de31eff Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sat, 30 Dec 2017 18:15:05 +0100 Subject: [PATCH] inherit theme background color dynamically without using hardcoded values --- .../android/vanilla/FullPlaybackActivity.java | 3 +- .../android/vanilla/SlidingView.java | 2 +- .../android/vanilla/ThemeHelper.java | 29 ++++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java b/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java index a08d203d..8ea95b01 100644 --- a/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java +++ b/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java @@ -206,8 +206,7 @@ public class FullPlaybackActivity extends SlidingPlaybackActivity TextView view = new TextView(this); // This will be drawn on top of all other controls, so we flood this view // with a non-alpha color - int[] colors = ThemeHelper.getDefaultCoverColors(this); - view.setBackgroundColor(colors[0]); // background of default cover + view.setBackgroundColor(ThemeHelper.fetchThemeColor(this, android.R.attr.windowBackground)); view.setGravity(Gravity.CENTER); view.setPadding(25, 25, 25, 25); // Make the view clickable so it eats touch events diff --git a/src/ch/blinkenlights/android/vanilla/SlidingView.java b/src/ch/blinkenlights/android/vanilla/SlidingView.java index 5f2c91a6..372f5f3c 100644 --- a/src/ch/blinkenlights/android/vanilla/SlidingView.java +++ b/src/ch/blinkenlights/android/vanilla/SlidingView.java @@ -107,7 +107,7 @@ public class SlidingView extends FrameLayout public SlidingView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - setBackgroundColor(ThemeHelper.getDefaultCoverColors(context)[0]); + setBackgroundColor(ThemeHelper.fetchThemeColor(context, android.R.attr.windowBackground)); mDetector = new GestureDetector(new GestureListener()); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingViewPreferences); diff --git a/src/ch/blinkenlights/android/vanilla/ThemeHelper.java b/src/ch/blinkenlights/android/vanilla/ThemeHelper.java index f5f9e25b..afe51f7b 100644 --- a/src/ch/blinkenlights/android/vanilla/ThemeHelper.java +++ b/src/ch/blinkenlights/android/vanilla/ThemeHelper.java @@ -116,22 +116,25 @@ public class ThemeHelper { } /** - * Hacky function to get the colors needed to draw the default cover - * These colors should actually be attributes, but getting them programatically - * is a big mess + * Fetches a color resource from the current theme + */ + final public static int fetchThemeColor(Context context, int resId) { + TypedArray a = context.obtainStyledAttributes(new int[] { resId }); + int color = a.getColor(0, 0); + a.recycle(); + return color; + } + + /** + * Returns the color to be used to draw the placeholder cover. */ final public static int[] getDefaultCoverColors(Context context) { - int[] colors_holo_yolo = { 0xff000000, 0xff404040 }; - int[] colors_material_light = { 0xffeeeeee, 0xffd6d7d7 }; - int[] colors_material_dark = { 0xff303030, 0xff404040 }; - int[] colors_marshmallow_light = { 0xfffafafa, 0xffd6d7d7 }; - int[] colors_marshmallow_dark = colors_material_dark; if (usesHoloTheme()) // pre material device - return colors_holo_yolo; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) - return usesDarkTheme(context) ? colors_marshmallow_dark : colors_marshmallow_light; - // else - return usesDarkTheme(context) ? colors_material_dark : colors_material_light; + return new int[] { 0xff000000, 0xff404040 }; + + int bg = fetchThemeColor(context, android.R.attr.windowBackground); + int diff = 0x00171717 * (bg > 0xFF888888 ? -1 : 1); + return new int[]{ bg, bg+diff }; } }