-
Notifications
You must be signed in to change notification settings - Fork 62
/
YouTubeActivity.java
352 lines (296 loc) · 11.8 KB
/
YouTubeActivity.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
/*
* Apache License
* Version 2.0, January 2004
* http://www.apache.org/licenses/LICENSE-2.0
*
* TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
*
* Copyright (c) 2018 Flipkart Internet Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.flipkart.youtubeview.activity;
import android.graphics.PorterDuff;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import com.flipkart.youtubeview.R;
import com.flipkart.youtubeview.listener.YouTubeEventListener;
import com.flipkart.youtubeview.models.PlayerStateList;
import com.flipkart.youtubeview.util.$Precondition$Check;
import com.flipkart.youtubeview.util.ServiceUtil;
import com.flipkart.youtubeview.webview.YouTubePlayerWebView;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
public final class YouTubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
/*
* Pass video id as extras
*/
public static final String ARG_VIDEO_ID = "videoId";
/**
* Pass api key as extras
*/
public static final String ARG_API_KEY = "apiKey";
/**
* Pass web-url as extras
*/
public static final String ARG_WEB_URL = "webUrl";
private static final String TAG = "YoutubeActivity";
@PlayerStateList.PlayerState
private String playerState = PlayerStateList.NONE;
@Nullable
private YouTubePlayer youTubePlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullscreen();
setContentView(R.layout.youtube_player_view);
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player);
String videoId = getVideoId();
String apiKey = getApiKey();
/*
* In case videoId or apiKey is null, throw IllegalStateException as apiKey and videoId is mandatory to run
* youtube activity.
*/
$Precondition$Check.checkArgument(!TextUtils.isEmpty(videoId), " videoId cannot be null");
$Precondition$Check.checkArgument(!TextUtils.isEmpty(apiKey), " apiKey cannot be null");
/*
* In case of YouTube Service not available, fallback to WebView implementation.
*/
if (ServiceUtil.isYouTubeServiceAvailable(this)) {
youTubePlayerView.initialize(apiKey, this);
} else {
String webViewUrl = getWebUrl();
if (!TextUtils.isEmpty(webViewUrl)) {
youTubePlayerView.setVisibility(GONE);
handleWebViewPlayer(videoId, webViewUrl);
} else {
Log.d(TAG, "Web Url is Null");
finish();
}
}
}
private void setFullscreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
@Nullable
private String getVideoId() {
Bundle extras = null != getIntent() ? getIntent().getExtras() : null;
return null != extras ? extras.getString(ARG_VIDEO_ID) : null;
}
@Nullable
private String getApiKey() {
Bundle extras = null != getIntent() ? getIntent().getExtras() : null;
return null != extras ? extras.getString(ARG_API_KEY) : null;
}
@Nullable
private String getWebUrl() {
Bundle extras = null != getIntent() ? getIntent().getExtras() : null;
return null != extras ? extras.getString(ARG_WEB_URL) : null;
}
private void handleWebViewPlayer(String videoId, String webViewUrl) {
//initialize youtube player webview
YouTubePlayerWebView youTubePlayerWebView = new YouTubePlayerWebView(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
youTubePlayerWebView.setLayoutParams(layoutParams);
//initialize progressbar and attach it to the view.
ProgressBar progressBar = initializeProgressBar();
handleProgressBar(progressBar, true);
FrameLayout.LayoutParams progressBarParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
progressBarParams.gravity = Gravity.CENTER;
youTubePlayerWebView.setBackgroundColor(getResources().getColor(R.color.black));
youTubePlayerWebView.initialize(webViewUrl);
YouTubeEventListener youTubeEventListener = getYoutubeEventListener(youTubePlayerWebView, progressBar, videoId);
youTubePlayerWebView.setYouTubeListener(youTubeEventListener);
addContentView(youTubePlayerWebView, layoutParams);
addContentView(progressBar, progressBarParams);
}
private ProgressBar initializeProgressBar() {
ProgressBar progressBar = new ProgressBar(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
int color;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
color = getResources().getColor(R.color.default_progress_bar_color, null);
} else {
color = getResources().getColor(R.color.default_progress_bar_color);
}
progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
return progressBar;
}
private void handleProgressBar(@NonNull ProgressBar progressBar, boolean show) {
progressBar.setVisibility(show ? VISIBLE : GONE);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player, boolean restored) {
youTubePlayer = player;
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.setShowFullscreenButton(true);
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
youTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
@Override
public void onPlaying() {
if (youTubePlayer != null && !PlayerStateList.PLAYING.equals(playerState)) {
playerState = PlayerStateList.PLAYING;
}
}
@Override
public void onPaused() {
handleOnPauseEvent();
}
@Override
public void onStopped() {
handleStopEvent();
}
@Override
public void onBuffering(boolean isBuffering) {
//intentionally left blank
}
@Override
public void onSeekTo(int newPositionMillis) {
handleOnPauseEvent();
}
});
youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {
//intentionally left blank
}
@Override
public void onLoaded(String s) {
//intentionally left blank
}
@Override
public void onAdStarted() {
//intentionally left blank
}
@Override
public void onVideoStarted() {
//intentionally left blank
}
@Override
public void onVideoEnded() {
handleStopEvent();
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
//intentionally left blank
}
});
player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
@Override
public void onFullscreen(boolean b) {
handleStopEvent();
}
});
if (!restored) {
youTubePlayer.loadVideo(getVideoId());
}
}
private void handleOnPauseEvent() {
if (youTubePlayer != null && (PlayerStateList.PLAYING.equals(playerState))
|| PlayerStateList.BUFFERING.equals(playerState)) {
playerState = PlayerStateList.PAUSED;
}
}
private void handleStopEvent() {
if (youTubePlayer != null && (PlayerStateList.PLAYING.equals(playerState))
|| PlayerStateList.BUFFERING.equals(playerState) || PlayerStateList.PAUSED.equals(playerState)) {
playerState = PlayerStateList.STOPPED;
}
}
private YouTubeEventListener getYoutubeEventListener(@NonNull final YouTubePlayerWebView youTubePlayerWebView,
@NonNull final ProgressBar progressBar,
@NonNull final String videoId) {
return new YouTubeEventListener() {
@Override
@MainThread
public void onReady() {
youTubePlayerWebView.loadVideo(videoId);
handleProgressBar(progressBar, false);
}
@Override
public void onCued() {
//intentionally left blank
}
@Override
@MainThread
public void onPlay(int currentTime) {
handleProgressBar(progressBar, false);
}
@Override
@MainThread
public void onPause(int currentTime) {
//intentionally left blank
}
@Override
@MainThread
public void onStop(int currentTime, int totalDuration) {
//intentionally left blank
}
@Override
@MainThread
public void onBuffering(int currentTime, boolean isBuffering) {
//intentionally left blank
}
@Override
@MainThread
public void onSeekTo(int currentTime, int newPositionMillis) {
//intentionally left blank
}
@Override
@MainThread
public void onInitializationFailure(String error) {
//intentionally left blank
}
@Override
@MainThread
public void onNativeNotSupported() {
//intentionally left blank
}
};
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
this.youTubePlayer = null;
}
@Override
public void onStop() {
super.onStop();
handleStopEvent();
}
@Override
public void onBackPressed() {
finish();
}
@Override
public void onDestroy() {
super.onDestroy();
if (youTubePlayer != null) {
youTubePlayer.release();
}
}
}