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