MainActivity.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. namespace comal.timesheets.Droid
  17. {
  18. [Activity(Name = "PRS.Mobile.Droid.MainActivity", Label = "TimeBench", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
  19. [IntentFilter(new[] { Android.Content.Intent.ActionView },
  20. AutoVerify = true,
  21. Categories = new[]
  22. {
  23. Android.Content.Intent.CategoryDefault,
  24. Android.Content.Intent.CategoryBrowsable
  25. },
  26. DataScheme = "http",
  27. DataPathPrefix = "/open",
  28. DataHost = "www.PRSMobile.com")]
  29. public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
  30. {
  31. private readonly string[] Permissions =
  32. {
  33. Manifest.Permission.Bluetooth,
  34. Manifest.Permission.BluetoothAdmin,
  35. Manifest.Permission.AccessCoarseLocation,
  36. Manifest.Permission.AccessFineLocation
  37. };
  38. protected override void OnCreate(Bundle savedInstanceState)
  39. {
  40. CheckPermissions();
  41. DependencyService.Register<Version_Android>();
  42. var data = Intent?.Data?.EncodedAuthority;
  43. if (!string.IsNullOrWhiteSpace(data))
  44. {
  45. try
  46. {
  47. string s = Intent.Data.Path;
  48. s = s.Remove(0, 6);
  49. GlobalVariables.LoadFromLinkString = s;
  50. }
  51. catch { }
  52. }
  53. TabLayoutResource = Resource.Layout.Tabbar;
  54. ToolbarResource = Resource.Layout.Toolbar;
  55. base.OnCreate(savedInstanceState);
  56. AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
  57. TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
  58. LocalNotificationCenter.CreateNotificationChannel(new Plugin.LocalNotification.AndroidOption.NotificationChannelRequest
  59. {
  60. Importance = Plugin.LocalNotification.AndroidOption.AndroidImportance.Max,
  61. LockScreenVisibility = Plugin.LocalNotification.AndroidOption.AndroidVisibilityType.Public,
  62. ShowBadge = true,
  63. EnableVibration = true,
  64. Sound = "requiitemadded.mp3"
  65. });
  66. LocalNotificationCenter.NotifyNotificationTapped(Intent);
  67. global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
  68. DisplayCrashReport();
  69. Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
  70. Xamarin.Essentials.Platform.Init(this, savedInstanceState);
  71. Xamarin.FormsMaps.Init(this, savedInstanceState);
  72. ZXing.Net.Mobile.Forms.Android.Platform.Init();
  73. XF.Material.Droid.Material.Init(this, savedInstanceState);
  74. LoadApplication(new App());
  75. Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
  76. }
  77. protected override void OnNewIntent(Intent intent)
  78. {
  79. LocalNotificationCenter.NotifyNotificationTapped(intent);
  80. base.OnNewIntent(intent);
  81. }
  82. public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
  83. {
  84. global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  85. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  86. base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  87. }
  88. private void CheckPermissions()
  89. {
  90. bool minimumPermissionsGranted = true;
  91. foreach (string permission in Permissions)
  92. {
  93. if (CheckSelfPermission(permission) != Permission.Granted)
  94. {
  95. minimumPermissionsGranted = false;
  96. }
  97. }
  98. // If any of the minimum permissions aren't granted, we request them from the user
  99. if (!minimumPermissionsGranted)
  100. {
  101. RequestPermissions(Permissions, 0);
  102. }
  103. }
  104. #region Error handling
  105. private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
  106. {
  107. var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
  108. LogUnhandledException(newExc);
  109. }
  110. private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
  111. {
  112. var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
  113. LogUnhandledException(newExc);
  114. }
  115. internal static void LogUnhandledException(Exception exception)
  116. {
  117. try
  118. {
  119. const string errorFileName = "Fatal.log";
  120. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources
  121. var errorFilePath = Path.Combine(libraryPath, errorFileName);
  122. var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
  123. DateTime.Now, exception.ToString());
  124. File.WriteAllText(errorFilePath, errorMessage);
  125. // Log to Android Device Logging.
  126. Android.Util.Log.Error("Crash Report", errorMessage);
  127. }
  128. catch
  129. {
  130. // just suppress any error logging exceptions
  131. }
  132. }
  133. /// <summary>
  134. // If there is an unhandled exception, the exception information is diplayed
  135. // on screen the next time the app is started (only in debug configuration)
  136. /// </summary>
  137. [Conditional("DEBUG")]
  138. private void DisplayCrashReport()
  139. {
  140. const string errorFilename = "Fatal.log";
  141. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  142. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  143. if (!File.Exists(errorFilePath))
  144. {
  145. return;
  146. }
  147. var errorText = File.ReadAllText(errorFilePath);
  148. new AlertDialog.Builder(this)
  149. .SetPositiveButton("Clear", (sender, args) =>
  150. {
  151. File.Delete(errorFilePath);
  152. })
  153. .SetNegativeButton("Close", (sender, args) =>
  154. {
  155. // User pressed Close.
  156. })
  157. .SetMessage(errorText)
  158. .SetTitle("Crash Report")
  159. .Show();
  160. }
  161. #endregion
  162. }
  163. }