aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2012-09-01 17:56:24 +0200
committerBananeweizen <bananeweizen@gmx.de>2012-09-01 17:56:24 +0200
commitea30aeba7778a0a9b847663b21b7bd1e0f24e123 (patch)
tree516c979708fda9fd2938aa7a2d5748370b9a137c
parent126868467cd1911071b0d25fa3943918ddb330cf (diff)
downloadcgeo-ea30aeba7778a0a9b847663b21b7bd1e0f24e123.zip
cgeo-ea30aeba7778a0a9b847663b21b7bd1e0f24e123.tar.gz
cgeo-ea30aeba7778a0a9b847663b21b7bd1e0f24e123.tar.bz2
fix #2013: Template for cache owner
-rw-r--r--main/res/values/strings.xml1
-rw-r--r--main/src/cgeo/geocaching/LogTrackableActivity.java9
-rw-r--r--main/src/cgeo/geocaching/SettingsActivity.java3
-rw-r--r--main/src/cgeo/geocaching/VisitCacheActivity.java12
-rw-r--r--main/src/cgeo/geocaching/cgCache.java3
-rw-r--r--main/src/cgeo/geocaching/utils/LogTemplateProvider.java177
-rw-r--r--tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java6
7 files changed, 137 insertions, 74 deletions
diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml
index 1bd425b..e4599bb 100644
--- a/main/res/values/strings.xml
+++ b/main/res/values/strings.xml
@@ -380,6 +380,7 @@
<string name="init_signature_template_datetime">Date &amp; Time</string>
<string name="init_signature_template_user">User</string>
<string name="init_signature_template_number">Number</string>
+ <string name="init_signature_template_owner">Owner</string>
<string name="init_details">Cache Details</string>
<string name="init_ratingwanted">Load cache rating from GCvote.com</string>
<string name="init_elevationwanted">Load cache elevation data</string>
diff --git a/main/src/cgeo/geocaching/LogTrackableActivity.java b/main/src/cgeo/geocaching/LogTrackableActivity.java
index bcbc4d4..5ae0d01 100644
--- a/main/src/cgeo/geocaching/LogTrackableActivity.java
+++ b/main/src/cgeo/geocaching/LogTrackableActivity.java
@@ -12,6 +12,7 @@ import cgeo.geocaching.ui.DateDialog;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
+import cgeo.geocaching.utils.LogTemplateProvider.LogContext;
import cgeo.geocaching.utils.LogTemplateProvider.LogTemplate;
import org.apache.commons.collections.CollectionUtils;
@@ -51,6 +52,7 @@ public class LogTrackableActivity extends AbstractActivity implements DateDialog
private int attempts = 0;
private CheckBox tweetCheck = null;
private LinearLayout tweetBox = null;
+ private cgTrackable trackable;
private static final int MENU_SIGNATURE = 1;
@@ -132,7 +134,7 @@ public class LogTrackableActivity extends AbstractActivity implements DateDialog
}
}
- final cgTrackable trackable = app.getTrackableByGeocode(geocode);
+ trackable = app.getTrackableByGeocode(geocode);
if (StringUtils.isNotBlank(trackable.getName())) {
setTitle(res.getString(R.string.trackable_touch) + ": " + trackable.getName());
@@ -183,15 +185,16 @@ public class LogTrackableActivity extends AbstractActivity implements DateDialog
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int id = item.getItemId();
+ final LogContext logContext = new LogContext(trackable);
if (id == MENU_SIGNATURE) {
- insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), false), true);
+ insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), logContext), true);
return true;
}
final LogTemplate template = LogTemplateProvider.getTemplate(id);
if (template != null) {
- insertIntoLog(template.getValue(false), true);
+ insertIntoLog(template.getValue(logContext), true);
return true;
}
diff --git a/main/src/cgeo/geocaching/SettingsActivity.java b/main/src/cgeo/geocaching/SettingsActivity.java
index 448407f..ac8167a 100644
--- a/main/src/cgeo/geocaching/SettingsActivity.java
+++ b/main/src/cgeo/geocaching/SettingsActivity.java
@@ -894,8 +894,7 @@ public class SettingsActivity extends AbstractActivity {
final Parameters params = new Parameters("name", nam, "code", cod);
HttpResponse response = Network.getRequest("http://send2.cgeo.org/auth.html", params);
- if (response != null && response.getStatusLine().getStatusCode() == 200)
- {
+ if (response != null && response.getStatusLine().getStatusCode() == 200) {
//response was OK
String[] strings = Network.getResponseData(response).split(",");
try {
diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java
index fd552f5..c6823c8 100644
--- a/main/src/cgeo/geocaching/VisitCacheActivity.java
+++ b/main/src/cgeo/geocaching/VisitCacheActivity.java
@@ -15,6 +15,7 @@ import cgeo.geocaching.ui.DateDialog;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
+import cgeo.geocaching.utils.LogTemplateProvider.LogContext;
import cgeo.geocaching.utils.LogTemplateProvider.LogTemplate;
import org.apache.commons.collections.CollectionUtils;
@@ -143,8 +144,7 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D
inventoryView.addView(inventoryItem);
- if (Settings.isTrackableAutoVisit())
- {
+ if (Settings.isTrackableAutoVisit()) {
tb.action = LogTypeTrackable.VISITED;
tbChanged = true;
}
@@ -330,7 +330,8 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D
final int id = item.getItemId();
if (id == MENU_SIGNATURE) {
- insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), false), true);
+ final LogContext logContext = new LogContext(cache);
+ insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), logContext), true);
return true;
}
@@ -345,7 +346,8 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D
final LogTemplate template = LogTemplateProvider.getTemplate(id);
if (template != null) {
- insertIntoLog(template.getValue(false), true);
+ final LogContext logContext = new LogContext(cache);
+ insertIntoLog(template.getValue(logContext), true);
return true;
}
@@ -481,7 +483,7 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D
} else if (StringUtils.isNotBlank(Settings.getSignature())
&& Settings.isAutoInsertSignature()
&& StringUtils.isBlank(((EditText) findViewById(R.id.log)).getText())) {
- insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), false), false);
+ insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), new LogContext(cache)), false);
}
if (!possibleLogTypes.contains(typeSelected)) {
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java
index cd4e15c..d22999d 100644
--- a/main/src/cgeo/geocaching/cgCache.java
+++ b/main/src/cgeo/geocaching/cgCache.java
@@ -24,6 +24,7 @@ import cgeo.geocaching.network.HtmlImage;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
+import cgeo.geocaching.utils.LogTemplateProvider.LogContext;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -409,7 +410,7 @@ public class cgCache implements ICache, IWaypoint {
public void logOffline(final Activity fromActivity, final LogType logType) {
final boolean mustIncludeSignature = StringUtils.isNotBlank(Settings.getSignature()) && Settings.isAutoInsertSignature();
- final String initial = mustIncludeSignature ? LogTemplateProvider.applyTemplates(Settings.getSignature(), true) : "";
+ final String initial = mustIncludeSignature ? LogTemplateProvider.applyTemplates(Settings.getSignature(), new LogContext(this, true)) : "";
logOffline(fromActivity, initial, Calendar.getInstance(), logType);
}
diff --git a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java
index 60866e0..7cacd9d 100644
--- a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java
+++ b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java
@@ -2,6 +2,8 @@ package cgeo.geocaching.utils;
import cgeo.geocaching.R;
import cgeo.geocaching.Settings;
+import cgeo.geocaching.cgCache;
+import cgeo.geocaching.cgTrackable;
import cgeo.geocaching.connector.gc.GCConstants;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.network.Network;
@@ -9,12 +11,57 @@ import cgeo.geocaching.ui.Formatter;
import org.apache.commons.lang3.StringUtils;
+import java.util.ArrayList;
/**
* provides all the available templates for logging
*
*/
public class LogTemplateProvider {
+
+ /**
+ * Context aware data container for log templates.
+ * <p>
+ * Some log templates need additional information. To provide that information, it can be encapsulated in this log
+ * context.
+ * </p>
+ *
+ */
+ public static class LogContext {
+ private cgCache cache;
+ private cgTrackable trackable;
+ private boolean offline = false;
+
+ public LogContext(final cgCache cache) {
+ this(cache, false);
+ }
+
+ public LogContext(final cgTrackable trackable) {
+ this.trackable = trackable;
+ }
+
+ public LogContext(boolean offline) {
+ this(null, offline);
+ }
+
+ public LogContext(final cgCache cache, boolean offline) {
+ this.cache = cache;
+ this.offline = offline;
+ }
+
+ public cgCache getCache() {
+ return cache;
+ }
+
+ public cgTrackable getTrackable() {
+ return trackable;
+ }
+
+ public boolean isOffline() {
+ return offline;
+ }
+ }
+
public static abstract class LogTemplate {
private final String template;
private final int resourceId;
@@ -24,7 +71,7 @@ public class LogTemplateProvider {
this.resourceId = resourceId;
}
- abstract public String getValue(boolean offline);
+ abstract public String getValue(LogContext context);
public int getResourceId() {
return resourceId;
@@ -38,70 +85,80 @@ public class LogTemplateProvider {
return template;
}
- protected String apply(String input, boolean offline) {
+ protected String apply(String input, LogContext context) {
if (input.contains("[" + template + "]")) {
- return StringUtils.replace(input, "[" + template + "]", getValue(offline));
+ return StringUtils.replace(input, "[" + template + "]", getValue(context));
}
return input;
}
}
- private static LogTemplate[] templates;
-
- public static LogTemplate[] getTemplates() {
- if (templates == null) {
- templates = new LogTemplate[] {
- new LogTemplate("DATE", R.string.init_signature_template_date) {
-
- @Override
- public String getValue(final boolean offline) {
- return Formatter.formatFullDate(System.currentTimeMillis());
- }
- },
- new LogTemplate("TIME", R.string.init_signature_template_time) {
-
- @Override
- public String getValue(final boolean offline) {
- return Formatter.formatTime(System.currentTimeMillis());
- }
- },
- new LogTemplate("DATETIME", R.string.init_signature_template_datetime) {
-
- @Override
- public String getValue(final boolean offline) {
- final long currentTime = System.currentTimeMillis();
- return Formatter.formatFullDate(currentTime) + " " + Formatter.formatTime(currentTime);
- }
- },
- new LogTemplate("USER", R.string.init_signature_template_user) {
-
- @Override
- public String getValue(final boolean offline) {
- return Settings.getUsername();
- }
- },
- new LogTemplate("NUMBER", R.string.init_signature_template_number) {
-
- @Override
- public String getValue(final boolean offline) {
- int current = Login.getActualCachesFound();
- if (current == 0) {
- if (offline) {
- return "";
- }
- final String page = Network.getResponseData(Network.getRequest("http://www.geocaching.com/email/"));
- current = parseFindCount(page);
- }
-
- String findCount = "";
- if (current >= 0) {
- findCount = String.valueOf(current + 1);
- }
- return findCount;
- }
+ public static ArrayList<LogTemplate> getTemplates() {
+ ArrayList<LogTemplate> templates = new ArrayList<LogTemplateProvider.LogTemplate>();
+ templates.add(new LogTemplate("DATE", R.string.init_signature_template_date) {
+
+ @Override
+ public String getValue(final LogContext context) {
+ return Formatter.formatFullDate(System.currentTimeMillis());
+ }
+ });
+ templates.add(new LogTemplate("TIME", R.string.init_signature_template_time) {
+
+ @Override
+ public String getValue(final LogContext context) {
+ return Formatter.formatTime(System.currentTimeMillis());
+ }
+ });
+ templates.add(new LogTemplate("DATETIME", R.string.init_signature_template_datetime) {
+
+ @Override
+ public String getValue(final LogContext context) {
+ final long currentTime = System.currentTimeMillis();
+ return Formatter.formatFullDate(currentTime) + " " + Formatter.formatTime(currentTime);
+ }
+ });
+ templates.add(new LogTemplate("USER", R.string.init_signature_template_user) {
+
+ @Override
+ public String getValue(final LogContext context) {
+ return Settings.getUsername();
+ }
+ });
+ templates.add(new LogTemplate("NUMBER", R.string.init_signature_template_number) {
+
+ @Override
+ public String getValue(final LogContext context) {
+ int current = Login.getActualCachesFound();
+ if (current == 0) {
+ if (context.isOffline()) {
+ return "";
}
- };
- }
+ final String page = Network.getResponseData(Network.getRequest("http://www.geocaching.com/email/"));
+ current = parseFindCount(page);
+ }
+
+ String findCount = "";
+ if (current >= 0) {
+ findCount = String.valueOf(current + 1);
+ }
+ return findCount;
+ }
+ });
+ templates.add(new LogTemplate("OWNER", R.string.init_signature_template_owner) {
+
+ @Override
+ public String getValue(final LogContext context) {
+ cgTrackable trackable = context.getTrackable();
+ if (trackable != null) {
+ return trackable.getOwner();
+ }
+ cgCache cache = context.getCache();
+ if (cache != null) {
+ return cache.getOwnerDisplayName();
+ }
+ return "";
+ }
+ });
return templates;
}
@@ -114,13 +171,13 @@ public class LogTemplateProvider {
return null;
}
- public static String applyTemplates(String signature, boolean offline) {
+ public static String applyTemplates(String signature, LogContext context) {
if (signature == null) {
return "";
}
String result = signature;
for (LogTemplate template : getTemplates()) {
- result = template.apply(result, offline);
+ result = template.apply(result, context);
}
return result;
}
diff --git a/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java b/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java
index ec54008..5911218 100644
--- a/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java
+++ b/tests/src/cgeo/geocaching/utils/LogTemplateProviderTest.java
@@ -1,6 +1,6 @@
package cgeo.geocaching.utils;
-import cgeo.geocaching.utils.LogTemplateProvider;
+import cgeo.geocaching.utils.LogTemplateProvider.LogContext;
import java.util.Calendar;
@@ -10,11 +10,11 @@ public class LogTemplateProviderTest extends TestCase {
public static void testApplyTemplates() {
final String noTemplates = " no templates ";
- assertEquals(noTemplates, LogTemplateProvider.applyTemplates(noTemplates, true));
+ assertEquals(noTemplates, LogTemplateProvider.applyTemplates(noTemplates, new LogContext(true)));
// This test can occasionally fail if the current year changes right after the next line.
final String currentYear = Integer.toString(Calendar.YEAR);
- assertTrue(LogTemplateProvider.applyTemplates("[DATE]", true).contains(currentYear));
+ assertTrue(LogTemplateProvider.applyTemplates("[DATE]", new LogContext(true)).contains(currentYear));
}
}