WIP coordinates parsing

Not working ATM
This commit is contained in:
Daniele Verducci (Slimpenguin) 2022-01-11 08:54:19 +01:00
parent c3e2496596
commit d009237679
3 changed files with 91 additions and 9 deletions

View File

@ -1,19 +1,25 @@
package it.danieleverducci.nextcloudmaps.activity.mappicker; package it.danieleverducci.nextcloudmaps.activity.mappicker;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
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.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.osmdroid.api.IGeoPoint; import org.osmdroid.api.IGeoPoint;
import org.osmdroid.api.IMapController; import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration; import org.osmdroid.config.Configuration;
import org.osmdroid.config.IConfigurationProvider; import org.osmdroid.config.IConfigurationProvider;
import org.osmdroid.events.MapEventsReceiver;
import org.osmdroid.events.MapListener; import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent; import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent; import org.osmdroid.events.ZoomEvent;
@ -21,7 +27,8 @@ import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.CustomZoomButtonsController; import org.osmdroid.views.CustomZoomButtonsController;
import org.osmdroid.views.MapView; import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection; import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.MapEventsOverlay;
import java.util.Locale;
import it.danieleverducci.nextcloudmaps.BuildConfig; import it.danieleverducci.nextcloudmaps.BuildConfig;
import it.danieleverducci.nextcloudmaps.R; import it.danieleverducci.nextcloudmaps.R;
@ -29,8 +36,11 @@ import it.danieleverducci.nextcloudmaps.databinding.ActivityMapPickerBinding;
public class MapPickerActivity extends AppCompatActivity { public class MapPickerActivity extends AppCompatActivity {
public static final String TAG = "MapPickerActivity"; public static final String TAG = "MapPickerActivity";
private static final int PERMISSION_REQUEST_CODE = 8888;
private ViewHolder mViewHolder; private ViewHolder mViewHolder;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -42,12 +52,35 @@ 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());
Location l = getLastKnownPosition();
if (l != null)
mViewHolder.centerMapOn(l.getLatitude(), l.getLongitude());
setContentView(mViewHolder.getRootView()); setContentView(mViewHolder.getRootView());
} }
/**
* May return last known GPS position or null. Used to center the map.
* @return last known GPS position or null
*/
private Location getLastKnownPosition() {
// Check if user granted location permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// User didn't grant permission. Ask it.
requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
return null;
}
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// Try to use last available location
return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
private class ViewHolder implements View.OnClickListener { private class ViewHolder implements View.OnClickListener {
private final ActivityMapPickerBinding binding; private final ActivityMapPickerBinding binding;
private final MapView map; private final MapView map;
private boolean coordsEditMode = false;
public ViewHolder(LayoutInflater inflater) { public ViewHolder(LayoutInflater inflater) {
this.binding = ActivityMapPickerBinding.inflate(inflater); this.binding = ActivityMapPickerBinding.inflate(inflater);
@ -59,7 +92,7 @@ public class MapPickerActivity extends AppCompatActivity {
if (!focused) if (!focused)
return; return;
(ViewHolder.this).binding.latlonConfirmBtn.setVisibility(View.VISIBLE); (ViewHolder.this).coordsEditMode(true);
} }
}; };
this.binding.latEt.setOnFocusChangeListener(latlonFocusListener); this.binding.latEt.setOnFocusChangeListener(latlonFocusListener);
@ -69,16 +102,17 @@ public class MapPickerActivity extends AppCompatActivity {
this.map = this.binding.map; this.map = this.binding.map;
this.map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER); this.map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
this.map.setMultiTouchControls(true); this.map.setMultiTouchControls(true);
IMapController mapController = binding.map.getController();
mapController.setZoom(7.0f);
this.map.addMapListener(new MapListener() { this.map.addMapListener(new MapListener() {
@Override @Override
public boolean onScroll(ScrollEvent event) { public boolean onScroll(ScrollEvent event) {
// Disable edit mode
if ((ViewHolder.this).coordsEditMode)
(ViewHolder.this).coordsEditMode(false);
// Convert XY coords to LatLng // Convert XY coords to LatLng
Projection p = (ViewHolder.this).map.getProjection(); Projection p = (ViewHolder.this).map.getProjection();
IGeoPoint igp = p.fromPixels(event.getX(), event.getY()); IGeoPoint igp = p.fromPixels(event.getX(), event.getY());
(ViewHolder.this).binding.latEt.setText(String.format("%.06f", igp.getLatitude())); (ViewHolder.this).binding.latEt.setText(String.format(Locale.ENGLISH, "%.06f", igp.getLatitude()));
(ViewHolder.this).binding.lonEt.setText(String.format("%.06f", igp.getLongitude())); (ViewHolder.this).binding.lonEt.setText(String.format(Locale.ENGLISH, "%.06f", igp.getLongitude()));
return false; return false;
} }
@ -87,12 +121,19 @@ public class MapPickerActivity extends AppCompatActivity {
return false; return false;
} }
}); });
IMapController mapController = binding.map.getController();
mapController.setZoom(7.0f);
// Setup onClick // Setup onClick
this.binding.latlonConfirmBtn.setOnClickListener(this); this.binding.latlonConfirmBtn.setOnClickListener(this);
} }
public void centerMapOn(Double lat, Double lon ) {
IMapController mapController = binding.map.getController();
mapController.setCenter(new GeoPoint(lat, lon));
}
public View getRootView() { public View getRootView() {
return this.binding.root; return this.binding.root;
} }
@ -100,10 +141,43 @@ public class MapPickerActivity extends AppCompatActivity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (view == this.binding.latlonConfirmBtn) { if (view == this.binding.latlonConfirmBtn) {
// TODO: Move map this.coordsEditMode(false);
view.setVisibility(View.GONE); Double lat;
Double lon;
try {
lat = Double.parseDouble(this.binding.latEt.getText().toString());
lon = Double.parseDouble(this.binding.lonEt.getText().toString());
} catch (NumberFormatException e) {
Log.e(TAG, "Unable to parse coordinates: " + e.getLocalizedMessage());
Toast.makeText(MapPickerActivity.this, R.string.coordinates_parse_error, Toast.LENGTH_SHORT).show();
return;
} }
// Validate coordinates
if (lon <= -180 || lon >= 180 || lat <= -90 || lat >= 90) {
Toast.makeText(MapPickerActivity.this, R.string.coordinates_invalid_error, Toast.LENGTH_SHORT).show();
return;
}
// Move map to coordinates
this.centerMapOn(lat, lon);
} }
} }
/**
* Enters/exits coordinates edit mode.
* On exit, removes focus from the coordinates ET and hides the button
*/
private void coordsEditMode(boolean active) {
this.coordsEditMode = active;
View btn = this.binding.latlonConfirmBtn;
if (active) {
btn.setVisibility(View.VISIBLE);
} else {
this.binding.latEt.clearFocus();
this.binding.lonEt.clearFocus();
btn.setVisibility(View.GONE);
}
}
}
} }

View File

@ -63,6 +63,10 @@
<string name="geofavorite_saved">Geosegnalibro salvato</string> <string name="geofavorite_saved">Geosegnalibro salvato</string>
<string name="incomplete_geofavorite">Geosegnalibro incompleto: nome e categoria sono obbligatori</string> <string name="incomplete_geofavorite">Geosegnalibro incompleto: nome e categoria sono obbligatori</string>
<!-- Map picker activity -->
<string name="coordinates_parse_error">Le coordinate dovrebbero essere nel formato xx.xxxxxx</string>
<string name="coordinates_invalid_error">Coordinate non valide</string>
<!-- About --> <!-- About -->
<string name="about_version_title">Versione</string> <string name="about_version_title">Versione</string>
<string name="about_version">Stai usando la versione &lt;strong>%1$s&lt;/strong></string> <string name="about_version">Stai usando la versione &lt;strong>%1$s&lt;/strong></string>

View File

@ -62,6 +62,10 @@
<string name="geofavorite_saved">Geofavorite saved</string> <string name="geofavorite_saved">Geofavorite saved</string>
<string name="incomplete_geofavorite">Incomplete geofavorite: Name and category are mandatory</string> <string name="incomplete_geofavorite">Incomplete geofavorite: Name and category are mandatory</string>
<!-- Map picker activity -->
<string name="coordinates_parse_error">Coordinates should be in format xx.xxxxxx</string>
<string name="coordinates_invalid_error">Invalid coordinates</string>
<!-- About --> <!-- About -->
<string name="about_version_title">Version</string> <string name="about_version_title">Version</string>
<string name="about_version">You are currently using &lt;strong>%1$s&lt;/strong></string> <string name="about_version">You are currently using &lt;strong>%1$s&lt;/strong></string>