diff options
Diffstat (limited to 'main/src')
29 files changed, 580 insertions, 240 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 98e7112..aa36223 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -15,6 +15,7 @@ import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.ui.DecryptTextClickListener; import cgeo.geocaching.utils.BaseUtils; import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.ClipboardUtils; @@ -1943,17 +1944,16 @@ public class CacheDetailActivity extends AbstractActivity { if (StringUtils.isNotBlank(cache.getHint())) { TextView hintView = ((TextView) view.findViewById(R.id.hint)); - hintView.setText(CryptUtils.rot13(cache.getHint().trim())); + if (BaseUtils.containsHtml(cache.getHint())) { + hintView.setText(Html.fromHtml(cache.getHint(), new HtmlImage(CacheDetailActivity.this, null, false, cache.getListId(), false), null), TextView.BufferType.SPANNABLE); + hintView.setText(CryptUtils.rot13((Spannable) hintView.getText())); + } + else { + hintView.setText(CryptUtils.rot13(cache.getHint().trim())); + } hintView.setVisibility(View.VISIBLE); hintView.setClickable(true); - hintView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - // code hint - TextView hintView = (TextView) view; - hintView.setText(CryptUtils.rot13(hintView.getText().toString())); - } - }); + hintView.setOnClickListener(new DecryptTextClickListener()); registerForContextMenu(hintView); } else { TextView hintView = ((TextView) view.findViewById(R.id.hint)); @@ -2268,7 +2268,7 @@ public class CacheDetailActivity extends AbstractActivity { ((TextView) rowView.findViewById(R.id.author)).setOnClickListener(new UserActionsClickListener()); TextView logView = (TextView) logLayout.findViewById(R.id.log); logView.setMovementMethod(LinkMovementMethod.getInstance()); - logView.setOnClickListener(new DecryptLogClickListener()); + logView.setOnClickListener(new DecryptTextClickListener()); registerForContextMenu(logView); loglist.add(rowView); @@ -2298,30 +2298,6 @@ public class CacheDetailActivity extends AbstractActivity { loglistView.setVisibility(View.VISIBLE); } } - - private class DecryptLogClickListener implements View.OnClickListener { - - public void onClick(View view) { - if (view == null) { - return; - } - - try { - final TextView logView = (TextView) view; - CharSequence text = logView.getText(); - if (text instanceof Spannable) { - Spannable span = (Spannable) text; - logView.setText(CryptUtils.rot13(span)); - } - else { - String string = (String) text; - logView.setText(CryptUtils.rot13(string)); - } - } catch (Exception e) { - // nothing - } - } - } } private class WaypointsViewCreator implements PageViewCreator { diff --git a/main/src/cgeo/geocaching/StaticMapsActivity.java b/main/src/cgeo/geocaching/StaticMapsActivity.java index 8f6bf23..3646c37 100644 --- a/main/src/cgeo/geocaching/StaticMapsActivity.java +++ b/main/src/cgeo/geocaching/StaticMapsActivity.java @@ -15,6 +15,8 @@ import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; import android.widget.ImageView; import android.widget.LinearLayout; @@ -23,6 +25,7 @@ import java.util.List; public class StaticMapsActivity extends AbstractActivity { + private static final int MENU_REFRESH = 1; private final List<Bitmap> maps = new ArrayList<Bitmap>(); private Integer waypoint_id = null; private String geocode = null; @@ -85,25 +88,13 @@ public class StaticMapsActivity extends AbstractActivity { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); switch (which) { - case DialogInterface.BUTTON_POSITIVE: - cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); - if (waypoint_id == null) { - StaticMapsProvider.storeCacheStaticMap(cache, StaticMapsActivity.this); - } else { - cgWaypoint waypoint = cache.getWaypointById(waypoint_id); - if (waypoint != null) { - StaticMapsProvider.storeWaypointStaticMap(cache, StaticMapsActivity.this, waypoint); - } else { - showToast(res.getString(R.string.err_detail_not_load_map_static)); - break; - } - } - showToast(res.getString(R.string.info_storing_static_maps)); - break; + case DialogInterface.BUTTON_POSITIVE: + downloadStaticMaps(); + break; - case DialogInterface.BUTTON_NEGATIVE: + case DialogInterface.BUTTON_NEGATIVE: showToast(res.getString(R.string.err_detail_not_load_map_static)); - break; + break; } finish(); } @@ -199,4 +190,36 @@ public class StaticMapsActivity extends AbstractActivity { } } } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + menu.add(0, MENU_REFRESH, 0, res.getString(R.string.cache_offline_refresh)); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == MENU_REFRESH) { + downloadStaticMaps(); + restartActivity(); + return true; + } + return super.onOptionsItemSelected(item); + } + + private void downloadStaticMaps() { + final cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); + if (waypoint_id == null) { + showToast(res.getString(R.string.info_storing_static_maps)); + StaticMapsProvider.storeCacheStaticMap(cache, this, true); + } else { + final cgWaypoint waypoint = cache.getWaypointById(waypoint_id); + if (waypoint != null) { + showToast(res.getString(R.string.info_storing_static_maps)); + StaticMapsProvider.storeWaypointStaticMap(cache, this, waypoint, true); + } else { + showToast(res.getString(R.string.err_detail_not_load_map_static)); + } + } + } }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index 6384d59..cd23489 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -26,7 +26,7 @@ public class StaticMapsProvider { return LocalStorage.getStorageFile(geocode, "map_" + prefix + level, false, createDirs); } - private static void downloadMapsInThread(final cgCache cache, String markerUrl, String prefix, String latlonMap, int edge, String waypoints) { + private static void downloadDifferentZooms(final cgCache cache, String markerUrl, String prefix, String latlonMap, int edge, String waypoints) { downloadMap(cache, 20, "satellite", markerUrl, prefix, 1, latlonMap, edge, waypoints); downloadMap(cache, 18, "satellite", markerUrl, prefix, 2, latlonMap, edge, waypoints); downloadMap(cache, 16, "roadmap", markerUrl, prefix, 3, latlonMap, edge, waypoints); @@ -59,38 +59,38 @@ public class StaticMapsProvider { int edge = guessMinDisplaySide(activity); if (Settings.isStoreOfflineMaps() && cache.getCoords() != null) { - storeCacheStaticMap(cache, edge); + storeCacheStaticMap(cache, edge, false); } // download static map for current waypoints if (Settings.isStoreOfflineWpMaps() && CollectionUtils.isNotEmpty(cache.getWaypoints())) { for (cgWaypoint waypoint : cache.getWaypoints()) { - storeWaypointStaticMap(cache, edge, waypoint); + storeWaypointStaticMap(cache, edge, waypoint, false); } } } - public static void storeWaypointStaticMap(cgCache cache, Activity activity, cgWaypoint waypoint) { + public static void storeWaypointStaticMap(cgCache cache, Activity activity, cgWaypoint waypoint, boolean waitForResult) { int edge = StaticMapsProvider.guessMinDisplaySide(activity); - storeWaypointStaticMap(cache, edge, waypoint); + storeWaypointStaticMap(cache, edge, waypoint, waitForResult); } - private static void storeWaypointStaticMap(cgCache cache, int edge, cgWaypoint waypoint) { + private static void storeWaypointStaticMap(cgCache cache, int edge, cgWaypoint waypoint, final boolean waitForResult) { if (waypoint.getCoords() == null) { return; } String wpLatlonMap = waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); String wpMarkerUrl = getWpMarkerUrl(waypoint); // download map images in separate background thread for higher performance - downloadMaps(cache, wpMarkerUrl, "wp" + waypoint.getId() + "_", wpLatlonMap, edge, ""); + downloadMaps(cache, wpMarkerUrl, "wp" + waypoint.getId() + "_", wpLatlonMap, edge, "", waitForResult); } - public static void storeCacheStaticMap(cgCache cache, Activity activity) { - int edge = StaticMapsProvider.guessMinDisplaySide(activity); - storeCacheStaticMap(cache, edge); + public static void storeCacheStaticMap(cgCache cache, Activity activity, final boolean waitForResult) { + int edge = guessMinDisplaySide(activity); + storeCacheStaticMap(cache, edge, waitForResult); } - private static void storeCacheStaticMap(cgCache cache, int edge) { + private static void storeCacheStaticMap(cgCache cache, int edge, final boolean waitForResult) { final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); final StringBuilder waypoints = new StringBuilder(); if (cache.hasWaypoints()) { @@ -107,7 +107,7 @@ public class StaticMapsProvider { } // download map images in separate background thread for higher performance final String cacheMarkerUrl = getCacheMarkerUrl(cache); - downloadMaps(cache, cacheMarkerUrl, "", latlonMap, edge, waypoints.toString()); + downloadMaps(cache, cacheMarkerUrl, "", latlonMap, edge, waypoints.toString(), waitForResult); } private static int guessMinDisplaySide(Activity activity) { @@ -124,15 +124,20 @@ public class StaticMapsProvider { } private static void downloadMaps(final cgCache cache, final String markerUrl, final String prefix, final String latlonMap, final int edge, - final String waypoints) { - Thread staticMapsThread = new Thread("getting static map") { - @Override - public void run() { - downloadMapsInThread(cache, markerUrl, prefix, latlonMap, edge, waypoints); - } - }; - staticMapsThread.setPriority(Thread.MIN_PRIORITY); - staticMapsThread.start(); + final String waypoints, boolean waitForResult) { + if (waitForResult) { + downloadDifferentZooms(cache, markerUrl, prefix, latlonMap, edge, waypoints); + } + else { + Thread staticMapsThread = new Thread("getting static map") { + @Override + public void run() { + downloadDifferentZooms(cache, markerUrl, prefix, latlonMap, edge, waypoints); + } + }; + staticMapsThread.setPriority(Thread.MIN_PRIORITY); + staticMapsThread.start(); + } } private static String getCacheMarkerUrl(final cgCache cache) { @@ -165,7 +170,7 @@ public class StaticMapsProvider { /** * Check if at least one map file exists for the given geocode. - * + * * @param geocode * @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise */ diff --git a/main/src/cgeo/geocaching/activity/AbstractActivity.java b/main/src/cgeo/geocaching/activity/AbstractActivity.java index e700c72..941cd08 100644 --- a/main/src/cgeo/geocaching/activity/AbstractActivity.java +++ b/main/src/cgeo/geocaching/activity/AbstractActivity.java @@ -95,4 +95,12 @@ public abstract class AbstractActivity extends Activity implements IAbstractActi Compatibility.disableSuggestions(edit); } + protected void restartActivity() { + Compatibility.restartActivity(this); + } + + public void invalidateOptionsMenuCompatible() { + ActivityMixin.invalidateOptionsMenu(this); + } + } diff --git a/main/src/cgeo/geocaching/activity/AbstractListActivity.java b/main/src/cgeo/geocaching/activity/AbstractListActivity.java index 00e04cf..b3bbb3f 100644 --- a/main/src/cgeo/geocaching/activity/AbstractListActivity.java +++ b/main/src/cgeo/geocaching/activity/AbstractListActivity.java @@ -3,6 +3,7 @@ package cgeo.geocaching.activity; import cgeo.geocaching.cgBase; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.compatibility.Compatibility; import android.app.ListActivity; import android.content.res.Resources; @@ -85,4 +86,8 @@ public abstract class AbstractListActivity extends ListActivity implements ActivityMixin.addVisitMenu(this, menu, cache); } + @Override + public void invalidateOptionsMenuCompatible() { + Compatibility.invalidateOptionsMenu(this); + } } diff --git a/main/src/cgeo/geocaching/activity/ActivityMixin.java b/main/src/cgeo/geocaching/activity/ActivityMixin.java index 30bc7bb..0e0041a 100644 --- a/main/src/cgeo/geocaching/activity/ActivityMixin.java +++ b/main/src/cgeo/geocaching/activity/ActivityMixin.java @@ -4,6 +4,7 @@ import cgeo.geocaching.R; import cgeo.geocaching.Settings; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgeo; +import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.enumerations.LogType; import org.apache.commons.lang3.StringUtils; @@ -153,4 +154,8 @@ public final class ActivityMixin { abstractActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } + + public static void invalidateOptionsMenu(Activity activity) { + Compatibility.invalidateOptionsMenu(activity); + } } diff --git a/main/src/cgeo/geocaching/activity/IAbstractActivity.java b/main/src/cgeo/geocaching/activity/IAbstractActivity.java index 85a112a..dd22cff 100644 --- a/main/src/cgeo/geocaching/activity/IAbstractActivity.java +++ b/main/src/cgeo/geocaching/activity/IAbstractActivity.java @@ -27,4 +27,5 @@ public interface IAbstractActivity { void addVisitMenu(Menu menu, cgCache cache); + public void invalidateOptionsMenuCompatible(); } diff --git a/main/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java b/main/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java index 3bea266..fd19353 100644 --- a/main/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java +++ b/main/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java @@ -5,6 +5,7 @@ import cgeo.geocaching.SearchResult; import cgeo.geocaching.Settings; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgGeo; +import cgeo.geocaching.activity.IAbstractActivity; import cgeo.geocaching.apps.AbstractAppFactory; import org.apache.commons.lang3.ArrayUtils; @@ -62,12 +63,14 @@ public final class CacheListAppFactory extends AbstractAppFactory { } public static boolean onMenuItemSelected(final MenuItem item, - final cgGeo geo, final List<cgCache> caches, final Activity activity, + final cgGeo geo, final List<cgCache> caches, final IAbstractActivity activity, final SearchResult search) { CacheListApp app = (CacheListApp) getAppFromMenuItem(item, apps); if (app != null) { try { - return app.invoke(geo, caches, activity, search); + boolean result = app.invoke(geo, caches, (Activity) activity, search); + activity.invalidateOptionsMenuCompatible(); + return result; } catch (Exception e) { Log.e(Settings.tag, "CacheListAppFactory.onMenuItemSelected: " + e.toString()); } diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 73f53ad..b3e5016 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -1770,17 +1770,20 @@ public class cgData { return null; } - Set<cgCache> caches = loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB); + final Set<cgCache> caches = loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB); - Double latMin = 360.0; - Double latMax = 0.0; - Double lonMin = 360.0; - Double lonMax = 0.0; + double latMin = 360.0; + double latMax = -360.0; + double lonMin = 360.0; + double lonMax = -360.0; for (cgCache cache : caches) { - latMin = Math.min(cache.getCoords().getLatitude(), latMin); - latMax = Math.max(cache.getCoords().getLatitude(), latMax); - lonMin = Math.min(cache.getCoords().getLongitude(), lonMin); - lonMax = Math.max(cache.getCoords().getLongitude(), lonMax); + final Geopoint coords = cache.getCoords(); + double latitude = coords.getLatitude(); + latMin = Math.min(latitude, latMin); + latMax = Math.max(latitude, latMax); + double longitude = coords.getLongitude(); + lonMin = Math.min(longitude, lonMin); + lonMax = Math.max(longitude, lonMax); } final List<Number> viewport = new ArrayList<Number>(); diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index 0ac8461..7d3a057 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -8,6 +8,7 @@ import cgeo.geocaching.apps.cachelist.CacheListAppFactory; import cgeo.geocaching.enumerations.CacheListType; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.LoadFlag; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.files.GPXImporter; @@ -78,6 +79,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -173,7 +175,9 @@ public class cgeocaches extends AbstractListActivity { setTitle(title + " [" + search.getCount() + "]"); cacheList.clear(); - final Set<cgCache> caches = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); + EnumSet<LoadFlag> loadFlags = LoadFlags.LOAD_CACHE_OR_DB; + loadFlags.add(LoadFlag.LOAD_OFFLINE_LOG); + final Set<cgCache> caches = search.getCachesFromSearchResult(loadFlags); if (CollectionUtils.isNotEmpty(caches)) { cacheList.addAll(caches); Collections.sort(cacheList, gcComparator); @@ -977,24 +981,31 @@ public class cgeocaches extends AbstractListActivity { if (adapter != null) { adapter.switchSelectMode(); } + invalidateOptionsMenuCompatible(); return true; case MENU_REFRESH_STORED: refreshStored(); + invalidateOptionsMenuCompatible(); return true; case MENU_DROP_CACHES: dropStored(false); + invalidateOptionsMenuCompatible(); return false; case MENU_DROP_CACHES_AND_LIST: dropStored(true); + invalidateOptionsMenuCompatible(); return true; case MENU_IMPORT_GPX: importGpx(); + invalidateOptionsMenuCompatible(); return false; case MENU_CREATE_LIST: createList(null); + invalidateOptionsMenuCompatible(); return false; case MENU_DROP_LIST: removeList(true); + invalidateOptionsMenuCompatible(); return false; case MENU_RENAME_LIST: renameList(); @@ -1003,6 +1014,7 @@ public class cgeocaches extends AbstractListActivity { if (adapter != null) { adapter.invertSelection(); } + invalidateOptionsMenuCompatible(); return false; case MENU_SORT_DISTANCE: setComparator(item, null); @@ -1027,6 +1039,7 @@ public class cgeocaches extends AbstractListActivity { return false; case MENU_SWITCH_LIST: selectList(null); + invalidateOptionsMenuCompatible(); return false; case MENU_SORT_RATING: setComparator(item, new RatingComparator()); @@ -1074,9 +1087,11 @@ public class cgeocaches extends AbstractListActivity { return false; case MENU_REMOVE_FROM_HISTORY: removeFromHistoryCheck(); + invalidateOptionsMenuCompatible(); return false; case MENU_MOVE_TO_LIST: moveCachesToOtherList(); + invalidateOptionsMenuCompatible(); return true; } @@ -1284,6 +1299,7 @@ public class cgeocaches extends AbstractListActivity { if (adapter != null) { adapter.setFilter(filter); prepareFilterBar(); + invalidateOptionsMenuCompatible(); return true; } return false; @@ -2377,6 +2393,7 @@ public class cgeocaches extends AbstractListActivity { setLoadingCaches(); (new MoveCachesToListThread(listId, new MoveHandler())).start(); + invalidateOptionsMenuCompatible(); } private class MoveHandler extends Handler { diff --git a/main/src/cgeo/geocaching/cgeocoords.java b/main/src/cgeo/geocaching/cgeocoords.java index 3aadf05..b4868ce 100644 --- a/main/src/cgeo/geocaching/cgeocoords.java +++ b/main/src/cgeo/geocaching/cgeocoords.java @@ -5,6 +5,10 @@ import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.geopoint.Geopoint.DDD; +import cgeo.geocaching.geopoint.Geopoint.DMM; +import cgeo.geocaching.geopoint.Geopoint.DMS; +import cgeo.geocaching.geopoint.Geopoint.Direction; import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.geopoint.GeopointParser.ParseException; @@ -147,41 +151,10 @@ public class cgeocoords extends Dialog { if (gp == null) { return; } - double lat = 0.0; - if (gp.getLatitude() < 0) { - bLat.setText("S"); - } else { - bLat.setText("N"); - } - - lat = Math.abs(gp.getLatitude()); - - double lon = 0.0; - if (gp.getLongitude() < 0) { - bLon.setText("W"); - } else { - bLon.setText("E"); - } - - lon = Math.abs(gp.getLongitude()); - - final int latDeg = (int) Math.floor(lat); - final int latDegFrac = (int) Math.round((lat - latDeg) * 100000); - final int latMin = (int) Math.floor((lat - latDeg) * 60); - final int latMinFrac = (int) Math.round(((lat - latDeg) * 60 - latMin) * 1000); - - final int latSec = (int) Math.floor(((lat - latDeg) * 60 - latMin) * 60); - final int latSecFrac = (int) Math.round((((lat - latDeg) * 60 - latMin) * 60 - latSec) * 1000); - - final int lonDeg = (int) Math.floor(lon); - final int lonDegFrac = (int) Math.round((lon - lonDeg) * 100000); - - final int lonMin = (int) Math.floor((lon - lonDeg) * 60); - final int lonMinFrac = (int) Math.round(((lon - lonDeg) * 60 - lonMin) * 1000); - - final int lonSec = (int) Math.floor(((lon - lonDeg) * 60 - lonMin) * 60); - final int lonSecFrac = (int) Math.round((((lon - lonDeg) * 60 - lonMin) * 60 - lonSec) * 1000); + Direction dir = gp.asDirection(); + bLat.setText(String.valueOf(dir.latDir)); + bLon.setText(String.valueOf(dir.lonDir)); switch (currentFormat) { case Plain: @@ -207,10 +180,11 @@ public class cgeocoords extends Dialog { tLatSep2.setText("°"); tLonSep2.setText("°"); - eLatDeg.setText(addZeros(latDeg, 2)); - eLatMin.setText(addZeros(latDegFrac, 5)); - eLonDeg.setText(addZeros(lonDeg, 3)); - eLonMin.setText(addZeros(lonDegFrac, 5)); + DDD ddd = gp.asDDD(); + eLatDeg.setText(addZeros(ddd.latDeg, 2)); + eLatMin.setText(addZeros(ddd.latDegFrac, 5)); + eLonDeg.setText(addZeros(ddd.lonDeg, 3)); + eLonMin.setText(addZeros(ddd.lonDegFrac, 5)); break; case Min: // DDD° MM.MMM findViewById(R.id.coordTable).setVisibility(View.VISIBLE); @@ -230,12 +204,13 @@ public class cgeocoords extends Dialog { tLatSep3.setText("'"); tLonSep3.setText("'"); - eLatDeg.setText(addZeros(latDeg, 2)); - eLatMin.setText(addZeros(latMin, 2)); - eLatSec.setText(addZeros(latMinFrac, 3)); - eLonDeg.setText(addZeros(lonDeg, 3)); - eLonMin.setText(addZeros(lonMin, 2)); - eLonSec.setText(addZeros(lonMinFrac, 3)); + DMM dmm = gp.asDMM(); + eLatDeg.setText(addZeros(dmm.latDeg, 2)); + eLatMin.setText(addZeros(dmm.latMin, 2)); + eLatSec.setText(addZeros(dmm.latMinFrac, 3)); + eLonDeg.setText(addZeros(dmm.lonDeg, 3)); + eLonMin.setText(addZeros(dmm.lonMin, 2)); + eLonSec.setText(addZeros(dmm.lonMinFrac, 3)); break; case Sec: // DDD° MM SS.SSS findViewById(R.id.coordTable).setVisibility(View.VISIBLE); @@ -255,14 +230,15 @@ public class cgeocoords extends Dialog { tLatSep3.setText("."); tLonSep3.setText("."); - eLatDeg.setText(addZeros(latDeg, 2)); - eLatMin.setText(addZeros(latMin, 2)); - eLatSec.setText(addZeros(latSec, 2)); - eLatSub.setText(addZeros(latSecFrac, 3)); - eLonDeg.setText(addZeros(lonDeg, 3)); - eLonMin.setText(addZeros(lonMin, 2)); - eLonSec.setText(addZeros(lonSec, 2)); - eLonSub.setText(addZeros(lonSecFrac, 3)); + DMS dms = gp.asDMS(); + eLatDeg.setText(addZeros(dms.latDeg, 2)); + eLatMin.setText(addZeros(dms.latMin, 2)); + eLatSec.setText(addZeros(dms.latSec, 2)); + eLatSub.setText(addZeros(dms.latSecFrac, 3)); + eLonDeg.setText(addZeros(dms.lonDeg, 3)); + eLonMin.setText(addZeros(dms.lonMin, 2)); + eLonSec.setText(addZeros(dms.lonSec, 2)); + eLonSub.setText(addZeros(dms.lonSecFrac, 3)); break; } } @@ -385,51 +361,35 @@ public class cgeocoords extends Dialog { return true; } - int latDeg = 0, latMin = 0, latSec = 0; - int lonDeg = 0, lonMin = 0, lonSec = 0; - double latDegFrac = 0.0, latMinFrac = 0.0, latSecFrac = 0.0; - double lonDegFrac = 0.0, lonMinFrac = 0.0, lonSecFrac = 0.0; - - try { - latDeg = Integer.parseInt(eLatDeg.getText().toString()); - lonDeg = Integer.parseInt(eLonDeg.getText().toString()); - latDegFrac = Double.parseDouble("0." + eLatMin.getText().toString()); - lonDegFrac = Double.parseDouble("0." + eLonMin.getText().toString()); - latMin = Integer.parseInt(eLatMin.getText().toString()); - lonMin = Integer.parseInt(eLonMin.getText().toString()); - latMinFrac = Double.parseDouble("0." + eLatSec.getText().toString()); - lonMinFrac = Double.parseDouble("0." + eLonSec.getText().toString()); - latSec = Integer.parseInt(eLatSec.getText().toString()); - lonSec = Integer.parseInt(eLonSec.getText().toString()); - latSecFrac = Double.parseDouble("0." + eLatSub.getText().toString()); - lonSecFrac = Double.parseDouble("0." + eLonSub.getText().toString()); - - } catch (NumberFormatException e) { - } - - double latitude = 0.0; - double longitude = 0.0; + String latDir = bLat.getText().toString(); + String lonDir = bLon.getText().toString(); + String latDeg = eLatDeg.getText().toString(); + String lonDeg = eLonDeg.getText().toString(); + String latDegFrac = eLatMin.getText().toString(); + String lonDegFrac = eLonMin.getText().toString(); + String latMin = eLatMin.getText().toString(); + String lonMin = eLonMin.getText().toString(); + String latMinFrac = eLatSec.getText().toString(); + String lonMinFrac = eLonSec.getText().toString(); + String latSec = eLatSec.getText().toString(); + String lonSec = eLonSec.getText().toString(); + String latSecFrac = eLatSub.getText().toString(); + String lonSecFrac = eLonSub.getText().toString(); switch (currentFormat) { case Deg: - latitude = latDeg + latDegFrac; - longitude = lonDeg + lonDegFrac; + gp = DDD.createGeopoint(latDir, latDeg, latDegFrac, lonDir, lonDeg, lonDegFrac); break; case Min: - latitude = latDeg + latMin / 60.0 + latMinFrac / 60.0; - longitude = lonDeg + lonMin / 60.0 + lonMinFrac / 60.0; + gp = DMM.createGeopoint(latDir, latDeg, latMin, latMinFrac, lonDir, lonDeg, lonMin, lonMinFrac); break; case Sec: - latitude = latDeg + latMin / 60.0 + latSec / 60.0 / 60.0 + latSecFrac / 60.0 / 60.0; - longitude = lonDeg + lonMin / 60.0 + lonSec / 60.0 / 60.0 + lonSecFrac / 60.0 / 60.0; + gp = DMS.createGeopoint(latDir, latDeg, latMin, latSec, latSecFrac, lonDir, lonDeg, lonMin, lonSec, lonSecFrac); break; case Plain: // This case has been handled above } - latitude *= (bLat.getText().toString().equalsIgnoreCase("S") ? -1 : 1); - longitude *= (bLon.getText().toString().equalsIgnoreCase("W") ? -1 : 1); - gp = new Geopoint(latitude, longitude); return true; } diff --git a/main/src/cgeo/geocaching/cgeonavigate.java b/main/src/cgeo/geocaching/cgeonavigate.java index e0ffb80..e5600a9 100644 --- a/main/src/cgeo/geocaching/cgeonavigate.java +++ b/main/src/cgeo/geocaching/cgeonavigate.java @@ -232,8 +232,7 @@ public class cgeonavigate extends AbstractActivity { public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); - MenuItem item; - item = menu.findItem(1); + MenuItem item = menu.findItem(MENU_SWITCH_COMPASS_GPS); if (Settings.isUseCompass()) { item.setTitle(res.getString(R.string.use_gps)); } else { @@ -252,6 +251,7 @@ public class cgeonavigate extends AbstractActivity { } else if (id == MENU_SWITCH_COMPASS_GPS) { boolean oldSetting = Settings.isUseCompass(); Settings.setUseCompass(!oldSetting); + invalidateOptionsMenuCompatible(); if (oldSetting) { if (dir != null) { dir = app.removeDir(); diff --git a/main/src/cgeo/geocaching/cgeopopup.java b/main/src/cgeo/geocaching/cgeopopup.java index ace1abc..0e0f1b2 100644 --- a/main/src/cgeo/geocaching/cgeopopup.java +++ b/main/src/cgeo/geocaching/cgeopopup.java @@ -559,6 +559,7 @@ public class cgeopopup extends AbstractActivity { @Override public void run() { cache.store(cgeopopup.this, handler); + invalidateOptionsMenuCompatible(); } } diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java index ee9de72..2508750 100644 --- a/main/src/cgeo/geocaching/cgeowaypointadd.java +++ b/main/src/cgeo/geocaching/cgeowaypointadd.java @@ -37,6 +37,7 @@ public class cgeowaypointadd extends AbstractActivity { private UpdateLocationCallback geoUpdate = new update(); private ProgressDialog waitDialog = null; private cgWaypoint waypoint = null; + private Geopoint gpTemp = null; private WaypointType type = WaypointType.OWN; private String prefix = "OWN"; private String lookup = "---"; @@ -53,11 +54,6 @@ public class cgeowaypointadd extends AbstractActivity { public void handleMessage(Message msg) { try { if (waypoint == null) { - if (waitDialog != null) { - waitDialog.dismiss(); - waitDialog = null; - } - id = -1; } else { geocode = waypoint.getGeocode(); @@ -74,22 +70,18 @@ public class cgeowaypointadd extends AbstractActivity { } ((EditText) findViewById(R.id.name)).setText(Html.fromHtml(StringUtils.trimToEmpty(waypoint.getName())).toString()); ((EditText) findViewById(R.id.note)).setText(Html.fromHtml(StringUtils.trimToEmpty(waypoint.getNote())).toString()); - - if (waitDialog != null) { - waitDialog.dismiss(); - waitDialog = null; - } } if (own) { initializeWaypointTypeSelector(); } } catch (Exception e) { + Log.e(Settings.tag, "cgeowaypointadd.loadWaypointHandler: " + e.toString()); + } finally { if (waitDialog != null) { waitDialog.dismiss(); waitDialog = null; } - Log.e(Settings.tag, "cgeowaypointadd.loadWaypointHandler: " + e.toString()); } } }; @@ -265,6 +257,8 @@ public class cgeowaypointadd extends AbstractActivity { Geopoint gp = null; if (waypoint != null && waypoint.getCoords() != null) { gp = waypoint.getCoords(); + } else if (gpTemp != null) { + gp = gpTemp; } cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS); cgeocoords coordsDialog = new cgeocoords(cgeowaypointadd.this, cache, gp, geo); @@ -276,6 +270,8 @@ public class cgeowaypointadd extends AbstractActivity { ((Button) findViewById(R.id.buttonLongitude)).setText(gp.format(GeopointFormatter.Format.LON_DECMINUTE)); if (waypoint != null) { waypoint.setCoords(gp); + } else { + gpTemp = gp; } } }); @@ -384,7 +380,7 @@ public class cgeowaypointadd extends AbstractActivity { if (app.saveOwnWaypoint(id, geocode, waypoint)) { StaticMapsProvider.removeWpStaticMaps(id, geocode); if (Settings.isStoreOfflineWpMaps()) { - StaticMapsProvider.storeWaypointStaticMap(app.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB), cgeowaypointadd.this, waypoint); + StaticMapsProvider.storeWaypointStaticMap(app.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB), cgeowaypointadd.this, waypoint, false); } finish(); return; diff --git a/main/src/cgeo/geocaching/compatibility/AndroidLevel11.java b/main/src/cgeo/geocaching/compatibility/AndroidLevel11.java new file mode 100644 index 0000000..acb9dca --- /dev/null +++ b/main/src/cgeo/geocaching/compatibility/AndroidLevel11.java @@ -0,0 +1,17 @@ +package cgeo.geocaching.compatibility; + +import android.app.Activity; + +/** + * Android level 11 support + * + * @author bananeweizen + * + */ +public class AndroidLevel11 implements AndroidLevel11Interface { + + public void invalidateOptionsMenu(final Activity activity) { + activity.invalidateOptionsMenu(); + } + +} diff --git a/main/src/cgeo/geocaching/compatibility/AndroidLevel11Dummy.java b/main/src/cgeo/geocaching/compatibility/AndroidLevel11Dummy.java new file mode 100644 index 0000000..cd18f67 --- /dev/null +++ b/main/src/cgeo/geocaching/compatibility/AndroidLevel11Dummy.java @@ -0,0 +1,17 @@ +package cgeo.geocaching.compatibility; + +import android.app.Activity; + +/** + * dummy class which has no functionality in the level 11 API + * + * @author bananeweizen + * + */ +public class AndroidLevel11Dummy implements AndroidLevel11Interface { + + public void invalidateOptionsMenu(final Activity activity) { + // do nothing + } + +} diff --git a/main/src/cgeo/geocaching/compatibility/AndroidLevel11Interface.java b/main/src/cgeo/geocaching/compatibility/AndroidLevel11Interface.java new file mode 100644 index 0000000..236e92d --- /dev/null +++ b/main/src/cgeo/geocaching/compatibility/AndroidLevel11Interface.java @@ -0,0 +1,8 @@ +package cgeo.geocaching.compatibility; + +import android.app.Activity; + +public interface AndroidLevel11Interface { + public void invalidateOptionsMenu(final Activity activity); + +} diff --git a/main/src/cgeo/geocaching/compatibility/AndroidLevel8.java b/main/src/cgeo/geocaching/compatibility/AndroidLevel8.java index 259cb5c..6d01e55 100644 --- a/main/src/cgeo/geocaching/compatibility/AndroidLevel8.java +++ b/main/src/cgeo/geocaching/compatibility/AndroidLevel8.java @@ -7,14 +7,14 @@ import android.app.backup.BackupManager; import android.util.Log; import android.view.Display; -public class AndroidLevel8 { +public class AndroidLevel8 implements AndroidLevel8Interface { - static public int getRotation(final Activity activity) { + public int getRotation(final Activity activity) { Display display = activity.getWindowManager().getDefaultDisplay(); return display.getRotation(); } - static public void dataChanged(final String name) { + public void dataChanged(final String name) { Log.i(Settings.tag, "Requesting settings backup with settings manager"); BackupManager.dataChanged(name); } diff --git a/main/src/cgeo/geocaching/compatibility/AndroidLevel8Dummy.java b/main/src/cgeo/geocaching/compatibility/AndroidLevel8Dummy.java new file mode 100644 index 0000000..664b55b --- /dev/null +++ b/main/src/cgeo/geocaching/compatibility/AndroidLevel8Dummy.java @@ -0,0 +1,14 @@ +package cgeo.geocaching.compatibility; + +import android.app.Activity; + +public class AndroidLevel8Dummy implements AndroidLevel8Interface { + + public int getRotation(final Activity activity) { + return 0; + } + + public void dataChanged(final String name) { + // do nothing + } +} diff --git a/main/src/cgeo/geocaching/compatibility/AndroidLevel8Interface.java b/main/src/cgeo/geocaching/compatibility/AndroidLevel8Interface.java new file mode 100644 index 0000000..b1c4f81 --- /dev/null +++ b/main/src/cgeo/geocaching/compatibility/AndroidLevel8Interface.java @@ -0,0 +1,9 @@ +package cgeo.geocaching.compatibility; + +import android.app.Activity; + +public interface AndroidLevel8Interface { + public int getRotation(final Activity activity); + public void dataChanged(final String name); + +}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/compatibility/Compatibility.java b/main/src/cgeo/geocaching/compatibility/Compatibility.java index 15d1924..409f837 100644 --- a/main/src/cgeo/geocaching/compatibility/Compatibility.java +++ b/main/src/cgeo/geocaching/compatibility/Compatibility.java @@ -1,8 +1,10 @@ package cgeo.geocaching.compatibility; import cgeo.geocaching.Settings; +import cgeo.geocaching.activity.AbstractActivity; import android.app.Activity; +import android.content.Intent; import android.content.res.Configuration; import android.net.Uri; import android.os.Build; @@ -12,27 +14,27 @@ import android.view.Display; import android.view.Surface; import android.widget.EditText; -import java.lang.reflect.Method; - public final class Compatibility { private final static int sdkVersion = Integer.parseInt(Build.VERSION.SDK); private final static boolean isLevel8 = sdkVersion >= 8; private final static boolean isLevel5 = sdkVersion >= 5; - private static Method dataChangedMethod = null; - private static Method getRotationMethod = null; + private final static AndroidLevel8Interface level8; + private final static AndroidLevel11Interface level11; static { if (isLevel8) { - try { - final Class<?> cl = Class.forName("cgeo.geocaching.compatibility.AndroidLevel8"); - dataChangedMethod = cl.getDeclaredMethod("dataChanged", String.class); - getRotationMethod = cl.getDeclaredMethod("getRotation", Activity.class); - } catch (final Exception e) { - // Exception can be ClassNotFoundException, SecurityException or NoSuchMethodException - Log.e(Settings.tag, "Cannot load AndroidLevel8 class", e); - } + level8 = new AndroidLevel8(); + } + else { + level8 = new AndroidLevel8Dummy(); + } + if (sdkVersion >= 11) { + level11 = new AndroidLevel11(); + } + else { + level11 = new AndroidLevel11Dummy(); } } @@ -40,7 +42,7 @@ public final class Compatibility { final Activity activity) { if (isLevel8) { try { - final int rotation = (Integer) getRotationMethod.invoke(null, activity); + final int rotation = level8.getRotation(activity); if (rotation == Surface.ROTATION_90) { return directionNowPre + 90; } else if (rotation == Surface.ROTATION_180) { @@ -72,14 +74,7 @@ public final class Compatibility { } public static void dataChanged(final String name) { - if (isLevel8) { - try { - dataChangedMethod.invoke(null, name); - } catch (final Exception e) { - // This should never happen: IllegalArgumentException, IllegalAccessException or InvocationTargetException - Log.e(Settings.tag, "Cannot call dataChanged()", e); - } - } + level8.dataChanged(name); } public static void disableSuggestions(EditText edit) { @@ -94,4 +89,21 @@ public final class Compatibility { } } + public static void restartActivity(AbstractActivity activity) { + final Intent intent = activity.getIntent(); + if (isLevel5) { + activity.overridePendingTransition(0, 0); + intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + } + activity.finish(); + if (isLevel5) { + activity.overridePendingTransition(0, 0); + } + activity.startActivity(intent); + } + + public static void invalidateOptionsMenu(final Activity activity) { + level11.invalidateOptionsMenu(activity); + } + } diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java index 43aef68..ca67014 100644 --- a/main/src/cgeo/geocaching/geopoint/Geopoint.java +++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java @@ -1,7 +1,12 @@ package cgeo.geocaching.geopoint; +import org.apache.commons.lang3.StringUtils; + import android.location.Location; +import java.math.BigDecimal; +import java.math.RoundingMode; + /** * Abstraction of geographic point. */ @@ -14,6 +19,11 @@ public final class Geopoint private final double latitude; private final double longitude; + private Direction direction; + private DDD ddd; + private DMM dmm; + private DMS dms; + /** * Creates new Geopoint with given latitude and longitude (both degree). * @@ -233,6 +243,228 @@ public final class Geopoint return format(GeopointFormatter.Format.LAT_LON_DECMINUTE); } + /** + * Converts this geopoint to value type Direction. + * + * @return Direction + */ + public Direction asDirection() { + if (direction == null) { // because geopoint is immutable we can "cache" the result + direction = new Direction(getLatitude(), getLongitude()); + } + return direction; + } + + /** + * Converts this geopoint to value type DDD. + * + * @return + */ + public DDD asDDD() { + if (ddd == null) { + ddd = new DDD(getLatitude(), getLongitude()); + } + return ddd; + } + + /** + * Converts this geopoint to value type DMM. + * + * @return + */ + public DMM asDMM() { + if (dmm == null) { + dmm = new DMM(getLatitude(), getLongitude()); + } + return dmm; + } + + /** + * Converts this geopoint to value type DMS. + * + * @return + */ + public DMS asDMS() { + if (dms == null) { + dms = new DMS(getLatitude(), getLongitude()); + } + return dms; + } + + /* Constant values needed for calculation */ + private static final double D60 = 60.0d; + private static final double D1000 = 1000.0d; + private static final double D3600 = 3600.0d; + private static final BigDecimal BD_SIXTY = BigDecimal.valueOf(D60); + private static final BigDecimal BD_THOUSAND = BigDecimal.valueOf(D1000); + private static final BigDecimal BD_ONEHOUNDREDTHOUSAND = BigDecimal.valueOf(100000.0d); + + /** + * Value type for the direction. + */ + public static class Direction { + /** latitude direction, 'N' or 'S' */ + public final char latDir; + /** longitude direction, 'E' or 'W' */ + public final char lonDir; + + private Direction(final double latSigned, final double lonSigned) { + latDir = latSigned < 0 ? 'S' : 'N'; + lonDir = lonSigned < 0 ? 'W' : 'E'; + } + + protected static String addZeros(final int value, final int len) { + return StringUtils.leftPad(Integer.toString(value), len, '0'); + } + } + + /** + * Value type for the DDD.DDDDD format. + */ + public static final class DDD extends Direction { + + /** latitude degree value */ + public final int latDeg; + /** fractional part of the latitude degree value */ + public final int latDegFrac; + + public final int lonDeg; + public final int lonDegFrac; + + private DDD(final double latSigned, final double lonSigned) { + super(latSigned, lonSigned); + BigDecimal bdLat = BigDecimal.valueOf(latSigned).abs(); + latDeg = bdLat.intValue(); + BigDecimal bdLatFrac = bdLat.subtract(BigDecimal.valueOf(latDeg)).multiply(BD_ONEHOUNDREDTHOUSAND); + latDegFrac = bdLatFrac.setScale(0, RoundingMode.HALF_UP).intValue(); + + BigDecimal bdlon = BigDecimal.valueOf(lonSigned).abs(); + lonDeg = bdlon.intValue(); + BigDecimal bdLonFrac = bdlon.subtract(BigDecimal.valueOf(lonDeg)).multiply(BD_ONEHOUNDREDTHOUSAND); + lonDegFrac = bdLonFrac.setScale(0, RoundingMode.HALF_UP).intValue(); + } + + public static Geopoint createGeopoint(final String latDir, final String latDeg, final String latDegFrac, + final String lonDir, final String lonDeg, final String lonDegFrac) { + double lat = 0.0d; + double lon = 0.0d; + try { + lat = Double.parseDouble(latDeg + "." + addZeros(Integer.parseInt(latDegFrac), 5)); + lon = Double.parseDouble(lonDeg + "." + addZeros(Integer.parseInt(lonDegFrac), 5)); + } catch (NumberFormatException e) { + } + lat *= "S".equalsIgnoreCase(latDir) ? -1 : 1; + lon *= "W".equalsIgnoreCase(lonDir) ? -1 : 1; + return new Geopoint(lat, lon); + } + } + + public static final class DMM extends Direction { + + public final int latDeg; + public final double latMinRaw; + public final int latMin; + public final int latMinFrac; + + public final int lonDeg; + public final double lonMinRaw; + public final int lonMin; + public final int lonMinFrac; + + private DMM(final double latSigned, final double lonSigned) { + super(latSigned, lonSigned); + BigDecimal bdLat = BigDecimal.valueOf(latSigned).abs(); + latDeg = bdLat.intValue(); + BigDecimal bdLatMin = bdLat.subtract(BigDecimal.valueOf(latDeg)).multiply(BD_SIXTY); + // Rounding here ... + bdLatMin = bdLatMin.setScale(3, RoundingMode.HALF_UP); + latMinRaw = bdLatMin.doubleValue(); + latMin = bdLatMin.intValue(); + BigDecimal bdLatMinFrac = bdLatMin.subtract(BigDecimal.valueOf(latMin)).multiply(BD_THOUSAND); + latMinFrac = bdLatMinFrac.setScale(0, RoundingMode.HALF_UP).intValue(); + + BigDecimal bdlon = BigDecimal.valueOf(lonSigned).abs(); + lonDeg = bdlon.intValue(); + BigDecimal bdLonMin = bdlon.subtract(BigDecimal.valueOf(lonDeg)).multiply(BD_SIXTY); + // Rounding here ... + bdLonMin = bdLonMin.setScale(3, RoundingMode.HALF_UP); + lonMinRaw = bdLonMin.doubleValue(); + lonMin = bdLonMin.intValue(); + BigDecimal bdLonMinFrac = bdLonMin.subtract(BigDecimal.valueOf(lonMin)).multiply(BD_THOUSAND); + lonMinFrac = bdLonMinFrac.setScale(0, RoundingMode.HALF_UP).intValue(); + } + + public static Geopoint createGeopoint(final String latDir, final String latDeg, final String latMin, final String latMinFrac, + final String lonDir, final String lonDeg, final String lonMin, final String lonMinFrac) { + double lat = 0.0d; + double lon = 0.0d; + try { + lat = Double.parseDouble(latDeg) + Double.parseDouble(latMin + "." + addZeros(Integer.parseInt(latMinFrac), 3)) / D60; + lon = Double.parseDouble(lonDeg) + Double.parseDouble(lonMin + "." + addZeros(Integer.parseInt(lonMinFrac), 3)) / D60; + } catch (NumberFormatException e) { + } + lat *= "S".equalsIgnoreCase(latDir) ? -1 : 1; + lon *= "W".equalsIgnoreCase(lonDir) ? -1 : 1; + return new Geopoint(lat, lon); + } + } + + public static final class DMS extends Direction { + + public final int latDeg; + public final int latMin; + public final double latSecRaw; + public final int latSec; + public final int latSecFrac; + + public final int lonDeg; + public final int lonMin; + public final double lonSecRaw; + public final int lonSec; + public final int lonSecFrac; + + private DMS(final double latSigned, final double lonSigned) { + super(latSigned, lonSigned); + BigDecimal bdLat = BigDecimal.valueOf(latSigned).abs(); + latDeg = bdLat.intValue(); + BigDecimal bdLatMin = bdLat.subtract(BigDecimal.valueOf(latDeg)).multiply(BD_SIXTY); + latMin = bdLatMin.intValue(); + BigDecimal bdLatSec = bdLatMin.subtract(BigDecimal.valueOf(latMin)).multiply(BD_SIXTY); + // Rounding here ... + bdLatSec = bdLatSec.setScale(3, RoundingMode.HALF_UP); + latSecRaw = bdLatSec.doubleValue(); + latSec = bdLatSec.intValue(); + BigDecimal bdLatSecFrac = bdLatSec.subtract(BigDecimal.valueOf(latSec)).multiply(BD_THOUSAND); + latSecFrac = bdLatSecFrac.setScale(0, RoundingMode.HALF_UP).intValue(); + + BigDecimal bdlon = BigDecimal.valueOf(lonSigned).abs(); + lonDeg = bdlon.intValue(); + BigDecimal bdLonMin = bdlon.subtract(BigDecimal.valueOf(lonDeg)).multiply(BD_SIXTY); + lonMin = bdLonMin.intValue(); + BigDecimal bdLonSec = bdLonMin.subtract(BigDecimal.valueOf(lonMin)).multiply(BD_SIXTY); + // Rounding here ... + bdLonSec = bdLonSec.setScale(3, RoundingMode.HALF_UP); + lonSecRaw = bdLonSec.doubleValue(); + lonSec = bdLonSec.intValue(); + BigDecimal bdLonSecFrac = bdLonSec.subtract(BigDecimal.valueOf(lonSec)).multiply(BD_THOUSAND); + lonSecFrac = bdLonSecFrac.setScale(0, RoundingMode.HALF_UP).intValue(); + } + + public static Geopoint createGeopoint(final String latDir, final String latDeg, final String latMin, final String latSec, final String latSecFrac, + final String lonDir, final String lonDeg, final String lonMin, final String lonSec, final String lonSecFrac) { + double lat = 0.0d; + double lon = 0.0d; + try { + lat = Double.parseDouble(latDeg) + Double.parseDouble(latMin) / D60 + Double.parseDouble(latSec + "." + addZeros(Integer.parseInt(latSecFrac), 3)) / D3600; + lon = Double.parseDouble(lonDeg) + Double.parseDouble(lonMin) / D60 + Double.parseDouble(lonSec + "." + addZeros(Integer.parseInt(lonSecFrac), 3)) / D3600; + } catch (NumberFormatException e) { + } + lat *= "S".equalsIgnoreCase(latDir) ? -1 : 1; + lon *= "W".equalsIgnoreCase(lonDir) ? -1 : 1; + return new Geopoint(lat, lon); + } + } + abstract public static class GeopointException extends RuntimeException { diff --git a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java index c4cfb8c..d0baee9 100644 --- a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java +++ b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java @@ -1,5 +1,8 @@ package cgeo.geocaching.geopoint; +import cgeo.geocaching.geopoint.Geopoint.DMM; +import cgeo.geocaching.geopoint.Geopoint.DMS; + import java.util.Locale; /** @@ -58,18 +61,9 @@ public class GeopointFormatter { final double latSigned = gp.getLatitude(); final double lonSigned = gp.getLongitude(); - final double lat = Math.abs(latSigned); - final double lon = Math.abs(lonSigned); - final double latFloor = Math.floor(lat); - final double lonFloor = Math.floor(lon); - final double latMin = (lat - latFloor) * 60; - final double lonMin = (lon - lonFloor) * 60; - final double latMinFloor = Math.floor(latMin); - final double lonMinFloor = Math.floor(lonMin); - final double latSec = (latMin - latMinFloor) * 60; - final double lonSec = (lonMin - lonMinFloor) * 60; - final char latDir = latSigned < 0 ? 'S' : 'N'; - final char lonDir = lonSigned < 0 ? 'W' : 'E'; + + DMM dmm = gp.asDMM(); + DMS dms = gp.asDMS(); switch (format) { case LAT_LON_DECDEGREE: @@ -79,34 +73,34 @@ public class GeopointFormatter return String.format((Locale) null, "%.6f,%.6f", latSigned, lonSigned); case LAT_LON_DECMINUTE: - return String.format("%c %02.0f° %06.3f · %c %03.0f° %06.3f", - latDir, latFloor, latMin, lonDir, lonFloor, lonMin); + return String.format("%c %02d° %06.3f · %c %03d° %06.3f", + dmm.latDir, dmm.latDeg, dmm.latMinRaw, dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); case LAT_LON_DECMINUTE_RAW: - return String.format((Locale) null, "%c %02.0f° %06.3f %c %03.0f° %06.3f", - latDir, latFloor, latMin, lonDir, lonFloor, lonMin); + return String.format((Locale) null, "%c %02d° %06.3f %c %03d° %06.3f", + dmm.latDir, dmm.latDeg, dmm.latMinRaw, dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); case LAT_LON_DECSECOND: - return String.format("%c %02.0f° %02.0f' %06.3f\" · %c %03.0f° %02.0f' %06.3f\"", - latDir, latFloor, latMinFloor, latSec, lonDir, lonFloor, lonMinFloor, lonSec); + return String.format("%c %02d° %02d' %06.3f\" · %c %03d° %02d' %06.3f\"", + dms.latDir, dms.latDeg, dms.latMin, dms.latSecRaw, dms.lonDir, dms.lonDeg, dms.lonMin, dms.lonSecRaw); case LAT_DECDEGREE_RAW: return String.format((Locale) null, "%.6f", latSigned); case LAT_DECMINUTE: - return String.format("%c %02.0f° %06.3f", latDir, latFloor, latMin); + return String.format("%c %02d° %06.3f", dmm.latDir, dmm.latDeg, dmm.latMinRaw); case LAT_DECMINUTE_RAW: - return String.format("%c %02.0f %06.3f", latDir, latFloor, latMin); + return String.format("%c %02d %06.3f", dmm.latDir, dmm.latDeg, dmm.latMinRaw); case LON_DECDEGREE_RAW: return String.format((Locale) null, "%.6f", lonSigned); case LON_DECMINUTE: - return String.format("%c %03.0f° %06.3f", lonDir, lonFloor, lonMin); + return String.format("%c %03d° %06.3f", dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); case LON_DECMINUTE_RAW: - return String.format("%c %03.0f %06.3f", lonDir, lonFloor, lonMin); + return String.format("%c %03d %06.3f", dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); } // Keep the compiler happy even though it cannot happen diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 0ddc4c3..5d8c0db 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -29,7 +29,7 @@ import cgeo.geocaching.maps.interfaces.MapActivityImpl; import cgeo.geocaching.maps.interfaces.MapControllerImpl; import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapViewImpl; -import cgeo.geocaching.maps.interfaces.OnDragListener; +import cgeo.geocaching.maps.interfaces.OnMapDragListener; import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; import cgeo.geocaching.utils.CancellableHandler; @@ -70,7 +70,7 @@ import java.util.Set; /** * Class representing the Map in c:geo */ -public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory { +public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFactory { /** Handler Messages */ private static final int HIDE_PROGRESS = 0; @@ -597,12 +597,14 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory switch (id) { case MENU_TRAIL_MODE: Settings.setMapTrail(!Settings.isMapTrail()); + ActivityMixin.invalidateOptionsMenu(activity); return true; case MENU_MAP_LIVE: Settings.setLiveMap(!Settings.isLiveMap()); liveChanged = true; search = null; searchIntent = null; + ActivityMixin.invalidateOptionsMenu(activity); return true; case MENU_STORE_CACHES: if (live && !isLoading() && CollectionUtils.isNotEmpty(caches)) { @@ -688,6 +690,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory overlayCaches.switchCircles(); mapView.repaintRequired(overlayCaches); + ActivityMixin.invalidateOptionsMenu(activity); return true; case MENU_AS_LIST: { final SearchResult searchResult = new SearchResult(); diff --git a/main/src/cgeo/geocaching/maps/google/GoogleMapView.java b/main/src/cgeo/geocaching/maps/google/GoogleMapView.java index 65e0ed1..9d9f3c9 100644 --- a/main/src/cgeo/geocaching/maps/google/GoogleMapView.java +++ b/main/src/cgeo/geocaching/maps/google/GoogleMapView.java @@ -12,7 +12,7 @@ import cgeo.geocaching.maps.interfaces.GeoPointImpl; import cgeo.geocaching.maps.interfaces.MapControllerImpl; import cgeo.geocaching.maps.interfaces.MapProjectionImpl; import cgeo.geocaching.maps.interfaces.MapViewImpl; -import cgeo.geocaching.maps.interfaces.OnDragListener; +import cgeo.geocaching.maps.interfaces.OnMapDragListener; import cgeo.geocaching.maps.interfaces.OverlayImpl; import cgeo.geocaching.maps.interfaces.OverlayImpl.overlayType; @@ -34,7 +34,7 @@ import android.widget.FrameLayout; public class GoogleMapView extends MapView implements MapViewImpl { private GestureDetector gestureDetector; - private OnDragListener onDragListener; + private OnMapDragListener onDragListener; public GoogleMapView(Context context, AttributeSet attrs) { super(context, attrs); @@ -151,7 +151,7 @@ public class GoogleMapView extends MapView implements MapViewImpl { } @Override - public void setOnDragListener(OnDragListener onDragListener) { + public void setOnDragListener(OnMapDragListener onDragListener) { this.onDragListener = onDragListener; } diff --git a/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java b/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java index ad35d26..08eaaf4 100644 --- a/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java +++ b/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java @@ -69,7 +69,7 @@ public interface MapViewImpl { */ void repaintRequired(GeneralOverlay overlay); - void setOnDragListener(OnDragListener onDragListener); + void setOnDragListener(OnMapDragListener onDragListener); /** * Indicates if overlay text or line colours should be dark (normal case) diff --git a/main/src/cgeo/geocaching/maps/interfaces/OnDragListener.java b/main/src/cgeo/geocaching/maps/interfaces/OnMapDragListener.java index 285aafa..0e51b32 100644 --- a/main/src/cgeo/geocaching/maps/interfaces/OnDragListener.java +++ b/main/src/cgeo/geocaching/maps/interfaces/OnMapDragListener.java @@ -6,7 +6,7 @@ package cgeo.geocaching.maps.interfaces; * @author cachapa * */ -public interface OnDragListener { +public interface OnMapDragListener { public void onDrag(); diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java index 9a64980..bcb6e61 100644 --- a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java +++ b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java @@ -10,7 +10,7 @@ import cgeo.geocaching.maps.interfaces.GeoPointImpl; import cgeo.geocaching.maps.interfaces.MapControllerImpl; import cgeo.geocaching.maps.interfaces.MapProjectionImpl; import cgeo.geocaching.maps.interfaces.MapViewImpl; -import cgeo.geocaching.maps.interfaces.OnDragListener; +import cgeo.geocaching.maps.interfaces.OnMapDragListener; import cgeo.geocaching.maps.interfaces.OverlayImpl; import cgeo.geocaching.maps.interfaces.OverlayImpl.overlayType; @@ -33,7 +33,7 @@ import android.view.MotionEvent; public class MapsforgeMapView extends MapView implements MapViewImpl { private GestureDetector gestureDetector; - private OnDragListener onDragListener; + private OnMapDragListener onDragListener; public MapsforgeMapView(Context context, AttributeSet attrs) { super(context, attrs); @@ -205,7 +205,7 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { } @Override - public void setOnDragListener(OnDragListener onDragListener) { + public void setOnDragListener(OnMapDragListener onDragListener) { this.onDragListener = onDragListener; } diff --git a/main/src/cgeo/geocaching/ui/DecryptTextClickListener.java b/main/src/cgeo/geocaching/ui/DecryptTextClickListener.java new file mode 100644 index 0000000..fd60553 --- /dev/null +++ b/main/src/cgeo/geocaching/ui/DecryptTextClickListener.java @@ -0,0 +1,31 @@ +package cgeo.geocaching.ui;
+
+import cgeo.geocaching.utils.CryptUtils;
+
+import android.text.Spannable;
+import android.view.View;
+import android.widget.TextView;
+
+public class DecryptTextClickListener implements View.OnClickListener {
+
+ public void onClick(View view) {
+ if (view == null) {
+ return;
+ }
+
+ try {
+ final TextView logView = (TextView) view;
+ CharSequence text = logView.getText();
+ if (text instanceof Spannable) {
+ Spannable span = (Spannable) text;
+ logView.setText(CryptUtils.rot13(span));
+ }
+ else {
+ String string = (String) text;
+ logView.setText(CryptUtils.rot13(string));
+ }
+ } catch (Exception e) {
+ // nothing
+ }
+ }
+}
|
