WIP Intent for geobookmark creation

This commit is contained in:
Daniele Verducci (Slimpenguin) 2022-01-12 09:00:05 +01:00
parent d888485fc2
commit 15c11a33ee
3 changed files with 63 additions and 1 deletions

View File

@ -2,13 +2,16 @@ package it.danieleverducci.nextcloudmaps.activity.mappicker;
import android.Manifest; import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.Location; import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -32,7 +35,9 @@ import java.util.Locale;
import it.danieleverducci.nextcloudmaps.BuildConfig; import it.danieleverducci.nextcloudmaps.BuildConfig;
import it.danieleverducci.nextcloudmaps.R; import it.danieleverducci.nextcloudmaps.R;
import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity;
import it.danieleverducci.nextcloudmaps.databinding.ActivityMapPickerBinding; import it.danieleverducci.nextcloudmaps.databinding.ActivityMapPickerBinding;
import it.danieleverducci.nextcloudmaps.utils.GeoUriParser;
public class MapPickerActivity extends AppCompatActivity { public class MapPickerActivity extends AppCompatActivity {
public static final String TAG = "MapPickerActivity"; public static final String TAG = "MapPickerActivity";
@ -52,6 +57,21 @@ public class MapPickerActivity extends AppCompatActivity {
osmdroidConfig.setUserAgentValue(BuildConfig.APPLICATION_ID); osmdroidConfig.setUserAgentValue(BuildConfig.APPLICATION_ID);
mViewHolder = new MapPickerActivity.ViewHolder(getLayoutInflater()); 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(); Location l = getLastKnownPosition();
if (l != null) if (l != null)
mViewHolder.centerMapOn(l.getLatitude(), l.getLongitude()); mViewHolder.centerMapOn(l.getLatitude(), l.getLongitude());
@ -79,6 +99,8 @@ public class MapPickerActivity extends AppCompatActivity {
private class ViewHolder implements View.OnClickListener { private class ViewHolder implements View.OnClickListener {
private final ActivityMapPickerBinding binding; private final ActivityMapPickerBinding binding;
private ViewEventListener listener;
private final MapView map; private final MapView map;
private boolean coordsEditMode = false; private boolean coordsEditMode = false;
@ -125,9 +147,15 @@ public class MapPickerActivity extends AppCompatActivity {
// Setup onClick // Setup onClick
this.binding.latlonConfirmBtn.setOnClickListener(this); 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 ) { public void centerMapOn(Double lat, Double lon ) {
IMapController mapController = binding.map.getController(); IMapController mapController = binding.map.getController();
mapController.setCenter(new GeoPoint(lat, lon)); mapController.setCenter(new GeoPoint(lat, lon));
@ -159,6 +187,17 @@ public class MapPickerActivity extends AppCompatActivity {
// Move map to coordinates // Move map to coordinates
this.centerMapOn(lat, lon); 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.latEt.clearFocus();
this.binding.lonEt.clearFocus(); this.binding.lonEt.clearFocus();
btn.setVisibility(View.GONE); 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();
}
} }

View File

@ -36,6 +36,8 @@ import org.threeten.bp.ZoneId;
import java.io.Serializable; import java.io.Serializable;
import java.util.Comparator; import java.util.Comparator;
import it.danieleverducci.nextcloudmaps.utils.GeoUriParser;
public class Geofavorite implements Serializable { public class Geofavorite implements Serializable {
public static final String DEFAULT_CATEGORY = "Personal"; public static final String DEFAULT_CATEGORY = "Personal";
private static final double EARTH_RADIUS = 6371; // https://en.wikipedia.org/wiki/Earth_radius 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() { 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() { public boolean valid() {

View File

@ -28,4 +28,17 @@ public class GeoUriParser {
throw new IllegalArgumentException("unable to parse uri"); 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);
}
} }