MainActivity.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.prssite.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. CheckPermissions();
  54. DependencyService.Register<Version_Android>();
  55. TabLayoutResource = Resource.Layout.Tabbar;
  56. ToolbarResource = Resource.Layout.Toolbar;
  57. AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
  58. TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
  59. if (!string.IsNullOrWhiteSpace(Intent?.Data?.EncodedAuthority))
  60. {
  61. string s = Intent?.Data?.Path ?? "";
  62. App.LaunchParameters = (s.Length > 6) ? s.Remove(0, 6) : "";
  63. }
  64. base.OnCreate(savedInstanceState);
  65. 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. LoadApplication(new App());
  85. //var color = XF.Material.Forms.Material.Color.Primary;
  86. //Window.SetStatusBarColor(Android.Graphics.Color.Argb((int)color.A, (int)color.R, (int)color.G, (int)color.B));
  87. // For Syncfusion Rich Text Editor Stuff
  88. // see: https://help.syncfusion.com/xamarin/rich-text-editor/gettingstarted
  89. Window?.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
  90. }
  91. protected override void OnNewIntent(Intent intent)
  92. {
  93. LocalNotificationCenter.NotifyNotificationTapped(intent);
  94. base.OnNewIntent(intent);
  95. }
  96. public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
  97. {
  98. //at this point in time, xamarin essentials is unable to ask for WRITE_MEDIA_STORAGE from the user, so it never gets granted
  99. //has to be set manually when asked for, in order for API 33 on Android 13 to work
  100. //https://github.com/xamarin/Essentials/issues/2041
  101. if (permissions.Any(x => x == "WRITE_MEDIA_STORAGE"))
  102. {
  103. var grants = new Permission[] { Permission.Granted };
  104. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grants);
  105. }
  106. else
  107. {
  108. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  109. }
  110. base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  111. }
  112. private async void CheckPermissions()
  113. {
  114. bool minimumPermissionsGranted = true;
  115. foreach (string permission in Permissions)
  116. {
  117. if (CheckSelfPermission(permission) != Permission.Granted)
  118. {
  119. minimumPermissionsGranted = false;
  120. }
  121. }
  122. // If any of the minimum permissions aren't granted, we request them from the user
  123. if (!minimumPermissionsGranted)
  124. {
  125. RequestPermissions(Permissions, 0);
  126. }
  127. }
  128. #region Error handling
  129. private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args)
  130. {
  131. LogUnhandledException("TaskScheduler", args.Exception?.GetType(), args.Exception);
  132. }
  133. private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
  134. {
  135. LogUnhandledException("CurrentDomain", args.ExceptionObject?.GetType(), args.ExceptionObject);
  136. }
  137. private static void LogUnhandledException(String source, Type type, object exceptionobject)
  138. {
  139. if (exceptionobject is Exception exception)
  140. MobileLogging.Log(exception, source);
  141. else
  142. MobileLogging.Log($"{source}: {type?.Name ?? "NULL"} -> {exceptionobject}");
  143. }
  144. #endregion
  145. }
  146. }