diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2015-02-08 10:39:46 +0100 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2015-02-08 10:39:46 +0100 |
| commit | 6641a9b054250769416d1dc2136b135f1283a10a (patch) | |
| tree | 3450dd013aeb3e2e50df9dd43da67acbe80b3c91 | |
| parent | ce3ae0201b919e9454b605cfa20ceaf4b1789a5f (diff) | |
| download | cgeo-6641a9b054250769416d1dc2136b135f1283a10a.zip cgeo-6641a9b054250769416d1dc2136b135f1283a10a.tar.gz cgeo-6641a9b054250769416d1dc2136b135f1283a10a.tar.bz2 | |
fix #4661: disallow future logs
| -rw-r--r-- | main/res/values/strings.xml | 1 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/LogCacheActivity.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/DateUtils.java | 17 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/DateUtilsTest.java | 12 |
4 files changed, 32 insertions, 2 deletions
diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml index 9c250c8..e1db81b 100644 --- a/main/res/values/strings.xml +++ b/main/res/values/strings.xml @@ -91,6 +91,7 @@ <string name="log_saving_and_uploading">Sending log and uploading image…</string> <string name="log_clear">Clear</string> <string name="log_post_not_possible">Loading Log Page…</string> + <string name="log_date_future_not_allowed">Future log dates are not allowed.</string> <string name="log_add">Add</string> <string name="log_repeat">Repeat last log</string> <string name="log_no_rating">No rating</string> diff --git a/main/src/cgeo/geocaching/LogCacheActivity.java b/main/src/cgeo/geocaching/LogCacheActivity.java index 1d31e27..c60d2e4 100644 --- a/main/src/cgeo/geocaching/LogCacheActivity.java +++ b/main/src/cgeo/geocaching/LogCacheActivity.java @@ -618,6 +618,10 @@ public class LogCacheActivity extends AbstractLoggingActivity implements DateDia Dialogs.message(this, R.string.log_post_not_possible); return; } + if (DateUtils.isFuture(date)) { + Dialogs.message(this, R.string.log_date_future_not_allowed); + return; + } if (typeSelected.mustConfirmLog()) { Dialogs.confirm(this, R.string.confirm_log_title, res.getString(R.string.confirm_log_message, typeSelected.getL10n()), new OnClickListener() { diff --git a/main/src/cgeo/geocaching/utils/DateUtils.java b/main/src/cgeo/geocaching/utils/DateUtils.java index 9aa4222..8278be7 100644 --- a/main/src/cgeo/geocaching/utils/DateUtils.java +++ b/main/src/cgeo/geocaching/utils/DateUtils.java @@ -11,7 +11,7 @@ public final class DateUtils { // utility class } - public static int daysSince(long date) { + public static int daysSince(final long date) { final Calendar logDate = Calendar.getInstance(); logDate.setTimeInMillis(date); logDate.set(Calendar.SECOND, 0); @@ -24,6 +24,10 @@ public final class DateUtils { return (int) Math.round((today.getTimeInMillis() - logDate.getTimeInMillis()) / 86400000d); } + public static int daysSince(final Calendar date) { + return daysSince(date.getTimeInMillis()); + } + public static boolean isPastEvent(final Geocache cache) { if (!cache.isEventCache()) { return false; @@ -32,4 +36,15 @@ public final class DateUtils { return hiddenDate != null && DateUtils.daysSince(hiddenDate.getTime()) > 0; } + /** + * Return whether the given date is *more* than 1 day away. We allow 1 day to be "present time" to compensate for + * potential timezone issues. + * + * @param date + * @return + */ + public static boolean isFuture(final Calendar date) { + return daysSince(date) < -1; + } + } diff --git a/tests/src/cgeo/geocaching/utils/DateUtilsTest.java b/tests/src/cgeo/geocaching/utils/DateUtilsTest.java index d0a2b4a..44442f6 100644 --- a/tests/src/cgeo/geocaching/utils/DateUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/DateUtilsTest.java @@ -32,7 +32,7 @@ public class DateUtilsTest extends TestCase { assertPastEvent(start, true); } - private static void assertPastEvent(final Calendar start, boolean expectedPast) { + private static void assertPastEvent(final Calendar start, final boolean expectedPast) { final Geocache cache = new Geocache(); cache.setType(CacheType.EVENT); @@ -40,4 +40,14 @@ public class DateUtilsTest extends TestCase { assertThat(DateUtils.isPastEvent(cache)).isEqualTo(expectedPast); } + public static void testIsFuture() { + final Calendar date = Calendar.getInstance(); + assertThat(DateUtils.isFuture(date)).isFalse(); + + date.add(Calendar.DAY_OF_MONTH, 1); + assertThat(DateUtils.isFuture(date)).isFalse(); + + date.add(Calendar.DAY_OF_MONTH, 1); + assertThat(DateUtils.isFuture(date)).isTrue(); + } } |
