-
Notifications
You must be signed in to change notification settings - Fork 215
/
TitleBarWidget.java
232 lines (193 loc) · 7.53 KB
/
TitleBarWidget.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
/* -*- 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.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.URLUtil;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.databinding.DataBindingUtil;
import org.mozilla.geckoview.MediaElement;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.Media;
import org.mozilla.vrbrowser.databinding.TitleBarBinding;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class TitleBarWidget extends UIWidget implements WidgetManagerDelegate.UpdateListener {
public interface Delegate {
void onTitleClicked(@NonNull TitleBarWidget titleBar);
void onMediaPlayClicked(@NonNull TitleBarWidget titleBar);
void onMediaPauseClicked(@NonNull TitleBarWidget titleBar);
}
private TitleBarBinding mBinding;
private WindowWidget mAttachedWindow;
private boolean mVisible = false;
private Media mMedia;
private boolean mWidgetAdded = false;
public TitleBarWidget(Context aContext) {
super(aContext);
initialize(aContext);
}
public TitleBarWidget(Context aContext, AttributeSet aAttrs) {
super(aContext, aAttrs);
initialize(aContext);
}
public TitleBarWidget(Context aContext, AttributeSet aAttrs, int aDefStyle) {
super(aContext, aAttrs, aDefStyle);
initialize(aContext);
}
private void initialize(@NonNull Context aContext) {
LayoutInflater inflater = LayoutInflater.from(aContext);
mBinding = DataBindingUtil.inflate(inflater, R.layout.title_bar, this, true);
mBinding.setWidget(this);
mBinding.executePendingBindings();
mWidgetManager.addUpdateListener(this);
}
public void setDelegate(Delegate delegate) {
mBinding.setDelegate(delegate);
}
public @Nullable
WindowWidget getAttachedWindow() {
return mAttachedWindow;
}
@Override
public void releaseWidget() {
detachFromWindow();
mWidgetManager.removeUpdateListener(this);
mAttachedWindow = null;
super.releaseWidget();
}
@Override
protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.width = WidgetPlacement.dpDimension(getContext(), R.dimen.title_bar_width);
float ratio = WidgetPlacement.floatDimension(getContext(), R.dimen.window_world_width) /
WidgetPlacement.dpDimension(getContext(), R.dimen.navigation_bar_width);
aPlacement.worldWidth = aPlacement.width * ratio;
aPlacement.height = WidgetPlacement.dpDimension(getContext(), R.dimen.navigation_bar_height);
aPlacement.anchorX = 0.5f;
aPlacement.anchorY = 1.0f;
aPlacement.parentAnchorX = 0.5f;
aPlacement.parentAnchorY = 0.0f;
aPlacement.translationY = -35;
aPlacement.opaque = false;
aPlacement.cylinder = true;
aPlacement.visible = true;
}
@Override
public void detachFromWindow() {
mAttachedWindow = null;
}
@Override
public void attachToWindow(@NonNull WindowWidget aWindow) {
if (aWindow == mAttachedWindow) {
return;
}
detachFromWindow();
mWidgetPlacement.parentHandle = aWindow.getHandle();
mAttachedWindow = aWindow;
setPrivateMode(aWindow.getSession().isPrivateMode());
}
@Override
public void setVisible(boolean aIsVisible) {
if (mVisible == aIsVisible || mWidgetManager == null) {
return;
}
mVisible = aIsVisible;
getPlacement().visible = aIsVisible;
if (!mWidgetAdded) {
mWidgetManager.addWidget(this);
mWidgetAdded = true;
} else {
mWidgetManager.updateWidget(this);
}
}
private void setPrivateMode(boolean aPrivateMode) {
mBinding.titleBar.setBackground(getContext().getDrawable(aPrivateMode ? R.drawable.title_bar_background_private : R.drawable.title_bar_background));
mBinding.mediaButton.setPrivateMode(aPrivateMode);
}
public void setURL(@StringRes int id) {
setURL(getResources().getString(id));
}
public void setURL(String urlString) {
if (urlString == null) {
return;
}
if (URLUtil.isValidUrl(urlString)) {
try {
URI uri = URI.create(urlString);
URL url = new URL(
uri.getScheme() != null ? uri.getScheme() : "",
uri.getAuthority() != null ? uri.getAuthority() : "",
"");
mBinding.url.setText(url.toString());
} catch (MalformedURLException | IllegalArgumentException e) {
mBinding.url.setText("");
}
} else {
mBinding.url.setText(urlString);
}
}
public void setIsInsecure(boolean aIsInsecure) {
if (mAttachedWindow != null && mAttachedWindow.getSession() != null &&
mAttachedWindow.getSession().getCurrentUri() != null &&
!(mAttachedWindow.getSession().getCurrentUri().startsWith("data") &&
mAttachedWindow.getSession().isPrivateMode())) {
mBinding.insecureIcon.setVisibility(aIsInsecure ? View.VISIBLE : View.GONE);
}
}
public void setInsecureVisibility(int visibility) {
mBinding.insecureIcon.setVisibility(visibility);
}
public void mediaAvailabilityChanged(boolean available) {
if (mMedia != null) {
mMedia.removeMediaListener(mMediaDelegate);
}
if (available && mAttachedWindow != null && mAttachedWindow.getSession() != null) {
mMedia = mAttachedWindow.getSession().getFullScreenVideo();
if (mMedia != null) {
mMedia.addMediaListener(mMediaDelegate);
if (mMedia.isPlayed()) {
mBinding.setIsMediaAvailable(true);
mBinding.setIsMediaPlaying(true);
}
}
} else {
mMedia = null;
mBinding.setIsMediaAvailable(false);
}
}
public void updateMediaStatus() {
if (mMedia != null) {
mBinding.setIsMediaAvailable(mMedia.isPlayed());
mBinding.setIsMediaPlaying(mMedia.isPlaying());
}
}
MediaElement.Delegate mMediaDelegate = new MediaElement.Delegate() {
@Override
public void onPlaybackStateChange(@NonNull MediaElement mediaElement, int state) {
switch(state) {
case MediaElement.MEDIA_STATE_PLAY:
case MediaElement.MEDIA_STATE_PLAYING:
mBinding.setIsMediaAvailable(true);
mBinding.setIsMediaPlaying(true);
break;
case MediaElement.MEDIA_STATE_PAUSE:
mBinding.setIsMediaAvailable(true);
mBinding.setIsMediaPlaying(false);
}
}
};
// WidgetManagerDelegate.UpdateListener
@Override
public void onWidgetUpdate(Widget aWidget) {
if (aWidget == mWidgetManager.getFocusedWindow()) {
mWidgetManager.updateWidget(this);
}
}
}