aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/EditWaypointActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/EditWaypointActivity.java')
-rw-r--r--main/src/cgeo/geocaching/EditWaypointActivity.java215
1 files changed, 110 insertions, 105 deletions
diff --git a/main/src/cgeo/geocaching/EditWaypointActivity.java b/main/src/cgeo/geocaching/EditWaypointActivity.java
index 3958d5f..3c8789f 100644
--- a/main/src/cgeo/geocaching/EditWaypointActivity.java
+++ b/main/src/cgeo/geocaching/EditWaypointActivity.java
@@ -11,6 +11,7 @@ import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.DistanceParser;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
+import cgeo.geocaching.ui.dialog.CoordinatesInputDialog;
import cgeo.geocaching.utils.BaseUtils;
import cgeo.geocaching.utils.GeoDirHandler;
import cgeo.geocaching.utils.Log;
@@ -20,6 +21,7 @@ import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@@ -81,7 +83,7 @@ public class EditWaypointActivity extends AbstractActivity {
else {
((EditText) findViewById(R.id.note)).setText(StringUtils.trimToEmpty(waypoint.getNote()));
}
- cgCache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_ONLY);
+ Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_ONLY);
setCoordsModificationVisibility(ConnectorFactory.getConnector(geocode), cache);
}
@@ -112,9 +114,9 @@ public class EditWaypointActivity extends AbstractActivity {
// get parameters
Bundle extras = getIntent().getExtras();
if (extras != null) {
- geocode = extras.getString("geocode");
- wpCount = extras.getInt("count", 0);
- id = extras.getInt("waypoint");
+ geocode = extras.getString(Intents.EXTRA_GEOCODE);
+ wpCount = extras.getInt(Intents.EXTRA_COUNT, 0);
+ id = extras.getInt(Intents.EXTRA_WAYPOINT_ID);
}
if (StringUtils.isBlank(geocode) && id <= 0) {
@@ -159,7 +161,7 @@ public class EditWaypointActivity extends AbstractActivity {
}
if (geocode != null) {
- cgCache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
+ Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
IConnector con = ConnectorFactory.getConnector(geocode);
setCoordsModificationVisibility(con, cache);
}
@@ -169,7 +171,7 @@ public class EditWaypointActivity extends AbstractActivity {
disableSuggestions((EditText) findViewById(R.id.distance));
}
- private void setCoordsModificationVisibility(IConnector con, cgCache cache) {
+ private void setCoordsModificationVisibility(IConnector con, Geocache cache) {
if (cache != null && (cache.getType() == CacheType.MYSTERY || cache.getType() == CacheType.MULTI)) {
findViewById(R.id.modify_cache_coordinates_group).setVisibility(View.VISIBLE);
findViewById(R.id.modify_cache_coordinates_local_and_remote).setVisibility(con.supportsOwnCoordinates() ? View.VISIBLE : View.GONE);
@@ -290,10 +292,10 @@ public class EditWaypointActivity extends AbstractActivity {
} else if (gpTemp != null) {
gp = gpTemp;
}
- cgCache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
- cgeocoords coordsDialog = new cgeocoords(EditWaypointActivity.this, cache, gp, app.currentGeo());
+ Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
+ CoordinatesInputDialog coordsDialog = new CoordinatesInputDialog(EditWaypointActivity.this, cache, gp, app.currentGeo());
coordsDialog.setCancelable(true);
- coordsDialog.setOnCoordinateUpdate(new cgeocoords.CoordinateUpdate() {
+ coordsDialog.setOnCoordinateUpdate(new CoordinatesInputDialog.CoordinateUpdate() {
@Override
public void update(final Geopoint gp) {
((Button) findViewById(R.id.buttonLatitude)).setText(gp.format(GeopointFormatter.Format.LAT_DECMINUTE));
@@ -352,11 +354,17 @@ public class EditWaypointActivity extends AbstractActivity {
}
}
+ public static final int SUCCESS = 0;
+ public static final int UPLOAD_START = 1;
+ public static final int UPLOAD_ERROR = 2;
+ public static final int UPLOAD_NOT_POSSIBLE = 3;
+ public static final int UPLOAD_SUCCESS = 4;
+ public static final int SAVE_ERROR = 5;
+
private class coordsListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
- // TODO Show progress across whole function, it is performing very long time on slower devices
final String bearingText = ((EditText) findViewById(R.id.bearing)).getText().toString();
// combine distance from EditText and distanceUnit saved from Spinner
final String distanceText = ((EditText) findViewById(R.id.distance)).getText().toString() + distanceUnit;
@@ -408,121 +416,118 @@ public class EditWaypointActivity extends AbstractActivity {
coords = coords.project(bearing, distance);
}
- String name = ((EditText) findViewById(R.id.name)).getText().toString().trim();
// if no name is given, just give the waypoint its number as name
- if (StringUtils.isEmpty(name)) {
- name = res.getString(R.string.waypoint) + " " + (wpCount + 1);
- }
+ final String givenName = ((EditText) findViewById(R.id.name)).getText().toString().trim();
+ final String name = StringUtils.isNotEmpty(givenName) ? givenName : res.getString(R.string.waypoint) + " " + (wpCount + 1);
final String note = ((EditText) findViewById(R.id.note)).getText().toString().trim();
+ final Geopoint coordsToSave = coords;
+ final ProgressDialog progress = ProgressDialog.show(EditWaypointActivity.this, getString(R.string.cache), getString(R.string.waypoint_being_saved), true);
+ final Handler finishHandler = new Handler() {
- final Waypoint waypoint = new Waypoint(name, type, own);
- waypoint.setGeocode(geocode);
- waypoint.setPrefix(prefix);
- waypoint.setLookup(lookup);
- waypoint.setCoords(coords);
- waypoint.setNote(note);
- waypoint.setId(id);
-
- cgCache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
- if (null != cache && cache.addOrChangeWaypoint(waypoint, true)) {
- cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
- StaticMapsProvider.removeWpStaticMaps(id, geocode);
- if (Settings.isStoreOfflineWpMaps()) {
- StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, false);
- }
- final RadioButton modifyLocal = (RadioButton) findViewById(R.id.modify_cache_coordinates_local);
- final RadioButton modifyBoth = (RadioButton) findViewById(R.id.modify_cache_coordinates_local_and_remote);
- if (modifyLocal.isChecked() || modifyBoth.isChecked()) {
- if (!cache.hasUserModifiedCoords()) {
- final Waypoint origWaypoint = new Waypoint(cgeoapplication.getInstance().getString(R.string.cache_coordinates_original), WaypointType.ORIGINAL, false);
- origWaypoint.setCoords(cache.getCoords());
- cache.addOrChangeWaypoint(origWaypoint, false);
- cache.setUserModifiedCoords(true);
+ @Override
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case UPLOAD_SUCCESS:
+ showToast(getString(R.string.waypoint_coordinates_has_been_modified_on_website, coordsToSave));
+ //$FALL-THROUGH$
+ case SUCCESS:
+ progress.dismiss();
+ finish();
+ break;
+ case UPLOAD_START:
+ progress.setMessage(getString(R.string.waypoint_coordinates_uploading_to_website, coordsToSave));
+ break;
+ case UPLOAD_ERROR:
+ progress.dismiss();
+ finish();
+ showToast(getString(R.string.waypoint_coordinates_upload_error));
+ break;
+ case UPLOAD_NOT_POSSIBLE:
+ progress.dismiss();
+ finish();
+ showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
+ break;
+ case SAVE_ERROR:
+ progress.dismiss();
+ finish(); //TODO: should we close activity here ?
+ showToast(res.getString(R.string.err_waypoint_add_failed));
+ break;
}
- cache.setCoords(waypoint.getCoords());
- cgData.saveChangedCache(cache);
}
- if (modifyBoth.isChecked() && waypoint.getCoords() != null) {
- if (cache.supportsOwnCoordinates()) {
- final ProgressDialog progress = ProgressDialog.show(EditWaypointActivity.this, getString(R.string.cache), getString(R.string.waypoint_coordinates_uploading_to_website, waypoint.getCoords()), true);
- Handler finishHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- progress.dismiss();
- finish();
+ };
+
+ class SaveWptTask extends AsyncTask<Void, Void, Void> {
+
+ @Override
+ protected Void doInBackground(Void... params) {
+ final Waypoint waypoint = new Waypoint(name, type, own);
+ waypoint.setGeocode(geocode);
+ waypoint.setPrefix(prefix);
+ waypoint.setLookup(lookup);
+ waypoint.setCoords(coordsToSave);
+ waypoint.setNote(note);
+ waypoint.setId(id);
+
+ Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
+ if (null != cache && cache.addOrChangeWaypoint(waypoint, true)) {
+ cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
+ StaticMapsProvider.removeWpStaticMaps(id, geocode);
+ if (Settings.isStoreOfflineWpMaps()) {
+ StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, false);
+ }
+ final RadioButton modifyLocal = (RadioButton) findViewById(R.id.modify_cache_coordinates_local);
+ final RadioButton modifyBoth = (RadioButton) findViewById(R.id.modify_cache_coordinates_local_and_remote);
+ if (modifyLocal.isChecked() || modifyBoth.isChecked()) {
+ if (!cache.hasUserModifiedCoords()) {
+ final Waypoint origWaypoint = new Waypoint(cgeoapplication.getInstance().getString(R.string.cache_coordinates_original), WaypointType.ORIGINAL, false);
+ origWaypoint.setCoords(cache.getCoords());
+ cache.addOrChangeWaypoint(origWaypoint, false);
+ cache.setUserModifiedCoords(true);
}
- };
- new UploadModifiedCoordsThread(cache, waypoint.getCoords(), progress, finishHandler).start();
+ cache.setCoords(waypoint.getCoords());
+ cgData.saveChangedCache(cache);
+ }
+ if (modifyBoth.isChecked() && waypoint.getCoords() != null) {
+ finishHandler.sendEmptyMessage(UPLOAD_START);
+
+ if (cache.supportsOwnCoordinates()) {
+ boolean result = uploadModifiedCoords(cache, waypoint.getCoords());
+ finishHandler.sendEmptyMessage(result ? SUCCESS : UPLOAD_ERROR);
+ } else {
+ showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
+ finishHandler.sendEmptyMessage(UPLOAD_NOT_POSSIBLE);
+ }
+ } else {
+ finishHandler.sendEmptyMessage(SUCCESS);
+ }
} else {
- showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
+ finishHandler.sendEmptyMessage(SAVE_ERROR);
}
- } else {
- finish();
+ return null;
}
- } else {
- showToast(res.getString(R.string.err_waypoint_add_failed));
}
+ new SaveWptTask().execute();
}
}
- private class UploadModifiedCoordsThread extends Thread {
-
- private final Geopoint waypoint_uploaded;
- private final ProgressDialog progress;
- private final cgCache cache;
- private final Handler handler;
-
- public UploadModifiedCoordsThread(cgCache cache, Geopoint wpt, ProgressDialog progress, Handler finishHandler) {
- this.cache = cache;
- this.waypoint_uploaded = wpt;
- this.progress = progress;
- this.handler = finishHandler;
- }
-
- @Override
- public void run() {
- boolean result = false;
- IConnector con = ConnectorFactory.getConnector(cache);
- if (con.supportsOwnCoordinates()) {
- result = con.uploadModifiedCoordinates(cache, waypoint_uploaded);
- }
- final boolean res = result;
- runOnUiThread(new Runnable() {
-
- @Override
- public void run() {
- if (res) {
- showToast(getString(R.string.waypoint_coordinates_has_been_modified_on_website, waypoint_uploaded.getCoords().toString()));
- } else {
- showToast(getString(R.string.waypoint_coordinates_upload_error));
- }
- if (progress != null) {
- progress.dismiss();
- }
- handler.sendMessage(Message.obtain());
- }
- });
- }
+ private static boolean uploadModifiedCoords(final Geocache cache, final Geopoint waypointUploaded) {
+ final IConnector con = ConnectorFactory.getConnector(cache);
+ return con.supportsOwnCoordinates() && con.uploadModifiedCoordinates(cache, waypointUploaded);
}
@Override
- public void goManual(View view) {
- if (id >= 0) {
- ActivityMixin.goManual(this, "c:geo-waypoint-edit");
- } else {
- ActivityMixin.goManual(this, "c:geo-waypoint-new");
- }
+ public void goManual(final View view) {
+ ActivityMixin.goManual(this, id >= 0 ? "c:geo-waypoint-edit" : "c:geo-waypoint-new");
}
public static void startActivityEditWaypoint(final Context context, final int waypointId) {
- final Intent editIntent = new Intent(context, EditWaypointActivity.class);
- editIntent.putExtra("waypoint", waypointId);
- context.startActivity(editIntent);
+ context.startActivity(new Intent(context, EditWaypointActivity.class)
+ .putExtra(Intents.EXTRA_WAYPOINT_ID, waypointId));
}
- public static void startActivityAddWaypoint(final Context context, final cgCache cache) {
- final Intent addWptIntent = new Intent(context, EditWaypointActivity.class);
- addWptIntent.putExtra("geocode", cache.getGeocode()).putExtra("count", cache.getWaypoints().size());
- context.startActivity(addWptIntent);
+ public static void startActivityAddWaypoint(final Context context, final Geocache cache) {
+ context.startActivity(new Intent(context, EditWaypointActivity.class)
+ .putExtra(Intents.EXTRA_GEOCODE, cache.getGeocode())
+ .putExtra(Intents.EXTRA_COUNT, cache.getWaypoints().size()));
}
}