michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.GeckoApplication; michael@0: import org.mozilla.gecko.LightweightTheme; michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.graphics.drawable.ColorDrawable; michael@0: import android.util.AttributeSet; michael@0: michael@0: public class Themed@VIEW_NAME_SUFFIX@ extends @BASE_TYPE@ michael@0: implements LightweightTheme.OnChangeListener { michael@0: private final LightweightTheme mTheme; michael@0: michael@0: private static final int[] STATE_PRIVATE_MODE = { R.attr.state_private }; michael@0: private static final int[] STATE_LIGHT = { R.attr.state_light }; michael@0: private static final int[] STATE_DARK = { R.attr.state_dark }; michael@0: michael@0: protected static final int[] PRIVATE_PRESSED_STATE_SET = { R.attr.state_private, android.R.attr.state_pressed }; michael@0: protected static final int[] PRIVATE_FOCUSED_STATE_SET = { R.attr.state_private, android.R.attr.state_focused }; michael@0: protected static final int[] PRIVATE_STATE_SET = { R.attr.state_private }; michael@0: michael@0: private boolean mIsPrivate = false; michael@0: private boolean mIsLight = false; michael@0: private boolean mIsDark = false; michael@0: private boolean mAutoUpdateTheme = true; michael@0: michael@0: public Themed@VIEW_NAME_SUFFIX@(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme(); michael@0: michael@0: TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LightweightTheme); michael@0: mAutoUpdateTheme = a.getBoolean(R.styleable.LightweightTheme_autoUpdateTheme, true); michael@0: a.recycle(); michael@0: } michael@0: michael@0: @Override michael@0: public void onAttachedToWindow() { michael@0: super.onAttachedToWindow(); michael@0: michael@0: if (mAutoUpdateTheme) michael@0: mTheme.addListener(this); michael@0: } michael@0: michael@0: @Override michael@0: public void onDetachedFromWindow() { michael@0: super.onDetachedFromWindow(); michael@0: michael@0: if (mAutoUpdateTheme) michael@0: mTheme.removeListener(this); michael@0: } michael@0: michael@0: @Override michael@0: public int[] onCreateDrawableState(int extraSpace) { michael@0: final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); michael@0: michael@0: if (mIsPrivate) michael@0: mergeDrawableStates(drawableState, STATE_PRIVATE_MODE); michael@0: else if (mIsLight) michael@0: mergeDrawableStates(drawableState, STATE_LIGHT); michael@0: else if (mIsDark) michael@0: mergeDrawableStates(drawableState, STATE_DARK); michael@0: michael@0: return drawableState; michael@0: } michael@0: michael@0: @Override michael@0: public void onLightweightThemeChanged() { michael@0: if (mAutoUpdateTheme && mTheme.isEnabled()) michael@0: setTheme(mTheme.isLightTheme()); michael@0: } michael@0: michael@0: @Override michael@0: public void onLightweightThemeReset() { michael@0: if (mAutoUpdateTheme) michael@0: resetTheme(); michael@0: } michael@0: michael@0: @Override michael@0: protected void onLayout(boolean changed, int left, int top, int right, int bottom) { michael@0: super.onLayout(changed, left, top, right, bottom); michael@0: onLightweightThemeChanged(); michael@0: } michael@0: michael@0: public boolean isPrivateMode() { michael@0: return mIsPrivate; michael@0: } michael@0: michael@0: public void setPrivateMode(boolean isPrivate) { michael@0: if (mIsPrivate != isPrivate) { michael@0: mIsPrivate = isPrivate; michael@0: refreshDrawableState(); michael@0: } michael@0: } michael@0: michael@0: public void setTheme(boolean isLight) { michael@0: // Set the theme only if it is different from existing theme. michael@0: if ((isLight && mIsLight != isLight) || michael@0: (!isLight && mIsDark == isLight)) { michael@0: if (isLight) { michael@0: mIsLight = true; michael@0: mIsDark = false; michael@0: } else { michael@0: mIsLight = false; michael@0: mIsDark = true; michael@0: } michael@0: michael@0: refreshDrawableState(); michael@0: } michael@0: } michael@0: michael@0: public void resetTheme() { michael@0: if (mIsLight || mIsDark) { michael@0: mIsLight = false; michael@0: mIsDark = false; michael@0: refreshDrawableState(); michael@0: } michael@0: } michael@0: michael@0: public void setAutoUpdateTheme(boolean autoUpdateTheme) { michael@0: if (mAutoUpdateTheme != autoUpdateTheme) { michael@0: mAutoUpdateTheme = autoUpdateTheme; michael@0: michael@0: if (mAutoUpdateTheme) michael@0: mTheme.addListener(this); michael@0: else michael@0: mTheme.removeListener(this); michael@0: } michael@0: } michael@0: michael@0: public ColorDrawable getColorDrawable(int id) { michael@0: return new ColorDrawable(getResources().getColor(id)); michael@0: } michael@0: }