MainActivity.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. Xamarin.FormsMaps.Init(this, savedInstanceState);
  75. ZXing.Net.Mobile.Forms.Android.Platform.Init();
  76. XF.Material.Droid.Material.Init(this, savedInstanceState);
  77. LoadApplication(new App());
  78. Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
  79. }
  80. protected override void OnNewIntent(Intent intent)
  81. {
  82. LocalNotificationCenter.NotifyNotificationTapped(intent);
  83. base.OnNewIntent(intent);
  84. }
  85. public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
  86. {
  87. global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  88. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  89. base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  90. }
  91. private void CheckPermissions()
  92. {
  93. bool minimumPermissionsGranted = true;
  94. foreach (string permission in Permissions)
  95. {
  96. if (CheckSelfPermission(permission) != Permission.Granted)
  97. {
  98. minimumPermissionsGranted = false;
  99. }
  100. }
  101. // If any of the minimum permissions aren't granted, we request them from the user
  102. if (!minimumPermissionsGranted)
  103. {
  104. RequestPermissions(Permissions, 0);
  105. }
  106. }
  107. #region Error handling
  108. private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
  109. {
  110. var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
  111. LogUnhandledException(newExc);
  112. }
  113. private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
  114. {
  115. var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
  116. LogUnhandledException(newExc);
  117. }
  118. internal static void LogUnhandledException(Exception exception)
  119. {
  120. try
  121. {
  122. const string errorFileName = "Fatal.log";
  123. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources
  124. var errorFilePath = Path.Combine(libraryPath, errorFileName);
  125. var errorMessage = "";
  126. if (File.Exists(errorFilePath))
  127. {
  128. errorMessage = File.ReadAllText(errorFilePath);
  129. }
  130. errorMessage = errorMessage + System.Environment.NewLine + String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
  131. DateTime.Now, exception.ToString());
  132. File.WriteAllText(errorFilePath, errorMessage);
  133. // Log to Android Device Logging.
  134. Android.Util.Log.Error("Crash Report", errorMessage);
  135. }
  136. catch
  137. {
  138. // just suppress any error logging exceptions
  139. }
  140. }
  141. /// <summary>
  142. // If there is an unhandled exception, the exception information is diplayed
  143. // on screen the next time the app is started (only in debug configuration)
  144. /// </summary>
  145. [Conditional("DEBUG")]
  146. private void DisplayCrashReport()
  147. {
  148. const string errorFilename = "Fatal.log";
  149. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  150. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  151. if (!File.Exists(errorFilePath))
  152. {
  153. return;
  154. }
  155. var errorText = File.ReadAllText(errorFilePath);
  156. new AlertDialog.Builder(this)
  157. .SetPositiveButton("Clear", (sender, args) =>
  158. {
  159. File.Delete(errorFilePath);
  160. })
  161. .SetNegativeButton("Close", (sender, args) =>
  162. {
  163. // User pressed Close.
  164. })
  165. .SetMessage(errorText)
  166. .SetTitle("Crash Report")
  167. .Show();
  168. }
  169. #endregion
  170. }
  171. }