Skip to content

Commit

Permalink
Also display time until fully charged
Browse files Browse the repository at this point in the history
also updated copyright notice in info plist
  • Loading branch information
mac-cain13 committed Aug 4, 2012
1 parent ac6e1e7 commit 06a6121
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Battery Time Remaining.xcodeproj/project.pbxproj
Expand Up @@ -52,7 +52,6 @@
2841C7D115C91CC100F4F15F = {
isa = PBXGroup;
children = (
2841C7FD15C91CEF00F4F15F /* IOKit.framework */,
2841C7E615C91CC100F4F15F /* Battery Time Remaining */,
2841C7DF15C91CC100F4F15F /* Frameworks */,
2841C7DD15C91CC100F4F15F /* Products */,
Expand All @@ -70,6 +69,7 @@
2841C7DF15C91CC100F4F15F /* Frameworks */ = {
isa = PBXGroup;
children = (
2841C7FD15C91CEF00F4F15F /* IOKit.framework */,
2841C7E015C91CC100F4F15F /* Cocoa.framework */,
2841C7E215C91CC100F4F15F /* Other Frameworks */,
);
Expand Down
47 changes: 46 additions & 1 deletion Battery Time Remaining/AppDelegate.m
Expand Up @@ -9,6 +9,7 @@
#import "AppDelegate.h"
#import "StartAtLoginHelper.h"
#import <IOKit/ps/IOPowerSources.h>
#import <IOKit/ps/IOPSKeys.h>

// IOPS notification callback on power source change
static void PowerSourceChanged(void * context)
Expand Down Expand Up @@ -54,7 +55,51 @@ - (NSString *)getTimeRemainingText
// We're connected to an unlimited power source (AC adapter probably)
if (kIOPSTimeRemainingUnlimited == timeRemaining)
{
return @"";
// Get list of power sources
CFTypeRef psBlob = IOPSCopyPowerSourcesInfo();
CFArrayRef psList = IOPSCopyPowerSourcesList(psBlob);

// Loop through the list of power sources
CFIndex count = CFArrayGetCount(psList);
for (CFIndex i = 0; i < count; i++)
{
CFTypeRef powersource = CFArrayGetValueAtIndex(psList, i);
CFDictionaryRef description = IOPSGetPowerSourceDescription(psBlob, powersource);

// Skip if not present or not a battery
if (CFDictionaryGetValue(description, CFSTR(kIOPSIsPresentKey)) == kCFBooleanFalse || !CFStringCompare(CFDictionaryGetValue(description, CFSTR(kIOPSPowerSourceStateKey)), CFSTR(kIOPSBatteryPowerValue), 0))
{
continue;
}

// Check if the battery is charging atm
if (CFDictionaryGetValue(description, CFSTR(kIOPSIsChargingKey)) == kCFBooleanTrue)
{
CFNumberRef timeToChargeNum = CFDictionaryGetValue(description, CFSTR(kIOPSTimeToFullChargeKey));
int timeTilCharged = [(__bridge NSNumber *)timeToChargeNum intValue];

if (timeTilCharged > 0)
{
// Calculate the hour/minutes
NSInteger hour = (int)timeTilCharged / 3600;
NSInteger minute = (int)timeTilCharged % 3600;

// Return the time remaining string
return [NSString stringWithFormat:@"🔌 %ld:%02ld", hour, minute];
}
else
{
return @"🔌 Calculating…";
}
}
else
{
// Not charging and on a endless powersource
return @"🔌";
}

return @"Error…";
}
}
// Still calculating the estimated time remaining...
else if (kIOPSTimeRemainingUnknown == timeRemaining)
Expand Down
2 changes: 1 addition & 1 deletion Battery Time Remaining/Battery Time Remaining-Info.plist
Expand Up @@ -27,7 +27,7 @@
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2012 Han Lin Yap. All rights reserved.</string>
<string>Copyright © 2012 Han Lin Yap and Wrep. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
Expand Down

0 comments on commit 06a6121

Please sign in to comment.