Skip to content

Commit

Permalink
Merge fc85826 into 89491ad
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann authored Jan 29, 2024
2 parents 89491ad + fc85826 commit ae790ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Move header reference out of "extern C" (#3538)
- Clarify FramesTracker log message (#3570)
- Fix rare battery breadcrumbs crash (#3582)


## 8.19.0

Expand Down
18 changes: 14 additions & 4 deletions Sources/Sentry/SentrySystemEventBreadcrumbs.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,15 @@ - (void)initBatteryObserver:(UIDevice *)currentDevice
- (void)batteryStateChanged:(NSNotification *)notification
{
// Notifications for battery level change are sent no more frequently than once per minute
NSMutableDictionary<NSString *, id> *batteryData = [self getBatteryStatus:notification.object];
batteryData[@"action"] = @"BATTERY_STATE_CHANGE";
UIDevice *currentDevice = notification.object;
// The object of an NSNotification may be nil.
if (currentDevice == nil) {
SENTRY_LOG_DEBUG(
@"UIDevice of NSNotification was nil. Won't create battery changed breadcrumb.");
return;
}

NSDictionary<NSString *, id> *batteryData = [self getBatteryStatus:notification.object];

SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentryLevelInfo
category:@"device.event"];
Expand All @@ -112,7 +119,7 @@ - (void)batteryStateChanged:(NSNotification *)notification
[_delegate addBreadcrumb:crumb];
}

- (NSMutableDictionary<NSString *, id> *)getBatteryStatus:(UIDevice *)currentDevice
- (NSDictionary<NSString *, id> *)getBatteryStatus:(UIDevice *)currentDevice
{
// borrowed and adapted from
// https://github.com/apache/cordova-plugin-battery-status/blob/master/src/ios/CDVBattery.m
Expand All @@ -124,7 +131,8 @@ - (void)batteryStateChanged:(NSNotification *)notification
isPlugged = YES;
}
float currentLevel = [currentDevice batteryLevel];
NSMutableDictionary<NSString *, id> *batteryData = [NSMutableDictionary new];
NSMutableDictionary<NSString *, id> *batteryData =
[NSMutableDictionary dictionaryWithCapacity:3];

// W3C spec says level must be null if it is unknown
if ((currentState != UIDeviceBatteryStateUnknown) && (currentLevel != -1.0)) {
Expand All @@ -135,6 +143,8 @@ - (void)batteryStateChanged:(NSNotification *)notification
}

batteryData[@"plugged"] = @(isPlugged);
batteryData[@"action"] = @"BATTERY_STATE_CHANGE";

return batteryData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
assertBatteryBreadcrumb(charging: false, level: 100)
}

func testBatteryUIDeviceNilNotification() {
let currentDevice = MyUIDevice()

sut = fixture.getSut(currentDevice: currentDevice)

postBatteryLevelNotification(uiDevice: nil)

expect(self.fixture.delegate.addCrumbInvocations.count) == 0
}

private func assertBatteryBreadcrumb(charging: Bool, level: Float) {

XCTAssertEqual(1, fixture.delegate.addCrumbInvocations.count)
Expand Down Expand Up @@ -273,7 +283,7 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
XCTAssertEqual(fixture.notificationCenterWrapper.removeObserverWithNameInvocations.count, 7)
}

private func postBatteryLevelNotification(uiDevice: UIDevice) {
private func postBatteryLevelNotification(uiDevice: UIDevice?) {
Dynamic(sut).batteryStateChanged(Notification(name: UIDevice.batteryLevelDidChangeNotification, object: uiDevice))
}

Expand Down

0 comments on commit ae790ff

Please sign in to comment.