From 3e6a2c763653f29be17620ef0378a500474e2b4a Mon Sep 17 00:00:00 2001 From: Sarah Zakarias Date: Thu, 30 Mar 2017 13:27:48 +0200 Subject: [PATCH] cleanup platform_channel (#9099) --- .../example/platformchannel/MainActivity.java | 4 ++-- .../platform_channel/ios/Runner/AppDelegate.m | 23 +++++++++++++------ examples/platform_channel/lib/main.dart | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/examples/platform_channel/android/app/src/main/java/com/example/platformchannel/MainActivity.java b/examples/platform_channel/android/app/src/main/java/com/example/platformchannel/MainActivity.java index 11691ca4c2..af0825c077 100644 --- a/examples/platform_channel/android/app/src/main/java/com/example/platformchannel/MainActivity.java +++ b/examples/platform_channel/android/app/src/main/java/com/example/platformchannel/MainActivity.java @@ -1,4 +1,4 @@ -// Copyright 2016 The Chromium Authors. All rights reserved. +// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -19,7 +19,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.Response; import io.flutter.plugin.common.MethodCall; public class MainActivity extends FlutterActivity { - private static final String CHANNEL = "battery"; + private static final String CHANNEL = "samples.flutter.io/battery"; @Override public void onCreate(Bundle savedInstanceState) { diff --git a/examples/platform_channel/ios/Runner/AppDelegate.m b/examples/platform_channel/ios/Runner/AppDelegate.m index 6bede77a93..c74eb73515 100644 --- a/examples/platform_channel/ios/Runner/AppDelegate.m +++ b/examples/platform_channel/ios/Runner/AppDelegate.m @@ -1,9 +1,8 @@ -// Copyright 2016 The Chromium Authors. All rights reserved. +// Copyright 2017 The Chromium 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 @implementation AppDelegate @@ -12,19 +11,18 @@ FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController; FlutterMethodChannel* batteryChannel = [FlutterMethodChannel - methodChannelWithName:@"battery" + methodChannelWithName:@"samples.flutter.io/battery" binaryMessenger:controller]; [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResultReceiver result) { if ([@"getBatteryLevel" isEqualToString:call.method]) { - UIDevice* device = UIDevice.currentDevice; - device.batteryMonitoringEnabled = YES; - if (device.batteryState == UIDeviceBatteryStateUnknown) { + int batteryLevel = [self getBatteryLevel]; + if (batteryLevel == -1) { result([FlutterError errorWithCode:@"UNAVAILABLE" message:@"Battery info unavailable" details:nil]); } else { - result(@((int)(device.batteryLevel * 100))); + result(@(batteryLevel)); } } else { result(FlutterMethodNotImplemented); @@ -32,4 +30,15 @@ }]; return YES; } + +- (int)getBatteryLevel { + UIDevice* device = UIDevice.currentDevice; + device.batteryMonitoringEnabled = YES; + if (device.batteryState == UIDeviceBatteryStateUnknown) { + return -1; + } else { + return ((int)(device.batteryLevel * 100)); + } +} + @end diff --git a/examples/platform_channel/lib/main.dart b/examples/platform_channel/lib/main.dart index 3b85f6cd97..6c8c1615e1 100644 --- a/examples/platform_channel/lib/main.dart +++ b/examples/platform_channel/lib/main.dart @@ -13,7 +13,7 @@ class PlatformChannel extends StatefulWidget { } class _PlatformChannelState extends State { - static const PlatformMethodChannel platform = const PlatformMethodChannel('battery'); + static const PlatformMethodChannel platform = const PlatformMethodChannel('samples.flutter.io/battery'); String _batteryLevel = ''; Future _getBatteryLevel() async {