Commit 616a7bed authored by Sarah Zakarias's avatar Sarah Zakarias Committed by GitHub

Update Android part of flutter_view to use the new platform message c… (#8729)

* update Android part of flutter_view to use the new platform message channel.

* addressed comments

* addressed comments
parent 789c2f1f
......@@ -6,6 +6,10 @@ 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.StringMessageCodec;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterView;
import java.util.ArrayList;
......@@ -15,6 +19,8 @@ public class MainActivity extends AppCompatActivity {
private int counter;
private static final String CHANNEL = "increment";
private static final String EMPTY_MESSAGE = "";
private static final String PING = "ping";
private FlutterMessageChannel messageChannel;
private String[] getArgsFromIntent(Intent intent) {
// Before adding more entries to this list, consider that arbitrary
......@@ -51,11 +57,15 @@ public class MainActivity extends AppCompatActivity {
flutterView = (FlutterView) findViewById(R.id.flutter_view);
flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null);
flutterView.addOnMessageListener(CHANNEL,
new FlutterView.OnMessageListener() {
messageChannel =
new FlutterMessageChannel<String>(flutterView, CHANNEL, StringMessageCodec.INSTANCE);
messageChannel.
setMessageHandler(new MessageHandler<String>() {
@Override
public String onMessage(FlutterView view, String message) {
return onFlutterIncrement();
public void onMessage(String s, Reply<String> reply) {
onFlutterIncrement();
reply.send(EMPTY_MESSAGE);
}
});
......@@ -69,15 +79,14 @@ public class MainActivity extends AppCompatActivity {
}
private void sendAndroidIncrement() {
flutterView.sendToFlutter(CHANNEL, EMPTY_MESSAGE, null);
messageChannel.send(PING);
}
private String onFlutterIncrement() {
private void onFlutterIncrement() {
counter++;
TextView textView = (TextView) findViewById(R.id.button_tap);
String value = "Flutter button tapped " + counter + (counter == 1 ? " time" : " times");
textView.setText(value);
return EMPTY_MESSAGE;
}
@Override
......
......@@ -31,29 +31,32 @@ class MyHomePage extends StatefulWidget {
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());
int _counter = 0;
Future<String> handlePlatformIncrement(String message) async {
_incrementCounter();
return _emptyMessage;
@override
void initState() {
super.initState();
platform.setMessageHandler(_handlePlatformIncrement);
}
void _incrementCounter() {
Future<String> _handlePlatformIncrement(String message) async {
setState(() {
_counter++;
});
return _emptyMessage;
}
void _sendFlutterIncrement() {
PlatformMessages.sendString(_channel, _emptyMessage);
platform.send(_pong);
}
@override
Widget build(BuildContext context) {
PlatformMessages.setStringMessageHandler(_channel,
handlePlatformIncrement);
return new Scaffold(
body: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
......
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