Reland isolate platform channels with conditional compilation (#111712)

This commit is contained in:
gaaclarke
2022-09-19 09:52:33 -07:00
committed by GitHub
parent cb8c72545c
commit a7166e7a4e
10 changed files with 217 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ found in the LICENSE file. -->
Application and put your custom class here. -->
<application android:name="${applicationName}" android:label="channels" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"

View File

@@ -29,6 +29,15 @@ public class MainActivity extends FlutterActivity {
setupMessageHandshake(new BasicMessageChannel<>(dartExecutor, "std-msg", ExtendedStandardMessageCodec.INSTANCE));
setupMethodHandshake(new MethodChannel(dartExecutor, "json-method", JSONMethodCodec.INSTANCE));
setupMethodHandshake(new MethodChannel(dartExecutor, "std-method", new StandardMethodCodec(ExtendedStandardMessageCodec.INSTANCE)));
BasicMessageChannel echoChannel =
new BasicMessageChannel(dartExecutor, "std-echo", ExtendedStandardMessageCodec.INSTANCE);
echoChannel.setMessageHandler(new BasicMessageChannel.MessageHandler(){
@Override
public void onMessage(final Object message, final BasicMessageChannel.Reply reply) {
reply.reply(message);
}
});
}
private <T> void setupMessageHandshake(final BasicMessageChannel<T> channel) {

View File

@@ -114,7 +114,18 @@ const UInt8 PAIR = 129;
[FlutterMethodChannel methodChannelWithName:@"std-method"
binaryMessenger:flutterController
codec:[FlutterStandardMethodCodec codecWithReaderWriter:extendedReaderWriter]]];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
[[FlutterBasicMessageChannel
messageChannelWithName:@"std-echo"
binaryMessenger:flutterController
codec:[FlutterStandardMessageCodec
codecWithReaderWriter:extendedReaderWriter]]
setMessageHandler:^(id message, FlutterReply reply) {
reply(message);
}];
return [super application:application
didFinishLaunchingWithOptions:launchOptions];
}
- (void)setupMessagingHandshakeOnChannel:(FlutterBasicMessageChannel*)channel {

View File

@@ -173,6 +173,8 @@ class _TestAppState extends State<TestApp> {
() => basicStringMessageToUnknownChannel(),
() => basicJsonMessageToUnknownChannel(),
() => basicStandardMessageToUnknownChannel(),
if (Platform.isIOS || Platform.isAndroid)
() => basicBackgroundStandardEcho(123),
];
Future<TestStepResult>? _result;
int _step = 0;

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:isolate';
import 'package:flutter/services.dart';
@@ -78,6 +79,41 @@ Future<TestStepResult> basicStandardHandshake(dynamic message) async {
'Standard >${toString(message)}<', channel, message);
}
Future<void> _basicBackgroundStandardEchoMain(List<Object> args) async {
final SendPort sendPort = args[2] as SendPort;
final Object message = args[1];
final String name = 'Background Echo >${toString(message)}<';
const String description =
'Uses a platform channel from a background isolate.';
try {
BackgroundIsolateBinaryMessenger.ensureInitialized(
args[0] as RootIsolateToken);
const BasicMessageChannel<dynamic> channel = BasicMessageChannel<dynamic>(
'std-echo',
ExtendedStandardMessageCodec(),
);
final Object response = await channel.send(message) as Object;
final TestStatus testStatus = TestStepResult.deepEquals(message, response)
? TestStatus.ok
: TestStatus.failed;
sendPort.send(TestStepResult(name, description, testStatus));
} catch (ex) {
sendPort.send(TestStepResult(name, description, TestStatus.failed,
error: ex.toString()));
}
}
Future<TestStepResult> basicBackgroundStandardEcho(Object message) async {
final ReceivePort receivePort = ReceivePort();
Isolate.spawn(_basicBackgroundStandardEchoMain, <Object>[
ServicesBinding.rootIsolateToken!,
message,
receivePort.sendPort,
]);
return await receivePort.first as TestStepResult;
}
Future<TestStepResult> basicBinaryMessageToUnknownChannel() async {
const BasicMessageChannel<ByteData?> channel =
BasicMessageChannel<ByteData?>(

View File

@@ -90,6 +90,8 @@ class TestStepResult {
],
);
}
static bool deepEquals(dynamic a, dynamic b) => _deepEquals(a, b);
}
Future<TestStepResult> resultOfHandshake(