2017-09-03 08:04:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Mauricio Colli <mauriciocolli@outlook.com>
|
|
|
|
* PopupVideoPlayer.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/>.
|
|
|
|
*/
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
package org.schabi.newpipe.player;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2017-05-01 07:23:50 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.PixelFormat;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.os.Build;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.os.IBinder;
|
2017-05-01 07:23:50 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2017-10-13 03:22:25 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.util.DisplayMetrics;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.GestureDetector;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.WindowManager;
|
2017-12-23 17:34:47 +01:00
|
|
|
import android.widget.ImageButton;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.widget.PopupMenu;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.widget.RemoteViews;
|
2017-05-08 15:33:26 +02:00
|
|
|
import android.widget.SeekBar;
|
2017-05-01 07:23:50 +02:00
|
|
|
import android.widget.TextView;
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2018-02-03 23:39:03 +01:00
|
|
|
import com.google.android.exoplayer2.C;
|
2017-10-13 03:22:25 +02:00
|
|
|
import com.google.android.exoplayer2.PlaybackParameters;
|
2017-09-05 00:38:58 +02:00
|
|
|
import com.google.android.exoplayer2.Player;
|
2018-02-07 22:11:19 +01:00
|
|
|
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
|
2018-02-09 22:26:03 +01:00
|
|
|
import com.google.android.exoplayer2.ui.SubtitleView;
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.BuildConfig;
|
|
|
|
import org.schabi.newpipe.R;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
2017-10-23 04:21:19 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
2017-10-13 03:22:25 +02:00
|
|
|
import org.schabi.newpipe.player.event.PlayerEventListener;
|
2018-01-04 07:53:31 +01:00
|
|
|
import org.schabi.newpipe.player.helper.LockManager;
|
2017-11-01 01:07:12 +01:00
|
|
|
import org.schabi.newpipe.player.helper.PlayerHelper;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.player.old.PlayVideoActivity;
|
2017-10-13 03:22:25 +02:00
|
|
|
import org.schabi.newpipe.playlist.PlayQueueItem;
|
2017-10-23 04:21:19 +02:00
|
|
|
import org.schabi.newpipe.util.ListHelper;
|
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.ThemeHelper;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2017-10-23 04:21:19 +02:00
|
|
|
import java.util.List;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2017-10-28 19:08:01 +02:00
|
|
|
import static org.schabi.newpipe.player.helper.PlayerHelper.isUsingOldPlayer;
|
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
|
|
|
* Service Popup Player implementing VideoPlayer
|
2017-03-27 06:08:16 +02:00
|
|
|
*
|
|
|
|
* @author mauriciocolli
|
|
|
|
*/
|
2017-09-15 04:52:40 +02:00
|
|
|
public final class PopupVideoPlayer extends Service {
|
2017-03-09 08:42:40 +01:00
|
|
|
private static final String TAG = ".PopupVideoPlayer";
|
2017-04-17 06:19:53 +02:00
|
|
|
private static final boolean DEBUG = BasePlayer.DEBUG;
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
private static final int NOTIFICATION_ID = 40028922;
|
2017-03-27 06:08:16 +02:00
|
|
|
public static final String ACTION_CLOSE = "org.schabi.newpipe.player.PopupVideoPlayer.CLOSE";
|
|
|
|
public static final String ACTION_PLAY_PAUSE = "org.schabi.newpipe.player.PopupVideoPlayer.PLAY_PAUSE";
|
|
|
|
public static final String ACTION_REPEAT = "org.schabi.newpipe.player.PopupVideoPlayer.REPEAT";
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-05-01 07:23:50 +02:00
|
|
|
private static final String POPUP_SAVED_WIDTH = "popup_saved_width";
|
|
|
|
private static final String POPUP_SAVED_X = "popup_saved_x";
|
|
|
|
private static final String POPUP_SAVED_Y = "popup_saved_y";
|
|
|
|
|
2018-02-07 22:11:19 +01:00
|
|
|
private static final int MINIMUM_SHOW_EXTRA_WIDTH_DP = 300;
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
private WindowManager windowManager;
|
|
|
|
private WindowManager.LayoutParams windowLayoutParams;
|
|
|
|
private GestureDetector gestureDetector;
|
|
|
|
|
2017-11-01 01:07:12 +01:00
|
|
|
private int shutdownFlingVelocity;
|
|
|
|
private int tossFlingVelocity;
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
private float screenWidth, screenHeight;
|
|
|
|
private float popupWidth, popupHeight;
|
2017-05-01 07:23:50 +02:00
|
|
|
|
|
|
|
private float minimumWidth, minimumHeight;
|
|
|
|
private float maximumWidth, maximumHeight;
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
private NotificationManager notificationManager;
|
2017-09-10 01:33:51 +02:00
|
|
|
private NotificationCompat.Builder notBuilder;
|
|
|
|
private RemoteViews notRemoteView;
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
private VideoPlayerImpl playerImpl;
|
2017-10-27 04:59:25 +02:00
|
|
|
private LockManager lockManager;
|
2018-01-23 01:40:00 +01:00
|
|
|
|
2017-10-13 03:22:25 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Service-Activity Binder
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private PlayerEventListener activityListener;
|
|
|
|
private IBinder mBinder;
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Service LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
|
|
|
notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
|
2017-03-27 06:08:16 +02:00
|
|
|
|
2017-10-27 04:59:25 +02:00
|
|
|
lockManager = new LockManager(this);
|
|
|
|
playerImpl = new VideoPlayerImpl(this);
|
2017-04-26 21:32:20 +02:00
|
|
|
ThemeHelper.setTheme(this);
|
2017-10-13 03:22:25 +02:00
|
|
|
|
2017-10-26 22:42:50 +02:00
|
|
|
mBinder = new PlayerServiceBinder(playerImpl);
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(final Intent intent, int flags, int startId) {
|
2017-09-03 08:04:18 +02:00
|
|
|
if (DEBUG)
|
|
|
|
Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
|
2017-03-27 06:08:16 +02:00
|
|
|
if (playerImpl.getPlayer() == null) initPopup();
|
|
|
|
if (!playerImpl.isPlaying()) playerImpl.getPlayer().setPlayWhenReady(true);
|
|
|
|
|
2018-01-23 01:40:00 +01:00
|
|
|
playerImpl.handleIntent(intent);
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
updateScreenSize();
|
2017-05-01 07:23:50 +02:00
|
|
|
updatePopupSize(windowLayoutParams.width, -1);
|
|
|
|
checkPositionBounds();
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onDestroy() called");
|
2017-10-26 22:42:50 +02:00
|
|
|
onClose();
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
2017-10-13 03:22:25 +02:00
|
|
|
return mBinder;
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@SuppressLint("RtlHardcoded")
|
2017-03-09 08:42:40 +01:00
|
|
|
private void initPopup() {
|
|
|
|
if (DEBUG) Log.d(TAG, "initPopup() called");
|
|
|
|
View rootView = View.inflate(this, R.layout.player_popup, null);
|
2017-03-27 06:08:16 +02:00
|
|
|
playerImpl.setup(rootView);
|
|
|
|
|
2017-11-01 01:07:12 +01:00
|
|
|
shutdownFlingVelocity = PlayerHelper.getShutdownFlingVelocity(this);
|
|
|
|
tossFlingVelocity = PlayerHelper.getTossFlingVelocity(this);
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
updateScreenSize();
|
2017-05-01 07:23:50 +02:00
|
|
|
|
2017-11-01 01:07:12 +01:00
|
|
|
final boolean popupRememberSizeAndPos = PlayerHelper.isRememberingPopupDimensions(this);
|
|
|
|
final float defaultSize = getResources().getDimension(R.dimen.popup_default_width);
|
2017-05-01 07:23:50 +02:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
popupWidth = popupRememberSizeAndPos ? sharedPreferences.getFloat(POPUP_SAVED_WIDTH, defaultSize) : defaultSize;
|
|
|
|
|
2017-08-18 14:05:31 +02:00
|
|
|
final int layoutParamType = Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_PHONE : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
windowLayoutParams = new WindowManager.LayoutParams(
|
2017-05-01 07:23:50 +02:00
|
|
|
(int) popupWidth, (int) getMinimumVideoHeight(popupWidth),
|
2017-08-18 14:05:31 +02:00
|
|
|
layoutParamType,
|
2017-03-09 08:42:40 +01:00
|
|
|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
|
|
|
PixelFormat.TRANSLUCENT);
|
|
|
|
windowLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
|
|
|
|
|
2017-05-01 07:23:50 +02:00
|
|
|
int centerX = (int) (screenWidth / 2f - popupWidth / 2f);
|
|
|
|
int centerY = (int) (screenHeight / 2f - popupHeight / 2f);
|
|
|
|
windowLayoutParams.x = popupRememberSizeAndPos ? sharedPreferences.getInt(POPUP_SAVED_X, centerX) : centerX;
|
|
|
|
windowLayoutParams.y = popupRememberSizeAndPos ? sharedPreferences.getInt(POPUP_SAVED_Y, centerY) : centerY;
|
|
|
|
|
|
|
|
checkPositionBounds();
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
MySimpleOnGestureListener listener = new MySimpleOnGestureListener();
|
|
|
|
gestureDetector = new GestureDetector(this, listener);
|
|
|
|
rootView.setOnTouchListener(listener);
|
2017-03-27 06:08:16 +02:00
|
|
|
playerImpl.getLoadingPanel().setMinimumWidth(windowLayoutParams.width);
|
|
|
|
playerImpl.getLoadingPanel().setMinimumHeight(windowLayoutParams.height);
|
2017-03-09 08:42:40 +01:00
|
|
|
windowManager.addView(rootView, windowLayoutParams);
|
|
|
|
}
|
|
|
|
|
2017-09-10 01:33:51 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Notification
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-10-26 22:42:50 +02:00
|
|
|
private void resetNotification() {
|
|
|
|
notBuilder = createNotification();
|
|
|
|
}
|
|
|
|
|
2017-09-10 01:33:51 +02:00
|
|
|
private NotificationCompat.Builder createNotification() {
|
|
|
|
notRemoteView = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_popup_notification);
|
|
|
|
|
|
|
|
notRemoteView.setTextViewText(R.id.notificationSongName, playerImpl.getVideoTitle());
|
|
|
|
notRemoteView.setTextViewText(R.id.notificationArtist, playerImpl.getUploaderName());
|
|
|
|
|
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationPlayPause,
|
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_PLAY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationStop,
|
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_CLOSE), PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationRepeat,
|
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_REPEAT), PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
|
2018-01-04 07:53:31 +01:00
|
|
|
// Starts popup player activity -- attempts to unlock lockscreen
|
|
|
|
final Intent intent = NavigationHelper.getPopupPlayerActivityIntent(this);
|
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationContent,
|
|
|
|
PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
|
2017-10-25 07:18:44 +02:00
|
|
|
setRepeatModeRemote(notRemoteView, playerImpl.getRepeatMode());
|
2017-09-10 01:33:51 +02:00
|
|
|
|
|
|
|
return new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
|
|
|
|
.setOngoing(true)
|
2017-12-20 16:13:33 +01:00
|
|
|
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
|
2017-09-10 01:33:51 +02:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
|
.setContent(notRemoteView);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the notification, and the play/pause button in it.
|
|
|
|
* Used for changes on the remoteView
|
|
|
|
*
|
|
|
|
* @param drawableId if != -1, sets the drawable with that id on the play/pause button
|
|
|
|
*/
|
|
|
|
private void updateNotification(int drawableId) {
|
|
|
|
if (DEBUG) Log.d(TAG, "updateNotification() called with: drawableId = [" + drawableId + "]");
|
|
|
|
if (notBuilder == null || notRemoteView == null) return;
|
|
|
|
if (drawableId != -1) notRemoteView.setImageViewResource(R.id.notificationPlayPause, drawableId);
|
|
|
|
notificationManager.notify(NOTIFICATION_ID, notBuilder.build());
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Misc
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-10-26 22:42:50 +02:00
|
|
|
public void onClose() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onClose() called");
|
|
|
|
|
|
|
|
if (playerImpl != null) {
|
|
|
|
if (playerImpl.getRootView() != null) {
|
|
|
|
windowManager.removeView(playerImpl.getRootView());
|
|
|
|
playerImpl.setRootView(null);
|
|
|
|
}
|
|
|
|
playerImpl.stopActivityBinding();
|
|
|
|
playerImpl.destroy();
|
|
|
|
}
|
2017-10-27 04:59:25 +02:00
|
|
|
if (lockManager != null) lockManager.releaseWifiAndCpu();
|
2017-10-26 22:42:50 +02:00
|
|
|
if (notificationManager != null) notificationManager.cancel(NOTIFICATION_ID);
|
|
|
|
mBinder = null;
|
|
|
|
playerImpl = null;
|
|
|
|
|
|
|
|
stopForeground(true);
|
2017-03-27 06:08:16 +02:00
|
|
|
stopSelf();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-05-01 07:23:50 +02:00
|
|
|
private void checkPositionBounds() {
|
2017-09-03 08:04:18 +02:00
|
|
|
if (windowLayoutParams.x > screenWidth - windowLayoutParams.width)
|
|
|
|
windowLayoutParams.x = (int) (screenWidth - windowLayoutParams.width);
|
2017-05-01 07:23:50 +02:00
|
|
|
if (windowLayoutParams.x < 0) windowLayoutParams.x = 0;
|
2017-09-03 08:04:18 +02:00
|
|
|
if (windowLayoutParams.y > screenHeight - windowLayoutParams.height)
|
|
|
|
windowLayoutParams.y = (int) (screenHeight - windowLayoutParams.height);
|
2017-05-01 07:23:50 +02:00
|
|
|
if (windowLayoutParams.y < 0) windowLayoutParams.y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void savePositionAndSize() {
|
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(PopupVideoPlayer.this);
|
|
|
|
sharedPreferences.edit().putInt(POPUP_SAVED_X, windowLayoutParams.x).apply();
|
|
|
|
sharedPreferences.edit().putInt(POPUP_SAVED_Y, windowLayoutParams.y).apply();
|
|
|
|
sharedPreferences.edit().putFloat(POPUP_SAVED_WIDTH, windowLayoutParams.width).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
private float getMinimumVideoHeight(float width) {
|
|
|
|
//if (DEBUG) Log.d(TAG, "getMinimumVideoHeight() called with: width = [" + width + "], returned: " + height);
|
|
|
|
return width / (16.0f / 9.0f); // Respect the 16:9 ratio that most videos have
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
private void updateScreenSize() {
|
|
|
|
DisplayMetrics metrics = new DisplayMetrics();
|
|
|
|
windowManager.getDefaultDisplay().getMetrics(metrics);
|
|
|
|
|
|
|
|
screenWidth = metrics.widthPixels;
|
|
|
|
screenHeight = metrics.heightPixels;
|
|
|
|
if (DEBUG) Log.d(TAG, "updateScreenSize() called > screenWidth = " + screenWidth + ", screenHeight = " + screenHeight);
|
2017-05-01 07:23:50 +02:00
|
|
|
|
|
|
|
popupWidth = getResources().getDimension(R.dimen.popup_default_width);
|
|
|
|
popupHeight = getMinimumVideoHeight(popupWidth);
|
|
|
|
|
|
|
|
minimumWidth = getResources().getDimension(R.dimen.popup_minimum_width);
|
|
|
|
minimumHeight = getMinimumVideoHeight(minimumWidth);
|
|
|
|
|
|
|
|
maximumWidth = screenWidth;
|
|
|
|
maximumHeight = screenHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePopupSize(int width, int height) {
|
2017-11-04 18:12:38 +01:00
|
|
|
if (playerImpl == null) return;
|
2017-10-28 19:08:01 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "updatePopupSize() called with: width = [" + width + "], height = [" + height + "]");
|
2017-05-01 07:23:50 +02:00
|
|
|
|
|
|
|
width = (int) (width > maximumWidth ? maximumWidth : width < minimumWidth ? minimumWidth : width);
|
|
|
|
|
|
|
|
if (height == -1) height = (int) getMinimumVideoHeight(width);
|
|
|
|
else height = (int) (height > maximumHeight ? maximumHeight : height < minimumHeight ? minimumHeight : height);
|
|
|
|
|
|
|
|
windowLayoutParams.width = width;
|
|
|
|
windowLayoutParams.height = height;
|
|
|
|
popupWidth = width;
|
|
|
|
popupHeight = height;
|
|
|
|
|
|
|
|
if (DEBUG) Log.d(TAG, "updatePopupSize() updated values: width = [" + width + "], height = [" + height + "]");
|
|
|
|
windowManager.updateViewLayout(playerImpl.getRootView(), windowLayoutParams);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 02:02:07 +02:00
|
|
|
protected void setRepeatModeRemote(final RemoteViews remoteViews, final int repeatMode) {
|
2017-10-27 04:59:25 +02:00
|
|
|
final String methodName = "setImageResource";
|
|
|
|
|
2017-10-25 07:18:44 +02:00
|
|
|
if (remoteViews == null) return;
|
|
|
|
|
2017-10-13 02:02:07 +02:00
|
|
|
switch (repeatMode) {
|
|
|
|
case Player.REPEAT_MODE_OFF:
|
2017-10-27 04:59:25 +02:00
|
|
|
remoteViews.setInt(R.id.notificationRepeat, methodName, R.drawable.exo_controls_repeat_off);
|
2017-10-13 02:02:07 +02:00
|
|
|
break;
|
|
|
|
case Player.REPEAT_MODE_ONE:
|
2017-10-27 04:59:25 +02:00
|
|
|
remoteViews.setInt(R.id.notificationRepeat, methodName, R.drawable.exo_controls_repeat_one);
|
2017-10-13 02:02:07 +02:00
|
|
|
break;
|
|
|
|
case Player.REPEAT_MODE_ALL:
|
2017-10-27 04:59:25 +02:00
|
|
|
remoteViews.setInt(R.id.notificationRepeat, methodName, R.drawable.exo_controls_repeat_all);
|
2017-10-13 02:02:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-02-07 22:11:19 +01:00
|
|
|
protected class VideoPlayerImpl extends VideoPlayer implements View.OnLayoutChangeListener {
|
2017-05-01 07:23:50 +02:00
|
|
|
private TextView resizingIndicator;
|
2017-12-23 17:34:47 +01:00
|
|
|
private ImageButton fullScreenButton;
|
2017-05-01 07:23:50 +02:00
|
|
|
|
2018-02-07 22:11:19 +01:00
|
|
|
private View extraOptionsView;
|
|
|
|
|
2017-10-26 22:42:50 +02:00
|
|
|
@Override
|
|
|
|
public void handleIntent(Intent intent) {
|
|
|
|
super.handleIntent(intent);
|
|
|
|
|
|
|
|
resetNotification();
|
|
|
|
startForeground(NOTIFICATION_ID, notBuilder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoPlayerImpl(final Context context) {
|
|
|
|
super("VideoPlayerImpl" + PopupVideoPlayer.TAG, context);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 07:23:50 +02:00
|
|
|
@Override
|
|
|
|
public void initViews(View rootView) {
|
|
|
|
super.initViews(rootView);
|
2017-09-03 08:04:18 +02:00
|
|
|
resizingIndicator = rootView.findViewById(R.id.resizing_indicator);
|
2017-12-23 17:34:47 +01:00
|
|
|
fullScreenButton = rootView.findViewById(R.id.fullScreenButton);
|
2018-01-04 07:53:31 +01:00
|
|
|
fullScreenButton.setOnClickListener(v -> onFullScreenButtonClicked());
|
2018-02-07 22:11:19 +01:00
|
|
|
|
|
|
|
extraOptionsView = rootView.findViewById(R.id.extraOptionsView);
|
|
|
|
rootView.addOnLayoutChangeListener(this);
|
|
|
|
}
|
|
|
|
|
2018-02-09 22:26:03 +01:00
|
|
|
@Override
|
|
|
|
protected void setupSubtitleView(@NonNull SubtitleView view,
|
|
|
|
@NonNull String captionSizeKey) {
|
|
|
|
float captionRatio = SubtitleView.DEFAULT_TEXT_SIZE_FRACTION;
|
|
|
|
if (captionSizeKey.equals(getString(R.string.smaller_caption_size_key))) {
|
|
|
|
captionRatio *= 0.9;
|
|
|
|
} else if (captionSizeKey.equals(getString(R.string.larger_caption_size_key))) {
|
|
|
|
captionRatio *= 1.1;
|
|
|
|
}
|
|
|
|
view.setFractionalTextSize(captionRatio);
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:11:19 +01:00
|
|
|
@Override
|
|
|
|
public void onLayoutChange(final View view, int left, int top, int right, int bottom,
|
|
|
|
int oldLeft, int oldTop, int oldRight, int oldBottom) {
|
|
|
|
float widthDp = Math.abs(right - left) / getResources().getDisplayMetrics().density;
|
|
|
|
final int visibility = widthDp > MINIMUM_SHOW_EXTRA_WIDTH_DP ? View.VISIBLE : View.GONE;
|
|
|
|
extraOptionsView.setVisibility(visibility);
|
2017-05-01 07:23:50 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 06:19:53 +02:00
|
|
|
@Override
|
|
|
|
public void destroy() {
|
2017-09-10 01:33:51 +02:00
|
|
|
if (notRemoteView != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, null);
|
2017-10-26 22:42:50 +02:00
|
|
|
super.destroy();
|
2017-04-17 06:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onThumbnailReceived(Bitmap thumbnail) {
|
|
|
|
super.onThumbnailReceived(thumbnail);
|
2017-09-10 01:33:51 +02:00
|
|
|
if (thumbnail != null) {
|
|
|
|
// rebuild notification here since remote view does not release bitmaps, causing memory leaks
|
|
|
|
notBuilder = createNotification();
|
|
|
|
|
|
|
|
if (notRemoteView != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, thumbnail);
|
|
|
|
|
|
|
|
updateNotification(-1);
|
|
|
|
}
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onFullScreenButtonClicked() {
|
2017-09-07 22:01:02 +02:00
|
|
|
super.onFullScreenButtonClicked();
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onFullScreenButtonClicked() called");
|
2017-10-22 21:43:49 +02:00
|
|
|
|
2017-10-26 22:42:50 +02:00
|
|
|
setRecovery();
|
2017-03-27 06:08:16 +02:00
|
|
|
Intent intent;
|
2017-10-27 04:59:25 +02:00
|
|
|
if (!isUsingOldPlayer(getApplicationContext())) {
|
2017-09-25 05:14:58 +02:00
|
|
|
intent = NavigationHelper.getPlayerIntent(
|
|
|
|
context,
|
|
|
|
MainVideoPlayer.class,
|
|
|
|
this.getPlayQueue(),
|
2017-10-25 07:18:44 +02:00
|
|
|
this.getRepeatMode(),
|
2017-10-23 03:58:01 +02:00
|
|
|
this.getPlaybackSpeed(),
|
2017-10-25 06:47:14 +02:00
|
|
|
this.getPlaybackPitch(),
|
|
|
|
this.getPlaybackQuality()
|
2017-09-25 05:14:58 +02:00
|
|
|
);
|
2017-03-27 06:08:16 +02:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
} else {
|
|
|
|
intent = new Intent(PopupVideoPlayer.this, PlayVideoActivity.class)
|
|
|
|
.putExtra(PlayVideoActivity.VIDEO_TITLE, getVideoTitle())
|
2017-12-08 15:05:08 +01:00
|
|
|
.putExtra(PlayVideoActivity.STREAM_URL, getSelectedVideoStream().getUrl())
|
2017-03-27 06:08:16 +02:00
|
|
|
.putExtra(PlayVideoActivity.VIDEO_URL, getVideoUrl())
|
|
|
|
.putExtra(PlayVideoActivity.START_POSITION, Math.round(getPlayer().getCurrentPosition() / 1000f));
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
2017-04-12 08:07:15 +02:00
|
|
|
context.startActivity(intent);
|
2017-10-26 22:42:50 +02:00
|
|
|
onClose();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
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(500, 0);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 22:11:19 +01:00
|
|
|
@Override
|
2018-02-11 04:33:48 +01:00
|
|
|
protected int nextResizeMode(int resizeMode) {
|
|
|
|
if (resizeMode == AspectRatioFrameLayout.RESIZE_MODE_FILL) {
|
|
|
|
return AspectRatioFrameLayout.RESIZE_MODE_FIT;
|
|
|
|
} else {
|
|
|
|
return AspectRatioFrameLayout.RESIZE_MODE_FILL;
|
2018-02-07 22:11:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:33:26 +02:00
|
|
|
@Override
|
|
|
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
|
|
|
super.onStopTrackingTouch(seekBar);
|
2017-09-07 22:01:02 +02:00
|
|
|
if (wasPlaying()) {
|
2017-05-08 15:33:26 +02:00
|
|
|
hideControls(100, 0);
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 03:22:25 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onShuffleClicked() {
|
|
|
|
super.onShuffleClicked();
|
|
|
|
updatePlayback();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdateProgress(int currentProgress, int duration, int bufferPercent) {
|
|
|
|
updateProgress(currentProgress, duration, bufferPercent);
|
2017-10-27 04:59:25 +02:00
|
|
|
super.onUpdateProgress(currentProgress, duration, bufferPercent);
|
2017-10-13 03:22:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 04:21:19 +02:00
|
|
|
@Override
|
|
|
|
protected int getDefaultResolutionIndex(final List<VideoStream> sortedVideos) {
|
|
|
|
return ListHelper.getPopupDefaultResolutionIndex(context, sortedVideos);
|
|
|
|
}
|
|
|
|
|
2017-10-25 06:47:14 +02:00
|
|
|
@Override
|
|
|
|
protected int getOverrideResolutionIndex(final List<VideoStream> sortedVideos,
|
|
|
|
final String playbackQuality) {
|
|
|
|
return ListHelper.getPopupDefaultResolutionIndex(context, sortedVideos, playbackQuality);
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:22:25 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Activity Event Listener
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-10-27 04:59:25 +02:00
|
|
|
/*package-private*/ void setActivityListener(PlayerEventListener listener) {
|
2017-10-13 03:22:25 +02:00
|
|
|
activityListener = listener;
|
|
|
|
updateMetadata();
|
|
|
|
updatePlayback();
|
|
|
|
triggerProgressUpdate();
|
|
|
|
}
|
|
|
|
|
2017-10-27 04:59:25 +02:00
|
|
|
/*package-private*/ void removeActivityListener(PlayerEventListener listener) {
|
2017-10-13 03:22:25 +02:00
|
|
|
if (activityListener == listener) {
|
|
|
|
activityListener = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateMetadata() {
|
|
|
|
if (activityListener != null && currentInfo != null) {
|
|
|
|
activityListener.onMetadataUpdate(currentInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePlayback() {
|
|
|
|
if (activityListener != null && simpleExoPlayer != null && playQueue != null) {
|
2017-10-25 07:18:44 +02:00
|
|
|
activityListener.onPlaybackUpdate(currentState, getRepeatMode(), playQueue.isShuffled(), simpleExoPlayer.getPlaybackParameters());
|
2017-10-13 03:22:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateProgress(int currentProgress, int duration, int bufferPercent) {
|
|
|
|
if (activityListener != null) {
|
|
|
|
activityListener.onProgressUpdate(currentProgress, duration, bufferPercent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopActivityBinding() {
|
|
|
|
if (activityListener != null) {
|
|
|
|
activityListener.onServiceStopped();
|
|
|
|
activityListener = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:02:07 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// ExoPlayer Video Listener
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRepeatModeChanged(int i) {
|
|
|
|
super.onRepeatModeChanged(i);
|
|
|
|
setRepeatModeRemote(notRemoteView, i);
|
|
|
|
updateNotification(-1);
|
2017-10-13 03:22:25 +02:00
|
|
|
updatePlayback();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
|
|
|
|
super.onPlaybackParametersChanged(playbackParameters);
|
|
|
|
updatePlayback();
|
2017-10-13 02:02:07 +02:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2017-09-05 21:27:12 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Playback Listener
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-10-13 03:22:25 +02:00
|
|
|
@Override
|
|
|
|
public void sync(@NonNull PlayQueueItem item, @Nullable StreamInfo info) {
|
2017-11-04 04:32:10 +01:00
|
|
|
if (currentItem == item && currentInfo == info) return;
|
2017-10-13 03:22:25 +02:00
|
|
|
super.sync(item, info);
|
|
|
|
updateMetadata();
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:27:12 +02:00
|
|
|
@Override
|
|
|
|
public void shutdown() {
|
|
|
|
super.shutdown();
|
2017-10-26 22:42:50 +02:00
|
|
|
onClose();
|
2017-09-05 21:27:12 +02:00
|
|
|
}
|
|
|
|
|
2017-08-18 20:07:57 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
2017-04-17 06:19:53 +02:00
|
|
|
// Broadcast Receiver
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setupBroadcastReceiver(IntentFilter intentFilter) {
|
|
|
|
super.setupBroadcastReceiver(intentFilter);
|
|
|
|
if (DEBUG) Log.d(TAG, "setupBroadcastReceiver() called with: intentFilter = [" + intentFilter + "]");
|
|
|
|
intentFilter.addAction(ACTION_CLOSE);
|
|
|
|
intentFilter.addAction(ACTION_PLAY_PAUSE);
|
|
|
|
intentFilter.addAction(ACTION_REPEAT);
|
2017-10-15 07:54:25 +02:00
|
|
|
|
|
|
|
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
|
|
|
|
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
2017-04-17 06:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBroadcastReceived(Intent intent) {
|
|
|
|
super.onBroadcastReceived(intent);
|
2017-10-26 22:42:50 +02:00
|
|
|
if (intent == null || intent.getAction() == null) return;
|
2017-04-17 06:19:53 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onBroadcastReceived() called with: intent = [" + intent + "]");
|
|
|
|
switch (intent.getAction()) {
|
|
|
|
case ACTION_CLOSE:
|
2017-10-26 22:42:50 +02:00
|
|
|
onClose();
|
2017-04-17 06:19:53 +02:00
|
|
|
break;
|
|
|
|
case ACTION_PLAY_PAUSE:
|
2017-09-07 22:01:02 +02:00
|
|
|
onVideoPlayPause();
|
2017-04-17 06:19:53 +02:00
|
|
|
break;
|
|
|
|
case ACTION_REPEAT:
|
2017-09-07 22:01:02 +02:00
|
|
|
onRepeatClicked();
|
2017-04-17 06:19:53 +02:00
|
|
|
break;
|
2017-10-15 07:54:25 +02:00
|
|
|
case Intent.ACTION_SCREEN_ON:
|
|
|
|
enableVideoRenderer(true);
|
|
|
|
break;
|
|
|
|
case Intent.ACTION_SCREEN_OFF:
|
|
|
|
enableVideoRenderer(false);
|
|
|
|
break;
|
2017-04-17 06:19:53 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 03:22:25 +02:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// States
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-10-13 03:22:25 +02:00
|
|
|
@Override
|
|
|
|
public void changeState(int state) {
|
|
|
|
super.changeState(state);
|
|
|
|
updatePlayback();
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
@Override
|
2017-09-14 17:44:09 +02:00
|
|
|
public void onBlocked() {
|
|
|
|
super.onBlocked();
|
2017-09-10 01:33:51 +02:00
|
|
|
updateNotification(R.drawable.ic_play_arrow_white);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onPlaying() {
|
|
|
|
super.onPlaying();
|
2017-09-10 01:33:51 +02:00
|
|
|
updateNotification(R.drawable.ic_pause_white);
|
2017-10-27 04:59:25 +02:00
|
|
|
lockManager.acquireWifiAndCpu();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onBuffering() {
|
|
|
|
super.onBuffering();
|
2017-09-10 01:33:51 +02:00
|
|
|
updateNotification(R.drawable.ic_play_arrow_white);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onPaused() {
|
|
|
|
super.onPaused();
|
2017-09-10 01:33:51 +02:00
|
|
|
updateNotification(R.drawable.ic_play_arrow_white);
|
2017-03-27 06:08:16 +02:00
|
|
|
showAndAnimateControl(R.drawable.ic_play_arrow_white, false);
|
2017-10-27 04:59:25 +02:00
|
|
|
lockManager.releaseWifiAndCpu();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onPausedSeek() {
|
|
|
|
super.onPausedSeek();
|
2017-09-10 01:33:51 +02:00
|
|
|
updateNotification(R.drawable.ic_play_arrow_white);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onCompleted() {
|
|
|
|
super.onCompleted();
|
2017-09-10 01:33:51 +02:00
|
|
|
updateNotification(R.drawable.ic_replay_white);
|
2017-03-27 06:08:16 +02:00
|
|
|
showAndAnimateControl(R.drawable.ic_replay_white, false);
|
2017-10-27 04:59:25 +02:00
|
|
|
lockManager.releaseWifiAndCpu();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-10-15 07:54:25 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-10-27 04:59:25 +02:00
|
|
|
/*package-private*/ void enableVideoRenderer(final boolean enable) {
|
2018-02-03 23:39:03 +01:00
|
|
|
final int videoRendererIndex = getRendererIndex(C.TRACK_TYPE_VIDEO);
|
2018-02-09 21:43:24 +01:00
|
|
|
if (trackSelector != null && videoRendererIndex != RENDERER_UNAVAILABLE) {
|
2017-10-15 07:54:25 +02:00
|
|
|
trackSelector.setRendererDisabled(videoRendererIndex, !enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Getters
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-05-01 07:23:50 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("WeakerAccess")
|
|
|
|
public TextView getResizingIndicator() {
|
|
|
|
return resizingIndicator;
|
|
|
|
}
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener {
|
|
|
|
private int initialPopupX, initialPopupY;
|
|
|
|
private boolean isMoving;
|
|
|
|
|
2017-05-01 07:23:50 +02:00
|
|
|
private boolean isResizing;
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
@Override
|
|
|
|
public boolean onDoubleTap(MotionEvent e) {
|
2017-09-03 08:04:18 +02:00
|
|
|
if (DEBUG)
|
|
|
|
Log.d(TAG, "onDoubleTap() called with: e = [" + e + "]" + "rawXy = " + e.getRawX() + ", " + e.getRawY() + ", xy = " + e.getX() + ", " + e.getY());
|
2017-10-28 19:08:01 +02:00
|
|
|
if (playerImpl == null || !playerImpl.isPlaying() || !playerImpl.isPlayerReady()) return false;
|
2017-09-06 02:48:48 +02:00
|
|
|
|
2017-09-05 21:27:12 +02:00
|
|
|
if (e.getX() > popupWidth / 2) {
|
2017-10-22 21:43:49 +02:00
|
|
|
playerImpl.onFastForward();
|
2017-09-05 21:27:12 +02:00
|
|
|
} else {
|
2017-10-22 21:43:49 +02:00
|
|
|
playerImpl.onFastRewind();
|
2017-09-05 21:27:12 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onSingleTapConfirmed(MotionEvent e) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onSingleTapConfirmed() called with: e = [" + e + "]");
|
2017-10-28 19:08:01 +02:00
|
|
|
if (playerImpl == null || playerImpl.getPlayer() == null) return false;
|
2017-03-27 06:08:16 +02:00
|
|
|
playerImpl.onVideoPlayPause();
|
2017-03-09 08:42:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onDown(MotionEvent e) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onDown() called with: e = [" + e + "]");
|
|
|
|
initialPopupX = windowLayoutParams.x;
|
|
|
|
initialPopupY = windowLayoutParams.y;
|
2017-05-01 07:23:50 +02:00
|
|
|
popupWidth = windowLayoutParams.width;
|
|
|
|
popupHeight = windowLayoutParams.height;
|
2017-10-26 07:00:58 +02:00
|
|
|
return super.onDown(e);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 07:23:50 +02:00
|
|
|
@Override
|
|
|
|
public void onLongPress(MotionEvent e) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onLongPress() called with: e = [" + e + "]");
|
2017-10-26 07:00:58 +02:00
|
|
|
updateScreenSize();
|
|
|
|
checkPositionBounds();
|
|
|
|
updatePopupSize((int) screenWidth, -1);
|
2017-05-01 07:23:50 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
@Override
|
|
|
|
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
2017-10-28 19:08:01 +02:00
|
|
|
if (isResizing || playerImpl == null) return super.onScroll(e1, e2, distanceX, distanceY);
|
2017-05-01 07:23:50 +02:00
|
|
|
|
2017-05-08 15:33:26 +02:00
|
|
|
if (playerImpl.getCurrentState() != BasePlayer.STATE_BUFFERING
|
|
|
|
&& (!isMoving || playerImpl.getControlsRoot().getAlpha() != 1f)) playerImpl.showControls(0);
|
2017-03-09 08:42:40 +01:00
|
|
|
isMoving = true;
|
2017-05-01 07:23:50 +02:00
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
float diffX = (int) (e2.getRawX() - e1.getRawX()), posX = (int) (initialPopupX + diffX);
|
|
|
|
float diffY = (int) (e2.getRawY() - e1.getRawY()), posY = (int) (initialPopupY + diffY);
|
|
|
|
|
|
|
|
if (posX > (screenWidth - popupWidth)) posX = (int) (screenWidth - popupWidth);
|
|
|
|
else if (posX < 0) posX = 0;
|
|
|
|
|
|
|
|
if (posY > (screenHeight - popupHeight)) posY = (int) (screenHeight - popupHeight);
|
|
|
|
else if (posY < 0) posY = 0;
|
|
|
|
|
|
|
|
windowLayoutParams.x = (int) posX;
|
|
|
|
windowLayoutParams.y = (int) posY;
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
//noinspection PointlessBooleanExpression
|
|
|
|
if (DEBUG && false) Log.d(TAG, "PopupVideoPlayer.onScroll = " +
|
2017-03-09 08:42:40 +01:00
|
|
|
", e1.getRaw = [" + e1.getRawX() + ", " + e1.getRawY() + "]" +
|
|
|
|
", e2.getRaw = [" + e2.getRawX() + ", " + e2.getRawY() + "]" +
|
|
|
|
", distanceXy = [" + distanceX + ", " + distanceY + "]" +
|
|
|
|
", posXy = [" + posX + ", " + posY + "]" +
|
2017-05-01 07:23:50 +02:00
|
|
|
", popupWh = [" + popupWidth + " x " + popupHeight + "]");
|
2017-03-27 06:08:16 +02:00
|
|
|
windowManager.updateViewLayout(playerImpl.getRootView(), windowLayoutParams);
|
2017-03-09 08:42:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onScrollEnd() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onScrollEnd() called");
|
2017-10-28 19:08:01 +02:00
|
|
|
if (playerImpl == null) return;
|
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-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 07:00:58 +02:00
|
|
|
@Override
|
|
|
|
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
2017-11-01 01:07:12 +01:00
|
|
|
if (DEBUG) Log.d(TAG, "Fling velocity: dX=[" + velocityX + "], dY=[" + velocityY + "]");
|
2017-10-28 19:08:01 +02:00
|
|
|
if (playerImpl == null) return false;
|
2017-11-01 01:07:12 +01:00
|
|
|
|
|
|
|
final float absVelocityX = Math.abs(velocityX);
|
|
|
|
final float absVelocityY = Math.abs(velocityY);
|
|
|
|
if (absVelocityX > shutdownFlingVelocity) {
|
2017-10-26 22:42:50 +02:00
|
|
|
onClose();
|
2017-10-26 07:00:58 +02:00
|
|
|
return true;
|
2017-11-01 01:07:12 +01:00
|
|
|
} else if (Math.max(absVelocityX, absVelocityY) > tossFlingVelocity) {
|
|
|
|
if (absVelocityX > tossFlingVelocity) windowLayoutParams.x = (int) velocityX;
|
|
|
|
if (absVelocityY > tossFlingVelocity) windowLayoutParams.y = (int) velocityY;
|
|
|
|
checkPositionBounds();
|
|
|
|
windowManager.updateViewLayout(playerImpl.getRootView(), windowLayoutParams);
|
|
|
|
return true;
|
2017-10-26 07:00:58 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
gestureDetector.onTouchEvent(event);
|
2017-10-28 19:08:01 +02:00
|
|
|
if (playerImpl == null) return false;
|
2017-10-26 07:00:58 +02:00
|
|
|
if (event.getPointerCount() == 2 && !isResizing) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onTouch() 2 finger pointer detected, enabling resizing.");
|
|
|
|
playerImpl.showAndAnimateControl(-1, true);
|
|
|
|
playerImpl.getLoadingPanel().setVisibility(View.GONE);
|
|
|
|
|
|
|
|
playerImpl.hideControls(0, 0);
|
|
|
|
animateView(playerImpl.getCurrentDisplaySeek(), false, 0, 0);
|
|
|
|
animateView(playerImpl.getResizingIndicator(), true, 200, 0);
|
|
|
|
isResizing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_MOVE && !isMoving && isResizing) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onTouch() ACTION_MOVE > v = [" + v + "], e1.getRaw = [" + event.getRawX() + ", " + event.getRawY() + "]");
|
|
|
|
return handleMultiDrag(event);
|
2017-05-01 07:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_UP) {
|
2017-09-03 08:04:18 +02:00
|
|
|
if (DEBUG)
|
|
|
|
Log.d(TAG, "onTouch() ACTION_UP > v = [" + v + "], e1.getRaw = [" + event.getRawX() + ", " + event.getRawY() + "]");
|
2017-05-01 07:23:50 +02:00
|
|
|
if (isMoving) {
|
|
|
|
isMoving = false;
|
|
|
|
onScrollEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isResizing) {
|
|
|
|
isResizing = false;
|
2017-05-08 15:33:26 +02:00
|
|
|
animateView(playerImpl.getResizingIndicator(), false, 100, 0);
|
2017-05-01 07:23:50 +02:00
|
|
|
playerImpl.changeState(playerImpl.getCurrentState());
|
|
|
|
}
|
|
|
|
savePositionAndSize();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
2017-11-14 19:13:36 +01:00
|
|
|
|
|
|
|
v.performClick();
|
2017-03-09 08:42:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-26 07:00:58 +02:00
|
|
|
private boolean handleMultiDrag(final MotionEvent event) {
|
|
|
|
if (event.getPointerCount() != 2) return false;
|
|
|
|
|
|
|
|
final float firstPointerX = event.getX(0);
|
|
|
|
final float secondPointerX = event.getX(1);
|
|
|
|
|
|
|
|
final float diff = Math.abs(firstPointerX - secondPointerX);
|
|
|
|
if (firstPointerX > secondPointerX) {
|
|
|
|
// second pointer is the anchor (the leftmost pointer)
|
|
|
|
windowLayoutParams.x = (int) (event.getRawX() - diff);
|
|
|
|
} else {
|
|
|
|
// first pointer is the anchor
|
|
|
|
windowLayoutParams.x = (int) event.getRawX();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPositionBounds();
|
|
|
|
updateScreenSize();
|
|
|
|
|
|
|
|
final int width = (int) Math.min(screenWidth, diff);
|
|
|
|
updatePopupSize(width, -1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
2017-03-27 06:08:16 +02:00
|
|
|
}
|