AppDelegate.m 3.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "AppDelegate.h"
#import <Flutter/Flutter.h>
7
#import "GeneratedPluginRegistrant.h"
8 9

@implementation AppDelegate {
10
  FlutterEventSink _eventSink;
11 12
}

13 14
- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
15
  [GeneratedPluginRegistrant registerWithRegistry:self];
16 17
  FlutterViewController* controller =
      (FlutterViewController*)self.window.rootViewController;
18

19
  FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
20
      methodChannelWithName:@"samples.flutter.io/battery"
21
            binaryMessenger:controller];
22
  __weak typeof(self) weakSelf = self;
23
  [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
24
                                         FlutterResult result) {
25
    if ([@"getBatteryLevel" isEqualToString:call.method]) {
26
      int batteryLevel = [weakSelf getBatteryLevel];
27
      if (batteryLevel == -1) {
28 29 30
        result([FlutterError errorWithCode:@"UNAVAILABLE"
                                   message:@"Battery info unavailable"
                                   details:nil]);
31
      } else {
32
        result(@(batteryLevel));
33 34
      }
    } else {
35
      result(FlutterMethodNotImplemented);
36 37
    }
  }];
38 39 40 41 42

  FlutterEventChannel* chargingChannel = [FlutterEventChannel
      eventChannelWithName:@"samples.flutter.io/charging"
           binaryMessenger:controller];
  [chargingChannel setStreamHandler:self];
43
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
44
}
45 46 47 48 49 50 51 52 53 54 55

- (int)getBatteryLevel {
  UIDevice* device = UIDevice.currentDevice;
  device.batteryMonitoringEnabled = YES;
  if (device.batteryState == UIDeviceBatteryStateUnknown) {
    return -1;
  } else {
    return ((int)(device.batteryLevel * 100));
  }
}

56
- (FlutterError*)onListenWithArguments:(id)arguments
57 58
                             eventSink:(FlutterEventSink)eventSink {
  _eventSink = eventSink;
59
  [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
60
  [self sendBatteryStateEvent];
61 62 63 64 65 66 67 68 69
  [[NSNotificationCenter defaultCenter]
   addObserver:self
      selector:@selector(onBatteryStateDidChange:)
          name:UIDeviceBatteryStateDidChangeNotification
        object:nil];
  return nil;
}

- (void)onBatteryStateDidChange:(NSNotification*)notification {
70 71 72 73
  [self sendBatteryStateEvent];
}

- (void)sendBatteryStateEvent {
74
  if (!_eventSink) return;
75 76 77 78
  UIDeviceBatteryState state = [[UIDevice currentDevice] batteryState];
  switch (state) {
    case UIDeviceBatteryStateFull:
    case UIDeviceBatteryStateCharging:
79
      _eventSink(@"charging");
80 81
      break;
    case UIDeviceBatteryStateUnplugged:
82
      _eventSink(@"discharging");
83 84
      break;
    default:
85 86 87
      _eventSink([FlutterError errorWithCode:@"UNAVAILABLE"
                                     message:@"Charging status unavailable"
                                     details:nil]);
88 89 90 91 92 93
      break;
  }
}

- (FlutterError*)onCancelWithArguments:(id)arguments {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
94
  _eventSink = nil;
95 96 97
  return nil;
}

98
@end