WakelockPlugin.m
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import "WakelockPlugin.h"
#import "messages.h"
static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathObserverContextApplicationIsIdleTimerDisabled;
@interface WakelockPlugin () <FLTWakelockApi>
@property (nonatomic, assign) BOOL enable;
@end
@implementation WakelockPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
WakelockPlugin* instance = [[WakelockPlugin alloc] init];
FLTWakelockApiSetup(registrar.messenger, instance);
[UIApplication.sharedApplication addObserver:instance forKeyPath:@"idleTimerDisabled" options:NSKeyValueObservingOptionNew context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
}
- (void)toggle:(FLTToggleMessage*)input error:(FlutterError**)error {
NSNumber *enable = input.enable;
self.enable = enable.boolValue;
NSNumber *enabled = [NSNumber numberWithBool:[[UIApplication sharedApplication] isIdleTimerDisabled]];
if (![enable isEqualToNumber:enabled]) {
[[UIApplication sharedApplication] setIdleTimerDisabled:enable.boolValue];
}
}
- (FLTIsEnabledMessage*)isEnabled:(FlutterError* __autoreleasing *)error {
NSNumber *enabled = [NSNumber numberWithBool:[[UIApplication sharedApplication] isIdleTimerDisabled]];
FLTIsEnabledMessage* result = [[FLTIsEnabledMessage alloc] init];
result.enabled = enabled;
return result;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == mKeyPathObserverContextApplicationIsIdleTimerDisabled) {
if (UIApplication.sharedApplication.idleTimerDisabled != self.enable) {
UIApplication.sharedApplication.idleTimerDisabled = self.enable;
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc {
[UIApplication.sharedApplication removeObserver:self forKeyPath:@"idleTimerDisabled" context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
}
@end