Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/SharpClient.App/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public static MauiApp CreateMauiApp()

// Eagerly resolve the coordinator so it begins observing session/connection activity at
// startup (DI singletons are otherwise lazily constructed on first request).
app.Services.GetRequiredService<ConnectionKeepAliveCoordinator>();
var keepAlive = app.Services.GetRequiredService<ConnectionKeepAliveCoordinator>();
NetworkChangeSignal.Changed += keepAlive.ReconnectAll;

return app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ internal sealed class AndroidConnectionKeepAlive : IConnectionKeepAlive
// service's android26.0 SupportedOSPlatform requirement.
internal const string ExtraStatus = "extra_status_text";

private bool _started;

public void Start(string statusText)
{
var ctx = Android.App.Application.Context;
var ctx = Android.App.Application.Context;

// Android 12+ refuses StartForegroundService while the app is in the background (screen
// locked), so once the service runs a status change only re-posts its notification.
if (_started && OperatingSystem.IsAndroidVersionAtLeast(26))
{
ConnectionKeepAliveService.UpdateStatus(ctx, statusText);
return;
}

var intent = new Intent(ctx, typeof(ConnectionKeepAliveService))
.PutExtra(ExtraStatus, statusText);

Expand All @@ -31,10 +42,13 @@ public void Start(string statusText)
{
ctx.StartService(intent);
}

_started = true;
}

public void Halt()
{
_started = false;
var ctx = Android.App.Application.Context;
ctx.StopService(new Intent(ctx, typeof(ConnectionKeepAliveService)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ internal sealed class ConnectionKeepAliveService : Service
private const int NotificationId = 1_001;
private const string WakeLockTag = "SharpClient::KeepAlive";

// The text on the notification, kept so a network change can re-post it unchanged.
private static string s_status = "Connected";

private PowerManager.WakeLock? _wakeLock;
private ConnectivityManager? _connectivity;
private ConnectivityManager.NetworkCallback? _networkCallback;
Expand All @@ -39,9 +42,9 @@ internal sealed class ConnectionKeepAliveService : Service
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent? intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
EnsureNotificationChannel();
var status = intent?.GetStringExtra(AndroidConnectionKeepAlive.ExtraStatus) ?? "Connected";
var notification = BuildNotification(status);
EnsureNotificationChannel(this);
s_status = intent?.GetStringExtra(AndroidConnectionKeepAlive.ExtraStatus) ?? s_status;
var notification = BuildNotification(this, s_status);

// On API 29+ supply the foreground service type declared in AndroidManifest.xml.
// TypeConnectedDevice = FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE (connectedDevice; API 29+).
Expand Down Expand Up @@ -157,8 +160,7 @@ private void OnNetworkAvailable(Network network)
}
_haveSeenNetwork = true;

var nm = (NotificationManager?)GetSystemService(NotificationService);
nm?.Notify(NotificationId, BuildNotification("Connected"));
UpdateStatus(this, s_status);
}

private void OnNetworkLost(Network network)
Expand All @@ -171,18 +173,29 @@ private void OnNetworkLost(Network network)
}
}

private void EnsureNotificationChannel()
/// <summary>
/// Changes the text on the running service's notification. Unlike another
/// StartForegroundService, this is allowed while the app is in the background.
/// </summary>
internal static void UpdateStatus(Context context, string statusText)
{
s_status = statusText;
var nm = (NotificationManager?)context.GetSystemService(NotificationService);
nm?.Notify(NotificationId, BuildNotification(context, statusText));
}

private static void EnsureNotificationChannel(Context context)
{
var channel = new NotificationChannel(ChannelId, ChannelName, NotificationImportance.Low)
{
Description = "Keeps the MUSH TCP connection alive while the app is in the background.",
};
var nm = (NotificationManager?)GetSystemService(NotificationService);
var nm = (NotificationManager?)context.GetSystemService(NotificationService);
nm?.CreateNotificationChannel(channel);
}

private Notification BuildNotification(string statusText) =>
new Notification.Builder(this, ChannelId)
private static Notification BuildNotification(Context context, string statusText) =>
new Notification.Builder(context, ChannelId)
.SetContentTitle("SharpClient")
.SetContentText(statusText)
.SetSmallIcon(Resource.Drawable.ic_stat_keepalive)
Expand Down
105 changes: 0 additions & 105 deletions src/SharpClient.App/Services/ConnectionKeepAliveCoordinator.cs

This file was deleted.

6 changes: 3 additions & 3 deletions src/SharpClient.App/Services/NetworkChangeSignal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace SharpClient.App.Services;
/// <summary>
/// Process-wide bridge for "the device's default network changed" events. The Android
/// keep-alive service (which owns the connectivity callback but has no session references)
/// raises it; <see cref="ConnectionKeepAliveCoordinator"/> (which has the sessions but is
/// platform-agnostic) listens and forces a reconnect. Keeping it here — not in the Android
/// service type — lets the coordinator subscribe without referencing any Android-only API.
/// raises it; MauiProgram routes it to <see cref="SharpClient.Core.Connection.ConnectionKeepAliveCoordinator.ReconnectAll"/>,
/// which has the sessions. Keeping it here, not in the Android service type, lets that wiring
/// avoid any Android-only API.
/// On platforms that never raise it, subscribers simply never fire.
/// </summary>
internal static class NetworkChangeSignal
Expand Down
Loading
Loading