-
Notifications
You must be signed in to change notification settings - Fork 91
/
AndroidPlatform.cs
62 lines (53 loc) · 2 KB
/
AndroidPlatform.cs
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
using System;
using System.Diagnostics;
using System.Reflection;
namespace Mindscape.Raygun4Net.Platforms
{
internal static class AndroidPlatform
{
private static Assembly AndroidAssembly;
public static bool TryAttachExceptionHandlers()
{
try
{
AndroidAssembly ??= AssemblyHelpers.FindAssembly("Mono.Android", AssemblyHelpers.HexStringToByteArray("84e04ff9cfb79065"));
if (AndroidAssembly is null)
{
return false;
}
// Reflection equivalent of:
// Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += Handler
var androidRuntimeType = AndroidAssembly.GetType("Android.Runtime.AndroidEnvironment");
var eventInfo = androidRuntimeType.GetEvent("UnhandledExceptionRaiser");
// We need to create a wrapper around the target because the handler is fired with
// RaiseThrowableEventArgs, which is a type we don't have access to
var eventHandler = new EventHandler(AndroidUnhandledExceptionHandler);
var typedHandler =
Delegate.CreateDelegate(eventInfo.EventHandlerType!, eventHandler.Target, eventHandler.Method);
// static event handler
eventInfo.AddEventHandler(null, typedHandler);
}
catch (Exception ex)
{
Debug.WriteLine("Error attaching to AndroidEnvironment.UnhandledExceptionRaiser: {0}", ex);
return false;
}
return true;
}
private static void AndroidUnhandledExceptionHandler(object sender, object exceptionArgs)
{
Exception exception;
try
{
var exceptionArgsType = exceptionArgs.GetType();
exception = (Exception)exceptionArgsType.GetProperty("Exception").GetValue(exceptionArgs);
}
catch (Exception ex)
{
Debug.WriteLine("Failure extracting exception details in AndroidEnvironment.UnhandledExceptionRaiser: {0}", ex);
return;
}
UnhandledExceptionBridge.RaiseUnhandledException(exception, false);
}
}
}