From 11428a557aaece7845a8bbe58f7563397a2a0eff Mon Sep 17 00:00:00 2001 From: Eric Hocking Date: Sat, 26 Oct 2019 10:08:11 -0400 Subject: [PATCH 1/4] remove Preconditions.java --- .../lightning/utils/Preconditions.java | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 app/src/main/java/acr/browser/lightning/utils/Preconditions.java diff --git a/app/src/main/java/acr/browser/lightning/utils/Preconditions.java b/app/src/main/java/acr/browser/lightning/utils/Preconditions.java deleted file mode 100644 index 6f8693f7c..000000000 --- a/app/src/main/java/acr/browser/lightning/utils/Preconditions.java +++ /dev/null @@ -1,21 +0,0 @@ -package acr.browser.lightning.utils; - -import androidx.annotation.Nullable; - -public final class Preconditions { - - private Preconditions() {} - - /** - * Ensure that an object is not null - * and throw a RuntimeException if it - * is null. - * - * @param object check nullness on this object. - */ - public static void checkNonNull(@Nullable Object object) { - if (object == null) { - throw new RuntimeException("Object must not be null"); - } - } -} From 851b3126c532d0eb8c5c2db03a6c0185403fd6fc Mon Sep 17 00:00:00 2001 From: Eric Hocking Date: Sat, 26 Oct 2019 10:09:41 -0400 Subject: [PATCH 2/4] convert Preconditions to kotlin --- .../browser/lightning/utils/Preconditions.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app/src/main/java/acr/browser/lightning/utils/Preconditions.kt diff --git a/app/src/main/java/acr/browser/lightning/utils/Preconditions.kt b/app/src/main/java/acr/browser/lightning/utils/Preconditions.kt new file mode 100644 index 000000000..4e959f3ea --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/utils/Preconditions.kt @@ -0,0 +1,17 @@ +package acr.browser.lightning.utils; + +object Preconditions { + + /** + * Ensure that an object is not null + * and throw a RuntimeException if it + * is null. + * + * @param obj check nullness on this object. + */ + fun checkNonNull(obj: Any?) { + if (obj == null) { + throw RuntimeException("Object must not be null") + } + } +} From 234fe219da0faeb378e1c4b9b3337cd788e5d30c Mon Sep 17 00:00:00 2001 From: Eric Hocking Date: Sat, 26 Oct 2019 10:10:08 -0400 Subject: [PATCH 3/4] Remove old java ThemeUtils class --- .../browser/lightning/utils/ThemeUtils.java | 189 ------------------ 1 file changed, 189 deletions(-) delete mode 100644 app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java diff --git a/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java b/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java deleted file mode 100644 index 52a9cddbc..000000000 --- a/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java +++ /dev/null @@ -1,189 +0,0 @@ -package acr.browser.lightning.utils; - -import android.annotation.TargetApi; -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.ColorFilter; -import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.util.TypedValue; - -import acr.browser.lightning.R; -import androidx.annotation.AttrRes; -import androidx.annotation.ColorInt; -import androidx.annotation.DrawableRes; -import androidx.annotation.NonNull; -import androidx.core.content.ContextCompat; -import androidx.core.graphics.drawable.DrawableCompat; - -public final class ThemeUtils { - - private static final TypedValue sTypedValue = new TypedValue(); - - private ThemeUtils() {} - - /** - * 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 - public static int getPrimaryColor(@NonNull Context context) { - return getColor(context, R.attr.colorPrimary); - } - - /** - * 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 - public static int getPrimaryColorDark(@NonNull Context context) { - return getColor(context, R.attr.colorPrimaryDark); - } - - /** - * 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 - public static int getAccentColor(@NonNull Context context) { - return getColor(context, R.attr.colorAccent); - } - - /** - * 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 - @TargetApi(21) - public static int getStatusBarColor(@NonNull Context context) { - return getColor(context, android.R.attr.statusBarColor); - } - - /** - * 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 - public static int getColor(@NonNull Context context, @AttrRes int resource) { - TypedArray a = context.obtainStyledAttributes(sTypedValue.data, new int[]{resource}); - int color = a.getColor(0, 0); - a.recycle(); - return color; - } - - /** - * Gets the icon color for the light theme. - * - * @param context the context to use. - * @return the color of the icon. - */ - @ColorInt - private static int getIconLightThemeColor(@NonNull Context context) { - return ContextCompat.getColor(context, R.color.icon_light_theme); - } - - /** - * Gets the icon color for the dark theme. - * - * @param context the context to use. - * @return the color of the icon. - */ - @ColorInt - private static int getIconDarkThemeColor(@NonNull Context context) { - return ContextCompat.getColor(context, R.color.icon_dark_theme); - } - - /** - * 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. - */ - @ColorInt - public static int getIconThemeColor(@NonNull Context context, boolean dark) { - return (dark) ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); - } - - @NonNull - private static Drawable getVectorDrawable(@NonNull Context context, int drawableId) { - Drawable drawable = ContextCompat.getDrawable(context, drawableId); - - Preconditions.checkNonNull(drawable); - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - drawable = (DrawableCompat.wrap(drawable)).mutate(); - } - return drawable; - } - - // http://stackoverflow.com/a/38244327/1499541 - @NonNull - public static Bitmap getBitmapFromVectorDrawable(@NonNull Context context, int drawableId) { - Drawable drawable = getVectorDrawable(context, drawableId); - - Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), - Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); - drawable.draw(canvas); - - return bitmap; - } - - /** - * 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. - */ - @NonNull - public static Bitmap createThemedBitmap(@NonNull Context context, @DrawableRes int res, boolean dark) { - int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); - - Bitmap sourceBitmap = getBitmapFromVectorDrawable(context, res); - Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(), - Bitmap.Config.ARGB_8888); - 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; - } - - /** - * Gets the edit text text color for the current theme. - * - * @param context the context to use. - * @return a text color. - */ - @ColorInt - public static int getTextColor(@NonNull Context context) { - return getColor(context, android.R.attr.editTextColor); - } -} From 05af3a007003221d684e67d1711cbf7d5bc7f20f Mon Sep 17 00:00:00 2001 From: Eric Hocking Date: Sat, 26 Oct 2019 10:12:31 -0400 Subject: [PATCH 4/4] convert ThemeUtils to kotlin --- .../acr/browser/lightning/utils/ThemeUtils.kt | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 app/src/main/java/acr/browser/lightning/utils/ThemeUtils.kt diff --git a/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.kt b/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.kt new file mode 100644 index 000000000..8e519376b --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.kt @@ -0,0 +1,193 @@ +package acr.browser.lightning.utils; + +import android.annotation.TargetApi +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.graphics.drawable.Drawable +import android.os.Build +import android.util.TypedValue + +import acr.browser.lightning.R; +import androidx.annotation.AttrRes +import androidx.annotation.ColorInt +import androidx.annotation.DrawableRes +import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.DrawableCompat + + +object ThemeUtils { + + private val sTypedValue = TypedValue() + + /** + * 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 + fun getPrimaryColor(context: Context): Int { + return getColor(context, R.attr.colorPrimary) + } + + /** + * 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 + fun getPrimaryColorDark(context: Context): Int { + return getColor(context, R.attr.colorPrimaryDark) + } + + /** + * 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 + fun getAccentColor(context: Context): Int { + return getColor(context, R.attr.colorAccent) + } + + /** + * 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 + @TargetApi(21) + fun getStatusBarColor(context: Context): Int { + return getColor(context, android.R.attr.statusBarColor) + } + + /** + * 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 + fun getColor(context: Context, @AttrRes resource: Int): Int { + val a = context.obtainStyledAttributes(sTypedValue.data, intArrayOf(resource)) + val color = a.getColor(0, 0) + a.recycle() + return color + } + + /** + * Gets the icon color for the light theme. + * + * @param context the context to use. + * @return the color of the icon. + */ + @ColorInt + private fun getIconLightThemeColor(context: Context): Int { + return ContextCompat.getColor(context, R.color.icon_light_theme) + } + + /** + * Gets the icon color for the dark theme. + * + * @param context the context to use. + * @return the color of the icon. + */ + @ColorInt + private fun getIconDarkThemeColor(context: Context): Int { + return ContextCompat.getColor(context, R.color.icon_dark_theme) + } + + /** + * 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. + */ + @ColorInt + fun getIconThemeColor(context: Context, dark: Boolean): Int { + return if (dark) getIconDarkThemeColor(context) else getIconLightThemeColor(context) + } + + private fun getVectorDrawable(context: Context, drawableId: Int): Drawable { + var drawable = ContextCompat.getDrawable(context, drawableId) + + // if it is null we throw a runtime exception + Preconditions.checkNonNull(drawable) + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + drawable = DrawableCompat.wrap(drawable!!).mutate() + } + + return drawable!! + } + + // http://stackoverflow.com/a/38244327/1499541 + fun getBitmapFromVectorDrawable(context: Context, drawableId: Int): Bitmap { + val drawable = getVectorDrawable(context, drawableId) + + val bitmap = Bitmap.createBitmap( + drawable.intrinsicWidth, drawable.intrinsicHeight, + Bitmap.Config.ARGB_8888 + ) + + val canvas = Canvas(bitmap) + drawable.setBounds(0, 0, canvas.width, canvas.height) + drawable.draw(canvas) + + return bitmap + } + + /** + * 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. + */ + fun createThemedBitmap(context: Context, @DrawableRes res: Int, dark: Boolean): Bitmap { + val color = if (dark) getIconDarkThemeColor(context) else getIconLightThemeColor(context) + + val sourceBitmap = getBitmapFromVectorDrawable(context, res) + val resultBitmap = Bitmap.createBitmap( + sourceBitmap.width, sourceBitmap.height, + Bitmap.Config.ARGB_8888 + ) + + val p = Paint() + val filter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN) + p.colorFilter = filter + + val canvas = Canvas(resultBitmap) + canvas.drawBitmap(sourceBitmap, 0f, 0f, p) + sourceBitmap.recycle() + + return resultBitmap + } + + /** + * Gets the edit text text color for the current theme. + * + * @param context the context to use. + * @return a text color. + */ + @ColorInt + fun getTextColor(context: Context): Int { + return getColor(context, android.R.attr.editTextColor) + } + +}