diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2012-04-23 07:50:26 +0200 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2012-04-23 07:50:26 +0200 |
commit | d2fba594dfea96b17ab01bb12afbc37ec10fe251 (patch) | |
tree | 2e272eae1a0eeadc9213e8059f8f1d6dc381e048 | |
parent | 0d19a993a9d2e99bd4f0627d182870ee1606cb91 (diff) | |
download | cgeo-d2fba594dfea96b17ab01bb12afbc37ec10fe251.zip cgeo-d2fba594dfea96b17ab01bb12afbc37ec10fe251.tar.gz cgeo-d2fba594dfea96b17ab01bb12afbc37ec10fe251.tar.bz2 |
refactoring of database access
* use only one transaction when saving a cache
* rework some methods to always take a collection of objects instead of
calling the database multiple times
-rw-r--r-- | main/src/cgeo/geocaching/LogEntry.java | 17 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/VisitCacheActivity.java | 26 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 4 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 464 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoapplication.java | 41 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeocaches.java | 77 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 45 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java | 10 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/files/GPXParser.java | 6 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/ui/CacheListAdapter.java | 13 | ||||
-rw-r--r-- | tests/src/cgeo/geocaching/cgDataTest.java | 7 | ||||
-rw-r--r-- | tests/src/cgeo/geocaching/files/GPXParserTest.java | 6 | ||||
-rw-r--r-- | tests/src/cgeo/geocaching/files/LocParserTest.java | 4 |
13 files changed, 272 insertions, 448 deletions
diff --git a/main/src/cgeo/geocaching/LogEntry.java b/main/src/cgeo/geocaching/LogEntry.java index 3f6ed74..9864f89 100644 --- a/main/src/cgeo/geocaching/LogEntry.java +++ b/main/src/cgeo/geocaching/LogEntry.java @@ -5,6 +5,7 @@ import cgeo.geocaching.enumerations.LogType; import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -14,6 +15,7 @@ public final class LogEntry { * using a cast, but that might trigger static code analysis tools. */ private static final List<cgImage> EMPTY_LIST = Collections.emptyList(); + public int id = 0; public LogType type = LogType.LOG_NOTE; // note public String author = ""; @@ -26,6 +28,21 @@ public final class LogEntry { public String cacheName = ""; // used for trackables public String cacheGuid = ""; // used for trackables + public LogEntry(final Calendar date, final LogType type, final String text) { + this(Settings.getUsername(), date.getTimeInMillis(), type, text); + } + + public LogEntry(final long dateInMilliSeconds, final LogType type, final String text) { + this(Settings.getUsername(), dateInMilliSeconds, type, text); + } + + public LogEntry(final String author, long dateInMilliSeconds, final LogType type, final String text) { + this.author = author; + this.date = dateInMilliSeconds; + this.type = type; + this.log = text; + } + @Override public int hashCode() { return (int) date * type.hashCode() * author.hashCode() * log.hashCode(); diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java index 88908f1..342f19d 100644 --- a/main/src/cgeo/geocaching/VisitCacheActivity.java +++ b/main/src/cgeo/geocaching/VisitCacheActivity.java @@ -4,7 +4,6 @@ import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.connector.gc.GCParser; import cgeo.geocaching.connector.gc.Login; import cgeo.geocaching.enumerations.LoadFlags; -import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag; import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.LogTypeTrackable; @@ -700,29 +699,16 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D log, trackables); if (status == StatusCode.NO_ERROR) { - final LogEntry logNow = new LogEntry(); - logNow.author = Settings.getUsername(); - logNow.date = date.getTimeInMillis(); - logNow.type = typeSelected; - logNow.log = log; - - if (cache != null) { - cache.prependLog(logNow); - } - app.addLog(geocode, logNow); + final LogEntry logNow = new LogEntry(date, typeSelected, log); + + cache.prependLog(logNow); + // app.saveLogs(cache); if (typeSelected == LogType.LOG_FOUND_IT) { - app.markFound(geocode); - if (cache != null) { - cache.setFound(true); - } + cache.setFound(true); } - if (cache != null) { - app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE)); - } else { - app.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); - } + app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE)); } if (status == StatusCode.NO_ERROR) { diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 3dd997d..a426614 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -908,7 +908,7 @@ public class cgCache implements ICache, IWaypoint { } } - return saveToDatabase && cgeoapplication.getInstance().saveWaypoints(geocode, waypoints, false); + return saveToDatabase && cgeoapplication.getInstance().saveWaypoints(this); } public List<LogEntry> getLogs() { @@ -1382,7 +1382,7 @@ public class cgCache implements ICache, IWaypoint { public void drop(Handler handler) { try { - cgeoapplication.getInstance().markDropped(getGeocode()); + cgeoapplication.getInstance().markDropped(Collections.singletonList(this)); cgeoapplication.getInstance().removeCache(getGeocode(), EnumSet.of(RemoveFlag.REMOVE_CACHE)); handler.sendMessage(Message.obtain()); diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 600a1e9..c290917 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -71,7 +71,7 @@ public class cgData { private Context context = null; private CacheCache cacheCache = null; private String path = null; - private cgDbHelper dbHelper = null; + private DbHelper dbHelper = null; private SQLiteDatabase databaseRO = null; private SQLiteDatabase databaseRW = null; private static final int dbVersion = 62; @@ -263,7 +263,7 @@ public class cgData { if (databaseRW == null || !databaseRW.isOpen()) { try { if (dbHelper == null) { - dbHelper = new cgDbHelper(context); + dbHelper = new DbHelper(context); } databaseRW = dbHelper.getWritableDatabase(); @@ -284,7 +284,7 @@ public class cgData { if (databaseRO == null || !databaseRO.isOpen()) { try { if (dbHelper == null) { - dbHelper = new cgDbHelper(context); + dbHelper = new DbHelper(context); } databaseRO = dbHelper.getReadableDatabase(); @@ -405,9 +405,9 @@ public class cgData { return restoreDone; } - private static class cgDbHelper extends SQLiteOpenHelper { + private static class DbHelper extends SQLiteOpenHelper { - cgDbHelper(Context context) { + DbHelper(Context context) { super(context, dbName, null, dbVersion); } @@ -1042,55 +1042,20 @@ public class cgData { values.put("coordsChanged", cache.hasUserModifiedCoords() ? 1 : 0); values.put("finalDefined", cache.hasFinalDefined() ? 1 : 0); - boolean statusOk = true; - - if (cache.hasAttributes()) { - if (!saveAttributes(cache.getGeocode(), cache.getAttributes())) { - statusOk = false; - } - } - - if (cache.hasWaypoints()) { - if (!saveWaypoints(cache.getGeocode(), cache.getWaypoints(), true)) { - statusOk = false; - } - } - - if (cache.getSpoilers() != null) { - if (!saveSpoilers(cache.getGeocode(), cache.getSpoilers())) { - statusOk = false; - } - } - - if (CollectionUtils.isNotEmpty(cache.getLogs())) { - if (!saveLogs(cache.getGeocode(), cache.getLogs())) { - statusOk = false; - } - } - - if (MapUtils.isNotEmpty(cache.getLogCounts())) { - if (!saveLogCount(cache.getGeocode(), cache.getLogCounts())) { - statusOk = false; - } - } - - if (cache.getInventory() != null) { - if (!saveInventory(cache.getGeocode(), cache.getInventory())) { - statusOk = false; - } - } - - if (!statusOk) { - cache.setDetailed(false); - cache.setDetailedUpdate(0L); - } - + boolean result = false; init(); //try to update record else insert fresh.. - boolean result = false; databaseRW.beginTransaction(); + try { + saveAttributesWithoutTransaction(cache); + saveWaypointsWithoutTransaction(cache); + saveSpoilersWithoutTransaction(cache); + saveLogsWithoutTransaction(cache.getGeocode(), cache.getLogs()); + saveLogCountsWithoutTransaction(cache); + saveInventoryWithoutTransaction(cache.getGeocode(), cache.getInventory()); + int rows = databaseRW.update(dbTableCaches, values, "geocode = ?", new String[] { cache.getGeocode() }); if (rows == 0) { // cache is not in the DB, insert it @@ -1104,43 +1069,30 @@ public class cgData { databaseRW.endTransaction(); } - values = null; return result; } - public boolean saveAttributes(String geocode, List<String> attributes) { - if (StringUtils.isBlank(geocode) || attributes == null) { - return false; - } - - init(); - - databaseRW.beginTransaction(); - try { - databaseRW.delete(dbTableAttributes, "geocode = ?", new String[] { geocode }); + private void saveAttributesWithoutTransaction(final cgCache cache) { + String geocode = cache.getGeocode(); + databaseRW.delete(dbTableAttributes, "geocode = ?", new String[] { geocode }); - if (!attributes.isEmpty()) { + final List<String> attributes = cache.getAttributes(); + if (CollectionUtils.isNotEmpty(attributes)) { - InsertHelper helper = new InsertHelper(databaseRW, dbTableAttributes); - long timeStamp = System.currentTimeMillis(); + InsertHelper helper = new InsertHelper(databaseRW, dbTableAttributes); + long timeStamp = System.currentTimeMillis(); - for (String attribute : attributes) { - helper.prepareForInsert(); + for (String attribute : attributes) { + helper.prepareForInsert(); - helper.bind(ATTRIBUTES_GEOCODE, geocode); - helper.bind(ATTRIBUTES_UPDATED, timeStamp); - helper.bind(ATTRIBUTES_ATTRIBUTE, attribute); + helper.bind(ATTRIBUTES_GEOCODE, geocode); + helper.bind(ATTRIBUTES_UPDATED, timeStamp); + helper.bind(ATTRIBUTES_ATTRIBUTE, attribute); - helper.execute(); - } - helper.close(); + helper.execute(); } - databaseRW.setTransactionSuccessful(); - } finally { - databaseRW.endTransaction(); + helper.close(); } - - return true; } /** @@ -1167,54 +1119,52 @@ public class cgData { } } - public boolean saveWaypoints(String geocode, List<cgWaypoint> waypoints, boolean drop) { - if (StringUtils.isBlank(geocode) || waypoints == null) { - return false; - } - + public boolean saveWaypoints(final cgCache cache) { + boolean result = false; init(); - - Log.d("cgData.saveWaypoints(drop=" + drop + ")"); - - boolean ok = false; databaseRW.beginTransaction(); - try { - if (drop) { - databaseRW.delete(dbTableWaypoints, "geocode = ? and type <> ? and own = 0", new String[] { geocode, "own" }); - } - - if (!waypoints.isEmpty()) { - ContentValues values = new ContentValues(); - long timeStamp = System.currentTimeMillis(); - for (cgWaypoint oneWaypoint : waypoints) { - if (oneWaypoint.isUserDefined()) { - continue; - } - - values.clear(); - values.put("geocode", geocode); - values.put("updated", timeStamp); - values.put("type", oneWaypoint.getWaypointType() != null ? oneWaypoint.getWaypointType().id : null); - values.put("prefix", oneWaypoint.getPrefix()); - values.put("lookup", oneWaypoint.getLookup()); - values.put("name", oneWaypoint.getName()); - values.put("latlon", oneWaypoint.getLatlon()); - putCoords(values, oneWaypoint.getCoords()); - values.put("note", oneWaypoint.getNote()); - values.put("own", oneWaypoint.isUserDefined() ? 1 : 0); - - final long rowId = databaseRW.insert(dbTableWaypoints, null, values); - oneWaypoint.setId((int) rowId); - } - } + try { + saveWaypointsWithoutTransaction(cache); databaseRW.setTransactionSuccessful(); - ok = true; + result = true; + } catch (Exception e) { + Log.e("saveWaypoints", e); } finally { databaseRW.endTransaction(); } + return result; + } - return ok; + private void saveWaypointsWithoutTransaction(final cgCache cache) { + String geocode = cache.getGeocode(); + databaseRW.delete(dbTableWaypoints, "geocode = ? and type <> ? and own = 0", new String[] { geocode, "own" }); + + List<cgWaypoint> waypoints = cache.getWaypoints(); + if (CollectionUtils.isNotEmpty(waypoints)) { + ContentValues values = new ContentValues(); + long timeStamp = System.currentTimeMillis(); + for (cgWaypoint oneWaypoint : waypoints) { + if (oneWaypoint.isUserDefined()) { + continue; + } + + values.clear(); + values.put("geocode", geocode); + values.put("updated", timeStamp); + values.put("type", oneWaypoint.getWaypointType() != null ? oneWaypoint.getWaypointType().id : null); + values.put("prefix", oneWaypoint.getPrefix()); + values.put("lookup", oneWaypoint.getLookup()); + values.put("name", oneWaypoint.getName()); + values.put("latlon", oneWaypoint.getLatlon()); + putCoords(values, oneWaypoint.getCoords()); + values.put("note", oneWaypoint.getNote()); + values.put("own", oneWaypoint.isUserDefined() ? 1 : 0); + + final long rowId = databaseRW.insert(dbTableWaypoints, null, values); + oneWaypoint.setId((int) rowId); + } + } } /** @@ -1301,112 +1251,69 @@ public class cgData { return databaseRW.delete(dbTableWaypoints, "_id = " + id, null) > 0; } - public boolean saveSpoilers(String geocode, List<cgImage> spoilers) { - if (StringUtils.isBlank(geocode) || spoilers == null) { - return false; - } - - init(); - - databaseRW.beginTransaction(); - try { - databaseRW.delete(dbTableSpoilers, "geocode = ?", new String[] { geocode }); + private void saveSpoilersWithoutTransaction(final cgCache cache) { + String geocode = cache.getGeocode(); + databaseRW.delete(dbTableSpoilers, "geocode = ?", new String[] { geocode }); - if (!spoilers.isEmpty()) { - ContentValues values = new ContentValues(); - long timeStamp = System.currentTimeMillis(); - for (cgImage oneSpoiler : spoilers) { - values.clear(); - values.put("geocode", geocode); - values.put("updated", timeStamp); - values.put("url", oneSpoiler.getUrl()); - values.put("title", oneSpoiler.getTitle()); - values.put("description", oneSpoiler.getDescription()); + List<cgImage> spoilers = cache.getSpoilers(); + if (CollectionUtils.isNotEmpty(spoilers)) { + ContentValues values = new ContentValues(); + long timeStamp = System.currentTimeMillis(); + for (cgImage spoiler : spoilers) { + values.clear(); + values.put("geocode", geocode); + values.put("updated", timeStamp); + values.put("url", spoiler.getUrl()); + values.put("title", spoiler.getTitle()); + values.put("description", spoiler.getDescription()); - databaseRW.insert(dbTableSpoilers, null, values); - } + databaseRW.insert(dbTableSpoilers, null, values); } - databaseRW.setTransactionSuccessful(); - } finally { - databaseRW.endTransaction(); } - - return true; - } - - public boolean saveLogs(String geocode, List<LogEntry> logs) { - return saveLogs(geocode, logs, true); } - public boolean saveLogs(String geocode, List<LogEntry> logs, boolean drop) { - if (StringUtils.isBlank(geocode) || logs == null) { - return false; - } - - init(); - - databaseRW.beginTransaction(); - try { - if (drop) { - // TODO delete logimages referring these logs - databaseRW.delete(dbTableLogs, "geocode = ?", new String[] { geocode }); - } + private void saveLogsWithoutTransaction(final String geocode, final List<LogEntry> logs) { + // TODO delete logimages referring these logs + databaseRW.delete(dbTableLogs, "geocode = ?", new String[] { geocode }); - if (!logs.isEmpty()) { - InsertHelper helper = new InsertHelper(databaseRW, 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()); - databaseRW.insert(dbTableLogImages, null, values); - } + if (CollectionUtils.isNotEmpty(logs)) { + InsertHelper helper = new InsertHelper(databaseRW, 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()); + databaseRW.insert(dbTableLogImages, null, values); } } - helper.close(); } - databaseRW.setTransactionSuccessful(); - } finally { - databaseRW.endTransaction(); + helper.close(); } - - return true; - } - - public boolean saveLogCount(String geocode, Map<LogType, Integer> logCounts) { - return saveLogCount(geocode, logCounts, true); } - public boolean saveLogCount(String geocode, Map<LogType, Integer> logCounts, boolean drop) { - if (StringUtils.isBlank(geocode) || MapUtils.isEmpty(logCounts)) { - return false; - } - - init(); - - databaseRW.beginTransaction(); - try { - if (drop) { - databaseRW.delete(dbTableLogCount, "geocode = ?", new String[] { geocode }); - } + private void saveLogCountsWithoutTransaction(final cgCache cache) { + String geocode = cache.getGeocode(); + databaseRW.delete(dbTableLogCount, "geocode = ?", new String[] { geocode }); + Map<LogType, Integer> logCounts = cache.getLogCounts(); + if (MapUtils.isNotEmpty(logCounts)) { ContentValues values = new ContentValues(); Set<Entry<LogType, Integer>> logCountsItems = logCounts.entrySet(); @@ -1420,6 +1327,15 @@ public class cgData { databaseRW.insert(dbTableLogCount, null, values); } + } + } + + public boolean saveTrackable(final cgTrackable trackable) { + init(); + + databaseRW.beginTransaction(); + try { + saveInventoryWithoutTransaction(null, Collections.singletonList(trackable)); databaseRW.setTransactionSuccessful(); } finally { databaseRW.endTransaction(); @@ -1428,51 +1344,37 @@ public class cgData { return true; } - public boolean saveInventory(String geocode, List<cgTrackable> trackables) { - if (trackables == null) { - return false; + private void saveInventoryWithoutTransaction(final String geocode, final List<cgTrackable> trackables) { + if (geocode != null) { + databaseRW.delete(dbTableTrackables, "geocode = ?", new String[] { geocode }); } - init(); - - databaseRW.beginTransaction(); - try { - if (geocode != null) { - databaseRW.delete(dbTableTrackables, "geocode = ?", new String[] { geocode }); - } - - if (!trackables.isEmpty()) { - ContentValues values = new ContentValues(); - long timeStamp = System.currentTimeMillis(); - for (cgTrackable oneTrackable : trackables) { - values.clear(); - if (geocode != null) { - values.put("geocode", geocode); - } - values.put("updated", timeStamp); - values.put("tbcode", oneTrackable.getGeocode()); - values.put("guid", oneTrackable.getGuid()); - values.put("title", oneTrackable.getName()); - values.put("owner", oneTrackable.getOwner()); - if (oneTrackable.getReleased() != null) { - values.put("released", oneTrackable.getReleased().getTime()); - } else { - values.put("released", 0L); - } - values.put("goal", oneTrackable.getGoal()); - values.put("description", oneTrackable.getDetails()); + if (CollectionUtils.isNotEmpty(trackables)) { + ContentValues values = new ContentValues(); + long timeStamp = System.currentTimeMillis(); + for (cgTrackable trackable : trackables) { + values.clear(); + if (geocode != null) { + values.put("geocode", geocode); + } + values.put("updated", timeStamp); + values.put("tbcode", trackable.getGeocode()); + values.put("guid", trackable.getGuid()); + values.put("title", trackable.getName()); + values.put("owner", trackable.getOwner()); + if (trackable.getReleased() != null) { + values.put("released", trackable.getReleased().getTime()); + } else { + values.put("released", 0L); + } + values.put("goal", trackable.getGoal()); + values.put("description", trackable.getDetails()); - databaseRW.insert(dbTableTrackables, null, values); + databaseRW.insert(dbTableTrackables, null, values); - saveLogs(oneTrackable.getGeocode(), oneTrackable.getLogs()); - } + saveLogsWithoutTransaction(trackable.getGeocode(), trackable.getLogs()); } - databaseRW.setTransactionSuccessful(); - } finally { - databaseRW.endTransaction(); } - - return true; } public Viewport getBounds(final Set<String> geocodes) { @@ -2036,12 +1938,12 @@ public class cgData { int indexUrl = cursor.getColumnIndex("url"); while (cursor.moveToNext() && logs.size() < 100) { if (log == null || log.id != cursor.getInt(indexLogsId)) { - log = new LogEntry(); + log = new LogEntry( + cursor.getString(indexAuthor), + cursor.getLong(indexDate), + LogType.getById(cursor.getInt(indexType)), + cursor.getString(indexLog)); log.id = cursor.getInt(indexLogsId); - log.type = LogType.getById(cursor.getInt(indexType)); - log.author = cursor.getString(indexAuthor); - log.log = cursor.getString(indexLog); - log.date = cursor.getLong(indexDate); log.found = cursor.getInt(indexFound); log.friend = cursor.getInt(indexFriend) == 1 ? true : false; logs.add(log); @@ -2453,36 +2355,6 @@ public class cgData { return geocodes; } - public boolean markFound(String geocode) { - if (StringUtils.isBlank(geocode)) { - return false; - } - - init(); - - boolean result = false; - databaseRW.beginTransaction(); - try { - ContentValues values = new ContentValues(); - values.put("found", 1); - int rows = databaseRW.update(dbTableCaches, values, "geocode = ?", new String[] { geocode }); - if (rows > 0) { - // update CacheCache - cgCache cache = cacheCache.getCacheFromCache(geocode); - if (cache != null) { - cache.setFound(true); - cacheCache.putCacheInCache(cache); - } - result = true; - } - databaseRW.setTransactionSuccessful(); - } finally { - databaseRW.endTransaction(); - } - - return result; - } - /** delete caches from the DB store 3 days or more before */ public void clean() { clean(false); @@ -2697,11 +2569,11 @@ public class cgData { if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); - log = new LogEntry(); + log = new LogEntry( + cursor.getLong(cursor.getColumnIndex("date")), + LogType.getById(cursor.getInt(cursor.getColumnIndex("type"))), + cursor.getString(cursor.getColumnIndex("log"))); log.id = cursor.getInt(cursor.getColumnIndex("_id")); - log.type = LogType.getById(cursor.getInt(cursor.getColumnIndex("type"))); - log.log = cursor.getString(cursor.getColumnIndex("log")); - log.date = cursor.getLong(cursor.getColumnIndex("date")); } if (cursor != null) { @@ -2934,29 +2806,25 @@ public class cgData { return status; } - public void moveToList(String geocode, int listId) { - if (StringUtils.isBlank(geocode)) { + public void moveToList(final List<cgCache> caches, final int listId) { + if (caches.isEmpty()) { return; } - init(); - ContentValues values = new ContentValues(); + final ContentValues values = new ContentValues(); values.put("reason", listId); + databaseRW.beginTransaction(); try { - databaseRW.update(dbTableCaches, values, "geocode = ?", new String[] { geocode }); + for (cgCache cache : caches) { + databaseRW.update(dbTableCaches, values, "geocode = ?", new String[] { cache.getGeocode() }); + cache.setListId(listId); + } databaseRW.setTransactionSuccessful(); } finally { databaseRW.endTransaction(); } - - // update CacheCache - cgCache cache = cacheCache.getCacheFromCache(geocode); - if (cache != null) { - cache.setListId(listId); - cacheCache.putCacheInCache(cache); - } } public synchronized boolean status() { diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 55fc84c..2b10c16 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -21,7 +21,6 @@ import android.os.Handler; import android.os.Message; import java.io.File; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.EnumSet; @@ -333,18 +332,13 @@ public class cgeoapplication extends Application { } /** {@link cgData#moveToList(String, int)} */ - public void markStored(String geocode, int listId) { - storage.moveToList(geocode, listId); + public void markStored(List<cgCache> caches, int listId) { + storage.moveToList(caches, listId); } /** {@link cgData#moveToList(String, int)} */ - public void markDropped(String geocode) { - storage.moveToList(geocode, StoredList.TEMPORARY_LIST_ID); - } - - /** {@link cgData#markFound(String)} */ - public boolean markFound(String geocode) { - return storage.markFound(geocode); + public void markDropped(List<cgCache> caches) { + storage.moveToList(caches, StoredList.TEMPORARY_LIST_ID); } /** {@link cgData#clearSearchedDestinations()} */ @@ -358,8 +352,8 @@ public class cgeoapplication extends Application { } /** {@link cgData#saveWaypoints(String, List, boolean)} */ - public boolean saveWaypoints(String geocode, List<cgWaypoint> waypoints, boolean drop) { - return storage.saveWaypoints(geocode, waypoints, drop); + public boolean saveWaypoints(final cgCache cache) { + return storage.saveWaypoints(cache); } public boolean saveOwnWaypoint(int id, String geocode, cgWaypoint waypoint) { @@ -376,10 +370,7 @@ public class cgeoapplication extends Application { } public boolean saveTrackable(cgTrackable trackable) { - final List<cgTrackable> list = new ArrayList<cgTrackable>(); - list.add(trackable); - - return storage.saveInventory("---", list); + return storage.saveTrackable(trackable); } /** {@link cgData#dropList(int)} **/ @@ -416,20 +407,6 @@ public class cgeoapplication extends Application { return StringUtils.defaultString(action); } - public boolean addLog(String geocode, LogEntry log) { - if (StringUtils.isBlank(geocode)) { - return false; - } - if (log == null) { - return false; - } - - List<LogEntry> list = new ArrayList<LogEntry>(); - list.add(log); - - return storage.saveLogs(geocode, list, false); - } - public void setLastCoords(final Geopoint coords) { lastCoords = coords; } @@ -494,8 +471,8 @@ public class cgeoapplication extends Application { } /** {@link cgData#moveToList(String, int)} */ - public void moveToList(String geocode, int listId) { - storage.moveToList(geocode, listId); + public void moveToList(List<cgCache> caches, int listId) { + storage.moveToList(caches, listId); } /** {@link cgData#getCacheDescription(String)} */ diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index 070e115..86ebaca 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -70,7 +70,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -400,11 +399,7 @@ public class cgeocaches extends AbstractListActivity { @Override public void handleMessage(Message msg) { - if (msg.what == MSG_CANCEL) { - if (threadR != null) { - threadR.kill(); - } - } else { + if (msg.what != MSG_CANCEL) { if (adapter != null) { adapter.setSelectMode(false, true); } @@ -1046,12 +1041,15 @@ public class cgeocaches extends AbstractListActivity { @Override public void run(Integer newListId) { + List<cgCache> selected; final boolean moveAll = adapter.getChecked() == 0; - for (final cgCache c : Collections.unmodifiableList(cacheList)) { - if (moveAll || c.isStatusChecked()) { - app.moveToList(c.getGeocode(), newListId); - } + if (moveAll) { + selected = new ArrayList<cgCache>(cacheList); } + else { + selected = adapter.getCheckedCaches(); + } + app.moveToList(selected, newListId); adapter.resetChecks(); refreshCurrentList(); @@ -1106,12 +1104,12 @@ public class cgeocaches extends AbstractListActivity { }); return true; } else if (id == MENU_MOVE_TO_LIST) { - final String geocode = getCacheFromAdapter(adapterInfo).getGeocode(); + final cgCache cache = getCacheFromAdapter(adapterInfo); new StoredList.UserInterface(this).promptForListSelection(R.string.cache_menu_move_list, new RunnableWithArgument<Integer>() { @Override public void run(Integer newListId) { - app.moveToList(geocode, newListId); + app.moveToList(Collections.singletonList(cache), newListId); adapter.resetChecks(); refreshCurrentList(); } @@ -1382,12 +1380,7 @@ public class cgeocaches extends AbstractListActivity { List<cgCache> caches; if (adapter != null && adapter.getChecked() > 0) { // there are some caches checked - caches = new LinkedList<cgCache>(); - for (cgCache cache : cacheList) { - if (cache.isStatusChecked()) { - caches.add(cache); - } - } + caches = adapter.getCheckedCaches(); } else { // no caches checked, export all caches = cacheList; @@ -1818,46 +1811,26 @@ public class cgeocaches extends AbstractListActivity { private class DropDetailsThread extends Thread { final private Handler handler; - private volatile boolean needToStop = false; - private int checked = 0; + private List<cgCache> selected = new ArrayList<cgCache>(); public DropDetailsThread(Handler handlerIn) { setPriority(Thread.MIN_PRIORITY); handler = handlerIn; - if (adapter != null) { - checked = adapter.getChecked(); + int checked = adapter.getChecked(); + if (checked == 0) { + selected = new ArrayList<cgCache>(cacheList); + } + else { + selected = adapter.getCheckedCaches(); } - } - - public void kill() { - needToStop = true; } @Override public void run() { removeGeoAndDir(); - - final List<cgCache> cacheListTemp = new ArrayList<cgCache>(cacheList); - for (cgCache cache : cacheListTemp) { - if (checked > 0 && !cache.isStatusChecked()) { - continue; - } - - try { - if (needToStop) { - Log.i("Stopped dropping process."); - break; - } - - app.markDropped(cache.getGeocode()); - } catch (Exception e) { - Log.e("cgeocaches.DropDetailsThread: " + e.toString()); - } - } - cacheListTemp.clear(); - + app.markDropped(selected); handler.sendEmptyMessage(MSG_DONE); } } @@ -1996,16 +1969,8 @@ public class cgeocaches extends AbstractListActivity { @Override public void run() { - int checked = adapter.getChecked(); - if (checked > 0) { - final List<cgCache> cacheListTemp = new ArrayList<cgCache>(cacheList); - for (cgCache cache : cacheListTemp) { - if (cache.isStatusChecked()) { - app.moveToList(cache.getGeocode(), listId); - } - } - } - + final List<cgCache> caches = adapter.getCheckedCaches(); + app.moveToList(caches, listId); handler.sendEmptyMessage(listId); } } diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index b726b26..9c61c10 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -1283,8 +1283,7 @@ public abstract class GCParser { } // trackable logs - try - { + try { final Matcher matcherLogs = GCConstants.PATTERN_TRACKABLE_LOG.matcher(page); /* * 1. Type (img) @@ -1295,23 +1294,20 @@ public abstract class GCParser { * 6. Cache-name * 7. Logtext */ - while (matcherLogs.find()) - { - final LogEntry logDone = new LogEntry(); - - logDone.type = LogType.getByIconName(matcherLogs.group(1)); - logDone.author = Html.fromHtml(matcherLogs.group(3)).toString().trim(); - - try - { - logDone.date = Login.parseGcCustomDate(matcherLogs.group(2)).getTime(); + while (matcherLogs.find()) { + long date = 0; + try { + date = Login.parseGcCustomDate(matcherLogs.group(2)).getTime(); } catch (ParseException e) { } - logDone.log = matcherLogs.group(7).trim(); + final LogEntry logDone = new LogEntry( + Html.fromHtml(matcherLogs.group(3)).toString().trim(), + date, + LogType.getByIconName(matcherLogs.group(1)), + matcherLogs.group(7).trim()); - if (matcherLogs.group(4) != null && matcherLogs.group(6) != null) - { + if (matcherLogs.group(4) != null && matcherLogs.group(6) != null) { logDone.cacheGuid = matcherLogs.group(4); logDone.cacheName = matcherLogs.group(6); } @@ -1322,8 +1318,7 @@ public abstract class GCParser { * 1. Image URL * 2. Image title */ - while (matcherLogImages.find()) - { + while (matcherLogImages.find()) { final cgImage logImage = new cgImage(matcherLogImages.group(1), matcherLogImages.group(2)); logDone.addLogImage(logImage); } @@ -1409,23 +1404,25 @@ public abstract class GCParser { for (int index = 0; index < data.length(); index++) { final JSONObject entry = data.getJSONObject(index); - final LogEntry logDone = new LogEntry(); - logDone.friend = friends; // FIXME: use the "LogType" field instead of the "LogTypeImage" one. final String logIconNameExt = entry.optString("LogTypeImage", ".gif"); final String logIconName = logIconNameExt.substring(0, logIconNameExt.length() - 4); - logDone.type = LogType.getByIconName(logIconName); + long date = 0; try { - logDone.date = Login.parseGcCustomDate(entry.getString("Visited")).getTime(); + date = Login.parseGcCustomDate(entry.getString("Visited")).getTime(); } catch (ParseException e) { - Log.e("cgBase.loadLogsFromDetails: failed to parse log date."); + Log.e("GCParser.loadLogsFromDetails: failed to parse log date."); } - logDone.author = entry.getString("UserName"); + final LogEntry logDone = new LogEntry( + entry.getString("UserName"), + date, + LogType.getByIconName(logIconName), + entry.getString("LogText")); logDone.found = entry.getInt("GeocacheFindCount"); - logDone.log = entry.getString("LogText"); + logDone.friend = friends; final JSONArray images = entry.getJSONArray("Images"); for (int i = 0; i < images.length(); i++) { diff --git a/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java b/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java index 80ad58e..ce59901 100644 --- a/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java +++ b/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java @@ -206,11 +206,11 @@ final public class OkapiClient { for (int i = 0; i < logsJSON.length(); i++) { try { JSONObject logResponse = logsJSON.getJSONObject(i); - LogEntry log = new LogEntry(); - log.date = parseDate(logResponse.getString(LOG_DATE)).getTime(); - log.log = logResponse.getString(LOG_COMMENT).trim(); - log.type = parseLogType(logResponse.getString(LOG_TYPE)); - log.author = parseUser(logResponse.getJSONObject(LOG_USER)); + LogEntry log = new LogEntry( + parseUser(logResponse.getJSONObject(LOG_USER)), + parseDate(logResponse.getString(LOG_DATE)).getTime(), + parseLogType(logResponse.getString(LOG_TYPE)), + logResponse.getString(LOG_COMMENT).trim()); if (result == null) { result = new ArrayList<LogEntry>(); } diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index 8b9cdda..ec8c307 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -1,9 +1,9 @@ package cgeo.geocaching.files; +import cgeo.geocaching.LogEntry; import cgeo.geocaching.R; import cgeo.geocaching.StoredList; import cgeo.geocaching.cgCache; -import cgeo.geocaching.LogEntry; import cgeo.geocaching.cgTrackable; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeoapplication; @@ -76,7 +76,7 @@ public abstract class GPXParser extends FileParser { private cgCache cache; private cgTrackable trackable = new cgTrackable(); - private LogEntry log = new LogEntry(); + private LogEntry log = null; private String type = null; private String sym = null; @@ -674,7 +674,7 @@ public abstract class GPXParser extends FileParser { @Override public void start(Attributes attrs) { - log = new LogEntry(); + log = new LogEntry("", 0, LogType.LOG_UNKNOWN, ""); try { if (attrs.getIndex("id") > -1) { diff --git a/main/src/cgeo/geocaching/ui/CacheListAdapter.java b/main/src/cgeo/geocaching/ui/CacheListAdapter.java index 2735a94..9f2d69d 100644 --- a/main/src/cgeo/geocaching/ui/CacheListAdapter.java +++ b/main/src/cgeo/geocaching/ui/CacheListAdapter.java @@ -856,4 +856,17 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { public List<cgCache> getFilteredList() { return list; } + + public List<cgCache> getCheckedCaches() { + ArrayList<cgCache> result = new ArrayList<cgCache>(); + int checked = getChecked(); + if (checked > 0) { + for (cgCache cache : list) { + if (cache.isStatusChecked()) { + result.add(cache); + } + } + } + return result; + } } diff --git a/tests/src/cgeo/geocaching/cgDataTest.java b/tests/src/cgeo/geocaching/cgDataTest.java index d6bb389..3ce6431 100644 --- a/tests/src/cgeo/geocaching/cgDataTest.java +++ b/tests/src/cgeo/geocaching/cgDataTest.java @@ -6,6 +6,7 @@ import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Viewport; +import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -51,17 +52,17 @@ public class cgDataTest extends CGeoTestCase { assertEquals("cgData Test (renamed)", list1.title); // move to list (cache1=listId2, cache2=listId2) - app.moveToList(cache1.getGeocode(), listId2); + app.moveToList(Collections.singletonList(cache1), listId2); assertEquals(1, app.getAllStoredCachesCount(false, CacheType.ALL, listId2)); // remove list (cache1=listId2, cache2=listId2) assertTrue(app.removeList(listId1)); // mark dropped (cache1=1, cache2=0) - app.markDropped(cache2.getGeocode()); + app.markDropped(Collections.singletonList(cache2)); // mark stored (cache1=1, cache2=listId2) - app.markStored(cache2.getGeocode(), listId2); + app.markStored(Collections.singletonList(cache2), listId2); assertEquals(2, app.getAllStoredCachesCount(false, CacheType.ALL, listId2)); // drop stored (cache1=0, cache2=0) diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java index 634c551..f37f327 100644 --- a/tests/src/cgeo/geocaching/files/GPXParserTest.java +++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java @@ -1,7 +1,7 @@ package cgeo.geocaching.files; -import cgeo.geocaching.cgCache; import cgeo.geocaching.LogEntry; +import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; @@ -42,7 +42,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { assertEquals(5.0f, cache.getTerrain()); assertEquals("Baden-Württemberg, Germany", cache.getLocation()); assertEquals("Ein alter Kindheitstraum, ein Schatz auf einer unbewohnten Insel.\nA old dream of my childhood, a treasure on a lonely island.", cache.getShortdesc()); - assertTrue(new Geopoint(48.859683, 9.1874).equals(cache.getCoords())); + assertEquals(new Geopoint(48.859683, 9.1874), cache.getCoords()); return cache; } @@ -66,7 +66,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { assertEquals(4.0f, cache.getTerrain()); assertEquals("Baden-Württemberg, Germany", cache.getLocation()); assertEquals("Ein alter Kindheitstraum, ein Schatz auf einer unbewohnten Insel. A old dream of my childhood, a treasure on a lonely is", cache.getShortdesc()); - assertTrue(new Geopoint(48.85968, 9.18740).equals(cache.getCoords())); + assertEquals(new Geopoint(48.85968, 9.18740), cache.getCoords()); assertTrue(cache.isReliableLatLon()); } diff --git a/tests/src/cgeo/geocaching/files/LocParserTest.java b/tests/src/cgeo/geocaching/files/LocParserTest.java index ede0e81..6cd6c7b 100644 --- a/tests/src/cgeo/geocaching/files/LocParserTest.java +++ b/tests/src/cgeo/geocaching/files/LocParserTest.java @@ -37,7 +37,7 @@ public class LocParserTest extends AbstractResourceInstrumentationTestCase { assertNotNull(cache); assertEquals("OC5952", cache.getGeocode()); assertEquals("Die Schatzinsel / treasure island", cache.getName()); - assertTrue(new Geopoint(48.85968, 9.18740).equals(cache.getCoords())); + assertEquals(new Geopoint(48.85968, 9.18740), cache.getCoords()); } public void testGCLoc() throws IOException, ParserException { @@ -47,7 +47,7 @@ public class LocParserTest extends AbstractResourceInstrumentationTestCase { assertNotNull(cache); assertEquals("GC1BKP3", cache.getGeocode()); assertEquals("Die Schatzinsel / treasure island", cache.getName()); - assertTrue(new Geopoint(48.859683, 9.1874).equals(cache.getCoords())); + assertEquals(new Geopoint(48.859683, 9.1874), cache.getCoords()); assertEquals(1.0f, cache.getDifficulty()); assertEquals(5.0f, cache.getTerrain()); assertEquals(CacheSize.MICRO, cache.getSize()); |