-
Notifications
You must be signed in to change notification settings - Fork 142
RUM-10378 Collect battery attributes #2351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dd-mergequeue
merged 2 commits into
develop
from
bplasovska/RUM-10378/collect-battery-attributes
Jun 24, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
DatadogCore/Sources/Core/Context/BrightnessLevelPublisher.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
import DatadogInternal | ||
|
||
#if os(iOS) | ||
import UIKit | ||
|
||
/// A publisher that publishes the screen brightness level from UIScreen. | ||
internal final class BrightnessLevelPublisher: ContextValuePublisher { | ||
/// The initial brightness level. | ||
let initialValue: BrightnessLevel? | ||
|
||
/// The notification center to observe brightness changes. | ||
private let notificationCenter: NotificationCenter | ||
private let screen: UIScreen | ||
private var observers: [Any]? = nil | ||
|
||
init(notificationCenter: NotificationCenter = .default, screen: UIScreen = .main) { | ||
self.notificationCenter = notificationCenter | ||
self.screen = screen | ||
self.initialValue = Float(screen.brightness) | ||
} | ||
|
||
/// Publishes the brightness level to the given receiver. | ||
/// | ||
/// - Parameter receiver: The receiver to publish the brightness level to. | ||
func publish(to receiver: @escaping ContextValueReceiver<BrightnessLevel?>) { | ||
let block = { (notification: Notification) in | ||
receiver(Float(self.screen.brightness)) | ||
} | ||
|
||
observers = [ | ||
notificationCenter.addObserver( | ||
forName: UIScreen.brightnessDidChangeNotification, | ||
object: nil, | ||
queue: .main, | ||
using: block | ||
) | ||
] | ||
|
||
receiver(initialValue) | ||
} | ||
|
||
func cancel() { | ||
observers?.forEach(notificationCenter.removeObserver) | ||
observers = nil | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
DatadogCore/Tests/Datadog/DatadogCore/Context/BrightnessLevelPublisherTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
#if os(iOS) | ||
|
||
import XCTest | ||
@testable import TestUtilities | ||
@testable import DatadogCore | ||
|
||
final class BrightnessLevelPublisherTests: XCTestCase { | ||
private let notificationCenter = MockNotificationCenter() | ||
|
||
func testInitialValue() throws { | ||
// Given | ||
let publisher = BrightnessLevelPublisher(notificationCenter: notificationCenter) | ||
|
||
// Then | ||
XCTAssertNotNil(publisher.initialValue) | ||
XCTAssertEqual(publisher.initialValue, Float(UIScreen.main.brightness)) | ||
} | ||
|
||
func testMultipleBrightnessChanges() throws { | ||
let expectation1 = self.expectation(description: "first brightness change") | ||
let expectation2 = self.expectation(description: "second brightness change") | ||
|
||
// Given | ||
let mockScreen = UIScreenMock(brightness: 0.2) | ||
let publisher = BrightnessLevelPublisher(notificationCenter: notificationCenter, screen: mockScreen) | ||
var receivedValues: [Float] = [] | ||
|
||
publisher.publish { level in | ||
if let level = level { | ||
receivedValues.append(level) | ||
|
||
switch receivedValues.count { | ||
case 1: | ||
expectation1.fulfill() | ||
case 2: | ||
expectation2.fulfill() | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
|
||
// When | ||
mockScreen.brightness = 0.5 | ||
notificationCenter.postFakeNotification(name: UIScreen.brightnessDidChangeNotification) | ||
mockScreen.brightness = 0.8 | ||
notificationCenter.postFakeNotification(name: UIScreen.brightnessDidChangeNotification) | ||
|
||
wait(for: [expectation1, expectation2], timeout: 0.1) | ||
|
||
// Then | ||
XCTAssertEqual(receivedValues.count, 3) | ||
XCTAssertEqual(receivedValues[0], 0.2) | ||
XCTAssertEqual(receivedValues[1], 0.5) | ||
XCTAssertEqual(receivedValues[2], 0.8) | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// Type alias for screen brightness level for mobile devices. | ||
public typealias BrightnessLevel = Float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anything for tvOS on that front?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should be available on tvOS as well!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! Actually I checked on the documentation and it's not available for tvOS.