1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/widget/ThemedView.java.frag Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.widget; 1.9 + 1.10 +import org.mozilla.gecko.GeckoApplication; 1.11 +import org.mozilla.gecko.LightweightTheme; 1.12 +import org.mozilla.gecko.R; 1.13 + 1.14 +import android.content.Context; 1.15 +import android.content.res.TypedArray; 1.16 +import android.graphics.drawable.ColorDrawable; 1.17 +import android.util.AttributeSet; 1.18 + 1.19 +public class Themed@VIEW_NAME_SUFFIX@ extends @BASE_TYPE@ 1.20 + implements LightweightTheme.OnChangeListener { 1.21 + private final LightweightTheme mTheme; 1.22 + 1.23 + private static final int[] STATE_PRIVATE_MODE = { R.attr.state_private }; 1.24 + private static final int[] STATE_LIGHT = { R.attr.state_light }; 1.25 + private static final int[] STATE_DARK = { R.attr.state_dark }; 1.26 + 1.27 + protected static final int[] PRIVATE_PRESSED_STATE_SET = { R.attr.state_private, android.R.attr.state_pressed }; 1.28 + protected static final int[] PRIVATE_FOCUSED_STATE_SET = { R.attr.state_private, android.R.attr.state_focused }; 1.29 + protected static final int[] PRIVATE_STATE_SET = { R.attr.state_private }; 1.30 + 1.31 + private boolean mIsPrivate = false; 1.32 + private boolean mIsLight = false; 1.33 + private boolean mIsDark = false; 1.34 + private boolean mAutoUpdateTheme = true; 1.35 + 1.36 + public Themed@VIEW_NAME_SUFFIX@(Context context, AttributeSet attrs) { 1.37 + super(context, attrs); 1.38 + mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme(); 1.39 + 1.40 + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LightweightTheme); 1.41 + mAutoUpdateTheme = a.getBoolean(R.styleable.LightweightTheme_autoUpdateTheme, true); 1.42 + a.recycle(); 1.43 + } 1.44 + 1.45 + @Override 1.46 + public void onAttachedToWindow() { 1.47 + super.onAttachedToWindow(); 1.48 + 1.49 + if (mAutoUpdateTheme) 1.50 + mTheme.addListener(this); 1.51 + } 1.52 + 1.53 + @Override 1.54 + public void onDetachedFromWindow() { 1.55 + super.onDetachedFromWindow(); 1.56 + 1.57 + if (mAutoUpdateTheme) 1.58 + mTheme.removeListener(this); 1.59 + } 1.60 + 1.61 + @Override 1.62 + public int[] onCreateDrawableState(int extraSpace) { 1.63 + final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 1.64 + 1.65 + if (mIsPrivate) 1.66 + mergeDrawableStates(drawableState, STATE_PRIVATE_MODE); 1.67 + else if (mIsLight) 1.68 + mergeDrawableStates(drawableState, STATE_LIGHT); 1.69 + else if (mIsDark) 1.70 + mergeDrawableStates(drawableState, STATE_DARK); 1.71 + 1.72 + return drawableState; 1.73 + } 1.74 + 1.75 + @Override 1.76 + public void onLightweightThemeChanged() { 1.77 + if (mAutoUpdateTheme && mTheme.isEnabled()) 1.78 + setTheme(mTheme.isLightTheme()); 1.79 + } 1.80 + 1.81 + @Override 1.82 + public void onLightweightThemeReset() { 1.83 + if (mAutoUpdateTheme) 1.84 + resetTheme(); 1.85 + } 1.86 + 1.87 + @Override 1.88 + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 1.89 + super.onLayout(changed, left, top, right, bottom); 1.90 + onLightweightThemeChanged(); 1.91 + } 1.92 + 1.93 + public boolean isPrivateMode() { 1.94 + return mIsPrivate; 1.95 + } 1.96 + 1.97 + public void setPrivateMode(boolean isPrivate) { 1.98 + if (mIsPrivate != isPrivate) { 1.99 + mIsPrivate = isPrivate; 1.100 + refreshDrawableState(); 1.101 + } 1.102 + } 1.103 + 1.104 + public void setTheme(boolean isLight) { 1.105 + // Set the theme only if it is different from existing theme. 1.106 + if ((isLight && mIsLight != isLight) || 1.107 + (!isLight && mIsDark == isLight)) { 1.108 + if (isLight) { 1.109 + mIsLight = true; 1.110 + mIsDark = false; 1.111 + } else { 1.112 + mIsLight = false; 1.113 + mIsDark = true; 1.114 + } 1.115 + 1.116 + refreshDrawableState(); 1.117 + } 1.118 + } 1.119 + 1.120 + public void resetTheme() { 1.121 + if (mIsLight || mIsDark) { 1.122 + mIsLight = false; 1.123 + mIsDark = false; 1.124 + refreshDrawableState(); 1.125 + } 1.126 + } 1.127 + 1.128 + public void setAutoUpdateTheme(boolean autoUpdateTheme) { 1.129 + if (mAutoUpdateTheme != autoUpdateTheme) { 1.130 + mAutoUpdateTheme = autoUpdateTheme; 1.131 + 1.132 + if (mAutoUpdateTheme) 1.133 + mTheme.addListener(this); 1.134 + else 1.135 + mTheme.removeListener(this); 1.136 + } 1.137 + } 1.138 + 1.139 + public ColorDrawable getColorDrawable(int id) { 1.140 + return new ColorDrawable(getResources().getColor(id)); 1.141 + } 1.142 +}