Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement per-MediaItem notification colors on Android #521

Draft
wants to merge 1 commit into
base: major
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class AudioService extends MediaBrowserServiceCompat {
private static int shuffleMode;
private static boolean notificationCreated;

public static void init(Activity activity, boolean resumeOnClick, String androidNotificationChannelName, String androidNotificationChannelDescription, String action, Integer notificationColor, String androidNotificationIcon, boolean androidShowNotificationBadge, boolean androidNotificationClickStartsActivity, boolean androidNotificationOngoing, boolean androidStopForegroundOnPause, Size artDownscaleSize, ServiceListener listener) {
public static void init(Activity activity, boolean resumeOnClick, String androidNotificationChannelName, String androidNotificationChannelDescription, String action, String androidNotificationIcon, boolean androidShowNotificationBadge, boolean androidNotificationClickStartsActivity, boolean androidNotificationOngoing, boolean androidStopForegroundOnPause, Size artDownscaleSize, ServiceListener listener) {
if (running)
throw new IllegalStateException("AudioService already running");
running = true;
Expand All @@ -91,7 +91,6 @@ public static void init(Activity activity, boolean resumeOnClick, String android
AudioService.resumeOnClick = resumeOnClick;
AudioService.androidNotificationChannelName = androidNotificationChannelName;
AudioService.androidNotificationChannelDescription = androidNotificationChannelDescription;
AudioService.notificationColor = notificationColor;
AudioService.androidNotificationIcon = androidNotificationIcon;
AudioService.androidShowNotificationBadge = androidShowNotificationBadge;
AudioService.androidNotificationClickStartsActivity = androidNotificationClickStartsActivity;
Expand Down Expand Up @@ -477,9 +476,10 @@ void playMediaItem(MediaDescriptionCompat description) {
mediaSessionCallback.onPlayMediaItem(description);
}

void setMetadata(final MediaMetadataCompat mediaMetadata) {
void setMetadata(final MediaMetadataCompat mediaMetadata, final Integer color) {
this.mediaMetadata = mediaMetadata;
mediaSession.setMetadata(mediaMetadata);
notificationColor = color;
updateNotification();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ public void onMethodCall(MethodCall call, final Result result) {
boolean androidResumeOnClick = (Boolean)arguments.get("androidResumeOnClick");
String androidNotificationChannelName = (String)arguments.get("androidNotificationChannelName");
String androidNotificationChannelDescription = (String)arguments.get("androidNotificationChannelDescription");
Integer androidNotificationColor = arguments.get("androidNotificationColor") == null ? null : getInt(arguments.get("androidNotificationColor"));
String androidNotificationIcon = (String)arguments.get("androidNotificationIcon");
boolean androidShowNotificationBadge = (Boolean)arguments.get("androidShowNotificationBadge");
final boolean androidEnableQueue = (Boolean)arguments.get("androidEnableQueue");
Expand All @@ -356,7 +355,7 @@ public void onMethodCall(MethodCall call, final Result result) {

final String appBundlePath = FlutterMain.findAppBundlePath(context.getApplicationContext());
backgroundHandler = new BackgroundHandler(callbackHandle, appBundlePath, androidEnableQueue);
AudioService.init(activity, androidResumeOnClick, androidNotificationChannelName, androidNotificationChannelDescription, NOTIFICATION_CLICK_ACTION, androidNotificationColor, androidNotificationIcon, androidShowNotificationBadge ,androidNotificationClickStartsActivity, androidNotificationOngoing, androidStopForegroundOnPause, artDownscaleSize, backgroundHandler);
AudioService.init(activity, androidResumeOnClick, androidNotificationChannelName, androidNotificationChannelDescription, NOTIFICATION_CLICK_ACTION, androidNotificationIcon, androidShowNotificationBadge ,androidNotificationClickStartsActivity, androidNotificationOngoing, androidStopForegroundOnPause, artDownscaleSize, backgroundHandler);

synchronized (connectionCallback) {
if (mediaController != null)
Expand Down Expand Up @@ -793,19 +792,21 @@ public void onMethodCall(MethodCall call, Result result) {
AudioService.instance.notifyChildrenChanged(subscribedParentMediaId);
result.success(true);
break;
case "setMediaItem":
Map<?, ?> rawMediaItem = (Map<?, ?>)call.arguments;
case "setMediaItem": {
List<Object> args = (List<Object>)call.arguments;
Map<?, ?> rawMediaItem = (Map<?, ?>)args.get(0);
MediaMetadataCompat mediaMetadata = createMediaMetadata(rawMediaItem);
AudioService.instance.setMetadata(mediaMetadata);
AudioService.instance.setMetadata(mediaMetadata, getInt(args.get(1)));
result.success(true);
break;
}
case "setQueue":
List<Map<?, ?>> rawQueue = (List<Map<?, ?>>)call.arguments;
List<MediaSessionCompat.QueueItem> queue = raw2queue(rawQueue);
AudioService.instance.setQueue(queue);
result.success(true);
break;
case "setState":
case "setState": {
List<Object> args = (List<Object>)call.arguments;
List<Map<?, ?>> rawControls = (List<Map<?, ?>>)args.get(0);
List<Integer> rawSystemActions = (List<Integer>)args.get(1);
Expand Down Expand Up @@ -844,6 +845,7 @@ public void onMethodCall(MethodCall call, Result result) {
AudioService.instance.setState(actions, actionBits, compactActionIndices, processingState, playing, position, bufferedPosition, speed, updateTimeSinceBoot, repeatMode, shuffleMode);
result.success(true);
break;
}
case "stopped":
clear();
result.success(true);
Expand Down
17 changes: 10 additions & 7 deletions lib/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ class AudioService {
Map<String, dynamic> params,
String androidNotificationChannelName = "Notifications",
String androidNotificationChannelDescription,
int androidNotificationColor,
String androidNotificationIcon = 'mipmap/ic_launcher',
bool androidShowNotificationBadge = false,
bool androidNotificationClickStartsActivity = true,
Expand Down Expand Up @@ -819,7 +818,6 @@ class AudioService {
'androidNotificationChannelName': androidNotificationChannelName,
'androidNotificationChannelDescription':
androidNotificationChannelDescription,
'androidNotificationColor': androidNotificationColor,
'androidNotificationIcon': androidNotificationIcon,
'androidShowNotificationBadge': androidShowNotificationBadge,
'androidNotificationClickStartsActivity':
Expand Down Expand Up @@ -1486,7 +1484,11 @@ class AudioServiceBackground {
}

/// Sets the currently playing media item and notifies all clients.
static Future<void> setMediaItem(MediaItem mediaItem) async {
///
/// [color] is the dominant color of the media item.
/// It is used in the service notification on supported platforms, and may be
/// left unset to let the system choose its own color.
static Future<void> setMediaItem(MediaItem mediaItem, [Color color]) async {
_mediaItem = mediaItem;
if (mediaItem.artUri != null) {
// We potentially need to fetch the art.
Expand All @@ -1497,8 +1499,8 @@ class AudioServiceBackground {
if (filePath == null) {
// We haven't fetched the art yet, so show the metadata now, and again
// after we load the art.
await _backgroundChannel.invokeMethod(
'setMediaItem', mediaItem.toJson());
await _backgroundChannel
.invokeMethod('setMediaItem', [mediaItem.toJson(), color?.value]);
// Load the art
filePath = await _loadArtwork(mediaItem);
// If we failed to download the art, abort.
Expand All @@ -1512,9 +1514,10 @@ class AudioServiceBackground {
final platformMediaItem = mediaItem.copyWith(extras: extras);
// Show the media item after the art is loaded.
await _backgroundChannel.invokeMethod(
'setMediaItem', platformMediaItem.toJson());
'setMediaItem', [platformMediaItem.toJson(), color?.value]);
} else {
await _backgroundChannel.invokeMethod('setMediaItem', mediaItem.toJson());
await _backgroundChannel
.invokeMethod('setMediaItem', [mediaItem.toJson(), color?.value]);
}
}

Expand Down