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

Upgraded to new Android plugin APIs. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ version '1.0-SNAPSHOT'

buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
google()
mavenCentral()
}

dependencies {
Expand All @@ -16,10 +14,8 @@ buildscript {

rootProject.allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
google()
mavenCentral()
}
}

Expand All @@ -29,11 +25,20 @@ 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"
}
lintOptions {
disable 'InvalidPackage'
}

dependencies {
implementation 'androidx.annotation:annotation:1.3.0'
}
}
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.geocoder">
package="com.aloisdeniel.geocoder">
</manifest>
98 changes: 27 additions & 71 deletions android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -135,10 +91,10 @@ protected void onPostExecute(List<Address> 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();
}
Expand All @@ -163,10 +119,10 @@ protected void onPostExecute(List<Address> 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();
}
Expand Down
19 changes: 11 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"