Skip to content

Commit

Permalink
Merge pull request aloisdeniel#15 from Eimji/master
Browse files Browse the repository at this point in the history
Migration to AndroidX, add support for language in GoogleGeocoding and fix issue with @UiThread
  • Loading branch information
aloisdeniel authored Jul 17, 2019
2 parents 8c6a178 + bc34cfe commit bcbde29
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 23 deletions.
11 changes: 4 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.3.0'
}
}

Expand All @@ -26,15 +26,12 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28
//buildToolsVersion '28.0.3'

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand Down Expand Up @@ -44,8 +46,55 @@ public static void registerWith(Registrar registrar) {
channel.setMethodCallHandler(new GeocoderPlugin(registrar.context()));
}

// MethodChannel.Result wrapper that responds on the platform thread.
private static class MethodResultWrapper implements Result {
private Result methodResult;
private Handler handler;

MethodResultWrapper(Result result) {
methodResult = result;
handler = new Handler(Looper.getMainLooper());
}

@Override
public void success(final Object result) {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.success(result);
}
});
}

@Override
public void error(
final String errorCode, final String errorMessage, final Object errorDetails) {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.error(errorCode, errorMessage, errorDetails);
}
});
}

@Override
public void notImplemented() {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.notImplemented();
}
});
}
}

@Override
public void onMethodCall(MethodCall call, Result result) {
public void onMethodCall(MethodCall call, Result rawResult) {
Result result = new MethodResultWrapper(rawResult);

if (call.method.equals("findAddressesFromQuery")) {
String address = (String) call.argument("address");
findAddressesFromQuery(address, result);
Expand Down
14 changes: 7 additions & 7 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28
//buildToolsVersion '25.0.3'

lintOptions {
disable 'InvalidPackage'
Expand All @@ -26,10 +26,10 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.yourcompany.geocoderexample"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -46,7 +46,7 @@ flutter {
}

dependencies {
androidTestCompile 'com.android.support:support-annotations:25.4.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'androidx.annotation:annotation:1.0.1'
androidTestCompile 'androidx.test:runner:1.1.1'
androidTestCompile 'androidx.test:rules:1.1.1'
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.3.0'
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
2 changes: 1 addition & 1 deletion lib/geocoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export 'model.dart';

class Geocoder {
static final Geocoding local = LocalGeocoding();
static Geocoding google(String apiKey) => GoogleGeocoding(apiKey);
static Geocoding google(String apiKey, { String language }) => GoogleGeocoding(apiKey, language: language);
}
9 changes: 5 additions & 4 deletions lib/services/distant_google.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class GoogleGeocoding implements Geocoding {
static const _host = 'https://maps.google.com/maps/api/geocode/json';

final String apiKey;
final String language;

final HttpClient _httpClient;

GoogleGeocoding(this.apiKey) :
GoogleGeocoding(this.apiKey, { this.language }) :
_httpClient = HttpClient(),
assert(apiKey != null, "apiKey must not be null");

Future<List<Address>> findAddressesFromCoordinates(Coordinates coordinates) async {
final url = '$_host?key=$apiKey&latlng=${coordinates.latitude},${coordinates.longitude}';
final url = '$_host?key=$apiKey${language != null ? '&language='+language : ''}&latlng=${coordinates.latitude},${coordinates.longitude}';
return _send(url);
}

Expand All @@ -31,12 +32,12 @@ class GoogleGeocoding implements Geocoding {
}

Future<List<Address>> _send(String url) async {
print("Sending $url...");
//print("Sending $url...");
final uri = Uri.parse(url);
final request = await this._httpClient.getUrl(uri);
final response = await request.close();
final responseBody = await response.transform(utf8.decoder).join();
print("Received $responseBody...");
//print("Received $responseBody...");
var data = jsonDecode(responseBody);

var results = data["results"];
Expand Down

0 comments on commit bcbde29

Please sign in to comment.