diff --git a/AideDeJeu/AideDeJeu.Android/MainActivity.cs b/AideDeJeu/AideDeJeu.Android/MainActivity.cs index 6186a19c..d6e33a85 100644 --- a/AideDeJeu/AideDeJeu.Android/MainActivity.cs +++ b/AideDeJeu/AideDeJeu.Android/MainActivity.cs @@ -6,6 +6,8 @@ using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; +using System.Threading.Tasks; +using System.Diagnostics; namespace AideDeJeu.Droid { @@ -19,6 +21,10 @@ namespace AideDeJeu.Droid ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); + + AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; + TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; + Xamarin.Essentials.Platform.Init(this, bundle); Xamarin.Essentials.ExperimentalFeatures.Enable(Xamarin.Essentials.ExperimentalFeatures.ShareFileRequest); @@ -30,6 +36,8 @@ namespace AideDeJeu.Droid //global::Xamarin.Forms.FormsMaterial.Init(this, bundle); SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3()); + DisplayCrashReport(); + LoadApplication(new App()); } @@ -52,6 +60,71 @@ namespace AideDeJeu.Droid base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } + + + 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 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources + var errorFilePath = System.IO.Path.Combine(libraryPath, errorFileName); + var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}", + DateTime.Now, exception.ToString()); + System.IO.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 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); + var errorFilePath = System.IO.Path.Combine(libraryPath, errorFilename); + + if (!System.IO.File.Exists(errorFilePath)) + { + return; + } + + var errorText = System.IO.File.ReadAllText(errorFilePath); + new AlertDialog.Builder(this) + .SetPositiveButton("Clear", (sender, args) => + { + System.IO.File.Delete(errorFilePath); + }) + .SetNegativeButton("Close", (sender, args) => + { + // User pressed Close. + }) + .SetMessage(errorText) + .SetTitle("Crash Report") + .Show(); + } + } }