cleanup platform_channel (#9099)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 <Flutter/Flutter.h>
|
||||
|
||||
@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
|
||||
|
||||
@@ -13,7 +13,7 @@ class PlatformChannel extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PlatformChannelState extends State<PlatformChannel> {
|
||||
static const PlatformMethodChannel platform = const PlatformMethodChannel('battery');
|
||||
static const PlatformMethodChannel platform = const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
String _batteryLevel = '';
|
||||
|
||||
Future<Null> _getBatteryLevel() async {
|
||||
|
||||
Reference in New Issue
Block a user