Skip to content

Commit c683d61

Browse files
committed
Update media playback service and permissions
- Added a new service for media playback in `AndroidManifest.xml` with intent filters for media button actions and media session service. - Changed default value of `AndroidForegroundServiceEnabled` to `false` in `MediaElementOptions.shared.cs`. - Removed unnecessary permission attributes from `MauiMediaElement.android.cs`. - Updated test cases in `AppBuilderExtensionsTests.cs` to reflect the new default value for `AndroidForegroundServiceEnabled`.
1 parent 44a9928 commit c683d61

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

samples/CommunityToolkit.Maui.Sample/Platforms/Android/AndroidManifest.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@
44
android:supportsRtl="true">
55

66
<meta-data android:name="com.google.android.geo.API_KEY" android:value="PASTE-YOUR-API-KEY-HERE" />
7+
8+
<service android:name="communityToolkit.maui.media.services" android:stopWithTask="true" android:exported="false" android:enabled="true"
9+
android:foregroundServiceType="mediaPlayback">
10+
<intent-filter>
11+
<action android:name="android.intent.action.MEDIA_BUTTON"/>
12+
</intent-filter>
13+
<intent-filter>
14+
<action android:name="androidx.media3.session.MediaSessionService"/>
15+
</intent-filter>
16+
</service>
717
</application>
818

919
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1020
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
1121
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1222

23+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
24+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
25+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
26+
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
1327
<!-- Samsung -->
1428
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
1529
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>

src/CommunityToolkit.Maui.MediaElement/MediaElementOptions.shared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal MediaElementOptions(in MauiAppBuilder builder) : this()
2020
/// <summary>
2121
/// Set Android Foreground Service for MediaElement on construction
2222
/// </summary>
23-
internal static bool AndroidForegroundServiceEnabled { get; private set; } = true;
23+
internal static bool AndroidForegroundServiceEnabled { get; private set; } = false;
2424

2525
/// <summary>
2626
/// Set Android View type for MediaElement as SurfaceView or TextureView on construction

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
using AndroidX.Media3.UI;
99
using CommunityToolkit.Maui.Views;
1010

11-
[assembly: UsesPermission(Android.Manifest.Permission.ForegroundServiceMediaPlayback)]
12-
[assembly: UsesPermission(Android.Manifest.Permission.ForegroundService)]
13-
[assembly: UsesPermission(Android.Manifest.Permission.MediaContentControl)]
14-
[assembly: UsesPermission(Android.Manifest.Permission.PostNotifications)]
15-
1611
namespace CommunityToolkit.Maui.Core.Views;
1712

1813
/// <summary>

src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ public void UseMauiCommunityToolkitMediaElement_ShouldSetDefaultAndroidViewType(
141141
}
142142

143143
[Fact]
144-
public void UseMauiCommunityToolkitMediaElement_ShouldSetAndroidServiceByDefault()
144+
public void UseMauiCommunityToolkitMediaElement_ShouldSetAndroidServiceByDefaultToFalse()
145145
{
146146
var builder = MauiApp.CreateBuilder();
147147
builder.UseMauiCommunityToolkitMediaElement();
148-
MediaElementOptions.AndroidForegroundServiceEnabled.Should().Be(true);
148+
MediaElementOptions.AndroidForegroundServiceEnabled.Should().Be(false);
149149
}
150150

151151
[Fact]
@@ -154,9 +154,9 @@ public void UseMauiCommunityToolkitMediaElement_ServiceCanBeDisabled()
154154
var builder = MauiApp.CreateBuilder();
155155
builder.UseMauiCommunityToolkitMediaElement(static options =>
156156
{
157-
options.SetDefaultAndroidForegroundService(false);
157+
options.SetDefaultAndroidForegroundService(true);
158158
});
159-
MediaElementOptions.AndroidForegroundServiceEnabled.Should().Be(false);
159+
MediaElementOptions.AndroidForegroundServiceEnabled.Should().Be(true);
160160
}
161161
}
162162
#pragma warning restore CA1416

0 commit comments

Comments
 (0)