diff --git a/library/src/main/java/com/patloew/rxlocation/Geocoding.java b/library/src/main/java/com/patloew/rxlocation/Geocoding.java index 97b3cfe..59859b9 100644 --- a/library/src/main/java/com/patloew/rxlocation/Geocoding.java +++ b/library/src/main/java/com/patloew/rxlocation/Geocoding.java @@ -4,8 +4,10 @@ import android.location.Address; import android.location.Geocoder; import android.location.Location; +import android.support.annotation.NonNull; import java.util.List; +import java.util.Locale; import io.reactivex.Maybe; import io.reactivex.Single; @@ -28,50 +30,90 @@ public class Geocoding { private static final Function, Maybe
> ADDRESS_MAYBE_FUNCTION = addresses -> addresses.isEmpty() ? Maybe.empty(): Maybe.just(addresses.get(0)); - private final android.location.Geocoder geocoder; + private final Context context; Geocoding(Context context) { - this.geocoder = getGeocoder(context); + this.context = context; } - Geocoder getGeocoder(Context context) { - return new Geocoder(context); + Geocoder getGeocoder(Locale locale) { + if(locale != null) { + return new Geocoder(context, locale); + } else { + return new Geocoder(context); + } } - public Maybe
fromLocation(Location location) { - return fromLocation(location, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + + public Maybe
fromLocation(@NonNull Location location) { + return fromLocation(null, location, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Maybe
fromLocation(Locale locale, @NonNull Location location) { + return fromLocation(locale, location, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Single> fromLocation(@NonNull Location location, int maxResults) { + return fromLocation(null, location, maxResults); } - public Single> fromLocation(Location location, int maxResults) { - return Single.fromCallable(() -> geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), maxResults)); + public Single> fromLocation(Locale locale, @NonNull Location location, int maxResults) { + return Single.fromCallable(() -> getGeocoder(locale).getFromLocation(location.getLatitude(), location.getLongitude(), maxResults)); } + public Maybe
fromLocation(double latitude, double longitude) { - return fromLocation(latitude, longitude, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + return fromLocation(null, latitude, longitude, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Maybe
fromLocation(Locale locale, double latitude, double longitude) { + return fromLocation(locale, latitude, longitude, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); } public Single> fromLocation(double latitude, double longitude, int maxResults) { - return Single.fromCallable(() -> geocoder.getFromLocation(latitude, longitude, maxResults)); + return fromLocation(null, latitude, longitude, maxResults); + } + + public Single> fromLocation(Locale locale, double latitude, double longitude, int maxResults) { + return Single.fromCallable(() -> getGeocoder(locale).getFromLocation(latitude, longitude, maxResults)); } - public Maybe
fromLocationName(String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) { - return fromLocationName(locationName, 1, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + + public Maybe
fromLocationName(@NonNull String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) { + return fromLocationName(null, locationName, 1, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Maybe
fromLocationName(Locale locale, @NonNull String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) { + return fromLocationName(locale, locationName, 1, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Single> fromLocationName(@NonNull String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) { + return fromLocationName(null, locationName, maxResults, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude); } - public Single> fromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) { - return Single.fromCallable(() -> geocoder.getFromLocationName(locationName, maxResults, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude)); + public Single> fromLocationName(Locale locale, @NonNull String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) { + return Single.fromCallable(() -> getGeocoder(locale).getFromLocationName(locationName, maxResults, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude)); } - public Maybe
fromLocationName(String locationName) { - return fromLocationName(locationName, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + + public Maybe
fromLocationName(@NonNull String locationName) { + return fromLocationName(null, locationName, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Maybe
fromLocationName(Locale locale, @NonNull String locationName) { + return fromLocationName(locale, locationName, 1).flatMapMaybe(ADDRESS_MAYBE_FUNCTION); + } + + public Single> fromLocationName(@NonNull String locationName, int maxResults) { + return fromLocationName(null, locationName, maxResults); } - public Single> fromLocationName(String locationName, int maxResults) { - return Single.fromCallable(() -> geocoder.getFromLocationName(locationName, maxResults)); + public Single> fromLocationName(Locale locale, @NonNull String locationName, int maxResults) { + return Single.fromCallable(() -> getGeocoder(locale).getFromLocationName(locationName, maxResults)); } } diff --git a/library/src/main/java/com/patloew/rxlocation/LocationAvailabilitySingle.java b/library/src/main/java/com/patloew/rxlocation/LocationAvailabilitySingle.java index 55e50b1..bd0fe73 100644 --- a/library/src/main/java/com/patloew/rxlocation/LocationAvailabilitySingle.java +++ b/library/src/main/java/com/patloew/rxlocation/LocationAvailabilitySingle.java @@ -6,8 +6,6 @@ import com.google.android.gms.location.LocationAvailability; import com.google.android.gms.location.LocationServices; -import java.util.concurrent.TimeoutException; - import io.reactivex.SingleEmitter; /* Copyright 2016 Patrick Löwenstein diff --git a/library/src/main/java/com/patloew/rxlocation/LocationSettingsNotSatisfiedException.java b/library/src/main/java/com/patloew/rxlocation/LocationSettingsNotSatisfiedException.java index 9fd3f9a..7738901 100644 --- a/library/src/main/java/com/patloew/rxlocation/LocationSettingsNotSatisfiedException.java +++ b/library/src/main/java/com/patloew/rxlocation/LocationSettingsNotSatisfiedException.java @@ -1,7 +1,5 @@ package com.patloew.rxlocation; -import com.google.android.gms.common.api.Status; - /* Copyright (C) 2015 Michał Charmas (http://blog.charmas.pl) * * Licensed under the Apache License, Version 2.0 (the "License");