MainActivity.cs 7.8 KB

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