2014-07-03 07:48:39 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.util;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
2016-03-04 04:54:27 +01:00
|
|
|
import android.content.SharedPreferences;
|
2016-03-12 15:20:56 +01:00
|
|
|
import android.content.res.Configuration;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Color;
|
2014-10-29 07:48:07 +01:00
|
|
|
import android.graphics.PorterDuff.Mode;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.graphics.Typeface;
|
2014-12-01 17:04:55 +01:00
|
|
|
import android.graphics.drawable.ColorDrawable;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Build;
|
2014-08-07 19:58:42 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2015-04-17 04:54:48 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2015-04-03 17:49:08 +02:00
|
|
|
import android.support.v4.app.FragmentActivity;
|
2015-11-21 08:26:10 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-04-03 17:49:08 +02:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2016-03-04 04:54:27 +01:00
|
|
|
import android.support.v7.app.AppCompatDelegate;
|
2015-11-21 08:26:10 +01:00
|
|
|
import android.support.v7.view.menu.ActionMenuItemView;
|
|
|
|
import android.support.v7.widget.ActionBarOverlayLayout;
|
2015-01-17 19:58:29 +01:00
|
|
|
import android.support.v7.widget.ActionMenuView;
|
2015-11-22 04:26:59 +01:00
|
|
|
import android.support.v7.widget.ContentFrameLayout;
|
2015-04-13 18:34:10 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2015-11-22 04:26:59 +01:00
|
|
|
import android.support.v7.widget.TwidereToolbar;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.text.TextUtils;
|
2015-04-27 14:25:26 +02:00
|
|
|
import android.util.TypedValue;
|
2014-10-25 12:45:50 +02:00
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.view.ContextThemeWrapper;
|
2014-10-25 12:45:50 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.view.View;
|
2015-01-17 19:58:29 +01:00
|
|
|
import android.view.View.OnLongClickListener;
|
2015-04-13 18:34:10 +02:00
|
|
|
import android.view.Window;
|
2015-04-14 17:38:50 +02:00
|
|
|
import android.view.WindowManager;
|
2015-09-01 16:42:48 +02:00
|
|
|
import android.widget.FrameLayout;
|
2015-04-13 18:34:10 +02:00
|
|
|
import android.widget.ImageView;
|
2014-07-03 07:48:39 +02:00
|
|
|
|
2015-01-11 10:28:45 +01:00
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.Constants;
|
|
|
|
import org.mariotaku.twidere.R;
|
2016-03-14 09:17:46 +01:00
|
|
|
import org.mariotaku.twidere.activity.LinkHandlerActivity;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.activity.iface.IThemedActivity;
|
2014-12-08 16:16:33 +01:00
|
|
|
import org.mariotaku.twidere.graphic.ActionBarColorDrawable;
|
2015-01-17 19:58:29 +01:00
|
|
|
import org.mariotaku.twidere.graphic.ActionIconDrawable;
|
2016-02-18 17:21:04 +01:00
|
|
|
import org.mariotaku.twidere.graphic.iface.DoNotWrapDrawable;
|
2015-05-17 17:16:54 +02:00
|
|
|
import org.mariotaku.twidere.preference.ThemeBackgroundPreference;
|
2014-11-03 16:44:32 +01:00
|
|
|
import org.mariotaku.twidere.util.menu.TwidereMenuInfo;
|
2015-04-27 14:25:26 +02:00
|
|
|
import org.mariotaku.twidere.util.support.ViewSupport;
|
2015-04-03 17:49:08 +02:00
|
|
|
import org.mariotaku.twidere.view.TabPagerIndicator;
|
2015-11-21 08:26:10 +01:00
|
|
|
|
2015-04-03 23:12:33 +02:00
|
|
|
import java.lang.reflect.Field;
|
2014-07-03 07:48:39 +02:00
|
|
|
|
|
|
|
public class ThemeUtils implements Constants {
|
|
|
|
|
2015-04-26 15:46:23 +02:00
|
|
|
public static final int ACCENT_COLOR_THRESHOLD = 192;
|
2016-01-29 18:38:03 +01:00
|
|
|
public static final int DARK_COLOR_THRESHOLD = 64;
|
|
|
|
|
2015-04-26 12:55:41 +02:00
|
|
|
public static final int[] ATTRS_TEXT_COLOR_PRIMARY = {android.R.attr.textColorPrimary};
|
2015-04-26 15:46:23 +02:00
|
|
|
public static final int[] ATTRS_TEXT_COLOR_PRIMARY_AND_INVERSE = {android.R.attr.textColorPrimary,
|
|
|
|
android.R.attr.textColorPrimaryInverse};
|
|
|
|
public static final int[] ATTRS_COLOR_FOREGROUND_AND_INVERSE = {android.R.attr.colorForeground,
|
|
|
|
android.R.attr.colorForegroundInverse};
|
2014-07-27 14:58:15 +02:00
|
|
|
|
|
|
|
private ThemeUtils() {
|
2015-11-22 04:26:59 +01:00
|
|
|
throw new AssertionError("ThemeUtils should never be instantiated");
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static void applyColorFilterToMenuIcon(final Menu menu, final int color, final int popupColor,
|
|
|
|
final int highlightColor, final Mode mode,
|
|
|
|
final int... excludedGroups) {
|
|
|
|
for (int i = 0, j = menu.size(); i < j; i++) {
|
|
|
|
final MenuItem item = menu.getItem(i);
|
|
|
|
final Drawable icon = item.getIcon();
|
|
|
|
final ContextMenuInfo info = item.getMenuInfo();
|
|
|
|
if (icon != null && !ArrayUtils.contains(excludedGroups, item.getGroupId())) {
|
|
|
|
icon.mutate();
|
|
|
|
if (info instanceof TwidereMenuInfo) {
|
|
|
|
final TwidereMenuInfo sInfo = (TwidereMenuInfo) info;
|
2015-10-12 08:59:17 +02:00
|
|
|
final int stateColor = sInfo.isHighlight() ? sInfo.getHighlightColor(highlightColor) : color;
|
|
|
|
if (stateColor != 0) {
|
|
|
|
icon.setColorFilter(stateColor, mode);
|
|
|
|
}
|
|
|
|
} else if (color != 0) {
|
2015-04-15 13:14:47 +02:00
|
|
|
icon.setColorFilter(color, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item.hasSubMenu()) {
|
|
|
|
// SubMenu item is always in popup
|
|
|
|
applyColorFilterToMenuIcon(item.getSubMenu(), popupColor, popupColor, highlightColor, mode, excludedGroups);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static void applyWindowBackground(@NonNull Context context, @NonNull Window window, String option, int alpha) {
|
|
|
|
if (isWindowFloating(context)) return;
|
2015-04-14 17:38:50 +02:00
|
|
|
if (VALUE_THEME_BACKGROUND_TRANSPARENT.equals(option)) {
|
|
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
|
2016-03-01 06:37:56 +01:00
|
|
|
window.setBackgroundDrawable(getWindowBackgroundFromThemeApplyAlpha(context, alpha));
|
2015-04-14 17:38:50 +02:00
|
|
|
} else if (VALUE_THEME_BACKGROUND_SOLID.equals(option)) {
|
2016-03-01 06:37:56 +01:00
|
|
|
window.setBackgroundDrawable(new ColorDrawable(isDarkTheme(context) ? Color.BLACK : Color.WHITE));
|
2015-05-05 07:46:29 +02:00
|
|
|
} else {
|
2016-03-01 06:37:56 +01:00
|
|
|
window.setBackgroundDrawable(getWindowBackgroundFromTheme(context));
|
2015-04-14 17:38:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static void applyWindowBackground(Context context, View window, String option, int alpha) {
|
|
|
|
if (isWindowFloating(context)) return;
|
2015-04-15 13:14:47 +02:00
|
|
|
if (VALUE_THEME_BACKGROUND_TRANSPARENT.equals(option)) {
|
2016-03-01 06:37:56 +01:00
|
|
|
ViewSupport.setBackground(window, getWindowBackgroundFromThemeApplyAlpha(context, alpha));
|
2015-04-15 13:14:47 +02:00
|
|
|
} else if (VALUE_THEME_BACKGROUND_SOLID.equals(option)) {
|
2016-03-01 06:37:56 +01:00
|
|
|
ViewSupport.setBackground(window, new ColorDrawable(isDarkTheme(context) ? Color.BLACK : Color.WHITE));
|
2015-04-15 13:14:47 +02:00
|
|
|
} else {
|
2016-03-01 06:37:56 +01:00
|
|
|
ViewSupport.setBackground(window, getWindowBackgroundFromTheme(context));
|
2015-04-04 00:06:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 17:38:50 +02:00
|
|
|
|
2015-04-16 22:29:36 +02:00
|
|
|
@NonNull
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Drawable getActionBarBackground(final Context context, final int accentColor,
|
|
|
|
final String backgroundOption, final boolean outlineEnabled) {
|
2015-04-16 22:29:36 +02:00
|
|
|
final int actionBarColor;
|
2016-03-01 06:37:56 +01:00
|
|
|
if (!isDarkTheme(context)) {
|
2015-04-16 22:29:36 +02:00
|
|
|
actionBarColor = accentColor;
|
2015-05-11 18:36:18 +02:00
|
|
|
} else if (isSolidBackground(backgroundOption)) {
|
|
|
|
actionBarColor = Color.BLACK;
|
|
|
|
} else {
|
|
|
|
actionBarColor = context.getResources().getColor(R.color.background_color_action_bar_dark);
|
2015-04-16 22:29:36 +02:00
|
|
|
}
|
2015-04-17 16:36:04 +02:00
|
|
|
return ActionBarColorDrawable.create(actionBarColor, outlineEnabled);
|
2015-04-16 22:29:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
@NonNull
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Drawable getActionBarStackedBackground(final Context context,
|
2015-05-12 17:47:52 +02:00
|
|
|
final int accentColor, String backgroundOption,
|
|
|
|
boolean outlineEnabled) {
|
2016-03-01 06:37:56 +01:00
|
|
|
return getActionBarBackground(context, accentColor, backgroundOption, outlineEnabled);
|
2015-04-13 18:34:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 16:36:04 +02:00
|
|
|
public static int getCardBackgroundColor(final Context context, String backgroundOption, int themeAlpha) {
|
2015-04-15 13:14:47 +02:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.cardItemBackgroundColor});
|
|
|
|
final int color = a.getColor(0, Color.TRANSPARENT);
|
|
|
|
a.recycle();
|
2015-04-17 16:36:04 +02:00
|
|
|
if (isTransparentBackground(backgroundOption)) {
|
|
|
|
return themeAlpha << 24 | (0x00FFFFFF & color);
|
|
|
|
} else if (isSolidBackground(backgroundOption)) {
|
2015-04-22 10:36:56 +02:00
|
|
|
return TwidereColorUtils.getContrastYIQ(color, Color.WHITE, Color.BLACK);
|
2015-04-17 16:36:04 +02:00
|
|
|
} else {
|
|
|
|
return color;
|
|
|
|
}
|
2015-03-19 17:19:57 +01:00
|
|
|
}
|
2015-01-17 19:58:29 +01:00
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static Drawable getCompatToolbarOverlay(Activity activity) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return null;
|
2015-04-17 09:17:22 +02:00
|
|
|
final Window window = activity.getWindow();
|
|
|
|
final View view = window.findViewById(android.support.v7.appcompat.R.id.decor_content_parent);
|
|
|
|
if (!(view instanceof ActionBarOverlayLayout)) {
|
|
|
|
final View contentLayout = window.findViewById(android.support.v7.appcompat.R.id.action_bar_activity_content);
|
|
|
|
if (contentLayout instanceof ContentFrameLayout) {
|
2015-09-01 15:24:45 +02:00
|
|
|
return contentLayout.getForeground();
|
2015-04-17 09:17:22 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
try {
|
|
|
|
final Field field = ActionBarOverlayLayout.class.getDeclaredField("mWindowContentOverlay");
|
|
|
|
field.setAccessible(true);
|
|
|
|
return (Drawable) field.get(view);
|
|
|
|
} catch (Exception ignore) {
|
2015-04-13 18:34:10 +02:00
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
return null;
|
2015-04-13 18:34:10 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static int getContrastForegroundColor(Context context, int color) {
|
2015-05-02 19:39:06 +02:00
|
|
|
final int[] colors = new int[2];
|
2016-03-01 06:37:56 +01:00
|
|
|
getDarkLightForegroundColors(context, colors);
|
|
|
|
if (isDarkTheme(context) || TwidereColorUtils.getYIQLuminance(color) <= ACCENT_COLOR_THRESHOLD) {
|
2015-04-17 09:17:22 +02:00
|
|
|
//return light text color
|
2015-05-02 19:39:06 +02:00
|
|
|
return colors[1];
|
2015-04-17 09:17:22 +02:00
|
|
|
}
|
|
|
|
//return dark text color
|
2015-05-02 19:39:06 +02:00
|
|
|
return colors[0];
|
2015-04-17 09:17:22 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 10:14:50 +01:00
|
|
|
|
|
|
|
public static int getContrastColor(int color, int darkColor, int lightColor) {
|
|
|
|
if (TwidereColorUtils.getYIQLuminance(color) <= ACCENT_COLOR_THRESHOLD) {
|
|
|
|
//return light text color
|
|
|
|
return lightColor;
|
|
|
|
}
|
|
|
|
//return dark text color
|
|
|
|
return darkColor;
|
|
|
|
}
|
|
|
|
|
2015-05-02 14:55:16 +02:00
|
|
|
public static int getContrastActionBarItemColor(Context context) {
|
2015-05-02 19:39:06 +02:00
|
|
|
return getColorFromAttribute(context, android.R.attr.colorForeground, 0);
|
2015-04-17 09:17:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static int getDialogThemeResource(final Context context) {
|
|
|
|
return getDialogThemeResource(getThemeNameOption(context));
|
2015-01-17 19:58:29 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static int getDialogThemeResource(final String name) {
|
2016-03-01 06:37:56 +01:00
|
|
|
if (VALUE_THEME_NAME_DARK.equals(name)) return R.style.Theme_Twidere_Dialog;
|
|
|
|
return R.style.Theme_Twidere_Dialog;
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static Context getDialogThemedContext(final Context context) {
|
|
|
|
return new ContextThemeWrapper(context, getDialogThemeResource(context));
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static Drawable getImageHighlightDrawable(final Context context) {
|
|
|
|
final Drawable d = getSelectableItemBackgroundDrawable(context);
|
|
|
|
if (d != null) {
|
|
|
|
d.setAlpha(0x80);
|
2014-10-25 12:45:50 +02:00
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
return d;
|
2014-10-25 12:45:50 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 18:38:03 +01:00
|
|
|
public static int getOptimalAccentColor(int accentColor, int foregroundColor) {
|
2015-01-08 10:13:20 +01:00
|
|
|
final int[] yiq = new int[3];
|
2016-01-29 18:38:03 +01:00
|
|
|
TwidereColorUtils.colorToYIQ(foregroundColor, yiq);
|
|
|
|
final int foregroundColorY = yiq[0];
|
|
|
|
TwidereColorUtils.colorToYIQ(accentColor, yiq);
|
|
|
|
if (foregroundColorY < DARK_COLOR_THRESHOLD && yiq[0] <= ACCENT_COLOR_THRESHOLD) {
|
|
|
|
return accentColor;
|
|
|
|
} else if (foregroundColorY > ACCENT_COLOR_THRESHOLD && yiq[0] > DARK_COLOR_THRESHOLD) {
|
|
|
|
return accentColor;
|
2015-01-08 10:13:20 +01:00
|
|
|
}
|
2016-01-29 18:38:03 +01:00
|
|
|
yiq[0] = yiq[0] + (foregroundColorY - yiq[0]) / 2;
|
|
|
|
return TwidereColorUtils.YIQToColor(Color.alpha(accentColor), yiq);
|
2015-01-08 10:13:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static Resources getResources(final Context context) {
|
|
|
|
return context.getResources();
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static Drawable getSelectableItemBackgroundDrawable(final Context context) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
|
2015-04-01 12:28:26 +02:00
|
|
|
try {
|
2015-04-15 13:14:47 +02:00
|
|
|
return a.getDrawable(0);
|
2015-04-01 12:28:26 +02:00
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static float getSupportActionBarElevation(final Context context) {
|
2015-04-14 17:38:50 +02:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
2015-04-15 13:14:47 +02:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(null, new int[]{R.attr.elevation}, R.attr.actionBarStyle, 0);
|
2015-04-01 12:28:26 +02:00
|
|
|
try {
|
|
|
|
return a.getDimension(0, 0);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
2014-07-27 14:58:15 +02:00
|
|
|
|
2015-04-30 11:55:03 +02:00
|
|
|
public static int getColorFromAttribute(Context context, int attr, int def) {
|
|
|
|
final TypedValue outValue = new TypedValue();
|
|
|
|
if (!context.getTheme().resolveAttribute(attr, outValue, true))
|
|
|
|
return def;
|
|
|
|
if (outValue.type == TypedValue.TYPE_REFERENCE)
|
|
|
|
return context.getResources().getColor(attr);
|
|
|
|
return outValue.data;
|
|
|
|
}
|
|
|
|
|
2015-05-17 12:14:29 +02:00
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static int getTextColorPrimary(final Context context) {
|
2015-04-26 12:55:41 +02:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(ATTRS_TEXT_COLOR_PRIMARY);
|
|
|
|
try {
|
|
|
|
return a.getColor(0, Color.TRANSPARENT);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:02:57 +02:00
|
|
|
|
2015-04-26 15:46:23 +02:00
|
|
|
public static void getTextColorPrimaryAndInverse(final Context context, int[] colors) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(ATTRS_TEXT_COLOR_PRIMARY_AND_INVERSE);
|
|
|
|
try {
|
|
|
|
colors[0] = a.getColor(0, Color.TRANSPARENT);
|
|
|
|
colors[1] = a.getColor(1, Color.TRANSPARENT);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static void getDarkLightForegroundColors(final Context context, int[] colors) {
|
2015-04-26 15:46:23 +02:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(ATTRS_COLOR_FOREGROUND_AND_INVERSE);
|
|
|
|
try {
|
2016-03-01 06:37:56 +01:00
|
|
|
if (isDarkTheme(context)) {
|
2015-04-26 15:46:23 +02:00
|
|
|
colors[0] = a.getColor(1, Color.WHITE);
|
|
|
|
colors[1] = a.getColor(0, Color.BLACK);
|
|
|
|
} else {
|
|
|
|
colors[0] = a.getColor(0, Color.WHITE);
|
|
|
|
colors[1] = a.getColor(1, Color.BLACK);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static int getThemeBackgroundColor(final Context context) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.colorBackground});
|
2014-07-27 14:58:15 +02:00
|
|
|
try {
|
2015-04-15 13:14:47 +02:00
|
|
|
return a.getColor(0, 0);
|
2014-07-27 14:58:15 +02:00
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static int getThemeBackgroundColor(final Context context, int themeRes) {
|
|
|
|
if (themeRes == 0) {
|
|
|
|
return getThemeBackgroundColor(context);
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
2015-04-14 17:38:50 +02:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
2015-04-15 13:14:47 +02:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.colorBackground},
|
|
|
|
0, themeRes);
|
2014-07-27 14:58:15 +02:00
|
|
|
try {
|
2015-04-15 13:14:47 +02:00
|
|
|
return a.getColor(0, 0);
|
2014-07-27 14:58:15 +02:00
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getThemeBackgroundOption(final Context context) {
|
|
|
|
if (context == null) return VALUE_THEME_BACKGROUND_DEFAULT;
|
2014-08-07 19:58:42 +02:00
|
|
|
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
|
2014-07-27 14:58:15 +02:00
|
|
|
return pref.getString(KEY_THEME_BACKGROUND, VALUE_THEME_BACKGROUND_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static int getThemeColor(Context context) {
|
2015-04-15 13:14:47 +02:00
|
|
|
final Context appContext = context.getApplicationContext();
|
2016-03-01 06:37:56 +01:00
|
|
|
final TypedArray a = appContext.obtainStyledAttributes(new int[]{R.attr.colorPrimary});
|
2015-04-15 13:14:47 +02:00
|
|
|
try {
|
2016-03-01 06:37:56 +01:00
|
|
|
return a.getColor(0, ContextCompat.getColor(context, R.color.material_light_blue));
|
2015-04-15 13:14:47 +02:00
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String getThemeFontFamily(final Context context) {
|
|
|
|
if (context == null) return VALUE_THEME_FONT_FAMILY_REGULAR;
|
2014-08-07 19:58:42 +02:00
|
|
|
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
|
2014-07-27 14:58:15 +02:00
|
|
|
final String fontFamily = pref.getString(KEY_THEME_FONT_FAMILY, VALUE_THEME_FONT_FAMILY_REGULAR);
|
|
|
|
if (!TextUtils.isEmpty(fontFamily)) return fontFamily;
|
|
|
|
return VALUE_THEME_FONT_FAMILY_REGULAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getThemeForegroundColor(final Context context) {
|
2014-11-01 17:57:31 +01:00
|
|
|
return getThemeForegroundColor(context, 0);
|
|
|
|
}
|
|
|
|
|
2015-06-01 11:35:34 +02:00
|
|
|
public static int getThemeForegroundColor(final Context context, int themeRes) {
|
2015-05-03 16:43:05 +02:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
2015-06-01 11:35:34 +02:00
|
|
|
final TypedValue value = new TypedValue();
|
|
|
|
final Resources.Theme theme;
|
|
|
|
if (themeRes != 0) {
|
|
|
|
theme = context.getResources().newTheme();
|
|
|
|
theme.applyStyle(themeRes, false);
|
|
|
|
} else {
|
|
|
|
theme = context.getTheme();
|
|
|
|
}
|
|
|
|
if (!theme.resolveAttribute(android.R.attr.colorForeground, value, true)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
|
|
|
|
// windowBackground is a color
|
|
|
|
return value.data;
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
2015-06-01 11:35:34 +02:00
|
|
|
return 0;
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String getThemeNameOption(final Context context) {
|
2016-03-01 06:37:56 +01:00
|
|
|
if (context == null) return VALUE_THEME_NAME_LIGHT;
|
2014-08-07 19:58:42 +02:00
|
|
|
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
|
2016-03-01 06:37:56 +01:00
|
|
|
return pref.getString(KEY_THEME, VALUE_THEME_NAME_LIGHT);
|
2015-04-27 14:25:26 +02:00
|
|
|
}
|
|
|
|
|
2014-07-27 14:58:15 +02:00
|
|
|
public static int getTitleTextAppearance(final Context context) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.titleTextStyle},
|
|
|
|
android.R.attr.actionBarStyle, android.R.style.Widget_Holo_ActionBar);
|
|
|
|
final int textAppearance = a.getResourceId(0, android.R.style.TextAppearance_Holo);
|
|
|
|
a.recycle();
|
|
|
|
return textAppearance;
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:56:43 +01:00
|
|
|
public static int getUserAccentColor(@NonNull final Context context) {
|
2015-04-15 13:14:47 +02:00
|
|
|
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
|
2016-03-02 04:56:43 +01:00
|
|
|
final int def = ContextCompat.getColor(context, R.color.branding_color);
|
2015-04-15 13:14:47 +02:00
|
|
|
return pref.getInt(KEY_THEME_COLOR, def);
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static int getOptimalAccentColor(final Context context, boolean isActionBarContext) {
|
|
|
|
return getOptimalAccentColor(context, getUserAccentColor(context), isActionBarContext);
|
2016-02-05 05:36:41 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static int getOptimalAccentColor(final Context context, final int accentColor, boolean isActionBarContext) {
|
2015-04-30 12:37:29 +02:00
|
|
|
final int backgroundColorApprox;
|
2016-03-01 06:37:56 +01:00
|
|
|
final boolean isDarkTheme = isDarkTheme(context);
|
2015-04-30 12:37:29 +02:00
|
|
|
if (!isActionBarContext) {
|
|
|
|
backgroundColorApprox = isDarkTheme ? Color.BLACK : Color.WHITE;
|
|
|
|
} else if (isDarkTheme) {
|
|
|
|
// View context is derived from ActionBar but is currently dark theme, so we should show
|
|
|
|
// light
|
|
|
|
backgroundColorApprox = Color.BLACK;
|
|
|
|
} else {
|
|
|
|
// View context is derived from ActionBar and it's light theme, so we use contrast color
|
|
|
|
backgroundColorApprox = Color.WHITE;
|
|
|
|
}
|
2016-02-05 05:36:41 +01:00
|
|
|
if (Math.abs(TwidereColorUtils.getYIQContrast(backgroundColorApprox, accentColor)) > DARK_COLOR_THRESHOLD)
|
|
|
|
return accentColor;
|
|
|
|
return getColorFromAttribute(context, R.attr.colorAccent,
|
|
|
|
ContextCompat.getColor(context, R.color.branding_color));
|
2015-04-30 12:37:29 +02:00
|
|
|
}
|
|
|
|
|
2014-07-27 14:58:15 +02:00
|
|
|
public static int getUserThemeBackgroundAlpha(final Context context) {
|
|
|
|
if (context == null) return DEFAULT_THEME_BACKGROUND_ALPHA;
|
2014-08-07 19:58:42 +02:00
|
|
|
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
|
2015-12-30 12:34:40 +01:00
|
|
|
return TwidereMathUtils.clamp(pref.getInt(KEY_THEME_BACKGROUND_ALPHA, DEFAULT_THEME_BACKGROUND_ALPHA),
|
2015-05-17 17:16:54 +02:00
|
|
|
ThemeBackgroundPreference.MIN_ALPHA, ThemeBackgroundPreference.MAX_ALPHA);
|
|
|
|
}
|
|
|
|
|
2016-03-13 16:30:49 +01:00
|
|
|
|
|
|
|
public static int getActionBarAlpha(final String option, final int alpha) {
|
|
|
|
if (isTransparentBackground(option)) {
|
|
|
|
return getActionBarAlpha(alpha);
|
|
|
|
}
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
2015-05-17 17:16:54 +02:00
|
|
|
public static int getActionBarAlpha(final int alpha) {
|
2015-12-30 12:34:40 +01:00
|
|
|
final int normalizedAlpha = TwidereMathUtils.clamp(alpha, 0, 0xFF);
|
2015-05-19 19:54:56 +02:00
|
|
|
final int delta = (ThemeBackgroundPreference.MAX_ALPHA - normalizedAlpha);
|
2015-12-30 12:34:40 +01:00
|
|
|
return TwidereMathUtils.clamp(ThemeBackgroundPreference.MAX_ALPHA - delta / 2,
|
2015-05-17 18:06:40 +02:00
|
|
|
ThemeBackgroundPreference.MIN_ALPHA, ThemeBackgroundPreference.MAX_ALPHA);
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 11:19:26 +02:00
|
|
|
public static Typeface getUserTypeface(final Context context, final String fontFamily, final Typeface defTypeface) {
|
|
|
|
if (context == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
|
|
|
|
return Typeface.DEFAULT;
|
|
|
|
final int fontStyle = defTypeface != null ? defTypeface.getStyle() : Typeface.NORMAL;
|
|
|
|
final Typeface tf = Typeface.create(fontFamily, fontStyle);
|
|
|
|
if (tf != null) return tf;
|
|
|
|
return Typeface.create(Typeface.DEFAULT, fontStyle);
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Drawable getWindowBackgroundFromTheme(final Context context) {
|
2016-03-04 04:54:27 +01:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.windowNormalBackground,
|
|
|
|
android.R.attr.windowBackground});
|
2015-04-15 13:14:47 +02:00
|
|
|
try {
|
2016-03-04 04:54:27 +01:00
|
|
|
if (a.hasValue(0)) {
|
|
|
|
return a.getDrawable(0);
|
|
|
|
} else {
|
|
|
|
return a.getDrawable(1);
|
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
} finally {
|
|
|
|
a.recycle();
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Drawable getWindowBackgroundFromThemeApplyAlpha(final Context context, final int alpha) {
|
2016-03-04 04:54:27 +01:00
|
|
|
final Drawable d = getWindowBackgroundFromTheme(context);
|
2015-07-19 17:10:08 +02:00
|
|
|
if (d == null) return null;
|
|
|
|
d.mutate();
|
2015-12-30 12:34:40 +01:00
|
|
|
d.setAlpha(TwidereMathUtils.clamp(alpha, ThemeBackgroundPreference.MIN_ALPHA,
|
2015-07-19 17:10:08 +02:00
|
|
|
ThemeBackgroundPreference.MAX_ALPHA));
|
2015-05-03 16:43:05 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Drawable getWindowContentOverlay(final Context context) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.windowContentOverlay});
|
2015-04-27 14:25:26 +02:00
|
|
|
try {
|
|
|
|
return a.getDrawable(0);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Drawable getNormalWindowContentOverlay(final Context context) {
|
|
|
|
return getWindowContentOverlay(context);
|
2015-04-27 19:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void initPagerIndicatorAsActionBarTab(FragmentActivity activity, TabPagerIndicator indicator, @Nullable View pagerOverlay) {
|
2015-04-15 13:14:47 +02:00
|
|
|
final float supportActionBarElevation = getSupportActionBarElevation(activity);
|
|
|
|
ViewCompat.setElevation(indicator, supportActionBarElevation);
|
|
|
|
if (!(activity instanceof IThemedActivity)) return;
|
|
|
|
final int themeColor = ((IThemedActivity) activity).getCurrentThemeColor();
|
2015-05-12 17:47:52 +02:00
|
|
|
final String backgroundOption = ((IThemedActivity) activity).getCurrentThemeBackgroundOption();
|
2015-04-26 15:46:23 +02:00
|
|
|
final int colorDark, colorLight;
|
|
|
|
final int[] textColors = new int[2];
|
|
|
|
getTextColorPrimaryAndInverse(activity, textColors);
|
2016-03-01 06:37:56 +01:00
|
|
|
if (isDarkTheme(activity)) {
|
2015-04-26 15:46:23 +02:00
|
|
|
colorDark = textColors[1];
|
|
|
|
colorLight = textColors[0];
|
|
|
|
} else {
|
|
|
|
colorDark = textColors[0];
|
|
|
|
colorLight = textColors[1];
|
|
|
|
}
|
2015-04-27 19:57:39 +02:00
|
|
|
final int contrastColor = TwidereColorUtils.getContrastYIQ(themeColor, ACCENT_COLOR_THRESHOLD,
|
|
|
|
colorDark, colorLight);
|
2016-03-01 06:37:56 +01:00
|
|
|
ViewSupport.setBackground(indicator, getActionBarStackedBackground(activity, themeColor, backgroundOption, true));
|
|
|
|
if (isDarkTheme(activity)) {
|
2015-04-15 13:14:47 +02:00
|
|
|
final int foregroundColor = getThemeForegroundColor(activity);
|
|
|
|
indicator.setIconColor(foregroundColor);
|
|
|
|
indicator.setLabelColor(foregroundColor);
|
|
|
|
indicator.setStripColor(themeColor);
|
|
|
|
} else {
|
|
|
|
indicator.setIconColor(contrastColor);
|
|
|
|
indicator.setLabelColor(contrastColor);
|
|
|
|
indicator.setStripColor(contrastColor);
|
|
|
|
}
|
|
|
|
indicator.updateAppearance();
|
2015-04-27 19:57:39 +02:00
|
|
|
if (pagerOverlay != null) {
|
2016-03-01 06:37:56 +01:00
|
|
|
ViewSupport.setBackground(pagerOverlay, getNormalWindowContentOverlay(activity));
|
2015-04-27 19:57:39 +02:00
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static boolean isColoredActionBar(Context context) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.coloredActionBar});
|
|
|
|
try {
|
|
|
|
return a.getBoolean(0, false);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isDarkTheme(final Context context) {
|
2016-03-01 06:37:56 +01:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.darkTheme});
|
|
|
|
try {
|
|
|
|
return a.getBoolean(0, false);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 10:36:56 +02:00
|
|
|
public static boolean isSolidBackground(final String option) {
|
|
|
|
return VALUE_THEME_BACKGROUND_SOLID.equals(option);
|
|
|
|
}
|
|
|
|
|
2014-07-27 14:58:15 +02:00
|
|
|
public static boolean isTransparentBackground(final Context context) {
|
2015-04-14 17:38:50 +02:00
|
|
|
return isTransparentBackground(getThemeBackgroundOption(context));
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 17:38:50 +02:00
|
|
|
public static boolean isTransparentBackground(final String option) {
|
|
|
|
return VALUE_THEME_BACKGROUND_TRANSPARENT.equals(option);
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static boolean isWindowFloating(Context context) {
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.windowIsFloating});
|
2015-04-15 13:14:47 +02:00
|
|
|
try {
|
|
|
|
return a.getBoolean(0, false);
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void resetCheatSheet(ActionMenuView menuView) {
|
|
|
|
final OnLongClickListener listener = new OnLongClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
if (((ActionMenuItemView) v).hasText()) return false;
|
|
|
|
final MenuItem menuItem = ((ActionMenuItemView) v).getItemData();
|
|
|
|
Utils.showMenuItemToast(v, menuItem.getTitle(), true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (int i = 0, j = menuView.getChildCount(); i < j; i++) {
|
|
|
|
final View child = menuView.getChildAt(i);
|
|
|
|
if (!(child instanceof ActionMenuItemView)) continue;
|
|
|
|
final ActionMenuItemView itemView = (ActionMenuItemView) child;
|
|
|
|
final MenuItem menuItem = itemView.getItemData();
|
|
|
|
if (menuItem.hasSubMenu()) continue;
|
|
|
|
itemView.setOnLongClickListener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setActionBarOverflowColor(Toolbar toolbar, int itemColor) {
|
|
|
|
if (toolbar == null) return;
|
2015-04-30 11:55:03 +02:00
|
|
|
if (toolbar instanceof TwidereToolbar) {
|
|
|
|
((TwidereToolbar) toolbar).setItemColor(itemColor);
|
|
|
|
}
|
2015-04-27 14:25:26 +02:00
|
|
|
final ActionMenuView actionMenuView = ViewSupport.findViewByType(toolbar, ActionMenuView.class);
|
2015-04-15 13:14:47 +02:00
|
|
|
if (actionMenuView == null) return;
|
|
|
|
View overflowView = null;
|
|
|
|
for (int i = 0, j = actionMenuView.getChildCount(); i < j; i++) {
|
|
|
|
final View child = actionMenuView.getChildAt(i);
|
|
|
|
final ActionMenuView.LayoutParams lp = (ActionMenuView.LayoutParams) child.getLayoutParams();
|
|
|
|
if (lp.isOverflowButton) {
|
|
|
|
overflowView = child;
|
|
|
|
break;
|
|
|
|
}
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
if (!(overflowView instanceof ImageView)) return;
|
2015-04-27 20:36:34 +02:00
|
|
|
((ImageView) overflowView).setColorFilter(itemColor, Mode.SRC_ATOP);
|
2014-07-27 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:17:22 +02:00
|
|
|
public static void setCompatToolbarOverlay(Activity activity, Drawable overlay) {
|
2015-04-15 13:14:47 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return;
|
2015-04-17 09:17:22 +02:00
|
|
|
final Window window = activity.getWindow();
|
|
|
|
final View view = window.findViewById(android.support.v7.appcompat.R.id.decor_content_parent);
|
2015-04-26 15:46:23 +02:00
|
|
|
if (!(view instanceof ActionBarOverlayLayout)) return;
|
2014-08-07 19:58:42 +02:00
|
|
|
try {
|
2015-04-15 13:14:47 +02:00
|
|
|
final Field field = ActionBarOverlayLayout.class.getDeclaredField("mWindowContentOverlay");
|
|
|
|
field.setAccessible(true);
|
2015-04-17 09:17:22 +02:00
|
|
|
field.set(view, overlay);
|
2015-04-15 13:14:47 +02:00
|
|
|
} catch (Exception ignore) {
|
2014-08-07 19:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-29 07:48:07 +01:00
|
|
|
|
2015-04-26 15:46:23 +02:00
|
|
|
public static void setCompatContentViewOverlay(Activity activity, Drawable overlay) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return;
|
|
|
|
final Window window = activity.getWindow();
|
|
|
|
View contentLayout = window.findViewById(android.support.v7.appcompat.R.id.action_bar_activity_content);
|
|
|
|
if (contentLayout == null) {
|
|
|
|
contentLayout = window.findViewById(android.R.id.content);
|
|
|
|
}
|
2015-09-01 16:42:48 +02:00
|
|
|
if (contentLayout instanceof FrameLayout) {
|
|
|
|
ViewSupport.setForeground(contentLayout, overlay);
|
2015-04-26 15:46:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:25:26 +02:00
|
|
|
public static void setWindowOverlayViewOverlay(Activity activity, Drawable overlay) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return;
|
|
|
|
final Window window = activity.getWindow();
|
|
|
|
final View windowOverlay = window.findViewById(R.id.window_overlay);
|
|
|
|
if (windowOverlay == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ViewSupport.setBackground(windowOverlay, overlay);
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static void setupDrawerBackground(Context context, View view) {
|
|
|
|
if (!(context instanceof IThemedActivity)) return;
|
|
|
|
final String backgroundOption = ((IThemedActivity) context).getThemeBackgroundOption();
|
|
|
|
final int alpha = ((IThemedActivity) context).getCurrentThemeBackgroundAlpha();
|
2015-05-11 18:36:18 +02:00
|
|
|
final Drawable d;
|
|
|
|
if (isSolidBackground(backgroundOption)) {
|
2016-03-01 06:37:56 +01:00
|
|
|
d = new ColorDrawable(isDarkTheme(context) ? Color.BLACK : Color.WHITE);
|
2015-05-11 18:36:18 +02:00
|
|
|
} else {
|
2016-03-01 06:37:56 +01:00
|
|
|
d = getWindowBackgroundFromTheme(context);
|
2015-05-11 18:36:18 +02:00
|
|
|
}
|
|
|
|
if (d == null) throw new NullPointerException();
|
2015-07-19 17:10:08 +02:00
|
|
|
d.mutate();
|
2015-05-11 18:36:18 +02:00
|
|
|
if (isTransparentBackground(backgroundOption)) {
|
2015-04-15 13:14:47 +02:00
|
|
|
d.setAlpha(alpha);
|
|
|
|
}
|
2015-04-27 14:25:26 +02:00
|
|
|
ViewSupport.setBackground(view, d);
|
2014-10-29 07:48:07 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static void wrapMenuIcon(@NonNull Menu menu, int itemColor, int subItemColor, int... excludeGroups) {
|
|
|
|
for (int i = 0, j = menu.size(); i < j; i++) {
|
|
|
|
final MenuItem item = menu.getItem(i);
|
|
|
|
wrapMenuItemIcon(item, itemColor, excludeGroups);
|
|
|
|
if (item.hasSubMenu()) {
|
|
|
|
wrapMenuIcon(item.getSubMenu(), subItemColor, subItemColor, excludeGroups);
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 07:48:07 +01:00
|
|
|
}
|
2014-11-22 15:12:54 +01:00
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static void wrapMenuIcon(ActionMenuView view, int... excludeGroups) {
|
2016-02-18 17:21:04 +01:00
|
|
|
final Context context = view.getContext();
|
|
|
|
final int colorDark = ContextCompat.getColor(context, R.color.action_icon_dark);
|
|
|
|
final int colorLight = ContextCompat.getColor(context, R.color.action_icon_light);
|
2015-04-15 13:14:47 +02:00
|
|
|
wrapMenuIcon(view, colorDark, colorLight, excludeGroups);
|
|
|
|
}
|
|
|
|
|
2016-02-18 17:21:04 +01:00
|
|
|
public static int getActionIconColor(Context context) {
|
|
|
|
final int colorDark = ContextCompat.getColor(context, R.color.action_icon_dark);
|
|
|
|
final int colorLight = ContextCompat.getColor(context, R.color.action_icon_light);
|
|
|
|
final int itemBackgroundColor = ThemeUtils.getThemeBackgroundColor(context);
|
|
|
|
return TwidereColorUtils.getContrastYIQ(itemBackgroundColor, colorDark, colorLight);
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static void wrapMenuIcon(ActionMenuView view, int colorDark, int colorLight, int... excludeGroups) {
|
2016-02-18 17:21:04 +01:00
|
|
|
final Context context = view.getContext();
|
|
|
|
final int itemBackgroundColor = ThemeUtils.getThemeBackgroundColor(context);
|
|
|
|
final int popupItemBackgroundColor = ThemeUtils.getThemeBackgroundColor(context, view.getPopupTheme());
|
2015-04-22 10:36:56 +02:00
|
|
|
final int itemColor = TwidereColorUtils.getContrastYIQ(itemBackgroundColor, colorDark, colorLight);
|
|
|
|
final int popupItemColor = TwidereColorUtils.getContrastYIQ(popupItemBackgroundColor, colorDark, colorLight);
|
2015-04-15 13:14:47 +02:00
|
|
|
final Menu menu = view.getMenu();
|
|
|
|
final int childCount = view.getChildCount();
|
|
|
|
for (int i = 0, j = menu.size(), k = 0; i < j; i++) {
|
|
|
|
final MenuItem item = menu.getItem(i);
|
|
|
|
wrapMenuItemIcon(item, itemColor, excludeGroups);
|
|
|
|
if (item.hasSubMenu()) {
|
2015-04-22 16:19:26 +02:00
|
|
|
wrapMenuIcon(item.getSubMenu(), popupItemColor, popupItemColor, excludeGroups);
|
2015-04-15 13:14:47 +02:00
|
|
|
}
|
|
|
|
if (item.isVisible()) {
|
|
|
|
k++;
|
2014-11-22 15:12:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 18:34:10 +02:00
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
public static void wrapMenuItemIcon(@NonNull MenuItem item, int itemColor, int... excludeGroups) {
|
|
|
|
if (ArrayUtils.contains(excludeGroups, item.getGroupId())) return;
|
|
|
|
final Drawable icon = item.getIcon();
|
2016-02-18 17:21:04 +01:00
|
|
|
if (icon == null || icon instanceof DoNotWrapDrawable) return;
|
2015-04-15 13:14:47 +02:00
|
|
|
if (icon instanceof ActionIconDrawable) {
|
|
|
|
((ActionIconDrawable) icon).setDefaultColor(itemColor);
|
|
|
|
item.setIcon(icon);
|
|
|
|
return;
|
2015-04-13 18:34:10 +02:00
|
|
|
}
|
2015-04-15 13:14:47 +02:00
|
|
|
icon.mutate();
|
|
|
|
final Drawable.Callback callback = icon.getCallback();
|
|
|
|
final ActionIconDrawable newIcon = new ActionIconDrawable(icon, itemColor);
|
|
|
|
newIcon.setCallback(callback);
|
|
|
|
item.setIcon(newIcon);
|
2015-04-13 18:34:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 04:54:48 +02:00
|
|
|
public static void wrapToolbarMenuIcon(@Nullable ActionMenuView view, int itemColor, int popupItemColor, int... excludeGroups) {
|
|
|
|
if (view == null) return;
|
2015-04-15 13:14:47 +02:00
|
|
|
final Menu menu = view.getMenu();
|
|
|
|
final int childCount = view.getChildCount();
|
|
|
|
for (int i = 0, j = menu.size(), k = 0; i < j; i++) {
|
|
|
|
final MenuItem item = menu.getItem(i);
|
|
|
|
wrapMenuItemIcon(item, itemColor, excludeGroups);
|
|
|
|
if (item.hasSubMenu()) {
|
2015-04-22 16:19:26 +02:00
|
|
|
wrapMenuIcon(item.getSubMenu(), popupItemColor, popupItemColor, excludeGroups);
|
2015-04-15 13:14:47 +02:00
|
|
|
}
|
|
|
|
if (item.isVisible()) {
|
|
|
|
k++;
|
|
|
|
}
|
2015-04-14 17:38:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:14:47 +02:00
|
|
|
@NonNull
|
|
|
|
private static SharedPreferencesWrapper getSharedPreferencesWrapper(Context context) {
|
|
|
|
final Context appContext = context.getApplicationContext();
|
|
|
|
return SharedPreferencesWrapper.getInstance(appContext, SHARED_PREFERENCES_NAME,
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:25:26 +02:00
|
|
|
public static int getActionBarHeight(Context context) {
|
|
|
|
final TypedValue tv = new TypedValue();
|
|
|
|
final Resources.Theme theme = context.getTheme();
|
|
|
|
final int attr = R.attr.actionBarSize;
|
|
|
|
if (theme.resolveAttribute(attr, tv, true)) {
|
|
|
|
return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-27 20:36:34 +02:00
|
|
|
|
2015-04-28 06:47:57 +02:00
|
|
|
public static Context getActionBarThemedContext(Context base) {
|
|
|
|
final TypedValue outValue = new TypedValue();
|
|
|
|
final Resources.Theme baseTheme = base.getTheme();
|
|
|
|
baseTheme.resolveAttribute(android.support.v7.appcompat.R.attr.actionBarTheme, outValue, true);
|
|
|
|
|
|
|
|
if (outValue.resourceId != 0) {
|
|
|
|
final Resources.Theme actionBarTheme = base.getResources().newTheme();
|
|
|
|
actionBarTheme.setTo(baseTheme);
|
|
|
|
actionBarTheme.applyStyle(outValue.resourceId, true);
|
|
|
|
|
2015-05-03 16:43:05 +02:00
|
|
|
final ActionBarContextThemeWrapper actionBarContext = new ActionBarContextThemeWrapper(base, outValue.resourceId);
|
2015-04-28 06:47:57 +02:00
|
|
|
actionBarContext.getTheme().setTo(actionBarTheme);
|
|
|
|
return actionBarContext;
|
|
|
|
} else {
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 22:22:29 +02:00
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static Context getActionBarThemedContext(Context base, int accentColor) {
|
2015-04-30 22:22:29 +02:00
|
|
|
final int actionBarThemeId;
|
2016-03-01 06:37:56 +01:00
|
|
|
if (isDarkTheme(base) || TwidereColorUtils.getYIQLuminance(accentColor) <= ACCENT_COLOR_THRESHOLD) {
|
|
|
|
actionBarThemeId = R.style.Theme_Twidere;
|
2015-05-01 05:04:08 +02:00
|
|
|
} else {
|
2016-03-01 06:37:56 +01:00
|
|
|
actionBarThemeId = R.style.Theme_Twidere;
|
2015-04-30 22:22:29 +02:00
|
|
|
}
|
|
|
|
final Resources.Theme baseTheme = base.getTheme();
|
|
|
|
final Resources.Theme actionBarTheme = base.getResources().newTheme();
|
|
|
|
actionBarTheme.setTo(baseTheme);
|
|
|
|
actionBarTheme.applyStyle(actionBarThemeId, true);
|
|
|
|
|
2015-05-03 16:43:05 +02:00
|
|
|
final ActionBarContextThemeWrapper actionBarContext = new ActionBarContextThemeWrapper(base, actionBarThemeId);
|
2015-04-30 22:22:29 +02:00
|
|
|
actionBarContext.getTheme().setTo(actionBarTheme);
|
|
|
|
return actionBarContext;
|
|
|
|
}
|
2015-05-03 16:43:05 +02:00
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static int getActionBarColor(Context context, int themeColor, String backgroundOption) {
|
|
|
|
if (!isDarkTheme(context)) {
|
2015-05-11 18:36:18 +02:00
|
|
|
return themeColor;
|
|
|
|
} else if (isSolidBackground(backgroundOption)) {
|
|
|
|
return Color.BLACK;
|
|
|
|
}
|
2015-11-21 08:26:10 +01:00
|
|
|
return ContextCompat.getColor(context, R.color.background_color_action_bar_dark);
|
2015-05-11 18:36:18 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:37:56 +01:00
|
|
|
public static void applyToolbarItemColor(Context context, Toolbar toolbar, int toolbarColor) {
|
|
|
|
if (toolbar == null || isDarkTheme(context)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-13 16:30:49 +01:00
|
|
|
final int contrastForegroundColor = getContrastForegroundColor(context, toolbarColor);
|
2016-03-01 06:37:56 +01:00
|
|
|
toolbar.setTitleTextColor(contrastForegroundColor);
|
|
|
|
toolbar.setSubtitleTextColor(contrastForegroundColor);
|
2016-03-13 16:30:49 +01:00
|
|
|
int popupItemColor, popupTheme = toolbar.getPopupTheme();
|
|
|
|
if (popupTheme != 0) {
|
|
|
|
popupItemColor = getThemeForegroundColor(context, popupTheme);
|
|
|
|
} else {
|
|
|
|
popupItemColor = getThemeForegroundColor(context);
|
|
|
|
}
|
|
|
|
getThemeForegroundColor(context);
|
2016-03-01 06:37:56 +01:00
|
|
|
setActionBarOverflowColor(toolbar, contrastForegroundColor);
|
|
|
|
wrapToolbarMenuIcon(ViewSupport.findViewByType(toolbar, ActionMenuView.class),
|
2016-03-13 16:30:49 +01:00
|
|
|
contrastForegroundColor, popupItemColor);
|
2016-03-01 06:37:56 +01:00
|
|
|
if (toolbar instanceof TwidereToolbar) {
|
|
|
|
((TwidereToolbar) toolbar).setItemColor(contrastForegroundColor);
|
2015-05-03 16:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 15:24:45 +02:00
|
|
|
|
2016-03-04 04:54:27 +01:00
|
|
|
public static int getLocalNightMode(SharedPreferences preferences) {
|
|
|
|
switch (Utils.getNonEmptyString(preferences, KEY_THEME, VALUE_THEME_NAME_LIGHT)) {
|
|
|
|
case VALUE_THEME_NAME_DARK: {
|
|
|
|
return AppCompatDelegate.MODE_NIGHT_YES;
|
|
|
|
}
|
|
|
|
case VALUE_THEME_NAME_AUTO: {
|
|
|
|
return AppCompatDelegate.MODE_NIGHT_AUTO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AppCompatDelegate.MODE_NIGHT_NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void applyDayNight(SharedPreferences preferences, AppCompatDelegate delegate) {
|
|
|
|
switch (getLocalNightMode(preferences)) {
|
|
|
|
case AppCompatDelegate.MODE_NIGHT_AUTO: {
|
|
|
|
delegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AppCompatDelegate.MODE_NIGHT_YES: {
|
|
|
|
delegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
delegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:20:56 +01:00
|
|
|
public static void fixNightMode(Resources resources, Configuration newConfig) {
|
|
|
|
int currentNightMode = resources.getConfiguration().uiMode
|
|
|
|
& Configuration.UI_MODE_NIGHT_MASK;
|
|
|
|
|
|
|
|
if (currentNightMode == Configuration.UI_MODE_NIGHT_YES)
|
|
|
|
newConfig.uiMode = (newConfig.uiMode & ~Configuration.UI_MODE_NIGHT_MASK)
|
|
|
|
| Configuration.UI_MODE_NIGHT_YES;
|
|
|
|
}
|
|
|
|
|
2016-03-14 09:17:46 +01:00
|
|
|
public static String getATEKey(Context context) {
|
|
|
|
TypedValue value = new TypedValue();
|
|
|
|
if (!context.getTheme().resolveAttribute(R.attr.ateThemeKey, value, true)) {
|
|
|
|
return "dark";
|
|
|
|
}
|
|
|
|
if (TextUtils.isEmpty(value.string)) return "dark";
|
|
|
|
return String.valueOf(value.string);
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:36:41 +01:00
|
|
|
|
2015-11-21 08:26:10 +01:00
|
|
|
public static final class ActionBarContextThemeWrapper extends android.support.v7.view.ContextThemeWrapper {
|
2015-09-01 15:24:45 +02:00
|
|
|
|
|
|
|
public ActionBarContextThemeWrapper(Context base, int themeres) {
|
|
|
|
super(base, themeres);
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 07:48:39 +02:00
|
|
|
}
|