session-android/src/org/thoughtcrime/securesms/util/ThemeUtil.java

70 lines
2.2 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.util;
import android.content.Context;
2018-06-26 17:27:44 +00:00
import android.content.res.Resources;
import android.graphics.Color;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
2020-08-20 10:36:56 +00:00
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
2020-08-20 10:36:56 +00:00
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
2019-07-24 02:30:23 +00:00
import network.loki.messenger.R;
public class ThemeUtil {
2020-08-20 10:36:56 +00:00
private static final String TAG = ThemeUtil.class.getSimpleName();
public static boolean isDarkTheme(@NonNull Context context) {
return getAttributeText(context, R.attr.theme_type, "light").equals("dark");
}
@ColorInt
2018-06-26 17:27:44 +00:00
public static int getThemedColor(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.data;
} else {
Log.e(TAG, "Couldn't find a color attribute with id: " + attr);
return Color.RED;
}
}
@DrawableRes
public static int getThemedDrawableResId(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.resourceId;
} else {
Log.e(TAG, "Couldn't find a drawable attribute with id: " + attr);
return 0;
2018-06-26 17:27:44 +00:00
}
}
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
Context contextThemeWrapper = new ContextThemeWrapper(context, theme);
return inflater.cloneInContext(contextThemeWrapper);
}
private static String getAttributeText(Context context, int attribute, String defaultValue) {
TypedValue outValue = new TypedValue();
if (context.getTheme().resolveAttribute(attribute, outValue, true)) {
CharSequence charSequence = outValue.coerceToString();
if (charSequence != null) {
return charSequence.toString();
}
}
return defaultValue;
}
}