From 958bd8009c8b3768309bc8fb09ad2899f04a0926 Mon Sep 17 00:00:00 2001 From: Brent Kleineibst Date: Sun, 28 Nov 2021 16:56:24 -0400 Subject: [PATCH] Upgraded to new Android plugin APIs. --- android/build.gradle | 21 ++-- android/src/main/AndroidManifest.xml | 2 +- .../aloisdeniel/geocoder/GeocoderPlugin.java | 98 +++++-------------- pubspec.yaml | 19 ++-- 4 files changed, 52 insertions(+), 88 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 4761cf6..d7ab57b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -3,10 +3,8 @@ version '1.0-SNAPSHOT' buildscript { repositories { - jcenter() - maven { - url "https://maven.google.com" - } + google() + mavenCentral() } dependencies { @@ -16,10 +14,8 @@ buildscript { rootProject.allprojects { repositories { - jcenter() - maven { - url "https://maven.google.com" - } + google() + mavenCentral() } } @@ -29,6 +25,11 @@ android { compileSdkVersion 28 //buildToolsVersion '28.0.3' + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + defaultConfig { minSdkVersion 16 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -36,4 +37,8 @@ android { lintOptions { disable 'InvalidPackage' } + + dependencies { + implementation 'androidx.annotation:annotation:1.3.0' + } } diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 4895e49..c587f1c 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,3 @@ + package="com.aloisdeniel.geocoder"> diff --git a/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java b/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java index f7cf971..ddeb6ba 100644 --- a/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java +++ b/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java @@ -12,12 +12,14 @@ import android.os.AsyncTask; import android.os.Handler; import android.os.Looper; +import androidx.annotation.NonNull; +import android.content.Context; +import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; -import io.flutter.plugin.common.MethodCall; -import io.flutter.plugin.common.PluginRegistry.Registrar; /** * NotAvailableException @@ -29,77 +31,26 @@ class NotAvailableException extends Exception { /** * GeocoderPlugin */ -public class GeocoderPlugin implements MethodCallHandler { +public class GeocoderPlugin implements FlutterPlugin, MethodCallHandler { + private MethodChannel channel; + private Context context; private Geocoder geocoder; - public GeocoderPlugin(Context context) { - + @Override + public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { + channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "github.com/aloisdeniel/geocoder"); + channel.setMethodCallHandler(this); + context = flutterPluginBinding.getApplicationContext(); this.geocoder = new Geocoder(context); } - /** - * Plugin registration. - */ - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), "github.com/aloisdeniel/geocoder"); - 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 rawResult) { - Result result = new MethodResultWrapper(rawResult); - + public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { if (call.method.equals("findAddressesFromQuery")) { String address = (String) call.argument("address"); findAddressesFromQuery(address, result); - } - else if (call.method.equals("findAddressesFromCoordinates")) { + } else if (call.method.equals("findAddressesFromCoordinates")) { float latitude = ((Number) call.argument("latitude")).floatValue(); float longitude = ((Number) call.argument("longitude")).floatValue(); findAddressesFromCoordinates(latitude,longitude, result); @@ -108,6 +59,11 @@ else if (call.method.equals("findAddressesFromCoordinates")) { } } + @Override + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { + channel.setMethodCallHandler(null); + } + private void assertPresent() throws NotAvailableException { if (!geocoder.isPresent()) { throw new NotAvailableException(); @@ -135,10 +91,10 @@ protected void onPostExecute(List
addresses) { if (addresses != null) { if (addresses.isEmpty()) result.error("not_available", "Empty", null); - - else result.success(createAddressMapList(addresses)); - } - else result.error("failed", "Failed", null); + else + result.success(createAddressMapList(addresses)); + } else + result.error("failed", "Failed", null); } }.execute(); } @@ -163,10 +119,10 @@ protected void onPostExecute(List
addresses) { if (addresses != null) { if (addresses.isEmpty()) result.error("not_available", "Empty", null); - - else result.success(createAddressMapList(addresses)); - } - else result.error("failed", "Failed", null); + else + result.success(createAddressMapList(addresses)); + } else + result.error("failed", "Failed", null); } }.execute(); } diff --git a/pubspec.yaml b/pubspec.yaml index 8c9cc6f..2962f20 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,18 +3,21 @@ description: Forward and reverse geocoding. version: 0.2.2-nullsafety homepage: https://github.com/JCKodel/flutter_geocoder +environment: + sdk: ">=2.12.0 <3.0.0" + flutter: ">=1.20.0" + +dependencies: + flutter: + sdk: flutter + meta: ^1.1.6 + flutter: plugin: platforms: android: package: com.aloisdeniel.geocoder pluginClass: GeocoderPlugin + ios: + pluginClass: GeocoderPlugin -dependencies: - flutter: - sdk: flutter - meta: ^1.1.6 - -environment: - flutter: ">=2.0.0" - sdk: ">=2.12.0-0 <3.0.0"