forked from Tubitv/TubiPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleViewTubiPlayerActivity.java
389 lines (324 loc) · 13.8 KB
/
DoubleViewTubiPlayerActivity.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.tubitv.media.activities;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;
import android.webkit.WebView;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.tubitv.media.bindings.UserController;
import com.tubitv.media.controller.PlayerAdLogicController;
import com.tubitv.media.controller.PlayerUIController;
import com.tubitv.media.di.PlayerModuleDefault;
import com.tubitv.media.di.component.DaggerFsmComonent;
import com.tubitv.media.fsm.Input;
import com.tubitv.media.fsm.callback.AdInterface;
import com.tubitv.media.fsm.concrete.AdPlayingState;
import com.tubitv.media.fsm.concrete.VpaidState;
import com.tubitv.media.fsm.listener.AdPlayingMonitor;
import com.tubitv.media.fsm.listener.CuePointMonitor;
import com.tubitv.media.fsm.state_machine.FsmPlayer;
import com.tubitv.media.helpers.Constants;
import com.tubitv.media.interfaces.AutoPlay;
import com.tubitv.media.interfaces.DoublePlayerInterface;
import com.tubitv.media.models.AdMediaModel;
import com.tubitv.media.models.AdRetriever;
import com.tubitv.media.models.CuePointsRetriever;
import com.tubitv.media.models.MediaModel;
import com.tubitv.media.models.VpaidClient;
import com.tubitv.media.utilities.ExoPlayerLogger;
import com.tubitv.media.utilities.PlayerDeviceUtils;
import com.tubitv.media.utilities.Utils;
import com.tubitv.media.views.UIControllerView;
import javax.inject.Inject;
/**
* Created by allensun on 7/24/17.
*/
public class DoubleViewTubiPlayerActivity extends TubiPlayerActivity implements DoublePlayerInterface, AutoPlay {
private static final String TAG = "DoubleViewTubiPlayerAct";
private static final DefaultBandwidthMeter BANDWIDTH_METER_AD = new DefaultBandwidthMeter();
protected SimpleExoPlayer adPlayer;
@Inject
FsmPlayer fsmPlayer;
@Inject
PlayerUIController playerUIController;
@Inject
AdPlayingMonitor adPlayingMonitor;
@Inject
CuePointMonitor cuePointMonitor;
@Inject
AdRetriever adRetriever;
@Inject
CuePointsRetriever cuePointsRetriever;
@Inject
AdInterface adInterface;
@Inject
PlayerAdLogicController playerComponentController;
@Inject
VpaidClient vpaidClient;
private DefaultTrackSelector trackSelector_ad;
protected AdRetriever getAdRetriever() {
return adRetriever;
}
protected CuePointsRetriever getCuePointsRetriever() {
return cuePointsRetriever;
}
protected PlayerUIController getPlayerUIController() {
return playerUIController;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
injectDependency();
dependencyPrepare();
}
@Override
public View addUserInteractionView() {
return new UIControllerView(getBaseContext())
.setUserController((UserController) getPlayerController());
}
/**
* this should be the first method the subclass of {@link DoubleViewTubiPlayerActivity} call, before accessing all the different variables.
*/
protected void injectDependency() {
//FSMModuleTesting requirement object such as ExoPlayer haven't been initialized yet
DaggerFsmComonent.builder().playerModuleDefault(new PlayerModuleDefault()).build()
.inject(this);
}
/**
* this method is called immediately after {@link DoubleViewTubiPlayerActivity#injectDependency()} for all injected dependencies preparation.
*/
protected void dependencyPrepare() {
}
protected FsmPlayer getFsmPlayer() {
return fsmPlayer;
}
@Override
protected void initMoviePlayer() {
super.initMoviePlayer();
createMediaSource(mediaModel);
if (!PlayerDeviceUtils.useSinglePlayer()) {
setupAdPlayer();
}
}
@Override
protected void onPlayerReady() {
prepareFSM();
}
@Override
protected void releaseMoviePlayer() {
super.releaseMoviePlayer();
if (!PlayerDeviceUtils.useSinglePlayer()) {
releaseAdPlayer();
}
}
private void setupAdPlayer() {
TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER_AD);
trackSelector_ad = new DefaultTrackSelector(adaptiveTrackSelectionFactory);
adPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector_ad);
}
private void releaseAdPlayer() {
if (adPlayer != null) {
updateAdResumePosition();
adPlayer.release();
adPlayer = null;
trackSelector_ad = null;
}
if (vpaidWebView != null) {
vpaidWebView.loadUrl(VpaidClient.EMPTY_URL);
vpaidWebView.clearHistory();
}
}
private void updateAdResumePosition() {
if (adPlayer != null && playerUIController != null) {
int adResumeWindow = adPlayer.getCurrentWindowIndex();
long adResumePosition = adPlayer.isCurrentWindowSeekable() ? Math.max(0, adPlayer.getCurrentPosition())
: C.TIME_UNSET;
playerUIController.setAdResumeInfo(adResumeWindow, adResumePosition);
}
}
/**
* update the movie and ad playing position when players are released
*/
@Override
protected void updateResumePosition() {
//keep track of movie player's position when activity resume back
if (mMoviePlayer != null && playerUIController != null
&& mMoviePlayer.getPlaybackState() != ExoPlayer.STATE_IDLE) {
int resumeWindow = mMoviePlayer.getCurrentWindowIndex();
long resumePosition = mMoviePlayer.isCurrentWindowSeekable() ?
Math.max(0, mMoviePlayer.getCurrentPosition())
:
C.TIME_UNSET;
playerUIController.setMovieResumeInfo(resumeWindow, resumePosition);
ExoPlayerLogger.i(Constants.FSMPLAYER_TESTING, resumePosition + "");
}
//keep track of ad player's position when activity resume back, only keep track when current state is in AdPlayingState.
if (fsmPlayer.getCurrentState() instanceof AdPlayingState && adPlayer != null && playerUIController != null
&& adPlayer.getPlaybackState() != ExoPlayer.STATE_IDLE) {
int ad_resumeWindow = adPlayer.getCurrentWindowIndex();
long ad_resumePosition = adPlayer.isCurrentWindowSeekable() ? Math.max(0, adPlayer.getCurrentPosition())
: C.TIME_UNSET;
playerUIController.setAdResumeInfo(ad_resumeWindow, ad_resumePosition);
}
}
@Override
protected boolean isCaptionPreferenceEnable() {
return true;
}
/**
* prepare / set up FSM and inject all the elements into the FSM
*/
@Override
public void prepareFSM() {
//update the playerUIController view, need to update the view everything when two ExoPlayer being recreated in activity lifecycle.
playerUIController.setContentPlayer(mMoviePlayer);
if (!PlayerDeviceUtils.useSinglePlayer()) {
playerUIController.setAdPlayer(adPlayer);
}
playerUIController.setExoPlayerView(mTubiPlayerView);
playerUIController.setVpaidWebView(vpaidWebView);
//update the MediaModel
fsmPlayer.setController(playerUIController);
fsmPlayer.setMovieMedia(mediaModel);
fsmPlayer.setAdRetriever(adRetriever);
fsmPlayer.setCuePointsRetriever(cuePointsRetriever);
fsmPlayer.setAdServerInterface(adInterface);
//set the PlayerComponentController.
playerComponentController.setAdPlayingMonitor(adPlayingMonitor);
playerComponentController.setTubiPlaybackInterface(this);
playerComponentController.setDoublePlayerInterface(this);
playerComponentController.setCuePointMonitor(cuePointMonitor);
playerComponentController.setVpaidClient(vpaidClient);
fsmPlayer.setPlayerComponentController(playerComponentController);
fsmPlayer.setLifecycle(getLifecycle());
if (fsmPlayer.isInitialized()) {
fsmPlayer.updateSelf();
Utils.hideSystemUI(this, true);
} else {
fsmPlayer.transit(Input.INITIALIZE);
}
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (playerUIController != null) {
playerUIController.clearMovieResumeInfo();
}
}
@Override
public void onBackPressed() {
if (fsmPlayer != null && fsmPlayer.getCurrentState() instanceof VpaidState && vpaidWebView != null
&& vpaidWebView.canGoBack()) {
//if the last page is empty url, then, it should
if (ingoreWebViewBackNavigation(vpaidWebView)) {
super.onBackPressed();
return;
}
vpaidWebView.goBack();
} else {
super.onBackPressed();
}
}
//when the last item is "about:blank", ingore the back navigation for webview.
private boolean ingoreWebViewBackNavigation(WebView vpaidWebView) {
if (vpaidWebView != null) {
WebBackForwardList mWebBackForwardList = vpaidWebView.copyBackForwardList();
if (mWebBackForwardList == null) {
return false;
}
WebHistoryItem historyItem = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex() - 1);
if (historyItem == null) {
return false;
}
String historyUrl = historyItem.getUrl();
if (historyUrl != null && historyUrl.equalsIgnoreCase(VpaidClient.EMPTY_URL)) {
return true;
}
}
return false;
}
/**
* creating the {@link MediaSource} for the Exoplayer, recreate it everytime when new {@link SimpleExoPlayer} has been initialized
*
* @return
*/
protected void createMediaSource(MediaModel videoMediaModel) {
videoMediaModel.setMediaSource(buildMediaSource(videoMediaModel));
}
@Override
public void onPrepareAds(@Nullable AdMediaModel adMediaModel) {
for (MediaModel singleMedia : adMediaModel.getListOfAds()) {
MediaSource adMediaSource = buildMediaSource(singleMedia);
singleMedia.setMediaSource(adMediaSource);
}
}
@Override
public void onProgress(@Nullable MediaModel mediaModel, long milliseconds, long durationMillis) {
// ExoPlayerLogger.v(TAG, mediaModel.getMediaName() + ": " + mediaModel.toString() + " onProgress: " + "milliseconds: " + milliseconds + " durationMillis: " + durationMillis);
// monitor the movie progress.
cuePointMonitor.onMovieProgress(milliseconds, durationMillis);
}
@Override
public void onSeek(@Nullable MediaModel mediaModel, long oldPositionMillis, long newPositionMillis) {
// ExoPlayerLogger.v(TAG, mediaModel.getMediaName() + ": " + mediaModel.toString() + " onSeek : " + "oldPositionMillis: " + oldPositionMillis + " newPositionMillis: " + newPositionMillis);
}
@Override
public void onPlayToggle(@Nullable MediaModel mediaModel, boolean playing) {
// ExoPlayerLogger.v(TAG, mediaModel.getMediaName() + ": " + mediaModel.toString() + " onPlayToggle :");
}
@Override
public void onLearnMoreClick(@NonNull MediaModel mediaModel) {
// ExoPlayerLogger.v(TAG, mediaModel.getMediaName() + ": " + mediaModel.toString() + " onLearnMoreClick :" + mediaModel.getClickThroughUrl());
if (!PlayerDeviceUtils.isTVDevice(this)
&& mediaModel != null
&& !TextUtils.isEmpty(mediaModel.getClickThroughUrl())) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mediaModel.getClickThroughUrl()));
startActivity(browserIntent);
}
}
@Override
public void onSubtitles(@Nullable MediaModel mediaModel, boolean enabled) {
// ExoPlayerLogger.v(TAG, mediaModel.getMediaName() + ": " + mediaModel.toString() + " onSubtitles :" + mediaModel.getMediaName());
}
@Override
public void onQuality(@Nullable MediaModel mediaModel) {
// ExoPlayerLogger.v(TAG, mediaModel.getMediaName() + ": " + mediaModel.toString() + " onQuality :" + mediaModel.getMediaName());
}
@Override
public void onCuePointReceived(long[] cuePoints) {
cuePointIndictor.setText(printCuePoints(cuePoints));
}
private String printCuePoints(long[] cuePoints) {
if (cuePoints == null) {
return "no cuepoints supplied";
}
StringBuilder builder = new StringBuilder();
builder.append("Adbreak will be in : ");
for (int i = 0; i < cuePoints.length; i++) {
double minutes = (double) cuePoints[i] / 1000 / 60;
double second = (minutes - Math.floor(minutes)) * 60;
builder.append((int) minutes + "min" + (int) second + "sec, ");
}
return builder.toString();
}
@Override
public void playNext(MediaModel nextVideo) {
createMediaSource(nextVideo);
fsmPlayer.setMovieMedia(nextVideo);
fsmPlayer.restart();
}
}