mobile/android/base/widget/ThemedView.java.frag

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.widget;
michael@0 6
michael@0 7 import org.mozilla.gecko.GeckoApplication;
michael@0 8 import org.mozilla.gecko.LightweightTheme;
michael@0 9 import org.mozilla.gecko.R;
michael@0 10
michael@0 11 import android.content.Context;
michael@0 12 import android.content.res.TypedArray;
michael@0 13 import android.graphics.drawable.ColorDrawable;
michael@0 14 import android.util.AttributeSet;
michael@0 15
michael@0 16 public class Themed@VIEW_NAME_SUFFIX@ extends @BASE_TYPE@
michael@0 17 implements LightweightTheme.OnChangeListener {
michael@0 18 private final LightweightTheme mTheme;
michael@0 19
michael@0 20 private static final int[] STATE_PRIVATE_MODE = { R.attr.state_private };
michael@0 21 private static final int[] STATE_LIGHT = { R.attr.state_light };
michael@0 22 private static final int[] STATE_DARK = { R.attr.state_dark };
michael@0 23
michael@0 24 protected static final int[] PRIVATE_PRESSED_STATE_SET = { R.attr.state_private, android.R.attr.state_pressed };
michael@0 25 protected static final int[] PRIVATE_FOCUSED_STATE_SET = { R.attr.state_private, android.R.attr.state_focused };
michael@0 26 protected static final int[] PRIVATE_STATE_SET = { R.attr.state_private };
michael@0 27
michael@0 28 private boolean mIsPrivate = false;
michael@0 29 private boolean mIsLight = false;
michael@0 30 private boolean mIsDark = false;
michael@0 31 private boolean mAutoUpdateTheme = true;
michael@0 32
michael@0 33 public Themed@VIEW_NAME_SUFFIX@(Context context, AttributeSet attrs) {
michael@0 34 super(context, attrs);
michael@0 35 mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
michael@0 36
michael@0 37 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LightweightTheme);
michael@0 38 mAutoUpdateTheme = a.getBoolean(R.styleable.LightweightTheme_autoUpdateTheme, true);
michael@0 39 a.recycle();
michael@0 40 }
michael@0 41
michael@0 42 @Override
michael@0 43 public void onAttachedToWindow() {
michael@0 44 super.onAttachedToWindow();
michael@0 45
michael@0 46 if (mAutoUpdateTheme)
michael@0 47 mTheme.addListener(this);
michael@0 48 }
michael@0 49
michael@0 50 @Override
michael@0 51 public void onDetachedFromWindow() {
michael@0 52 super.onDetachedFromWindow();
michael@0 53
michael@0 54 if (mAutoUpdateTheme)
michael@0 55 mTheme.removeListener(this);
michael@0 56 }
michael@0 57
michael@0 58 @Override
michael@0 59 public int[] onCreateDrawableState(int extraSpace) {
michael@0 60 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
michael@0 61
michael@0 62 if (mIsPrivate)
michael@0 63 mergeDrawableStates(drawableState, STATE_PRIVATE_MODE);
michael@0 64 else if (mIsLight)
michael@0 65 mergeDrawableStates(drawableState, STATE_LIGHT);
michael@0 66 else if (mIsDark)
michael@0 67 mergeDrawableStates(drawableState, STATE_DARK);
michael@0 68
michael@0 69 return drawableState;
michael@0 70 }
michael@0 71
michael@0 72 @Override
michael@0 73 public void onLightweightThemeChanged() {
michael@0 74 if (mAutoUpdateTheme && mTheme.isEnabled())
michael@0 75 setTheme(mTheme.isLightTheme());
michael@0 76 }
michael@0 77
michael@0 78 @Override
michael@0 79 public void onLightweightThemeReset() {
michael@0 80 if (mAutoUpdateTheme)
michael@0 81 resetTheme();
michael@0 82 }
michael@0 83
michael@0 84 @Override
michael@0 85 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
michael@0 86 super.onLayout(changed, left, top, right, bottom);
michael@0 87 onLightweightThemeChanged();
michael@0 88 }
michael@0 89
michael@0 90 public boolean isPrivateMode() {
michael@0 91 return mIsPrivate;
michael@0 92 }
michael@0 93
michael@0 94 public void setPrivateMode(boolean isPrivate) {
michael@0 95 if (mIsPrivate != isPrivate) {
michael@0 96 mIsPrivate = isPrivate;
michael@0 97 refreshDrawableState();
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 public void setTheme(boolean isLight) {
michael@0 102 // Set the theme only if it is different from existing theme.
michael@0 103 if ((isLight && mIsLight != isLight) ||
michael@0 104 (!isLight && mIsDark == isLight)) {
michael@0 105 if (isLight) {
michael@0 106 mIsLight = true;
michael@0 107 mIsDark = false;
michael@0 108 } else {
michael@0 109 mIsLight = false;
michael@0 110 mIsDark = true;
michael@0 111 }
michael@0 112
michael@0 113 refreshDrawableState();
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 public void resetTheme() {
michael@0 118 if (mIsLight || mIsDark) {
michael@0 119 mIsLight = false;
michael@0 120 mIsDark = false;
michael@0 121 refreshDrawableState();
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 public void setAutoUpdateTheme(boolean autoUpdateTheme) {
michael@0 126 if (mAutoUpdateTheme != autoUpdateTheme) {
michael@0 127 mAutoUpdateTheme = autoUpdateTheme;
michael@0 128
michael@0 129 if (mAutoUpdateTheme)
michael@0 130 mTheme.addListener(this);
michael@0 131 else
michael@0 132 mTheme.removeListener(this);
michael@0 133 }
michael@0 134 }
michael@0 135
michael@0 136 public ColorDrawable getColorDrawable(int id) {
michael@0 137 return new ColorDrawable(getResources().getColor(id));
michael@0 138 }
michael@0 139 }

mercurial