Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add time since unplugged feature #54

Merged
merged 1 commit into from

2 participants

@alexanderad

Basic idea is to add one more menuitem in advanced mode that counts how much time you've actually spent on battery since you've unplugged power source.

It is very similar to Android's "Time on battery" or "Time since last plugged".

Implementation is a basic timer and counter that counts 15 seconds intervals. When mode changes from AC to Battery timer starts and shows "Time since unplugged: 00:01". When mode changes from Battery to AC timer stops but value is still stored for information purposes in shown in menu as "On battery while last unplugged: 00:12".

This is my first time I've opened Xcode and write Cocoa code, please review :-)

Thanks.

@codler codler was assigned
@codler codler merged commit 901b4dd into codler:master
@codler
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 21, 2012
  1. @alexanderad
This page is out of date. Refresh to see the latest.
View
38 Battery Time Remaining/AppDelegate.m
@@ -36,6 +36,9 @@ @interface AppDelegate ()
NSDictionary *batteryIcons;
NSTimer *menuUpdateTimer;
NSTimer *optionKeyPressedTimer;
+ NSString *previousState;
+ NSTimer *unpluggedTimer;
+ NSInteger unpluggedTimerCount;
BOOL isOptionKeyPressed;
BOOL isCapacityWarning;
BOOL showParenthesis;
@@ -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
@@ -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
@@ -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];
View
BIN  Battery Time Remaining/en.lproj/Localizable.strings
Binary file not shown
Something went wrong with that request. Please try again.