MainActivity.cs 7.0 KB

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