mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Make naming consistent across channel APIs (#9270)
This commit is contained in:
parent
752d6096a6
commit
945cfc3ee2
@ -1 +1 @@
|
||||
c24a0e51750570f271f860bbffbe082aab4ea72f
|
||||
2a4434a058d0243447483101160c833029cb3654
|
||||
|
@ -6,9 +6,9 @@ import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import io.flutter.plugin.common.FlutterMessageChannel;
|
||||
import io.flutter.plugin.common.FlutterMessageChannel.MessageHandler;
|
||||
import io.flutter.plugin.common.FlutterMessageChannel.Reply;
|
||||
import io.flutter.plugin.common.BasicMessageChannel;
|
||||
import io.flutter.plugin.common.BasicMessageChannel.MessageHandler;
|
||||
import io.flutter.plugin.common.BasicMessageChannel.Reply;
|
||||
import io.flutter.plugin.common.StringCodec;
|
||||
import io.flutter.view.FlutterMain;
|
||||
import io.flutter.view.FlutterView;
|
||||
@ -20,7 +20,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
private static final String CHANNEL = "increment";
|
||||
private static final String EMPTY_MESSAGE = "";
|
||||
private static final String PING = "ping";
|
||||
private FlutterMessageChannel messageChannel;
|
||||
private BasicMessageChannel messageChannel;
|
||||
|
||||
private String[] getArgsFromIntent(Intent intent) {
|
||||
// Before adding more entries to this list, consider that arbitrary
|
||||
@ -57,15 +57,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
flutterView = (FlutterView) findViewById(R.id.flutter_view);
|
||||
flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null);
|
||||
|
||||
messageChannel =
|
||||
new FlutterMessageChannel<String>(flutterView, CHANNEL, StringCodec.INSTANCE);
|
||||
messageChannel = new BasicMessageChannel<>(flutterView, CHANNEL, StringCodec.INSTANCE);
|
||||
messageChannel.
|
||||
setMessageHandler(new MessageHandler<String>() {
|
||||
@Override
|
||||
public void onMessage(String s, Reply<String> reply) {
|
||||
onFlutterIncrement();
|
||||
reply.send(EMPTY_MESSAGE);
|
||||
|
||||
reply.reply(EMPTY_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
@ -108,4 +106,4 @@ public class MainActivity extends AppCompatActivity {
|
||||
super.onPostResume();
|
||||
flutterView.onPostResume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
@property (nonatomic) NativeViewController* nativeViewController;
|
||||
@property (nonatomic) FlutterViewController* flutterViewController;
|
||||
@property (nonatomic) FlutterMessageChannel* messageChannel;
|
||||
@property (nonatomic) FlutterBasicMessageChannel* messageChannel;
|
||||
@end
|
||||
|
||||
static NSString* const emptyString = @"";
|
||||
@ -35,20 +35,20 @@ static NSString* const channel = @"increment";
|
||||
if ([segue.identifier isEqualToString:@"FlutterViewControllerSegue"]) {
|
||||
self.flutterViewController = segue.destinationViewController;
|
||||
|
||||
self.messageChannel = [FlutterMessageChannel messageChannelWithName:channel
|
||||
binaryMessenger:self.flutterViewController
|
||||
codec:[FlutterStringCodec sharedInstance]];
|
||||
self.messageChannel = [FlutterBasicMessageChannel messageChannelWithName:channel
|
||||
binaryMessenger:self.flutterViewController
|
||||
codec:[FlutterStringCodec sharedInstance]];
|
||||
|
||||
MainViewController* __weak weakSelf = self;
|
||||
[self.messageChannel setMessageHandler:^(id message, FlutterReplyHandler replyHandler) {
|
||||
[self.messageChannel setMessageHandler:^(id message, FlutterReply reply) {
|
||||
[weakSelf.nativeViewController didReceiveIncrement];
|
||||
replyHandler(emptyString);
|
||||
reply(emptyString);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)didTapIncrementButton {
|
||||
[self.messageChannel sendMessage:ping replyHandler:nil];
|
||||
[self.messageChannel sendMessage:ping];
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -29,8 +29,8 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
static const String _channel = "increment";
|
||||
static const String _pong = "pong";
|
||||
static const String _emptyMessage = "";
|
||||
static const PlatformMessageChannel<String> platform =
|
||||
const PlatformMessageChannel<String>(_channel, const StringCodec());
|
||||
static const BasicMessageChannel<String> platform =
|
||||
const BasicMessageChannel<String>(_channel, const StringCodec());
|
||||
|
||||
int _counter = 0;
|
||||
|
||||
|
@ -15,12 +15,12 @@ import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
|
||||
import io.flutter.app.FlutterActivity;
|
||||
import io.flutter.plugin.common.FlutterEventChannel;
|
||||
import io.flutter.plugin.common.FlutterEventChannel.EventSink;
|
||||
import io.flutter.plugin.common.FlutterEventChannel.StreamHandler;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.Response;
|
||||
import io.flutter.plugin.common.EventChannel;
|
||||
import io.flutter.plugin.common.EventChannel.EventSink;
|
||||
import io.flutter.plugin.common.EventChannel.StreamHandler;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
|
||||
public class MainActivity extends FlutterActivity {
|
||||
@ -31,7 +31,7 @@ public class MainActivity extends FlutterActivity {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
new FlutterEventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
|
||||
new EventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
|
||||
new StreamHandler() {
|
||||
private BroadcastReceiver chargingStateChangeReceiver;
|
||||
@Override
|
||||
@ -49,20 +49,20 @@ public class MainActivity extends FlutterActivity {
|
||||
}
|
||||
);
|
||||
|
||||
new FlutterMethodChannel(getFlutterView(), BATTERY_CHANNEL).setMethodCallHandler(
|
||||
new MethodChannel(getFlutterView(), BATTERY_CHANNEL).setMethodCallHandler(
|
||||
new MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Response response) {
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("getBatteryLevel")) {
|
||||
int batteryLevel = getBatteryLevel();
|
||||
|
||||
if (batteryLevel != -1) {
|
||||
response.success(batteryLevel);
|
||||
result.success(batteryLevel);
|
||||
} else {
|
||||
response.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
result.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
}
|
||||
} else {
|
||||
response.notImplemented();
|
||||
result.notImplemented();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
|
||||
@implementation AppDelegate {
|
||||
FlutterEventReceiver _eventReceiver;
|
||||
FlutterEventSink _eventSink;
|
||||
}
|
||||
|
||||
- (BOOL)application:(UIApplication*)application
|
||||
@ -19,7 +19,7 @@
|
||||
methodChannelWithName:@"samples.flutter.io/battery"
|
||||
binaryMessenger:controller];
|
||||
[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
|
||||
FlutterResultReceiver result) {
|
||||
FlutterResult result) {
|
||||
if ([@"getBatteryLevel" isEqualToString:call.method]) {
|
||||
int batteryLevel = [self getBatteryLevel];
|
||||
if (batteryLevel == -1) {
|
||||
@ -52,8 +52,8 @@
|
||||
}
|
||||
|
||||
- (FlutterError*)onListenWithArguments:(id)arguments
|
||||
eventReceiver:(FlutterEventReceiver)eventReceiver {
|
||||
_eventReceiver = eventReceiver;
|
||||
eventSink:(FlutterEventSink)eventSink {
|
||||
_eventSink = eventSink;
|
||||
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
|
||||
[self sendBatteryStateEvent];
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
@ -74,22 +74,22 @@
|
||||
switch (state) {
|
||||
case UIDeviceBatteryStateFull:
|
||||
case UIDeviceBatteryStateCharging:
|
||||
_eventReceiver(@"charging");
|
||||
_eventSink(@"charging");
|
||||
break;
|
||||
case UIDeviceBatteryStateUnplugged:
|
||||
_eventReceiver(@"discharging");
|
||||
_eventSink(@"discharging");
|
||||
break;
|
||||
default:
|
||||
_eventReceiver([FlutterError errorWithCode:@"UNAVAILABLE"
|
||||
message:@"Charging status unavailable"
|
||||
details:nil]);
|
||||
_eventSink([FlutterError errorWithCode:@"UNAVAILABLE"
|
||||
message:@"Charging status unavailable"
|
||||
details:nil]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
- (FlutterError*)onCancelWithArguments:(id)arguments {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
_eventReceiver = nil;
|
||||
_eventSink = nil;
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
@ -13,10 +13,10 @@ class PlatformChannel extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PlatformChannelState extends State<PlatformChannel> {
|
||||
static const PlatformMethodChannel methodChannel =
|
||||
const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
static const PlatformEventChannel eventChannel =
|
||||
const PlatformEventChannel('samples.flutter.io/charging');
|
||||
static const MethodChannel methodChannel =
|
||||
const MethodChannel('samples.flutter.io/battery');
|
||||
static const EventChannel eventChannel =
|
||||
const EventChannel('samples.flutter.io/charging');
|
||||
|
||||
String _batteryLevel = 'Battery level: unknown.';
|
||||
String _chargingStatus = 'Battery status: unknown.';
|
||||
|
@ -7,7 +7,7 @@ import Flutter
|
||||
|
||||
@UIApplicationMain
|
||||
@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
|
||||
private var eventReceiver: FlutterEventReceiver?;
|
||||
private var eventSink: FlutterEventSink?;
|
||||
|
||||
override func application(
|
||||
_ application: UIApplication,
|
||||
@ -16,14 +16,13 @@ import Flutter
|
||||
let batteryChannel = FlutterMethodChannel.init(name: "samples.flutter.io/battery",
|
||||
binaryMessenger: controller);
|
||||
batteryChannel.setMethodCallHandler({
|
||||
(call: FlutterMethodCall, result: FlutterResultReceiver) -> Void in
|
||||
(call: FlutterMethodCall, result: FlutterResult) -> Void in
|
||||
if ("getBatteryLevel" == call.method) {
|
||||
self.receiveBatteryLevel(result: result);
|
||||
} else {
|
||||
result(FlutterMethodNotImplemented);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
let chargingChannel = FlutterEventChannel.init(name: "samples.flutter.io/charging",
|
||||
binaryMessenger: controller);
|
||||
@ -31,7 +30,7 @@ import Flutter
|
||||
return true
|
||||
}
|
||||
|
||||
private func receiveBatteryLevel(result: FlutterResultReceiver) {
|
||||
private func receiveBatteryLevel(result: FlutterResult) {
|
||||
let device = UIDevice.current;
|
||||
device.isBatteryMonitoringEnabled = true;
|
||||
if (device.batteryState == UIDeviceBatteryState.unknown) {
|
||||
@ -44,8 +43,8 @@ import Flutter
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?,
|
||||
eventReceiver: @escaping FlutterEventReceiver) -> FlutterError? {
|
||||
self.eventReceiver = eventReceiver;
|
||||
eventSink: @escaping FlutterEventSink) -> FlutterError? {
|
||||
self.eventSink = eventSink;
|
||||
UIDevice.current.isBatteryMonitoringEnabled = true;
|
||||
self.sendBatteryStateEvent();
|
||||
NotificationCenter.default.addObserver(
|
||||
@ -61,32 +60,32 @@ import Flutter
|
||||
}
|
||||
|
||||
private func sendBatteryStateEvent() {
|
||||
if (eventReceiver == nil) {
|
||||
if (eventSink == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
let state = UIDevice.current.batteryState;
|
||||
switch state {
|
||||
case UIDeviceBatteryState.full:
|
||||
eventReceiver!("charging");
|
||||
eventSink!("charging");
|
||||
break;
|
||||
case UIDeviceBatteryState.charging:
|
||||
eventReceiver!("charging");
|
||||
eventSink!("charging");
|
||||
break;
|
||||
case UIDeviceBatteryState.unplugged:
|
||||
eventReceiver!("discharging");
|
||||
eventSink!("discharging");
|
||||
break;
|
||||
default:
|
||||
eventReceiver!(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Charging status unavailable",
|
||||
details: nil));
|
||||
eventSink!(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Charging status unavailable",
|
||||
details: nil));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
NotificationCenter.default.removeObserver(self);
|
||||
eventReceiver = nil;
|
||||
eventSink = nil;
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
@ -13,10 +13,10 @@ class PlatformChannel extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PlatformChannelState extends State<PlatformChannel> {
|
||||
static const PlatformMethodChannel methodChannel =
|
||||
const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
static const PlatformEventChannel eventChannel =
|
||||
const PlatformEventChannel('samples.flutter.io/charging');
|
||||
static const MethodChannel methodChannel =
|
||||
const MethodChannel('samples.flutter.io/battery');
|
||||
static const EventChannel eventChannel =
|
||||
const EventChannel('samples.flutter.io/charging');
|
||||
|
||||
String _batteryLevel = 'Battery level: unknown.';
|
||||
String _chargingStatus = 'Battery status: unknown.';
|
||||
|
@ -216,7 +216,7 @@ class PlatformAssetBundle extends CachingAssetBundle {
|
||||
Future<ByteData> load(String key) async {
|
||||
final Uint8List encoded = UTF8.encoder.convert(key);
|
||||
final ByteData asset =
|
||||
await PlatformMessages.sendBinary('flutter/assets', encoded.buffer.asByteData());
|
||||
await BinaryMessages.send('flutter/assets', encoded.buffer.asByteData());
|
||||
if (asset == null)
|
||||
throw new FlutterError('Unable to load asset: $key');
|
||||
return asset;
|
||||
|
@ -11,7 +11,7 @@ import 'asset_bundle.dart';
|
||||
import 'image_cache.dart';
|
||||
import 'platform_messages.dart';
|
||||
|
||||
/// Listens for platform messages and directs them to [PlatformMessages].
|
||||
/// Listens for platform messages and directs them to [BinaryMessages].
|
||||
///
|
||||
/// The ServicesBinding also registers a [LicenseEntryCollector] that exposes
|
||||
/// the licenses found in the `LICENSE` file stored at the root of the asset
|
||||
@ -21,7 +21,7 @@ abstract class ServicesBinding extends BindingBase {
|
||||
void initInstances() {
|
||||
super.initInstances();
|
||||
ui.window
|
||||
..onPlatformMessage = PlatformMessages.handlePlatformMessage;
|
||||
..onPlatformMessage = BinaryMessages.handlePlatformMessage;
|
||||
LicenseRegistry.addLicense(_addLicenses);
|
||||
}
|
||||
|
||||
|
@ -27,11 +27,11 @@ import 'platform_messages.dart';
|
||||
/// time.
|
||||
///
|
||||
/// See: <https://flutter.io/platform-channels/>
|
||||
class PlatformMessageChannel<T> {
|
||||
/// Creates a [PlatformMessageChannel] with the specified [name] and [codec].
|
||||
class BasicMessageChannel<T> {
|
||||
/// Creates a [BasicMessageChannel] with the specified [name] and [codec].
|
||||
///
|
||||
/// Neither [name] nor [codec] may be `null`.
|
||||
const PlatformMessageChannel(this.name, this.codec);
|
||||
const BasicMessageChannel(this.name, this.codec);
|
||||
|
||||
/// The logical channel on which communication happens, not `null`.
|
||||
final String name;
|
||||
@ -45,7 +45,7 @@ class PlatformMessageChannel<T> {
|
||||
/// or to a [FormatException], if encoding or decoding fails.
|
||||
Future<T> send(T message) async {
|
||||
return codec.decodeMessage(
|
||||
await PlatformMessages.sendBinary(name, codec.encodeMessage(message))
|
||||
await BinaryMessages.send(name, codec.encodeMessage(message))
|
||||
);
|
||||
}
|
||||
|
||||
@ -60,9 +60,9 @@ class PlatformMessageChannel<T> {
|
||||
/// plugins as a response.
|
||||
void setMessageHandler(Future<T> handler(T message)) {
|
||||
if (handler == null) {
|
||||
PlatformMessages.setBinaryMessageHandler(name, null);
|
||||
BinaryMessages.setMessageHandler(name, null);
|
||||
} else {
|
||||
PlatformMessages.setBinaryMessageHandler(name, (ByteData message) async {
|
||||
BinaryMessages.setMessageHandler(name, (ByteData message) async {
|
||||
return codec.encodeMessage(await handler(codec.decodeMessage(message)));
|
||||
});
|
||||
}
|
||||
@ -80,9 +80,9 @@ class PlatformMessageChannel<T> {
|
||||
/// sent to platform plugins.
|
||||
void setMockMessageHandler(Future<T> handler(T message)) {
|
||||
if (handler == null) {
|
||||
PlatformMessages.setMockBinaryMessageHandler(name, null);
|
||||
BinaryMessages.setMockMessageHandler(name, null);
|
||||
} else {
|
||||
PlatformMessages.setMockBinaryMessageHandler(name, (ByteData message) async {
|
||||
BinaryMessages.setMockMessageHandler(name, (ByteData message) async {
|
||||
return codec.encodeMessage(await handler(codec.decodeMessage(message)));
|
||||
});
|
||||
}
|
||||
@ -103,14 +103,14 @@ class PlatformMessageChannel<T> {
|
||||
/// with may interfere with this channel's communication.
|
||||
///
|
||||
/// See: <https://flutter.io/platform-channels/>
|
||||
class PlatformMethodChannel {
|
||||
/// Creates a [PlatformMethodChannel] with the specified [name].
|
||||
class MethodChannel {
|
||||
/// Creates a [MethodChannel] with the specified [name].
|
||||
///
|
||||
/// The [codec] used will be [StandardMethodCodec], unless otherwise
|
||||
/// specified.
|
||||
///
|
||||
/// Neither [name] nor [codec] may be `null`.
|
||||
const PlatformMethodChannel(this.name, [this.codec = const StandardMethodCodec()]);
|
||||
const MethodChannel(this.name, [this.codec = const StandardMethodCodec()]);
|
||||
|
||||
/// The logical channel on which communication happens, not `null`.
|
||||
final String name;
|
||||
@ -128,7 +128,7 @@ class PlatformMethodChannel {
|
||||
/// * a [MissingPluginException], if the method has not been implemented.
|
||||
Future<dynamic> invokeMethod(String method, [dynamic arguments]) async {
|
||||
assert(method != null);
|
||||
final dynamic result = await PlatformMessages.sendBinary(
|
||||
final dynamic result = await BinaryMessages.send(
|
||||
name,
|
||||
codec.encodeMethodCall(new MethodCall(method, arguments)),
|
||||
);
|
||||
@ -152,9 +152,9 @@ class PlatformMethodChannel {
|
||||
/// similarly to what happens if no method call handler has been set.
|
||||
void setMethodCallHandler(Future<dynamic> handler(MethodCall call)) {
|
||||
if (handler == null) {
|
||||
PlatformMessages.setBinaryMessageHandler(name, null);
|
||||
BinaryMessages.setMessageHandler(name, null);
|
||||
} else {
|
||||
PlatformMessages.setBinaryMessageHandler(
|
||||
BinaryMessages.setMessageHandler(
|
||||
name,
|
||||
(ByteData message) async {
|
||||
final MethodCall call = codec.decodeMethodCall(message);
|
||||
@ -187,9 +187,9 @@ class PlatformMethodChannel {
|
||||
/// not sent to platform plugins.
|
||||
void setMockMethodCallHandler(Future<dynamic> handler(MethodCall call)) {
|
||||
if (handler == null) {
|
||||
PlatformMessages.setMockBinaryMessageHandler(name, null);
|
||||
BinaryMessages.setMockMessageHandler(name, null);
|
||||
} else {
|
||||
PlatformMessages.setMockBinaryMessageHandler(
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
name,
|
||||
(ByteData message) async {
|
||||
final MethodCall call = codec.decodeMethodCall(message);
|
||||
@ -208,13 +208,13 @@ class PlatformMethodChannel {
|
||||
}
|
||||
}
|
||||
|
||||
/// A [PlatformMethodChannel] that ignores missing platform plugins.
|
||||
/// A [MethodChannel] that ignores missing platform plugins.
|
||||
///
|
||||
/// When [invokeMethod] fails to find the platform plugin, it returns null
|
||||
/// instead of throwing an exception.
|
||||
class OptionalPlatformMethodChannel extends PlatformMethodChannel {
|
||||
/// Creates a [PlatformMethodChannel] that ignores missing platform plugins.
|
||||
const OptionalPlatformMethodChannel(String name, [MethodCodec codec = const StandardMethodCodec()])
|
||||
class OptionalMethodChannel extends MethodChannel {
|
||||
/// Creates a [MethodChannel] that ignores missing platform plugins.
|
||||
const OptionalMethodChannel(String name, [MethodCodec codec = const StandardMethodCodec()])
|
||||
: super(name, codec);
|
||||
|
||||
@override
|
||||
@ -240,14 +240,14 @@ class OptionalPlatformMethodChannel extends PlatformMethodChannel {
|
||||
/// with may interfere with this channel's communication.
|
||||
///
|
||||
/// See: <https://flutter.io/platform-channels/>
|
||||
class PlatformEventChannel {
|
||||
/// Creates a [PlatformEventChannel] with the specified [name].
|
||||
class EventChannel {
|
||||
/// Creates a [EventChannel] with the specified [name].
|
||||
///
|
||||
/// The [codec] used will be [StandardMethodCodec], unless otherwise
|
||||
/// specified.
|
||||
///
|
||||
/// Neither [name] nor [codec] may be `null`.
|
||||
const PlatformEventChannel(this.name, [this.codec = const StandardMethodCodec()]);
|
||||
const EventChannel(this.name, [this.codec = const StandardMethodCodec()]);
|
||||
|
||||
/// The logical channel on which communication happens, not `null`.
|
||||
final String name;
|
||||
@ -272,7 +272,7 @@ class PlatformEventChannel {
|
||||
/// Notes for platform plugin implementers:
|
||||
///
|
||||
/// Plugins must expose methods named `listen` and `cancel` suitable for
|
||||
/// invocations by [PlatformMethodChannel.invokeMethod]. Both methods are
|
||||
/// invocations by [MethodChannel.invokeMethod]. Both methods are
|
||||
/// invoked with the specified [arguments].
|
||||
///
|
||||
/// Following the semantics of broadcast streams, `listen` will be called as
|
||||
@ -281,11 +281,11 @@ class PlatformEventChannel {
|
||||
/// indefinitely. Platform plugins should consume no stream-related resources
|
||||
/// while listener count is zero.
|
||||
Stream<dynamic> receiveBroadcastStream([dynamic arguments]) {
|
||||
final PlatformMethodChannel methodChannel = new PlatformMethodChannel(name, codec);
|
||||
final MethodChannel methodChannel = new MethodChannel(name, codec);
|
||||
StreamController<dynamic> controller;
|
||||
controller = new StreamController<dynamic>.broadcast(
|
||||
onListen: () async {
|
||||
PlatformMessages.setBinaryMessageHandler(
|
||||
BinaryMessages.setMessageHandler(
|
||||
name, (ByteData reply) async {
|
||||
if (reply == null) {
|
||||
controller.close();
|
||||
@ -301,11 +301,11 @@ class PlatformEventChannel {
|
||||
try {
|
||||
await methodChannel.invokeMethod('listen', arguments);
|
||||
} catch (e) {
|
||||
PlatformMessages.setBinaryMessageHandler(name, null);
|
||||
BinaryMessages.setMessageHandler(name, null);
|
||||
controller.addError(e);
|
||||
}
|
||||
}, onCancel: () async {
|
||||
PlatformMessages.setBinaryMessageHandler(name, null);
|
||||
BinaryMessages.setMessageHandler(name, null);
|
||||
try {
|
||||
await methodChannel.invokeMethod('cancel', arguments);
|
||||
} catch (exception, stack) {
|
||||
|
@ -8,29 +8,29 @@ import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
typedef Future<ByteData> _PlatformMessageHandler(ByteData message);
|
||||
typedef Future<ByteData> _MessageHandler(ByteData message);
|
||||
|
||||
/// Sends binary messages to and receives binary messages from platform plugins.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [PlatformMessageChannel], which provides messaging services similar to
|
||||
/// PlatformMessages, but with pluggable message codecs in support of sending
|
||||
/// * [BasicMessageChannel], which provides messaging services similar to
|
||||
/// [BinaryMessages], but with pluggable message codecs in support of sending
|
||||
/// strings or semi-structured messages.
|
||||
/// * [PlatformMethodChannel], which provides higher-level platform
|
||||
/// * [MethodChannel], which provides higher-level platform
|
||||
/// communication such as method invocations and event streams.
|
||||
///
|
||||
/// See: <https://flutter.io/platform-channels/>
|
||||
class PlatformMessages {
|
||||
PlatformMessages._();
|
||||
class BinaryMessages {
|
||||
BinaryMessages._();
|
||||
|
||||
// Handlers for incoming messages from platform plugins.
|
||||
static final Map<String, _PlatformMessageHandler> _handlers =
|
||||
<String, _PlatformMessageHandler>{};
|
||||
static final Map<String, _MessageHandler> _handlers =
|
||||
<String, _MessageHandler>{};
|
||||
|
||||
// Mock handlers that intercept and respond to outgoing messages.
|
||||
static final Map<String, _PlatformMessageHandler> _mockHandlers =
|
||||
<String, _PlatformMessageHandler>{};
|
||||
static final Map<String, _MessageHandler> _mockHandlers =
|
||||
<String, _MessageHandler>{};
|
||||
|
||||
static Future<ByteData> _sendPlatformMessage(String channel, ByteData message) {
|
||||
final Completer<ByteData> completer = new Completer<ByteData>();
|
||||
@ -59,7 +59,7 @@ class PlatformMessages {
|
||||
String channel, ByteData data, ui.PlatformMessageResponseCallback callback) async {
|
||||
ByteData response;
|
||||
try {
|
||||
final _PlatformMessageHandler handler = _handlers[channel];
|
||||
final _MessageHandler handler = _handlers[channel];
|
||||
if (handler != null)
|
||||
response = await handler(data);
|
||||
} catch (exception, stack) {
|
||||
@ -78,8 +78,8 @@ class PlatformMessages {
|
||||
///
|
||||
/// Returns a [Future] which completes to the received response, undecoded, in
|
||||
/// binary form.
|
||||
static Future<ByteData> sendBinary(String channel, ByteData message) {
|
||||
final _PlatformMessageHandler handler = _mockHandlers[channel];
|
||||
static Future<ByteData> send(String channel, ByteData message) {
|
||||
final _MessageHandler handler = _mockHandlers[channel];
|
||||
if (handler != null)
|
||||
return handler(message);
|
||||
return _sendPlatformMessage(channel, message);
|
||||
@ -93,7 +93,7 @@ class PlatformMessages {
|
||||
/// argument.
|
||||
///
|
||||
/// The handler's return value, if non-null, is sent as a response, unencoded.
|
||||
static void setBinaryMessageHandler(String channel, Future<ByteData> handler(ByteData message)) {
|
||||
static void setMessageHandler(String channel, Future<ByteData> handler(ByteData message)) {
|
||||
if (handler == null)
|
||||
_handlers.remove(channel);
|
||||
else
|
||||
@ -111,7 +111,7 @@ class PlatformMessages {
|
||||
///
|
||||
/// This is intended for testing. Messages intercepted in this manner are not
|
||||
/// sent to platform plugins.
|
||||
static void setMockBinaryMessageHandler(String channel, Future<ByteData> handler(ByteData message)) {
|
||||
static void setMockMessageHandler(String channel, Future<ByteData> handler(ByteData message)) {
|
||||
if (handler == null)
|
||||
_mockHandlers.remove(channel);
|
||||
else
|
||||
|
@ -9,42 +9,42 @@ import 'platform_channel.dart';
|
||||
class SystemChannels {
|
||||
SystemChannels._();
|
||||
|
||||
/// A JSON [PlatformMethodChannel] for navigation.
|
||||
static const PlatformMethodChannel navigation = const PlatformMethodChannel(
|
||||
/// A JSON [MethodChannel] for navigation.
|
||||
static const MethodChannel navigation = const MethodChannel(
|
||||
'flutter/navigation',
|
||||
const JSONMethodCodec(),
|
||||
);
|
||||
|
||||
/// A JSON [PlatformMethodChannel] for invoking miscellaneous platform methods.
|
||||
/// A JSON [MethodChannel] for invoking miscellaneous platform methods.
|
||||
///
|
||||
/// Ignores missing plugins.
|
||||
static const PlatformMethodChannel platform = const OptionalPlatformMethodChannel(
|
||||
static const MethodChannel platform = const OptionalMethodChannel(
|
||||
'flutter/platform',
|
||||
const JSONMethodCodec(),
|
||||
);
|
||||
|
||||
/// A JSON [PlatformMethodChannel] for handling text input.
|
||||
/// A JSON [MethodChannel] for handling text input.
|
||||
///
|
||||
/// Ignores missing plugins.
|
||||
static const PlatformMethodChannel textInput = const OptionalPlatformMethodChannel(
|
||||
static const MethodChannel textInput = const OptionalMethodChannel(
|
||||
'flutter/textinput',
|
||||
const JSONMethodCodec(),
|
||||
);
|
||||
|
||||
/// A JSON [PlatformMessageChannel] for key events.
|
||||
static const PlatformMessageChannel<dynamic> keyEvent = const PlatformMessageChannel<dynamic>(
|
||||
/// A JSON [BasicMessageChannel] for key events.
|
||||
static const BasicMessageChannel<dynamic> keyEvent = const BasicMessageChannel<dynamic>(
|
||||
'flutter/keyevent',
|
||||
const JSONMessageCodec(),
|
||||
);
|
||||
|
||||
/// A string [PlatformMessageChannel] for lifecycle events.
|
||||
static const PlatformMessageChannel<String> lifecycle = const PlatformMessageChannel<String>(
|
||||
/// A string [BasicMessageChannel] for lifecycle events.
|
||||
static const BasicMessageChannel<String> lifecycle = const BasicMessageChannel<String>(
|
||||
'flutter/lifecycle',
|
||||
const StringCodec(),
|
||||
);
|
||||
|
||||
/// A JSON [PlatformMessageChannel] for system events.
|
||||
static const PlatformMessageChannel<dynamic> system = const PlatformMessageChannel<dynamic>(
|
||||
/// A JSON [BasicMessageChannel] for system events.
|
||||
static const BasicMessageChannel<dynamic> system = const BasicMessageChannel<dynamic>(
|
||||
'flutter/system',
|
||||
const JSONMessageCodec(),
|
||||
);
|
||||
|
@ -195,7 +195,7 @@ void main() {
|
||||
bool completed;
|
||||
|
||||
completed = false;
|
||||
PlatformMessages.setMockBinaryMessageHandler('flutter/assets', (ByteData message) async {
|
||||
BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) async {
|
||||
expect(UTF8.decode(message.buffer.asUint8List()), 'test');
|
||||
completed = true;
|
||||
return new ByteData(5); // 0x0000000000
|
||||
@ -214,7 +214,7 @@ void main() {
|
||||
data = await rootBundle.loadStructuredData<bool>('test', (String value) async { expect(value, '\x00\x00\x00\x00\x00'); return false; });
|
||||
expect(data, isFalse);
|
||||
expect(completed, isTrue);
|
||||
PlatformMessages.setMockBinaryMessageHandler('flutter/assets', null);
|
||||
BinaryMessages.setMockMessageHandler('flutter/assets', null);
|
||||
});
|
||||
|
||||
test('Service extensions - exit', () async {
|
||||
|
@ -11,9 +11,9 @@ import 'package:test/test.dart';
|
||||
void main() {
|
||||
group('PlatformMessageChannel', () {
|
||||
const MessageCodec<String> string = const StringCodec();
|
||||
const PlatformMessageChannel<String> channel = const PlatformMessageChannel<String>('ch', string);
|
||||
const BasicMessageChannel<String> channel = const BasicMessageChannel<String>('ch', string);
|
||||
test('can send string message and get reply', () async {
|
||||
PlatformMessages.setMockBinaryMessageHandler(
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
'ch',
|
||||
(ByteData message) async => string.encodeMessage(string.decodeMessage(message) + ' world'),
|
||||
);
|
||||
@ -23,7 +23,7 @@ void main() {
|
||||
test('can receive string message and send reply', () async {
|
||||
channel.setMessageHandler((String message) async => message + ' world');
|
||||
String reply;
|
||||
await PlatformMessages.handlePlatformMessage(
|
||||
await BinaryMessages.handlePlatformMessage(
|
||||
'ch',
|
||||
const StringCodec().encodeMessage('hello'),
|
||||
(ByteData replyBinary) {
|
||||
@ -37,9 +37,9 @@ void main() {
|
||||
group('PlatformMethodChannel', () {
|
||||
const MessageCodec<dynamic> jsonMessage = const JSONMessageCodec();
|
||||
const MethodCodec jsonMethod = const JSONMethodCodec();
|
||||
const PlatformMethodChannel channel = const PlatformMethodChannel('ch7', jsonMethod);
|
||||
const MethodChannel channel = const MethodChannel('ch7', jsonMethod);
|
||||
test('can invoke method and get result', () async {
|
||||
PlatformMessages.setMockBinaryMessageHandler(
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
'ch7',
|
||||
(ByteData message) async {
|
||||
final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message);
|
||||
@ -53,7 +53,7 @@ void main() {
|
||||
expect(result, equals('hello world'));
|
||||
});
|
||||
test('can invoke method and get error', () async {
|
||||
PlatformMessages.setMockBinaryMessageHandler(
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
'ch7',
|
||||
(ByteData message) async {
|
||||
return jsonMessage.encodeMessage(<dynamic>[
|
||||
@ -75,7 +75,7 @@ void main() {
|
||||
}
|
||||
});
|
||||
test('can invoke unimplemented method', () async {
|
||||
PlatformMessages.setMockBinaryMessageHandler(
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
'ch7',
|
||||
(ByteData message) async => null,
|
||||
);
|
||||
@ -93,17 +93,17 @@ void main() {
|
||||
group('PlatformEventChannel', () {
|
||||
const MessageCodec<dynamic> jsonMessage = const JSONMessageCodec();
|
||||
const MethodCodec jsonMethod = const JSONMethodCodec();
|
||||
const PlatformEventChannel channel = const PlatformEventChannel('ch', jsonMethod);
|
||||
const EventChannel channel = const EventChannel('ch', jsonMethod);
|
||||
test('can receive event stream', () async {
|
||||
void emitEvent(dynamic event) {
|
||||
PlatformMessages.handlePlatformMessage(
|
||||
BinaryMessages.handlePlatformMessage(
|
||||
'ch',
|
||||
event,
|
||||
(ByteData reply) {},
|
||||
);
|
||||
}
|
||||
bool cancelled = false;
|
||||
PlatformMessages.setMockBinaryMessageHandler(
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
'ch',
|
||||
(ByteData message) async {
|
||||
final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message);
|
||||
|
@ -11,17 +11,17 @@ void main() {
|
||||
test('Mock binary message handler control test', () async {
|
||||
final List<ByteData> log = <ByteData>[];
|
||||
|
||||
PlatformMessages.setMockBinaryMessageHandler('test1', (ByteData message) async {
|
||||
BinaryMessages.setMockMessageHandler('test1', (ByteData message) async {
|
||||
log.add(message);
|
||||
});
|
||||
|
||||
final ByteData message = new ByteData(2)..setUint16(0, 0xABCD);
|
||||
await PlatformMessages.sendBinary('test1', message);
|
||||
await BinaryMessages.send('test1', message);
|
||||
expect(log, equals(<ByteData>[message]));
|
||||
log.clear();
|
||||
|
||||
PlatformMessages.setMockBinaryMessageHandler('test1', null);
|
||||
await PlatformMessages.sendBinary('test1', message);
|
||||
BinaryMessages.setMockMessageHandler('test1', null);
|
||||
await BinaryMessages.send('test1', message);
|
||||
expect(log, isEmpty);
|
||||
});
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ void main() {
|
||||
test('setApplicationSwitcherDescription missing plugin', () async {
|
||||
final List<ByteData> log = <ByteData>[];
|
||||
|
||||
PlatformMessages.setMockBinaryMessageHandler('flutter/platform', (ByteData message) {
|
||||
BinaryMessages.setMockMessageHandler('flutter/platform', (ByteData message) {
|
||||
log.add(message);
|
||||
return null;
|
||||
});
|
||||
|
@ -39,7 +39,7 @@ void main() {
|
||||
WidgetsBinding.instance.addObserver(observer);
|
||||
final ByteData message = const JSONMessageCodec().encodeMessage(
|
||||
<String, dynamic>{'type': 'memoryPressure'});
|
||||
await PlatformMessages.handlePlatformMessage('flutter/system', message, (_) {});
|
||||
await BinaryMessages.handlePlatformMessage('flutter/system', message, (_) {});
|
||||
expect(observer.sawMemoryPressure, true);
|
||||
WidgetsBinding.instance.removeObserver(observer);
|
||||
});
|
||||
|
@ -7,7 +7,7 @@ import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void sendFakeKeyEvent(Map<String, dynamic> data) {
|
||||
PlatformMessages.handlePlatformMessage(
|
||||
BinaryMessages.handlePlatformMessage(
|
||||
SystemChannels.keyEvent.name,
|
||||
SystemChannels.keyEvent.codec.encodeMessage(data),
|
||||
(_) {});
|
||||
|
@ -41,7 +41,7 @@ class TestTextInput {
|
||||
|
||||
void updateEditingValue(TextEditingValue value) {
|
||||
expect(_client, isNonZero);
|
||||
PlatformMessages.handlePlatformMessage(
|
||||
BinaryMessages.handlePlatformMessage(
|
||||
SystemChannels.textInput.name,
|
||||
SystemChannels.textInput.codec.encodeMethodCall(
|
||||
new MethodCall(
|
||||
|
@ -1,9 +1,9 @@
|
||||
package {{androidIdentifier}};
|
||||
|
||||
import io.flutter.app.FlutterActivity;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.Response;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
|
||||
/**
|
||||
@ -18,15 +18,15 @@ public class {{pluginClass}} implements MethodCallHandler {
|
||||
|
||||
private {{pluginClass}}(FlutterActivity activity) {
|
||||
this.activity = activity;
|
||||
new FlutterMethodChannel(activity.getFlutterView(), "{{projectName}}").setMethodCallHandler(this);
|
||||
new MethodChannel(activity.getFlutterView(), "{{projectName}}").setMethodCallHandler(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Response response) {
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("getPlatformVersion")) {
|
||||
response.success("Android " + android.os.Build.VERSION.RELEASE);
|
||||
result.success("Android " + android.os.Build.VERSION.RELEASE);
|
||||
} else {
|
||||
response.notImplemented();
|
||||
result.notImplemented();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,12 +8,14 @@
|
||||
if (self) {
|
||||
FlutterMethodChannel *channel = [FlutterMethodChannel
|
||||
methodChannelWithName:@"{{projectName}}"
|
||||
binaryMessenger:controller];
|
||||
binaryMessenger:controller];
|
||||
[channel setMethodCallHandler:^(FlutterMethodCall *call,
|
||||
FlutterResultReceiver result) {
|
||||
FlutterResult result) {
|
||||
if ([@"getPlatformVersion" isEqualToString:call.method]) {
|
||||
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice]
|
||||
systemVersion]]);
|
||||
} else {
|
||||
result(FlutterMethodNotImplemented);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user