MainActivity.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Android;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.Content.PM;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. using Android.OS;
  12. using InABox.Mobile.Android;
  13. using Plugin.LocalNotification;
  14. using Xamarin.Essentials;
  15. using Xamarin.Forms;
  16. namespace PRS.Mobile.Droid
  17. {
  18. [Activity(
  19. Label = "PRS.Mobile",
  20. Theme = "@style/MainTheme",
  21. MainLauncher = true,
  22. ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation
  23. )]
  24. [IntentFilter(new[] { Android.Content.Intent.ActionView },
  25. AutoVerify = true,
  26. Categories = new[]
  27. {
  28. Android.Content.Intent.CategoryDefault,
  29. Android.Content.Intent.CategoryBrowsable,
  30. },
  31. DataScheme = "http",
  32. DataPathPrefix = "/open",
  33. DataHost = "www.prsmobile.com")
  34. ]
  35. public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
  36. {
  37. private readonly string[] Permissions =
  38. {
  39. Manifest.Permission.Bluetooth,
  40. Manifest.Permission.BluetoothAdmin,
  41. Manifest.Permission.AccessCoarseLocation,
  42. Manifest.Permission.AccessFineLocation,
  43. Manifest.Permission.BluetoothScan,
  44. Manifest.Permission.BluetoothAdvertise,
  45. Manifest.Permission.BluetoothConnect,
  46. Manifest.Permission.ReadMediaVideo,
  47. Manifest.Permission.ReadMediaAudio,
  48. Manifest.Permission.ReadMediaImages,
  49. Manifest.Permission.Flashlight,
  50. };
  51. protected override void OnCreate(Bundle savedInstanceState)
  52. {
  53. if (!string.IsNullOrWhiteSpace(Intent?.Data?.EncodedAuthority))
  54. {
  55. string s = Intent?.Data?.Path ?? "";
  56. App.LaunchParameters = (s.Length > 6) ? s.Remove(0, 6) : "";
  57. }
  58. CheckPermissions();
  59. DependencyService.Register<Version_Android>();
  60. TabLayoutResource = Resource.Layout.Tabbar;
  61. ToolbarResource = Resource.Layout.Toolbar;
  62. AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
  63. TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
  64. base.OnCreate(savedInstanceState);
  65. global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
  66. // https://github.com/Redth/ZXing.Net.Mobile
  67. ZXing.Net.Mobile.Forms.Android.Platform.Init();
  68. // https://github.com/Baseflow/XF-Material-Library
  69. XF.Material.Droid.Material.Init(this, savedInstanceState);
  70. LocalNotificationCenter.CreateNotificationChannel(new Plugin.LocalNotification.AndroidOption.NotificationChannelRequest
  71. {
  72. Importance = Plugin.LocalNotification.AndroidOption.AndroidImportance.Max,
  73. LockScreenVisibility = Plugin.LocalNotification.AndroidOption.AndroidVisibilityType.Public,
  74. ShowBadge = true,
  75. EnableVibration = true,
  76. Sound = "requiitemadded.mp3"
  77. });
  78. LocalNotificationCenter.NotifyNotificationTapped(Intent);
  79. // https://learn.microsoft.com/en-au/xamarin/essentials/get-started?tabs=windows%2Candroid
  80. Xamarin.Essentials.Platform.Init(this, savedInstanceState);
  81. Syncfusion.XForms.Android.PopupLayout.SfPopupLayoutRenderer.Init();
  82. Xamarin.FormsMaps.Init(this, savedInstanceState);
  83. ZXing.Net.Mobile.Forms.Android.Platform.Init();
  84. XF.Material.Droid.Material.Init(this, savedInstanceState);
  85. LoadApplication(new App());
  86. // FV 23-07-07 Not sure what this does - can we strip it out?
  87. // Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
  88. }
  89. protected override void OnNewIntent(Intent intent)
  90. {
  91. LocalNotificationCenter.NotifyNotificationTapped(intent);
  92. base.OnNewIntent(intent);
  93. }
  94. public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
  95. {
  96. //at this point in time, xamarin essentials is unable to ask for WRITE_MEDIA_STORAGE from the user, so it never gets granted
  97. //has to be set manually when asked for, in order for API 33 on Android 13 to work
  98. //https://github.com/xamarin/Essentials/issues/2041
  99. if (permissions.Any(x => x == "WRITE_MEDIA_STORAGE"))
  100. {
  101. var grants = new Permission[] { Permission.Granted };
  102. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grants);
  103. }
  104. else
  105. {
  106. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  107. }
  108. base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  109. }
  110. private async void CheckPermissions()
  111. {
  112. bool minimumPermissionsGranted = true;
  113. foreach (string permission in Permissions)
  114. {
  115. if (CheckSelfPermission(permission) != Permission.Granted)
  116. {
  117. minimumPermissionsGranted = false;
  118. }
  119. }
  120. // If any of the minimum permissions aren't granted, we request them from the user
  121. if (!minimumPermissionsGranted)
  122. {
  123. RequestPermissions(Permissions, 0);
  124. }
  125. }
  126. #region Error handling
  127. private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args)
  128. {
  129. LogUnhandledException("TaskScheduler", args.Exception?.GetType(), args.Exception);
  130. }
  131. private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
  132. {
  133. LogUnhandledException("CurrentDomain", args.ExceptionObject?.GetType(), args.ExceptionObject);
  134. }
  135. private static void LogUnhandledException(String source, Type type, object exceptionobject)
  136. {
  137. if (exceptionobject is Exception exception)
  138. MobileLogging.Log(exception, source);
  139. else
  140. MobileLogging.Log($"{source}: {type?.Name ?? "NULL"} -> {exceptionobject}");
  141. }
  142. #endregion
  143. }
  144. }