diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cgeo/geocaching/activity/ActivityMixin.java | 3 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgCache.java | 37 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeodetail.java | 44 | ||||
| -rw-r--r-- | src/cgeo/geocaching/connector/AbstractConnector.java | 26 | ||||
| -rw-r--r-- | src/cgeo/geocaching/connector/ConnectorFactory.java | 31 | ||||
| -rw-r--r-- | src/cgeo/geocaching/connector/GCConnector.java | 31 | ||||
| -rw-r--r-- | src/cgeo/geocaching/connector/IConnector.java | 11 | ||||
| -rw-r--r-- | src/cgeo/geocaching/files/GPXParser.java | 63 |
8 files changed, 177 insertions, 69 deletions
diff --git a/src/cgeo/geocaching/activity/ActivityMixin.java b/src/cgeo/geocaching/activity/ActivityMixin.java index 27af9d4..68a4403 100644 --- a/src/cgeo/geocaching/activity/ActivityMixin.java +++ b/src/cgeo/geocaching/activity/ActivityMixin.java @@ -120,6 +120,9 @@ public final class ActivityMixin { } protected static void addVisitMenu(IAbstractActivity activity, Menu menu, cgCache cache) { + if (!cache.supportsLogging()) { + return; + } cgSettings settings = activity.getSettings(); Resources res = ((Activity)activity).getResources(); if (settings.isLogin()) { diff --git a/src/cgeo/geocaching/cgCache.java b/src/cgeo/geocaching/cgCache.java index de0dace..8de5a5d 100644 --- a/src/cgeo/geocaching/cgCache.java +++ b/src/cgeo/geocaching/cgCache.java @@ -14,6 +14,8 @@ import android.net.Uri; import android.text.Spannable; import android.util.Log; import cgeo.geocaching.activity.IAbstractActivity; +import cgeo.geocaching.connector.ConnectorFactory; +import cgeo.geocaching.connector.IConnector; public class cgCache { @@ -290,19 +292,19 @@ public class cgCache { return true; } - + public boolean logOffline(final IAbstractActivity fromActivity, final int logType) { logOffline(fromActivity, "", Calendar.getInstance(), logType); return true; } - + void logOffline(final IAbstractActivity fromActivity, final String log, Calendar date, final int logType) { if (logType <= 0) { return; } cgeoapplication app = (cgeoapplication)((Activity)fromActivity).getApplication(); final boolean status = app.saveLogOffline(geocode, date.getTime(), logType, log); - + Resources res = ((Activity)fromActivity).getResources(); if (status) { fromActivity.showToast(res.getString(R.string.info_log_saved)); @@ -311,7 +313,7 @@ public class cgCache { fromActivity.showToast(res.getString(R.string.err_log_post_failed)); } } - + public ArrayList<Integer> getPossibleLogTypes(cgSettings settings) { boolean isOwner = owner != null && owner.equalsIgnoreCase(settings.getUsername()); ArrayList<Integer> types = new ArrayList<Integer>(); @@ -347,14 +349,31 @@ public class cgCache { } public void openInBrowser(Activity fromActivity) { - fromActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getCacheUrl() + geocode))); + fromActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getCacheUrl()))); } private String getCacheUrl() { - if (geocode.startsWith("OC")) { // TODO refactor method into connectors, once available - return "http://www.opencaching.de/viewcache.php?wp="; - } - return "http://www.geocaching.com/seek/cache_details.aspx?wp="; + return getConnector().getCacheUrl(this); + } + + private IConnector getConnector() { + return ConnectorFactory.getConnector(this); + } + + public boolean canOpenInBrowser() { + return getCacheUrl() != null; + } + + public boolean supportsRefresh() { + return getConnector().supportsRefreshCache(this); + } + + public boolean supportsWatchList() { + return getConnector().supportsWatchList(); + } + + public boolean supportsLogging() { + return getConnector().supportsLogging(); } } diff --git a/src/cgeo/geocaching/cgeodetail.java b/src/cgeo/geocaching/cgeodetail.java index 0681621..7f5e7e5 100644 --- a/src/cgeo/geocaching/cgeodetail.java +++ b/src/cgeo/geocaching/cgeodetail.java @@ -90,7 +90,7 @@ public class cgeodetail extends AbstractActivity { private ViewGroup attributeDescriptionsLayout; // layout for attribute descriptions private boolean attributesShowAsIcons = true; // default: show icons private int attributeBoxMaxWidth; - + private Handler storeCacheHandler = new Handler() { @Override public void handleMessage(Message msg) { @@ -240,6 +240,12 @@ public class cgeodetail extends AbstractActivity { * shows/hides buttons, sets text in watchlist box */ private void updateWatchlistBox() { + LinearLayout layout = (LinearLayout)findViewById(R.id.watchlist_box); + boolean supportsWatchList = cache.supportsWatchList(); + layout.setVisibility(supportsWatchList ? View.VISIBLE : View.GONE); + if (!supportsWatchList) { + return; + } Button buttonAdd = (Button) findViewById(R.id.add_to_watchlist); Button buttonRemove = (Button) findViewById(R.id.remove_from_watchlist); TextView text = (TextView) findViewById(R.id.watchlist_text); @@ -489,7 +495,9 @@ public class cgeodetail extends AbstractActivity { menu.add(0, 10, 0, res.getString(R.string.cache_menu_around)).setIcon(android.R.drawable.ic_menu_rotate); // caches around } - menu.add(1, 7, 0, res.getString(R.string.cache_menu_browser)).setIcon(android.R.drawable.ic_menu_info_details); // browser + if (cache != null && cache.canOpenInBrowser()) { + menu.add(1, 7, 0, res.getString(R.string.cache_menu_browser)).setIcon(android.R.drawable.ic_menu_info_details); // browser + } menu.add(0, 12, 0, res.getString(R.string.cache_menu_share)).setIcon(android.R.drawable.ic_menu_share); // share cache return true; @@ -532,7 +540,7 @@ public class cgeodetail extends AbstractActivity { if (GeneralAppsFactory.onMenuItemSelected(item, this, cache)) { return true; } - + int logType = menuItem - MENU_LOG_VISIT_OFFLINE; cache.logOffline(this, logType); return true; @@ -608,7 +616,7 @@ public class cgeodetail extends AbstractActivity { { geocode = cache.geocode; } - + if (null == guid && cache.guid.length() > 0) { guid = cache.guid; @@ -829,16 +837,16 @@ public class cgeodetail extends AbstractActivity { ViewParent child = attribBox; do { if (child instanceof View) - attributeBoxMaxWidth = attributeBoxMaxWidth - ((View) child).getPaddingLeft() + attributeBoxMaxWidth = attributeBoxMaxWidth - ((View) child).getPaddingLeft() - ((View) child).getPaddingRight(); child = child.getParent(); } while (child != null); - + // delete views holding description / icons attributeDescriptionsLayout = null; attributeIconsLayout = null; - attribBox.setOnClickListener(new View.OnClickListener() { + attribBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // toggle between attribute icons and descriptions @@ -901,9 +909,6 @@ public class cgeodetail extends AbstractActivity { } offlineText.setText(res.getString(R.string.cache_offline_stored) + "\n" + ago); - - offlineRefresh.setVisibility(View.VISIBLE); - offlineRefresh.setClickable(true); offlineRefresh.setOnClickListener(new storeCache()); offlineStore.setText(res.getString(R.string.cache_offline_drop)); @@ -911,15 +916,14 @@ public class cgeodetail extends AbstractActivity { offlineStore.setOnClickListener(new dropCache()); } else { offlineText.setText(res.getString(R.string.cache_offline_not_ready)); - - offlineRefresh.setVisibility(View.VISIBLE); - offlineRefresh.setClickable(true); offlineRefresh.setOnClickListener(new refreshCache()); offlineStore.setText(res.getString(R.string.cache_offline_store)); offlineStore.setClickable(true); offlineStore.setOnClickListener(new storeCache()); } + offlineRefresh.setVisibility(cache.supportsRefresh() ? View.VISIBLE : View.GONE); + offlineRefresh.setClickable(true); // cache personal note if (cache.personalNote != null && cache.personalNote.length() > 0) { @@ -1949,7 +1953,7 @@ public class cgeodetail extends AbstractActivity { cgeonavigate.coordinates = getCoordinates(); startActivity(navigateIntent); } - + /** * lazy-creates the layout holding the icons of the chaches attributes * and makes it visible @@ -1973,7 +1977,7 @@ public class cgeodetail extends AbstractActivity { attribBox.addView(attributeDescriptionsLayout); attributesShowAsIcons = false; } - + /** * toggle attribute descriptions and icons */ @@ -1991,7 +1995,7 @@ public class cgeodetail extends AbstractActivity { LinearLayout attributeRow = newAttributeIconsRow(); rows.addView(attributeRow); - + for(String attributeName : cache.attributes) { boolean strikethru = attributeName.endsWith("_no"); // cut off _yes / _no @@ -2028,10 +2032,10 @@ public class cgeodetail extends AbstractActivity { } attributeRow.addView(fl); } - + return rows; } - + private LinearLayout newAttributeIconsRow() { LinearLayout rowLayout = new LinearLayout(this); rowLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, @@ -2039,7 +2043,7 @@ public class cgeodetail extends AbstractActivity { rowLayout.setOrientation(LinearLayout.HORIZONTAL); return rowLayout; } - + private ViewGroup createAttributeDescriptionsLayout() { final LinearLayout descriptions = (LinearLayout) inflater.inflate(R.layout.attribute_descriptions, null); TextView attribView = (TextView) descriptions.getChildAt(0); @@ -2064,7 +2068,7 @@ public class cgeodetail extends AbstractActivity { } attribView.setText(buffer); - + return descriptions; } } diff --git a/src/cgeo/geocaching/connector/AbstractConnector.java b/src/cgeo/geocaching/connector/AbstractConnector.java new file mode 100644 index 0000000..24d13f8 --- /dev/null +++ b/src/cgeo/geocaching/connector/AbstractConnector.java @@ -0,0 +1,26 @@ +package cgeo.geocaching.connector; + +import cgeo.geocaching.cgCache; + +public abstract class AbstractConnector implements IConnector { + + @Override + public boolean canHandle(String geocode) { + return false; + } + + @Override + public boolean supportsRefreshCache(cgCache cache) { + return false; + } + + @Override + public boolean supportsWatchList() { + return false; + } + + @Override + public boolean supportsLogging() { + return false; + } +} diff --git a/src/cgeo/geocaching/connector/ConnectorFactory.java b/src/cgeo/geocaching/connector/ConnectorFactory.java new file mode 100644 index 0000000..9d8f955 --- /dev/null +++ b/src/cgeo/geocaching/connector/ConnectorFactory.java @@ -0,0 +1,31 @@ +package cgeo.geocaching.connector; + +import cgeo.geocaching.cgCache; + +public final class ConnectorFactory { + private static final GCConnector GC_CONNECTOR = new GCConnector(); + private static final IConnector[] connectors = new IConnector[] {GC_CONNECTOR, new OCConnector()}; + + public static IConnector[] getConnectors() { + return connectors; + } + + public static boolean canHandle(final String geocode) { + for (IConnector connector : connectors) { + if (connector.canHandle(geocode)) { + return true; + } + } + return false; + } + + public static IConnector getConnector(cgCache cache) { + for (IConnector connector : connectors) { + if (connector.canHandle(cache.geocode)) { + return connector; + } + } + // in case of errors, assume GC as default + return GC_CONNECTOR; + } +} diff --git a/src/cgeo/geocaching/connector/GCConnector.java b/src/cgeo/geocaching/connector/GCConnector.java new file mode 100644 index 0000000..5d5de6b --- /dev/null +++ b/src/cgeo/geocaching/connector/GCConnector.java @@ -0,0 +1,31 @@ +package cgeo.geocaching.connector; + +import cgeo.geocaching.cgCache; + +public class GCConnector extends AbstractConnector implements IConnector { + + @Override + public boolean canHandle(String geocode) { + return geocode != null && geocode.toUpperCase().startsWith("GC"); + } + + @Override + public boolean supportsRefreshCache(cgCache cache) { + return true; + } + + @Override + public String getCacheUrl(cgCache cache) { + return "http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.geocode; + } + + @Override + public boolean supportsWatchList() { + return true; + } + + @Override + public boolean supportsLogging() { + return true; + } +} diff --git a/src/cgeo/geocaching/connector/IConnector.java b/src/cgeo/geocaching/connector/IConnector.java new file mode 100644 index 0000000..6366b5c --- /dev/null +++ b/src/cgeo/geocaching/connector/IConnector.java @@ -0,0 +1,11 @@ +package cgeo.geocaching.connector; + +import cgeo.geocaching.cgCache; + +public interface IConnector { + public boolean canHandle(final String geocode); + public boolean supportsRefreshCache(final cgCache cache); + public String getCacheUrl(final cgCache cache); + public boolean supportsWatchList(); + public boolean supportsLogging(); +} diff --git a/src/cgeo/geocaching/files/GPXParser.java b/src/cgeo/geocaching/files/GPXParser.java index 255bed0..cc864ce 100644 --- a/src/cgeo/geocaching/files/GPXParser.java +++ b/src/cgeo/geocaching/files/GPXParser.java @@ -27,6 +27,7 @@ import cgeo.geocaching.cgSearch; import cgeo.geocaching.cgSettings; import cgeo.geocaching.cgTrackable; import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.connector.ConnectorFactory; public abstract class GPXParser extends FileParser { @@ -43,7 +44,7 @@ public abstract class GPXParser extends FileParser { private String sym = null; protected String namespace = null; private ArrayList<String> nsGCList = new ArrayList<String>(); - private static final Pattern patternGeocode = Pattern.compile("([A-Z]C[0-9A-Z]+)", Pattern.CASE_INSENSITIVE); + private static final Pattern patternGeocode = Pattern.compile("([A-Z]{2}[0-9A-Z]+)", Pattern.CASE_INSENSITIVE); private String name = null; private String cmt = null; private String desc = null; @@ -94,42 +95,9 @@ public abstract class GPXParser extends FileParser { public void end() { if (cache.geocode == null || cache.geocode.length() == 0) { // try to find geocode somewhere else - String geocode = null; - Matcher matcherGeocode = null; - - if (name != null && geocode == null) { - matcherGeocode = patternGeocode.matcher(name); - while (matcherGeocode.find()) { - if (matcherGeocode.groupCount() > 0) { - geocode = matcherGeocode.group(1); - } - } - } - - if (desc != null && geocode == null) { - matcherGeocode = patternGeocode.matcher(desc); - while (matcherGeocode.find()) { - if (matcherGeocode.groupCount() > 0) { - geocode = matcherGeocode.group(1); - } - } - } - - if (cmt != null && geocode == null) { - matcherGeocode = patternGeocode.matcher(cmt); - while (matcherGeocode.find()) { - if (matcherGeocode.groupCount() > 0) { - geocode = matcherGeocode.group(1); - } - } - } - - if (geocode != null && geocode.length() > 0) { - cache.geocode = geocode; - } - - geocode = null; - matcherGeocode = null; + findGeoCode(name); + findGeoCode(desc); + findGeoCode(cmt); } if (cache.geocode != null && cache.geocode.length() > 0 @@ -179,9 +147,9 @@ public abstract class GPXParser extends FileParser { final String content = Html.fromHtml(body).toString().trim(); cache.name = content; - if (cache.name.length() > 2 && cache.name.substring(0, 2).equalsIgnoreCase("GC")) { - cache.geocode = cache.name.toUpperCase(); - } + + findGeoCode(cache.name); + findGeoCode(cache.description); } }); @@ -565,6 +533,21 @@ public abstract class GPXParser extends FileParser { } } + private void findGeoCode(final String input) { + if (input == null || (cache.geocode != null && cache.geocode.length() != 0)) { + return; + } + Matcher matcherGeocode = patternGeocode.matcher(input); + if (matcherGeocode.find()) { + if (matcherGeocode.groupCount() > 0) { + String geocode = matcherGeocode.group(1); + if (ConnectorFactory.canHandle(geocode)) { + cache.geocode = geocode; + } + } + } + } + public static Long parseGPX(cgeoapplication app, File file, int listId, Handler handler) { cgSearch search = new cgSearch(); long searchId = 0l; |
