Skip to content

Commit

Permalink
Revert "Remove old platform messaging API (#8837)"
Browse files Browse the repository at this point in the history
This reverts commit dce4bf8.
  • Loading branch information
abarth committed Mar 17, 2017
1 parent dce4bf8 commit dd249c4
Show file tree
Hide file tree
Showing 39 changed files with 553 additions and 576 deletions.
2 changes: 1 addition & 1 deletion bin/internal/engine.version
@@ -1 +1 @@
c4edec741704ace2c4edeff57ac348435cd1f898
4a5a32466958dab49b9940e4528ee6d523f4a5ac
@@ -1,58 +1,62 @@
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 org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import io.flutter.view.FlutterView;

import java.util.Arrays;
import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
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 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;
import static org.junit.Assert.*;

@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);
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) {

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();
}
}
});
}
Expand All @@ -74,7 +78,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 @@ -101,23 +105,13 @@ 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 Runnable checkBitmap = new Runnable() {
public void run() {
bitmap = flutterView.getBitmap();
triesPending--;
if (bitmap != null || triesPending == 0) {
latch.countDown();
} else {
flutterView.postDelayed(checkBitmap, delayMsec);
}
}
};

private final int delayMsec = 1000;

BitmapPoller(int tries) {
triesPending = tries;
Expand All @@ -133,5 +127,17 @@ 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,26 +6,28 @@

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.util.Arrays;
import java.io.File;
import org.json.JSONException;
import org.json.JSONObject;

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 @@ -37,24 +39,20 @@ public void onCreate(Bundle savedInstanceState) {
flutterView = (FlutterView) findViewById(R.id.flutter_view);
flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null);

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);
flutterView.addOnMessageListener("getLocation",
new FlutterView.OnMessageListener() {
@Override
public String onMessage(FlutterView view, String message) {
return onGetLocation(message);
}
}
});
});

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

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

private void getRandom() {
randomChannel.invokeMethod("getRandom", Arrays.asList(1, 1000), new FlutterMethodChannel.Response() {
TextView textView = (TextView) findViewById(R.id.random_value);
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;
}

@Override
public void success(Object result) {
textView.setText(result.toString());
}
flutterView.sendToFlutter("getRandom", message.toString(),
new FlutterView.MessageReplyCallback() {
@Override
public void onReply(String json) {
onRandomReply(json);
}
});
}

@Override
public void error(String code, String message, Object details) {
textView.setText("Error: " + message);
}
});
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;
}

TextView randomValue = (TextView) findViewById(R.id.random_value);
randomValue.setText(Double.toString(value));
}

private void getLocation(String provider, FlutterMethodChannel.Response response) {
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;
}

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

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

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

return reply.toString();
}
}
5 changes: 5 additions & 0 deletions examples/hello_services/ios/Runner.xcodeproj/project.pbxproj
Expand Up @@ -13,6 +13,7 @@
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 @@ -41,6 +42,8 @@
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 @@ -102,6 +105,8 @@
97C146F11CF9000F007C117D /* Supporting Files */,
97A38A331CFDEC680099F1B4 /* AppDelegate.h */,
97A38A341CFDEC880099F1B4 /* AppDelegate.m */,
977505171CFDF21E00BC28DA /* LocationProvider.h */,
977505181CFDF23500BC28DA /* LocationProvider.m */,
);
path = Runner;
sourceTree = "<group>";
Expand Down
40 changes: 15 additions & 25 deletions examples/hello_services/ios/Runner/AppDelegate.m
Expand Up @@ -5,34 +5,24 @@
#import "AppDelegate.h"

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

@implementation AppDelegate {
CLLocationManager* _locationManager;
LocationProvider* _locationProvider;
}
- (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;

- (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;
}

@end

0 comments on commit dd249c4

Please sign in to comment.