diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/Chameleon.java b/chameleon/src/main/java/org/mariotaku/chameleon/Chameleon.java index dbd44413a..b2a62d308 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/Chameleon.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/Chameleon.java @@ -18,6 +18,7 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatDelegate; import android.support.v7.widget.DecorToolbar; import android.support.v7.widget.Toolbar; +import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -36,16 +37,23 @@ public class Chameleon { private final Activity activity; private final Theme theme; + @Nullable + private final AppearanceCreator creator; private final ArrayMap postApplyViews; - private Chameleon(Activity activity) { + private Chameleon(Activity activity, @Nullable AppearanceCreator creator) { this.activity = activity; + this.creator = creator; this.theme = getOverrideTheme(activity, activity); this.postApplyViews = new ArrayMap<>(); } public static Chameleon getInstance(Activity activity) { - return new Chameleon(activity); + return getInstance(activity, null); + } + + public static Chameleon getInstance(Activity activity, AppearanceCreator creator) { + return new Chameleon(activity, creator); } @SuppressWarnings("WeakerAccess") @@ -57,7 +65,7 @@ public class Chameleon { delegate = ((AppCompatActivity) activity).getDelegate(); } final ChameleonInflationFactory factory = new ChameleonInflationFactory(inflater, activity, - delegate, theme, postApplyViews); + creator, delegate, theme, postApplyViews); LayoutInflaterCompat.setFactory(inflater, factory); } @@ -146,10 +154,19 @@ public class Chameleon { DrawableCompat.setTint(icon, color); } + public interface AppearanceCreator { + @Nullable + ChameleonView.Appearance createAppearance(@NonNull View view, + @NonNull Context context, + @NonNull AttributeSet attributeSet, + @NonNull Chameleon.Theme theme); + + void applyAppearance(@NonNull View view, @NonNull ChameleonView.Appearance appearance); + } + /** * Created by mariotaku on 2016/12/18. */ - public static class Theme { private int colorBackground; private int colorForeground; diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/ChameleonActivity.java b/chameleon/src/main/java/org/mariotaku/chameleon/ChameleonActivity.java index 8533f3204..a6d5b1b1a 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/ChameleonActivity.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/ChameleonActivity.java @@ -15,7 +15,7 @@ public class ChameleonActivity extends AppCompatActivity implements Chameleon.Th @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - mChameleon = Chameleon.getInstance(this); + mChameleon = Chameleon.getInstance(this, onCreateAppearanceCreator()); mChameleon.preApply(); super.onCreate(savedInstanceState); } @@ -59,4 +59,9 @@ public class ChameleonActivity extends AppCompatActivity implements Chameleon.Th public Chameleon.Theme getOverrideTheme() { return null; } + + @Nullable + protected Chameleon.AppearanceCreator onCreateAppearanceCreator() { + return null; + } } diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/internal/ChameleonInflationFactory.java b/chameleon/src/main/java/org/mariotaku/chameleon/internal/ChameleonInflationFactory.java index 6df0d3ec7..1d359591f 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/internal/ChameleonInflationFactory.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/internal/ChameleonInflationFactory.java @@ -36,6 +36,7 @@ public class ChameleonInflationFactory implements LayoutInflaterFactory { private final LayoutInflater mInflater; @Nullable private final Activity mActivity; + private final Chameleon.AppearanceCreator mCreator; @Nullable private final AppCompatDelegate mDelegate; @NonNull @@ -45,11 +46,13 @@ public class ChameleonInflationFactory implements LayoutInflaterFactory { public ChameleonInflationFactory(@NonNull LayoutInflater inflater, @Nullable Activity activity, + Chameleon.AppearanceCreator creator, @Nullable AppCompatDelegate delegate, @NonNull Chameleon.Theme theme, @NonNull ArrayMap postApplyViews) { this.mInflater = inflater; this.mActivity = activity; + this.mCreator = creator; this.mDelegate = delegate; this.mTheme = theme; this.mPostApplyViews = postApplyViews; @@ -166,18 +169,28 @@ public class ChameleonInflationFactory implements LayoutInflaterFactory { cv.applyAppearance(appearance); } } - } else { - ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attrs, - R.styleable.ChameleonView, mTheme); - Drawable background = a.getDrawable(R.styleable.ChameleonView_android_background); - if (background != null) { - int tint = a.getColor(R.styleable.ChameleonView_backgroundTint, 0); - if (tint != 0) { - DrawableCompat.setTint(background, tint); + } else if (view != null) { + boolean handled = false; + if (mCreator != null) { + ChameleonView.Appearance appearance = mCreator.createAppearance(view, context, attrs, mTheme); + if (appearance != null) { + mCreator.applyAppearance(view, appearance); + handled = true; } - SupportMethods.setBackground(view, background); } - a.recycle(); + if (!handled) { + ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attrs, + R.styleable.ChameleonView, mTheme); + Drawable background = a.getDrawable(R.styleable.ChameleonView_android_background); + if (background != null) { + int tint = a.getColor(R.styleable.ChameleonView_backgroundTint, 0); + if (tint != 0) { + DrawableCompat.setTint(background, tint); + } + SupportMethods.setBackground(view, background); + } + a.recycle(); + } } return view; } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt index 41eb32a8e..d871d18c8 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt @@ -53,6 +53,7 @@ import org.mariotaku.twidere.preference.iface.IDialogPreference import org.mariotaku.twidere.util.* import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback import org.mariotaku.twidere.util.dagger.GeneralComponentHelper +import org.mariotaku.twidere.util.theme.TwidereAppearanceCreator import org.mariotaku.twidere.view.iface.IExtendedView.OnFitSystemWindowsListener import java.lang.reflect.InvocationTargetException import java.util.* @@ -359,8 +360,13 @@ open class BaseActivity : ChameleonActivity(), IExtendedActivity, IThemedActivit return userTheme } + override fun onCreateAppearanceCreator(): Chameleon.AppearanceCreator? { + return TwidereAppearanceCreator + } + companion object { private val sClassPrefixList = arrayOf("android.widget.", "android.view.", "android.webkit.") } } + diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/TwidereAppearanceCreator.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/TwidereAppearanceCreator.kt new file mode 100644 index 000000000..2bd8ff6fc --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/TwidereAppearanceCreator.kt @@ -0,0 +1,32 @@ +package org.mariotaku.twidere.util.theme + +import android.content.Context +import android.util.AttributeSet +import android.view.View +import com.pnikosis.materialishprogress.ProgressWheel +import org.mariotaku.chameleon.Chameleon +import org.mariotaku.chameleon.ChameleonView + +object TwidereAppearanceCreator : Chameleon.AppearanceCreator { + override fun createAppearance(view: View, context: Context, attributeSet: AttributeSet, theme: Chameleon.Theme): ChameleonView.Appearance? { + when (view) { + is ProgressWheel -> { + return BasicColorAppearance(theme.colorAccent) + } + } + return null + } + + override fun applyAppearance(view: View, appearance: ChameleonView.Appearance) { + when (view) { + is ProgressWheel -> { + appearance as BasicColorAppearance + view.barColor = appearance.color + } + } + } + + + data class BasicColorAppearance(var color: Int) : ChameleonView.Appearance + +} \ No newline at end of file