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.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.res.Configuration;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.content.res.Resources;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.PixelFormat;
|
|
|
|
import android.net.Uri;
|
2017-03-27 06:08:16 +02:00
|
|
|
import android.os.Build;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.IBinder;
|
|
|
|
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-03-27 06:08:16 +02:00
|
|
|
import android.widget.PopupMenu;
|
2017-03-09 08:42:40 +01:00
|
|
|
import android.widget.RemoteViews;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
|
|
|
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.ActivityCommunicator;
|
|
|
|
import org.schabi.newpipe.BuildConfig;
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.detail.VideoItemDetailActivity;
|
|
|
|
import org.schabi.newpipe.extractor.MediaFormat;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.stream_info.StreamExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.stream_info.StreamInfo;
|
|
|
|
import org.schabi.newpipe.extractor.stream_info.VideoStream;
|
|
|
|
import org.schabi.newpipe.util.NavStack;
|
2017-03-27 06:08:16 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-03-17 20:18:44 +01:00
|
|
|
import java.io.IOException;
|
2017-03-27 06:08:16 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service Popup Player implementing AbstractPlayer
|
|
|
|
*
|
|
|
|
* @author mauriciocolli
|
|
|
|
*/
|
|
|
|
public class PopupVideoPlayer extends Service {
|
2017-03-09 08:42:40 +01:00
|
|
|
private static final String TAG = ".PopupVideoPlayer";
|
2017-03-27 06:08:16 +02:00
|
|
|
private static final boolean DEBUG = AbstractPlayer.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_OPEN_DETAIL = "org.schabi.newpipe.player.PopupVideoPlayer.OPEN_DETAIL";
|
|
|
|
public static final String ACTION_REPEAT = "org.schabi.newpipe.player.PopupVideoPlayer.REPEAT";
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
private BroadcastReceiver broadcastReceiver;
|
|
|
|
|
|
|
|
private WindowManager windowManager;
|
|
|
|
private WindowManager.LayoutParams windowLayoutParams;
|
|
|
|
private GestureDetector gestureDetector;
|
|
|
|
|
|
|
|
private float screenWidth, screenHeight;
|
|
|
|
private float popupWidth, popupHeight;
|
2017-03-27 06:08:16 +02:00
|
|
|
private float currentPopupHeight = 110.0f * Resources.getSystem().getDisplayMetrics().density;
|
2017-03-09 08:42:40 +01:00
|
|
|
//private float minimumHeight = 100; // TODO: Use it when implementing the resize of the popup
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
private final String setAlphaMethodName = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) ? "setImageAlpha" : "setAlpha";
|
2017-03-09 08:42:40 +01:00
|
|
|
private NotificationManager notificationManager;
|
|
|
|
private NotificationCompat.Builder notBuilder;
|
|
|
|
private RemoteViews notRemoteView;
|
|
|
|
|
|
|
|
|
|
|
|
private ImageLoader imageLoader = ImageLoader.getInstance();
|
2017-03-27 06:08:16 +02:00
|
|
|
private DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).build();
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
private AbstractPlayerImpl playerImpl;
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Service LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
|
|
|
notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
|
|
|
|
initReceiver();
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
playerImpl = new AbstractPlayerImpl();
|
|
|
|
ThemeHelper.setTheme(this, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public int onStartCommand(final Intent intent, int flags, int startId) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
|
|
|
|
if (playerImpl.getPlayer() == null) initPopup();
|
|
|
|
if (!playerImpl.isPlaying()) playerImpl.getPlayer().setPlayWhenReady(true);
|
|
|
|
|
|
|
|
if (imageLoader != null) imageLoader.clearMemoryCache();
|
|
|
|
if (intent.getStringExtra(NavStack.URL) != null) {
|
|
|
|
playerImpl.setStartedFromNewPipe(false);
|
|
|
|
Thread fetcher = new Thread(new FetcherRunnable(intent));
|
|
|
|
fetcher.start();
|
|
|
|
} else {
|
|
|
|
playerImpl.setStartedFromNewPipe(true);
|
|
|
|
playerImpl.handleIntent(intent);
|
|
|
|
}
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
updateScreenSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onDestroy() called");
|
|
|
|
stopForeground(true);
|
|
|
|
if (playerImpl != null) {
|
|
|
|
playerImpl.destroy();
|
|
|
|
if (playerImpl.getRootView() != null) windowManager.removeView(playerImpl.getRootView());
|
|
|
|
}
|
|
|
|
if (imageLoader != null) imageLoader.clearMemoryCache();
|
|
|
|
if (notificationManager != null) notificationManager.cancel(NOTIFICATION_ID);
|
|
|
|
if (broadcastReceiver != null) unregisterReceiver(broadcastReceiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
private void initReceiver() {
|
|
|
|
broadcastReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2017-03-27 06:08:16 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onReceive() called with: context = [" + context + "], intent = [" + intent + "]");
|
2017-03-09 08:42:40 +01:00
|
|
|
switch (intent.getAction()) {
|
2017-03-27 06:08:16 +02:00
|
|
|
case ACTION_CLOSE:
|
|
|
|
onVideoClose();
|
2017-03-09 08:42:40 +01:00
|
|
|
break;
|
2017-03-27 06:08:16 +02:00
|
|
|
case ACTION_PLAY_PAUSE:
|
|
|
|
playerImpl.onVideoPlayPause();
|
2017-03-09 08:42:40 +01:00
|
|
|
break;
|
2017-03-27 06:08:16 +02:00
|
|
|
case ACTION_OPEN_DETAIL:
|
|
|
|
onOpenDetail(PopupVideoPlayer.this, playerImpl.getVideoUrl());
|
2017-03-09 08:42:40 +01:00
|
|
|
break;
|
2017-03-27 06:08:16 +02:00
|
|
|
case ACTION_REPEAT:
|
|
|
|
playerImpl.onRepeatClicked();
|
|
|
|
break;
|
|
|
|
case AbstractPlayer.ACTION_UPDATE_THUMB:
|
|
|
|
playerImpl.onUpdateThumbnail(intent);
|
2017-03-09 08:42:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
2017-03-27 06:08:16 +02:00
|
|
|
intentFilter.addAction(ACTION_CLOSE);
|
|
|
|
intentFilter.addAction(ACTION_PLAY_PAUSE);
|
|
|
|
intentFilter.addAction(ACTION_OPEN_DETAIL);
|
|
|
|
intentFilter.addAction(ACTION_REPEAT);
|
|
|
|
intentFilter.addAction(AbstractPlayer.ACTION_UPDATE_THUMB);
|
2017-03-09 08:42:40 +01:00
|
|
|
registerReceiver(broadcastReceiver, intentFilter);
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@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);
|
|
|
|
|
|
|
|
updateScreenSize();
|
2017-03-09 08:42:40 +01:00
|
|
|
windowLayoutParams = new WindowManager.LayoutParams(
|
|
|
|
(int) getMinimumVideoWidth(currentPopupHeight), (int) currentPopupHeight,
|
|
|
|
WindowManager.LayoutParams.TYPE_PHONE,
|
|
|
|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
|
|
|
PixelFormat.TRANSLUCENT);
|
2017-03-27 06:08:16 +02:00
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
windowLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
|
|
|
|
|
|
|
|
MySimpleOnGestureListener listener = new MySimpleOnGestureListener();
|
|
|
|
gestureDetector = new GestureDetector(this, listener);
|
|
|
|
gestureDetector.setIsLongpressEnabled(false);
|
|
|
|
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-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Notification
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
private NotificationCompat.Builder createNotification() {
|
|
|
|
notRemoteView = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_popup_notification);
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
if (playerImpl.getVideoThumbnail() != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, playerImpl.getVideoThumbnail());
|
2017-03-09 08:42:40 +01:00
|
|
|
else notRemoteView.setImageViewResource(R.id.notificationCover, R.drawable.dummy_thumbnail);
|
2017-03-27 06:08:16 +02:00
|
|
|
notRemoteView.setTextViewText(R.id.notificationSongName, playerImpl.getVideoTitle());
|
|
|
|
notRemoteView.setTextViewText(R.id.notificationArtist, playerImpl.getChannelName());
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationPlayPause,
|
2017-03-27 06:08:16 +02:00
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_PLAY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT));
|
2017-03-09 08:42:40 +01:00
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationStop,
|
2017-03-27 06:08:16 +02:00
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_CLOSE), PendingIntent.FLAG_UPDATE_CURRENT));
|
2017-03-09 08:42:40 +01:00
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationContent,
|
2017-03-27 06:08:16 +02:00
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_OPEN_DETAIL), PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
notRemoteView.setOnClickPendingIntent(R.id.notificationRepeat,
|
|
|
|
PendingIntent.getBroadcast(this, NOTIFICATION_ID, new Intent(ACTION_REPEAT), PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
|
|
|
|
switch (playerImpl.getCurrentRepeatMode()) {
|
|
|
|
case REPEAT_DISABLED:
|
|
|
|
notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 77);
|
|
|
|
break;
|
|
|
|
case REPEAT_ONE:
|
|
|
|
notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 255);
|
|
|
|
break;
|
|
|
|
case REPEAT_ALL:
|
|
|
|
// Waiting :)
|
|
|
|
break;
|
|
|
|
}
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
return new NotificationCompat.Builder(this)
|
|
|
|
.setOngoing(true)
|
2017-03-27 06:08:16 +02:00
|
|
|
.setSmallIcon(R.drawable.ic_play_arrow_white)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
2017-03-09 08:42:40 +01:00
|
|
|
.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-03-27 06:08:16 +02:00
|
|
|
public void onVideoClose() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onVideoClose() called");
|
|
|
|
stopSelf();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onOpenDetail(Context context, String videoUrl) {
|
|
|
|
if (DEBUG) Log.d(TAG, "onOpenDetail() called with: context = [" + context + "], videoUrl = [" + videoUrl + "]");
|
|
|
|
Intent i = new Intent(context, VideoItemDetailActivity.class);
|
|
|
|
i.putExtra(NavStack.SERVICE_ID, 0)
|
|
|
|
.putExtra(NavStack.URL, videoUrl)
|
|
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
context.startActivity(i);
|
2017-03-31 20:15:26 +02:00
|
|
|
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
|
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-03-27 06:08:16 +02:00
|
|
|
private float getMinimumVideoWidth(float height) {
|
|
|
|
float width = height * (16.0f / 9.0f); // Respect the 16:9 ratio that most videos have
|
|
|
|
if (DEBUG) Log.d(TAG, "getMinimumVideoWidth() called with: height = [" + height + "], returned: " + width);
|
|
|
|
return width;
|
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-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
private class AbstractPlayerImpl extends AbstractPlayer {
|
|
|
|
AbstractPlayerImpl() {
|
|
|
|
super("AbstractPlayerImpl" + PopupVideoPlayer.TAG, PopupVideoPlayer.this);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void playVideo(Uri videoURI, boolean autoPlay) {
|
|
|
|
super.playVideo(videoURI, autoPlay);
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
windowLayoutParams.width = (int) getMinimumVideoWidth(currentPopupHeight);
|
|
|
|
windowManager.updateViewLayout(getRootView(), windowLayoutParams);
|
2017-03-09 08:42:40 +01:00
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
notBuilder = createNotification();
|
|
|
|
startForeground(NOTIFICATION_ID, notBuilder.build());
|
|
|
|
notificationManager.notify(NOTIFICATION_ID, notBuilder.build());
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onFullScreenButtonClicked() {
|
|
|
|
if (DEBUG) Log.d(TAG, "onFullScreenButtonClicked() called");
|
|
|
|
Intent intent;
|
|
|
|
//if (getSharedPreferences().getBoolean(getResources().getString(R.string.use_exoplayer_key), false)) {
|
|
|
|
// TODO: Remove this check when ExoPlayer is the default
|
|
|
|
// For now just disable the non-exoplayer player
|
|
|
|
//noinspection ConstantConditions,ConstantIfStatement
|
|
|
|
if (true) {
|
|
|
|
intent = new Intent(PopupVideoPlayer.this, ExoPlayerActivity.class)
|
|
|
|
.putExtra(AbstractPlayer.VIDEO_TITLE, getVideoTitle())
|
|
|
|
.putExtra(AbstractPlayer.VIDEO_URL, getVideoUrl())
|
|
|
|
.putExtra(AbstractPlayer.CHANNEL_NAME, getChannelName())
|
|
|
|
.putExtra(AbstractPlayer.INDEX_SEL_VIDEO_STREAM, getSelectedIndexStream())
|
|
|
|
.putExtra(AbstractPlayer.VIDEO_STREAMS_LIST, getVideoStreamsList())
|
|
|
|
.putExtra(AbstractPlayer.START_POSITION, ((int) getPlayer().getCurrentPosition()));
|
|
|
|
if (!playerImpl.isStartedFromNewPipe()) intent.putExtra(AbstractPlayer.STARTED_FROM_NEWPIPE, false);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
} else {
|
|
|
|
intent = new Intent(PopupVideoPlayer.this, PlayVideoActivity.class)
|
|
|
|
.putExtra(PlayVideoActivity.VIDEO_TITLE, getVideoTitle())
|
|
|
|
.putExtra(PlayVideoActivity.STREAM_URL, getSelectedStreamUri().toString())
|
|
|
|
.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-03-27 06:08:16 +02:00
|
|
|
context.startActivity(intent);
|
|
|
|
stopSelf();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onRepeatClicked() {
|
|
|
|
super.onRepeatClicked();
|
|
|
|
switch (getCurrentRepeatMode()) {
|
|
|
|
case REPEAT_DISABLED:
|
|
|
|
// Drawable didn't work on low API :/
|
|
|
|
//notRemoteView.setImageViewResource(R.id.notificationRepeat, R.drawable.ic_repeat_disabled_white);
|
|
|
|
// Set the icon to 30% opacity - 255 (max) * .3
|
|
|
|
notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 77);
|
|
|
|
break;
|
|
|
|
case REPEAT_ONE:
|
|
|
|
notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 255);
|
|
|
|
break;
|
|
|
|
case REPEAT_ALL:
|
|
|
|
// Waiting :)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
updateNotification(-1);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
2017-03-09 08:42:40 +01:00
|
|
|
public void onUpdateThumbnail(Intent intent) {
|
2017-03-27 06:08:16 +02:00
|
|
|
super.onUpdateThumbnail(intent);
|
|
|
|
if (getVideoThumbnail() != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, getVideoThumbnail());
|
2017-03-09 08:42:40 +01:00
|
|
|
updateNotification(-1);
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onDismiss(PopupMenu menu) {
|
|
|
|
super.onDismiss(menu);
|
|
|
|
if (isPlaying()) animateView(getControlsRoot(), false, 500, 0);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
@Override
|
|
|
|
public void onError() {
|
|
|
|
Toast.makeText(context, "Failed to play this video", Toast.LENGTH_SHORT).show();
|
|
|
|
stopSelf();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// States
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-03-09 08:42:40 +01:00
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onLoading() {
|
|
|
|
super.onLoading();
|
|
|
|
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();
|
|
|
|
updateNotification(R.drawable.ic_pause_white);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onBuffering() {
|
|
|
|
super.onBuffering();
|
|
|
|
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();
|
|
|
|
updateNotification(R.drawable.ic_play_arrow_white);
|
|
|
|
showAndAnimateControl(R.drawable.ic_play_arrow_white, false);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-27 06:08:16 +02:00
|
|
|
public void onPausedSeek() {
|
|
|
|
super.onPausedSeek();
|
|
|
|
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();
|
|
|
|
updateNotification(R.drawable.ic_replay_white);
|
|
|
|
showAndAnimateControl(R.drawable.ic_replay_white, false);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener {
|
|
|
|
private int initialPopupX, initialPopupY;
|
|
|
|
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-03-27 06:08:16 +02:00
|
|
|
if (!playerImpl.isPlaying()) return false;
|
|
|
|
if (e.getX() > popupWidth / 2) playerImpl.onFastForward();
|
|
|
|
else playerImpl.onFastRewind();
|
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-03-27 06:08:16 +02:00
|
|
|
if (playerImpl.getPlayer() == null) return false;
|
|
|
|
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-03-27 06:08:16 +02:00
|
|
|
popupWidth = playerImpl.getRootView().getWidth();
|
|
|
|
popupHeight = playerImpl.getRootView().getHeight();
|
2017-03-09 08:42:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
2017-03-27 06:08:16 +02:00
|
|
|
if (!isMoving || playerImpl.getControlsRoot().getAlpha() != 1f) playerImpl.animateView(playerImpl.getControlsRoot(), true, 30, 0);
|
2017-03-09 08:42:40 +01:00
|
|
|
isMoving = true;
|
|
|
|
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 + "]" +
|
|
|
|
", popupWh rootView.get wh = [" + 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-03-27 06:08:16 +02:00
|
|
|
if (playerImpl.isControlsVisible() && playerImpl.getCurrentState() == StateInterface.STATE_PLAYING) {
|
|
|
|
playerImpl.animateView(playerImpl.getControlsRoot(), false, 300, AbstractPlayer.DEFAULT_CONTROLS_HIDE_TIME);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
gestureDetector.onTouchEvent(event);
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_UP && isMoving) {
|
|
|
|
isMoving = false;
|
|
|
|
onScrollEnd();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetcher used if open by a link out of NewPipe
|
|
|
|
*/
|
|
|
|
private class FetcherRunnable implements Runnable {
|
|
|
|
private final Intent intent;
|
|
|
|
private final Handler mainHandler;
|
|
|
|
private final boolean printStreams = true;
|
|
|
|
|
|
|
|
|
|
|
|
FetcherRunnable(Intent intent) {
|
|
|
|
this.intent = intent;
|
|
|
|
this.mainHandler = new Handler(PopupVideoPlayer.this.getMainLooper());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
StreamExtractor streamExtractor;
|
|
|
|
try {
|
|
|
|
StreamingService service = NewPipe.getService(0);
|
|
|
|
if (service == null) return;
|
|
|
|
streamExtractor = service.getExtractorInstance(intent.getStringExtra(NavStack.URL));
|
|
|
|
StreamInfo info = StreamInfo.getVideoInfo(streamExtractor);
|
2017-03-27 06:08:16 +02:00
|
|
|
String defaultResolution = playerImpl.getSharedPreferences().getString(
|
2017-03-09 08:42:40 +01:00
|
|
|
getResources().getString(R.string.default_resolution_key),
|
|
|
|
getResources().getString(R.string.default_resolution_value));
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
VideoStream chosen = null, secondary = null, fallback = null;
|
|
|
|
playerImpl.setVideoStreamsList(info.video_streams instanceof ArrayList
|
|
|
|
? (ArrayList<VideoStream>) info.video_streams
|
|
|
|
: new ArrayList<>(info.video_streams));
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
for (VideoStream item : info.video_streams) {
|
|
|
|
if (DEBUG && printStreams) {
|
2017-03-27 06:08:16 +02:00
|
|
|
Log.d(TAG, "FetcherRunnable.StreamExtractor: current Item"
|
2017-03-09 08:42:40 +01:00
|
|
|
+ ", item.resolution = " + item.resolution
|
|
|
|
+ ", item.format = " + item.format
|
|
|
|
+ ", item.url = " + item.url);
|
|
|
|
}
|
|
|
|
if (defaultResolution.equals(item.resolution)) {
|
|
|
|
if (item.format == MediaFormat.MPEG_4.id) {
|
2017-03-27 06:08:16 +02:00
|
|
|
chosen = item;
|
|
|
|
if (DEBUG) Log.d(TAG, "FetcherRunnable.StreamExtractor: CHOSEN item, item.resolution = " + item.resolution + ", item.format = " + item.format + ", item.url = " + item.url);
|
|
|
|
} else if (item.format == 2) secondary = item;
|
|
|
|
else fallback = item;
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
int selectedIndexStream;
|
|
|
|
|
|
|
|
if (chosen != null) selectedIndexStream = info.video_streams.indexOf(chosen);
|
|
|
|
else if (secondary != null) selectedIndexStream = info.video_streams.indexOf(secondary);
|
|
|
|
else if (fallback != null) selectedIndexStream = info.video_streams.indexOf(fallback);
|
|
|
|
else selectedIndexStream = 0;
|
|
|
|
|
|
|
|
playerImpl.setSelectedIndexStream(selectedIndexStream);
|
|
|
|
|
|
|
|
if (DEBUG && printStreams) Log.d(TAG, "FetcherRunnable.StreamExtractor: chosen = " + chosen
|
2017-03-09 08:42:40 +01:00
|
|
|
+ "\n, secondary = " + secondary
|
|
|
|
+ "\n, fallback = " + fallback
|
|
|
|
+ "\n, info.video_streams.get(0).url = " + info.video_streams.get(0).url);
|
|
|
|
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
playerImpl.setVideoUrl(info.webpage_url);
|
|
|
|
playerImpl.setVideoTitle(info.title);
|
|
|
|
playerImpl.setChannelName(info.uploader);
|
|
|
|
if (info.start_position > 0) playerImpl.setVideoStartPos(info.start_position * 1000);
|
|
|
|
else playerImpl.setVideoStartPos(-1);
|
|
|
|
|
2017-03-09 08:42:40 +01:00
|
|
|
mainHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-03-27 06:08:16 +02:00
|
|
|
playerImpl.playVideo(playerImpl.getSelectedStreamUri(), true);
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
imageLoader.loadImage(info.thumbnail_url, displayImageOptions, new SimpleImageLoadingListener() {
|
|
|
|
@Override
|
|
|
|
public void onLoadingComplete(String imageUri, View view, final Bitmap loadedImage) {
|
|
|
|
mainHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-03-27 06:08:16 +02:00
|
|
|
playerImpl.setVideoThumbnail(loadedImage);
|
|
|
|
if (loadedImage != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, loadedImage);
|
2017-03-09 08:42:40 +01:00
|
|
|
updateNotification(-1);
|
2017-03-27 06:08:16 +02:00
|
|
|
ActivityCommunicator.getCommunicator().backgroundPlayerThumbnail = loadedImage;
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-03-17 16:09:20 +01:00
|
|
|
} catch (IOException ie) {
|
|
|
|
if (DEBUG) ie.printStackTrace();
|
|
|
|
mainHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Toast.makeText(PopupVideoPlayer.this, R.string.network_error, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stopSelf();
|
2017-03-09 08:42:40 +01:00
|
|
|
} catch (Exception e) {
|
2017-03-17 15:50:27 +01:00
|
|
|
if (DEBUG) e.printStackTrace();
|
|
|
|
mainHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Toast.makeText(PopupVideoPlayer.this, R.string.content_not_available, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stopSelf();
|
2017-03-09 08:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 06:08:16 +02:00
|
|
|
|
|
|
|
}
|