From 945cfc3ee2c0d5d5691e83e0454c6f8cd9d44778 Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Tue, 18 Apr 2017 15:23:15 +0200 Subject: [PATCH] Make naming consistent across channel APIs (#9270) --- bin/internal/engine.version | 2 +- .../java/com/example/view/MainActivity.java | 16 +++--- .../ios/Runner/MainViewController.m | 14 ++--- examples/flutter_view/lib/main.dart | 4 +- .../example/platformchannel/MainActivity.java | 24 ++++---- .../platform_channel/ios/Runner/AppDelegate.m | 20 +++---- examples/platform_channel/lib/main.dart | 8 +-- .../ios/Runner/AppDelegate.swift | 29 +++++----- examples/platform_channel_swift/lib/main.dart | 8 +-- .../lib/src/services/asset_bundle.dart | 2 +- .../flutter/lib/src/services/binding.dart | 4 +- .../lib/src/services/platform_channel.dart | 56 +++++++++---------- .../lib/src/services/platform_messages.dart | 30 +++++----- .../lib/src/services/system_channels.dart | 24 ++++---- .../foundation/service_extensions_test.dart | 4 +- .../test/services/platform_channel_test.dart | 20 +++---- .../test/services/platform_messages_test.dart | 8 +-- .../test/services/system_chrome_test.dart | 2 +- .../flutter/test/widgets/binding_test.dart | 2 +- .../widgets/raw_keyboard_listener_test.dart | 2 +- .../flutter_test/lib/src/test_text_input.dart | 2 +- .../projectName/pluginClass.java.tmpl | 14 ++--- .../ios.tmpl/Classes/pluginClass.m.tmpl | 6 +- 23 files changed, 150 insertions(+), 151 deletions(-) diff --git a/bin/internal/engine.version b/bin/internal/engine.version index 801ff6ecd4..d7f7bb8c8b 100644 --- a/bin/internal/engine.version +++ b/bin/internal/engine.version @@ -1 +1 @@ -c24a0e51750570f271f860bbffbe082aab4ea72f +2a4434a058d0243447483101160c833029cb3654 diff --git a/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java b/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java index a572f6bc61..17c2a5eb54 100644 --- a/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java +++ b/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java @@ -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(flutterView, CHANNEL, StringCodec.INSTANCE); + messageChannel = new BasicMessageChannel<>(flutterView, CHANNEL, StringCodec.INSTANCE); messageChannel. setMessageHandler(new MessageHandler() { @Override public void onMessage(String s, Reply reply) { onFlutterIncrement(); - reply.send(EMPTY_MESSAGE); - + reply.reply(EMPTY_MESSAGE); } }); @@ -108,4 +106,4 @@ public class MainActivity extends AppCompatActivity { super.onPostResume(); flutterView.onPostResume(); } -} \ No newline at end of file +} diff --git a/examples/flutter_view/ios/Runner/MainViewController.m b/examples/flutter_view/ios/Runner/MainViewController.m index 8ff7b1fa5d..b969c7fbf4 100644 --- a/examples/flutter_view/ios/Runner/MainViewController.m +++ b/examples/flutter_view/ios/Runner/MainViewController.m @@ -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 diff --git a/examples/flutter_view/lib/main.dart b/examples/flutter_view/lib/main.dart index 9cd4da84e9..459fa92ab9 100644 --- a/examples/flutter_view/lib/main.dart +++ b/examples/flutter_view/lib/main.dart @@ -29,8 +29,8 @@ class _MyHomePageState extends State { static const String _channel = "increment"; static const String _pong = "pong"; static const String _emptyMessage = ""; - static const PlatformMessageChannel platform = - const PlatformMessageChannel(_channel, const StringCodec()); + static const BasicMessageChannel platform = + const BasicMessageChannel(_channel, const StringCodec()); int _counter = 0; 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 6f10be30e0..036d88d931 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 @@ -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(); } } } diff --git a/examples/platform_channel/ios/Runner/AppDelegate.m b/examples/platform_channel/ios/Runner/AppDelegate.m index 9ac4819df2..f4ee7da00f 100644 --- a/examples/platform_channel/ios/Runner/AppDelegate.m +++ b/examples/platform_channel/ios/Runner/AppDelegate.m @@ -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; } diff --git a/examples/platform_channel/lib/main.dart b/examples/platform_channel/lib/main.dart index eea88f1f74..173a80d793 100644 --- a/examples/platform_channel/lib/main.dart +++ b/examples/platform_channel/lib/main.dart @@ -13,10 +13,10 @@ class PlatformChannel extends StatefulWidget { } class _PlatformChannelState extends State { - 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.'; diff --git a/examples/platform_channel_swift/ios/Runner/AppDelegate.swift b/examples/platform_channel_swift/ios/Runner/AppDelegate.swift index 04f5c55b27..976635a85a 100644 --- a/examples/platform_channel_swift/ios/Runner/AppDelegate.swift +++ b/examples/platform_channel_swift/ios/Runner/AppDelegate.swift @@ -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; } } diff --git a/examples/platform_channel_swift/lib/main.dart b/examples/platform_channel_swift/lib/main.dart index eea88f1f74..173a80d793 100644 --- a/examples/platform_channel_swift/lib/main.dart +++ b/examples/platform_channel_swift/lib/main.dart @@ -13,10 +13,10 @@ class PlatformChannel extends StatefulWidget { } class _PlatformChannelState extends State { - 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.'; diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index be882122e7..de3074c0bb 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -216,7 +216,7 @@ class PlatformAssetBundle extends CachingAssetBundle { Future 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; diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index aedb0a52e3..13eaa6703c 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -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); } diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart index 0f541d6969..c064c28326 100644 --- a/packages/flutter/lib/src/services/platform_channel.dart +++ b/packages/flutter/lib/src/services/platform_channel.dart @@ -27,11 +27,11 @@ import 'platform_messages.dart'; /// time. /// /// See: -class PlatformMessageChannel { - /// Creates a [PlatformMessageChannel] with the specified [name] and [codec]. +class BasicMessageChannel { + /// 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 { /// or to a [FormatException], if encoding or decoding fails. Future 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 { /// plugins as a response. void setMessageHandler(Future 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 { /// sent to platform plugins. void setMockMessageHandler(Future 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 { /// with may interfere with this channel's communication. /// /// See: -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 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 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 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: -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 receiveBroadcastStream([dynamic arguments]) { - final PlatformMethodChannel methodChannel = new PlatformMethodChannel(name, codec); + final MethodChannel methodChannel = new MethodChannel(name, codec); StreamController controller; controller = new StreamController.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) { diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart index 51303cca49..972c3149bf 100644 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ b/packages/flutter/lib/src/services/platform_messages.dart @@ -8,29 +8,29 @@ import 'dart:ui' as ui; import 'package:flutter/foundation.dart'; -typedef Future _PlatformMessageHandler(ByteData message); +typedef Future _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: -class PlatformMessages { - PlatformMessages._(); +class BinaryMessages { + BinaryMessages._(); // Handlers for incoming messages from platform plugins. - static final Map _handlers = - {}; + static final Map _handlers = + {}; // Mock handlers that intercept and respond to outgoing messages. - static final Map _mockHandlers = - {}; + static final Map _mockHandlers = + {}; static Future _sendPlatformMessage(String channel, ByteData message) { final Completer completer = new Completer(); @@ -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 sendBinary(String channel, ByteData message) { - final _PlatformMessageHandler handler = _mockHandlers[channel]; + static Future 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 handler(ByteData message)) { + static void setMessageHandler(String channel, Future 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 handler(ByteData message)) { + static void setMockMessageHandler(String channel, Future handler(ByteData message)) { if (handler == null) _mockHandlers.remove(channel); else diff --git a/packages/flutter/lib/src/services/system_channels.dart b/packages/flutter/lib/src/services/system_channels.dart index 6cef21a899..d0eacbb0f7 100644 --- a/packages/flutter/lib/src/services/system_channels.dart +++ b/packages/flutter/lib/src/services/system_channels.dart @@ -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 keyEvent = const PlatformMessageChannel( + /// A JSON [BasicMessageChannel] for key events. + static const BasicMessageChannel keyEvent = const BasicMessageChannel( 'flutter/keyevent', const JSONMessageCodec(), ); - /// A string [PlatformMessageChannel] for lifecycle events. - static const PlatformMessageChannel lifecycle = const PlatformMessageChannel( + /// A string [BasicMessageChannel] for lifecycle events. + static const BasicMessageChannel lifecycle = const BasicMessageChannel( 'flutter/lifecycle', const StringCodec(), ); - /// A JSON [PlatformMessageChannel] for system events. - static const PlatformMessageChannel system = const PlatformMessageChannel( + /// A JSON [BasicMessageChannel] for system events. + static const BasicMessageChannel system = const BasicMessageChannel( 'flutter/system', const JSONMessageCodec(), ); diff --git a/packages/flutter/test/foundation/service_extensions_test.dart b/packages/flutter/test/foundation/service_extensions_test.dart index 93efa25f1a..9f2259993f 100644 --- a/packages/flutter/test/foundation/service_extensions_test.dart +++ b/packages/flutter/test/foundation/service_extensions_test.dart @@ -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('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 { diff --git a/packages/flutter/test/services/platform_channel_test.dart b/packages/flutter/test/services/platform_channel_test.dart index 0477f43004..6478146c03 100644 --- a/packages/flutter/test/services/platform_channel_test.dart +++ b/packages/flutter/test/services/platform_channel_test.dart @@ -11,9 +11,9 @@ import 'package:test/test.dart'; void main() { group('PlatformMessageChannel', () { const MessageCodec string = const StringCodec(); - const PlatformMessageChannel channel = const PlatformMessageChannel('ch', string); + const BasicMessageChannel channel = const BasicMessageChannel('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 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 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([ @@ -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 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 methodCall = jsonMessage.decodeMessage(message); diff --git a/packages/flutter/test/services/platform_messages_test.dart b/packages/flutter/test/services/platform_messages_test.dart index a202809228..76deaabfd2 100644 --- a/packages/flutter/test/services/platform_messages_test.dart +++ b/packages/flutter/test/services/platform_messages_test.dart @@ -11,17 +11,17 @@ void main() { test('Mock binary message handler control test', () async { final List log = []; - 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([message])); log.clear(); - PlatformMessages.setMockBinaryMessageHandler('test1', null); - await PlatformMessages.sendBinary('test1', message); + BinaryMessages.setMockMessageHandler('test1', null); + await BinaryMessages.send('test1', message); expect(log, isEmpty); }); } diff --git a/packages/flutter/test/services/system_chrome_test.dart b/packages/flutter/test/services/system_chrome_test.dart index aa0c37561f..ca1393fb58 100644 --- a/packages/flutter/test/services/system_chrome_test.dart +++ b/packages/flutter/test/services/system_chrome_test.dart @@ -59,7 +59,7 @@ void main() { test('setApplicationSwitcherDescription missing plugin', () async { final List log = []; - PlatformMessages.setMockBinaryMessageHandler('flutter/platform', (ByteData message) { + BinaryMessages.setMockMessageHandler('flutter/platform', (ByteData message) { log.add(message); return null; }); diff --git a/packages/flutter/test/widgets/binding_test.dart b/packages/flutter/test/widgets/binding_test.dart index 2d4673035d..9d2581b2eb 100644 --- a/packages/flutter/test/widgets/binding_test.dart +++ b/packages/flutter/test/widgets/binding_test.dart @@ -39,7 +39,7 @@ void main() { WidgetsBinding.instance.addObserver(observer); final ByteData message = const JSONMessageCodec().encodeMessage( {'type': 'memoryPressure'}); - await PlatformMessages.handlePlatformMessage('flutter/system', message, (_) {}); + await BinaryMessages.handlePlatformMessage('flutter/system', message, (_) {}); expect(observer.sawMemoryPressure, true); WidgetsBinding.instance.removeObserver(observer); }); diff --git a/packages/flutter/test/widgets/raw_keyboard_listener_test.dart b/packages/flutter/test/widgets/raw_keyboard_listener_test.dart index 2c25ccdfeb..79cf78894e 100644 --- a/packages/flutter/test/widgets/raw_keyboard_listener_test.dart +++ b/packages/flutter/test/widgets/raw_keyboard_listener_test.dart @@ -7,7 +7,7 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; void sendFakeKeyEvent(Map data) { - PlatformMessages.handlePlatformMessage( + BinaryMessages.handlePlatformMessage( SystemChannels.keyEvent.name, SystemChannels.keyEvent.codec.encodeMessage(data), (_) {}); diff --git a/packages/flutter_test/lib/src/test_text_input.dart b/packages/flutter_test/lib/src/test_text_input.dart index f7631f8c02..d0a06f7ae9 100644 --- a/packages/flutter_test/lib/src/test_text_input.dart +++ b/packages/flutter_test/lib/src/test_text_input.dart @@ -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( diff --git a/packages/flutter_tools/templates/plugin/android.tmpl/src/main/java/com/yourcompany/projectName/pluginClass.java.tmpl b/packages/flutter_tools/templates/plugin/android.tmpl/src/main/java/com/yourcompany/projectName/pluginClass.java.tmpl index 1c4a967be6..15db8690ec 100644 --- a/packages/flutter_tools/templates/plugin/android.tmpl/src/main/java/com/yourcompany/projectName/pluginClass.java.tmpl +++ b/packages/flutter_tools/templates/plugin/android.tmpl/src/main/java/com/yourcompany/projectName/pluginClass.java.tmpl @@ -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(); } } } diff --git a/packages/flutter_tools/templates/plugin/ios.tmpl/Classes/pluginClass.m.tmpl b/packages/flutter_tools/templates/plugin/ios.tmpl/Classes/pluginClass.m.tmpl index e5e9c60d7b..3a1e3909f3 100644 --- a/packages/flutter_tools/templates/plugin/ios.tmpl/Classes/pluginClass.m.tmpl +++ b/packages/flutter_tools/templates/plugin/ios.tmpl/Classes/pluginClass.m.tmpl @@ -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); } }]; }