-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
10 changes: 10 additions & 0 deletions
10
incubator/@react-native-webapis/battery-status/macos/RNWBatteryStatus.h
This file contains 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 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#import <React/RCTBridgeModule.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface RNWBatteryStatus : NSObject <RCTBridgeModule> | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
65 changes: 65 additions & 0 deletions
65
incubator/@react-native-webapis/battery-status/macos/RNWBatteryStatus.m
This file contains 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 @@ | ||
#import "RNWBatteryStatus.h" | ||
|
||
#import <IOKit/ps/IOPowerSources.h> | ||
|
||
@implementation RNWBatteryStatus | ||
|
||
RCT_EXPORT_MODULE(RNWBatteryStatus) | ||
|
||
+ (BOOL)requiresMainQueueSetup | ||
{ | ||
return NO; | ||
} | ||
|
||
// clang-format off | ||
RCT_EXPORT_METHOD(getStatus:(RCTPromiseResolveBlock)resolve | ||
rejecter:(RCTPromiseRejectBlock)reject) | ||
// clang-format on | ||
{ | ||
NSMutableDictionary *status = [NSMutableDictionary dictionaryWithCapacity:4]; | ||
|
||
CFTypeRef psInfo = IOPSCopyPowerSourcesInfo(); | ||
if (psInfo != nil) { | ||
CFArrayRef psListRef = IOPSCopyPowerSourcesList(psInfo); | ||
if (CFArrayGetCount(psListRef) > 0) { | ||
CFTypeRef ps = CFArrayGetValueAtIndex(psListRef, 0); | ||
CFDictionaryRef desc = IOPSGetPowerSourceDescription(psInfo, ps); | ||
|
||
CFTypeRef state = CFDictionaryGetValue(desc, CFSTR(kIOPSPowerSourceStateKey)); | ||
BOOL isPluggedIn = CFStringCompare(state, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo; | ||
|
||
CFTypeRef charging = CFDictionaryGetValue(desc, CFSTR(kIOPSIsChargingKey)); | ||
status[@"charging"] = [NSNumber numberWithBool:CFBooleanGetValue(charging)]; | ||
|
||
CFTypeRef chargingTime = nil; | ||
if (isPluggedIn && CFDictionaryGetValueIfPresent(desc, CFSTR(kIOPSTimeToFullChargeKey), &chargingTime)) { | ||
int minutes = 0; | ||
CFNumberGetValue(chargingTime, kCFNumberIntType, &minutes); | ||
status[@"chargingTime"] = [NSNumber numberWithInt:minutes < 0 ? minutes : minutes * 60]; | ||
} else { | ||
status[@"chargingTime"] = @-1; | ||
} | ||
|
||
CFTypeRef dischargingTime = nil; | ||
if (!isPluggedIn && CFDictionaryGetValueIfPresent(desc, CFSTR(kIOPSTimeToEmptyKey), &dischargingTime)) { | ||
int minutes = 0; | ||
CFNumberGetValue(dischargingTime, kCFNumberIntType, &minutes); | ||
status[@"dischargingTime"] = [NSNumber numberWithInt:minutes < 0 ? minutes : minutes * 60]; | ||
} else { | ||
status[@"dischargingTime"] = @-1; | ||
} | ||
|
||
CFTypeRef level = CFDictionaryGetValue(desc, CFSTR(kIOPSCurrentCapacityKey)); | ||
int capacity = 0; | ||
CFNumberGetValue(level, kCFNumberIntType, &capacity); | ||
status[@"level"] = [NSNumber numberWithFloat:capacity / 100.0]; | ||
} | ||
|
||
CFRelease(psListRef); | ||
CFRelease(psInfo); | ||
} | ||
|
||
resolve(status); | ||
} | ||
|
||
@end |
1 change: 1 addition & 0 deletions
1
...tor/@react-native-webapis/battery-status/macos/RNWBatteryStatus.xcodeproj/project.pbxproj
This file contains 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 @@ | ||
/* Dummy file so @react-native-community/cli recognizes this as an macOS package */ |