From 7978dad043275ffc1b272739687ca9214fceedce Mon Sep 17 00:00:00 2001 From: Norbert Truchsess Date: Sun, 16 Feb 2020 22:59:14 +0100 Subject: [PATCH] add support for Google-maps urls --- .gitignore | 7 +++ app/src/main/assets/nominatim.properties | 1 + .../com/truchsess/send2car/MainActivity.java | 7 ++- .../com/truchsess/send2car/geo/GeoUrl.java | 48 +++++++++++++++---- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..755f7dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.iml +.gradle +/local.properties +/.idea +/app/build +/app/release +/build diff --git a/app/src/main/assets/nominatim.properties b/app/src/main/assets/nominatim.properties index a08f8e7..9f6c59e 100644 --- a/app/src/main/assets/nominatim.properties +++ b/app/src/main/assets/nominatim.properties @@ -14,6 +14,7 @@ cafe Cafe car_rental Autovermietung car_wash Autowaschanlage casino Spielkasino +charging_station Ladestation cinema Kino clinic Krankenhaus club Klub diff --git a/app/src/main/java/com/truchsess/send2car/MainActivity.java b/app/src/main/java/com/truchsess/send2car/MainActivity.java index 3645e7f..b983405 100644 --- a/app/src/main/java/com/truchsess/send2car/MainActivity.java +++ b/app/src/main/java/com/truchsess/send2car/MainActivity.java @@ -315,10 +315,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin if (Intent.ACTION_VIEW.equals(action)) { final Uri uri = intent.getData(); - final String scheme = uri.getScheme(); - if (scheme.equals("geo") || scheme.equals("google.navigation")) { - mServiceMessageController.setGeoUrlFromUri(uri); - } + + mServiceMessageController.setGeoUrlFromUri(uri); + final GeoUrl geoUrl = mServiceMessageController.getGeoUrl(); if (mGeoFragment != null) { diff --git a/app/src/main/java/com/truchsess/send2car/geo/GeoUrl.java b/app/src/main/java/com/truchsess/send2car/geo/GeoUrl.java index ab377d8..59877d8 100644 --- a/app/src/main/java/com/truchsess/send2car/geo/GeoUrl.java +++ b/app/src/main/java/com/truchsess/send2car/geo/GeoUrl.java @@ -8,7 +8,7 @@ import java.util.regex.Pattern; /********************************************************************************************** - Copyright (C) 2018 Norbert Truchsess norbert.truchsess@t-online.de + Copyright (C) 2020 Norbert Truchsess norbert.truchsess@t-online.de This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -37,19 +37,30 @@ public GeoUrl(final double lat, final double lon, final String description) { this.description = description; } - public void fromUri(Uri uri) { + public void fromUri(final Uri uri) { - String data = uri.getSchemeSpecificPart(); + final String scheme = uri.getScheme(); + + if (scheme.equals("geo") || scheme.equals("google.navigation")) { + fromGeoUri(uri); + } else if (scheme.equals("http") || scheme.equals("https")) { + fromHttpUri(uri); + } + } + + public void fromGeoUri(final Uri uri) { + + final String data = uri.getSchemeSpecificPart(); // format is: // ,?z= // 0,0?q=,(label) int qpos = data.indexOf("?"); - String resource = qpos < 0 ? data : data.substring(0, qpos); - String query = data.substring(qpos+1); + final String resource = qpos < 0 ? data : data.substring(0, qpos); + final String query = data.substring(qpos+1); - Matcher resMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)$").matcher(resource); + final Matcher resMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)$").matcher(resource); boolean resMatches = resMatcher.matches(); lat = resMatches ? Double.parseDouble(resMatcher.group(1)) : Double.NaN; @@ -57,14 +68,14 @@ public void fromUri(Uri uri) { description = null; - StringTokenizer queryTokenizer = new StringTokenizer(query,"&"); + final StringTokenizer queryTokenizer = new StringTokenizer(query,"&"); while (queryTokenizer.hasMoreTokens()) { final String nextToken = queryTokenizer.nextToken(); - Matcher paramMatcher = Pattern.compile("^q=(.*)$",Pattern.DOTALL).matcher(nextToken); + final Matcher paramMatcher = Pattern.compile("^q=(.*)$",Pattern.DOTALL).matcher(nextToken); if (paramMatcher.matches()) { final String q0 = paramMatcher.group(1); final String q1 = q0.replaceAll("[\\n\\r\\t\\f]",", "); - Matcher locationMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)(\\((.*)\\)|)+$").matcher(q1); + final Matcher locationMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)(\\((.*)\\)|)+$").matcher(q1); if (locationMatcher.matches()) { lat = Double.parseDouble(locationMatcher.group(1)); lon = Double.parseDouble(locationMatcher.group(2)); @@ -77,6 +88,25 @@ public void fromUri(Uri uri) { } } + public void fromHttpUri(final Uri uri) { + // formats of google-maps uris: + // https://www.google.com/maps?q=&ll=, + // https://maps.google.com/?q=&ll=, + // https://www.google.com/maps/@,,z + + if (uri.getHost().contains("google")) { + description = uri.getQueryParameter("q"); + final String ll = uri.getQueryParameter("ll"); + if (ll != null && !ll.isEmpty()) { + final Matcher locationMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)$").matcher(ll); + if (locationMatcher.matches()) { + lat = Double.parseDouble(locationMatcher.group(1)); + lon = Double.parseDouble(locationMatcher.group(2)); + } + } + } + } + public boolean isValid() { return !Double.isNaN(lat) && !Double.isNaN(lon); }