using Android.App; using Android.Content; using Android.Graphics; using Android.OS; using Android.Provider; using Android.Runtime; using Java.Security; using System; using System.Collections.Generic; using System.Net.Http; using System.Net.NetworkInformation; using System.Text; using System.Threading; using System.Threading.Tasks; using Tesses.YouTubeDownloader.Droid; namespace Tesses.YouTubeDownloader.Droid { [Service] public class TYTDServerService : Service { public override IBinder? OnBind(Intent? intent) { return null; } public const int SERVER_NOTIFICATION_ID = 42; public const string SERVER_CHANNEL_ID = "TYTDServerNotificationChannel"; public override void OnCreate() { if(TYTDCommon.MainActivity == null || TYTDCommon.SettingsActivity == null || TYTDCommon.Context == null) return; Intent mainIntent = new Intent(TYTDCommon.Context, TYTDCommon.MainActivity); PendingIntent? mainPendingIntent = PendingIntent.GetActivity(TYTDCommon.Context, 0, mainIntent, PendingIntentFlags.Immutable); Intent settingsIntent = new Intent(TYTDCommon.Context,TYTDCommon.SettingsActivity); PendingIntent? settingsPendingIntent = PendingIntent.GetActivity(TYTDCommon.Context,0,settingsIntent,PendingIntentFlags.Immutable); Intent shutdownServerIntent = new Intent(TYTDCommon.Context,typeof(TYTDReceiver)); PendingIntent? shutdownServerPendingIntent =PendingIntent.GetBroadcast(this,0,shutdownServerIntent,PendingIntentFlags.Immutable); shutdownServerIntent.PutExtra(TYTDReceiver.RECEIVER_KEY,TYTDReceiver.COMMAND_CLOSE_SERVER); Notification notification = new Notification.Builder(TYTDCommon.Context, SERVER_CHANNEL_ID).SetContentTitle("TYTD Server").SetSmallIcon(Resource.Drawable.dns_48px).SetContentText($"TYTD Server is bound to:\nhttp://{(TYTDCommon.ServerAccessable ? "0.0.0.0" : "127.0.0.1")}:{TYTDCommon.ServerPort}/").AddAction(new Notification.Action(-1, "Shutdown Server", shutdownServerPendingIntent)).AddAction(new Notification.Action(-1,"Settings",settingsPendingIntent)).SetContentIntent(mainPendingIntent).Build(); StartForeground(SERVER_NOTIFICATION_ID, notification); } [return: GeneratedEnum] public override StartCommandResult OnStartCommand(Intent? intent, [GeneratedEnum] StartCommandFlags flags, int startId) { return StartCommandResult.Sticky; } public override void OnDestroy() { } } }