fixed theme not applied to compose activity bug

This commit is contained in:
Mariotaku Lee 2015-04-25 23:49:04 +08:00
parent 4c61a2be38
commit 855594e6f9
5 changed files with 63 additions and 40 deletions

View File

@ -22,16 +22,12 @@ package android.support.v7.app;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.view.LayoutInflaterCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.activity.iface.IThemedActivity;
import org.mariotaku.twidere.util.ThemeUtils;
import org.mariotaku.twidere.util.ThemedLayoutInflaterFactory;
/**
@ -70,15 +66,6 @@ public class ThemedAppCompatDelegate implements Constants {
LayoutInflaterCompat.setFactory(inflater, new ThemedLayoutInflaterFactory(themed, this));
}
}
@Override
public View createView(View parent, String name, @NonNull Context context, @NonNull AttributeSet attrs) {
View view = super.createView(parent, name, context, attrs);
if (view == null) {
// view = ThemeUtils.createCustomView(parent, name, context, attrs);
}
return view;
}
}
}

View File

@ -1259,7 +1259,7 @@ public class ComposeActivity extends ThemedFragmentActivity implements LocationL
public AccountIconsAdapter(ComposeActivity activity) {
mActivity = activity;
mInflater = LayoutInflater.from(activity);
mInflater = activity.getLayoutInflater();
mImageLoader = TwidereApplication.getInstance(activity).getMediaLoaderWrapper();
mSelection = new LongSparseArray<>();
}

View File

@ -19,13 +19,19 @@
package org.mariotaku.twidere.activity.support;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.view.LayoutInflaterFactory;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.activity.iface.IThemedActivity;
@ -34,6 +40,7 @@ import org.mariotaku.twidere.util.KeyboardShortcutsHandler;
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback;
import org.mariotaku.twidere.util.StrictModeUtils;
import org.mariotaku.twidere.util.ThemeUtils;
import org.mariotaku.twidere.util.ThemedLayoutInflaterFactory;
import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.view.ShapedImageView.ShapeStyle;
@ -50,6 +57,21 @@ public abstract class ThemedFragmentActivity extends FragmentActivity implements
private String mCurrentThemeBackgroundOption;
private String mCurrentThemeFontFamily;
@NonNull
@Override
public LayoutInflater getLayoutInflater() {
final LayoutInflater inflater = super.getLayoutInflater();
if (inflater.getFactory() == null) {
LayoutInflaterCompat.setFactory(inflater, new ThemedLayoutInflaterFactory(this, new LayoutInflaterFactory() {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return ThemedFragmentActivity.this.onCreateView(parent, name, context, attrs);
}
}));
}
return inflater;
}
@Override
public String getCurrentThemeFontFamily() {
return mCurrentThemeFontFamily;

View File

@ -23,8 +23,6 @@ import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
@ -37,9 +35,7 @@ import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.TintableBackgroundView;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.internal.app.WindowDecorActionBar;
import android.support.v7.internal.app.WindowDecorActionBar.ActionModeImpl;
import android.support.v7.internal.view.SupportActionModeWrapper;
@ -63,22 +59,18 @@ import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import org.apache.commons.lang3.ArrayUtils;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.R;
import org.mariotaku.twidere.activity.AppCompatPreferenceActivity;
import org.mariotaku.twidere.activity.iface.IThemedActivity;
import org.mariotaku.twidere.graphic.ActionBarColorDrawable;
import org.mariotaku.twidere.graphic.ActionIconDrawable;
import org.mariotaku.twidere.text.ParagraphSpacingSpan;
import org.mariotaku.twidere.util.menu.TwidereMenuInfo;
import org.mariotaku.twidere.view.ShapedImageView;
import org.mariotaku.twidere.view.TabPagerIndicator;
import org.mariotaku.twidere.view.iface.IThemedView;
import java.lang.reflect.Field;

View File

@ -33,6 +33,7 @@ import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@ -62,6 +63,16 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory {
this.delegate = delegate;
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
View view = delegate.onCreateView(parent, name, context, attrs);
if (view == null) {
view = createCustomView(name, context, attrs);
}
initView(view, activity);
return view;
}
public static View createCustomView(String name, Context context, AttributeSet attrs) {
if (!name.contains(".")) return null;
boolean whiteListed = false;
@ -104,21 +115,43 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory {
}
private static void initViewTint(View view, IThemedActivity activity) {
final ColorStateList tintList;
final int noTintColor, tintColor;
final boolean isColorTint;
// View context is not derived from ActionBar, apply color tint directly
if (!isActionBarContext(view.getContext(), getActionBarContext((Activity) activity))) {
tintList = ColorStateList.valueOf(activity.getCurrentThemeColor());
tintColor = activity.getCurrentThemeColor();
noTintColor = TwidereColorUtils.getContrastYIQ(tintColor, 192);
isColorTint = true;
} else if (ThemeUtils.isDarkTheme(activity.getCurrentThemeResourceId())) {
tintList = ColorStateList.valueOf(Color.WHITE);
// View context is derived from ActionBar but is currently dark theme, so we should show
// light
noTintColor = Color.WHITE;
tintColor = activity.getCurrentThemeColor();
isColorTint = true;
} else {
// View context is derived from ActionBar and it's light theme, so we use contrast color
final int themeColor = activity.getCurrentThemeColor();
tintList = ColorStateList.valueOf(TwidereColorUtils.getContrastYIQ(themeColor, 192));
tintColor = TwidereColorUtils.getContrastYIQ(themeColor, 192);
noTintColor = TwidereColorUtils.getContrastYIQ(tintColor, 192);
isColorTint = false;
}
if (view instanceof IThemedView) {
((IThemedView) view).setThemeTintColor(tintList);
((IThemedView) view).setThemeTintColor(ColorStateList.valueOf(tintColor));
} else if (view instanceof TintableBackgroundView) {
((TintableBackgroundView) view).setSupportBackgroundTintList(tintList);
final TintableBackgroundView tintable = (TintableBackgroundView) view;
if (view instanceof Button) {
} else {
if (isColorTint) {
final int[][] states = {{android.R.attr.state_selected}, {android.R.attr.state_focused},
{android.R.attr.state_pressed}, {0}};
final int[] colors = {tintColor, tintColor, tintColor, noTintColor};
tintable.setSupportBackgroundTintList(new ColorStateList(states, colors));
} else {
tintable.setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));
}
}
} else if (view instanceof EditText) {
ViewCompat.setBackgroundTintList(view, tintList);
ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(tintColor));
}
}
@ -145,15 +178,4 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory {
if (actionBar != null) return actionBar.getThemedContext();
return null;
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
View view = delegate.onCreateView(parent, name, context, attrs);
if (view == null) {
view = createCustomView(name, context, attrs);
}
initView(view, activity);
return view;
}
}