2017-09-03 08:04:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Mauricio Colli <mauriciocolli@outlook.com>
|
|
|
|
* MainVideoPlayer.java is part of NewPipe
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-22 20:28:37 +01:00
|
|
|
package org.schabi.newpipe.player;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-03-27 15:12:22 +02:00
|
|
|
import android.content.pm.ActivityInfo;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.os.Build;
|
2016-02-22 20:28:37 +01:00
|
|
|
import android.os.Bundle;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-02-22 20:28:37 +01:00
|
|
|
import android.util.Log;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.view.GestureDetector;
|
|
|
|
import android.view.MotionEvent;
|
2016-02-22 20:28:37 +01:00
|
|
|
import android.view.View;
|
2017-03-09 05:44:00 +01:00
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.widget.ImageButton;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.widget.PopupMenu;
|
2017-03-09 05:44:00 +01:00
|
|
|
import android.widget.SeekBar;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-09-05 00:38:58 +02:00
|
|
|
import com.google.android.exoplayer2.Player;
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
import org.schabi.newpipe.R;
|
2017-09-04 19:23:56 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
2017-05-08 15:33:26 +02:00
|
|
|
import org.schabi.newpipe.util.AnimationUtils;
|
2017-04-12 08:07:15 +02:00
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
2017-03-27 06:08:16 +02:00
|
|
|
import org.schabi.newpipe.util.PermissionHelper;
|
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
|
|
|
|
2017-05-08 15:33:26 +02:00
|
|
|
import static org.schabi.newpipe.util.AnimationUtils.animateView;
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/**
|
2017-04-17 06:19:53 +02:00
|
|
|
* Activity Player implementing VideoPlayer
|
2017-03-27 06:08:16 +02:00
|
|
|
*
|
|
|
|
* @author mauriciocolli
|
|
|
|
*/
|
2017-04-17 06:19:53 +02:00
|
|
|
public class MainVideoPlayer extends Activity {
|
|
|
|
private static final String TAG = ".MainVideoPlayer";
|
|
|
|
private static final boolean DEBUG = BasePlayer.DEBUG;
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
private AudioManager audioManager;
|
|
|
|
private GestureDetector gestureDetector;
|
|
|
|
|
|
|
|
private boolean activityPaused;
|
2017-04-17 06:19:53 +02:00
|
|
|
private VideoPlayerImpl playerImpl;
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Activity LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
2017-03-09 05:44:00 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2017-03-27 06:08:16 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
|
2017-04-26 21:32:20 +02:00
|
|
|
ThemeHelper.setTheme(this);
|
2017-03-27 06:08:16 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK);
|
|
|
|
setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
|
|
|
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
|
|
|
|
if (getIntent() == null) {
|
|
|
|
Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 05:44:00 +01:00
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
showSystemUi();
|
2017-04-26 21:32:20 +02:00
|
|
|
setContentView(R.layout.activity_main_player);
|
2017-04-17 06:19:53 +02:00
|
|
|
playerImpl = new VideoPlayerImpl();
|
2017-03-27 06:08:16 +02:00
|
|
|
playerImpl.setup(findViewById(android.R.id.content));
|
|
|
|
playerImpl.handleIntent(getIntent());
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onNewIntent() called with: intent = [" + intent + "]");
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
playerImpl.handleIntent(intent);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onBackPressed() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onBackPressed() called");
|
|
|
|
super.onBackPressed();
|
|
|
|
if (playerImpl.isPlaying()) playerImpl.getPlayer().setPlayWhenReady(false);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
protected void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
if (DEBUG) Log.d(TAG, "onStop() called");
|
|
|
|
activityPaused = true;
|
2017-04-17 06:19:53 +02:00
|
|
|
if (playerImpl.getPlayer() != null) {
|
|
|
|
playerImpl.setVideoStartPos((int) playerImpl.getPlayer().getCurrentPosition());
|
|
|
|
playerImpl.destroyPlayer();
|
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-09 05:44:00 +01:00
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2017-03-27 06:08:16 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onResume() called");
|
|
|
|
if (activityPaused) {
|
|
|
|
playerImpl.initPlayer();
|
2017-04-17 06:19:53 +02:00
|
|
|
playerImpl.getPlayPauseButton().setImageResource(R.drawable.ic_play_arrow_white);
|
2017-09-05 21:27:12 +02:00
|
|
|
playerImpl.playQueue.init();
|
|
|
|
//playerImpl.play(false);
|
2017-03-27 06:08:16 +02:00
|
|
|
activityPaused = false;
|
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-09 05:44:00 +01:00
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2017-03-27 06:08:16 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onDestroy() called");
|
|
|
|
if (playerImpl != null) playerImpl.destroy();
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
private void showSystemUi() {
|
2017-03-09 08:42:40 +01:00
|
|
|
if (DEBUG) Log.d(TAG, "showSystemUi() called");
|
2017-03-27 06:08:16 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
|
|
|
getWindow().getDecorView().setSystemUiVisibility(
|
|
|
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
|
|
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
|
);
|
|
|
|
} else getWindow().getDecorView().setSystemUiVisibility(0);
|
2017-03-09 05:44:00 +01:00
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
private void hideSystemUi() {
|
2017-03-09 08:42:40 +01:00
|
|
|
if (DEBUG) Log.d(TAG, "hideSystemUi() called");
|
2017-03-27 06:08:16 +02:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 16) {
|
|
|
|
int visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
2017-03-09 05:44:00 +01:00
|
|
|
| View.SYSTEM_UI_FLAG_FULLSCREEN
|
2017-03-27 06:08:16 +02:00
|
|
|
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
|
|
|
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) visibility |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
|
|
|
|
getWindow().getDecorView().setSystemUiVisibility(visibility);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
2017-03-09 05:44:00 +01:00
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 15:12:22 +02:00
|
|
|
private void toggleOrientation() {
|
|
|
|
setRequestedOrientation(getResources().getDisplayMetrics().heightPixels > getResources().getDisplayMetrics().widthPixels
|
|
|
|
? ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
|
|
|
|
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-03-09 05:44:00 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@SuppressWarnings({"unused", "WeakerAccess"})
|
2017-04-17 06:19:53 +02:00
|
|
|
private class VideoPlayerImpl extends VideoPlayer {
|
2017-03-27 06:08:16 +02:00
|
|
|
private TextView titleTextView;
|
|
|
|
private TextView channelTextView;
|
|
|
|
private TextView volumeTextView;
|
|
|
|
private TextView brightnessTextView;
|
|
|
|
private ImageButton repeatButton;
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-27 15:12:22 +02:00
|
|
|
private ImageButton screenRotationButton;
|
2017-03-27 06:08:16 +02:00
|
|
|
private ImageButton playPauseButton;
|
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
VideoPlayerImpl() {
|
|
|
|
super("VideoPlayerImpl" + MainVideoPlayer.TAG, MainVideoPlayer.this);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void initViews(View rootView) {
|
|
|
|
super.initViews(rootView);
|
2017-09-03 08:04:18 +02:00
|
|
|
this.titleTextView = rootView.findViewById(R.id.titleTextView);
|
|
|
|
this.channelTextView = rootView.findViewById(R.id.channelTextView);
|
|
|
|
this.volumeTextView = rootView.findViewById(R.id.volumeTextView);
|
|
|
|
this.brightnessTextView = rootView.findViewById(R.id.brightnessTextView);
|
|
|
|
this.repeatButton = rootView.findViewById(R.id.repeatButton);
|
2017-03-27 06:08:16 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
this.screenRotationButton = rootView.findViewById(R.id.screenRotationButton);
|
|
|
|
this.playPauseButton = rootView.findViewById(R.id.playPauseButton);
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
// Due to a bug on lower API, lets set the alpha instead of using a drawable
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) repeatButton.setImageAlpha(77);
|
|
|
|
else { //noinspection deprecation
|
|
|
|
repeatButton.setAlpha(77);
|
|
|
|
}
|
|
|
|
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(true);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void initListeners() {
|
|
|
|
super.initListeners();
|
|
|
|
|
|
|
|
MySimpleOnGestureListener listener = new MySimpleOnGestureListener();
|
|
|
|
gestureDetector = new GestureDetector(context, listener);
|
|
|
|
gestureDetector.setIsLongpressEnabled(false);
|
|
|
|
playerImpl.getRootView().setOnTouchListener(listener);
|
|
|
|
|
|
|
|
repeatButton.setOnClickListener(this);
|
|
|
|
playPauseButton.setOnClickListener(this);
|
2017-03-27 15:12:22 +02:00
|
|
|
screenRotationButton.setOnClickListener(this);
|
2017-03-09 05:44:00 +01:00
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void handleIntent(Intent intent) {
|
|
|
|
super.handleIntent(intent);
|
|
|
|
titleTextView.setText(getVideoTitle());
|
2017-09-03 08:04:18 +02:00
|
|
|
channelTextView.setText(getUploaderName());
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 21:27:12 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Playback Listener
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-09-02 20:06:36 +02:00
|
|
|
@Override
|
2017-09-05 21:27:12 +02:00
|
|
|
public void shutdown() {
|
|
|
|
super.shutdown();
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sync(final StreamInfo info, final int sortedStreamsIndex) {
|
|
|
|
super.sync(info, sortedStreamsIndex);
|
2017-09-02 20:06:36 +02:00
|
|
|
titleTextView.setText(getVideoTitle());
|
2017-09-04 19:23:56 +02:00
|
|
|
channelTextView.setText(getUploaderName());
|
2017-09-03 04:30:34 +02:00
|
|
|
|
|
|
|
playPauseButton.setImageResource(R.drawable.ic_pause_white);
|
2017-09-02 20:06:36 +02:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onFullScreenButtonClicked() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onFullScreenButtonClicked() called");
|
|
|
|
if (playerImpl.getPlayer() == null) return;
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
2017-04-17 06:19:53 +02:00
|
|
|
&& !PermissionHelper.checkSystemAlertWindowPermission(MainVideoPlayer.this)) {
|
|
|
|
Toast.makeText(MainVideoPlayer.this, R.string.msg_popup_permission, Toast.LENGTH_LONG).show();
|
2017-03-27 06:08:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
context.startService(NavigationHelper.getOpenVideoPlayerIntent(context, PopupVideoPlayer.class, playerImpl));
|
|
|
|
if (playerImpl != null) playerImpl.destroyPlayer();
|
2017-04-12 08:07:15 +02:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
((View) getControlAnimationView().getParent()).setVisibility(View.GONE);
|
2017-04-17 06:19:53 +02:00
|
|
|
MainVideoPlayer.this.finish();
|
2017-03-09 05:44:00 +01:00
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public void onRepeatClicked() {
|
|
|
|
super.onRepeatClicked();
|
|
|
|
if (DEBUG) Log.d(TAG, "onRepeatClicked() called");
|
2017-09-05 00:38:58 +02:00
|
|
|
switch (simpleExoPlayer.getRepeatMode()) {
|
|
|
|
case Player.REPEAT_MODE_OFF:
|
2017-03-27 06:08:16 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) repeatButton.setImageAlpha(77);
|
|
|
|
else repeatButton.setAlpha(77);
|
|
|
|
|
|
|
|
break;
|
2017-09-05 00:38:58 +02:00
|
|
|
case Player.REPEAT_MODE_ONE:
|
|
|
|
// todo change image
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) repeatButton.setImageAlpha(168);
|
|
|
|
else repeatButton.setAlpha(168);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case Player.REPEAT_MODE_ALL:
|
2017-03-27 06:08:16 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) repeatButton.setImageAlpha(255);
|
|
|
|
else repeatButton.setAlpha(255);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
super.onClick(v);
|
|
|
|
if (v.getId() == repeatButton.getId()) onRepeatClicked();
|
|
|
|
else if (v.getId() == playPauseButton.getId()) onVideoPlayPause();
|
2017-03-27 15:12:22 +02:00
|
|
|
else if (v.getId() == screenRotationButton.getId()) onScreenRotationClicked();
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
if (getCurrentState() != STATE_COMPLETED) {
|
2017-05-08 15:33:26 +02:00
|
|
|
getControlsVisibilityHandler().removeCallbacksAndMessages(null);
|
2017-03-27 06:08:16 +02:00
|
|
|
animateView(playerImpl.getControlsRoot(), true, 300, 0, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-07-10 03:38:47 +02:00
|
|
|
if (getCurrentState() == STATE_PLAYING && !playerImpl.isSomePopupMenuVisible()) {
|
2017-05-08 15:33:26 +02:00
|
|
|
hideControls(300, DEFAULT_CONTROLS_HIDE_TIME);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-08 15:33:26 +02:00
|
|
|
});
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-22 20:28:37 +01:00
|
|
|
|
2017-03-27 15:12:22 +02:00
|
|
|
private void onScreenRotationClicked() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onScreenRotationClicked() called");
|
|
|
|
toggleOrientation();
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
|
|
|
super.onStopTrackingTouch(seekBar);
|
|
|
|
if (playerImpl.wasPlaying()) {
|
2017-05-08 15:33:26 +02:00
|
|
|
hideControls(100, 0);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDismiss(PopupMenu menu) {
|
|
|
|
super.onDismiss(menu);
|
2017-05-08 15:33:26 +02:00
|
|
|
if (isPlaying()) hideControls(300, 0);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-04-17 06:19:53 +02:00
|
|
|
public void onError(Exception exception) {
|
|
|
|
exception.printStackTrace();
|
2017-03-27 06:08:16 +02:00
|
|
|
Toast.makeText(context, "Failed to play this video", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// States
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoading() {
|
|
|
|
super.onLoading();
|
|
|
|
playPauseButton.setImageResource(R.drawable.ic_pause_white);
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 100);
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(true);
|
2017-04-09 19:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBuffering() {
|
|
|
|
super.onBuffering();
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 100);
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(true);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
@Override
|
|
|
|
public void onPlaying() {
|
|
|
|
super.onPlaying();
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 80, 0, new Runnable() {
|
2017-04-17 06:19:53 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
playPauseButton.setImageResource(R.drawable.ic_pause_white);
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, true, 200);
|
2017-04-17 06:19:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
showSystemUi();
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(true);
|
2017-04-17 06:19:53 +02:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onPaused() {
|
|
|
|
super.onPaused();
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 80, 0, new Runnable() {
|
2017-04-17 06:19:53 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
playPauseButton.setImageResource(R.drawable.ic_play_arrow_white);
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, true, 200);
|
2017-04-17 06:19:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
showSystemUi();
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(false);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onPausedSeek() {
|
|
|
|
super.onPausedSeek();
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 100);
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(true);
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onCompleted() {
|
2017-09-05 00:38:58 +02:00
|
|
|
showSystemUi();
|
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 0, 0, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
playPauseButton.setImageResource(R.drawable.ic_replay_white);
|
|
|
|
animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, true, 300);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-14 05:32:28 +02:00
|
|
|
getRootView().setKeepScreenOn(false);
|
2017-03-27 06:08:16 +02:00
|
|
|
super.onCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2017-05-08 15:33:26 +02:00
|
|
|
public void hideControls(final long duration, long delay) {
|
|
|
|
if (DEBUG) Log.d(TAG, "hideControls() called with: delay = [" + delay + "]");
|
|
|
|
getControlsVisibilityHandler().removeCallbacksAndMessages(null);
|
|
|
|
getControlsVisibilityHandler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
animateView(getControlsRoot(), false, duration, 0, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
hideSystemUi();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, delay);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Getters
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public TextView getTitleTextView() {
|
|
|
|
return titleTextView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextView getChannelTextView() {
|
|
|
|
return channelTextView;
|
2016-02-22 20:28:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
public TextView getVolumeTextView() {
|
|
|
|
return volumeTextView;
|
2017-03-09 05:44:00 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
public TextView getBrightnessTextView() {
|
|
|
|
return brightnessTextView;
|
2017-03-09 05:44:00 +01:00
|
|
|
}
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
public ImageButton getRepeatButton() {
|
|
|
|
return repeatButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageButton getPlayPauseButton() {
|
|
|
|
return playPauseButton;
|
|
|
|
}
|
2017-08-18 20:07:57 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRepeatModeChanged(int i) {
|
|
|
|
|
|
|
|
}
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener {
|
|
|
|
private boolean isMoving;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onDoubleTap(MotionEvent e) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onDoubleTap() called with: e = [" + e + "]" + "rawXy = " + e.getRawX() + ", " + e.getRawY() + ", xy = " + e.getX() + ", " + e.getY());
|
2017-09-01 21:10:36 +02:00
|
|
|
//if (!playerImpl.isPlaying()) return false;
|
2017-09-06 08:49:00 +02:00
|
|
|
if (!playerImpl.isPlayerReady()) return false;
|
2017-09-01 21:10:36 +02:00
|
|
|
|
|
|
|
if (e.getX() > playerImpl.getRootView().getWidth() / 2)
|
2017-09-06 08:49:00 +02:00
|
|
|
playerImpl.playQueue.offsetIndex(+1);
|
2017-09-01 21:10:36 +02:00
|
|
|
//playerImpl.onFastForward();
|
|
|
|
else
|
2017-09-06 08:49:00 +02:00
|
|
|
playerImpl.playQueue.offsetIndex(-1);
|
2017-09-01 21:10:36 +02:00
|
|
|
//playerImpl.onFastRewind();
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onSingleTapConfirmed(MotionEvent e) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onSingleTapConfirmed() called with: e = [" + e + "]");
|
2017-04-17 06:19:53 +02:00
|
|
|
if (playerImpl.getCurrentState() != BasePlayer.STATE_PLAYING) return true;
|
2017-03-27 06:08:16 +02:00
|
|
|
|
2017-05-08 15:33:26 +02:00
|
|
|
if (playerImpl.isControlsVisible()) playerImpl.hideControls(150, 0);
|
2017-03-27 06:08:16 +02:00
|
|
|
else {
|
2017-05-08 15:33:26 +02:00
|
|
|
playerImpl.showControlsThenHide();
|
2017-03-27 06:08:16 +02:00
|
|
|
showSystemUi();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:45:38 +02:00
|
|
|
private final boolean isGestureControlsEnabled = playerImpl.getSharedPreferences().getBoolean(getString(R.string.player_gesture_controls_key), true);
|
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
private final float stepsBrightness = 15, stepBrightness = (1f / stepsBrightness), minBrightness = .01f;
|
2017-03-27 06:08:16 +02:00
|
|
|
private float currentBrightness = .5f;
|
|
|
|
|
|
|
|
private int currentVolume, maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
|
2017-04-17 06:19:53 +02:00
|
|
|
private final float stepsVolume = 15, stepVolume = (float) Math.ceil(maxVolume / stepsVolume), minVolume = 0;
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
private final String brightnessUnicode = new String(Character.toChars(0x2600));
|
|
|
|
private final String volumeUnicode = new String(Character.toChars(0x1F508));
|
|
|
|
|
|
|
|
private final int MOVEMENT_THRESHOLD = 40;
|
2017-04-17 06:19:53 +02:00
|
|
|
private final int eventsThreshold = 8;
|
2017-03-27 06:08:16 +02:00
|
|
|
private boolean triggered = false;
|
|
|
|
private int eventsNum;
|
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
// TODO: Improve video gesture controls
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
2017-05-17 02:45:38 +02:00
|
|
|
if (!isGestureControlsEnabled) return false;
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
//noinspection PointlessBooleanExpression
|
2017-04-26 21:32:20 +02:00
|
|
|
if (DEBUG && false) Log.d(TAG, "MainVideoPlayer.onScroll = " +
|
2017-03-27 06:08:16 +02:00
|
|
|
", e1.getRaw = [" + e1.getRawX() + ", " + e1.getRawY() + "]" +
|
|
|
|
", e2.getRaw = [" + e2.getRawX() + ", " + e2.getRawY() + "]" +
|
|
|
|
", distanceXy = [" + distanceX + ", " + distanceY + "]");
|
|
|
|
float abs = Math.abs(e2.getY() - e1.getY());
|
|
|
|
if (!triggered) {
|
|
|
|
triggered = abs > MOVEMENT_THRESHOLD;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
if (eventsNum++ % eventsThreshold != 0 || playerImpl.getCurrentState() == BasePlayer.STATE_COMPLETED) return false;
|
2017-03-27 06:08:16 +02:00
|
|
|
isMoving = true;
|
|
|
|
// boolean up = !((e2.getY() - e1.getY()) > 0) && distanceY > 0; // Android's origin point is on top
|
|
|
|
boolean up = distanceY > 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (e1.getX() > playerImpl.getRootView().getWidth() / 2) {
|
2017-04-17 06:19:53 +02:00
|
|
|
double floor = Math.floor(up ? stepVolume : -stepVolume);
|
|
|
|
currentVolume = (int) (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + floor);
|
2017-03-27 06:08:16 +02:00
|
|
|
if (currentVolume >= maxVolume) currentVolume = maxVolume;
|
2017-04-17 06:19:53 +02:00
|
|
|
if (currentVolume <= minVolume) currentVolume = (int) minVolume;
|
2017-03-27 06:08:16 +02:00
|
|
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
|
|
|
|
|
|
|
|
if (DEBUG) Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume);
|
|
|
|
playerImpl.getVolumeTextView().setText(volumeUnicode + " " + Math.round((((float) currentVolume) / maxVolume) * 100) + "%");
|
|
|
|
|
2017-05-08 15:33:26 +02:00
|
|
|
if (playerImpl.getVolumeTextView().getVisibility() != View.VISIBLE) animateView(playerImpl.getVolumeTextView(), true, 200);
|
2017-03-27 06:08:16 +02:00
|
|
|
if (playerImpl.getBrightnessTextView().getVisibility() == View.VISIBLE) playerImpl.getBrightnessTextView().setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
WindowManager.LayoutParams lp = getWindow().getAttributes();
|
|
|
|
currentBrightness += up ? stepBrightness : -stepBrightness;
|
|
|
|
if (currentBrightness >= 1f) currentBrightness = 1f;
|
|
|
|
if (currentBrightness <= minBrightness) currentBrightness = minBrightness;
|
|
|
|
|
|
|
|
lp.screenBrightness = currentBrightness;
|
|
|
|
getWindow().setAttributes(lp);
|
|
|
|
if (DEBUG) Log.d(TAG, "onScroll().brightnessControl, currentBrightness = " + currentBrightness);
|
|
|
|
int brightnessNormalized = Math.round(currentBrightness * 100);
|
|
|
|
|
|
|
|
playerImpl.getBrightnessTextView().setText(brightnessUnicode + " " + (brightnessNormalized == 1 ? 0 : brightnessNormalized) + "%");
|
|
|
|
|
2017-05-08 15:33:26 +02:00
|
|
|
if (playerImpl.getBrightnessTextView().getVisibility() != View.VISIBLE) animateView(playerImpl.getBrightnessTextView(), true, 200);
|
2017-03-27 06:08:16 +02:00
|
|
|
if (playerImpl.getVolumeTextView().getVisibility() == View.VISIBLE) playerImpl.getVolumeTextView().setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onScrollEnd() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onScrollEnd() called");
|
|
|
|
triggered = false;
|
|
|
|
eventsNum = 0;
|
|
|
|
/* if (playerImpl.getVolumeTextView().getVisibility() == View.VISIBLE) playerImpl.getVolumeTextView().setVisibility(View.GONE);
|
|
|
|
if (playerImpl.getBrightnessTextView().getVisibility() == View.VISIBLE) playerImpl.getBrightnessTextView().setVisibility(View.GONE);*/
|
2017-05-08 15:33:26 +02:00
|
|
|
if (playerImpl.getVolumeTextView().getVisibility() == View.VISIBLE) animateView(playerImpl.getVolumeTextView(), false, 200, 200);
|
|
|
|
if (playerImpl.getBrightnessTextView().getVisibility() == View.VISIBLE) animateView(playerImpl.getBrightnessTextView(), false, 200, 200);
|
2017-03-27 06:08:16 +02:00
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
if (playerImpl.isControlsVisible() && playerImpl.getCurrentState() == BasePlayer.STATE_PLAYING) {
|
2017-05-08 15:33:26 +02:00
|
|
|
playerImpl.hideControls(300, VideoPlayer.DEFAULT_CONTROLS_HIDE_TIME);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
2017-04-26 21:32:20 +02:00
|
|
|
//noinspection PointlessBooleanExpression
|
|
|
|
if (DEBUG && false) Log.d(TAG, "onTouch() called with: v = [" + v + "], event = [" + event + "]");
|
2017-03-27 06:08:16 +02:00
|
|
|
gestureDetector.onTouchEvent(event);
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_UP && isMoving) {
|
|
|
|
isMoving = false;
|
|
|
|
onScrollEnd();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-09 05:44:00 +01:00
|
|
|
}
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|