-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainActivity.cs
More file actions
109 lines (94 loc) · 4.33 KB
/
MainActivity.cs
File metadata and controls
109 lines (94 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Acr.UserDialogs;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using System;
using System.Threading.Tasks;
using System.Diagnostics;
using CoreServices.Constants;
namespace BaseTemplate.Droid
{
[Activity(Label = "BaseTemplate", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
base.OnCreate(savedInstanceState);
UserDialogs.Init(this);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
DisplayCrashReport();
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
#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
{
var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var errorFilePath = System.IO.Path.Combine(libraryPath, AppConstants.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 (Exception ex)
{
throw ex;
// just suppress any error logging exceptions
}
}
/// <summary>
// If there is an unhandled exception, the exception information is diplayed
// on screen the next time the app is started (only in debug configuration)
/// </summary>
[Conditional("DEBUG")]
private void DisplayCrashReport()
{
var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var errorFilePath = System.IO.Path.Combine(libraryPath, AppConstants.ErrorFileName);
if (!System.IO.File.Exists(errorFilePath))
{
return;
}
var errorText = System.IO.File.ReadAllText(errorFilePath);
if (string.IsNullOrEmpty(errorText))
{
return;
}
new AlertDialog.Builder(this)
.SetPositiveButton("Clear", (sender, args) =>
{
System.IO.File.Delete(errorFilePath);
})
.SetNegativeButton("Close", (sender, args) =>
{
// When User pressed Close.
})
.SetMessage(errorText)
.SetTitle("Crash Report")
.Show();
}
#endregion
}
}