Skip to content

Commit

Permalink
Isolated deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHoogstrate committed Feb 1, 2024
1 parent d5e7974 commit ee449f1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,29 @@ void setLocaleIdentifier(@Nullable Locale locale) {
* Returns a list of Address objects matching the supplied address string.
*
* @param address the address string for the search
* @param callback the GeocodeListenerAdapter that listens for success or error
* @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
* @throws java.io.IOException if the network is unavailable or any other I/O problem occurs.
*/
@SuppressWarnings("deprecation")
void placemarkFromAddress(String address, GeocodeListenerAdapter callback) throws IOException {
void placemarkFromAddress(String address, GeocodeListenerAdapter callback) {
final Geocoder geocoder = createGeocoder(androidContext, locale);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getAddressesWithGeocodeListener(geocoder, address, 5, callback);
} else {
List<Address> addresses = geocoder.getFromLocationName(address, 5);
callback.onGeocode(addresses);
try {
List<Address> addresses = deprecatedGetFromLocationName(geocoder, address);
callback.onGeocode(addresses);
} catch (IOException ex) {
callback.onError(ex.getMessage());
}
}
}

@SuppressWarnings("deprecation")
private List<Address> deprecatedGetFromLocationName(Geocoder geocoder, String address) throws IOException {
return geocoder.getFromLocationName(address, 5);
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
private void getAddressesWithGeocodeListener(Geocoder geocoder, String address, int maxResults, GeocodeListenerAdapter callback) {
geocoder.getFromLocationName(address, maxResults, new Geocoder.GeocodeListener() {
Expand All @@ -70,26 +78,37 @@ public void onError(@Nullable String errorMessage) {
});
}


/**
* Returns a list of Address objects matching the supplied coordinates.
*
* @param latitude the latitude point for the search
* @param longitude the longitude point for the search
* @param callback the GeocodeListenerAdapter that listens for success or error
* @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
* @throws IOException if the network is unavailable or any other I/O problem occurs.
*/
@SuppressWarnings("deprecation")
void placemarkFromCoordinates(
double latitude,
double longitude,
GeocodeListenerAdapter callback
) throws IOException {
) {
final Geocoder geocoder = createGeocoder(androidContext, locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getLocationWithGeocodeListener(geocoder, latitude, longitude, 5, callback);
} else {
callback.onGeocode(geocoder.getFromLocation(latitude, longitude, 5));
}
try {
List<Address> addresses = deprecatedGetFromLocation(geocoder, latitude, longitude);
callback.onGeocode(addresses);
} catch (IOException ex) {
callback.onError(ex.getMessage());
}}
}

@SuppressWarnings("deprecation")
private List<Address> deprecatedGetFromLocation(Geocoder geocoder,
double latitude,
double longitude) throws IOException {
return geocoder.getFromLocation(latitude, longitude, 5);
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ final class MethodCallHandlerImpl implements MethodCallHandler {

@Override
public void onMethodCall(
final MethodCall call,
@NonNull final Result result
final MethodCall call,
@NonNull final Result result
) {
switch (call.method) {
case "setLocaleIdentifier":
Expand Down Expand Up @@ -111,37 +111,28 @@ private void onLocationFromAddress(MethodCall call, Result result) {
null);
}

try {
geocoding.placemarkFromAddress(address, new GeocodeListenerAdapter() {
geocoding.placemarkFromAddress(address, new GeocodeListenerAdapter() {

@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toLocationHashMapList(addresses));
} else {
result.error(
"NOT_FOUND",
String.format("No coordinates found for '%s'", address),
null);
}
}

@Override
public void onError(String errorMessage) {
@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toLocationHashMapList(addresses));
} else {
result.error(
"IO_ERROR",
String.format(errorMessage),
"NOT_FOUND",
String.format("No coordinates found for '%s'", address),
null);
}
});

} catch (IOException ex) {
result.error(
"IO_ERROR",
String.format("A network error occurred trying to lookup the address '%s'.", address),
null
);
}
}

@Override
public void onError(String errorMessage) {
result.error(
"IO_ERROR",
String.format(errorMessage),
null);
}
});
}

private void onPlacemarkFromAddress(final MethodCall call, final Result result) {
Expand All @@ -154,85 +145,62 @@ private void onPlacemarkFromAddress(final MethodCall call, final Result result)
null);
}

try {
geocoding.placemarkFromAddress(address, new GeocodeListenerAdapter() {
geocoding.placemarkFromAddress(address, new GeocodeListenerAdapter() {

@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toAddressHashMapList(addresses));
} else {
result.error(
"NOT_FOUND",
String.format("No coordinates found for '%s'", address),
null);
}
}

@Override
public void onError(String errorMessage) {
@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toAddressHashMapList(addresses));
} else {
result.error(
"IO_ERROR",
String.format(errorMessage),
"NOT_FOUND",
String.format("No coordinates found for '%s'", address),
null);
}
});

} catch (IOException ex) {
result.error(
"IO_ERROR",
String.format("A network error occurred trying to lookup the address '%s'.", address),
null
);
}
}

@Override
public void onError(String errorMessage) {
result.error(
"IO_ERROR",
String.format(errorMessage),
null);
}
});
}

private void onPlacemarkFromCoordinates(final MethodCall call, final Result result) {
final double latitude = call.argument("latitude");
final double longitude = call.argument("longitude");

try {
geocoding.placemarkFromCoordinates(
latitude,
longitude, new GeocodeListenerAdapter() {

@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toAddressHashMapList(addresses));
} else {
result.error(
"NOT_FOUND",
String.format(
Locale.ENGLISH,
"No address information found for supplied coordinates (latitude: %f, longitude: %f).",
latitude,
longitude
),
null);
}
}
geocoding.placemarkFromCoordinates(
latitude,
longitude, new GeocodeListenerAdapter() {

@Override
public void onError(String errorMessage) {
@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toAddressHashMapList(addresses));
} else {
result.error(
"IO_ERROR",
String.format(errorMessage),
"NOT_FOUND",
String.format(
Locale.ENGLISH,
"No address information found for supplied coordinates (latitude: %f, longitude: %f).",
latitude,
longitude
),
null);
}
});
}

} catch (IOException ex) {
result.error(
"IO_ERROR",
String.format(
Locale.ENGLISH,
"A network error occurred trying to lookup the supplied coordinates (latitude: %f, longitude: %f).",
latitude,
longitude
),
null
);
}
@Override
public void onError(String errorMessage) {
result.error(
"IO_ERROR",
String.format(errorMessage),
null);
}
});
}
}

0 comments on commit ee449f1

Please sign in to comment.