MainActivity.cs 7.4 KB

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