2012-11-28 11:23:54 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-10-10 11:53:14 -07:00
|
|
|
package org.mozilla.gecko.widget;
|
|
|
|
|
2013-10-30 14:44:14 -07:00
|
|
|
import org.mozilla.gecko.GeckoApplication;
|
2013-10-10 11:53:14 -07:00
|
|
|
import org.mozilla.gecko.LightweightTheme;
|
|
|
|
import org.mozilla.gecko.R;
|
2012-11-28 11:23:54 -08:00
|
|
|
|
|
|
|
import android.content.Context;
|
2013-02-26 15:32:53 -08:00
|
|
|
import android.content.res.TypedArray;
|
2013-10-30 14:44:14 -07:00
|
|
|
import android.graphics.drawable.ColorDrawable;
|
2012-11-28 11:23:54 -08:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
2014-04-17 13:34:11 -07:00
|
|
|
public class Themed@VIEW_NAME_SUFFIX@ extends @BASE_TYPE@
|
|
|
|
implements LightweightTheme.OnChangeListener {
|
2014-10-17 13:01:54 -07:00
|
|
|
private LightweightTheme mTheme;
|
2013-10-30 14:44:14 -07:00
|
|
|
|
2012-11-28 11:23:54 -08:00
|
|
|
private static final int[] STATE_PRIVATE_MODE = { R.attr.state_private };
|
2012-12-17 12:31:52 -08:00
|
|
|
private static final int[] STATE_LIGHT = { R.attr.state_light };
|
|
|
|
private static final int[] STATE_DARK = { R.attr.state_dark };
|
2012-11-28 11:23:54 -08:00
|
|
|
|
2013-10-30 14:44:14 -07:00
|
|
|
protected static final int[] PRIVATE_PRESSED_STATE_SET = { R.attr.state_private, android.R.attr.state_pressed };
|
|
|
|
protected static final int[] PRIVATE_FOCUSED_STATE_SET = { R.attr.state_private, android.R.attr.state_focused };
|
|
|
|
protected static final int[] PRIVATE_STATE_SET = { R.attr.state_private };
|
|
|
|
|
2014-07-25 20:14:47 -07:00
|
|
|
private boolean mIsPrivate;
|
|
|
|
private boolean mIsLight;
|
|
|
|
private boolean mIsDark;
|
2013-02-26 15:32:53 -08:00
|
|
|
private boolean mAutoUpdateTheme = true;
|
2012-11-28 11:23:54 -08:00
|
|
|
|
2014-04-17 13:34:11 -07:00
|
|
|
public Themed@VIEW_NAME_SUFFIX@(Context context, AttributeSet attrs) {
|
2014-10-17 13:01:54 -07:00
|
|
|
super(context, attrs);
|
|
|
|
initialize(context, attrs);
|
2014-10-10 11:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//#ifdef STYLE_CONSTRUCTOR
|
2014-10-17 13:01:54 -07:00
|
|
|
public Themed@VIEW_NAME_SUFFIX@(Context context, AttributeSet attrs, int defStyle) {
|
2014-10-10 11:32:12 -07:00
|
|
|
super(context, attrs, defStyle);
|
2014-10-17 13:01:54 -07:00
|
|
|
initialize(context, attrs);
|
|
|
|
}
|
2014-10-10 11:32:12 -07:00
|
|
|
|
2014-10-17 13:01:54 -07:00
|
|
|
//#endif
|
|
|
|
private void initialize(final Context context, final AttributeSet attrs) {
|
2013-10-30 14:44:14 -07:00
|
|
|
mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
|
2013-02-26 15:32:53 -08:00
|
|
|
|
2014-10-17 13:01:54 -07:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LightweightTheme);
|
2013-02-26 15:32:53 -08:00
|
|
|
mAutoUpdateTheme = a.getBoolean(R.styleable.LightweightTheme_autoUpdateTheme, true);
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
|
|
|
|
if (mAutoUpdateTheme)
|
2013-10-30 14:44:14 -07:00
|
|
|
mTheme.addListener(this);
|
2013-02-26 15:32:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
|
|
|
if (mAutoUpdateTheme)
|
2013-10-30 14:44:14 -07:00
|
|
|
mTheme.removeListener(this);
|
2012-11-28 11:23:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] onCreateDrawableState(int extraSpace) {
|
|
|
|
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
|
|
|
|
|
|
|
|
if (mIsPrivate)
|
|
|
|
mergeDrawableStates(drawableState, STATE_PRIVATE_MODE);
|
2012-12-17 12:31:52 -08:00
|
|
|
else if (mIsLight)
|
|
|
|
mergeDrawableStates(drawableState, STATE_LIGHT);
|
|
|
|
else if (mIsDark)
|
|
|
|
mergeDrawableStates(drawableState, STATE_DARK);
|
2012-11-28 11:23:54 -08:00
|
|
|
|
|
|
|
return drawableState;
|
|
|
|
}
|
|
|
|
|
2013-02-26 15:32:53 -08:00
|
|
|
@Override
|
|
|
|
public void onLightweightThemeChanged() {
|
2013-10-30 14:44:14 -07:00
|
|
|
if (mAutoUpdateTheme && mTheme.isEnabled())
|
|
|
|
setTheme(mTheme.isLightTheme());
|
2013-02-26 15:32:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeReset() {
|
|
|
|
if (mAutoUpdateTheme)
|
|
|
|
resetTheme();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
|
|
onLightweightThemeChanged();
|
|
|
|
}
|
|
|
|
|
2012-11-30 13:22:49 -08:00
|
|
|
public boolean isPrivateMode() {
|
|
|
|
return mIsPrivate;
|
|
|
|
}
|
|
|
|
|
2012-11-28 11:23:54 -08:00
|
|
|
public void setPrivateMode(boolean isPrivate) {
|
|
|
|
if (mIsPrivate != isPrivate) {
|
|
|
|
mIsPrivate = isPrivate;
|
|
|
|
refreshDrawableState();
|
|
|
|
}
|
2012-12-17 12:31:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setTheme(boolean isLight) {
|
|
|
|
// Set the theme only if it is different from existing theme.
|
|
|
|
if ((isLight && mIsLight != isLight) ||
|
|
|
|
(!isLight && mIsDark == isLight)) {
|
|
|
|
if (isLight) {
|
|
|
|
mIsLight = true;
|
|
|
|
mIsDark = false;
|
|
|
|
} else {
|
|
|
|
mIsLight = false;
|
|
|
|
mIsDark = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshDrawableState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void resetTheme() {
|
|
|
|
if (mIsLight || mIsDark) {
|
|
|
|
mIsLight = false;
|
|
|
|
mIsDark = false;
|
|
|
|
refreshDrawableState();
|
|
|
|
}
|
2013-02-26 15:32:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAutoUpdateTheme(boolean autoUpdateTheme) {
|
|
|
|
if (mAutoUpdateTheme != autoUpdateTheme) {
|
|
|
|
mAutoUpdateTheme = autoUpdateTheme;
|
|
|
|
|
|
|
|
if (mAutoUpdateTheme)
|
2013-10-30 14:44:14 -07:00
|
|
|
mTheme.addListener(this);
|
2013-02-26 15:32:53 -08:00
|
|
|
else
|
2013-10-30 14:44:14 -07:00
|
|
|
mTheme.removeListener(this);
|
2013-02-26 15:32:53 -08:00
|
|
|
}
|
|
|
|
}
|
2013-10-30 14:44:14 -07:00
|
|
|
|
|
|
|
public ColorDrawable getColorDrawable(int id) {
|
|
|
|
return new ColorDrawable(getResources().getColor(id));
|
|
|
|
}
|
2012-11-28 11:23:54 -08:00
|
|
|
}
|