Skip to content

Commit

Permalink
Limit Low Power Mode to Apple Silicon
Browse files Browse the repository at this point in the history
  • Loading branch information
newmarcel committed Mar 2, 2022
1 parent c5d1ec5 commit 82ce12e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <KYADeviceInfo/KYADevice.h>
#import <sys/utsname.h>
#import "../KYADefines.h"
#import "../KYABatteryMonitor/KYABatteryMonitor+Private.h"
#import "../KYALowPowerModeMonitor/KYALowPowerModeMonitor+Private.h"
Expand Down Expand Up @@ -42,6 +43,7 @@ - (instancetype)init
{
self.batteryMonitor = [KYABatteryMonitor new];
self.lowPowerModeMonitor = [KYALowPowerModeMonitor new];
self.lowPowerModeMonitor.device = self;
}
return self;
}
Expand Down Expand Up @@ -96,7 +98,7 @@ - (void)setLowPowerModeMonitoringEnabled:(BOOL)lowPowerModeMonitoringEnabled
if(lowPowerModeMonitoringEnabled)
{
// Do not start monitoring if the device does not support this feature
if([KYALowPowerModeMonitor supportsLowPowerMode] == NO) { return; }
if([lowPowerModeMonitor supportsLowPowerMode] == NO) { return; }

AutoWeak weakSelf = self;
lowPowerModeMonitor.lowPowerModeChangeHandler = ^(BOOL enabled) {
Expand All @@ -118,6 +120,29 @@ - (void)setLowPowerModeMonitoringEnabled:(BOOL)lowPowerModeMonitoringEnabled
KYALog(@"Low Power Mode Monitoring: %@", lowPowerModeMonitoringEnabled ? @"YES" : @"NO");
}

#pragma mark - Architecture

- (KYADeviceArchitecture)architecture
{
struct utsname sysInfo;
if(uname(&sysInfo) == -1)
{
return KYADeviceArchitectureUnknown;
}

char *machine = sysInfo.machine;
if(strcmp(machine, "x86_64") == 0)
{
return KYADeviceArchitectureIntel;
}
else if(strcmp(machine, "arm64") == 0)
{
return KYADeviceArchitectureAppleSilicon;
}

return KYADeviceArchitectureUnknown;
}

#pragma mark - Notifications

- (void)deviceParameterDidChange:(KYADeviceParameter)deviceParameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

#import <Foundation/Foundation.h>
#import <KYADeviceInfo/KYALowPowerModeMonitor.h>
#import <KYADeviceInfo/KYADevice.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^KYALowPowerModeChangeBlock)(BOOL enabled);

@interface KYALowPowerModeMonitor ()
@property (nonatomic, getter=isRegistered) BOOL registered;
@property (weak, nonatomic, nullable) KYADevice *device;

/// An optional block that will be called when the Low Power Mode status
/// changes and -registerForLowPowerModeChanges was called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@

@implementation KYALowPowerModeMonitor

+ (BOOL)supportsLowPowerMode
- (BOOL)supportsLowPowerMode
{
if(@available(macOS 12.0, *))
{
return YES;
Auto device = self.device ?: KYADevice.currentDevice;
if(device.architecture == KYADeviceArchitectureAppleSilicon)
{
return YES;
}
return NO;
}
else
{
Expand All @@ -28,11 +33,6 @@ - (void)dealloc
[self unregisterFromLowPowerModeChanges];
}

- (BOOL)supportsLowPowerMode
{
return [[self class] supportsLowPowerMode];
}

- (BOOL)isLowPowerModeEnabled
{
if(@available(macOS 12.0, *))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ typedef NSString *KYADeviceParameter;
FOUNDATION_EXPORT const KYADeviceParameter KYADeviceParameterBattery;
FOUNDATION_EXPORT const KYADeviceParameter KYADeviceParameterLowPowerMode;

/// The hardware architecture of a device
typedef NS_ENUM(NSUInteger, KYADeviceArchitecture)
{
/// An unknown architecture
KYADeviceArchitectureUnknown = 0,
/// The Intel x86_64 architecture
KYADeviceArchitectureIntel,
/// The arm64 architecture
KYADeviceArchitectureAppleSilicon
};

/// Provides access to physical device parameters, like battery status and Low Power Mode.
@interface KYADevice : NSObject

/// Represents the current device
@property (class, nonatomic, readonly) KYADevice *currentDevice;

/// The hardware architecture of the device
@property (nonatomic, readonly) KYADeviceArchitecture architecture;

#pragma mark - Battery Monitor

/// The battery status monitor for devices that support this feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ NS_ASSUME_NONNULL_BEGIN
@interface KYALowPowerModeMonitor : NSObject

/// Returns `YES` if the current device supports the Low Power Mode.
@property (class, nonatomic, readonly) BOOL supportsLowPowerMode;

/// Convenience property to determine if the current device supports the Low Power Mode.
/// @note Please prefer using the `+supportsLowPowerMode` class property.
@property (nonatomic, readonly) BOOL supportsLowPowerMode;

/// Returns `YES` if Low Power Mode is currently enabled.
Expand Down

0 comments on commit 82ce12e

Please sign in to comment.