aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/util/DrawableTint.java
blob: 8f7e645cd288ee44cf440daaee800a1dc7d5329d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
  This file is part of Subsonic.
	Subsonic is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	Subsonic is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
	Copyright 2015 (C) Scott Jackson
*/

package github.daneren2005.dsub.util;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import android.util.TypedValue;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import github.daneren2005.dsub.R;

public class DrawableTint {
	private static final Map<Integer, Integer> attrMap = new HashMap<>();
	private static final WeakHashMap<Integer, Drawable> tintedDrawables = new WeakHashMap<>();

	public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes) {
		return getTintedDrawable(context, drawableRes, R.attr.colorAccent);
	}
	public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes, @AttrRes int colorAttr) {
		if(tintedDrawables.containsKey(drawableRes)) {
			return tintedDrawables.get(drawableRes);
		}

		int color = getColorRes(context, colorAttr);
		Drawable background = context.getResources().getDrawable(drawableRes);
		background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
		tintedDrawables.put(drawableRes, background);
		return background;
	}
	public static Drawable getTintedDrawableFromColor(Context context, @DrawableRes int drawableRes, @ColorRes int colorRes) {
		if(tintedDrawables.containsKey(drawableRes)) {
			return tintedDrawables.get(drawableRes);
		}

		int color = context.getResources().getColor(colorRes);
		Drawable background = context.getResources().getDrawable(drawableRes);
		background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
		tintedDrawables.put(drawableRes, background);
		return background;
	}
	public static int getColorRes(Context context, @AttrRes int colorAttr) {
		int color;
		if(attrMap.containsKey(colorAttr)) {
			color = attrMap.get(colorAttr);
		} else {
			TypedValue typedValue = new TypedValue();
			Resources.Theme theme = context.getTheme();
			theme.resolveAttribute(colorAttr, typedValue, true);
			color = typedValue.data;
			attrMap.put(colorAttr, color);
		}

		return color;
	}
	public static int getDrawableRes(Context context, @AttrRes int drawableAttr) {
		if(attrMap.containsKey(drawableAttr)) {
			return attrMap.get(drawableAttr);
		} else {
			int[] attrs = new int[]{drawableAttr};
			TypedArray typedArray = context.obtainStyledAttributes(attrs);
			@DrawableRes int drawableRes = typedArray.getResourceId(0, 0);
			typedArray.recycle();
			attrMap.put(drawableAttr, drawableRes);
			return drawableRes;
		}
	}
	public static Drawable getTintedAttrDrawable(Context context, @AttrRes int drawableAttr, @AttrRes int colorAttr) {
		if(tintedDrawables.containsKey(drawableAttr)) {
			return getTintedDrawable(context, attrMap.get(drawableAttr), colorAttr);
		}

		@DrawableRes int drawableRes = getDrawableRes(context, drawableAttr);
		return getTintedDrawable(context, drawableRes, colorAttr);
	}

	public static void clearCache() {
		attrMap.clear();
		tintedDrawables.clear();
	}
}