Skip to content

Commit 4106c25

Browse files
committed
Switch to using using OptIn and move from options to builder to set service on/off
1 parent af24853 commit 4106c25

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

samples/CommunityToolkit.Maui.Sample/MauiProgram.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ public static MauiApp CreateMauiApp()
6868
#endif
6969
.UseMauiCommunityToolkitMarkup()
7070
.UseMauiCommunityToolkitCamera()
71-
.UseMauiCommunityToolkitMediaElement(static options =>
72-
{
73-
options.SetDefaultAndroidForegroundServiceEnabled(false);
74-
options.SetDefaultAndroidViewType(AndroidViewType.TextureView);
75-
})
71+
.UseMauiCommunityToolkitMediaElement(
72+
static options =>
73+
{
74+
options.SetDefaultAndroidViewType(AndroidViewType.TextureView);
75+
},
76+
enableForegroundService: true)
7677
.ConfigureMauiHandlers(static handlers =>
7778
{
7879
#if IOS || MACCATALYST

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ public static class AppBuilderExtensions
2121
/// </summary>
2222
/// <param name="builder"><see cref="MauiAppBuilder"/> generated by <see cref="MauiApp"/>.</param>
2323
/// <param name="options"><see cref="MediaElementOptions"/>.</param>
24+
/// <param name="enableForegroundService">Enable Android Foreground Service for MediaElement.</param>
2425
/// <returns><see cref="MauiAppBuilder"/> initialized for <see cref="MediaElement"/>.</returns>
25-
public static MauiAppBuilder UseMauiCommunityToolkitMediaElement(this MauiAppBuilder builder, Action<MediaElementOptions>? options = null)
26+
public static MauiAppBuilder UseMauiCommunityToolkitMediaElement(this MauiAppBuilder builder, Action<MediaElementOptions>? options = null, bool enableForegroundService = false)
2627
{
28+
// Create MediaElementOptions and set the foreground service setting
29+
var mediaElementOptions = new MediaElementOptions(builder);
30+
mediaElementOptions.EnableAndroidForegroundService(enableForegroundService);
31+
2732
// Update the default MediaElementOptions for MediaElement if Action is not null
28-
options?.Invoke(new MediaElementOptions(builder));
33+
options?.Invoke(mediaElementOptions);
2934

3035
// Perform Handler configuration
3136
builder.ConfigureMauiHandlers(h =>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ internal MediaElementOptions(in MauiAppBuilder builder) : this()
2121
this.builder = builder;
2222
}
2323

24-
/// <summary>
25-
/// Set Android Foreground Service for MediaElement on construction
26-
/// </summary>
27-
internal static bool IsAndroidForegroundServiceEnabled { get; private set; } = true;
28-
2924
/// <summary>
3025
/// Set Android View type for MediaElement as SurfaceView or TextureView on construction
3126
/// </summary>
3227
internal static AndroidViewType DefaultAndroidViewType { get; private set; } = AndroidViewType.SurfaceView;
3328

3429
/// <summary>
35-
/// Set Android Foreground Service for MediaElement on construction
30+
/// Set whether Android Foreground Service is enabled for MediaElement
3631
/// </summary>
37-
/// <param name="isEnabled"></param>
38-
public void SetDefaultAndroidForegroundServiceEnabled(bool isEnabled) => IsAndroidForegroundServiceEnabled = isEnabled;
32+
internal static bool IsAndroidForegroundServiceEnabled { get; private set; }
3933

4034
/// <summary>
4135
/// Set Android View type for MediaElement as SurfaceView or TextureView on construction
4236
/// </summary>
4337
public void SetDefaultAndroidViewType(AndroidViewType androidViewType) => DefaultAndroidViewType = androidViewType;
38+
39+
/// <summary>
40+
/// Enable Android Foreground Service for MediaElement
41+
/// </summary>
42+
/// <param name="enabled">True to enable foreground service, false to disable</param>
43+
internal void EnableAndroidForegroundService(bool enabled) => IsAndroidForegroundServiceEnabled = enabled;
4444
}

src/CommunityToolkit.Maui.UnitTests/BaseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected virtual void Dispose(bool isDisposing)
9090
// Restore default MediaElementOptions
9191
var mediaElementOptions = new MediaElementOptions();
9292
mediaElementOptions.SetDefaultAndroidViewType(AndroidViewType.SurfaceView);
93-
mediaElementOptions.SetDefaultAndroidForegroundServiceEnabled(true);
93+
mediaElementOptions.EnableAndroidForegroundService(true);
9494
isDisposed = true;
9595
}
9696

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void UseMauiCommunityToolkitMediaElement_ShouldSetAndroidServiceByDefault
172172
{
173173
var builder = MauiApp.CreateBuilder();
174174
builder.UseMauiCommunityToolkitMediaElement();
175-
MediaElementOptions.IsAndroidForegroundServiceEnabled.Should().Be(true);
175+
MediaElementOptions.IsAndroidForegroundServiceEnabled.Should().Be(false);
176176
}
177177

178178
[Fact]
@@ -181,7 +181,7 @@ public void UseMauiCommunityToolkitMediaElement_ServiceCanBeDisabled()
181181
var builder = MauiApp.CreateBuilder();
182182
builder.UseMauiCommunityToolkitMediaElement(static options =>
183183
{
184-
options.SetDefaultAndroidForegroundServiceEnabled(false);
184+
options.EnableAndroidForegroundService(false);
185185
});
186186
MediaElementOptions.IsAndroidForegroundServiceEnabled.Should().Be(false);
187187
}

src/CommunityToolkit.Maui.UnitTests/Views/MediaElement/MediaElementOptionsTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ public void SetDefaultAndroidForegroundServiceEnabled_Updates_StaticDefault()
2323

2424
try
2525
{
26-
optionsInstance.SetDefaultAndroidForegroundServiceEnabled(false);
26+
optionsInstance.EnableAndroidForegroundService(false);
2727
MediaElementOptions.IsAndroidForegroundServiceEnabled.Should().BeFalse();
2828

29-
optionsInstance.SetDefaultAndroidForegroundServiceEnabled(true);
29+
optionsInstance.EnableAndroidForegroundService(true);
3030
MediaElementOptions.IsAndroidForegroundServiceEnabled.Should().BeTrue();
3131
}
3232
finally
3333
{
3434
// restore original state to avoid test pollution
35-
optionsInstance.SetDefaultAndroidForegroundServiceEnabled(original);
35+
optionsInstance.EnableAndroidForegroundService(original);
3636
}
3737
}
3838

@@ -68,7 +68,7 @@ public void MediaElement_Initializes_From_MediaElementOptions_Defaults()
6868
{
6969
// change defaults then create a new MediaElement and verify it picked them up
7070
optionsInstance.SetDefaultAndroidViewType(AndroidViewType.TextureView);
71-
optionsInstance.SetDefaultAndroidForegroundServiceEnabled(false);
71+
optionsInstance.EnableAndroidForegroundService(false);
7272

7373
var mediaElement = new MediaElement();
7474

@@ -79,7 +79,7 @@ public void MediaElement_Initializes_From_MediaElementOptions_Defaults()
7979
{
8080
// restore original state
8181
optionsInstance.SetDefaultAndroidViewType(originalViewType);
82-
optionsInstance.SetDefaultAndroidForegroundServiceEnabled(originalForegroundEnabled);
82+
optionsInstance.EnableAndroidForegroundService(originalForegroundEnabled);
8383
}
8484
}
8585
}

0 commit comments

Comments
 (0)