That fuck shit the fascists are using
at master 102 lines 3.3 kB view raw
1package org.tm.archive.util; 2 3import android.content.Context; 4import android.content.res.Configuration; 5import android.content.res.Resources; 6import android.graphics.Color; 7import android.graphics.drawable.Drawable; 8import android.util.TypedValue; 9import android.view.LayoutInflater; 10 11import androidx.annotation.AttrRes; 12import androidx.annotation.ColorInt; 13import androidx.annotation.NonNull; 14import androidx.annotation.Nullable; 15import androidx.annotation.StyleRes; 16import androidx.appcompat.content.res.AppCompatResources; 17import androidx.appcompat.view.ContextThemeWrapper; 18 19import org.tm.archive.R; 20 21public class ThemeUtil { 22 23 public static boolean isDarkNotificationTheme(@NonNull Context context) { 24 return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; 25 } 26 27 public static boolean isDarkTheme(@NonNull Context context) { 28 return getAttribute(context, R.attr.theme_type, "light").equals("dark"); 29 } 30 31 public static int getThemedResourceId(@NonNull Context context, @AttrRes int attr) { 32 TypedValue typedValue = new TypedValue(); 33 Resources.Theme theme = context.getTheme(); 34 35 if (theme.resolveAttribute(attr, typedValue, true)) { 36 return typedValue.resourceId; 37 } 38 39 return -1; 40 } 41 42 public static boolean getThemedBoolean(@NonNull Context context, @AttrRes int attr) { 43 TypedValue typedValue = new TypedValue(); 44 Resources.Theme theme = context.getTheme(); 45 46 if (theme.resolveAttribute(attr, typedValue, true)) { 47 return typedValue.data != 0; 48 } 49 50 return false; 51 } 52 53 public static @ColorInt int getThemedColor(@NonNull Context context, @AttrRes int attr) { 54 TypedValue typedValue = new TypedValue(); 55 Resources.Theme theme = context.getTheme(); 56 57 if (theme.resolveAttribute(attr, typedValue, true)) { 58 return typedValue.data; 59 } 60 return Color.RED; 61 } 62 63 public static @Nullable Drawable getThemedDrawable(@NonNull Context context, @AttrRes int attr) { 64 TypedValue typedValue = new TypedValue(); 65 Resources.Theme theme = context.getTheme(); 66 67 if (theme.resolveAttribute(attr, typedValue, true)) { 68 return AppCompatResources.getDrawable(context, typedValue.resourceId); 69 } 70 71 return null; 72 } 73 74 public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) { 75 Context contextThemeWrapper = new ContextThemeWrapper(context, theme); 76 return inflater.cloneInContext(contextThemeWrapper); 77 } 78 79 public static float getThemedDimen(@NonNull Context context, @AttrRes int attr) { 80 TypedValue typedValue = new TypedValue(); 81 Resources.Theme theme = context.getTheme(); 82 83 if (theme.resolveAttribute(attr, typedValue, true)) { 84 return typedValue.getDimension(context.getResources().getDisplayMetrics()); 85 } 86 87 return 0; 88 } 89 90 private static String getAttribute(Context context, int attribute, String defaultValue) { 91 TypedValue outValue = new TypedValue(); 92 93 if (context.getTheme().resolveAttribute(attribute, outValue, true)) { 94 CharSequence charSequence = outValue.coerceToString(); 95 if (charSequence != null) { 96 return charSequence.toString(); 97 } 98 } 99 100 return defaultValue; 101 } 102}