From 7dadb2b26c7b3409527da3d1a0dc5fca58e9c99a Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sat, 2 Jan 2016 16:08:18 +0100 Subject: [PATCH] fixed close notification problem --- .../org/schabi/newpipe/BackgroundPlayer.java | 144 +++++++++++------- .../res/drawable-hdpi/ic_close_white_24dp.png | Bin 0 -> 221 bytes .../res/drawable-mdpi/ic_close_white_24dp.png | Bin 0 -> 175 bytes .../drawable-xhdpi/ic_close_white_24dp.png | Bin 0 -> 257 bytes .../drawable-xxhdpi/ic_close_white_24dp.png | Bin 0 -> 347 bytes .../drawable-xxxhdpi/ic_close_white_24dp.png | Bin 0 -> 436 bytes .../main/res/layout/player_notification.xml | 66 ++++++++ .../layout/player_notification_expanded.xml | 80 ++++++++++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 1 + 10 files changed, 240 insertions(+), 52 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_close_white_24dp.png create mode 100644 app/src/main/res/drawable-mdpi/ic_close_white_24dp.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_close_white_24dp.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_close_white_24dp.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_close_white_24dp.png create mode 100644 app/src/main/res/layout/player_notification.xml create mode 100644 app/src/main/res/layout/player_notification_expanded.xml diff --git a/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java index 5573e2771..f9caef26a 100644 --- a/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java @@ -17,6 +17,7 @@ import android.os.IBinder; import android.os.PowerManager; import android.support.v7.app.NotificationCompat; import android.util.Log; +import android.widget.RemoteViews; import android.widget.Toast; import java.io.IOException; @@ -113,9 +114,9 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare private int noteID = TAG.hashCode(); private BackgroundPlayer owner; private NotificationManager noteMgr; - private NotificationCompat.Builder noteBuilder; private WifiManager.WifiLock wifiLock; private Bitmap videoThumbnail = null; + private NotificationCompat.Builder noteBuilder; public PlayerThread(String src, String title, BackgroundPlayer owner) { this.source = src; @@ -124,10 +125,9 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } + @Override public void run() { - Resources res = getApplicationContext().getResources(); - mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock try { mediaPlayer.setDataSource(source); @@ -177,54 +177,7 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare filter.addAction(ACTION_STOP); registerReceiver(broadcastReceiver, filter); - PendingIntent playPI = PendingIntent.getBroadcast(owner, noteID, - new Intent(ACTION_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT); - - NotificationCompat.Action playButton = new NotificationCompat.Action.Builder - (R.drawable.ic_play_arrow_white_48dp, "Play", playPI).build(); - - /* - NotificationCompat.Action pauseButton = new NotificationCompat.Action.Builder - (R.drawable.ic_pause_white_24dp, "Pause", playPI).build(); - */ - - PendingIntent stopPI = PendingIntent.getBroadcast(owner, noteID, - new Intent(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT); - - noteBuilder = new NotificationCompat.Builder(owner); - noteBuilder - .setContentTitle(title) - //really? Id like to put something more helpful here. - //.setContentText("NewPipe is playing in the background") - .setContentText(channelName) - //.setAutoCancel(!mediaPlayer.isPlaying()) - .setOngoing(true) - .setDeleteIntent(stopPI) - //doesn't fit with Notification.MediaStyle - //.setProgress(vidLength, 0, false) - .setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp) - .setLargeIcon(videoThumbnail) - .setTicker( - String.format(res.getString( - R.string.backgroundPlayerTickerText), title)) - .addAction(playButton); - //.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - //.setLargeIcon(cover) - if(android.os.Build.VERSION.SDK_INT >= 16) - noteBuilder.setPriority(Notification.PRIORITY_LOW); - if(android.os.Build.VERSION.SDK_INT >= 21) - noteBuilder.setCategory(Notification.CATEGORY_TRANSPORT); - - noteBuilder.setStyle(new NotificationCompat.MediaStyle() - //.setMediaSession(mMediaSession.getSessionToken()) - .setShowActionsInCompactView(new int[]{0}) - .setShowCancelButton(true) - .setCancelButtonIntent(stopPI)); - if(videoThumbnail != null) { - noteBuilder.setLargeIcon(videoThumbnail); - } - - Notification note = noteBuilder.build(); + Notification note = buildNotification(); Intent openDetailView = new Intent(getApplicationContext(), VideoItemDetailActivity.class); @@ -252,7 +205,6 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare Log.d(TAG, "sleep failure"); } }*/ - } private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @@ -306,5 +258,93 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare afterPlayCleanup(); } } + + private Notification buildNotification() { + Notification note; + Resources res = getApplicationContext().getResources(); + noteBuilder = new NotificationCompat.Builder(owner); + + PendingIntent playPI = PendingIntent.getBroadcast(owner, noteID, + new Intent(ACTION_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent stopPI = PendingIntent.getBroadcast(owner, noteID, + new Intent(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT); + /* + NotificationCompat.Action pauseButton = new NotificationCompat.Action.Builder + (R.drawable.ic_pause_white_24dp, "Pause", playPI).build(); + */ + + noteBuilder + .setOngoing(true) + .setDeleteIntent(stopPI) + //doesn't fit with Notification.MediaStyle + //.setProgress(vidLength, 0, false) + .setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp) + .setTicker( + String.format(res.getString( + R.string.backgroundPlayerTickerText), title)); + + if (android.os.Build.VERSION.SDK_INT < 21) { + + NotificationCompat.Action playButton = new NotificationCompat.Action.Builder + (R.drawable.ic_play_arrow_white_48dp, + res.getString(R.string.play), playPI).build(); + + noteBuilder + .setContentTitle(title) + //really? Id like to put something more helpful here. + //.setContentText("NewPipe is playing in the background") + .setContentText(channelName) + //.setAutoCancel(!mediaPlayer.isPlaying()) + .setDeleteIntent(stopPI) + //doesn't fit with Notification.MediaStyle + //.setProgress(vidLength, 0, false) + .setLargeIcon(videoThumbnail) + .addAction(playButton); + //.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + //.setLargeIcon(cover) + + if (android.os.Build.VERSION.SDK_INT >= 16) + noteBuilder.setPriority(Notification.PRIORITY_LOW); + + noteBuilder.setStyle(new NotificationCompat.MediaStyle() + //.setMediaSession(mMediaSession.getSessionToken()) + .setShowActionsInCompactView(new int[]{0}) + .setShowCancelButton(true) + .setCancelButtonIntent(stopPI)); + if (videoThumbnail != null) { + noteBuilder.setLargeIcon(videoThumbnail); + } + note = noteBuilder.build(); + } else { + RemoteViews view = + new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification); + view.setImageViewBitmap(R.id.backgroundCover, videoThumbnail); + view.setTextViewText(R.id.backgroundSongName, title); + view.setTextViewText(R.id.backgroundArtist, channelName); + view.setOnClickPendingIntent(R.id.backgroundStop, stopPI); + view.setOnClickPendingIntent(R.id.backgroundPlayPause, playPI); + + RemoteViews expandedView = + new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification); + expandedView.setImageViewBitmap(R.id.backgroundCover, videoThumbnail); + expandedView.setTextViewText(R.id.backgroundSongName, title); + expandedView.setTextViewText(R.id.backgroundArtist, channelName); + expandedView.setOnClickPendingIntent(R.id.backgroundStop, stopPI); + expandedView.setOnClickPendingIntent(R.id.backgroundPlayPause, playPI); + + noteBuilder.setCategory(Notification.CATEGORY_TRANSPORT); + + //Make notification appear on lockscreen + noteBuilder.setVisibility(Notification.VISIBILITY_PUBLIC); + + note = noteBuilder.build(); + note.contentView = view; + + //todo: This never shows up. I was not able to figure out why: + note.bigContentView = expandedView; + } + + return note; + } } } diff --git a/app/src/main/res/drawable-hdpi/ic_close_white_24dp.png b/app/src/main/res/drawable-hdpi/ic_close_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..ceb1a1eebf2b2cc9a008f42010e144f4dab968de GIT binary patch literal 221 zcmV<303!d1P)og+*{ z>6z1@lfD*AYSPav72B5kK^|J))MUC zlz7ydwCsb8xn}W?^r?}5wtsjdmvX{3=JbT!J9sKPrYmk={m|7^>A|Z*7j^D$`>}ry Zqi^Z9GQN4ct^!@a;OXk;vd$@?2>?I&LQeny literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_close_white_24dp.png b/app/src/main/res/drawable-xhdpi/ic_close_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..b7c7ffd0e795ba76ed3a062566c9016448795f7a GIT binary patch literal 257 zcmV+c0sj7pP) zOA5m<3jYBNi%A&<@ZO?$P9};P4Y5CjG5$M&YXI45J{s}~# zf|&?x1_gn4B7+hS@X!l}&!voFhmZP^sujifL@~PKMMM~{6xH}^g$q7WOzwCQ5vHTU z6`v~H@rlA8e;CUh_(b84zg=+ih`wG<)HiJjzSlQx5#CnjMR;A)R^jtaTa9;7rSy)7O%~`cm?ZjXImW?6TYRT<;U^@VKiSj`soFk00000NkvXX Hu0mjfhD&W| literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_close_white_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_close_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..6b717e0dda8649aa3b5f1d6851ba0dd20cc4ea66 GIT binary patch literal 347 zcmV-h0i^zkP)vMkH8EXyh{JTRaptiwcq@D(P;=ehLbl?EMKm*m7(@7_siS}}k zNFtnck{HL4mc!ypcyUoqJV~4rM^fS3C#iAnkyJU3biCmDy`VZLOv=LXld^HVq32ETvhYyB39D^lU%DgQgv&#U%8l^-CA%qY@2qA=!B5=cm zUgehujQG*l{{`@rPr!f|fEk^>KI9WteE@ilV6HEl&_rJ@p_zU*;T}RirIc{5NocNLm*7JGdV(AM zYYDFOvk5~8{Z*t_>U=!XvoehUSEh=adIga45oe)B9kGgT}7UT3Cirmr(oHPv^YQ1-p=Hlh5u;xggf zX{&yw+El-OrrKQJRl@b7x{HLmNkj95`a#LLnW{Veb2C+!`ppt#r)=g4@?c8RY{yJj~W@W|h^mU4q`i)2y~Rw@J`jIh$1%|In!~{Tb{nMqaxlgb+dq eA%qY@zJ@mriVM?qfwL0;0000 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/player_notification_expanded.xml b/app/src/main/res/layout/player_notification_expanded.xml new file mode 100644 index 000000000..3fd379a6b --- /dev/null +++ b/app/src/main/res/layout/player_notification_expanded.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index e14ee9606..7e0d71406 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -7,4 +7,5 @@ #efff #6000 #EEEEEE + #323232 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 177280277..ffa004d91 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,6 +53,7 @@ %1$s - NewPipe Playing in background https://www.c3s.cc/ + Play Video preview thumbnail