From 9413856463505c3afcf21d51f84ad5e212e779fe Mon Sep 17 00:00:00 2001 From: John Zhen M Date: Sat, 9 Sep 2017 16:33:51 -0700 Subject: [PATCH] -Added back notification on popup player. --- .../newpipe/player/PopupVideoPlayer.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index 8614b7398..7fd03df6c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -119,7 +119,10 @@ public class PopupVideoPlayer extends Service { private float minimumWidth, minimumHeight; private float maximumWidth, maximumHeight; + private final String setAlphaMethodName = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) ? "setImageAlpha" : "setAlpha"; private NotificationManager notificationManager; + private NotificationCompat.Builder notBuilder; + private RemoteViews notRemoteView; private VideoPlayerImpl playerImpl; private Disposable currentWorker; @@ -241,6 +244,58 @@ public class PopupVideoPlayer extends Service { windowManager.addView(rootView, windowLayoutParams); } + /*////////////////////////////////////////////////////////////////////////// + // Notification + //////////////////////////////////////////////////////////////////////////*/ + + 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.notificationContent, + 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.simpleExoPlayer.getRepeatMode()) { + case Player.REPEAT_MODE_OFF: + notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 77); + break; + case Player.REPEAT_MODE_ONE: + //todo change image + notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 168); + break; + case Player.REPEAT_MODE_ALL: + notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 255); + break; + } + + return new NotificationCompat.Builder(this, getString(R.string.notification_channel_id)) + .setOngoing(true) + .setSmallIcon(R.drawable.ic_play_arrow_white) + .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()); + } + /*////////////////////////////////////////////////////////////////////////// // Misc //////////////////////////////////////////////////////////////////////////*/ @@ -341,11 +396,20 @@ public class PopupVideoPlayer extends Service { @Override public void destroy() { super.destroy(); + if (notRemoteView != null) notRemoteView.setImageViewBitmap(R.id.notificationCover, null); } @Override public void onThumbnailReceived(Bitmap thumbnail) { super.onThumbnailReceived(thumbnail); + 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); + } } @Override @@ -371,6 +435,27 @@ public class PopupVideoPlayer extends Service { stopSelf(); } + @Override + public void onRepeatClicked() { + super.onRepeatClicked(); + switch (simpleExoPlayer.getRepeatMode()) { + case Player.REPEAT_MODE_OFF: + // 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 Player.REPEAT_MODE_ONE: + // todo change image + notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 168); + break; + case Player.REPEAT_MODE_ALL: + notRemoteView.setInt(R.id.notificationRepeat, setAlphaMethodName, 255); + break; + } + updateNotification(-1); + } + @Override public void onDismiss(PopupMenu menu) { super.onDismiss(menu); @@ -441,32 +526,38 @@ public class PopupVideoPlayer extends Service { @Override public void onLoading() { super.onLoading(); + updateNotification(R.drawable.ic_play_arrow_white); } @Override public void onPlaying() { super.onPlaying(); + updateNotification(R.drawable.ic_pause_white); } @Override public void onBuffering() { super.onBuffering(); + updateNotification(R.drawable.ic_play_arrow_white); } @Override public void onPaused() { super.onPaused(); + updateNotification(R.drawable.ic_play_arrow_white); showAndAnimateControl(R.drawable.ic_play_arrow_white, false); } @Override public void onPausedSeek() { super.onPausedSeek(); + updateNotification(R.drawable.ic_play_arrow_white); } @Override public void onCompleted() { super.onCompleted(); + updateNotification(R.drawable.ic_replay_white); showAndAnimateControl(R.drawable.ic_replay_white, false); }