Skip to content

Commit

Permalink
Added simple Battery Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Mar 16, 2012
1 parent 6f48602 commit be5333b
Show file tree
Hide file tree
Showing 24 changed files with 367 additions and 2,093 deletions.
345 changes: 245 additions & 100 deletions CocoaneticsBench.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions CocoaneticsBench/Battery Log/Battery Log-Prefix.pch
@@ -0,0 +1,14 @@
//
// Prefix header for all source files of the 'Battery Log' target in the 'Battery Log' project
//

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
10 changes: 4 additions & 6 deletions CocoaneticsBench/Battery Log/Source/AppDelegate.m
Expand Up @@ -9,6 +9,7 @@
#import "AppDelegate.h"

#import "ViewController.h"
#import "BatteryLogger.h"

@implementation AppDelegate

Expand All @@ -17,13 +18,10 @@ @implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[BatteryLogger sharedLogger] startLogging];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
self.viewController = [[ViewController alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
Expand Down
5 changes: 5 additions & 0 deletions CocoaneticsBench/Battery Log/Source/BatteryLogger.h
Expand Up @@ -10,4 +10,9 @@

@interface BatteryLogger : NSObject

+ (BatteryLogger *)sharedLogger;

- (void)startLogging;
- (void)stopLogging;

@end
99 changes: 99 additions & 0 deletions CocoaneticsBench/Battery Log/Source/BatteryLogger.m
Expand Up @@ -8,6 +8,105 @@

#import "BatteryLogger.h"

@interface BatteryLogger ()

- (void)batteryLevelChanged:(NSNotification *)notification;
- (void)batteryStateChanged:(NSNotification *)notification;

@end


@implementation BatteryLogger
{
}


+ (BatteryLogger *)sharedLogger
{
static BatteryLogger *_sharedInstance = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[BatteryLogger alloc] init];
});

return _sharedInstance;
}

- (void)startLogging
{
UIDevice *device = [UIDevice currentDevice];

if ([device isBatteryMonitoringEnabled])
{
// already logging
return;
}

[device setBatteryMonitoringEnabled:YES];

NSNotificationCenter *notifications = [NSNotificationCenter defaultCenter];

[notifications addObserver:self selector:@selector(batteryStateChanged:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
[notifications addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];

// log initial
[self batteryStateChanged:nil];
[self batteryLevelChanged:nil];
}

- (void)stopLogging
{
UIDevice *device = [UIDevice currentDevice];

if (![device isBatteryMonitoringEnabled])
{
// already not logging
return;
}


[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];

NSNotificationCenter *notifications = [NSNotificationCenter defaultCenter];

[notifications removeObserver:self name:UIDeviceBatteryStateDidChangeNotification object:nil];
[notifications removeObserver:self name:UIDeviceBatteryLevelDidChangeNotification object:nil];
}

#pragma mark Notifications
- (void)batteryStateChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];

switch (device.batteryState)
{
case UIDeviceBatteryStateUnknown:
NSLog(@"State: Unknown");
break;

case UIDeviceBatteryStateUnplugged:
NSLog(@"State: Unplugged");
break;

case UIDeviceBatteryStateCharging:
NSLog(@"State: Charging");
break;

case UIDeviceBatteryStateFull:
NSLog(@"State: Full");
break;
}
}

- (void)batteryLevelChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];

NSLog(@"Level: %0.2f%%", device.batteryLevel*100.0f);
}




@end
38 changes: 0 additions & 38 deletions CocoaneticsBench/CocoaneticsBench-Info.plist

This file was deleted.

15 changes: 0 additions & 15 deletions CocoaneticsBench/CocoaneticsBench-Prefix.pch

This file was deleted.

This file was deleted.

This file was deleted.

24 changes: 0 additions & 24 deletions CocoaneticsBench/CocoaneticsBenchAppDelegate.h

This file was deleted.

0 comments on commit be5333b

Please sign in to comment.