Commit 3e6a2c76 authored by Sarah Zakarias's avatar Sarah Zakarias Committed by GitHub

cleanup platform_channel (#9099)

parent f34f8a31
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -19,7 +19,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.Response; ...@@ -19,7 +19,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.Response;
import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodCall;
public class MainActivity extends FlutterActivity { public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "battery"; private static final String CHANNEL = "samples.flutter.io/battery";
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
......
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import "AppDelegate.h" #import "AppDelegate.h"
#import <Flutter/Flutter.h> #import <Flutter/Flutter.h>
@implementation AppDelegate @implementation AppDelegate
...@@ -12,19 +11,18 @@ ...@@ -12,19 +11,18 @@
FlutterViewController* controller = FlutterViewController* controller =
(FlutterViewController*)self.window.rootViewController; (FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* batteryChannel = [FlutterMethodChannel FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
methodChannelWithName:@"battery" methodChannelWithName:@"samples.flutter.io/battery"
binaryMessenger:controller]; binaryMessenger:controller];
[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
FlutterResultReceiver result) { FlutterResultReceiver result) {
if ([@"getBatteryLevel" isEqualToString:call.method]) { if ([@"getBatteryLevel" isEqualToString:call.method]) {
UIDevice* device = UIDevice.currentDevice; int batteryLevel = [self getBatteryLevel];
device.batteryMonitoringEnabled = YES; if (batteryLevel == -1) {
if (device.batteryState == UIDeviceBatteryStateUnknown) {
result([FlutterError errorWithCode:@"UNAVAILABLE" result([FlutterError errorWithCode:@"UNAVAILABLE"
message:@"Battery info unavailable" message:@"Battery info unavailable"
details:nil]); details:nil]);
} else { } else {
result(@((int)(device.batteryLevel * 100))); result(@(batteryLevel));
} }
} else { } else {
result(FlutterMethodNotImplemented); result(FlutterMethodNotImplemented);
...@@ -32,4 +30,15 @@ ...@@ -32,4 +30,15 @@
}]; }];
return YES; return YES;
} }
- (int)getBatteryLevel {
UIDevice* device = UIDevice.currentDevice;
device.batteryMonitoringEnabled = YES;
if (device.batteryState == UIDeviceBatteryStateUnknown) {
return -1;
} else {
return ((int)(device.batteryLevel * 100));
}
}
@end @end
...@@ -13,7 +13,7 @@ class PlatformChannel extends StatefulWidget { ...@@ -13,7 +13,7 @@ class PlatformChannel extends StatefulWidget {
} }
class _PlatformChannelState extends State<PlatformChannel> { class _PlatformChannelState extends State<PlatformChannel> {
static const PlatformMethodChannel platform = const PlatformMethodChannel('battery'); static const PlatformMethodChannel platform = const PlatformMethodChannel('samples.flutter.io/battery');
String _batteryLevel = ''; String _batteryLevel = '';
Future<Null> _getBatteryLevel() async { Future<Null> _getBatteryLevel() async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment