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