Move LocationView.addressToLocation() to GeocoderThread.

This commit is contained in:
Andreas Schildbach 2018-12-21 19:23:59 +01:00
parent 653c5afad1
commit 68f6908b40
4 changed files with 32 additions and 28 deletions

View file

@ -486,7 +486,7 @@ public class DirectionsActivity extends OeffiMainActivity implements ActivityCom
new GeocoderThread(DirectionsActivity.this, p.getLatitude(), p.getLongitude(), new GeocoderThread(DirectionsActivity.this, p.getLatitude(), p.getLongitude(),
new GeocoderThread.Callback() { new GeocoderThread.Callback() {
public void onGeocoderResult(final Address address) { public void onGeocoderResult(final Address address) {
pinLocation = LocationView.addressToLocation(address); pinLocation = GeocoderThread.addressToLocation(address);
locationView.setLocation(pinLocation); locationView.setLocation(pinLocation);
locationView.setShowLocationType(false); locationView.setShowLocationType(false);
} }

View file

@ -168,7 +168,7 @@ public class DirectionsShortcutActivity extends OeffiActivity
public void onLocation(final Point here) { public void onLocation(final Point here) {
new GeocoderThread(DirectionsShortcutActivity.this, here, new GeocoderThread.Callback() { new GeocoderThread(DirectionsShortcutActivity.this, here, new GeocoderThread.Callback() {
public void onGeocoderResult(final Address address) { public void onGeocoderResult(final Address address) {
final Location location = LocationView.addressToLocation(address); final Location location = GeocoderThread.addressToLocation(address);
query(location); query(location);
} }

View file

@ -19,7 +19,6 @@ package de.schildbach.oeffi.directions;
import java.util.Locale; import java.util.Locale;
import com.google.common.base.Joiner;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import de.schildbach.oeffi.Constants; import de.schildbach.oeffi.Constants;
@ -340,7 +339,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
new GeocoderThread(getContext(), coord, new GeocoderThread.Callback() { new GeocoderThread(getContext(), coord, new GeocoderThread.Callback() {
public void onGeocoderResult(final Address address) { public void onGeocoderResult(final Address address) {
if (locationType == LocationType.COORD) { if (locationType == LocationType.COORD) {
setLocation(addressToLocation(address)); setLocation(GeocoderThread.addressToLocation(address));
hint = null; hint = null;
} }
} }
@ -446,30 +445,6 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
other.fireChanged(); other.fireChanged();
} }
public static Location addressToLocation(final Address address) {
final Point coord;
if (address.hasLatitude() && address.hasLongitude())
coord = Point.fromDouble(address.getLatitude(), address.getLongitude());
else
coord = null;
final Location location;
final int maxAddressLineIndex = address.getMaxAddressLineIndex();
if (maxAddressLineIndex >= 2 && Strings.emptyToNull(address.getAddressLine(2)) != null) {
location = new Location(LocationType.ADDRESS, null, coord, address.getAddressLine(1),
address.getAddressLine(0));
} else if (address.getThoroughfare() != null) {
final Joiner joiner = Joiner.on(' ').skipNulls();
location = new Location(LocationType.ADDRESS, null, coord,
joiner.join(address.getPostalCode(), address.getLocality()),
joiner.join(address.getThoroughfare(), address.getFeatureName()));
} else {
location = new Location(LocationType.ADDRESS, null, coord);
}
return location;
}
private void fireChanged() { private void fireChanged() {
if (listener != null) if (listener != null)
listener.changed(); listener.changed();

View file

@ -27,6 +27,11 @@ import java.util.Locale;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.Point; import de.schildbach.pte.dto.Point;
import android.content.Context; import android.content.Context;
@ -109,4 +114,28 @@ public class GeocoderThread extends Thread {
} }
}); });
} }
public static Location addressToLocation(final Address address) {
final Point coord;
if (address.hasLatitude() && address.hasLongitude())
coord = Point.fromDouble(address.getLatitude(), address.getLongitude());
else
coord = null;
final Location location;
final int maxAddressLineIndex = address.getMaxAddressLineIndex();
if (maxAddressLineIndex >= 2 && Strings.emptyToNull(address.getAddressLine(2)) != null) {
location = new Location(LocationType.ADDRESS, null, coord, address.getAddressLine(1),
address.getAddressLine(0));
} else if (address.getThoroughfare() != null) {
final Joiner joiner = Joiner.on(' ').skipNulls();
location = new Location(LocationType.ADDRESS, null, coord,
joiner.join(address.getPostalCode(), address.getLocality()),
joiner.join(address.getThoroughfare(), address.getFeatureName()));
} else {
location = new Location(LocationType.ADDRESS, null, coord);
}
return location;
}
} }