You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
7.3 KiB
221 lines
7.3 KiB
6 years ago
|
package org.purplei2p.lightning.utils;
|
||
9 years ago
|
|
||
7 years ago
|
import android.annotation.TargetApi;
|
||
9 years ago
|
import android.content.Context;
|
||
|
import android.content.res.TypedArray;
|
||
9 years ago
|
import android.graphics.Bitmap;
|
||
|
import android.graphics.Canvas;
|
||
8 years ago
|
import android.graphics.Color;
|
||
9 years ago
|
import android.graphics.ColorFilter;
|
||
|
import android.graphics.Paint;
|
||
|
import android.graphics.PorterDuff;
|
||
|
import android.graphics.PorterDuffColorFilter;
|
||
|
import android.graphics.drawable.Drawable;
|
||
8 years ago
|
import android.os.Build;
|
||
9 years ago
|
import android.support.annotation.AttrRes;
|
||
8 years ago
|
import android.support.annotation.ColorInt;
|
||
9 years ago
|
import android.support.annotation.DrawableRes;
|
||
9 years ago
|
import android.support.annotation.NonNull;
|
||
9 years ago
|
import android.support.v4.content.ContextCompat;
|
||
8 years ago
|
import android.support.v4.graphics.drawable.DrawableCompat;
|
||
9 years ago
|
import android.util.TypedValue;
|
||
|
|
||
6 years ago
|
import org.purplei2p.lightning.R;
|
||
9 years ago
|
|
||
|
public class ThemeUtils {
|
||
|
|
||
|
private static final TypedValue sTypedValue = new TypedValue();
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the primary color of the current theme.
|
||
|
*
|
||
|
* @param context the context to get the theme from.
|
||
|
* @return the primary color of the current theme.
|
||
|
*/
|
||
|
@ColorInt
|
||
9 years ago
|
public static int getPrimaryColor(@NonNull Context context) {
|
||
|
return getColor(context, R.attr.colorPrimary);
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the primary dark color of the current theme.
|
||
|
*
|
||
|
* @param context the context to get the theme from.
|
||
|
* @return the primary dark color of the current theme.
|
||
|
*/
|
||
|
@ColorInt
|
||
9 years ago
|
public static int getPrimaryColorDark(@NonNull Context context) {
|
||
|
return getColor(context, R.attr.colorPrimaryDark);
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the accent color of the current theme.
|
||
|
*
|
||
|
* @param context the context to get the theme from.
|
||
|
* @return the accent color of the current theme.
|
||
|
*/
|
||
|
@ColorInt
|
||
9 years ago
|
public static int getAccentColor(@NonNull Context context) {
|
||
|
return getColor(context, R.attr.colorAccent);
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the color of the status bar as set in styles
|
||
|
* for the current theme.
|
||
|
*
|
||
|
* @param context the context to get the theme from.
|
||
|
* @return the status bar color of the current theme.
|
||
|
*/
|
||
|
@ColorInt
|
||
7 years ago
|
@TargetApi(21)
|
||
|
public static int getStatusBarColor(@NonNull Context context) {
|
||
|
return getColor(context, android.R.attr.statusBarColor);
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the color attribute from the current theme.
|
||
|
*
|
||
|
* @param context the context to get the theme from.
|
||
|
* @param resource the color attribute resource.
|
||
|
* @return the color for the given attribute.
|
||
|
*/
|
||
|
@ColorInt
|
||
8 years ago
|
public static int getColor(@NonNull Context context, @AttrRes int resource) {
|
||
9 years ago
|
TypedArray a = context.obtainStyledAttributes(sTypedValue.data, new int[]{resource});
|
||
|
int color = a.getColor(0, 0);
|
||
|
a.recycle();
|
||
|
return color;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the icon color for the light theme.
|
||
|
*
|
||
|
* @param context the context to use.
|
||
|
* @return the color of the icon.
|
||
|
*/
|
||
8 years ago
|
@ColorInt
|
||
9 years ago
|
public static int getIconLightThemeColor(@NonNull Context context) {
|
||
9 years ago
|
return ContextCompat.getColor(context, R.color.icon_light_theme);
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the icon color for the dark theme.
|
||
|
*
|
||
|
* @param context the context to use.
|
||
|
* @return the color of the icon.
|
||
|
*/
|
||
8 years ago
|
@ColorInt
|
||
9 years ago
|
public static int getIconDarkThemeColor(@NonNull Context context) {
|
||
9 years ago
|
return ContextCompat.getColor(context, R.color.icon_dark_theme);
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the color icon for the light or
|
||
|
* dark theme.
|
||
|
*
|
||
|
* @param context the context to use.
|
||
|
* @param dark true for the dark theme,
|
||
|
* false for the light theme.
|
||
|
* @return the color of the icon.
|
||
|
*/
|
||
8 years ago
|
@ColorInt
|
||
|
public static int getIconThemeColor(@NonNull Context context, boolean dark) {
|
||
|
return (dark) ? getIconDarkThemeColor(context) : getIconLightThemeColor(context);
|
||
|
}
|
||
|
|
||
8 years ago
|
@NonNull
|
||
|
private static Drawable getVectorDrawable(@NonNull Context context, int drawableId) {
|
||
7 years ago
|
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
|
||
7 years ago
|
|
||
|
Preconditions.checkNonNull(drawable);
|
||
|
|
||
8 years ago
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||
|
drawable = (DrawableCompat.wrap(drawable)).mutate();
|
||
|
}
|
||
|
return drawable;
|
||
|
}
|
||
|
|
||
|
// http://stackoverflow.com/a/38244327/1499541
|
||
|
@NonNull
|
||
8 years ago
|
private static Bitmap getBitmapFromVectorDrawable(@NonNull Context context, int drawableId) {
|
||
8 years ago
|
Drawable drawable = getVectorDrawable(context, drawableId);
|
||
|
|
||
7 years ago
|
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
|
||
7 years ago
|
Bitmap.Config.ARGB_8888);
|
||
8 years ago
|
Canvas canvas = new Canvas(bitmap);
|
||
|
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||
|
drawable.draw(canvas);
|
||
|
|
||
|
return bitmap;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the icon with an applied color filter
|
||
|
* for the correct theme.
|
||
|
*
|
||
|
* @param context the context to use.
|
||
|
* @param res the drawable resource to use.
|
||
|
* @param dark true for icon suitable for use with a dark theme,
|
||
|
* false for icon suitable for use with a light theme.
|
||
|
* @return a themed icon.
|
||
|
*/
|
||
8 years ago
|
@NonNull
|
||
8 years ago
|
public static Bitmap getThemedBitmap(@NonNull Context context, @DrawableRes int res, boolean dark) {
|
||
9 years ago
|
int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context);
|
||
7 years ago
|
|
||
8 years ago
|
Bitmap sourceBitmap = getBitmapFromVectorDrawable(context, res);
|
||
7 years ago
|
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(),
|
||
7 years ago
|
Bitmap.Config.ARGB_8888);
|
||
9 years ago
|
Paint p = new Paint();
|
||
|
ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||
|
p.setColorFilter(filter);
|
||
|
Canvas canvas = new Canvas(resultBitmap);
|
||
|
canvas.drawBitmap(sourceBitmap, 0, 0, p);
|
||
|
sourceBitmap.recycle();
|
||
|
return resultBitmap;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the icon with an applied color filter
|
||
|
* for the correct theme.
|
||
|
*
|
||
|
* @param context the context to use.
|
||
|
* @param res the drawable resource to use.
|
||
|
* @param dark true for icon suitable for use with a dark theme,
|
||
|
* false for icon suitable for use with a light theme.
|
||
|
* @return a themed icon.
|
||
|
*/
|
||
8 years ago
|
@NonNull
|
||
9 years ago
|
public static Drawable getThemedDrawable(@NonNull Context context, @DrawableRes int res, boolean dark) {
|
||
9 years ago
|
int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context);
|
||
7 years ago
|
|
||
8 years ago
|
final Drawable drawable = getVectorDrawable(context, res);
|
||
9 years ago
|
drawable.mutate();
|
||
|
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||
|
return drawable;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* The text hint color for dark theme or light theme.
|
||
|
*
|
||
|
* @param dark true for a text color suitable for use with a dark theme,
|
||
|
* false for a text color suitable for use with a light theme.
|
||
|
* @return a text color.
|
||
|
*/
|
||
|
@ColorInt
|
||
8 years ago
|
public static int getThemedTextHintColor(boolean dark) {
|
||
8 years ago
|
return 0x80ffffff & (dark ? Color.WHITE : Color.BLACK);
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Gets the edit text text color for the current theme.
|
||
|
*
|
||
|
* @param context the context to use.
|
||
|
* @return a text color.
|
||
|
*/
|
||
|
@ColorInt
|
||
8 years ago
|
public static int getTextColor(@NonNull Context context) {
|
||
9 years ago
|
return getColor(context, android.R.attr.editTextColor);
|
||
|
}
|
||
9 years ago
|
}
|