From 92f5e6e77c42c7dae11586aa73458220bcb5a749 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sat, 1 Sep 2018 22:45:20 +0100 Subject: [PATCH] group the background service and background media player notifications --- app/src/component/AppNavigator.js | 4 ++-- app/src/component/mediaPlayer/view.js | 4 ++-- app/src/page/file/view.js | 9 +++------ .../java/io/lbry/browser/LbrynetService.java | 19 +++++++++++++++++-- .../reactmodules/BackgroundMediaModule.java | 9 ++++++++- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/src/component/AppNavigator.js b/app/src/component/AppNavigator.js index ba69c482..68ee12f5 100644 --- a/app/src/component/AppNavigator.js +++ b/app/src/component/AppNavigator.js @@ -297,8 +297,8 @@ class AppWithNavigationState extends React.Component { // Background media if (backgroundPlayEnabled && NativeModules.BackgroundMedia && window.currentMediaInfo) { - const { title, channel } = window.currentMediaInfo; - NativeModules.BackgroundMedia.showPlaybackNotification(title, channel, null, false); + const { title, channel, uri } = window.currentMediaInfo; + NativeModules.BackgroundMedia.showPlaybackNotification(title, channel, uri, false); } }); } diff --git a/app/src/component/mediaPlayer/view.js b/app/src/component/mediaPlayer/view.js index 9b8e562d..e1e51a98 100644 --- a/app/src/component/mediaPlayer/view.js +++ b/app/src/component/mediaPlayer/view.js @@ -253,8 +253,8 @@ class MediaPlayer extends React.PureComponent { const { backgroundPlayEnabled } = this.props; if (backgroundPlayEnabled) { if (NativeModules.BackgroundMedia && window.currentMediaInfo) { - const { title, channel } = window.currentMediaInfo; - NativeModules.BackgroundMedia.showPlaybackNotification(title, channel, null, this.state.paused); + const { title, channel, uri } = window.currentMediaInfo; + NativeModules.BackgroundMedia.showPlaybackNotification(title, channel, uri, this.state.paused); } } } diff --git a/app/src/page/file/view.js b/app/src/page/file/view.js index 8e96b5d5..475f5e22 100644 --- a/app/src/page/file/view.js +++ b/app/src/page/file/view.js @@ -241,12 +241,9 @@ class FilePage extends React.PureComponent { } } - onMediaLoaded = (title, channelName) => { + onMediaLoaded = (channelName, title, uri) => { this.setState({ mediaLoaded: true }); - window.currentMediaInfo = { - title: title, - channel: channelName - }; + window.currentMediaInfo = { channel: channelName, title, uri }; } onPlaybackStarted = () => { @@ -410,7 +407,7 @@ class FilePage extends React.PureComponent { this.setState({ playerHeight: evt.nativeEvent.layout.height }); } }} - onMediaLoaded={() => this.onMediaLoaded(title, channelName)} + onMediaLoaded={() => this.onMediaLoaded(channelName, title, uri)} onPlaybackStarted={this.onPlaybackStarted} />} diff --git a/src/main/java/io/lbry/browser/LbrynetService.java b/src/main/java/io/lbry/browser/LbrynetService.java index 254f9367..8d860f00 100644 --- a/src/main/java/io/lbry/browser/LbrynetService.java +++ b/src/main/java/io/lbry/browser/LbrynetService.java @@ -39,8 +39,12 @@ public class LbrynetService extends PythonService { private static final String NOTIFICATION_CHANNEL_ID = "io.lbry.browser.DAEMON_NOTIFICATION_CHANNEL"; + private static final int SERVICE_NOTIFICATION_GROUP_ID = -1; + public static final String ACTION_STOP_SERVICE = "io.lbry.browser.ACTION_STOP_SERVICE"; + public static final String GROUP_SERVICE = "io.lbry.browser.GROUP_SERVICE"; + public static String TAG = "LbrynetService"; public static LbrynetService serviceInstance; @@ -86,16 +90,27 @@ public class LbrynetService extends PythonService { } Intent contextIntent = new Intent(context, MainActivity.class); - PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent stopIntent = new Intent(ACTION_STOP_SERVICE); PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent, 0); + // Create the notification group + NotificationCompat.Builder groupBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); + groupBuilder.setContentTitle("LBRY Browser") + .setColor(ContextCompat.getColor(context, R.color.lbrygreen)) + .setSmallIcon(R.drawable.ic_lbry) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setGroup(GROUP_SERVICE) + .setGroupSummary(true); + notificationManager.notify(SERVICE_NOTIFICATION_GROUP_ID, groupBuilder.build()); + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); Notification notification = builder.setColor(ContextCompat.getColor(context, R.color.lbrygreen)) + .setContentIntent(pendingIntent) .setContentTitle(serviceTitle) .setContentText(serviceDescription) - .setContentIntent(pIntent) + .setGroup(GROUP_SERVICE) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_lbry) .setOngoing(true) diff --git a/src/main/java/io/lbry/browser/reactmodules/BackgroundMediaModule.java b/src/main/java/io/lbry/browser/reactmodules/BackgroundMediaModule.java index 55c65d6c..7bae3504 100644 --- a/src/main/java/io/lbry/browser/reactmodules/BackgroundMediaModule.java +++ b/src/main/java/io/lbry/browser/reactmodules/BackgroundMediaModule.java @@ -8,6 +8,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Build; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; @@ -17,12 +18,13 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import io.lbry.browser.LbrynetService; import io.lbry.browser.MainActivity; import io.lbry.browser.R; public class BackgroundMediaModule extends ReactContextBaseJavaModule { - private static final int NOTIFICATION_ID = 900; + private static final int NOTIFICATION_ID = -2; private static final String NOTIFICATION_CHANNEL_ID = "io.lbry.browser.MEDIA_PLAYER_NOTIFICATION_CHANNEL"; @@ -58,6 +60,9 @@ public class BackgroundMediaModule extends ReactContextBaseJavaModule { channelCreated = true; } + Intent contextIntent = new Intent(context, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); + Intent playIntent = new Intent(); playIntent.setAction(ACTION_PLAY); PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, 0, playIntent, 0); @@ -69,8 +74,10 @@ public class BackgroundMediaModule extends ReactContextBaseJavaModule { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); builder.setColor(ContextCompat.getColor(context, R.color.lbrygreen)) + .setContentIntent(pendingIntent) .setContentTitle(title) .setContentText(publisher) + .setGroup(LbrynetService.GROUP_SERVICE) .setOngoing(!paused) .setSmallIcon(paused ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play) .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()