diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/mappicker/MapPickerActivity.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/mappicker/MapPickerActivity.java index 52d2cea..8031407 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/mappicker/MapPickerActivity.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/mappicker/MapPickerActivity.java @@ -2,13 +2,16 @@ package it.danieleverducci.nextcloudmaps.activity.mappicker; import android.Manifest; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.view.inputmethod.InputMethodManager; import android.widget.Toast; import androidx.annotation.Nullable; @@ -32,7 +35,9 @@ import java.util.Locale; import it.danieleverducci.nextcloudmaps.BuildConfig; import it.danieleverducci.nextcloudmaps.R; +import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity; import it.danieleverducci.nextcloudmaps.databinding.ActivityMapPickerBinding; +import it.danieleverducci.nextcloudmaps.utils.GeoUriParser; public class MapPickerActivity extends AppCompatActivity { public static final String TAG = "MapPickerActivity"; @@ -52,6 +57,21 @@ public class MapPickerActivity extends AppCompatActivity { osmdroidConfig.setUserAgentValue(BuildConfig.APPLICATION_ID); mViewHolder = new MapPickerActivity.ViewHolder(getLayoutInflater()); + mViewHolder.setViewEventListener(new ViewEventListener() { + @Override + public void onExitButtonPressed() { + finish(); + } + + @Override + public void onConfirmButtonPressed() { + double[] coords = mViewHolder.getCurrentCoordinates(); + Uri geoUri = GeoUriParser.createUri(coords[0], coords[1], null); + Intent i = new Intent(MapPickerActivity.this, GeofavoriteDetailActivity.class); + i.setData(geoUri); + startActivity(i); + } + }); Location l = getLastKnownPosition(); if (l != null) mViewHolder.centerMapOn(l.getLatitude(), l.getLongitude()); @@ -79,6 +99,8 @@ public class MapPickerActivity extends AppCompatActivity { private class ViewHolder implements View.OnClickListener { private final ActivityMapPickerBinding binding; + private ViewEventListener listener; + private final MapView map; private boolean coordsEditMode = false; @@ -125,9 +147,15 @@ public class MapPickerActivity extends AppCompatActivity { // Setup onClick this.binding.latlonConfirmBtn.setOnClickListener(this); + this.binding.backBt.setOnClickListener(this); + this.binding.okBt.setOnClickListener(this); } + public void setViewEventListener(ViewEventListener listener) { + this.listener = listener; + } + public void centerMapOn(Double lat, Double lon ) { IMapController mapController = binding.map.getController(); mapController.setCenter(new GeoPoint(lat, lon)); @@ -159,6 +187,17 @@ public class MapPickerActivity extends AppCompatActivity { // Move map to coordinates this.centerMapOn(lat, lon); } + + if (view == this.binding.backBt) + listener.onExitButtonPressed(); + + if (view == this.binding.okBt) + listener.onConfirmButtonPressed(); + } + + public double[] getCurrentCoordinates() { + IGeoPoint igp = (ViewHolder.this).map.getMapCenter(); + return new double[]{igp.getLatitude(), igp.getLongitude()}; } /** @@ -174,9 +213,17 @@ public class MapPickerActivity extends AppCompatActivity { this.binding.latEt.clearFocus(); this.binding.lonEt.clearFocus(); btn.setVisibility(View.GONE); + // Hide soft keyboard + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(btn.getWindowToken(), 0); } } } + protected interface ViewEventListener { + public void onExitButtonPressed(); + public void onConfirmButtonPressed(); + } + } diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java index 51d129b..ea8827c 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java @@ -36,6 +36,8 @@ import org.threeten.bp.ZoneId; import java.io.Serializable; import java.util.Comparator; +import it.danieleverducci.nextcloudmaps.utils.GeoUriParser; + public class Geofavorite implements Serializable { public static final String DEFAULT_CATEGORY = "Personal"; private static final double EARTH_RADIUS = 6371; // https://en.wikipedia.org/wiki/Earth_radius @@ -162,7 +164,7 @@ public class Geofavorite implements Serializable { } public Uri getGeoUri() { - return Uri.parse("geo:" + this.lat + "," + this.lng + "(" + this.name + ")"); + return GeoUriParser.createUri(this.lat, this.lng, this.name); } public boolean valid() { diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeoUriParser.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeoUriParser.java index b4e64d6..8e64eb6 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeoUriParser.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeoUriParser.java @@ -28,4 +28,17 @@ public class GeoUriParser { throw new IllegalArgumentException("unable to parse uri"); } } + + public static Uri createUri(double lat, double lon, String name) { + // 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 = "geo:" + lat + "," + lon; + if (name != null) + uriStr += "(" + name + ")"; + return Uri.parse(uriStr); + } }