diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2015-03-08 10:37:16 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2015-03-08 10:37:16 +0100 |
commit | 1b42d3c5ebf38a7710cfe4040a90d98b54575cca (patch) | |
tree | cf4fcfd1b91c1aec10fa4b80ee66d83677ed5807 /main/src | |
parent | 90232fd33aa4fabc49f11dce7a983a949aaf61db (diff) | |
download | cgeo-1b42d3c5ebf38a7710cfe4040a90d98b54575cca.zip cgeo-1b42d3c5ebf38a7710cfe4040a90d98b54575cca.tar.gz cgeo-1b42d3c5ebf38a7710cfe4040a90d98b54575cca.tar.bz2 |
fix #4717, fix #4723: more descriptive map title
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 132 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/utils/Formatter.java | 13 |
2 files changed, 102 insertions, 43 deletions
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 7cd13a7..6b1c23c 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -40,6 +40,7 @@ import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.LiveMapInfoDialogBuilder; import cgeo.geocaching.utils.AngleUtils; import cgeo.geocaching.utils.CancellableHandler; +import cgeo.geocaching.utils.Formatter; import cgeo.geocaching.utils.LeastRecentlyUsedSet; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.MapUtils; @@ -47,6 +48,7 @@ import cgeo.geocaching.utils.MapUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import rx.Subscription; import rx.functions.Action0; @@ -218,34 +220,8 @@ public class CGeoMap extends AbstractMap implements ViewFactory { switch (what) { case UPDATE_TITLE: - // set title - if (map.mapMode == MapMode.LIVE && map.isLiveEnabled) { - map.setTitle(map.res.getString(R.string.map_live)); - } else { - map.setTitle(map.mapTitle); - } - - // count caches in the sub title - map.countVisibleCaches(); - final StringBuilder subtitle = new StringBuilder(); - if (!map.caches.isEmpty()) { - final int totalCount = map.caches.size(); - - if (map.cachesCnt != totalCount && Settings.isDebug()) { - subtitle.append(map.cachesCnt).append('/').append(map.res.getQuantityString(R.plurals.cache_counts, totalCount, totalCount)); - } - else { - subtitle.append(map.res.getQuantityString(R.plurals.cache_counts, map.cachesCnt, map.cachesCnt)); - } - } - - if (Settings.isDebug() && map.lastSearchResult != null && StringUtils.isNotBlank(map.lastSearchResult.getUrl())) { - subtitle.append(" [").append(map.lastSearchResult.getUrl()).append(']'); - } - - if (subtitle.length() > 0) { - map.setSubtitle(subtitle.toString()); - } + map.setTitle(); + map.setSubtitle(); break; case INVALIDATE_MAP: @@ -261,7 +237,8 @@ public class CGeoMap extends AbstractMap implements ViewFactory { final private Handler displayHandler = new DisplayHandler(this); - private void setTitle(final String title) { + private void setTitle() { + final String title = calculateTitle(); /* Compatibility for the old Action Bar, only used by the maps activity at the moment */ final TextView titleview = ButterKnife.findById(activity, R.id.actionbar_title); if (titleview != null) { @@ -273,7 +250,35 @@ public class CGeoMap extends AbstractMap implements ViewFactory { } } - private void setSubtitle(final String subtitle) { + private String calculateTitle() { + if (isLiveEnabled) { + return res.getString(R.string.map_live); + } + if (mapMode == MapMode.SINGLE) { + final Geocache cache = getSingleModeCache(); + if (cache != null) { + return cache.getName(); + } + } + return StringUtils.defaultIfEmpty(mapTitle, res.getString(R.string.map_map)); + } + + @Nullable + private Geocache getSingleModeCache() { + for (final Geocache geocache : caches) { + if (geocache.getGeocode().equals(geocodeIntent)) { + return geocache; + } + } + return null; + } + + private void setSubtitle() { + final String subtitle = calculateSubtitle(); + if (StringUtils.isEmpty(subtitle)) { + return; + } + /* Compatibility for the old Action Bar, only used by the maps activity at the moment */ final TextView titleView = ButterKnife.findById(activity, R.id.actionbar_title); if (titleView != null) { @@ -284,6 +289,34 @@ public class CGeoMap extends AbstractMap implements ViewFactory { } } + private String calculateSubtitle() { + // count caches in the sub title + countVisibleCaches(); + final StringBuilder subtitle = new StringBuilder(); + if (!isLiveEnabled && mapMode == MapMode.SINGLE) { + final Geocache cache = getSingleModeCache(); + if (cache != null) { + return Formatter.formatMapSubtitle(cache); + } + } + if (!caches.isEmpty()) { + final int totalCount = caches.size(); + + if (cachesCnt != totalCount && Settings.isDebug()) { + subtitle.append(cachesCnt).append('/').append(res.getQuantityString(R.plurals.cache_counts, totalCount, totalCount)); + } + else { + subtitle.append(res.getQuantityString(R.plurals.cache_counts, cachesCnt, cachesCnt)); + } + } + + if (Settings.isDebug() && lastSearchResult != null && StringUtils.isNotBlank(lastSearchResult.getUrl())) { + subtitle.append(" [").append(lastSearchResult.getUrl()).append(']'); + } + + return subtitle.toString(); + } + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void setTitleIceCreamSandwich(final String title) { activity.getActionBar().setTitle(title); @@ -459,7 +492,7 @@ public class CGeoMap extends AbstractMap implements ViewFactory { activity.getActionBar().setDisplayHomeAsUpEnabled(true); } activity.setContentView(mapProvider.getMapLayoutId()); - setTitle(res.getString(R.string.map_map)); + setTitle(); // initialize map mapView = (MapViewImpl) activity.findViewById(mapProvider.getMapViewId()); @@ -737,6 +770,10 @@ public class CGeoMap extends AbstractMap implements ViewFactory { lastSearchResult = null; searchIntent = null; ActivityMixin.invalidateOptionsMenu(activity); + if (mapMode != MapMode.SINGLE) { + mapTitle = StringUtils.EMPTY; + } + updateMapTitle(); return true; case R.id.menu_store_caches: if (!isLoading()) { @@ -813,14 +850,10 @@ public class CGeoMap extends AbstractMap implements ViewFactory { return true; } case R.id.menu_hint: - if (caches.size() == 1) { - caches.iterator().next().showHintToast(getActivity()); - } + menuShowHint(); return true; case R.id.menu_compass: - if (caches.size() == 1) { - CompassActivity.startActivityCache(this.getActivity(), caches.iterator().next()); - } + menuCompass(); return true; default: final MapSource mapSource = MapProviderFactory.getMapSource(id); @@ -833,6 +866,20 @@ public class CGeoMap extends AbstractMap implements ViewFactory { return false; } + private void menuCompass() { + final Geocache cache = getSingleModeCache(); + if (cache != null) { + CompassActivity.startActivityCache(this.getActivity(), cache); + } + } + + private void menuShowHint() { + final Geocache cache = getSingleModeCache(); + if (cache != null) { + cache.showHintToast(getActivity()); + } + } + private void selectMapTheme() { final File[] themeFiles = Settings.getMapThemeFiles(); @@ -1132,7 +1179,7 @@ public class CGeoMap extends AbstractMap implements ViewFactory { // update title on any change if (moved || !viewportNow.equals(previousViewport)) { - map.displayHandler.sendEmptyMessage(UPDATE_TITLE); + map.updateMapTitle(); } previousZoom = zoomNow; @@ -1358,7 +1405,7 @@ public class CGeoMap extends AbstractMap implements ViewFactory { } displayHandler.sendEmptyMessage(INVALIDATE_MAP); - displayHandler.sendEmptyMessage(UPDATE_TITLE); + updateMapTitle(); } finally { showProgressHandler.sendEmptyMessage(HIDE_PROGRESS); } @@ -1371,11 +1418,15 @@ public class CGeoMap extends AbstractMap implements ViewFactory { final CachesOverlayItemImpl item = getWaypointItem(waypoint); overlayCaches.updateItems(item); displayHandler.sendEmptyMessage(INVALIDATE_MAP); - displayHandler.sendEmptyMessage(UPDATE_TITLE); + updateMapTitle(); cachesCnt = 1; } + private void updateMapTitle() { + displayHandler.sendEmptyMessage(UPDATE_TITLE); + } + private static abstract class DoRunnable implements Runnable { private final WeakReference<CGeoMap> mapRef; @@ -1688,7 +1739,6 @@ public class CGeoMap extends AbstractMap implements ViewFactory { mapIntent.putExtra(Intents.EXTRA_MAP_MODE, MapMode.SINGLE); mapIntent.putExtra(Intents.EXTRA_LIVE_ENABLED, false); mapIntent.putExtra(Intents.EXTRA_GEOCODE, geocode); - mapIntent.putExtra(Intents.EXTRA_TITLE, geocode); fromActivity.startActivity(mapIntent); } diff --git a/main/src/cgeo/geocaching/utils/Formatter.java b/main/src/cgeo/geocaching/utils/Formatter.java index db649d8..2127d59 100644 --- a/main/src/cgeo/geocaching/utils/Formatter.java +++ b/main/src/cgeo/geocaching/utils/Formatter.java @@ -17,6 +17,7 @@ import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Locale; public abstract class Formatter { @@ -149,10 +150,10 @@ public abstract class Formatter { private static void addShortInfos(final Geocache cache, final ArrayList<String> infos) { if (cache.hasDifficulty()) { - infos.add("D " + String.format("%.1f", cache.getDifficulty())); + infos.add("D " + formatDT(cache.getDifficulty())); } if (cache.hasTerrain()) { - infos.add("T " + String.format("%.1f", cache.getTerrain())); + infos.add("T " + formatDT(cache.getTerrain())); } // don't show "not chosen" for events and virtuals, that should be the normal case @@ -166,6 +167,10 @@ public abstract class Formatter { } } + private static String formatDT(final float value) { + return String.format(Locale.getDefault(), "%.1f", value); + } + public static String formatCacheInfoHistory(final Geocache cache) { final ArrayList<String> infos = new ArrayList<>(3); infos.add(StringUtils.upperCase(cache.getGeocode())); @@ -226,4 +231,8 @@ public abstract class Formatter { return dateString; } + public static String formatMapSubtitle(final Geocache cache) { + return "D " + formatDT(cache.getDifficulty()) + SEPARATOR + "T " + formatDT(cache.getTerrain()) + SEPARATOR + cache.getGeocode(); + } + } |