| // Copyright 2014 The Flutter Authors. All rights reserved. | |
| // 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> | |
| #import "GeneratedPluginRegistrant.h" | |
| @implementation AppDelegate { | |
| FlutterEventSink _eventSink; | |
| } | |
| - (BOOL)application:(UIApplication*)application | |
| didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { | |
| return [super application:application didFinishLaunchingWithOptions:launchOptions]; | |
| } | |
| - (void)didInitializeImplicitFlutterEngine:(NSObject<FlutterImplicitEngineBridge> *)engineBridge { | |
| [GeneratedPluginRegistrant registerWithRegistry:engineBridge.pluginRegistry]; | |
| FlutterMethodChannel* batteryChannel = [FlutterMethodChannel | |
| methodChannelWithName:@"samples.flutter.io/battery" | |
| binaryMessenger:engineBridge.applicationRegistrar.messenger]; | |
| __weak typeof(self) weakSelf = self; | |
| [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, | |
| FlutterResult result) { | |
| if ([@"getBatteryLevel" isEqualToString:call.method]) { | |
| int batteryLevel = [weakSelf getBatteryLevel]; | |
| if (batteryLevel == -1) { | |
| result([FlutterError errorWithCode:@"UNAVAILABLE" | |
| message:@"Battery info unavailable" | |
| details:nil]); | |
| } else { | |
| result(@(batteryLevel)); | |
| } | |
| } else { | |
| result(FlutterMethodNotImplemented); | |
| } | |
| }]; | |
| FlutterEventChannel* chargingChannel = [FlutterEventChannel | |
| eventChannelWithName:@"samples.flutter.io/charging" | |
| binaryMessenger:engineBridge.applicationRegistrar.messenger]; | |
| [chargingChannel setStreamHandler:self]; | |
| } | |
| - (int)getBatteryLevel { | |
| UIDevice* device = UIDevice.currentDevice; | |
| device.batteryMonitoringEnabled = YES; | |
| if (device.batteryState == UIDeviceBatteryStateUnknown) { | |
| #if TARGET_OS_SIMULATOR | |
| return 100; | |
| #else | |
| return -1; | |
| #endif | |
| } else { | |
| return ((int)(device.batteryLevel * 100)); | |
| } | |
| } | |
| - (FlutterError*)onListenWithArguments:(id)arguments | |
| eventSink:(FlutterEventSink)eventSink { | |
| _eventSink = eventSink; | |
| [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; | |
| [self sendBatteryStateEvent]; | |
| [[NSNotificationCenter defaultCenter] | |
| addObserver:self | |
| selector:@selector(onBatteryStateDidChange:) | |
| name:UIDeviceBatteryStateDidChangeNotification | |
| object:nil]; | |
| return nil; | |
| } | |
| - (void)onBatteryStateDidChange:(NSNotification*)notification { | |
| [self sendBatteryStateEvent]; | |
| } | |
| - (void)sendBatteryStateEvent { | |
| if (!_eventSink) return; | |
| UIDeviceBatteryState state = [[UIDevice currentDevice] batteryState]; | |
| switch (state) { | |
| case UIDeviceBatteryStateFull: | |
| case UIDeviceBatteryStateCharging: | |
| _eventSink(@"charging"); | |
| break; | |
| case UIDeviceBatteryStateUnplugged: | |
| _eventSink(@"discharging"); | |
| break; | |
| default: | |
| #if TARGET_OS_SIMULATOR | |
| _eventSink(@"charging"); | |
| #else | |
| _eventSink([FlutterError errorWithCode:@"UNAVAILABLE" | |
| message:@"Charging status unavailable" | |
| details:nil]); | |
| #endif | |
| break; | |
| } | |
| } | |
| - (FlutterError*)onCancelWithArguments:(id)arguments { | |
| [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| _eventSink = nil; | |
| return nil; | |
| } | |
| @end |