Skip to content

Instantly share code, notes, and snippets.

@nevyn
Created September 2, 2011 09:55
Show Gist options
  • Save nevyn/1188290 to your computer and use it in GitHub Desktop.
Save nevyn/1188290 to your computer and use it in GitHub Desktop.
-(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