Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time since unplugged feature #54

Merged
merged 1 commit into from
Jul 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Battery Time Remaining/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ @interface AppDelegate ()
NSDictionary *batteryIcons;
NSTimer *menuUpdateTimer;
NSTimer *optionKeyPressedTimer;
NSString *previousState;
NSTimer *unpluggedTimer;
NSInteger unpluggedTimerCount;
BOOL isOptionKeyPressed;
BOOL isCapacityWarning;
BOOL showParenthesis;
Expand Down Expand Up @@ -177,6 +180,12 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
CFRelease(loop);
}

- (void)unpluggedTimerTick
{
// increase counter by 15 seconds each tick of corresponding timer
unpluggedTimerCount += 15;
}

- (void)updateStatusItem
{
// reset warning state; new state will be calculated here anyway
Expand Down Expand Up @@ -217,6 +226,29 @@ - (void)updateStatusItem
NSLocalizedString(@"Off Line", @"Powersource state");

[self.statusItem.menu itemWithTag:kBTRMenuPowerSourceState].title = [NSString stringWithFormat:NSLocalizedString(@"Power source: %@", @"Powersource menuitem"), psStateTranslated];

// Figure out what's changed: AC to Battery or vise versa
if(![previousState isEqualToString:psState]) {
if([psState isEqualToString:(NSString *)CFSTR(kIOPSBatteryPowerValue)]) {
// power source changed from AC to battery
// start counting time we spend on battery
unpluggedTimerCount = 0;
unpluggedTimer = [NSTimer
timerWithTimeInterval:15
target:self
selector:@selector(unpluggedTimerTick)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:unpluggedTimer forMode:NSRunLoopCommonModes];
}
if([psState isEqualToString:(NSString *)CFSTR(kIOPSACPowerValue)]) {
// power source changed from battery to AC
// stop timer and reset
[unpluggedTimer invalidate];
unpluggedTimer = nil;
}
previousState = psState;
}

// Still calculating the estimated time remaining...
// Fixes #22 - state after reboot
Expand Down Expand Up @@ -328,10 +360,16 @@ - (void)updateStatusItemMenu

[self.statusItem.menu itemWithTag:kBTRMenuPowerSourcePercent].title = [NSString stringWithFormat: NSLocalizedString(@"%ld %% left ( %ld/%ld mAh )", @"Advanced percentage left menuitem"), self.currentPercent, [currentBatteryPower integerValue], [maxBatteryPower integerValue]];

// time since unplugged
NSString *timeSinceUnplugged = [NSString stringWithFormat:@"%02lu:%02lu",
unpluggedTimerCount / 3600,
(unpluggedTimerCount / 60) % 60];

// Each item in array will be a row in menu
NSArray *advancedBatteryInfoTexts = [NSArray arrayWithObjects:
[NSString stringWithFormat:NSLocalizedString(@"Cycle count: %ld", @"Advanced battery info menuitem"), [cycleCount integerValue]],
[NSString stringWithFormat:NSLocalizedString(@"Power usage: %.2f Watt", @"Advanced battery info menuitem"), [watt doubleValue]],
[NSString stringWithFormat:NSLocalizedString((unpluggedTimer != nil) ? @"Time since unplugged: %@" : @"On battery while last unplugged: %@", @"Advanced battery info menuitem"), timeSinceUnplugged],
[NSString stringWithFormat:NSLocalizedString(@"Temperature: %.1f°C", @"Advanced battery info menuitem"), [temperature doubleValue]],
nil];

Expand Down
Binary file modified Battery Time Remaining/en.lproj/Localizable.strings
Binary file not shown.