MainActivity.cs 8.0 KB

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