Skip to content

Commit 3bab125

Browse files
Catch Android/MAUI permission exceptions (#1750)
1 parent 5d75abb commit 3bab125

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Expose EnumerateChainedExceptions ([#1733](https://github.com/getsentry/sentry-dotnet/pull/1733))
88
- Android Scope Sync ([#1737](https://github.com/getsentry/sentry-dotnet/pull/1737))
99
- Enable logging in MAUI ([#1738](https://github.com/getsentry/sentry-dotnet/pull/1738))
10+
- Catch permission exceptions on Android ([#1750](https://github.com/getsentry/sentry-dotnet/pull/1750))
1011

1112
## Sentry.Maui 3.18.0-preview.1
1213

src/Sentry.Maui/Internal/MauiDeviceData.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,35 @@ public static void ApplyMauiDeviceData(this Device device, IDiagnosticLogger? lo
3737
// ? = deviceInfo.VersionString;
3838

3939
// https://docs.microsoft.com/dotnet/maui/platform-integration/device/battery
40-
var battery = Battery.Default;
41-
device.BatteryLevel ??= battery.ChargeLevel < 0 ? null : (short)battery.ChargeLevel;
42-
device.BatteryStatus ??= battery.State.ToString();
43-
device.IsCharging ??= battery.State switch
40+
try
4441
{
45-
BatteryState.Unknown => null,
46-
BatteryState.Charging => true,
47-
_ => false
48-
};
42+
var battery = Battery.Default;
43+
device.BatteryLevel ??= battery.ChargeLevel < 0 ? null : (short)battery.ChargeLevel;
44+
device.BatteryStatus ??= battery.State.ToString();
45+
device.IsCharging ??= battery.State switch
46+
{
47+
BatteryState.Unknown => null,
48+
BatteryState.Charging => true,
49+
_ => false
50+
};
51+
}
52+
catch (PermissionException)
53+
{
54+
logger?.LogDebug("No permission to read battery state from the device.");
55+
}
4956

5057
// https://docs.microsoft.com/dotnet/maui/platform-integration/communication/networking#using-connectivity
51-
var connectivity = Connectivity.Current;
52-
device.IsOnline ??= connectivity.NetworkAccess == NetworkAccess.Internet;
58+
try
59+
{
60+
device.IsOnline ??= Connectivity.NetworkAccess == NetworkAccess.Internet;
61+
}
62+
catch (PermissionException)
63+
{
64+
logger?.LogDebug("No permission to read network state from the device.");
65+
}
5366

5467
// https://docs.microsoft.com/dotnet/maui/platform-integration/device/display
55-
var display = DeviceDisplay.Current.MainDisplayInfo;
68+
var display = DeviceDisplay.MainDisplayInfo;
5669
device.ScreenResolution ??= $"{(int)display.Width}x{(int)display.Height}";
5770
device.ScreenDensity ??= (float)display.Density;
5871
device.Orientation ??= display.Orientation switch

src/Sentry/Android/SentrySdk.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public static IDisposable Init(AndroidContext context, SentryOptions options)
162162
options.CrashedLastRun = () => Java.Sentry.IsCrashedLastRun()?.BooleanValue() is true;
163163
options.EnableScopeSync = true;
164164
options.ScopeObserver = new AndroidScopeObserver(options);
165+
166+
// "Best" mode throws permission exception on Android
167+
options.DetectStartupTime = StartupTimeDetectionMode.Fast;
168+
165169
// TODO: Pause/Resume
166170

167171
// Init the managed SDK

src/Sentry/Internal/ProcessInfo.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ internal class ProcessInfo
1919
/// </summary>
2020
internal DateTimeOffset? BootTime { get; }
2121

22-
private readonly SentryOptions _options;
23-
private readonly Func<DateTimeOffset> _findPreciseStartupTime;
2422
private volatile Task _preciseAppStartupTask = Task.CompletedTask;
2523

2624
// For testability
@@ -34,12 +32,9 @@ internal ProcessInfo(
3432
SentryOptions options,
3533
Func<DateTimeOffset>? findPreciseStartupTime = null)
3634
{
37-
_options = options;
38-
_findPreciseStartupTime = findPreciseStartupTime ?? GetStartupTime;
3935
if (options.DetectStartupTime == StartupTimeDetectionMode.None)
4036
{
41-
_options.LogDebug("Not detecting startup time due to option: {0}",
42-
_options.DetectStartupTime);
37+
options.LogDebug("Not detecting startup time due to option: {0}", options.DetectStartupTime);
4338
return;
4439
}
4540

@@ -64,7 +59,7 @@ internal ProcessInfo(
6459
// ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
6560
// https://github.com/getsentry/sentry-unity/issues/233
6661

67-
_options.LogError(
62+
options.LogError(
6863
"Failed to find BootTime: Now {0}, GetTimestamp {1}, Frequency {2}, TicksPerSecond: {3}",
6964
e,
7065
now,
@@ -75,33 +70,41 @@ internal ProcessInfo(
7570

7671
// An opt-out to the more precise approach (mainly due to IL2CPP):
7772
// https://issuetracker.unity3d.com/issues/il2cpp-player-crashes-when-calling-process-dot-getcurrentprocess-dot-starttime
78-
if (_options.DetectStartupTime == StartupTimeDetectionMode.Best)
73+
if (options.DetectStartupTime == StartupTimeDetectionMode.Best)
7974
{
75+
#if ANDROID
76+
options.LogWarning("StartupTimeDetectionMode.Best is not available on android. Using 'Fast' mode.");
77+
return;
78+
#else
8079
// StartupTime is set to UtcNow in this constructor.
8180
// That's computationally cheap but not very precise.
8281
// This method will give a better precision to the StartupTime at a cost
8382
// of calling Process.GetCurrentProcess, on a thread pool thread.
83+
var preciseStartupTimeFunc = findPreciseStartupTime ?? GetStartupTime;
8484
PreciseAppStartupTask = Task.Run(() =>
8585
{
8686
try
8787
{
88-
StartupTime = _findPreciseStartupTime();
88+
StartupTime = preciseStartupTimeFunc();
8989
}
9090
catch (Exception e)
9191
{
92-
_options.LogError("Failure getting precise App startup time.", e);
92+
options.LogError("Failure getting precise App startup time.", e);
9393
//Ignore any exception and stay with the less-precise DateTime.UtcNow value.
9494
}
9595
}).ContinueWith(_ =>
9696
// Let the actual task get collected
9797
PreciseAppStartupTask = Task.CompletedTask);
98+
#endif
9899
}
99100
}
100101

102+
#if !ANDROID
101103
private static DateTimeOffset GetStartupTime()
102104
{
103105
using var proc = Process.GetCurrentProcess();
104106
return proc.StartTime.ToUniversalTime();
105107
}
108+
#endif
106109
}
107110
}

0 commit comments

Comments
 (0)