Skip to content

Commit 390993d

Browse files
authored
PlatformXxxChannel concepts added to support Flutter/platform interop (flutter#8394)
New concepts: PlatformMessageChannel (basic message send/receive superseding some existing PlatformMessages methods), PlatformMethodChannel (method invocation and event streams), pluggable codecs for messages and method calls: unencoded binary, string, json, and 'standard' flutter binary encoding.
1 parent 41d8113 commit 390993d

File tree

15 files changed

+1206
-71
lines changed

15 files changed

+1206
-71
lines changed

bin/internal/engine.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7f25cd0d65ca52a5fddb5f41abf5b82acbe14085
1+
74de13c0bde4eeb967391bd2a7ba973c525113b1

examples/platform_services/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.example.platformservices"
3+
package="com.example.flutter"
44
android:versionCode="1"
55
android:versionName="0.0.1">
66

examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,60 @@
44

55
package com.example.flutter;
66

7-
import android.app.Activity;
87
import android.content.Context;
9-
import android.content.Intent;
10-
import android.content.pm.ApplicationInfo;
118
import android.content.pm.PackageManager;
129
import android.location.Location;
1310
import android.location.LocationManager;
1411
import android.os.Bundle;
15-
import android.util.Log;
16-
import android.view.View;
17-
import android.widget.Button;
18-
import android.widget.TextView;
1912

2013
import io.flutter.app.FlutterActivity;
21-
import io.flutter.view.FlutterMain;
14+
import io.flutter.plugin.common.FlutterMethodChannel;
15+
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
16+
import io.flutter.plugin.common.FlutterMethodChannel.Response;
17+
import io.flutter.plugin.common.MethodCall;
2218
import io.flutter.view.FlutterView;
2319

24-
import java.io.File;
25-
import org.json.JSONException;
26-
import org.json.JSONObject;
27-
2820
public class ExampleActivity extends FlutterActivity {
29-
private static final String TAG = "ExampleActivity";
3021
private FlutterView flutterView;
3122

3223
@Override
3324
public void onCreate(Bundle savedInstanceState) {
3425
super.onCreate(savedInstanceState);
35-
36-
flutterView = getFlutterView();
37-
flutterView.addOnMessageListener("getLocation",
38-
new FlutterView.OnMessageListener() {
39-
@Override
40-
public String onMessage(FlutterView view, String message) {
41-
return onGetLocation(message);
26+
new FlutterMethodChannel(getFlutterView(), "geo").setMethodCallHandler(new MethodCallHandler() {
27+
@Override
28+
public void onMethodCall(MethodCall call, Response response) {
29+
if (call.method.equals("getLocation")) {
30+
if (!(call.arguments instanceof String)) {
31+
throw new IllegalArgumentException("Invalid argument type, String expected");
32+
}
33+
getLocation((String) call.arguments, response);
34+
} else {
35+
throw new IllegalArgumentException("Unknown method " + call.method);
4236
}
43-
});
37+
}
38+
});
4439
}
4540

46-
private String onGetLocation(String json) {
47-
String provider;
48-
try {
49-
JSONObject message = new JSONObject(json);
50-
provider = message.getString("provider");
51-
} catch (JSONException e) {
52-
Log.e(TAG, "JSON exception", e);
53-
return null;
54-
}
55-
41+
private void getLocation(String provider, Response response) {
5642
String locationProvider;
5743
if (provider.equals("network")) {
5844
locationProvider = LocationManager.NETWORK_PROVIDER;
5945
} else if (provider.equals("gps")) {
6046
locationProvider = LocationManager.GPS_PROVIDER;
6147
} else {
62-
return null;
48+
throw new IllegalArgumentException("Unknown provider " + provider);
6349
}
64-
6550
String permission = "android.permission.ACCESS_FINE_LOCATION";
66-
Location location = null;
6751
if (checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
6852
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
69-
location = locationManager.getLastKnownLocation(locationProvider);
70-
}
71-
72-
JSONObject reply = new JSONObject();
73-
try {
53+
Location location = locationManager.getLastKnownLocation(locationProvider);
7454
if (location != null) {
75-
reply.put("latitude", location.getLatitude());
76-
reply.put("longitude", location.getLongitude());
55+
response.success(new double[] { location.getLatitude(), location.getLongitude() });
7756
} else {
78-
reply.put("latitude", 0);
79-
reply.put("longitude", 0);
57+
response.error("unknown", "Location unknown", null);
8058
}
81-
} catch (JSONException e) {
82-
Log.e(TAG, "JSON exception", e);
83-
return null;
59+
} else {
60+
response.error("permission", "Access denied", null);
8461
}
85-
86-
return reply.toString();
8762
}
88-
}
63+
}

examples/platform_services/lib/main.dart

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class PlatformServices extends StatefulWidget {
1313
}
1414

1515
class _PlatformServicesState extends State<PlatformServices> {
16-
double _latitude;
17-
double _longitude;
16+
Future<dynamic> _locationRequest;
1817

1918
@override
2019
Widget build(BuildContext context) {
@@ -26,28 +25,42 @@ class _PlatformServicesState extends State<PlatformServices> {
2625
new Text('Hello from Flutter!'),
2726
new RaisedButton(
2827
child: new Text('Get Location'),
29-
onPressed: _getLocation
28+
onPressed: _requestLocation,
3029
),
31-
new Text('Latitude: $_latitude, Longitude: $_longitude'),
32-
]
33-
)
34-
)
30+
new FutureBuilder<dynamic>(
31+
future: _locationRequest,
32+
builder: _buildLocation,
33+
),
34+
],
35+
),
36+
),
3537
);
3638
}
3739

38-
Future<Null> _getLocation() async {
39-
final Map<String, String> message = <String, String>{'provider': 'network'};
40-
final Map<String, dynamic> reply = await PlatformMessages.sendJSON('getLocation', message);
41-
// If the widget was removed from the tree while the message was in flight,
42-
// we want to discard the reply rather than calling setState to update our
43-
// non-existent appearance.
44-
if (!mounted)
45-
return;
40+
void _requestLocation() {
4641
setState(() {
47-
_latitude = reply['latitude'].toDouble();
48-
_longitude = reply['longitude'].toDouble();
42+
_locationRequest = const PlatformMethodChannel('geo').invokeMethod(
43+
'getLocation',
44+
'network',
45+
);
4946
});
5047
}
48+
49+
Widget _buildLocation(BuildContext context, AsyncSnapshot<dynamic> snapshot) {
50+
switch (snapshot.connectionState) {
51+
case ConnectionState.none:
52+
return new Text('Press button to request location');
53+
case ConnectionState.waiting:
54+
return new Text('Awaiting response...');
55+
default:
56+
try {
57+
final List<double> location = snapshot.requireData;
58+
return new Text('Lat. ${location[0]}, Long. ${location[1]}');
59+
} on PlatformException catch (e) {
60+
return new Text('Request failed: ${e.message}');
61+
}
62+
}
63+
}
5164
}
5265

5366
void main() {

packages/flutter/lib/foundation.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ export 'src/foundation/licenses.dart';
2424
export 'src/foundation/observer_list.dart';
2525
export 'src/foundation/platform.dart';
2626
export 'src/foundation/print.dart';
27+
export 'src/foundation/serialization.dart';
2728
export 'src/foundation/synchronous_future.dart';

packages/flutter/lib/services.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ library services;
1313
export 'src/services/asset_bundle.dart';
1414
export 'src/services/binding.dart';
1515
export 'src/services/clipboard.dart';
16+
export 'src/services/message_codec.dart';
17+
export 'src/services/message_codecs.dart';
1618
export 'src/services/haptic_feedback.dart';
1719
export 'src/services/image_cache.dart';
1820
export 'src/services/image_decoder.dart';
1921
export 'src/services/image_provider.dart';
2022
export 'src/services/image_resolution.dart';
2123
export 'src/services/image_stream.dart';
2224
export 'src/services/path_provider.dart';
25+
export 'src/services/platform_channel.dart';
2326
export 'src/services/platform_messages.dart';
2427
export 'src/services/raw_keyboard.dart';
2528
export 'src/services/system_chrome.dart';

0 commit comments

Comments
 (0)