-
Notifications
You must be signed in to change notification settings - Fork 215
/
UIButton.java
323 lines (273 loc) · 11.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- 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.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import androidx.annotation.Dimension;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.ViewUtils;
public class UIButton extends AppCompatImageButton implements CustomUIButton {
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 @IdRes int mNotificationModeTintColorListRes;
private TooltipWidget mTooltipView;
private String mTooltipText;
private int mTooltipDelay;
private float mTooltipDensity;
private @LayoutRes int mTooltipLayout;
private boolean mCurvedTooltip = true;
private ViewUtils.TooltipPosition mTooltipPosition;
private boolean mIsPrivate;
private boolean mIsActive;
private boolean mIsNotification;
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);
mNotificationModeTintColorListRes = attributes.getResourceId(R.styleable.UIButton_notificationModeTintColorList, 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()));
TypedValue densityValue = new TypedValue();
getResources().getValue(R.dimen.tooltip_default_density, densityValue, true);
mTooltipDensity = attributes.getFloat(R.styleable.UIButton_tooltipDensity, densityValue.getFloat());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
TypedArray arr = context.obtainStyledAttributes(attrs, new int [] {android.R.attr.tooltipText});
mTooltipText = arr.getString(0);
}
mTooltipLayout = attributes.getResourceId(R.styleable.UIButton_tooltipLayout, R.layout.tooltip);
attributes.recycle();
mBackground = getBackground();
// Android >8 doesn't perform a click when long clicking in ImageViews even if long click is disabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setLongClickable(false);
setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
long time = event.getEventTime() - event.getDownTime();
if (time > ViewConfiguration.getLongPressTimeout()) {
performClick();
}
}
return false;
});
}
}
@Nullable
@Override
public CharSequence getTooltipText() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return getTooltipTextInternal();
} else {
return mTooltipText;
}
}
@TargetApi(Build.VERSION_CODES.O)
private CharSequence getTooltipTextInternal() {
return super.getTooltipText();
}
@Override
public void setTooltipText(@Nullable CharSequence tooltipText) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setTooltipTextInternal(tooltipText);
}
if (tooltipText != null) {
mTooltipText = tooltipText.toString();
if (mTooltipView != null && mTooltipView.isVisible()) {
mTooltipView.setText(tooltipText.toString());
}
}
}
@TargetApi(Build.VERSION_CODES.O)
private void setTooltipTextInternal(@Nullable CharSequence tooltipText) {
super.setTooltipText(tooltipText);
}
public void setCurvedTooltip(boolean aEnabled) {
mCurvedTooltip = aEnabled;
if (mTooltipView != null) {
mTooltipView.setCurvedMode(aEnabled);
}
}
@Override
public boolean onHoverEvent(MotionEvent event) {
if (getTooltipText() != 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) {
mIsPrivate = isPrivateMode;
updateButtonColor();
}
public void setActiveMode(boolean isActive) {
mIsActive = isActive;
updateButtonColor();
}
public void setNotificationMode(boolean isNotification) {
mIsNotification = isNotification;
updateButtonColor();
}
public boolean isActive() {
return mIsActive;
}
public boolean isPrivate() {
return mIsPrivate;
}
private void updateButtonColor() {
if (mIsNotification) {
setNotification();
} else if (mIsPrivate) {
setPrivate();
} else if (mIsActive) {
setActive();
} else {
setNormal();
}
}
private void setPrivate() {
if (mPrivateModeBackground != null) {
setBackground(mPrivateModeBackground);
}
if (mPrivateModeTintColorListRes != 0) {
setTintColorList(mPrivateModeTintColorListRes);
}
}
private void setNormal() {
if (mBackground != null) {
setBackground(mBackground);
}
if(mTintColorListRes != 0) {
setTintColorList(mTintColorListRes);
}
}
private void setActive() {
if (mActiveModeBackground != null) {
setBackground(mActiveModeBackground);
}
if (mActiveModeTintColorListRes != 0) {
setTintColorList(mActiveModeTintColorListRes);
}
}
private void setNotification() {
if (mActiveModeTintColorListRes != 0) {
setTintColorList(mNotificationModeTintColorListRes);
}
}
public void updateBackgrounds(int aBackgroundRes, int aPrivateModeRes, int aActiveModeRes) {
mBackground = getContext().getDrawable(aBackgroundRes);
mPrivateModeBackground = getContext().getDrawable(aPrivateModeRes);
mActiveModeBackground = getContext().getDrawable(aActiveModeRes);
updateButtonColor();
}
private Runnable mShowTooltipRunnable = new Runnable() {
@Override
public void run() {
if (mTooltipView != null && mTooltipView.isVisible()) {
return;
}
if (mTooltipView == null) {
mTooltipView = new TooltipWidget(getContext(), mTooltipLayout);
}
mTooltipView.setCurvedMode(mCurvedTooltip);
if (getTooltipText() != null) {
mTooltipView.setText(getTooltipText().toString());
}
Rect offsetViewBounds = new Rect();
getDrawingRect(offsetViewBounds);
UIWidget parent = ViewUtils.getParentWidget(UIButton.this);
parent.offsetDescendantRectToMyCoords(UIButton.this, offsetViewBounds);
float ratio = WidgetPlacement.viewToWidgetRatio(getContext(), parent);
mTooltipView.getPlacement().parentHandle = parent.getHandle();
mTooltipView.getPlacement().density = mTooltipDensity;
// At the moment we only support showing tooltips on top or bottom of the target view
if (mTooltipPosition == ViewUtils.TooltipPosition.BOTTOM) {
mTooltipView.getPlacement().anchorY = 1.0f;
mTooltipView.getPlacement().parentAnchorY = 0.0f;
mTooltipView.getPlacement().translationX = (offsetViewBounds.left + UIButton.this.getWidth() / 2.0f) * ratio;
mTooltipView.getPlacement().translationY = -offsetViewBounds.top * ratio;
} else {
mTooltipView.getPlacement().anchorY = 0.0f;
mTooltipView.getPlacement().parentAnchorY = 1.0f;
mTooltipView.getPlacement().translationX = (offsetViewBounds.left + UIButton.this.getHeight() / 2.0f) * ratio;
mTooltipView.getPlacement().translationY = offsetViewBounds.top * ratio;
}
mTooltipView.setCurvedMode(false);
mTooltipView.show(UIWidget.CLEAR_FOCUS);
}
};
private Runnable mHideTooltipRunnable = new Runnable() {
@Override
public void run() {
if (mTooltipView != null) {
mTooltipView.hide(UIWidget.REMOVE_WIDGET);
}
}
};
public void setLayoutWidth(@NonNull @Dimension float dimen) {
ViewGroup.LayoutParams params = getLayoutParams();
params.width = (int)dimen;
setLayoutParams(params);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
setHovered(false);
}
}