using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Android; using Android.App; using Android.Content.PM; using Android.OS; using Android.Runtime; using InABox.Mobile.Android; using Xamarin.Forms; using Plugin.LocalNotification; using Android.Content; using Environment = System.Environment; namespace comal.timesheets.Droid { [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)] [IntentFilter(new[] { Android.Content.Intent.ActionView }, AutoVerify = true, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, DataScheme = "http", DataPathPrefix = "/open", DataHost = "www.PRSMobile.com")] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { private readonly string[] Permissions = { Manifest.Permission.Bluetooth, Manifest.Permission.BluetoothAdmin, Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation, Manifest.Permission.BluetoothScan, Manifest.Permission.BluetoothAdvertise, Manifest.Permission.BluetoothConnect }; protected override void OnCreate(Bundle savedInstanceState) { CheckPermissions(); DependencyService.Register(); var data = Intent?.Data?.EncodedAuthority; if (!string.IsNullOrWhiteSpace(data)) { try { string s = Intent.Data.Path; s = s.Remove(0, 6); GlobalVariables.LoadFromLinkString = s; } catch { } } TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; LocalNotificationCenter.CreateNotificationChannel(new Plugin.LocalNotification.AndroidOption.NotificationChannelRequest { Importance = Plugin.LocalNotification.AndroidOption.AndroidImportance.Max, LockScreenVisibility = Plugin.LocalNotification.AndroidOption.AndroidVisibilityType.Public, ShowBadge = true, EnableVibration = true, Sound = "requiitemadded.mp3" }); LocalNotificationCenter.NotifyNotificationTapped(Intent); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); DisplayCrashReport(); Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); Syncfusion.XForms.Android.PopupLayout.SfPopupLayoutRenderer.Init(); Xamarin.FormsMaps.Init(this, savedInstanceState); ZXing.Net.Mobile.Forms.Android.Platform.Init(); XF.Material.Droid.Material.Init(this, savedInstanceState); LoadApplication(new App()); Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize); } protected override void OnNewIntent(Intent intent) { LocalNotificationCenter.NotifyNotificationTapped(intent); base.OnNewIntent(intent); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults) { global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults); Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } private void CheckPermissions() { bool minimumPermissionsGranted = true; foreach (string permission in Permissions) { if (CheckSelfPermission(permission) != Permission.Granted) { minimumPermissionsGranted = false; } } // If any of the minimum permissions aren't granted, we request them from the user if (!minimumPermissionsGranted) { RequestPermissions(Permissions, 0); } } #region Error handling private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs) { var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception); LogUnhandledException(newExc); } private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) { var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception); LogUnhandledException(newExc); } internal static void LogUnhandledException(Exception exception) { try { const string errorFileName = "Fatal.log"; var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources var errorFilePath = Path.Combine(libraryPath, errorFileName); var errorMessage = ""; if (File.Exists(errorFilePath)) { errorMessage = File.ReadAllText(errorFilePath); } errorMessage = errorMessage + System.Environment.NewLine + String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}", DateTime.Now, exception.ToString()); File.WriteAllText(errorFilePath, errorMessage); // Log to Android Device Logging. Android.Util.Log.Error("Crash Report", errorMessage); } catch { // just suppress any error logging exceptions } } /// // If there is an unhandled exception, the exception information is diplayed // on screen the next time the app is started (only in debug configuration) /// [Conditional("DEBUG")] private void DisplayCrashReport() { const string errorFilename = "Fatal.log"; var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); var errorFilePath = Path.Combine(libraryPath, errorFilename); if (!File.Exists(errorFilePath)) { return; } var errorText = File.ReadAllText(errorFilePath); new AlertDialog.Builder(this) .SetPositiveButton("Clear", (sender, args) => { File.Delete(errorFilePath); }) .SetNegativeButton("Close", (sender, args) => { // User pressed Close. }) .SetMessage(errorText) .SetTitle("Crash Report") .Show(); } #endregion } }