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.java176
1 files changed, 109 insertions, 67 deletions
diff --git a/main/src/cgeo/geocaching/EditWaypointActivity.java b/main/src/cgeo/geocaching/EditWaypointActivity.java
index d49308e..ce279bf 100644
--- a/main/src/cgeo/geocaching/EditWaypointActivity.java
+++ b/main/src/cgeo/geocaching/EditWaypointActivity.java
@@ -7,11 +7,12 @@ import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.WaypointType;
-import cgeo.geocaching.geopoint.DistanceParser;
-import cgeo.geocaching.geopoint.Geopoint;
-import cgeo.geocaching.geopoint.GeopointFormatter;
+import cgeo.geocaching.location.DistanceParser;
+import cgeo.geocaching.location.Geopoint;
+import cgeo.geocaching.location.GeopointFormatter;
+import cgeo.geocaching.sensors.GeoData;
import cgeo.geocaching.sensors.GeoDirHandler;
-import cgeo.geocaching.sensors.IGeoData;
+import cgeo.geocaching.sensors.Sensors;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.dialog.CoordinatesInputDialog;
import cgeo.geocaching.ui.dialog.Dialogs;
@@ -66,11 +67,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
@ViewById(R.id.modify_cache_coordinates_local) protected RadioButton modifyLocal;
@Extra(Intents.EXTRA_GEOCODE) protected String geocode = null;
- @Extra(Intents.EXTRA_WAYPOINT_ID) protected int id = -1;
- /**
- * number of waypoints that the corresponding cache has until now
- */
- @Extra(Intents.EXTRA_COUNT) protected int wpCount = 0;
+ @Extra(Intents.EXTRA_WAYPOINT_ID) protected int waypointId = -1;
@InstanceState protected int waypointTypeSelectorPosition = -1;
@@ -89,14 +86,14 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
*/
private Geocache cache;
- private Handler loadWaypointHandler = new Handler() {
+ private final Handler loadWaypointHandler = new Handler() {
@Override
- public void handleMessage(Message msg) {
+ public void handleMessage(final Message msg) {
try {
if (waypoint == null) {
- Log.d("No waypoint loaded to edit. id= " + id);
- id = -1;
+ Log.d("No waypoint loaded to edit. id= " + waypointId);
+ waypointId = -1;
} else {
geocode = waypoint.getGeocode();
prefix = waypoint.getPrefix();
@@ -119,14 +116,23 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
}
Dialogs.moveCursorToEnd(note);
}
- final Geocache cache = DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_ONLY);
- setCoordsModificationVisibility(ConnectorFactory.getConnector(geocode), cache);
+ new AsyncTask<Void, Void, Geocache>() {
+ @Override
+ protected Geocache doInBackground(final Void... params) {
+ return DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_ONLY);
+ }
+
+ @Override
+ protected void onPostExecute(final Geocache cache) {
+ setCoordsModificationVisibility(ConnectorFactory.getConnector(geocode), cache);
+ }
+ }.execute();
}
if (own) {
initializeWaypointTypeSelector();
}
- } catch (RuntimeException e) {
+ } catch (final RuntimeException e) {
Log.e("EditWaypointActivity.loadWaypointHandler", e);
} finally {
if (waitDialog != null) {
@@ -138,17 +144,17 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
};
@Override
- public void onCreate(Bundle savedInstanceState) {
+ public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.editwaypoint_activity);
- if (StringUtils.isBlank(geocode) && id <= 0) {
+ if (StringUtils.isBlank(geocode) && waypointId <= 0) {
showToast(res.getString(R.string.err_waypoint_cache_unknown));
finish();
return;
}
- if (id <= 0) {
+ if (waypointId <= 0) {
setTitle(res.getString(R.string.waypoint_add_title));
} else {
setTitle(res.getString(R.string.waypoint_edit_title));
@@ -159,11 +165,11 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
addWaypoint.setOnClickListener(new SaveWaypointListener());
- List<String> wayPointNames = new ArrayList<>();
- for (WaypointType wpt : WaypointType.ALL_TYPES_EXCEPT_OWN_AND_ORIGINAL) {
+ final List<String> wayPointNames = new ArrayList<>();
+ for (final WaypointType wpt : WaypointType.ALL_TYPES_EXCEPT_OWN_AND_ORIGINAL) {
wayPointNames.add(wpt.getL10n());
}
- ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, wayPointNames);
+ final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, wayPointNames);
waypointName.setAdapter(adapter);
if (savedInstanceState != null) {
@@ -174,7 +180,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
cache = DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
setCoordsModificationVisibility(ConnectorFactory.getConnector(geocode), cache);
}
- if (id > 0) { // existing waypoint
+ if (waypointId > 0) { // existing waypoint
waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
waitDialog.setCancelable(true);
@@ -189,7 +195,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
disableSuggestions(distanceView);
}
- private void setCoordsModificationVisibility(IConnector con, Geocache cache) {
+ private void setCoordsModificationVisibility(final IConnector con, final Geocache cache) {
if (cache != null && (cache.getType() == CacheType.MYSTERY || cache.getType() == CacheType.MULTI)) {
coordinatesGroup.setVisibility(View.VISIBLE);
modifyBoth.setVisibility(con.supportsOwnCoordinates() ? View.VISIBLE : View.GONE);
@@ -205,18 +211,23 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
}
private void initializeWaypointTypeSelector() {
- ArrayAdapter<WaypointType> wpAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, POSSIBLE_WAYPOINT_TYPES.toArray(new WaypointType[POSSIBLE_WAYPOINT_TYPES.size()]));
+ final ArrayAdapter<WaypointType> wpAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, POSSIBLE_WAYPOINT_TYPES.toArray(new WaypointType[POSSIBLE_WAYPOINT_TYPES.size()]));
wpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
waypointTypeSelector.setAdapter(wpAdapter);
waypointTypeSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
- public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
+ public void onItemSelected(final AdapterView<?> parent, final View v, final int pos, final long id) {
+ final String oldDefaultName = waypointTypeSelectorPosition >= 0 ? getDefaultWaypointName(POSSIBLE_WAYPOINT_TYPES.get(waypointTypeSelectorPosition)) : StringUtils.EMPTY;
waypointTypeSelectorPosition = pos;
+ final String currentName = waypointName.getText().toString().trim();
+ if (StringUtils.isBlank(currentName) || oldDefaultName.equals(currentName)) {
+ waypointName.setText(getDefaultWaypointName(getSelectedWaypointType()));
+ }
}
@Override
- public void onNothingSelected(AdapterView<?> parent) {
+ public void onNothingSelected(final AdapterView<?> parent) {
}
});
@@ -249,17 +260,13 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
private void initializeDistanceUnitSelector() {
distanceUnits = new ArrayList<>(Arrays.asList(res.getStringArray(R.array.distance_units)));
if (initViews) {
- distanceUnitSelector.setSelection(Settings.isUseImperialUnits() ? 2 : 0); //0:m, 2:ft
+ distanceUnitSelector.setSelection(Settings.useImperialUnits() ? 2 : 0); //0:m, 2:ft
}
}
final private GeoDirHandler geoDirHandler = new GeoDirHandler() {
@Override
- public void updateGeoData(final IGeoData geo) {
- if (geo.getCoords() == null) {
- return;
- }
-
+ public void updateGeoData(final GeoData geo) {
try {
buttonLat.setHint(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE_RAW));
buttonLon.setHint(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE_RAW));
@@ -274,10 +281,10 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
@Override
public void run() {
try {
- waypoint = DataStore.loadWaypoint(id);
+ waypoint = DataStore.loadWaypoint(waypointId);
loadWaypointHandler.sendMessage(Message.obtain());
- } catch (Exception e) {
+ } catch (final Exception e) {
Log.e("EditWaypointActivity.loadWaypoint.run", e);
}
}
@@ -286,28 +293,71 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
private class CoordDialogListener implements View.OnClickListener {
@Override
- public void onClick(View arg0) {
+ public void onClick(final View arg0) {
Geopoint gp = null;
try {
gp = new Geopoint(buttonLat.getText().toString(), buttonLon.getText().toString());
- } catch (Geopoint.ParseException e) {
+ } catch (final Geopoint.ParseException ignored) {
// button text is blank when creating new waypoint
}
- Geocache cache = DataStore.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
- CoordinatesInputDialog coordsDialog = CoordinatesInputDialog.getInstance(cache, gp, app.currentGeo());
- coordsDialog.setCancelable(true);
- coordsDialog.show(getSupportFragmentManager(),"wpeditdialog");
+ final Geopoint geopoint = gp;
+ new AsyncTask<Void, Void, Geocache>() {
+ @Override
+ protected Geocache doInBackground(final Void... params) {
+ return DataStore.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
+ }
+
+ @Override
+ protected void onPostExecute(final Geocache cache) {
+ final CoordinatesInputDialog coordsDialog = CoordinatesInputDialog.getInstance(cache, geopoint, Sensors.getInstance().currentGeo());
+ coordsDialog.setCancelable(true);
+ coordsDialog.show(getSupportFragmentManager(), "wpeditdialog");
+ }
+ }.execute();
}
}
@Override
- public void updateCoordinates(Geopoint gp) {
+ public void updateCoordinates(final Geopoint gp) {
buttonLat.setText(gp.format(GeopointFormatter.Format.LAT_DECMINUTE));
buttonLon.setText(gp.format(GeopointFormatter.Format.LON_DECMINUTE));
}
+ /**
+ * Suffix the waypoint type with a running number to get a default name.
+ *
+ * @param type
+ * type to create a new default name for
+ *
+ */
+ private String getDefaultWaypointName(final WaypointType type) {
+ final ArrayList<String> wpNames = new ArrayList<>();
+ for (final Waypoint waypoint : cache.getWaypoints()) {
+ wpNames.add(waypoint.getName());
+ }
+ // try final and trailhead without index
+ if (type == WaypointType.FINAL || type == WaypointType.TRAILHEAD) {
+ if (!wpNames.contains(type.getL10n())) {
+ return type.getL10n();
+ }
+ }
+ // for other types add an index by default, which is highest found index + 1
+ int max = 0;
+ for (int i = 0; i < 30; i++) {
+ if (wpNames.contains(type.getL10n() + " " + i)) {
+ max = i;
+ }
+ }
+ return type.getL10n() + " " + (max + 1);
+ }
+
+ private WaypointType getSelectedWaypointType() {
+ final int selectedTypeIndex = waypointTypeSelector.getSelectedItemPosition();
+ return selectedTypeIndex >= 0 ? POSSIBLE_WAYPOINT_TYPES.get(selectedTypeIndex) : waypoint.getWaypointType();
+ }
+
public static final int SUCCESS = 0;
public static final int UPLOAD_START = 1;
public static final int UPLOAD_ERROR = 2;
@@ -318,7 +368,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
private class SaveWaypointListener implements View.OnClickListener {
@Override
- public void onClick(View arg0) {
+ public void onClick(final View arg0) {
final String bearingText = bearing.getText().toString();
// combine distance from EditText and distanceUnit saved from Spinner
final String distanceText = distanceView.getText().toString() + distanceUnits.get(distanceUnitSelector.getSelectedItemPosition());
@@ -336,17 +386,12 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
if (StringUtils.isNotBlank(latText) && StringUtils.isNotBlank(lonText)) {
try {
coords = new Geopoint(latText, lonText);
- } catch (Geopoint.ParseException e) {
+ } catch (final Geopoint.ParseException e) {
showToast(res.getString(e.resource));
return;
}
} else {
- final IGeoData geo = app.currentGeo();
- if (geo.getCoords() == null) {
- showToast(res.getString(R.string.err_point_curr_position_unavailable));
- return;
- }
- coords = geo.getCoords();
+ coords = Sensors.getInstance().currentGeo().getCoords();
}
if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
@@ -354,7 +399,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
double bearing;
try {
bearing = Double.parseDouble(bearingText);
- } catch (NumberFormatException e) {
+ } catch (final NumberFormatException ignored) {
Dialogs.message(EditWaypointActivity.this, R.string.err_point_bear_and_dist_title, R.string.err_point_bear_and_dist);
return;
}
@@ -362,8 +407,8 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
double distance;
try {
distance = DistanceParser.parseDistance(distanceText,
- !Settings.isUseImperialUnits());
- } catch (NumberFormatException e) {
+ !Settings.useImperialUnits());
+ } catch (final NumberFormatException ignored) {
showToast(res.getString(R.string.err_parse_dist));
return;
}
@@ -371,25 +416,22 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
coords = coords.project(bearing, distance);
}
- // if no name is given, just give the waypoint its number as name
final String givenName = waypointName.getText().toString().trim();
- final String name = StringUtils.isNotEmpty(givenName) ? givenName : res.getString(R.string.waypoint) + " " + (wpCount + 1);
+ final String name = StringUtils.defaultIfBlank(givenName, getDefaultWaypointName(getSelectedWaypointType()));
final String noteText = note.getText().toString().trim();
final Geopoint coordsToSave = coords;
- final int selectedTypeIndex = waypointTypeSelector.getSelectedItemPosition();
- final WaypointType type = selectedTypeIndex >= 0 ? POSSIBLE_WAYPOINT_TYPES.get(selectedTypeIndex) : waypoint.getWaypointType();
+ final WaypointType type = getSelectedWaypointType();
final boolean visited = visitedCheckBox.isChecked();
final ProgressDialog progress = ProgressDialog.show(EditWaypointActivity.this, getString(R.string.waypoint), getString(R.string.waypoint_being_saved), true);
final Handler finishHandler = new Handler() {
@Override
- public void handleMessage(Message msg) {
- // TODO: The order of showToast, progress.dismiss and finish is different in these cases. Why?
+ public void handleMessage(final Message msg) {
switch (msg.what) {
case UPLOAD_SUCCESS:
- showToast(getString(R.string.waypoint_coordinates_has_been_modified_on_website, coordsToSave));
progress.dismiss();
finish();
+ showToast(getString(R.string.waypoint_coordinates_has_been_modified_on_website, coordsToSave));
break;
case SUCCESS:
progress.dismiss();
@@ -422,7 +464,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
class SaveWptTask extends AsyncTask<Void, Void, Void> {
@Override
- protected Void doInBackground(Void... params) {
+ protected Void doInBackground(final Void... params) {
final Waypoint waypoint = new Waypoint(name, type, own);
waypoint.setGeocode(geocode);
waypoint.setPrefix(prefix);
@@ -430,14 +472,14 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
waypoint.setCoords(coordsToSave);
waypoint.setNote(noteText);
waypoint.setVisited(visited);
- waypoint.setId(id);
+ waypoint.setId(waypointId);
- Geocache cache = DataStore.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
+ final Geocache cache = DataStore.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
if (cache == null) {
finishHandler.sendEmptyMessage(SAVE_ERROR);
return null;
}
- Waypoint oldWaypoint = cache.getWaypointById(id);
+ final Waypoint oldWaypoint = cache.getWaypointById(waypointId);
if (cache.addOrChangeWaypoint(waypoint, true)) {
DataStore.saveCache(cache, EnumSet.of(SaveFlag.DB));
if (!StaticMapsProvider.hasAllStaticMapsForWaypoint(geocode, waypoint)) {
@@ -460,7 +502,7 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
finishHandler.sendEmptyMessage(UPLOAD_START);
if (cache.supportsOwnCoordinates()) {
- boolean result = uploadModifiedCoords(cache, waypoint.getCoords());
+ final boolean result = uploadModifiedCoords(cache, waypoint.getCoords());
finishHandler.sendEmptyMessage(result ? SUCCESS : UPLOAD_ERROR);
} else {
showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
@@ -485,10 +527,10 @@ public class EditWaypointActivity extends AbstractActionBarActivity implements C
}
public static void startActivityEditWaypoint(final Context context, final Geocache cache, final int waypointId) {
- EditWaypointActivity_.intent(context).geocode(cache.getGeocode()).id(waypointId).start();
+ EditWaypointActivity_.intent(context).geocode(cache.getGeocode()).waypointId(waypointId).start();
}
public static void startActivityAddWaypoint(final Context context, final Geocache cache) {
- EditWaypointActivity_.intent(context).geocode(cache.getGeocode()).wpCount(cache.getWaypoints().size()).start();
+ EditWaypointActivity_.intent(context).geocode(cache.getGeocode()).start();
}
}