2018-04-20 15:50:03 -07:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-06-26 10:27:44 -07:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.support.annotation.AttrRes;
|
2018-04-20 15:50:03 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2018-11-20 09:59:23 -08:00
|
|
|
import android.support.annotation.StyleRes;
|
|
|
|
import android.support.v7.view.ContextThemeWrapper;
|
2018-04-20 15:50:03 -07:00
|
|
|
import android.util.TypedValue;
|
2018-11-20 09:59:23 -08:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2018-04-20 15:50:03 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
|
|
|
|
public class ThemeUtil {
|
|
|
|
|
|
|
|
public static boolean isDarkTheme(@NonNull Context context) {
|
|
|
|
return getAttribute(context, R.attr.theme_type, "light").equals("dark");
|
|
|
|
}
|
|
|
|
|
2018-06-26 10:27:44 -07: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;
|
|
|
|
}
|
|
|
|
return Color.RED;
|
|
|
|
}
|
|
|
|
|
2018-11-20 09:59:23 -08:00
|
|
|
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
|
|
|
|
Context contextThemeWrapper = new ContextThemeWrapper(context, theme);
|
|
|
|
return inflater.cloneInContext(contextThemeWrapper);
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:50:03 -07:00
|
|
|
private static String getAttribute(Context context, int attribute, String defaultValue) {
|
|
|
|
TypedValue outValue = new TypedValue();
|
|
|
|
|
|
|
|
if (context.getTheme().resolveAttribute(attribute, outValue, true)) {
|
|
|
|
return outValue.coerceToString().toString();
|
|
|
|
} else {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|