inherit theme background color dynamically without using hardcoded values

This commit is contained in:
Adrian Ulrich 2017-12-30 18:15:05 +01:00
parent 43f1d345cf
commit 056acee017
3 changed files with 18 additions and 16 deletions

View File

@ -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

View File

@ -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);

View File

@ -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 };
}
}