-
Notifications
You must be signed in to change notification settings - Fork 215
/
UIButton.java
222 lines (185 loc) · 7.25 KB
/
UIButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* 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/. */
package org.mozilla.vrbrowser.ui.views;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageButton;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.widgets.TooltipWidget;
import org.mozilla.vrbrowser.ui.widgets.UIWidget;
import org.mozilla.vrbrowser.utils.ViewUtils;
public class UIButton extends AppCompatImageButton implements CustomUIButton {
private enum State {
NORMAL,
PRIVATE,
ACTIVE
}
private ColorStateList mTintColorList;
private Drawable mPrivateModeBackground;
private Drawable mActiveModeBackground;
private Drawable mBackground;
private @IdRes int mTintColorListRes;
private @IdRes int mPrivateModeTintColorListRes;
private @IdRes int mActiveModeTintColorListRes;
private TooltipWidget mTooltipView;
private String mTooltipText;
private State mState;
private int mTooltipDelay;
private float mTooltipDensity;
private ViewUtils.TooltipPosition mTooltipPosition;
public UIButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.imageButtonStyle);
}
public UIButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.UIButton, defStyleAttr, 0);
mTintColorListRes = attributes.getResourceId(R.styleable.UIButton_tintColorList, 0);
if (mTintColorListRes != 0) {
setTintColorList(mTintColorListRes);
}
mPrivateModeBackground = attributes.getDrawable(R.styleable.UIButton_privateModeBackground);
mActiveModeBackground = attributes.getDrawable(R.styleable.UIButton_activeModeBackground);
mPrivateModeTintColorListRes = attributes.getResourceId(R.styleable.UIButton_privateModeTintColorList, 0);
mActiveModeTintColorListRes = attributes.getResourceId(R.styleable.UIButton_activeModeTintColorList, 0);
mTooltipDelay = attributes.getInt(R.styleable.UIButton_tooltipDelay, getResources().getInteger(R.integer.tooltip_delay));
mTooltipPosition = ViewUtils.TooltipPosition.fromId(attributes.getInt(R.styleable.UIButton_tooltipPosition, ViewUtils.TooltipPosition.BOTTOM.ordinal()));
mTooltipDensity = attributes.getFloat(R.styleable.UIButton_tooltipDensity, getContext().getResources().getDisplayMetrics().density);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
TypedArray arr = context.obtainStyledAttributes(attrs, new int [] {android.R.attr.tooltipText});
mTooltipText = arr.getString(0);
}
attributes.recycle();
mBackground = getBackground();
mState = State.NORMAL;
}
@TargetApi(Build.VERSION_CODES.O)
public String getTooltip() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return mTooltipText;
} else {
return getTooltipText() == null ? null : getTooltipText().toString();
}
}
@TargetApi(Build.VERSION_CODES.O)
public void setTooltip(String text) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
mTooltipText = text;
} else {
setTooltipText(text);
}
if (mTooltipView != null && mTooltipView.isVisible()) {
mTooltipView.setText(text);
}
}
public void setTooltipText(@NonNull String text) {
mTooltipText = text;
}
@Override
public boolean onHoverEvent(MotionEvent event) {
if (getTooltip() != null) {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
ThreadUtils.postDelayedToUiThread(mShowTooltipRunnable, mTooltipDelay);
} else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
ThreadUtils.removeCallbacksFromUiThread(mShowTooltipRunnable);
ThreadUtils.postToUiThread(mHideTooltipRunnable);
}
}
return super.onHoverEvent(event);
}
public void setTintColorList(int aColorListId) {
mTintColorList = getContext().getResources().getColorStateList(
aColorListId,
getContext().getTheme());
if (mTintColorList != null) {
int color = mTintColorList.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mTintColorList != null && mTintColorList.isStateful()) {
int color = mTintColorList.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
}
@Override
public void setPrivateMode(boolean isPrivateMode) {
if (isPrivateMode) {
setPrivate();
} else {
setNormal();
}
}
public void setActiveMode(boolean isActive) {
if (isActive) {
setActive();
} else {
setNormal();
}
}
public boolean isActive() {
return mState == State.ACTIVE;
}
public boolean isPrivate() {
return mState == State.PRIVATE;
}
private void setPrivate() {
mState = State.PRIVATE;
if (mPrivateModeBackground != null) {
setBackground(mPrivateModeBackground);
}
if (mPrivateModeTintColorListRes != 0) {
setTintColorList(mPrivateModeTintColorListRes);
}
}
private void setNormal() {
mState = State.NORMAL;
if (mBackground != null) {
setBackground(mBackground);
}
if(mTintColorListRes != 0) {
setTintColorList(mTintColorListRes);
}
}
private void setActive() {
mState = State.ACTIVE;
if (mActiveModeBackground != null) {
setBackground(mActiveModeBackground);
}
if (mActiveModeTintColorListRes != 0) {
setTintColorList(mActiveModeTintColorListRes);
}
}
private Runnable mShowTooltipRunnable = new Runnable() {
@Override
public void run() {
if (mTooltipView != null && mTooltipView.isVisible()) {
return;
}
mTooltipView = new TooltipWidget(getContext());
mTooltipView.setText(getTooltip());
mTooltipView.setLayoutParams(UIButton.this, mTooltipPosition, mTooltipDensity);
mTooltipView.show(UIWidget.CLEAR_FOCUS);
}
};
private Runnable mHideTooltipRunnable = new Runnable() {
@Override
public void run() {
if (mTooltipView != null) {
mTooltipView.hide(UIWidget.REMOVE_WIDGET);
}
}
};
}