AppDelegate.m 3.08 KB
Newer Older
1
// Copyright 2017 The Chromium 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
  [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
23
                                         FlutterResult result) {
24
    if ([@"getBatteryLevel" isEqualToString:call.method]) {
25 26
      int batteryLevel = [self getBatteryLevel];
      if (batteryLevel == -1) {
27 28 29
        result([FlutterError errorWithCode:@"UNAVAILABLE"
                                   message:@"Battery info unavailable"
                                   details:nil]);
30
      } else {
31
        result(@(batteryLevel));
32 33
      }
    } else {
34
      result(FlutterMethodNotImplemented);
35 36
    }
  }];
37 38 39 40 41

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

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

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

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

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

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

97
@end