From 73f597fb5450c4aa463fab6f17eaeef06b2bcf21 Mon Sep 17 00:00:00 2001 From: Daniele Date: Wed, 23 Feb 2022 22:06:28 +0100 Subject: [PATCH] Open Google Maps on Google devices --- .idea/deploymentTargetDropDown.xml | 17 ++++ app/src/main/AndroidManifest.xml | 3 + .../detail/GeofavoriteDetailActivity.java | 2 +- .../activity/mappicker/MapPickerActivity.java | 8 +- .../nextcloudmaps/model/Geofavorite.java | 5 +- .../nextcloudmaps/utils/GeoUriParser.java | 80 ++++++++++++++----- .../nextcloudmaps/utils/GoogleMapsUri.java | 33 -------- .../nextcloudmaps/utils/IntentGenerator.java | 12 ++- 8 files changed, 99 insertions(+), 61 deletions(-) create mode 100644 .idea/deploymentTargetDropDown.xml delete mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GoogleMapsUri.java diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..48a78bc --- /dev/null +++ b/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d18d7dc..0eda7f7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,10 +21,13 @@ package="it.danieleverducci.nextcloudmaps"> + + + = 180 ) - throw new IllegalArgumentException("Invalid longitude: " + lon); - if (lat <= -90 || lat >= 90) - throw new IllegalArgumentException("Invalid latitude: " + lat); + public static Uri createGeoUri(double lat, double lon, String name) { + String error = checkCoordsValidity(lat, lon); + if (error != null) + throw new IllegalArgumentException(error); String uriStr = "geo:" + lat + "," + lon; if (name != null) uriStr += "(" + name + ")"; return Uri.parse(uriStr); } + + public static Uri createGmapsUri(double lat, double lon) { + String error = checkCoordsValidity(lat, lon); + if (error != null) + throw new IllegalArgumentException(error); + + String uriStr = "https://www.google.com/maps/search/?api=1&query=" + lat + "," + lon; + return Uri.parse(uriStr); + } + + /** + * Checks a latitude/longitude is valid + * @param lat latitude + * @param lon longitude + * @return null if valid, a string containing an error otherwise + */ + private static String checkCoordsValidity(double lat, double lon) { + // Check coords validity + if (lon <= -180 || lon >= 180 ) + return "Invalid longitude: " + lon; + if (lat <= -90 || lat >= 90) + return "Invalid latitude: " + lat; + return null; + } } diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GoogleMapsUri.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GoogleMapsUri.java deleted file mode 100644 index 444d26a..0000000 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GoogleMapsUri.java +++ /dev/null @@ -1,33 +0,0 @@ -package it.danieleverducci.nextcloudmaps.utils; - -import android.content.Context; -import android.content.pm.PackageManager; -import android.net.Uri; - -/** - * Google maps doesn't honor geouri standard, instead implements its own uri. - * A geouri opens google maps, but the position is ignored, so it is needed to use gmaps own uri. - * Utility to check if gmaps is installed and generate a gmaps uri - */ -public class GoogleMapsUri { - - public static boolean isGoogleMapsInstalled(Context context) { - try { - context.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0); - return true; - } catch (PackageManager.NameNotFoundException e) { - return false; - } - } - - public static Uri createGmapsUri(double lat, double lon) { - // Check coords validity - if (lon <= -180 || lon >= 180 ) - throw new IllegalArgumentException("Invalid longitude: " + lon); - if (lat <= -90 || lat >= 90) - throw new IllegalArgumentException("Invalid latitude: " + lat); - - String uriStr = "https://www.google.com/maps/search/?api=1&query=" + lat + "," + lon; - return Uri.parse(uriStr); - } -} diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/IntentGenerator.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/IntentGenerator.java index d491fbf..67c9712 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/IntentGenerator.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/IntentGenerator.java @@ -2,6 +2,7 @@ package it.danieleverducci.nextcloudmaps.utils; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.util.Log; import it.danieleverducci.nextcloudmaps.R; @@ -22,7 +23,16 @@ public class IntentGenerator { public static Intent newGeoUriIntent(Context context, Geofavorite item) { Intent i = new Intent(); i.setAction(Intent.ACTION_VIEW); - i.setData(item.getGeoUri()); + i.setData(isGoogleMapsInstalled(context) ? item.getGmapsUri() : item.getGeoUri()); return i; } + + public static boolean isGoogleMapsInstalled(Context context) { + try { + context.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0); + return true; + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } }