diff --git a/android/build.gradle b/android/build.gradle index 6acb6b4..5581c78 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -27,4 +27,9 @@ android { defaultConfig { minSdkVersion 16 } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } diff --git a/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java b/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java index f7cf971..941e968 100644 --- a/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java +++ b/android/src/main/java/com/aloisdeniel/geocoder/GeocoderPlugin.java @@ -1,11 +1,14 @@ package com.aloisdeniel.geocoder; +import androidx.annotation.NonNull; + import java.util.List; import java.util.ArrayList; import java.util.Map; +import java.util.Collections; import java.util.HashMap; import java.io.IOException; -import java.lang.Exception; +import android.app.Activity; import android.content.Context; import android.location.Address; import android.location.Geocoder; @@ -13,37 +16,32 @@ import android.os.Handler; import android.os.Looper; +import io.flutter.embedding.engine.plugins.FlutterPlugin; 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 - */ -class NotAvailableException extends Exception { - NotAvailableException() {} -} - /** * GeocoderPlugin */ -public class GeocoderPlugin implements MethodCallHandler { +public class GeocoderPlugin implements FlutterPlugin, MethodCallHandler { private Geocoder geocoder; + private MethodChannel channel; - public GeocoderPlugin(Context context) { - - this.geocoder = new Geocoder(context); + @Override + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { + channel = new MethodChannel(binding.getBinaryMessenger(), "github.com/aloisdeniel/geocoder"); + channel.setMethodCallHandler(this); + geocoder = new Geocoder(binding.getApplicationContext()); } - /** - * 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())); + @Override + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { + channel.setMethodCallHandler(null); + } // MethodChannel.Result wrapper that responds on the platform thread. @@ -58,36 +56,18 @@ private static class MethodResultWrapper implements Result { @Override public void success(final Object result) { - handler.post( - new Runnable() { - @Override - public void run() { - methodResult.success(result); - } - }); + handler.post(() -> 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); - } - }); + handler.post(() -> methodResult.error(errorCode, errorMessage, errorDetails)); } @Override public void notImplemented() { - handler.post( - new Runnable() { - @Override - public void run() { - methodResult.notImplemented(); - } - }); + methodResult.notImplemented(); } } @@ -98,11 +78,10 @@ public void onMethodCall(MethodCall call, Result rawResult) { 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); + findAddressesFromCoordinates(latitude, longitude, result); } else { result.notImplemented(); } @@ -115,68 +94,68 @@ private void assertPresent() throws NotAvailableException { } private void findAddressesFromQuery(final String address, final Result result) { - final GeocoderPlugin plugin = this; new AsyncTask>() { - @Override - protected List
doInBackground(Void... params) { - try { - plugin.assertPresent(); - return geocoder.getFromLocationName(address, 20); - } catch (IOException ex) { - return null; - } catch (NotAvailableException ex) { - return new ArrayList<>(); - } + @Override + protected List
doInBackground(Void... params) { + try { + plugin.assertPresent(); + return geocoder.getFromLocationName(address, 20); + } catch (IOException ex) { + return null; + } catch (NotAvailableException ex) { + return new ArrayList<>(); } + } - @Override - protected void onPostExecute(List
addresses) { - if (addresses != null) { - if (addresses.isEmpty()) - result.error("not_available", "Empty", null); + @Override + 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(); } private void findAddressesFromCoordinates(final float latitude, final float longitude, final Result result) { final GeocoderPlugin plugin = this; new AsyncTask>() { - @Override - protected List
doInBackground(Void... params) { - try { - plugin.assertPresent(); - return geocoder.getFromLocation(latitude, longitude, 20); - } catch (IOException ex) { - return null; - } catch (NotAvailableException ex) { - return new ArrayList<>(); - } + @Override + protected List
doInBackground(Void... params) { + try { + plugin.assertPresent(); + return geocoder.getFromLocation(latitude, longitude, 20); + } catch (IOException ex) { + return null; + } catch (NotAvailableException ex) { + return new ArrayList<>(); } + } - @Override - 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); - } + @Override + 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); + } }.execute(); } private Map createCoordinatesMap(Address address) { - if(address == null) - return null; + if (address == null) + return Collections.emptyMap(); - Map result = new HashMap(); + Map result = new HashMap<>(); result.put("latitude", address.getLatitude()); result.put("longitude", address.getLongitude()); @@ -186,8 +165,8 @@ private Map createCoordinatesMap(Address address) { private Map createAddressMap(Address address) { - if(address == null) - return null; + if (address == null) + return Collections.emptyMap(); // Creating formatted address StringBuilder sb = new StringBuilder(); @@ -198,7 +177,7 @@ private Map createAddressMap(Address address) { sb.append(address.getAddressLine(i)); } - Map result = new HashMap(); + Map result = new HashMap<>(); result.put("coordinates", createCoordinatesMap(address)); result.put("featureName", address.getFeatureName()); @@ -218,10 +197,10 @@ private Map createAddressMap(Address address) { private List> createAddressMapList(List
addresses) { - if(addresses == null) - return new ArrayList>(); + if (addresses == null) + return new ArrayList<>(); - List> result = new ArrayList>(addresses.size()); + List> result = new ArrayList<>(addresses.size()); for (Address address : addresses) { result.add(createAddressMap(address)); @@ -231,3 +210,10 @@ private List> createAddressMapList(List
addresses) } } +/** + * NotAvailableException + */ +class NotAvailableException extends Exception { + NotAvailableException() { + } +} diff --git a/lib/geocoder.dart b/lib/geocoder.dart index 1353022..46bdb3f 100644 --- a/lib/geocoder.dart +++ b/lib/geocoder.dart @@ -6,14 +6,7 @@ export 'model.dart'; class Geocoder { static final Geocoding local = LocalGeocoding(); - static Geocoding google( - String apiKey, { - String? language, - Map? headers, - bool preserveHeaderCase = false, - }) => - GoogleGeocoding(apiKey, - language: language, - headers: headers, - preserveHeaderCase: preserveHeaderCase); + static Geocoding google(String apiKey, + {String? language, Map? headers}) => + GoogleGeocoding(apiKey, language: language, headers: headers); } diff --git a/lib/services/distant_google.dart b/lib/services/distant_google.dart index 83b7f55..7147d0e 100644 --- a/lib/services/distant_google.dart +++ b/lib/services/distant_google.dart @@ -1,10 +1,10 @@ import 'dart:async'; import 'dart:convert'; import 'dart:core'; -import 'dart:io'; import 'package:geocoder/model.dart'; import 'package:geocoder/services/base.dart'; +import 'package:http/http.dart' as http; /// Geocoding and reverse geocoding through requests to Google APIs. class GoogleGeocoding implements Geocoding { @@ -12,17 +12,13 @@ class GoogleGeocoding implements Geocoding { final String apiKey; final String? language; - final Map? headers; - final bool preserveHeaderCase; - - final HttpClient _httpClient; + final Map? headers; GoogleGeocoding( this.apiKey, { this.language, this.headers, - this.preserveHeaderCase = false, - }) : _httpClient = HttpClient(); + }); Future> findAddressesFromCoordinates( Coordinates coordinates) async { @@ -40,19 +36,11 @@ class GoogleGeocoding implements Geocoding { Future?> _send(String url) async { //print("Sending $url..."); final uri = Uri.parse(url); - final request = await this._httpClient.getUrl(uri); - if (headers != null) { - headers!.forEach((key, value) { - request.headers.add(key, value, preserveHeaderCase: preserveHeaderCase); - }); - } - final response = await request.close(); - final responseBody = await utf8.decoder.bind(response).join(); - //print("Received $responseBody..."); - var data = jsonDecode(responseBody); + final response = await http.get(uri, headers: headers); - var results = data["results"]; + var data = jsonDecode(response.body); + var results = data["results"]; if (results == null) return null; return results diff --git a/pubspec.yaml b/pubspec.yaml index 98b6625..dabedb4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,11 +4,12 @@ version: 0.3.0 homepage: https://github.com/aloisdeniel/flutter_geocoder environment: - sdk: ">=2.12.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter + http: ^0.13.4 flutter: plugin: