improving theme

This commit is contained in:
Mariotaku Lee 2015-04-14 23:38:50 +08:00
parent b57e8fc51a
commit 476b335a79
11 changed files with 212 additions and 208 deletions

View File

@ -35,6 +35,7 @@ public abstract class BasePreferenceActivity extends PreferenceActivity implemen
IThemedActivity { IThemedActivity {
private int mCurrentThemeResource; private int mCurrentThemeResource;
private String mCurrentThemeBackgroundOption;
@Override @Override
public boolean onMenuOpened(int featureId, Menu menu) { public boolean onMenuOpened(int featureId, Menu menu) {
@ -99,10 +100,21 @@ public abstract class BasePreferenceActivity extends PreferenceActivity implemen
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
setTheme(mCurrentThemeResource = getThemeResourceId()); setTheme(mCurrentThemeResource = getThemeResourceId());
mCurrentThemeBackgroundOption = getThemeBackgroundOption();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setActionBarBackground(); setActionBarBackground();
} }
@Override
public String getCurrentThemeBackgroundOption() {
return mCurrentThemeBackgroundOption;
}
@Override
public String getThemeBackgroundOption() {
return ThemeUtils.getThemeBackgroundOption(this);
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();

View File

@ -38,10 +38,16 @@ public abstract class BaseThemedActivity extends Activity implements IThemedActi
private int mCurrentThemeResource, mCurrentThemeColor, mCurrentThemeBackgroundAlpha; private int mCurrentThemeResource, mCurrentThemeColor, mCurrentThemeBackgroundAlpha;
private String mCurrentThemeFontFamily; private String mCurrentThemeFontFamily;
private Theme mTheme; private Theme mTheme;
private String mCurrentThemeBackgroundOption;
@Override @Override
public Resources getDefaultResources() { public int getCurrentThemeBackgroundAlpha() {
return super.getResources(); return mCurrentThemeBackgroundAlpha;
}
@Override
public int getCurrentThemeColor() {
return mCurrentThemeColor;
} }
@Override @Override
@ -50,21 +56,13 @@ public abstract class BaseThemedActivity extends Activity implements IThemedActi
} }
@Override @Override
public Theme getTheme() { public Resources getDefaultResources() {
if (mTheme == null) { return super.getResources();
mTheme = getResources().newTheme();
mTheme.setTo(super.getTheme());
final int getThemeResourceId = getThemeResourceId();
if (getThemeResourceId != 0) {
mTheme.applyStyle(getThemeResourceId, true);
}
}
return mTheme;
} }
@Override @Override
public int getThemeBackgroundAlpha() { public int getThemeBackgroundAlpha() {
return ThemeUtils.isTransparentBackground(this) ? ThemeUtils.getUserThemeBackgroundAlpha(this) : 0xff; return ThemeUtils.getUserThemeBackgroundAlpha(this);
} }
@Override @Override
@ -88,22 +86,25 @@ public abstract class BaseThemedActivity extends Activity implements IThemedActi
restartActivity(this); restartActivity(this);
} }
@Override
public Theme getTheme() {
if (mTheme == null) {
mTheme = getResources().newTheme();
mTheme.setTo(super.getTheme());
final int getThemeResourceId = getThemeResourceId();
if (getThemeResourceId != 0) {
mTheme.applyStyle(getThemeResourceId, true);
}
}
return mTheme;
}
protected final boolean isThemeChanged() { protected final boolean isThemeChanged() {
return getThemeResourceId() != mCurrentThemeResource || getThemeColor() != mCurrentThemeColor return getThemeResourceId() != mCurrentThemeResource || getThemeColor() != mCurrentThemeColor
|| !CompareUtils.objectEquals(getThemeFontFamily(), mCurrentThemeFontFamily) || !CompareUtils.objectEquals(getThemeFontFamily(), mCurrentThemeFontFamily)
|| getThemeBackgroundAlpha() != mCurrentThemeBackgroundAlpha; || getThemeBackgroundAlpha() != mCurrentThemeBackgroundAlpha;
} }
@Override
public int getCurrentThemeBackgroundAlpha() {
return mCurrentThemeBackgroundAlpha;
}
@Override
public int getCurrentThemeColor() {
return mCurrentThemeColor;
}
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
if (Utils.isDebugBuild()) { if (Utils.isDebugBuild()) {
@ -133,9 +134,8 @@ public abstract class BaseThemedActivity extends Activity implements IThemedActi
mCurrentThemeColor = getThemeColor(); mCurrentThemeColor = getThemeColor();
mCurrentThemeFontFamily = getThemeFontFamily(); mCurrentThemeFontFamily = getThemeFontFamily();
mCurrentThemeBackgroundAlpha = getThemeBackgroundAlpha(); mCurrentThemeBackgroundAlpha = getThemeBackgroundAlpha();
mCurrentThemeBackgroundOption = getThemeBackgroundOption();
setTheme(mCurrentThemeResource); setTheme(mCurrentThemeResource);
if (ThemeUtils.isTransparentBackground(mCurrentThemeResource)) { ThemeUtils.applyWindowBackground(this, getWindow(),mCurrentThemeResource, mCurrentThemeBackgroundOption, mCurrentThemeBackgroundAlpha);
getWindow().setBackgroundDrawable(ThemeUtils.getWindowBackground(this));
}
} }
} }

View File

@ -23,18 +23,22 @@ import android.content.res.Resources;
public interface IThemedActivity { public interface IThemedActivity {
public int getCurrentThemeBackgroundAlpha();
public String getCurrentThemeBackgroundOption();
public int getCurrentThemeColor();
public int getCurrentThemeResourceId(); public int getCurrentThemeResourceId();
public Resources getDefaultResources(); public Resources getDefaultResources();
public int getThemeBackgroundAlpha(); public int getThemeBackgroundAlpha();
public int getCurrentThemeBackgroundAlpha(); String getThemeBackgroundOption();
public int getThemeColor(); public int getThemeColor();
public int getCurrentThemeColor();
public String getThemeFontFamily(); public String getThemeFontFamily();
int getThemeResourceId(); int getThemeResourceId();

View File

@ -46,11 +46,6 @@ public class BaseSupportDialogActivity extends ThemedFragmentActivity implements
return (TwidereApplication) getApplication(); return (TwidereApplication) getApplication();
} }
@Override
protected final boolean shouldSetWindowBackground() {
return false;
}
protected boolean isStateSaved() { protected boolean isStateSaved() {
return mInstanceStateSaved; return mInstanceStateSaved;
} }

View File

@ -100,9 +100,9 @@ import org.mariotaku.twidere.util.ParseUtils;
import org.mariotaku.twidere.util.ReadStateManager; import org.mariotaku.twidere.util.ReadStateManager;
import org.mariotaku.twidere.util.ThemeUtils; import org.mariotaku.twidere.util.ThemeUtils;
import org.mariotaku.twidere.util.Utils; import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.util.ViewUtils;
import org.mariotaku.twidere.util.accessor.ActivityAccessor; import org.mariotaku.twidere.util.accessor.ActivityAccessor;
import org.mariotaku.twidere.util.accessor.ActivityAccessor.TaskDescriptionCompat; import org.mariotaku.twidere.util.accessor.ActivityAccessor.TaskDescriptionCompat;
import org.mariotaku.twidere.util.ViewUtils;
import org.mariotaku.twidere.util.message.TaskStateChangedEvent; import org.mariotaku.twidere.util.message.TaskStateChangedEvent;
import org.mariotaku.twidere.util.message.UnreadCountUpdatedEvent; import org.mariotaku.twidere.util.message.UnreadCountUpdatedEvent;
import org.mariotaku.twidere.view.ExtendedViewPager; import org.mariotaku.twidere.view.ExtendedViewPager;
@ -661,11 +661,6 @@ public class HomeActivity extends BaseActionBarActivity implements OnClickListen
mColorStatusFrameLayout = (TintedStatusFrameLayout) findViewById(R.id.home_content); mColorStatusFrameLayout = (TintedStatusFrameLayout) findViewById(R.id.home_content);
} }
@Override
protected boolean shouldSetWindowBackground() {
return false;
}
private boolean handleFragmentKeyboardShortcutSingle(int keyCode, @NonNull KeyEvent event) { private boolean handleFragmentKeyboardShortcutSingle(int keyCode, @NonNull KeyEvent event) {
final Fragment fragment = getCurrentVisibleFragment(); final Fragment fragment = getCurrentVisibleFragment();
if (fragment instanceof ShortcutCallback) { if (fragment instanceof ShortcutCallback) {
@ -780,7 +775,7 @@ public class HomeActivity extends BaseActionBarActivity implements OnClickListen
private void setupBars() { private void setupBars() {
final int themeColor = getThemeColor(); final int themeColor = getThemeColor();
final int themeResId = getCurrentThemeResourceId(); final int themeResId = getCurrentThemeResourceId();
final boolean isTransparent = ThemeUtils.isTransparentBackground(themeResId); final boolean isTransparent = ThemeUtils.isTransparentBackground(getCurrentThemeBackgroundOption());
final int actionBarAlpha = isTransparent ? ThemeUtils.getUserThemeBackgroundAlpha(this) : 0xFF; final int actionBarAlpha = isTransparent ? ThemeUtils.getUserThemeBackgroundAlpha(this) : 0xFF;
final IHomeActionButton homeActionButton = (IHomeActionButton) mActionsButton; final IHomeActionButton homeActionButton = (IHomeActionButton) mActionsButton;
mTabIndicator.setItemContext(ThemeUtils.getActionBarContext(this)); mTabIndicator.setItemContext(ThemeUtils.getActionBarContext(this));
@ -846,14 +841,14 @@ public class HomeActivity extends BaseActionBarActivity implements OnClickListen
mLeftDrawerContainer = (LeftDrawerFrameLayout) mSlidingMenu.getMenu().findViewById(R.id.left_drawer_container); mLeftDrawerContainer = (LeftDrawerFrameLayout) mSlidingMenu.getMenu().findViewById(R.id.left_drawer_container);
mRightDrawerContainer = (RightDrawerFrameLayout) mSlidingMenu.getSecondaryMenu().findViewById( mRightDrawerContainer = (RightDrawerFrameLayout) mSlidingMenu.getSecondaryMenu().findViewById(
R.id.right_drawer_container); R.id.right_drawer_container);
final boolean isTransparentBackground = ThemeUtils.isTransparentBackground(this); final boolean isTransparentBackground = ThemeUtils.isTransparentBackground(getCurrentThemeBackgroundOption());
mLeftDrawerContainer.setClipEnabled(isTransparentBackground); mLeftDrawerContainer.setClipEnabled(isTransparentBackground);
mLeftDrawerContainer.setScrollScale(mSlidingMenu.getBehindScrollScale()); mLeftDrawerContainer.setScrollScale(mSlidingMenu.getBehindScrollScale());
mRightDrawerContainer.setClipEnabled(isTransparentBackground); mRightDrawerContainer.setClipEnabled(isTransparentBackground);
mRightDrawerContainer.setScrollScale(mSlidingMenu.getBehindScrollScale()); mRightDrawerContainer.setScrollScale(mSlidingMenu.getBehindScrollScale());
mSlidingMenu.setBehindCanvasTransformer(new ListenerCanvasTransformer(this)); mSlidingMenu.setBehindCanvasTransformer(new ListenerCanvasTransformer(this));
final Window window = getWindow(); final Window window = getWindow();
final Drawable windowBackground = ThemeUtils.getWindowBackground(this, getCurrentThemeResourceId()); final Drawable windowBackground = ThemeUtils.getWindowBackground(this);
ViewUtils.setBackground(mSlidingMenu.getContent(), windowBackground); ViewUtils.setBackground(mSlidingMenu.getContent(), windowBackground);
window.setBackgroundDrawable(new EmptyDrawable(windowBackground)); window.setBackgroundDrawable(new EmptyDrawable(windowBackground));
} }

View File

@ -78,10 +78,9 @@ public class LinkHandlerActivity extends BaseActionBarActivity implements System
return getSupportFragmentManager().findFragmentById(R.id.main_content); return getSupportFragmentManager().findFragmentById(R.id.main_content);
} }
@Override @Override
public int getThemeResourceId() { public int getThemeResourceId() {
return R.style.Theme_Twidere_Light_DialogWhenLarge; return ThemeUtils.getDialogWhenLargeThemeResource(this);
} }
@Override @Override
@ -172,6 +171,7 @@ public class LinkHandlerActivity extends BaseActionBarActivity implements System
final View actionBarView = getWindow().findViewById(android.support.v7.appcompat.R.id.action_bar); final View actionBarView = getWindow().findViewById(android.support.v7.appcompat.R.id.action_bar);
if (actionBarView instanceof Toolbar) { if (actionBarView instanceof Toolbar) {
((Toolbar) actionBarView).setTitleTextColor(mActionBarItemsColor); ((Toolbar) actionBarView).setTitleTextColor(mActionBarItemsColor);
((Toolbar) actionBarView).setSubtitleTextColor(mActionBarItemsColor);
ThemeUtils.setActionBarOverflowColor((Toolbar) actionBarView, mActionBarItemsColor); ThemeUtils.setActionBarOverflowColor((Toolbar) actionBarView, mActionBarItemsColor);
} }
} }
@ -250,7 +250,7 @@ public class LinkHandlerActivity extends BaseActionBarActivity implements System
} }
} }
if (transitionRes != 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP if (transitionRes != 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& !ThemeUtils.isTransparentBackground(this)) { && !ThemeUtils.isTransparentBackground(getCurrentThemeBackgroundOption())) {
Utils.setSharedElementTransition(this, window, transitionRes); Utils.setSharedElementTransition(this, window, transitionRes);
} }
} }

View File

@ -47,10 +47,21 @@ public abstract class ThemedActionBarActivity extends ActionBarActivity implemen
private int mCurrentThemeResource, mCurrentThemeColor, mCurrentThemeBackgroundAlpha; private int mCurrentThemeResource, mCurrentThemeColor, mCurrentThemeBackgroundAlpha;
@ShapeStyle @ShapeStyle
private int mProfileImageStyle; private int mProfileImageStyle;
private String mCurrentThemeBackgroundOption;
@Override @Override
public Resources getDefaultResources() { public int getCurrentThemeBackgroundAlpha() {
return super.getResources(); return mCurrentThemeBackgroundAlpha;
}
@Override
public String getCurrentThemeBackgroundOption() {
return mCurrentThemeBackgroundOption;
}
@Override
public int getCurrentThemeColor() {
return mCurrentThemeColor;
} }
@Override @Override
@ -58,19 +69,19 @@ public abstract class ThemedActionBarActivity extends ActionBarActivity implemen
return mCurrentThemeResource; return mCurrentThemeResource;
} }
@Override
public Resources getDefaultResources() {
return super.getResources();
}
@Override @Override
public int getThemeBackgroundAlpha() { public int getThemeBackgroundAlpha() {
return ThemeUtils.isTransparentBackground(this) ? ThemeUtils.getUserThemeBackgroundAlpha(this) : 0xff; return ThemeUtils.getUserThemeBackgroundAlpha(this);
} }
@Override @Override
public int getCurrentThemeBackgroundAlpha() { public String getThemeBackgroundOption() {
return mCurrentThemeBackgroundAlpha; return ThemeUtils.getThemeBackgroundOption(this);
}
@Override
public int getCurrentThemeColor() {
return mCurrentThemeColor;
} }
@Override @Override
@ -94,15 +105,10 @@ public abstract class ThemedActionBarActivity extends ActionBarActivity implemen
StrictModeUtils.detectAllVmPolicy(); StrictModeUtils.detectAllVmPolicy();
StrictModeUtils.detectAllThreadPolicy(); StrictModeUtils.detectAllThreadPolicy();
} }
setTheme(); setupTheme();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
@Override
protected void onStart() {
super.onStart();
}
@Override @Override
protected void onTitleChanged(CharSequence title, int color) { protected void onTitleChanged(CharSequence title, int color) {
final SpannableStringBuilder builder = new SpannableStringBuilder(title); final SpannableStringBuilder builder = new SpannableStringBuilder(title);
@ -130,18 +136,20 @@ public abstract class ThemedActionBarActivity extends ActionBarActivity implemen
super.onResume(); super.onResume();
} }
protected boolean shouldSetWindowBackground() { @Override
return true; protected void onStart() {
super.onStart();
} }
private void setTheme() { private void setupTheme() {
mCurrentThemeResource = getThemeResourceId(); mCurrentThemeResource = getThemeResourceId();
mCurrentThemeColor = getThemeColor(); mCurrentThemeColor = getThemeColor();
mCurrentThemeBackgroundAlpha = getThemeBackgroundAlpha(); mCurrentThemeBackgroundAlpha = getThemeBackgroundAlpha();
mProfileImageStyle = Utils.getProfileImageStyle(this); mProfileImageStyle = Utils.getProfileImageStyle(this);
mCurrentThemeBackgroundOption = getThemeBackgroundOption();
setTheme(mCurrentThemeResource); setTheme(mCurrentThemeResource);
if (shouldSetWindowBackground() && ThemeUtils.isTransparentBackground(mCurrentThemeResource)) { ThemeUtils.applyWindowBackground(this, getWindow(), mCurrentThemeResource, mCurrentThemeBackgroundOption, mCurrentThemeBackgroundAlpha);
getWindow().setBackgroundDrawable(ThemeUtils.getWindowBackground(this));
}
} }
} }

View File

@ -47,20 +47,21 @@ public abstract class ThemedFragmentActivity extends FragmentActivity implements
private int mCurrentThemeResource, mCurrentThemeColor, mCurrentThemeBackgroundAlpha; private int mCurrentThemeResource, mCurrentThemeColor, mCurrentThemeBackgroundAlpha;
@ShapeStyle @ShapeStyle
private int mProfileImageStyle; private int mProfileImageStyle;
private String mCurrentThemeBackgroundOption;
@Override
public Resources getDefaultResources() {
return super.getResources();
}
@Override @Override
public final int getCurrentThemeResourceId() { public final int getCurrentThemeResourceId() {
return mCurrentThemeResource; return mCurrentThemeResource;
} }
@Override
public Resources getDefaultResources() {
return super.getResources();
}
@Override @Override
public int getThemeBackgroundAlpha() { public int getThemeBackgroundAlpha() {
return ThemeUtils.isTransparentBackground(this) ? ThemeUtils.getUserThemeBackgroundAlpha(this) : 0xff; return ThemeUtils.getUserThemeBackgroundAlpha(this);
} }
@Override @Override
@ -68,6 +69,16 @@ public abstract class ThemedFragmentActivity extends FragmentActivity implements
return mCurrentThemeBackgroundAlpha; return mCurrentThemeBackgroundAlpha;
} }
@Override
public String getCurrentThemeBackgroundOption() {
return mCurrentThemeBackgroundOption;
}
@Override
public String getThemeBackgroundOption() {
return ThemeUtils.getThemeBackgroundOption(this);
}
@Override @Override
public int getCurrentThemeColor() { public int getCurrentThemeColor() {
return mCurrentThemeColor; return mCurrentThemeColor;
@ -98,22 +109,6 @@ public abstract class ThemedFragmentActivity extends FragmentActivity implements
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onTitleChanged(CharSequence title, int color) {
final SpannableStringBuilder builder = new SpannableStringBuilder(title);
final int themeResId = getCurrentThemeResourceId();
final int themeColor = getThemeColor(), contrastColor = ColorUtils.getContrastYIQ(themeColor, 192);
if (!ThemeUtils.isDarkTheme(themeResId)) {
builder.setSpan(new ForegroundColorSpan(contrastColor), 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
super.onTitleChanged(title, color);
}
@Override @Override
public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) { public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) {
final View view = ThemeUtils.createView(name, context, attrs, mCurrentThemeColor); final View view = ThemeUtils.createView(name, context, attrs, mCurrentThemeColor);
@ -130,8 +125,20 @@ public abstract class ThemedFragmentActivity extends FragmentActivity implements
super.onResume(); super.onResume();
} }
protected boolean shouldSetWindowBackground() { @Override
return true; protected void onStart() {
super.onStart();
}
@Override
protected void onTitleChanged(CharSequence title, int color) {
final SpannableStringBuilder builder = new SpannableStringBuilder(title);
final int themeResId = getCurrentThemeResourceId();
final int themeColor = getThemeColor(), contrastColor = ColorUtils.getContrastYIQ(themeColor, 192);
if (!ThemeUtils.isDarkTheme(themeResId)) {
builder.setSpan(new ForegroundColorSpan(contrastColor), 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
super.onTitleChanged(title, color);
} }
private void setTheme() { private void setTheme() {
@ -139,9 +146,8 @@ public abstract class ThemedFragmentActivity extends FragmentActivity implements
mCurrentThemeColor = getThemeColor(); mCurrentThemeColor = getThemeColor();
mCurrentThemeBackgroundAlpha = getThemeBackgroundAlpha(); mCurrentThemeBackgroundAlpha = getThemeBackgroundAlpha();
mProfileImageStyle = Utils.getProfileImageStyle(this); mProfileImageStyle = Utils.getProfileImageStyle(this);
mCurrentThemeBackgroundOption = getThemeBackgroundOption();
setTheme(mCurrentThemeResource); setTheme(mCurrentThemeResource);
if (shouldSetWindowBackground() && ThemeUtils.isTransparentBackground(mCurrentThemeResource)) { ThemeUtils.applyWindowBackground(this, getWindow(), mCurrentThemeResource, mCurrentThemeBackgroundOption, mCurrentThemeBackgroundAlpha);
getWindow().setBackgroundDrawable(ThemeUtils.getWindowBackground(this));
}
} }
} }

View File

@ -1094,8 +1094,8 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
final boolean isTransparentBackground; final boolean isTransparentBackground;
if (activity instanceof IThemedActivity) { if (activity instanceof IThemedActivity) {
final int themeRes = ((IThemedActivity) activity).getCurrentThemeResourceId(); final String backgroundOption = ((IThemedActivity) activity).getCurrentThemeBackgroundOption();
isTransparentBackground = ThemeUtils.isTransparentBackground(themeRes); isTransparentBackground = ThemeUtils.isTransparentBackground(backgroundOption);
} else { } else {
isTransparentBackground = ThemeUtils.isTransparentBackground(getActivity()); isTransparentBackground = ThemeUtils.isTransparentBackground(getActivity());
} }
@ -1324,8 +1324,11 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener
if (actionBar == null) return; if (actionBar == null) return;
final Drawable shadow = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.shadow_user_banner_action_bar, null); final Drawable shadow = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.shadow_user_banner_action_bar, null);
mActionBarBackground = new ActionBarDrawable(shadow); mActionBarBackground = new ActionBarDrawable(shadow);
mActionBarBackground.setAlpha(linkHandler.getCurrentThemeBackgroundAlpha()); if (!ThemeUtils.isWindowFloating(linkHandler, linkHandler.getCurrentThemeResourceId())
mProfileBannerView.setAlpha(linkHandler.getCurrentThemeBackgroundAlpha() / 255f); && ThemeUtils.isTransparentBackground(linkHandler.getCurrentThemeBackgroundOption())) {
mActionBarBackground.setAlpha(linkHandler.getCurrentThemeBackgroundAlpha());
mProfileBannerView.setAlpha(linkHandler.getCurrentThemeBackgroundAlpha() / 255f);
}
actionBar.setBackgroundDrawable(mActionBarBackground); actionBar.setBackgroundDrawable(mActionBarBackground);
mActionBarHomeAsUpIndicator = ThemeUtils.getActionBarHomeAsUpIndicator(actionBar); mActionBarHomeAsUpIndicator = ThemeUtils.getActionBarHomeAsUpIndicator(actionBar);
actionBar.setHomeAsUpIndicator(mActionBarHomeAsUpIndicator); actionBar.setHomeAsUpIndicator(mActionBarHomeAsUpIndicator);

View File

@ -57,6 +57,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
import android.view.Window; import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -165,6 +166,16 @@ public class ThemeUtils implements Constants {
} }
} }
public static void applyWindowBackground(Context context, Window window, int theme, String option, int alpha) {
if (window.isFloating()) return;
if (VALUE_THEME_BACKGROUND_TRANSPARENT.equals(option)) {
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
window.setBackgroundDrawable(ThemeUtils.getWindowBackgroundApplyAlpha(context, alpha));
} else if (VALUE_THEME_BACKGROUND_SOLID.equals(option)) {
window.setBackgroundDrawable(new ColorDrawable(isDarkTheme(theme) ? Color.BLACK : Color.WHITE));
}
}
public static Drawable getCompatToolbarOverlay(Activity activity) { public static Drawable getCompatToolbarOverlay(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return null;
final View view = activity.getWindow().findViewById(android.support.v7.appcompat.R.id.decor_content_parent); final View view = activity.getWindow().findViewById(android.support.v7.appcompat.R.id.decor_content_parent);
@ -199,6 +210,21 @@ public class ThemeUtils implements Constants {
indicator.updateAppearance(); indicator.updateAppearance();
} }
public static boolean isWindowFloating(Context context, int theme) {
final TypedArray a;
if (theme != 0) {
//noinspection ConstantConditions
a = context.obtainStyledAttributes(null, new int[]{android.R.attr.windowIsFloating}, 0, theme);
} else {
a = context.obtainStyledAttributes(new int[]{android.R.attr.windowIsFloating});
}
try {
return a.getBoolean(0, false);
} finally {
a.recycle();
}
}
public static void resetCheatSheet(ActionMenuView menuView) { public static void resetCheatSheet(ActionMenuView menuView) {
final OnLongClickListener listener = new OnLongClickListener() { final OnLongClickListener listener = new OnLongClickListener() {
@Override @Override
@ -357,6 +383,7 @@ public class ThemeUtils implements Constants {
public static int getActionBarPopupThemeRes(final Context context) { public static int getActionBarPopupThemeRes(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return 0; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return 0;
@SuppressLint("InlinedApi")
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.actionBarPopupTheme}); final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.actionBarPopupTheme});
try { try {
return a.getResourceId(0, 0); return a.getResourceId(0, 0);
@ -475,6 +502,7 @@ public class ThemeUtils implements Constants {
} }
public static float getSupportActionBarElevation(final Context context) { public static float getSupportActionBarElevation(final Context context) {
@SuppressWarnings("ConstantConditions")
final TypedArray a = context.obtainStyledAttributes(null, new int[]{R.attr.elevation}, R.attr.actionBarStyle, 0); final TypedArray a = context.obtainStyledAttributes(null, new int[]{R.attr.elevation}, R.attr.actionBarStyle, 0);
try { try {
return a.getDimension(0, 0); return a.getDimension(0, 0);
@ -486,6 +514,7 @@ public class ThemeUtils implements Constants {
public static float getActionBarElevation(final Context context) { public static float getActionBarElevation(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return 0; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return 0;
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
@SuppressWarnings("ConstantConditions")
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.elevation}, final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.elevation},
android.R.attr.actionBarStyle, 0); android.R.attr.actionBarStyle, 0);
try { try {
@ -496,21 +525,24 @@ public class ThemeUtils implements Constants {
} }
@Deprecated
public static Drawable getActionBarBackground(final Context context, final int themeRes) { public static Drawable getActionBarBackground(final Context context, final int themeRes) {
@SuppressWarnings("ConstantConditions")
final TypedArray a1 = context.obtainStyledAttributes(null, new int[]{android.R.attr.background}, final TypedArray a1 = context.obtainStyledAttributes(null, new int[]{android.R.attr.background},
R.attr.actionBarStyle, themeRes); R.attr.actionBarStyle, themeRes);
try { try {
if (a1.hasValue(0)) { if (a1.hasValue(0)) {
return applyActionBarDrawable(context, a1.getDrawable(0), isTransparentBackground(themeRes)); return applyActionBarDrawable(context, a1.getDrawable(0), isTransparentBackground(context));
} }
} finally { } finally {
a1.recycle(); a1.recycle();
} }
@SuppressWarnings("ConstantConditions")
final TypedArray a2 = context.obtainStyledAttributes(null, new int[]{android.R.attr.background}, final TypedArray a2 = context.obtainStyledAttributes(null, new int[]{android.R.attr.background},
android.R.attr.actionBarStyle, themeRes); android.R.attr.actionBarStyle, themeRes);
try { try {
if (a2.hasValue(0)) { if (a2.hasValue(0)) {
return applyActionBarDrawable(context, a2.getDrawable(0), isTransparentBackground(themeRes)); return applyActionBarDrawable(context, a2.getDrawable(0), isTransparentBackground(context));
} }
} finally { } finally {
a2.recycle(); a2.recycle();
@ -528,7 +560,7 @@ public class ThemeUtils implements Constants {
actionBarColor = accentColor; actionBarColor = accentColor;
} }
final ColorDrawable d = new ActionBarColorDrawable(actionBarColor, outlineEnabled); final ColorDrawable d = new ActionBarColorDrawable(actionBarColor, outlineEnabled);
return applyActionBarDrawable(context, d, isTransparentBackground(themeRes)); return applyActionBarDrawable(context, d, isTransparentBackground(context));
} }
@NonNull @NonNull
@ -541,10 +573,11 @@ public class ThemeUtils implements Constants {
actionBarColor = accentColor; actionBarColor = accentColor;
} }
final ColorDrawable d = new ActionBarColorDrawable(actionBarColor, outlineEnabled); final ColorDrawable d = new ActionBarColorDrawable(actionBarColor, outlineEnabled);
return applyActionBarDrawable(context, d, isTransparentBackground(themeRes)); return applyActionBarDrawable(context, d, isTransparentBackground(context));
} }
public static Context getActionBarContext(final Context context) { public static Context getActionBarContext(final Context context) {
@SuppressLint("InlinedApi")
final TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.actionBarTheme, final TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.actionBarTheme,
R.attr.actionBarWidgetTheme, android.R.attr.actionBarTheme, R.attr.actionBarWidgetTheme, android.R.attr.actionBarTheme,
android.R.attr.actionBarWidgetTheme}); android.R.attr.actionBarWidgetTheme});
@ -561,18 +594,12 @@ public class ThemeUtils implements Constants {
public static Drawable getActionBarSplitBackground(final Context context, final int themeRes) { public static Drawable getActionBarSplitBackground(final Context context, final int themeRes) {
@SuppressWarnings("ConstantConditions")
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.backgroundSplit}, final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.backgroundSplit},
android.R.attr.actionBarStyle, themeRes); android.R.attr.actionBarStyle, themeRes);
final Drawable d = a.getDrawable(0); final Drawable d = a.getDrawable(0);
a.recycle(); a.recycle();
return applyActionBarDrawable(context, d, isTransparentBackground(themeRes)); return applyActionBarDrawable(context, d, isTransparentBackground(context));
}
public static int getColorBackgroundCacheHint(final Context context) {
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.colorBackgroundCacheHint});
final int color = a.getColor(0, Color.TRANSPARENT);
a.recycle();
return color;
} }
public static int getCardBackgroundColor(final Context context) { public static int getCardBackgroundColor(final Context context) {
@ -610,26 +637,9 @@ public class ThemeUtils implements Constants {
} }
public static int getDrawerThemeResource(final int themeRes) { public static int getDrawerThemeResource(final int themeRes) {
switch (themeRes) {
case R.style.Theme_Twidere_Dark_Transparent:
case R.style.Theme_Twidere_Light_Transparent:
return R.style.Theme_Twidere_Drawer_Dark_Transparent;
}
return R.style.Theme_Twidere_Drawer_Dark; return R.style.Theme_Twidere_Drawer_Dark;
} }
public static int getLightDrawerThemeResource(final Context context) {
return getLightDrawerThemeResource(getThemeResource(context));
}
public static int getLightDrawerThemeResource(final int themeRes) {
switch (themeRes) {
case R.style.Theme_Twidere_Light_Transparent:
return R.style.Theme_Twidere_Drawer_Light_Transparent;
}
return R.style.Theme_Twidere_Drawer_Light;
}
public static Drawable getImageHighlightDrawable(final Context context) { public static Drawable getImageHighlightDrawable(final Context context) {
final Drawable d = getSelectableItemBackgroundDrawable(context); final Drawable d = getSelectableItemBackgroundDrawable(context);
if (d != null) { if (d != null) {
@ -693,6 +703,7 @@ public class ThemeUtils implements Constants {
} }
public static int getTextColorSecondary(final Context context) { public static int getTextColorSecondary(final Context context) {
@SuppressWarnings("ConstantConditions")
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary}); final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary});
try { try {
return a.getColor(0, Color.TRANSPARENT); return a.getColor(0, Color.TRANSPARENT);
@ -706,11 +717,6 @@ public class ThemeUtils implements Constants {
} }
public static int getThemeAlpha(final int themeRes) { public static int getThemeAlpha(final int themeRes) {
switch (themeRes) {
case R.style.Theme_Twidere_Dark_Transparent:
case R.style.Theme_Twidere_Light_Transparent:
return 0xa0;
}
return 0xff; return 0xff;
} }
@ -760,6 +766,7 @@ public class ThemeUtils implements Constants {
if (themeRes == 0) { if (themeRes == 0) {
return getThemeBackgroundColor(context); return getThemeBackgroundColor(context);
} }
@SuppressWarnings("ConstantConditions")
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.colorBackground}, final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.colorBackground},
0, themeRes); 0, themeRes);
try { try {
@ -798,28 +805,29 @@ public class ThemeUtils implements Constants {
} }
public static int getThemeResource(final Context context) { public static int getThemeResource(final Context context) {
return getThemeResource(getThemeNameOption(context), getThemeBackgroundOption(context)); return getThemeResource(getThemeNameOption(context));
} }
public static int getThemeResource(final String name, final String background) { public static int getDialogWhenLargeThemeResource(final Context context) {
if (VALUE_THEME_NAME_LIGHT.equals(name)) { return getDialogWhenLargeThemeResource(getThemeNameOption(context));
if (VALUE_THEME_BACKGROUND_SOLID.equals(background)) }
return R.style.Theme_Twidere_Light_SolidBackground;
else if (VALUE_THEME_BACKGROUND_TRANSPARENT.equals(background))
return R.style.Theme_Twidere_Light_Transparent;
return R.style.Theme_Twidere_Light;
} else if (VALUE_THEME_NAME_DARK.equals(name)) { public static int getThemeResource(final String name) {
if (VALUE_THEME_BACKGROUND_SOLID.equals(background)) if (VALUE_THEME_NAME_DARK.equals(name)) {
return R.style.Theme_Twidere_Dark_SolidBackground;
else if (VALUE_THEME_BACKGROUND_TRANSPARENT.equals(background))
return R.style.Theme_Twidere_Dark_Transparent;
return R.style.Theme_Twidere_Dark; return R.style.Theme_Twidere_Dark;
} }
return R.style.Theme_Twidere_Light; return R.style.Theme_Twidere_Light;
} }
public static int getDialogWhenLargeThemeResource(final String name) {
if (VALUE_THEME_NAME_DARK.equals(name)) {
return R.style.Theme_Twidere_Dark_DialogWhenLarge;
}
return R.style.Theme_Twidere_Light_DialogWhenLarge;
}
public static int getTitleTextAppearance(final Context context) { public static int getTitleTextAppearance(final Context context) {
@SuppressWarnings("ConstantConditions")
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.titleTextStyle}, final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.titleTextStyle},
android.R.attr.actionBarStyle, android.R.style.Widget_Holo_ActionBar); android.R.attr.actionBarStyle, android.R.style.Widget_Holo_ActionBar);
final int textAppearance = a.getResourceId(0, android.R.style.TextAppearance_Holo); final int textAppearance = a.getResourceId(0, android.R.style.TextAppearance_Holo);
@ -848,6 +856,10 @@ public class ThemeUtils implements Constants {
public static int getUserThemeBackgroundAlpha(final Context context) { public static int getUserThemeBackgroundAlpha(final Context context) {
if (context instanceof IThemedActivity) {
final int alpha = ((IThemedActivity) context).getCurrentThemeBackgroundAlpha();
if (alpha >= 0) return alpha;
}
if (context == null) return DEFAULT_THEME_BACKGROUND_ALPHA; if (context == null) return DEFAULT_THEME_BACKGROUND_ALPHA;
final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context); final SharedPreferencesWrapper pref = getSharedPreferencesWrapper(context);
return pref.getInt(KEY_THEME_BACKGROUND_ALPHA, DEFAULT_THEME_BACKGROUND_ALPHA); return pref.getInt(KEY_THEME_BACKGROUND_ALPHA, DEFAULT_THEME_BACKGROUND_ALPHA);
@ -885,17 +897,27 @@ public class ThemeUtils implements Constants {
return d; return d;
} }
public static Drawable getWindowBackground(final Context context, final int themeRes) { public static Drawable getWindowBackground(final Context context, int theme) {
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.windowBackground}, 0, @SuppressWarnings("ConstantConditions")
themeRes); final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.windowBackground}, 0, theme);
final Drawable d = a.getDrawable(0); final Drawable d = a.getDrawable(0);
a.recycle(); a.recycle();
if (isTransparentBackground(themeRes)) { if (isTransparentBackground(context)) {
applyThemeBackgroundAlphaToDrawable(context, d); applyThemeBackgroundAlphaToDrawable(context, d);
} }
return d; return d;
} }
public static Drawable getWindowBackgroundApplyAlpha(final Context context, final int alpha) {
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
final Drawable d = a.getDrawable(0);
a.recycle();
if (d != null) {
d.setAlpha(alpha);
}
return d;
}
public static Drawable getWindowContentOverlay(final Context context) { public static Drawable getWindowContentOverlay(final Context context) {
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.windowContentOverlay}); final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.windowContentOverlay});
final Drawable d = a.getDrawable(0); final Drawable d = a.getDrawable(0);
@ -910,26 +932,24 @@ public class ThemeUtils implements Constants {
public static boolean isDarkTheme(final int themeRes) { public static boolean isDarkTheme(final int themeRes) {
switch (themeRes) { switch (themeRes) {
case R.style.Theme_Twidere_Dark: case R.style.Theme_Twidere_Dark:
case R.style.Theme_Twidere_Dark_SolidBackground:
case R.style.Theme_Twidere_Dark_Dialog: case R.style.Theme_Twidere_Dark_Dialog:
case R.style.Theme_Twidere_Dark_DialogWhenLarge:
case R.style.Theme_Twidere_Dark_Compose: case R.style.Theme_Twidere_Dark_Compose:
case R.style.Theme_Twidere_Dark_Transparent:
return true; return true;
} }
return false; return false;
} }
public static boolean isTransparentBackground(final Context context) { public static boolean isTransparentBackground(final Context context) {
return isTransparentBackground(getThemeResource(context)); if (context instanceof IThemedActivity) {
final String option = ((IThemedActivity) context).getCurrentThemeBackgroundOption();
if (option != null) return isTransparentBackground(option);
}
return isTransparentBackground(getThemeBackgroundOption(context));
} }
public static boolean isTransparentBackground(final int themeRes) { public static boolean isTransparentBackground(final String option) {
switch (themeRes) { return VALUE_THEME_BACKGROUND_TRANSPARENT.equals(option);
case R.style.Theme_Twidere_Dark_Transparent:
case R.style.Theme_Twidere_Light_Transparent:
return true;
}
return false;
} }
public static void overrideActivityCloseAnimation(final Activity activity) { public static void overrideActivityCloseAnimation(final Activity activity) {
@ -962,6 +982,7 @@ public class ThemeUtils implements Constants {
} }
public static void overrideNormalActivityCloseAnimation(final Activity activity) { public static void overrideNormalActivityCloseAnimation(final Activity activity) {
@SuppressWarnings("ConstantConditions")
final TypedArray a = activity.obtainStyledAttributes(null, ANIM_CLOSE_STYLE_ATTRS, final TypedArray a = activity.obtainStyledAttributes(null, ANIM_CLOSE_STYLE_ATTRS,
0, android.R.style.Animation_Activity); 0, android.R.style.Animation_Activity);
final int activityCloseEnterAnimation = a.getResourceId(0, 0); final int activityCloseEnterAnimation = a.getResourceId(0, 0);
@ -992,6 +1013,7 @@ public class ThemeUtils implements Constants {
public static int getThemeColor(Context context, int themeResourceId) { public static int getThemeColor(Context context, int themeResourceId) {
final Context appContext = context.getApplicationContext(); final Context appContext = context.getApplicationContext();
final Resources res = appContext.getResources(); final Resources res = appContext.getResources();
@SuppressWarnings("ConstantConditions")
final TypedArray a = appContext.obtainStyledAttributes(null, final TypedArray a = appContext.obtainStyledAttributes(null,
new int[]{android.R.attr.colorActivatedHighlight}, 0, themeResourceId); new int[]{android.R.attr.colorActivatedHighlight}, 0, themeResourceId);
try { try {
@ -1045,6 +1067,13 @@ public class ThemeUtils implements Constants {
} }
} }
public static void setActionBarSubtitleTextColor(Window window, int itemColor) {
final View actionBarView = window.findViewById(android.support.v7.appcompat.R.id.action_bar);
if (actionBarView instanceof Toolbar) {
((Toolbar) actionBarView).setSubtitleTextColor(itemColor);
}
}
public static Drawable getActionBarHomeAsUpIndicator(android.support.v7.app.ActionBar actionBar) { public static Drawable getActionBarHomeAsUpIndicator(android.support.v7.app.ActionBar actionBar) {
final Context context = actionBar.getThemedContext(); final Context context = actionBar.getThemedContext();
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")

View File

@ -136,37 +136,6 @@
<item name="messageBubbleColor">@color/message_bubble_color_light</item> <item name="messageBubbleColor">@color/message_bubble_color_light</item>
</style> </style>
<style name="Theme.Twidere.Dark.Transparent">
<!-- Colors -->
<item name="android:colorBackgroundCacheHint">@null</item>
<!-- Window attributes -->
<!--<item name="android:windowBackground">@color/bg_color_transparent_dark</item>-->
<item name="android:windowShowWallpaper">true</item>
</style>
<style name="Theme.Twidere.Light.Transparent">
<!-- Colors -->
<item name="android:colorBackgroundCacheHint">@null</item>
<!-- Window attributes -->
<!--<item name="android:windowBackground">@color/bg_color_transparent_light</item>-->
<item name="android:windowShowWallpaper">true</item>
</style>
<style name="Theme.Twidere.Dark.SolidBackground">
<!-- Window attributes -->
<item name="android:windowBackground">@android:color/black</item>
</style>
<style name="Theme.Twidere.Light.SolidBackground">
<!-- Window attributes -->
<item name="android:windowBackground">@android:color/white</item>
</style>
<style name="Theme.Twidere.Dark.Dialog" parent="Theme.Base.Dialog"> <style name="Theme.Twidere.Dark.Dialog" parent="Theme.Base.Dialog">
<!-- Custom view styles --> <!-- Custom view styles -->
@ -296,27 +265,10 @@
</style> </style>
<style name="Theme.Twidere.Drawer.Dark" parent="Theme.Twidere.Dark"> <style name="Theme.Twidere.Drawer.Dark" parent="Theme.Twidere.Dark">
<!-- Window attributes --> <!-- Window attributes -->
<item name="android:windowBackground">@color/bg_color_drawer_dark</item> <item name="android:windowBackground">@color/bg_color_drawer_dark</item>
</style> </style>
<style name="Theme.Twidere.Drawer.Dark.Transparent" parent="Theme.Twidere.Dark.Transparent">
<!-- Window attributes -->
<item name="android:windowBackground">@color/bg_color_drawer_transparent_dark</item>
</style>
<style name="Theme.Twidere.Drawer.Light" parent="Theme.Twidere.Light">
<!-- Window attributes -->
</style>
<style name="Theme.Twidere.Drawer.Light.Transparent" parent="Theme.Twidere.Light.Transparent">
<!-- Window attributes -->
</style>
<style name="Theme.Twidere.Dark.NoDisplay" parent="Theme.Compat.Base"> <style name="Theme.Twidere.Dark.NoDisplay" parent="Theme.Compat.Base">
<item name="android:windowBackground">@null</item> <item name="android:windowBackground">@null</item>
<item name="android:windowContentOverlay">@null</item> <item name="android:windowContentOverlay">@null</item>