diff options
Diffstat (limited to 'main/src')
| -rw-r--r-- | main/src/cgeo/geocaching/CacheDetailActivity.java | 15 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ICache.java | 7 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/VisitCacheActivity.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 51 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 61 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/export/GpxExport.java | 7 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXParser.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/LazyInitializedList.java | 33 |
9 files changed, 94 insertions, 91 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index d5d028e..6fef4c4 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -708,10 +708,10 @@ public class CacheDetailActivity extends AbstractActivity { pageOrder.add(Page.DETAILS); final int detailsIndex = pageOrder.size() - 1; pageOrder.add(Page.DESCRIPTION); - if (CollectionUtils.isNotEmpty(cache.getLogs(true))) { + if (cache.getLogs().isNotEmpty()) { pageOrder.add(Page.LOGS); } - if (CollectionUtils.isNotEmpty(cache.getLogs(false))) { + if (CollectionUtils.isNotEmpty(cache.getFriendsLogs())) { pageOrder.add(Page.LOGSFRIENDS); } if (CollectionUtils.isNotEmpty(cache.getInventory())) { @@ -1271,9 +1271,7 @@ public class CacheDetailActivity extends AbstractActivity { final TextView attribView = (TextView) descriptions.getChildAt(0); final StringBuilder buffer = new StringBuilder(); - final List<String> attributes = cache.getAttributes(); - - for (String attributeName : attributes) { + for (String attributeName : cache.getAttributes()) { final boolean enabled = CacheAttribute.isEnabled(attributeName); // search for a translation of the attribute CacheAttribute attrib = CacheAttribute.getByGcRawName(CacheAttribute.trimAttributeName(attributeName)); @@ -1450,7 +1448,7 @@ public class CacheDetailActivity extends AbstractActivity { } // cache attributes - if (cache.hasAttributes()) { + if (cache.getAttributes().isNotEmpty()) { new AttributeViewBuilder().fillView((LinearLayout) view.findViewById(R.id.attributes_innerbox)); view.findViewById(R.id.attributes_box).setVisibility(View.VISIBLE); } @@ -2094,7 +2092,7 @@ public class CacheDetailActivity extends AbstractActivity { /* * (non-Javadoc) - * + * * @see android.os.AsyncTask#onProgressUpdate(Progress[]) */ @Override @@ -2204,7 +2202,8 @@ public class CacheDetailActivity extends AbstractActivity { } } - view.setAdapter(new ArrayAdapter<LogEntry>(CacheDetailActivity.this, R.layout.cacheview_logs_item, cache.getLogs(allLogs)) { + final List<LogEntry> logs = allLogs ? cache.getLogs().asList() : cache.getFriendsLogs(); + view.setAdapter(new ArrayAdapter<LogEntry>(CacheDetailActivity.this, R.layout.cacheview_logs_item, logs) { final UserActionsClickListener userActionsClickListener = new UserActionsClickListener(); final DecryptTextClickListener decryptTextClickListener = new DecryptTextClickListener(); diff --git a/main/src/cgeo/geocaching/ICache.java b/main/src/cgeo/geocaching/ICache.java index d76bacc..8f8baf9 100644 --- a/main/src/cgeo/geocaching/ICache.java +++ b/main/src/cgeo/geocaching/ICache.java @@ -4,6 +4,7 @@ package cgeo.geocaching; import cgeo.geocaching.enumerations.LogType; +import cgeo.geocaching.utils.LazyInitializedList; import java.util.Date; import java.util.List; @@ -102,11 +103,11 @@ public interface ICache extends IBasicCache { public Date getHiddenDate(); /** - * immutable list of attributes, never <code>null</code> - * + * null safe list of attributes + * * @return the list of attributes for this cache */ - public List<String> getAttributes(); + public LazyInitializedList<String> getAttributes(); /** * @return the list of trackables in this cache diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java index 10fe4b0..443ef3a 100644 --- a/main/src/cgeo/geocaching/VisitCacheActivity.java +++ b/main/src/cgeo/geocaching/VisitCacheActivity.java @@ -680,7 +680,7 @@ public class VisitCacheActivity extends AbstractLoggingActivity implements DateD if (status == StatusCode.NO_ERROR) { final LogEntry logNow = new LogEntry(date, typeSelected, log); - cache.prependLog(logNow); + cache.getLogs().prepend(logNow); if (typeSelected == LogType.FOUND_IT) { cache.setFound(true); diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index b38992f..bb167bd 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -270,13 +270,13 @@ public class cgCache implements ICache, IWaypoint { myVote = other.myVote; } if (attributes.isEmpty()) { - attributes.set(other.attributes.get()); + attributes.set(other.attributes); } if (waypoints.isEmpty()) { - waypoints.set(other.waypoints.get()); + waypoints.set(other.waypoints); } else { - ArrayList<cgWaypoint> newPoints = new ArrayList<cgWaypoint>(waypoints.get()); + ArrayList<cgWaypoint> newPoints = new ArrayList<cgWaypoint>(waypoints.asList()); cgWaypoint.mergeWayPoints(newPoints, other.getWaypoints(), false); waypoints.set(newPoints); } @@ -292,7 +292,7 @@ public class cgCache implements ICache, IWaypoint { inventoryItems = other.inventoryItems; } if (logs.isEmpty()) { // keep last known logs if none - logs.set(other.logs.get()); + logs.set(other.logs); } if (logCounts.size() == 0) { logCounts = other.logCounts; @@ -691,8 +691,8 @@ public class cgCache implements ICache, IWaypoint { } @Override - public List<String> getAttributes() { - return Collections.unmodifiableList(attributes.get()); + public LazyInitializedList<String> getAttributes() { + return attributes; } @Override @@ -915,7 +915,7 @@ public class cgCache implements ICache, IWaypoint { * @return always non <code>null</code> */ public List<cgWaypoint> getWaypoints() { - return Collections.unmodifiableList(waypoints.get()); + return waypoints.asList(); } /** @@ -942,28 +942,23 @@ public class cgCache implements ICache, IWaypoint { } /** - * @return never <code>null</code>, but an empty collection instead + * @return never <code>null</code> */ - public List<LogEntry> getLogs() { - return Collections.unmodifiableList(getLogs(true)); + public LazyInitializedList<LogEntry> getLogs() { + return logs; } /** - * @param allLogs - * true for all logs, false for friend logs only - * @return the logs with all entries or just the entries of the friends, never <code>null</code> + * @return only the logs of friends, never <code>null</code> */ - public List<LogEntry> getLogs(boolean allLogs) { - if (allLogs) { - return logs.get(); - } + public List<LogEntry> getFriendsLogs() { ArrayList<LogEntry> friendLogs = new ArrayList<LogEntry>(); - for (LogEntry log : logs.get()) { + for (LogEntry log : logs) { if (log.friend) { friendLogs.add(log); } } - return friendLogs; + return Collections.unmodifiableList(friendLogs); } /** @@ -1321,22 +1316,6 @@ public class cgCache implements ICache, IWaypoint { } } - public void addAttribute(final String attribute) { - attributes.add(attribute); - } - - public boolean hasAttributes() { - return !attributes.isEmpty(); - } - - public void prependLog(final LogEntry log) { - logs.prepend(log); - } - - public void appendLog(final LogEntry log) { - logs.add(log); - } - /* * For working in the debugger * (non-Javadoc) @@ -1502,7 +1481,7 @@ public class cgCache implements ICache, IWaypoint { // store images from logs if (Settings.isStoreLogImages()) { - for (LogEntry log : cache.getLogs(true)) { + for (LogEntry log : cache.getLogs()) { if (log.hasLogImages()) { for (cgImage oneLogImg : log.getLogImages()) { imgGetter.getDrawable(oneLogImg.getUrl()); diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 26ddc2c..773f08a 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -1074,13 +1074,12 @@ public class cgData { String geocode = cache.getGeocode(); database.delete(dbTableAttributes, "geocode = ?", new String[]{geocode}); - final List<String> attributes = cache.getAttributes(); - if (CollectionUtils.isNotEmpty(attributes)) { + if (cache.getAttributes().isNotEmpty()) { InsertHelper helper = new InsertHelper(database, dbTableAttributes); long timeStamp = System.currentTimeMillis(); - for (String attribute : attributes) { + for (String attribute : cache.getAttributes()) { helper.prepareForInsert(); helper.bind(ATTRIBUTES_GEOCODE, geocode); @@ -1266,40 +1265,42 @@ public class cgData { } } - private void saveLogsWithoutTransaction(final String geocode, final List<LogEntry> logs) { + private void saveLogsWithoutTransaction(final String geocode, final Iterable<LogEntry> logs) { // TODO delete logimages referring these logs database.delete(dbTableLogs, "geocode = ?", new String[]{geocode}); - if (CollectionUtils.isNotEmpty(logs)) { - InsertHelper helper = new InsertHelper(database, dbTableLogs); - long timeStamp = System.currentTimeMillis(); - for (LogEntry log : logs) { - helper.prepareForInsert(); + if (!logs.iterator().hasNext()) { + return; + } - helper.bind(LOGS_GEOCODE, geocode); - helper.bind(LOGS_UPDATED, timeStamp); - helper.bind(LOGS_TYPE, log.type.id); - helper.bind(LOGS_AUTHOR, log.author); - helper.bind(LOGS_LOG, log.log); - helper.bind(LOGS_DATE, log.date); - helper.bind(LOGS_FOUND, log.found); - helper.bind(LOGS_FRIEND, log.friend); - - long log_id = helper.execute(); - - if (log.hasLogImages()) { - ContentValues values = new ContentValues(); - for (cgImage img : log.getLogImages()) { - values.clear(); - values.put("log_id", log_id); - values.put("title", img.getTitle()); - values.put("url", img.getUrl()); - database.insert(dbTableLogImages, null, values); - } + InsertHelper helper = new InsertHelper(database, dbTableLogs); + long timeStamp = System.currentTimeMillis(); + for (LogEntry log : logs) { + helper.prepareForInsert(); + + helper.bind(LOGS_GEOCODE, geocode); + helper.bind(LOGS_UPDATED, timeStamp); + helper.bind(LOGS_TYPE, log.type.id); + helper.bind(LOGS_AUTHOR, log.author); + helper.bind(LOGS_LOG, log.log); + helper.bind(LOGS_DATE, log.date); + helper.bind(LOGS_FOUND, log.found); + helper.bind(LOGS_FRIEND, log.friend); + + long log_id = helper.execute(); + + if (log.hasLogImages()) { + ContentValues values = new ContentValues(); + for (cgImage img : log.getLogImages()) { + values.clear(); + values.put("log_id", log_id); + values.put("title", img.getTitle()); + values.put("url", img.getUrl()); + database.insert(dbTableLogImages, null, values); } } - helper.close(); } + helper.close(); } private void saveLogCountsWithoutTransaction(final cgCache cache) { diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index d5707d6..bc4b855 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -28,6 +28,7 @@ import cgeo.geocaching.network.Parameters; import cgeo.geocaching.ui.DirectionImage; import cgeo.geocaching.utils.BaseUtils; import cgeo.geocaching.utils.CancellableHandler; +import cgeo.geocaching.utils.LazyInitializedList; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; @@ -1630,14 +1631,14 @@ public abstract class GCParser { //cache.setLogs(loadLogsFromDetails(page, cache, false)); if (Settings.isFriendLogsWanted()) { CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_logs); - List<LogEntry> allLogs = cache.getLogs(); + LazyInitializedList<LogEntry> allLogs = cache.getLogs(); List<LogEntry> friendLogs = loadLogsFromDetails(page, cache, true, false); if (friendLogs != null) { for (LogEntry log : friendLogs) { if (allLogs.contains(log)) { allLogs.get(allLogs.indexOf(log)).friend = true; } else { - cache.appendLog(log); + cache.getLogs().add(log); } } } diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java index c06f0c6..7573db9 100644 --- a/main/src/cgeo/geocaching/export/GpxExport.java +++ b/main/src/cgeo/geocaching/export/GpxExport.java @@ -352,13 +352,12 @@ class GpxExport extends AbstractExport { } private void writeLogs(final cgCache cache) throws IOException { - final List<LogEntry> logs = cache.getLogs(); - if (logs.size() <= 0) { + if (cache.getLogs().isEmpty()) { return; } gpx.write("<groundspeak:logs>"); - for (LogEntry log : logs) { + for (LogEntry log : cache.getLogs()) { gpx.write("<groundspeak:log id=\""); gpx.write(Integer.toString(log.id)); gpx.write("\">"); @@ -386,7 +385,7 @@ class GpxExport extends AbstractExport { } private void writeAttributes(final cgCache cache) throws IOException { - if (!cache.hasAttributes()) { + if (cache.getAttributes().isEmpty()) { return; } //TODO: Attribute conversion required: English verbose name, gpx-id diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index d8f78fc..221aa1e 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -561,7 +561,7 @@ public abstract class GPXParser extends FileParser { boolean attributeActive = Integer.parseInt(attrs.getValue("inc")) != 0; String internalId = CacheAttributeTranslator.getInternalId(attributeId, attributeActive); if (internalId != null) { - cache.addAttribute(internalId); + cache.getAttributes().add(internalId); } } } catch (NumberFormatException e) { @@ -722,7 +722,7 @@ public abstract class GPXParser extends FileParser { @Override public void end() { if (log.type != LogType.UNKNOWN) { - cache.appendLog(log); + cache.getLogs().add(log); } } }); diff --git a/main/src/cgeo/geocaching/utils/LazyInitializedList.java b/main/src/cgeo/geocaching/utils/LazyInitializedList.java index 8c773cf..76876fc 100644 --- a/main/src/cgeo/geocaching/utils/LazyInitializedList.java +++ b/main/src/cgeo/geocaching/utils/LazyInitializedList.java @@ -1,6 +1,7 @@ package cgeo.geocaching.utils; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -8,11 +9,6 @@ public abstract class LazyInitializedList<ElementType> implements Iterable<Eleme private volatile List<ElementType> list; - public List<ElementType> get() { - initializeList(); - return list; - } - private void initializeList() { if (list == null) { synchronized (this) { @@ -39,6 +35,10 @@ public abstract class LazyInitializedList<ElementType> implements Iterable<Eleme list = new ArrayList<ElementType>(elements); } + public void set(LazyInitializedList<ElementType> other) { + list = new ArrayList<ElementType>(other.asList()); + } + public boolean isEmpty() { initializeList(); return list.isEmpty(); @@ -69,4 +69,27 @@ public abstract class LazyInitializedList<ElementType> implements Iterable<Eleme initializeList(); return list.get(index); } + + public boolean contains(final ElementType element) { + initializeList(); + return list.contains(element); + } + + public boolean isNotEmpty() { + initializeList(); + return !list.isEmpty(); + } + + /** + * @return an unmodifiable list of the elements + */ + public List<ElementType> asList() { + initializeList(); + return Collections.unmodifiableList(list); + } + + public int indexOf(ElementType element) { + initializeList(); + return list.indexOf(element); + } } |
