diff --git a/library/build.gradle b/library/build.gradle index 10c576c..fe6e66c 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'com.github.Fox2Code' -version = '0.1.7' +version = '0.1.8' android { namespace 'com.fox2code.foxcompat' diff --git a/library/src/main/java/com/fox2code/foxcompat/FoxActivity.java b/library/src/main/java/com/fox2code/foxcompat/FoxActivity.java index ca54379..1db9bc2 100644 --- a/library/src/main/java/com/fox2code/foxcompat/FoxActivity.java +++ b/library/src/main/java/com/fox2code/foxcompat/FoxActivity.java @@ -42,6 +42,7 @@ import androidx.annotation.ColorRes; import androidx.annotation.Dimension; import androidx.annotation.DrawableRes; +import androidx.annotation.IdRes; import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -1026,7 +1027,11 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten } public boolean isLightTheme() { - return FoxDisplay.isLightTheme(this.getTheme()); + return FoxDisplay.isLightThemeSafe(this.getTheme()); + } + + public String resolveId(@IdRes int id) { + return FoxDisplay.resolveId(this, id); } @ColorInt diff --git a/library/src/main/java/com/fox2code/foxcompat/FoxActivityView.java b/library/src/main/java/com/fox2code/foxcompat/FoxActivityView.java index e7806ed..0564d0a 100644 --- a/library/src/main/java/com/fox2code/foxcompat/FoxActivityView.java +++ b/library/src/main/java/com/fox2code/foxcompat/FoxActivityView.java @@ -1,11 +1,12 @@ package com.fox2code.foxcompat; +import static com.fox2code.foxcompat.FoxDisplay.resolveId; + import android.app.Activity; import android.app.Application; import android.content.ComponentName; import android.content.Context; import android.content.ContextWrapper; -import android.content.res.Resources; import android.content.res.TypedArray; import android.os.Bundle; import android.os.Looper; @@ -335,22 +336,6 @@ protected void onRestoreInstanceState(Parcelable state) { } } - static Object resolveId(Context context, int id) { - Object fieldValue; - final Resources resources = context.getResources(); - if (id >= 0) { - try { - fieldValue = resources.getResourceTypeName(id) + '/' + - resources.getResourceEntryName(id); - } catch (Resources.NotFoundException e) { - fieldValue = "id/0x" + Integer.toHexString(id).toUpperCase(); - } - } else { - fieldValue = "NO_ID"; - } - return fieldValue; - } - @NonNull @Override public SavedStateRegistry getSavedStateRegistry() { diff --git a/library/src/main/java/com/fox2code/foxcompat/FoxApplication.java b/library/src/main/java/com/fox2code/foxcompat/FoxApplication.java index af36277..3489a06 100644 --- a/library/src/main/java/com/fox2code/foxcompat/FoxApplication.java +++ b/library/src/main/java/com/fox2code/foxcompat/FoxApplication.java @@ -31,7 +31,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; -import androidx.core.graphics.ColorUtils; import com.fox2code.foxcompat.internal.FoxAlert; import com.fox2code.foxcompat.internal.FoxCompat; @@ -131,20 +130,7 @@ protected void attachBaseContext(Context base) { } public boolean isLightTheme() { - Resources.Theme theme = this.getTheme(); - TypedValue typedValue = new TypedValue(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - theme.resolveAttribute(android.R.attr.isLightTheme, typedValue, true); - if (typedValue.type == TypedValue.TYPE_INT_BOOLEAN) { - return typedValue.data == 1; - } - } - theme.resolveAttribute(android.R.attr.background, typedValue, true); - if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && - typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) { - return ColorUtils.calculateLuminance(typedValue.data) > 0.7D; - } - throw new IllegalStateException("Theme is not a valid theme!"); + return FoxDisplay.isLightThemeSafe(this.getTheme()); } @ColorInt diff --git a/library/src/main/java/com/fox2code/foxcompat/FoxDisplay.java b/library/src/main/java/com/fox2code/foxcompat/FoxDisplay.java index 538ec26..f1ed7d8 100644 --- a/library/src/main/java/com/fox2code/foxcompat/FoxDisplay.java +++ b/library/src/main/java/com/fox2code/foxcompat/FoxDisplay.java @@ -1,6 +1,9 @@ package com.fox2code.foxcompat; +import android.annotation.SuppressLint; import android.app.Activity; +import android.content.Context; +import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Color; import android.os.Build; @@ -10,11 +13,10 @@ import androidx.annotation.ColorInt; import androidx.annotation.Dimension; +import androidx.annotation.IdRes; import androidx.annotation.Px; import androidx.core.graphics.ColorUtils; -import com.fox2code.foxcompat.internal.FoxCompat; - import org.jetbrains.annotations.Contract; public final class FoxDisplay { @@ -32,8 +34,27 @@ public static Display getDisplay(Activity activity) { return activity.getWindowManager().getDefaultDisplay(); } + /** + * @param theme to check + * @return if theme is light theme + * @throws IllegalStateException if unable to determine if theme is dark/light mode. + */ + @Contract("null -> false") + public static boolean isLightTheme(Resources.Theme theme) throws IllegalStateException { + return isLightTheme(theme, true); + } + + /** + * @param theme to check + * @return if theme is light theme, use night mode as fallback + * if unable to determine if theme is dark/light mode. + */ @Contract("null -> false") - public static boolean isLightTheme(Resources.Theme theme) { + public static boolean isLightThemeSafe(Resources.Theme theme) { + return isLightTheme(theme, false); + } + + private static boolean isLightTheme(Resources.Theme theme,boolean doThrow) { if (theme == null) return false; TypedValue typedValue = new TypedValue(); theme.resolveAttribute( // Check with google material first @@ -52,7 +73,12 @@ public static boolean isLightTheme(Resources.Theme theme) { typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) { return ColorUtils.calculateLuminance(typedValue.data) > 0.7D; } - throw new IllegalStateException("Theme is not a valid theme!"); + if (doThrow) { + throw new IllegalStateException("Theme is not a valid theme!"); + } else { + return (theme.getResources().getConfiguration().uiMode & + Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO; + } } @Dimension @Px @@ -66,4 +92,24 @@ public static float pixelsToDp(@Dimension @Px int px){ return (px / ((float) Resources.getSystem().getDisplayMetrics() .densityDpi / DisplayMetrics.DENSITY_DEFAULT)); } + + public static String resolveId(Context context,@IdRes int id) { + return resolveId(context.getResources(), id); + } + + @SuppressLint("ResourceType") + public static String resolveId(Resources resources, @IdRes int id) { + String fieldValue; + if (id >= 0) { + try { + fieldValue = resources.getResourceTypeName(id) + '/' + + resources.getResourceEntryName(id); + } catch (Resources.NotFoundException e) { + fieldValue = "id/0x" + Integer.toHexString(id).toUpperCase(); + } + } else { + fieldValue = "NO_ID"; + } + return fieldValue; + } } diff --git a/library/src/main/java/com/fox2code/foxcompat/FoxThemeWrapper.java b/library/src/main/java/com/fox2code/foxcompat/FoxThemeWrapper.java index e7bcd83..0aeede7 100644 --- a/library/src/main/java/com/fox2code/foxcompat/FoxThemeWrapper.java +++ b/library/src/main/java/com/fox2code/foxcompat/FoxThemeWrapper.java @@ -70,7 +70,7 @@ protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean fi } public boolean isLightTheme() { - return FoxDisplay.isLightTheme(this.getTheme()); + return FoxDisplay.isLightThemeSafe(this.getTheme()); } @ColorInt