Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old platform messaging API #8837

Merged
merged 11 commits into from Mar 17, 2017
2 changes: 1 addition & 1 deletion bin/internal/engine.version
@@ -1 +1 @@
4a5a32466958dab49b9940e4528ee6d523f4a5ac
c4edec741704ace2c4edeff57ac348435cd1f898
@@ -1,62 +1,58 @@
package com.example.flutter;

import android.app.Instrumentation;
import android.graphics.Bitmap;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import io.flutter.view.FlutterView;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.view.FlutterView;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule<ExampleActivity> activityRule =
new ActivityTestRule<>(ExampleActivity.class);
new ActivityTestRule<>(ExampleActivity.class);

@Test
public void testFlutterMessage() {
final Instrumentation instr = InstrumentationRegistry.getInstrumentation();

final JSONObject message = new JSONObject();
final int RANDOM_MIN = 1;
final int RANDOM_MAX = 1000;
try {
message.put("min", RANDOM_MIN);
message.put("max", RANDOM_MAX);
} catch (JSONException e) {
fail(e.getMessage());
}

final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger random = new AtomicInteger();

instr.runOnMainSync(new Runnable() {
public void run() {
final FlutterView flutterView = (FlutterView) activityRule.getActivity().findViewById(
R.id.flutter_view);
flutterView.sendToFlutter("getRandom", message.toString(), new FlutterView.MessageReplyCallback() {
public void onReply(String json) {
try {
JSONObject reply = new JSONObject(json);
random.set(reply.getInt("value"));
} catch (JSONException e) {
fail(e.getMessage());
} finally {
latch.countDown();
}
R.id.flutter_view);
final FlutterMethodChannel randomChannel = new FlutterMethodChannel(flutterView, "random");
randomChannel.invokeMethod("getRandom", Arrays.asList(RANDOM_MIN, RANDOM_MAX), new FlutterMethodChannel.Response() {
@Override
public void success(Object o) {
random.set(((Number) o).intValue());
latch.countDown();
}

@Override
public void error(String code, String message, Object details) {

}
});
}
Expand All @@ -78,7 +74,7 @@ public void testBitmap() {
instr.runOnMainSync(new Runnable() {
public void run() {
final FlutterView flutterView = (FlutterView) activityRule.getActivity().findViewById(
R.id.flutter_view);
R.id.flutter_view);

// Call onPostResume to start the engine's renderer even if the activity
// is paused in the test environment.
Expand All @@ -105,13 +101,23 @@ public void run() {

// Waits on a FlutterView until it is able to produce a bitmap.
private class BitmapPoller {
private final int delayMsec = 1000;
private int triesPending;
private int waitMsec;
private FlutterView flutterView;
private Bitmap bitmap;
private CountDownLatch latch = new CountDownLatch(1);

private final int delayMsec = 1000;
private Runnable checkBitmap = new Runnable() {
public void run() {
bitmap = flutterView.getBitmap();
triesPending--;
if (bitmap != null || triesPending == 0) {
latch.countDown();
} else {
flutterView.postDelayed(checkBitmap, delayMsec);
}
}
};

BitmapPoller(int tries) {
triesPending = tries;
Expand All @@ -127,17 +133,5 @@ Bitmap waitForBitmap() throws InterruptedException {
latch.await(waitMsec, TimeUnit.MILLISECONDS);
return bitmap;
}

private Runnable checkBitmap = new Runnable() {
public void run() {
bitmap = flutterView.getBitmap();
triesPending--;
if (bitmap != null || triesPending == 0) {
latch.countDown();
} else {
flutterView.postDelayed(checkBitmap, delayMsec);
}
}
};
}
}
Expand Up @@ -6,28 +6,26 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterView;

import java.io.File;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;

public class ExampleActivity extends Activity {
private static final String TAG = "ExampleActivity";

private FlutterView flutterView;
private FlutterMethodChannel randomChannel;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -39,20 +37,24 @@ public void onCreate(Bundle savedInstanceState) {
flutterView = (FlutterView) findViewById(R.id.flutter_view);
flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null);

flutterView.addOnMessageListener("getLocation",
new FlutterView.OnMessageListener() {
@Override
public String onMessage(FlutterView view, String message) {
return onGetLocation(message);
FlutterMethodChannel locationChannel = new FlutterMethodChannel(flutterView, "location");
randomChannel = new FlutterMethodChannel(flutterView, "random");

locationChannel.setMethodCallHandler(new FlutterMethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, FlutterMethodChannel.Response response) {
if (methodCall.method.equals("getLocation")) {
getLocation((String) methodCall.arguments, response);
} else {
response.error("unknown method", "Unknown method: " + methodCall.method, null);
}
});
}
});

Button getRandom = (Button) findViewById(R.id.get_random);
getRandom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendGetRandom();
}
public void onClick(View v) { getRandom(); }
});
}

Expand All @@ -76,79 +78,44 @@ protected void onPostResume() {
flutterView.onPostResume();
}

private void sendGetRandom() {
JSONObject message = new JSONObject();
try {
message.put("min", 1);
message.put("max", 1000);
} catch (JSONException e) {
Log.e(TAG, "JSON exception", e);
return;
}

flutterView.sendToFlutter("getRandom", message.toString(),
new FlutterView.MessageReplyCallback() {
@Override
public void onReply(String json) {
onRandomReply(json);
}
});
}
private void getRandom() {
randomChannel.invokeMethod("getRandom", Arrays.asList(1, 1000), new FlutterMethodChannel.Response() {
TextView textView = (TextView) findViewById(R.id.random_value);

private void onRandomReply(String json) {
double value;
try {
JSONObject reply = new JSONObject(json);
value = reply.getDouble("value");
} catch (JSONException e) {
Log.e(TAG, "JSON exception", e);
return;
}
@Override
public void success(Object result) {
textView.setText(result.toString());
}

TextView randomValue = (TextView) findViewById(R.id.random_value);
randomValue.setText(Double.toString(value));
@Override
public void error(String code, String message, Object details) {
textView.setText("Error: " + message);
}
});
}

private String onGetLocation(String json) {
String provider;
try {
JSONObject message = new JSONObject(json);
provider = message.getString("provider");
} catch (JSONException e) {
Log.e(TAG, "JSON exception", e);
return null;
}

private void getLocation(String provider, FlutterMethodChannel.Response response) {
String locationProvider;
if (provider.equals("network")) {
locationProvider = LocationManager.NETWORK_PROVIDER;
} else if (provider.equals("gps")) {
locationProvider = LocationManager.GPS_PROVIDER;
} else {
return null;
response.error("unknown provider", "Unknown location provider: " + provider, null);
return;
}

String permission = "android.permission.ACCESS_FINE_LOCATION";
Location location = null;
if (checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(locationProvider);
}

JSONObject reply = new JSONObject();
try {
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
reply.put("latitude", location.getLatitude());
reply.put("longitude", location.getLongitude());
response.success(Arrays.asList(location.getLatitude(), location.getLongitude()));
} else {
reply.put("latitude", 0);
reply.put("longitude", 0);
response.error("location unavailable", "Location is not available", null);
}
} catch (JSONException e) {
Log.e(TAG, "JSON exception", e);
return null;
} else {
response.error("access error", "Location permissions not granted", null);
}

return reply.toString();
}
}
5 changes: 0 additions & 5 deletions examples/hello_services/ios/Runner.xcodeproj/project.pbxproj
Expand Up @@ -13,7 +13,6 @@
9740EEB41CF90195004384FC /* Flutter.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Flutter.xcconfig */; };
9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; };
9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; };
977505191CFDF23500BC28DA /* LocationProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 977505181CFDF23500BC28DA /* LocationProvider.m */; };
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 97A38A341CFDEC880099F1B4 /* AppDelegate.m */; };
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
Expand Down Expand Up @@ -42,8 +41,6 @@
9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = "<group>"; };
9740EEB81CF902C7004384FC /* app.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = app.dylib; path = Flutter/app.dylib; sourceTree = "<group>"; };
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
977505171CFDF21E00BC28DA /* LocationProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocationProvider.h; sourceTree = "<group>"; };
977505181CFDF23500BC28DA /* LocationProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationProvider.m; sourceTree = "<group>"; };
97A38A331CFDEC680099F1B4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
97A38A341CFDEC880099F1B4 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -105,8 +102,6 @@
97C146F11CF9000F007C117D /* Supporting Files */,
97A38A331CFDEC680099F1B4 /* AppDelegate.h */,
97A38A341CFDEC880099F1B4 /* AppDelegate.m */,
977505171CFDF21E00BC28DA /* LocationProvider.h */,
977505181CFDF23500BC28DA /* LocationProvider.m */,
);
path = Runner;
sourceTree = "<group>";
Expand Down
40 changes: 25 additions & 15 deletions examples/hello_services/ios/Runner/AppDelegate.m
Expand Up @@ -5,24 +5,34 @@
#import "AppDelegate.h"

#import <Flutter/Flutter.h>
#import "LocationProvider.h"
#import <CoreLocation/CoreLocation.h>

@implementation AppDelegate {
LocationProvider* _locationProvider;
CLLocationManager* _locationManager;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project
nibName:nil
bundle:nil];
_locationProvider = [[LocationProvider alloc] init];
[flutterController addMessageListener:_locationProvider];

self.window.rootViewController = flutterController;
[self.window makeKeyAndVisible];
return YES;
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
FlutterViewController* controller =
(FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* locationChannel = [FlutterMethodChannel
methodChannelNamed:@"location"
binaryMessenger:controller
codec:[FlutterStandardMethodCodec sharedInstance]];
[locationChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResultReceiver result) {
if ([@"getLocation" isEqualToString:call.method]) {
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager startMonitoringSignificantLocationChanges];
}
CLLocation* location = _locationManager.location;
result(@[@(location.coordinate.latitude), @(location.coordinate.longitude)], nil);
} else {
result(nil, [FlutterError errorWithCode:@"unknown method"
message:@"Unknown location method called"
details:nil]);
}
}];
return YES;
}

@end