aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgData.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/cgData.java')
-rw-r--r--main/src/cgeo/geocaching/cgData.java227
1 files changed, 141 insertions, 86 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index 2ef5b27..87710fb 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -1,5 +1,7 @@
package cgeo.geocaching;
+import cgeo.geocaching.connector.IConnector;
+import cgeo.geocaching.connector.gc.Tile;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
@@ -11,6 +13,7 @@ import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Viewport;
+import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.Log;
import org.apache.commons.collections.CollectionUtils;
@@ -59,18 +62,52 @@ public class cgData {
DATABASE,
}
- /** The list of fields needed for mapping. */
- private static final String[] CACHE_COLUMNS = new String[] {
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
- "updated", "reason", "detailed", "detailedupdate", "visiteddate", "geocode", "cacheid", "guid", "type", "name", "owner", "owner_real", "hidden", "hint", "size",
- // 15 16 17 18 19 20 21 22 23
- "difficulty", "direction", "distance", "terrain", "latlon", "location", "elevation", "personal_note", "shortdesc",
- // 24 25 26 27 28 29 30 31 32
- "favourite_cnt", "rating", "votes", "myvote", "disabled", "archived", "members", "found", "favourite",
- // 33 34 35 36 37 38 39 40 41 42
- "inventoryunknown", "onWatchlist", "reliable_latlon", "coordsChanged", "latitude", "longitude", "finalDefined", "_id", "inventorycoins", "inventorytags"
- // reason is replaced by listId in Geocache
- };
+ // Columns and indices for the cache data
+ private static final String QUERY_CACHE_DATA =
+ "SELECT " +
+ "cg_caches.updated," + // 0
+ "cg_caches.reason," + // 1
+ "cg_caches.detailed," + // 2
+ "cg_caches.detailedupdate," + // 3
+ "cg_caches.visiteddate," + // 4
+ "cg_caches.geocode," + // 5
+ "cg_caches.cacheid," + // 6
+ "cg_caches.guid," + // 7
+ "cg_caches.type," + // 8
+ "cg_caches.name," + // 9
+ "cg_caches.owner," + // 10
+ "cg_caches.owner_real," + // 11
+ "cg_caches.hidden," + // 12
+ "cg_caches.hint," + // 13
+ "cg_caches.size," + // 14
+ "cg_caches.difficulty," + // 15
+ "cg_caches.direction," + // 16
+ "cg_caches.distance," + // 17
+ "cg_caches.terrain," + // 18
+ "cg_caches.latlon," + // 19
+ "cg_caches.location," + // 20
+ "cg_caches.personal_note," + // 21
+ "cg_caches.shortdesc," + // 22
+ "cg_caches.favourite_cnt," + // 23
+ "cg_caches.rating," + // 24
+ "cg_caches.votes," + // 25
+ "cg_caches.myvote," + // 26
+ "cg_caches.disabled," + // 27
+ "cg_caches.archived," + // 28
+ "cg_caches.members," + // 29
+ "cg_caches.found," + // 30
+ "cg_caches.favourite," + // 31
+ "cg_caches.inventoryunknown," + // 32
+ "cg_caches.onWatchlist," + // 33
+ "cg_caches.reliable_latlon," + // 34
+ "cg_caches.coordsChanged," + // 35
+ "cg_caches.latitude," + // 36
+ "cg_caches.longitude," + // 37
+ "cg_caches.finalDefined," + // 38
+ "cg_caches._id," + // 39
+ "cg_caches.inventorycoins," + // 40
+ "cg_caches.inventorytags," + // 41
+ "cg_caches.logPasswordRequired"; // 42
//TODO: remove "latlon" field from cache table
@@ -83,10 +120,9 @@ public class cgData {
/**
* holds the column indexes of the cache table to avoid lookups
*/
- private static int[] cacheColumnIndex;
private static CacheCache cacheCache = new CacheCache();
private static SQLiteDatabase database = null;
- private static final int dbVersion = 67;
+ private static final int dbVersion = 68;
public static final int customListIdOffset = 10;
private static final String dbName = "data";
private static final String dbTableCaches = "cg_caches";
@@ -127,7 +163,6 @@ public class cgData {
+ "latitude double, "
+ "longitude double, "
+ "reliable_latlon integer, "
- + "elevation double, "
+ "personal_note text, "
+ "shortdesc text, "
+ "description text, "
@@ -145,7 +180,8 @@ public class cgData {
+ "inventoryunknown integer default 0, "
+ "onWatchlist integer default 0, "
+ "coordsChanged integer default 0, "
- + "finalDefined integer default 0"
+ + "finalDefined integer default 0, "
+ + "logPasswordRequired integer default 0"
+ "); ";
private static final String dbCreateLists = ""
+ "create table " + dbTableLists + " ("
@@ -542,7 +578,6 @@ public class cgData {
+ "latitude double, "
+ "longitude double, "
+ "reliable_latlon integer, "
- + "elevation double, "
+ "personal_note text, "
+ "shortdesc text, "
+ "description text, "
@@ -563,7 +598,7 @@ public class cgData {
db.execSQL(dbCreateCachesTemp);
db.execSQL("insert into " + dbTableCachesTemp + " select _id,updated,detailed,detailedupdate,visiteddate,geocode,reason,cacheid,guid,type,name,own,owner,owner_real," +
- "hidden,hint,size,difficulty,terrain,latlon,location,direction,distance,latitude,longitude, 0,elevation," +
+ "hidden,hint,size,difficulty,terrain,latlon,location,direction,distance,latitude,longitude, 0," +
"personal_note,shortdesc,description,favourite_cnt,rating,votes,myvote,disabled,archived,members,found,favourite,inventorycoins," +
"inventorytags,inventoryunknown,onWatchlist from " + dbTableCaches);
db.execSQL("drop table " + dbTableCaches);
@@ -685,6 +720,15 @@ public class cgData {
}
}
+ // Introduces logPasswordRequired on caches
+ if (oldVersion < 68) {
+ try {
+ db.execSQL("alter table " + dbTableCaches + " add column logPasswordRequired integer default 0");
+ } catch (Exception e) {
+ Log.e("Failed to upgrade to ver. 68", e);
+
+ }
+ }
}
db.setTransactionSuccessful();
@@ -1035,7 +1079,6 @@ public class cgData {
values.put("direction", cache.getDirection());
putCoords(values, cache.getCoords());
values.put("reliable_latlon", cache.isReliableLatLon() ? 1 : 0);
- values.put("elevation", cache.getElevation());
values.put("shortdesc", cache.getShortDescription());
values.put("personal_note", cache.getPersonalNote());
values.put("description", cache.getDescription());
@@ -1052,13 +1095,13 @@ public class cgData {
values.put("onWatchlist", cache.isOnWatchlist() ? 1 : 0);
values.put("coordsChanged", cache.hasUserModifiedCoords() ? 1 : 0);
values.put("finalDefined", cache.hasFinalDefined() ? 1 : 0);
+ values.put("logPasswordRequired", cache.isLogPasswordRequired() ? 1 : 0);
init();
//try to update record else insert fresh..
database.beginTransaction();
- boolean result = false;
try {
saveAttributesWithoutTransaction(cache);
saveOriginalWaypointsWithoutTransaction(cache);
@@ -1074,14 +1117,14 @@ public class cgData {
database.insert(dbTableCaches, null, values);
}
database.setTransactionSuccessful();
- result = true;
+ return true;
} catch (Exception e) {
Log.e("SaveCache", e);
} finally {
database.endTransaction();
}
- return result;
+ return false;
}
private static void saveAttributesWithoutTransaction(final Geocache cache) {
@@ -1128,17 +1171,16 @@ public class cgData {
init();
database.beginTransaction();
- boolean result = false;
try {
saveOriginalWaypointsWithoutTransaction(cache);
database.setTransactionSuccessful();
- result = true;
+ return true;
} catch (Exception e) {
Log.e("saveWaypoints", e);
} finally {
database.endTransaction();
}
- return result;
+ return false;
}
private static void saveOriginalWaypointsWithoutTransaction(final Geocache cache) {
@@ -1332,7 +1374,7 @@ public class cgData {
}
}
- public static boolean saveTrackable(final Trackable trackable) {
+ public static void saveTrackable(final Trackable trackable) {
init();
database.beginTransaction();
@@ -1342,8 +1384,6 @@ public class cgData {
} finally {
database.endTransaction();
}
-
- return true;
}
private static void saveInventoryWithoutTransaction(final String geocode, final List<Trackable> trackables) {
@@ -1478,10 +1518,7 @@ public class cgData {
// do not log the entire collection of geo codes to the debug log. This can be more than 100 KB of text for large lists!
init();
- final StringBuilder query = new StringBuilder("SELECT ");
- for (int i = 0; i < CACHE_COLUMNS.length; i++) {
- query.append(i > 0 ? ", " : "").append(dbTableCaches).append('.').append(CACHE_COLUMNS[i]).append(' ');
- }
+ final StringBuilder query = new StringBuilder(QUERY_CACHE_DATA);
if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
query.append(',').append(dbTableLogsOffline).append(".log");
}
@@ -1578,71 +1615,58 @@ public class cgData {
private static Geocache createCacheFromDatabaseContent(Cursor cursor) {
Geocache cache = new Geocache();
- if (cacheColumnIndex == null) {
- final int[] local_cci = new int[CACHE_COLUMNS.length]; // use a local variable to avoid having the not yet fully initialized array be visible to other threads
- for (int i = 0; i < CACHE_COLUMNS.length; i++) {
- local_cci[i] = cursor.getColumnIndex(CACHE_COLUMNS[i]);
- }
- cacheColumnIndex = local_cci;
- }
-
- cache.setUpdated(cursor.getLong(cacheColumnIndex[0]));
- cache.setListId(cursor.getInt(cacheColumnIndex[1]));
- cache.setDetailed(cursor.getInt(cacheColumnIndex[2]) == 1);
- cache.setDetailedUpdate(cursor.getLong(cacheColumnIndex[3]));
- cache.setVisitedDate(cursor.getLong(cacheColumnIndex[4]));
- cache.setGeocode(cursor.getString(cacheColumnIndex[5]));
- cache.setCacheId(cursor.getString(cacheColumnIndex[6]));
- cache.setGuid(cursor.getString(cacheColumnIndex[7]));
- cache.setType(CacheType.getById(cursor.getString(cacheColumnIndex[8])));
- cache.setName(cursor.getString(cacheColumnIndex[9]));
- cache.setOwnerDisplayName(cursor.getString(cacheColumnIndex[10]));
- cache.setOwnerUserId(cursor.getString(cacheColumnIndex[11]));
- long dateValue = cursor.getLong(cacheColumnIndex[12]);
+ cache.setUpdated(cursor.getLong(0));
+ cache.setListId(cursor.getInt(1));
+ cache.setDetailed(cursor.getInt(2) == 1);
+ cache.setDetailedUpdate(cursor.getLong(3));
+ cache.setVisitedDate(cursor.getLong(4));
+ cache.setGeocode(cursor.getString(5));
+ cache.setCacheId(cursor.getString(6));
+ cache.setGuid(cursor.getString(7));
+ cache.setType(CacheType.getById(cursor.getString(8)));
+ cache.setName(cursor.getString(9));
+ cache.setOwnerDisplayName(cursor.getString(10));
+ cache.setOwnerUserId(cursor.getString(11));
+ long dateValue = cursor.getLong(12);
if (dateValue != 0) {
cache.setHidden(new Date(dateValue));
}
// do not set cache.hint
- cache.setSize(CacheSize.getById(cursor.getString(cacheColumnIndex[14])));
- cache.setDifficulty(cursor.getFloat(cacheColumnIndex[15]));
- int index = cacheColumnIndex[16];
+ cache.setSize(CacheSize.getById(cursor.getString(14)));
+ cache.setDifficulty(cursor.getFloat(15));
+ int index = 16;
if (cursor.isNull(index)) {
cache.setDirection(null);
} else {
cache.setDirection(cursor.getFloat(index));
}
- index = cacheColumnIndex[17];
+ index = 17;
if (cursor.isNull(index)) {
cache.setDistance(null);
} else {
cache.setDistance(cursor.getFloat(index));
}
- cache.setTerrain(cursor.getFloat(cacheColumnIndex[18]));
+ cache.setTerrain(cursor.getFloat(18));
// do not set cache.location
- cache.setCoords(getCoords(cursor, cacheColumnIndex[37], cacheColumnIndex[38]));
- index = cacheColumnIndex[21];
- if (cursor.isNull(index)) {
- cache.setElevation(null);
- } else {
- cache.setElevation(cursor.getDouble(index));
- }
- cache.setPersonalNote(cursor.getString(cacheColumnIndex[22]));
+ cache.setCoords(getCoords(cursor, 36, 37));
+ cache.setPersonalNote(cursor.getString(21));
// do not set cache.shortdesc
// do not set cache.description
- cache.setFavoritePoints(cursor.getInt(cacheColumnIndex[24]));
- cache.setRating(cursor.getFloat(cacheColumnIndex[25]));
- cache.setVotes(cursor.getInt(cacheColumnIndex[26]));
- cache.setMyVote(cursor.getFloat(cacheColumnIndex[27]));
- cache.setDisabled(cursor.getInt(cacheColumnIndex[28]) == 1);
- cache.setArchived(cursor.getInt(cacheColumnIndex[29]) == 1);
- cache.setPremiumMembersOnly(cursor.getInt(cacheColumnIndex[30]) == 1);
- cache.setFound(cursor.getInt(cacheColumnIndex[31]) == 1);
- cache.setFavorite(cursor.getInt(cacheColumnIndex[32]) == 1);
- cache.setInventoryItems(cursor.getInt(cacheColumnIndex[33]));
- cache.setOnWatchlist(cursor.getInt(cacheColumnIndex[34]) == 1);
- cache.setReliableLatLon(cursor.getInt(cacheColumnIndex[35]) > 0);
- cache.setUserModifiedCoords(cursor.getInt(cacheColumnIndex[36]) > 0);
- cache.setFinalDefined(cursor.getInt(cacheColumnIndex[39]) > 0);
+ cache.setFavoritePoints(cursor.getInt(23));
+ cache.setRating(cursor.getFloat(24));
+ cache.setVotes(cursor.getInt(25));
+ cache.setMyVote(cursor.getFloat(26));
+ cache.setDisabled(cursor.getInt(27) == 1);
+ cache.setArchived(cursor.getInt(28) == 1);
+ cache.setPremiumMembersOnly(cursor.getInt(29) == 1);
+ cache.setFound(cursor.getInt(30) == 1);
+ cache.setFavorite(cursor.getInt(31) == 1);
+ cache.setInventoryItems(cursor.getInt(32));
+ cache.setOnWatchlist(cursor.getInt(33) == 1);
+ cache.setReliableLatLon(cursor.getInt(34) > 0);
+ cache.setUserModifiedCoords(cursor.getInt(35) > 0);
+ cache.setFinalDefined(cursor.getInt(38) > 0);
+ cache.setLogPasswordRequired(cursor.getInt(42) > 0);
Log.d("Loading " + cache.toString() + " (" + cache.getListId() + ") from DB");
@@ -1814,18 +1838,17 @@ public class cgData {
init();
database.beginTransaction();
- boolean success = true;
try {
database.delete(dbTableSearchDestionationHistory, null, null);
database.setTransactionSuccessful();
+ return true;
} catch (Exception e) {
- success = false;
Log.e("Unable to clear searched destinations", e);
} finally {
database.endTransaction();
}
- return success;
+ return false;
}
public static List<LogEntry> loadLogs(String geocode) {
@@ -2665,18 +2688,17 @@ public class cgData {
init();
database.beginTransaction();
- boolean result = false;
try {
database.delete(dbTableSearchDestionationHistory, "_id = " + destination.getId(), null);
database.setTransactionSuccessful();
- result = true;
+ return true;
} catch (Exception e) {
Log.e("Unable to remove searched destination", e);
} finally {
database.endTransaction();
}
- return result;
+ return false;
}
/**
@@ -2963,4 +2985,37 @@ public class cgData {
return false;
}
+ public static Set<String> getCachedMissingFromSearch(final SearchResult searchResult, final Set<Tile> tiles, final IConnector connector, final int maxZoom) {
+
+ // get cached cgeocaches
+ final Set<String> cachedGeocodes = new HashSet<String>();
+ for (Tile tile : tiles) {
+ cachedGeocodes.addAll(cacheCache.getInViewport(tile.getViewport(), CacheType.ALL));
+ }
+ // remove found in search result
+ cachedGeocodes.removeAll(searchResult.getGeocodes());
+
+ // check remaining against viewports
+ Set<String> missingFromSearch = new HashSet<String>();
+ for (String geocode : cachedGeocodes) {
+ if (connector.canHandle(geocode)) {
+ Geocache geocache = cacheCache.getCacheFromCache(geocode);
+ if (geocache.getZoomLevel() <= maxZoom) {
+ boolean found = false;
+ for (Tile tile : tiles) {
+ if (tile.containsPoint(geocache)) {
+ found = true;
+ break;
+ }
+ }
+ if (found) {
+ missingFromSearch.add(geocode);
+ }
+ }
+ }
+ }
+
+ return missingFromSearch;
+ }
+
}