diff options
Diffstat (limited to 'main/src/cgeo/geocaching/NavigateAnyPointActivity.java')
-rw-r--r-- | main/src/cgeo/geocaching/NavigateAnyPointActivity.java | 121 |
1 files changed, 59 insertions, 62 deletions
diff --git a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java index 2591987..b0d413d 100644 --- a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java +++ b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java @@ -7,11 +7,12 @@ import butterknife.Optional; import cgeo.geocaching.activity.AbstractActionBarActivity; import cgeo.geocaching.activity.INavigationSource; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; -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.AbstractViewHolder; import cgeo.geocaching.ui.NavigationActionProvider; @@ -19,8 +20,13 @@ import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.Formatter; import cgeo.geocaching.utils.Log; +import cgeo.geocaching.utils.RxUtils; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.Nullable; + +import rx.functions.Action0; +import rx.schedulers.Schedulers; import android.app.Activity; import android.content.Context; @@ -94,7 +100,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen public View getView(final int position, final View convertView, final ViewGroup parent) { View rowView = convertView; - ViewHolder viewHolder; + final ViewHolder viewHolder; if (rowView == null) { rowView = getInflater().inflate(R.layout.simple_way_point, parent, false); viewHolder = new ViewHolder(rowView); @@ -261,7 +267,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen private void initializeDistanceUnitSelector() { if (StringUtils.isBlank(distanceUnit)) { - if (Settings.isUseImperialUnits()) { + if (Settings.useImperialUnits()) { distanceUnitSelector.setSelection(2); // ft distanceUnit = res.getStringArray(R.array.distance_units)[2]; } else { @@ -281,7 +287,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen if (latButton.getText().length() > 0 && lonButton.getText().length() > 0) { gp = new Geopoint(latButton.getText().toString() + " " + lonButton.getText().toString()); } - final CoordinatesInputDialog coordsDialog = CoordinatesInputDialog.getInstance(null, gp, app.currentGeo()); + final CoordinatesInputDialog coordsDialog = CoordinatesInputDialog.getInstance(null, gp, Sensors.getInstance().currentGeo()); coordsDialog.setCancelable(true); coordsDialog.show(getSupportFragmentManager(),"wpedit_dialog"); } @@ -336,7 +342,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen menu.findItem(R.id.menu_caches_around).setVisible(visible); menu.findItem(R.id.menu_clear_history).setVisible(!getHistoryOfSearchedLocations().isEmpty()); - } catch (final RuntimeException e) { + } catch (final RuntimeException ignored) { // nothing } @@ -346,20 +352,14 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen @Override public boolean onOptionsItemSelected(final MenuItem item) { final int menuItem = item.getItemId(); - - final Geopoint coords = getDestination(); - - if (coords != null) { - addToHistory(coords); - } - + final Geopoint coords = getDestinationAndAddToHistory(); switch (menuItem) { case R.id.menu_default_navigation: - navigateTo(); + navigateTo(coords); return true; case R.id.menu_caches_around: - cachesAround(); + cachesAround(coords); return true; case R.id.menu_clear_history: @@ -373,22 +373,33 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen return super.onOptionsItemSelected(item); } - private void addToHistory(final Geopoint coords) { + private Geopoint getDestinationAndAddToHistory() { + final Geopoint coords = getDestination(); + addToHistory(coords); + return coords; + } + + private void addToHistory(@Nullable final Geopoint coords) { + if (coords == null) { + return; + } + // Add locations to history final Destination loc = new Destination(coords); if (!getHistoryOfSearchedLocations().contains(loc)) { getHistoryOfSearchedLocations().add(0, loc); - - // Save location - DataStore.saveSearchedDestination(loc); - - // Ensure to remove the footer - historyListView.removeFooterView(getEmptyHistoryFooter()); - - runOnUiThread(new Runnable() { + RxUtils.andThenOnUi(Schedulers.io(), new Action0() { @Override - public void run() { + public void call() { + // Save location + DataStore.saveSearchedDestination(loc); + } + }, new Action0() { + @Override + public void call() { + // Ensure to remove the footer + historyListView.removeFooterView(getEmptyHistoryFooter()); destinationHistoryAdapter.notifyDataSetChanged(); } }); @@ -431,35 +442,34 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen } } - private void navigateTo() { - navigateTo(getDestination()); - } + private void navigateTo(final Geopoint coords) { + if (coords == null) { + showToast(res.getString(R.string.err_location_unknown)); + return; + } - private void navigateTo(final Geopoint geopoint) { - NavigationAppFactory.startDefaultNavigationApplication(1, this, geopoint); + NavigationAppFactory.startDefaultNavigationApplication(1, this, coords); } - private void cachesAround() { - final Geopoint coords = getDestination(); - + private void cachesAround(final Geopoint coords) { if (coords == null) { showToast(res.getString(R.string.err_location_unknown)); return; } - CacheListActivity.startActivityCoordinates(this, coords); + CacheListActivity.startActivityCoordinates(this, coords, null); finish(); } private final GeoDirHandler geoDirHandler = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoData(final GeoData geo) { try { latButton.setHint(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE_RAW)); lonButton.setHint(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE_RAW)); } catch (final RuntimeException e) { - Log.w("Failed to update location."); + Log.w("Failed to update location", e); } } }; @@ -468,15 +478,9 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen @Override public void onClick(final View arg0) { - final Geopoint coords = app.currentGeo().getCoords(); - if (coords == null) { - showToast(res.getString(R.string.err_point_unknown_position)); - return; - } - + final Geopoint coords = Sensors.getInstance().currentGeo().getCoords(); latButton.setText(coords.format(GeopointFormatter.Format.LAT_DECMINUTE)); lonButton.setText(coords.format(GeopointFormatter.Format.LON_DECMINUTE)); - changed = false; } } @@ -504,30 +508,25 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen return null; } } else { - if (app.currentGeo().getCoords() == null) { - showToast(res.getString(R.string.err_point_curr_position_unavailable)); - return null; - } - - coords = app.currentGeo().getCoords(); + coords = Sensors.getInstance().currentGeo().getCoords(); } // apply projection - if (coords != null && StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) { + if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) { // bearing & distance - double bearing; + final double bearing; try { bearing = Double.parseDouble(bearingText); - } catch (final NumberFormatException e) { + } catch (final NumberFormatException ignored) { Dialogs.message(this, R.string.err_point_bear_and_dist_title, R.string.err_point_bear_and_dist); return null; } - double distance; + final double distance; try { distance = DistanceParser.parseDistance(distanceText, - !Settings.isUseImperialUnits()); - } catch (final NumberFormatException e) { + !Settings.useImperialUnits()); + } catch (final NumberFormatException ignored) { showToast(res.getString(R.string.err_parse_dist)); return null; } @@ -535,9 +534,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen coords = coords.project(bearing, distance); } - if (coords != null) { - saveCoords(coords); - } + saveCoords(coords); return coords; } @@ -551,11 +548,11 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen @Override public void startDefaultNavigation() { - navigateTo(); + navigateTo(getDestinationAndAddToHistory()); } @Override public void startDefaultNavigation2() { - NavigationAppFactory.startDefaultNavigationApplication(2, this, getDestination()); + NavigationAppFactory.startDefaultNavigationApplication(2, this, getDestinationAndAddToHistory()); } } |