-
Notifications
You must be signed in to change notification settings - Fork 410
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
Fix crash when multiple Android Service are active #2418
Open
mr5z
wants to merge
2
commits into
CommunityToolkit:main
Choose a base branch
from
mr5z:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,15 +18,20 @@ | |
namespace CommunityToolkit.Maui.Media.Services; | ||
|
||
[SupportedOSPlatform("Android26.0")] | ||
[Service(Exported = false, Enabled = true, Name = "communityToolkit.maui.media.services", ForegroundServiceType = ForegroundService.TypeMediaPlayback)] | ||
[Service(Exported = false, Enabled = true, Name = "CommunityToolkit.Maui.Media.Services", ForegroundServiceType = ForegroundService.TypeMediaPlayback)] | ||
class MediaControlsService : Service | ||
{ | ||
public const string ACTION_PLAY = "MediaAction.play"; | ||
public const string ACTION_PAUSE = "MediaAction.pause"; | ||
public const string ACTION_UPDATE_UI = "CommunityToolkit.Maui.Services.action.UPDATE_UI"; | ||
public const string ACTION_UPDATE_PLAYER = "CommunityToolkit.Maui.Services.action.UPDATE_PLAYER"; | ||
public const string ACTION_REWIND = "MediaAction.rewind"; | ||
public const string ACTION_FASTFORWARD = "MediaAction.fastForward"; | ||
public const string ActionPlay = "MediaAction.play"; | ||
public const string ActionPause = "MediaAction.pause"; | ||
public const string ActionUpdateUI = "CommunityToolkit.Maui.Services.action.UPDATE_UI"; | ||
public const string ActionUpdatePlayer = "CommunityToolkit.Maui.Services.action.UPDATE_PLAYER"; | ||
public const string ActionRewind = "MediaAction.rewind"; | ||
public const string ActionFastForward = "MediaAction.fastForward"; | ||
Comment on lines
+24
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the pattern? Can you revert and follow our pattern |
||
|
||
public const string NotificationChannelId = "Maui.MediaElement"; | ||
public const string NotificationChannelName = "Transport Controls"; | ||
|
||
public const int NotificationId = 2024; | ||
|
||
bool isDisposed; | ||
|
||
|
@@ -50,7 +55,7 @@ public override StartCommandResult OnStartCommand([NotNull] Intent? intent, Star | |
|
||
if (!string.IsNullOrEmpty(intent.Action) && receiveUpdates is not null) | ||
{ | ||
BroadcastUpdate(ACTION_UPDATE_PLAYER, intent.Action); | ||
BroadcastUpdate(ActionUpdatePlayer, intent.Action); | ||
} | ||
|
||
StartForegroundService(intent).AsTask().ContinueWith(t => | ||
|
@@ -69,7 +74,7 @@ public override StartCommandResult OnStartCommand([NotNull] Intent? intent, Star | |
|
||
static void CreateNotificationChannel(NotificationManager notificationMnaManager) | ||
{ | ||
var channel = new NotificationChannel("1", "notification", NotificationImportance.Low); | ||
var channel = new NotificationChannel(NotificationChannelId, NotificationChannelName, NotificationImportance.Low); | ||
notificationMnaManager.CreateNotificationChannel(channel); | ||
} | ||
|
||
|
@@ -91,7 +96,7 @@ ValueTask StartForegroundService(Intent mediaManagerIntent, CancellationToken ca | |
{ | ||
receiveUpdates = new ReceiveUpdates(); | ||
receiveUpdates.PropertyChanged += OnReceiveUpdatesPropertyChanged; | ||
LocalBroadcastManager.GetInstance(this).RegisterReceiver(receiveUpdates, new IntentFilter(ACTION_UPDATE_UI)); | ||
LocalBroadcastManager.GetInstance(this).RegisterReceiver(receiveUpdates, new IntentFilter(ActionUpdateUI)); | ||
} | ||
|
||
OnSetupAudioServices(); | ||
|
@@ -112,20 +117,15 @@ async ValueTask InitializeNotification(MediaSessionCompat mediaSession, Intent m | |
var style = new AndroidX.Media.App.NotificationCompat.MediaStyle(); | ||
style.SetMediaSession(token); | ||
|
||
if (Build.VERSION.SdkInt >= BuildVersionCodes.S) | ||
{ | ||
style.SetShowActionsInCompactView(0, 1, 2, 3); | ||
} | ||
|
||
if (Build.VERSION.SdkInt < BuildVersionCodes.Tiramisu | ||
&& notification is null) | ||
{ | ||
notification = new NotificationCompat.Builder(Platform.AppContext, "1"); | ||
notification = new NotificationCompat.Builder(Platform.AppContext, NotificationChannelId); | ||
OnSetIntents(); | ||
await OnSetContent(mediaManagerIntent, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
notification ??= new NotificationCompat.Builder(Platform.AppContext, "1"); | ||
notification ??= new NotificationCompat.Builder(Platform.AppContext, NotificationChannelId); | ||
|
||
notification.SetStyle(style); | ||
notification.SetSmallIcon(_Microsoft.Android.Resource.Designer.Resource.Drawable.exo_styled_controls_audiotrack); | ||
|
@@ -142,13 +142,13 @@ async ValueTask InitializeNotification(MediaSessionCompat mediaSession, Intent m | |
|
||
if (OperatingSystem.IsAndroidVersionAtLeast(29)) | ||
{ | ||
StartForeground(1, notification.Build(), ForegroundService.TypeMediaPlayback); | ||
StartForeground(NotificationId, notification.Build(), ForegroundService.TypeMediaPlayback); | ||
return; | ||
} | ||
|
||
if (OperatingSystem.IsAndroidVersionAtLeast(26)) | ||
{ | ||
StartForeground(1, notification.Build()); | ||
StartForeground(NotificationId, notification.Build()); | ||
} | ||
} | ||
|
||
|
@@ -160,16 +160,15 @@ void OnReceiveUpdatesPropertyChanged(object? sender, PropertyChangedEventArgs e) | |
} | ||
notification.ClearActions(); | ||
notification.AddAction(actionPrevious); | ||
if (receiveUpdates.Action is ACTION_PLAY) | ||
if (receiveUpdates.Action is ActionPlay) | ||
{ | ||
notification.AddAction(actionPause); | ||
} | ||
if (receiveUpdates.Action is ACTION_PAUSE) | ||
if (receiveUpdates.Action is ActionPause) | ||
{ | ||
notification.AddAction(actionPlay); | ||
} | ||
notification.AddAction(actionNext); | ||
notification.Build(); | ||
} | ||
|
||
void OnSetupAudioServices() | ||
|
@@ -195,24 +194,24 @@ async Task OnSetContent(Intent mediaManagerIntent, CancellationToken cancellatio | |
void OnSetIntents() | ||
{ | ||
var pause = new Intent(this, typeof(MediaControlsService)); | ||
pause.SetAction("MediaAction.pause"); | ||
pause.SetAction(ActionPause); | ||
var pPause = PendingIntent.GetService(this, 1, pause, pendingIntentFlags); | ||
actionPause ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_pause, ACTION_PAUSE, pPause).Build(); | ||
actionPause ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_pause, ActionPause, pPause).Build(); | ||
|
||
var play = new Intent(this, typeof(MediaControlsService)); | ||
play.SetAction("MediaAction.play"); | ||
play.SetAction(ActionPlay); | ||
var pPlay = PendingIntent.GetService(this, 1, play, pendingIntentFlags); | ||
actionPlay ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_play, ACTION_PLAY, pPlay).Build(); | ||
actionPlay ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_play, ActionPlay, pPlay).Build(); | ||
|
||
var previous = new Intent(this, typeof(MediaControlsService)); | ||
previous.SetAction("MediaAction.rewind"); | ||
previous.SetAction(ActionRewind); | ||
var pPrevious = PendingIntent.GetService(this, 1, previous, pendingIntentFlags); | ||
actionPrevious ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_rewind, ACTION_REWIND, pPrevious).Build(); | ||
actionPrevious ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_rewind, ActionRewind, pPrevious).Build(); | ||
|
||
var next = new Intent(this, typeof(MediaControlsService)); | ||
next.SetAction("MediaAction.fastForward"); | ||
next.SetAction(ActionFastForward); | ||
var pNext = PendingIntent.GetService(this, 1, next, pendingIntentFlags); | ||
actionNext ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_fastforward, ACTION_FASTFORWARD, pNext).Build(); | ||
actionNext ??= new NotificationCompat.Action.Builder(Resource.Drawable.exo_controls_fastforward, ActionFastForward, pNext).Build(); | ||
|
||
notification?.AddAction(actionPrevious); | ||
notification?.AddAction(actionPause); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the case to uppercase will break older API's compatibility and prevent app from running on Android 23 or below. We only support android 26+ now on media element but I do want to point out this would break some current apps. Also changing this here requires changing the string in the manifest to match which has not been done.