diff options
Diffstat (limited to 'main/src/cgeo/geocaching/Geocache.java')
| -rw-r--r-- | main/src/cgeo/geocaching/Geocache.java | 98 |
1 files changed, 61 insertions, 37 deletions
diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index a224e4e..bd1d8bf 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -25,6 +25,7 @@ import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.CancellableHandler; +import cgeo.geocaching.utils.DateUtils; import cgeo.geocaching.utils.ImageUtils; import cgeo.geocaching.utils.LazyInitializedList; import cgeo.geocaching.utils.Log; @@ -34,12 +35,16 @@ import cgeo.geocaching.utils.MatcherWrapper; import cgeo.geocaching.utils.UncertainProperty; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.Predicate; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; + import rx.Scheduler; import rx.Scheduler.Inner; import rx.Subscription; @@ -50,11 +55,13 @@ import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Environment; import android.os.Handler; import android.os.Message; import android.text.Html; import android.text.Html.ImageGetter; +import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; @@ -73,6 +80,10 @@ import java.util.regex.Pattern; /** * Internal c:geo representation of a "cache" */ +/** + * @author kep9fe + * + */ public class Geocache implements ICache, IWaypoint { private static final int OWN_WP_PREFIX_OFFSET = 17; @@ -138,12 +149,7 @@ public class Geocache implements ICache, IWaypoint { } }; private List<Image> spoilers = null; - private final LazyInitializedList<LogEntry> logs = new LazyInitializedList<LogEntry>() { - @Override - public List<LogEntry> call() { - return DataStore.loadLogs(geocode); - } - }; + private List<Trackable> inventory = null; private Map<LogType, Integer> logCounts = new HashMap<LogType, Integer>(); private boolean userModifiedCoords = false; @@ -177,11 +183,10 @@ public class Geocache implements ICache, IWaypoint { * * @param gpxParser */ - public Geocache(@SuppressWarnings("unused") GPXParser gpxParser) { + public Geocache(GPXParser gpxParser) { setReliableLatLon(true); setAttributes(Collections.<String> emptyList()); setWaypoints(Collections.<Waypoint> emptyList(), false); - setLogs(Collections.<LogEntry> emptyList()); } public void setChangeNotificationHandler(Handler newNotificationHandler) { @@ -349,12 +354,6 @@ public class Geocache implements ICache, IWaypoint { inventory = other.inventory; inventoryItems = other.inventoryItems; } - if (logs.isEmpty()) { // keep last known logs if none - logs.clear(); - if (other.logs != null) { - logs.addAll(other.logs); - } - } if (logCounts.isEmpty()) { logCounts = other.logCounts; } @@ -421,7 +420,6 @@ public class Geocache implements ICache, IWaypoint { attributes == other.attributes && waypoints == other.waypoints && spoilers == other.spoilers && - logs == other.logs && inventory == other.inventory && logCounts == other.logCounts && ObjectUtils.equals(logOffline, other.logOffline) && @@ -776,10 +774,7 @@ public class Geocache implements ICache, IWaypoint { @Override public List<Image> getSpoilers() { - if (spoilers == null) { - return Collections.emptyList(); - } - return Collections.unmodifiableList(spoilers); + return ListUtils.unmodifiableList(ListUtils.emptyIfNull(spoilers)); } @Override @@ -1005,21 +1000,24 @@ public class Geocache implements ICache, IWaypoint { } /** - * @return never <code>null</code> + * The list of logs is immutable, because it is directly fetched from the database on demand, and not stored at this + * object. If you want to modify logs, you have to load all logs of the cache, create a new list from the existing + * list and store that new list in the database. + * + * @return immutable list of logs */ + @NonNull public List<LogEntry> getLogs() { - // It is important to return the underlying list here and not the lazily initialized one, - // because database manipulation may erase the existing logs before methods are called - // on the previous logs, when updating the saved logs for example. - return logs.getUnderlyingList(); + return DataStore.loadLogs(geocode); } /** - * @return only the logs of friends, never <code>null</code> + * @return only the logs of friends */ + @NonNull public List<LogEntry> getFriendsLogs() { final ArrayList<LogEntry> friendLogs = new ArrayList<LogEntry>(); - for (final LogEntry log : logs) { + for (final LogEntry log : getLogs()) { if (log.friend) { friendLogs.add(log); } @@ -1027,17 +1025,6 @@ public class Geocache implements ICache, IWaypoint { return Collections.unmodifiableList(friendLogs); } - /** - * @param logs - * the log entries - */ - public void setLogs(List<LogEntry> logs) { - this.logs.clear(); - if (logs != null) { - this.logs.addAll(logs); - } - } - public boolean isLogOffline() { return BooleanUtils.isTrue(logOffline); } @@ -1735,6 +1722,7 @@ public class Geocache implements ICache, IWaypoint { public Collection<Image> getImages() { final LinkedList<Image> result = new LinkedList<Image>(); result.addAll(getSpoilers()); + addLocalSpoilersTo(result); for (final LogEntry log : getLogs()) { result.addAll(log.getLogImages()); } @@ -1746,6 +1734,23 @@ public class Geocache implements ICache, IWaypoint { return result; } + // add spoilers stored locally in /sdcard/GeocacheSpoilers + private void addLocalSpoilersTo(final List<Image> spoilers) { + if (StringUtils.length(geocode) >= 2) { + final String suffix = StringUtils.right(geocode, 2); + final File baseDir = new File(Environment.getExternalStorageDirectory().toString(), "GeocacheSpoilers"); + final File lastCharDir = new File(baseDir, suffix.substring(1)); + final File secondToLastCharDir = new File(lastCharDir, suffix.substring(0, 1)); + final File finalDir = new File(secondToLastCharDir, getGeocode()); + final File[] files = finalDir.listFiles(); + if (files != null) { + for (final File image : files) { + spoilers.add(new Image("file://" + image.getAbsolutePath(), image.getName())); + } + } + } + } + public void setDetailedUpdatedNow() { final long now = System.currentTimeMillis(); setUpdated(now); @@ -1806,4 +1811,23 @@ public class Geocache implements ICache, IWaypoint { return (getType().applyDistanceRule() || hasUserModifiedCoords()) && getConnector() == GCConnector.getInstance(); } + public LogType getDefaultLogType() { + if (isEventCache()) { + final Date eventDate = getHiddenDate(); + boolean expired = DateUtils.isPastEvent(this); + + if (hasOwnLog(LogType.WILL_ATTEND) || expired || (eventDate != null && DateUtils.daysSince(eventDate.getTime()) == 0)) { + return hasOwnLog(LogType.ATTENDED) ? LogType.NOTE : LogType.ATTENDED; + } + return LogType.WILL_ATTEND; + } + if (isFound()) { + return LogType.NOTE; + } + if (getType() == CacheType.WEBCAM) { + return LogType.WEBCAM_PHOTO_TAKEN; + } + return LogType.FOUND_IT; + } + } |
