Created
September 2, 2011 09:55
-
-
Save nevyn/1188290 to your computer and use it in GitHub Desktop.
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
-(id)init; | |
... | |
// sets up some KVO with https://github.com/nevyn/SPSuccinct/blob/master/SPSuccinct/SPDepends.h | |
$depends(@"audio session", self, @"pause", (id)^{ | |
[selff matchIdleTimer]; | |
}); | |
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(matchIdleTimer) name:UIDeviceBatteryStateDidChangeNotification object:nil]; | |
} | |
-(void)matchIdleTimer; | |
{ | |
UIDevice *dev = [UIDevice currentDevice]; | |
UIApplication *app = [UIApplication sharedApplication]; | |
BOOL isCharging = ([dev batteryState] == UIDeviceBatteryStateCharging) || ([dev batteryState] == UIDeviceBatteryStateFull); | |
BOOL isPlaying = self.trackPlayerManager && !self.pause; | |
BOOL should = isCharging && isPlaying; | |
BOOL is = [app isIdleTimerDisabled]; | |
if(is != should) { | |
DINFO("Transitioning idle timer to %s (%s, %s)", should?"on":"off", isCharging?"charging":"not charging", isPlaying?"playing":"not playing"); | |
app.idleTimerDisabled = should; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment