Skip to content

Commit

Permalink
Merge branch 'support_geo_uris_as_geocode_input'
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Jan 9, 2025
2 parents 8934bef + 9f096e2 commit a3d7b19
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 199 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/blau/android/GeoUrlActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.content.Intent;
import android.net.Uri;
import de.blau.android.util.GeoUrlData;
import de.blau.android.util.GeoUriData;

/**
* Start vespucci with geo: URLs. see http://www.ietf.org/rfc/rfc5870.txt
Expand All @@ -13,7 +13,7 @@ public class GeoUrlActivity extends UrlActivity {

@Override
boolean setIntentExtras(Intent intent, Uri data) {
GeoUrlData geoUrlData = GeoUrlData.parse(data.getSchemeSpecificPart());
GeoUriData geoUrlData = GeoUriData.parse(data.getSchemeSpecificPart());
if (geoUrlData != null) {
intent.putExtra(GEODATA, geoUrlData);
return true;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/de/blau/android/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
import de.blau.android.filter.Filter;
import de.blau.android.filter.PresetFilter;
import de.blau.android.filter.TagFilter;
import de.blau.android.geocode.CoordinatesOrOLC;
import de.blau.android.geocode.GeocodeInput;
import de.blau.android.geocode.Search.SearchResult;
import de.blau.android.gpx.TrackPoint;
import de.blau.android.imageryoffset.ImageryAlignmentActionModeCallback;
Expand Down Expand Up @@ -184,7 +184,7 @@
import de.blau.android.util.FileUtil;
import de.blau.android.util.FullScreenAppCompatActivity;
import de.blau.android.util.GeoMath;
import de.blau.android.util.GeoUrlData;
import de.blau.android.util.GeoUriData;
import de.blau.android.util.Geometry;
import de.blau.android.util.LatLon;
import de.blau.android.util.MenuUtil;
Expand Down Expand Up @@ -345,7 +345,7 @@ public void onReceive(Context context, Intent intent) {

private Queue<Intent> newIntents = new LinkedList<>();
private final Object newIntentsLock = new Object();
private GeoUrlData geoData = null;
private GeoUriData geoData = null;
private RemoteControlUrlData rcData = null;
private Uri contentUri = null;
private String contentUriType = null;
Expand Down Expand Up @@ -936,7 +936,7 @@ private void checkPermission(@NonNull final String permission, @NonNull final Li
*/
private void getIntentData() {
synchronized (newIntentsLock) {
geoData = Util.getSerializableExtra(getIntent(), GeoUrlActivity.GEODATA, GeoUrlData.class);
geoData = Util.getSerializableExtra(getIntent(), GeoUrlActivity.GEODATA, GeoUriData.class);
rcData = Util.getSerializableExtra(getIntent(), RemoteControlUrlActivity.RCDATA, RemoteControlUrlData.class);
shortcutExtras = getIntent().getBundleExtra(Splash.SHORTCUT_EXTRAS_KEY);
Uri uri = getIntent().getData();
Expand Down Expand Up @@ -1227,7 +1227,7 @@ protected void onPostExecute(Note result) {
*
* @param geoData the data from the intent
*/
void processGeoIntent(@NonNull final GeoUrlData geoData) {
void processGeoIntent(@NonNull final GeoUriData geoData) {
final Logic logic = App.getLogic();
final ViewBox viewBox = logic.getViewBox();
final double lon = geoData.getLon();
Expand Down Expand Up @@ -2139,7 +2139,7 @@ public void onError(Context context) {
return true;
case R.id.menu_gps_goto_coordinates:
descheduleAutoLock();
CoordinatesOrOLC.get(this, new CoordinatesOrOLC.HandleResult() {
GeocodeInput.get(this, new GeocodeInput.HandleResult() {
@Override
public void onSuccess(LatLon ll) {
runOnUiThread(() -> {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/blau/android/ShareOnOpenStreetMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.net.Uri;
import androidx.annotation.NonNull;
import de.blau.android.contract.Urls;
import de.blau.android.util.GeoUrlData;
import de.blau.android.util.GeoUriData;

/**
* Take a geo intent and open the location on OSM
Expand All @@ -13,7 +13,7 @@ public class ShareOnOpenStreetMap extends IntentDataActivity {

@Override
protected void process(@NonNull Uri data) {
GeoUrlData geoUrlData = GeoUrlData.parse(data.getSchemeSpecificPart());
GeoUriData geoUrlData = GeoUriData.parse(data.getSchemeSpecificPart());
if (geoUrlData != null) {
double lat = geoUrlData.getLat();
double lon = geoUrlData.getLon();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.blau.android.geocode;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
Expand All @@ -13,28 +15,32 @@
import com.google.openlocationcode.OpenLocationCode.CodeArea;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import de.blau.android.App;
import de.blau.android.R;
import de.blau.android.contract.Schemes;
import de.blau.android.dialogs.TextLineDialog;
import de.blau.android.geocode.Search.SearchResult;
import de.blau.android.osm.ViewBox;
import de.blau.android.util.CoordinateParser;
import de.blau.android.util.ExecutorTask;
import de.blau.android.util.GeoUriData;
import de.blau.android.util.LatLon;
import de.blau.android.util.NetworkStatus;

/**
* Ask the user for coordinates or an OLC for example WF8Q+WF Praia, Cabo Verde
* Ask the user for input Supported are coordinates or an OLC for example WF8Q+WF Praia, Cabo Verde, or an geo: Uri
*
* @author simon
*
*/
public class CoordinatesOrOLC {
public class GeocodeInput {

protected static final String DEBUG_TAG = CoordinatesOrOLC.class.getSimpleName().substring(0, Math.min(23, CoordinatesOrOLC.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, GeocodeInput.class.getSimpleName().length());
protected static final String DEBUG_TAG = GeocodeInput.class.getSimpleName().substring(0, TAG_LEN);

private static final Pattern OLC_SHORT = Pattern.compile("^([23456789CFGHJMPQRVWX]{4,6}\\+[23456789CFGHJMPQRVWX]{2,3})\\s*(.*)$",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Expand Down Expand Up @@ -92,34 +98,17 @@ protected LatLon doInBackground(String param) {
return CoordinateParser.parseVerbatimCoordinates(text);
} catch (ParseException pex) {
try {
OpenLocationCode olc = null;
Matcher m = OLC_FULL.matcher(text);
if (m.find()) {
olc = new OpenLocationCode(m.group(1));
} else {
m = OLC_SHORT.matcher(text);
if (m.find()) {
olc = new OpenLocationCode(m.group(1));
final String loc = m.group(2);
if (!"".equals(loc)) { // user has supplied a location
olc = recoverLocation(context, handler, olc, loc);
} else { // relative to screen center
ViewBox box = App.getLogic().getViewBox();
if (box != null) {
double[] c = box.getCenter();
olc = olc.recover(c[1], c[0]);
}
}
return parseOLC(context, handler, text);
} catch (Exception e) {
try {
Uri uri = Uri.parse(text);
if (Schemes.GEO.equals(uri.getScheme())) {
return GeoUriData.parse(uri.getSchemeSpecificPart()).getLatLon();
}
} catch (Exception e2) {
Log.e(DEBUG_TAG, e.getMessage());
handler.onError(context.getString(R.string.unparseable_coordinates));
}
if (olc == null) {
throw new IOException("Unparseable OLC " + text);
}
CodeArea ca = olc.decode();
return new LatLon(ca.getCenterLatitude(), ca.getCenterLongitude());
} catch (Exception e) {
Log.e(DEBUG_TAG, e.getMessage());
handler.onError(context.getString(R.string.unparseable_coordinates));
}
}
return null;
Expand All @@ -144,6 +133,43 @@ private static void dismiss() {
}
}

/**
* Parse an OLC (code)
*
* @param context an android context
* @param handler handler for errors
* @param text the input text
* @return a LatLon object
* @throws IOException on any kind of errror
*/
private static LatLon parseOLC(@NonNull final Context context, @NonNull final HandleResult handler, @NonNull String text) throws IOException {
OpenLocationCode olc = null;
Matcher m = OLC_FULL.matcher(text);
if (m.find()) {
olc = new OpenLocationCode(m.group(1));
} else {
m = OLC_SHORT.matcher(text);
if (m.find()) {
olc = new OpenLocationCode(m.group(1));
final String loc = m.group(2);
if (!"".equals(loc)) { // user has supplied a location
olc = recoverLocation(context, handler, olc, loc);
} else { // relative to screen center
ViewBox box = App.getLogic().getViewBox();
if (box != null) {
double[] c = box.getCenter();
olc = olc.recover(c[1], c[0]);
}
}
}
}
if (olc == null) {
throw new IOException("Unparseable OLC " + text);
}
CodeArea ca = olc.decode();
return new LatLon(ca.getCenterLatitude(), ca.getCenterLongitude());
}

/**
* Recover coordinates for a location from Nominatim and update the provided OLC
*
Expand Down
Loading

0 comments on commit a3d7b19

Please sign in to comment.