Skip to content

Commit

Permalink
Release 0.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Fox2Code committed Dec 16, 2022
1 parent 4be39ba commit 5110afa
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 39 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'com.github.Fox2Code'
version = '0.1.7'
version = '0.1.8'

android {
namespace 'com.fox2code.foxcompat'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
19 changes: 2 additions & 17 deletions library/src/main/java/com/fox2code/foxcompat/FoxActivityView.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
16 changes: 1 addition & 15 deletions library/src/main/java/com/fox2code/foxcompat/FoxApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
54 changes: 50 additions & 4 deletions library/src/main/java/com/fox2code/foxcompat/FoxDisplay.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5110afa

Please sign in to comment.