You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
5.4 KiB

  1. using Android.App;
  2. using Android.Content;
  3. using Android.OS;
  4. using Android.Runtime;
  5. using AndroidX.Core.App;
  6. using GMCabsDriverAssistant.Messages;
  7. using GMCabsDriverAssistant.Services;
  8. using GMCabsDriverAssistantSolution.Platforms.Android.Services;
  9. using Sentry;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. [assembly: Dependency(typeof(AndroidLocationService))]
  16. namespace GMCabsDriverAssistantSolution.Platforms.Android.Services
  17. {
  18. [Service(Enabled = true)]
  19. public class AndroidLocationService : Service, IForegroundService
  20. {
  21. CancellationTokenSource _cts;
  22. public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
  23. public static bool IsForegroundServiceRunning;
  24. public override IBinder OnBind(Intent intent)
  25. {
  26. return null;
  27. }
  28. [return: GeneratedEnum]
  29. public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
  30. {
  31. string foregroundChannelId = "GMCabsForeGroundServiceChannel";
  32. _cts = new CancellationTokenSource();
  33. Task.Run(() =>
  34. {
  35. while (IsForegroundServiceRunning)
  36. {
  37. try
  38. {
  39. var locShared = new LocationService();
  40. locShared.Run(_cts.Token).Wait();
  41. }
  42. catch (System.OperationCanceledException oce)
  43. {
  44. SentrySdk.CaptureException(oce);
  45. }
  46. finally
  47. {
  48. if (_cts.IsCancellationRequested)
  49. {
  50. var message = new StopServiceMessage();
  51. Device.BeginInvokeOnMainThread(
  52. () => DependencyService.Resolve<IForegroundService>().StopMyForegroundService()
  53. );
  54. }
  55. }
  56. }
  57. }, _cts.Token);
  58. var notifManager = this.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager;
  59. var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S)
  60. ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable
  61. : PendingIntentFlags.UpdateCurrent;
  62. var pendingActivityIntent = PendingIntent.GetActivity(this, 0, intent, pendingIntentFlags);
  63. var pendingIntent = PendingIntent.GetBroadcast(this, 0, intent, pendingIntentFlags);
  64. var notificationBuilder = new NotificationCompat.Builder(this, foregroundChannelId)
  65. .SetContentTitle("Location")
  66. .SetContentText("Update location")
  67. .SetPriority(1)
  68. .SetOngoing(true)
  69. .SetAutoCancel(true)
  70. .SetContentIntent(pendingIntent);
  71. if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
  72. {
  73. var notificationChannel = new NotificationChannel(foregroundChannelId, "Title", NotificationImportance.High);
  74. notificationChannel.Importance = NotificationImportance.High;
  75. notificationChannel.EnableLights(true);
  76. notificationChannel.EnableVibration(true);
  77. notificationChannel.SetShowBadge(true);
  78. notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300 });
  79. if (notificationBuilder != null)
  80. {
  81. notificationBuilder.SetChannelId(foregroundChannelId);
  82. notifManager.CreateNotificationChannel(notificationChannel);
  83. }
  84. }
  85. // Notification notif = DependencyService.Get<INotification>().ReturnNotif();
  86. StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notificationBuilder.Build());
  87. return base.OnStartCommand(intent, flags, startId);
  88. }
  89. public override void OnCreate()
  90. {
  91. base.OnCreate();
  92. IsForegroundServiceRunning = true;
  93. }
  94. public override void OnDestroy()
  95. {
  96. IsForegroundServiceRunning = false;
  97. if (_cts != null)
  98. {
  99. _cts.Token.ThrowIfCancellationRequested();
  100. _cts.Cancel();
  101. }
  102. base.OnDestroy();
  103. }
  104. public void StartMyForegroundService()
  105. {
  106. var intent = new Intent(global::Android.App.Application.Context, typeof(AndroidLocationService));
  107. if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
  108. {
  109. global::Android.App.Application.Context.StartForegroundService(intent);
  110. }
  111. else
  112. {
  113. global::Android.App.Application.Context.StartService(intent);
  114. }
  115. }
  116. public void StopMyForegroundService()
  117. {
  118. var intent = new Intent(global::Android.App.Application.Context, typeof(AndroidLocationService));
  119. global::Android.App.Application.Context.StopService(intent);
  120. }
  121. public bool IsForeGroundServiceRunning()
  122. {
  123. return IsForegroundServiceRunning;
  124. }
  125. }
  126. }