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.java630
1 files changed, 349 insertions, 281 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index b822c2b..c163c57 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -23,7 +23,6 @@ import android.content.ContextWrapper;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.DatabaseUtils;
-import android.database.DatabaseUtils.InsertHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDoneException;
@@ -48,6 +47,10 @@ import java.util.regex.Pattern;
public class cgData {
+ private cgData() {
+ // utility class
+ }
+
public enum StorageLocation {
HEAP,
CACHE,
@@ -72,8 +75,8 @@ public class cgData {
* holds the column indexes of the cache table to avoid lookups
*/
private static int[] cacheColumnIndex;
- private CacheCache cacheCache = new CacheCache();
- private SQLiteDatabase database = null;
+ private static CacheCache cacheCache = new CacheCache();
+ private static SQLiteDatabase database = null;
private static final int dbVersion = 64;
public static final int customListIdOffset = 10;
private static final String dbName = "data";
@@ -151,9 +154,6 @@ public class cgData {
+ "updated long not null, " // date of save
+ "attribute text "
+ "); ";
- private final static int ATTRIBUTES_GEOCODE = 2;
- private final static int ATTRIBUTES_UPDATED = 3;
- private final static int ATTRIBUTES_ATTRIBUTE = 4;
private static final String dbCreateWaypoints = ""
+ "create table " + dbTableWaypoints + " ("
@@ -191,14 +191,6 @@ public class cgData {
+ "found integer not null default 0, "
+ "friend integer "
+ "); ";
- private final static int LOGS_GEOCODE = 2;
- private final static int LOGS_UPDATED = 3;
- private final static int LOGS_TYPE = 4;
- private final static int LOGS_AUTHOR = 5;
- private final static int LOGS_LOG = 6;
- private final static int LOGS_DATE = 7;
- private final static int LOGS_FOUND = 8;
- private final static int LOGS_FRIEND = 9;
private static final String dbCreateLogCount = ""
+ "create table " + dbTableLogCount + " ("
@@ -246,10 +238,10 @@ public class cgData {
+ "longitude double "
+ "); ";
- private HashMap<String, SQLiteStatement> statements = new HashMap<String, SQLiteStatement>();
private static boolean newlyCreatedDatabase = false;
+ private static boolean databaseCleaned = false;
- public synchronized void init() {
+ public synchronized static void init() {
if (database != null) {
return;
}
@@ -262,35 +254,28 @@ public class cgData {
}
}
- public void closeDb() {
+ public static void closeDb() {
if (database == null) {
return;
}
cacheCache.removeAllFromCache();
- clearPreparedStatements();
+ PreparedStatements.clearPreparedStatements();
database.close();
database = null;
}
- private void clearPreparedStatements() {
- for (SQLiteStatement statement : statements.values()) {
- statement.close();
- }
- statements.clear();
- }
-
- private static File backupFile() {
+ private static File getBackupFile() {
return new File(LocalStorage.getStorage(), "cgeo.sqlite");
}
- public String backupDatabase() {
+ public static String backupDatabase() {
if (!LocalStorage.isExternalStorageAvailable()) {
Log.w("Database wasn't backed up: no external memory");
return null;
}
- final File target = backupFile();
+ final File target = getBackupFile();
closeDb();
final boolean backupDone = LocalStorage.copy(databasePath(), target);
init();
@@ -304,7 +289,7 @@ public class cgData {
return target.getPath();
}
- public boolean moveDatabase() {
+ public static boolean moveDatabase() {
if (!LocalStorage.isExternalStorageAvailable()) {
Log.w("Database was not moved: external memory not available");
return false;
@@ -340,18 +325,18 @@ public class cgData {
return databasePath(Settings.isDbOnSDCard());
}
- public static File isRestoreFile() {
- final File fileSourceFile = backupFile();
+ public static File getRestoreFile() {
+ final File fileSourceFile = getBackupFile();
return fileSourceFile.exists() ? fileSourceFile : null;
}
- public boolean restoreDatabase() {
+ public static boolean restoreDatabase() {
if (!LocalStorage.isExternalStorageAvailable()) {
Log.w("Database wasn't restored: no external memory");
return false;
}
- final File sourceFile = backupFile();
+ final File sourceFile = getBackupFile();
closeDb();
final boolean restoreDone = LocalStorage.copy(sourceFile, databasePath());
init();
@@ -680,7 +665,7 @@ public class cgData {
final FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
- return (filename.startsWith("map_") && filename.contains("__"));
+ return filename.startsWith("map_") && filename.contains("__");
}
};
for (File dir : geocodeDirs) {
@@ -751,7 +736,7 @@ public class cgData {
db.execSQL("drop table if exists " + dbTableTrackables);
}
- public String[] allDetailedThere() {
+ public static String[] getRecentGeocodesForSearch() {
init();
Cursor cursor = null;
@@ -795,7 +780,7 @@ public class cgData {
return list.toArray(new String[list.size()]);
}
- public boolean isThere(String geocode, String guid, boolean detailed, boolean checkTime) {
+ public static boolean isThere(String geocode, String guid, boolean detailed, boolean checkTime) {
init();
Cursor cursor = null;
@@ -878,7 +863,7 @@ public class cgData {
}
/** is cache stored in one of the lists (not only temporary) */
- public boolean isOffline(String geocode, String guid) {
+ public static boolean isOffline(String geocode, String guid) {
if (StringUtils.isBlank(geocode) && StringUtils.isBlank(guid)) {
return false;
}
@@ -888,11 +873,11 @@ public class cgData {
final SQLiteStatement listId;
final String value;
if (StringUtils.isNotBlank(geocode)) {
- listId = getStatementListIdFromGeocode();
+ listId = PreparedStatements.getListIdOfGeocode();
value = geocode;
}
else {
- listId = getStatementListIdFromGuid();
+ listId = PreparedStatements.getListIdOfGuid();
value = guid;
}
synchronized (listId) {
@@ -908,14 +893,14 @@ public class cgData {
return false;
}
- public String getGeocodeForGuid(String guid) {
+ public static String getGeocodeForGuid(String guid) {
if (StringUtils.isBlank(guid)) {
return null;
}
init();
try {
- final SQLiteStatement description = getStatementGeocode();
+ final SQLiteStatement description = PreparedStatements.getGeocodeOfGuid();
synchronized (description) {
description.bindString(1, guid);
return description.simpleQueryForString();
@@ -929,14 +914,14 @@ public class cgData {
return null;
}
- public String getCacheidForGeocode(String geocode) {
+ public static String getCacheidForGeocode(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
init();
try {
- final SQLiteStatement description = getStatementCacheId();
+ final SQLiteStatement description = PreparedStatements.getCacheIdOfGeocode();
synchronized (description) {
description.bindString(1, geocode);
return description.simpleQueryForString();
@@ -959,7 +944,7 @@ public class cgData {
*
* @return true = cache saved successfully to the CacheCache/DB
*/
- public boolean saveCache(cgCache cache, EnumSet<LoadFlags.SaveFlag> saveFlags) {
+ public static boolean saveCache(cgCache cache, EnumSet<LoadFlags.SaveFlag> saveFlags) {
if (cache == null) {
throw new IllegalArgumentException("cache must not be null");
}
@@ -1062,7 +1047,7 @@ public class cgData {
database.setTransactionSuccessful();
result = true;
} catch (Exception e) {
- // nothing
+ Log.e("SaveCache", e);
} finally {
database.endTransaction();
}
@@ -1070,25 +1055,21 @@ public class cgData {
return result;
}
- private void saveAttributesWithoutTransaction(final cgCache cache) {
+ private static void saveAttributesWithoutTransaction(final cgCache cache) {
String geocode = cache.getGeocode();
database.delete(dbTableAttributes, "geocode = ?", new String[]{geocode});
- if (cache.getAttributes().isNotEmpty()) {
-
- InsertHelper helper = new InsertHelper(database, dbTableAttributes);
- long timeStamp = System.currentTimeMillis();
-
- for (String attribute : cache.getAttributes()) {
- helper.prepareForInsert();
-
- helper.bind(ATTRIBUTES_GEOCODE, geocode);
- helper.bind(ATTRIBUTES_UPDATED, timeStamp);
- helper.bind(ATTRIBUTES_ATTRIBUTE, attribute);
+ if (cache.getAttributes().isEmpty()) {
+ return;
+ }
+ SQLiteStatement statement = PreparedStatements.getInsertAttribute();
+ final long timestamp = System.currentTimeMillis();
+ for (String attribute : cache.getAttributes()) {
+ statement.bindString(1, geocode);
+ statement.bindLong(2, timestamp);
+ statement.bindString(3, attribute);
- helper.execute();
- }
- helper.close();
+ statement.executeInsert();
}
}
@@ -1098,16 +1079,14 @@ public class cgData {
* @param destination
* a destination to save
*/
- public void saveSearchedDestination(final Destination destination) {
+ public static void saveSearchedDestination(final Destination destination) {
init();
database.beginTransaction();
try {
- ContentValues values = new ContentValues();
- values.put("date", destination.getDate());
- putCoords(values, destination.getCoords());
- database.insert(dbTableSearchDestionationHistory, null, values);
+ SQLiteStatement insertDestination = PreparedStatements.getInsertSearchDestination(destination);
+ insertDestination.executeInsert();
database.setTransactionSuccessful();
} catch (Exception e) {
Log.e("Updating searchedDestinations db failed", e);
@@ -1116,7 +1095,7 @@ public class cgData {
}
}
- public boolean saveWaypoints(final cgCache cache) {
+ public static boolean saveWaypoints(final cgCache cache) {
boolean result = false;
init();
database.beginTransaction();
@@ -1133,7 +1112,7 @@ public class cgData {
return result;
}
- private void saveOriginalWaypointsWithoutTransaction(final cgCache cache) {
+ private static void saveOriginalWaypointsWithoutTransaction(final cgCache cache) {
String geocode = cache.getGeocode();
database.delete(dbTableWaypoints, "geocode = ? and type <> ? and own = 0", new String[]{geocode, "own"});
@@ -1196,7 +1175,7 @@ public class cgData {
return new Geopoint(cursor.getDouble(indexLat), cursor.getDouble(indexLon));
}
- public boolean saveWaypoint(int id, String geocode, cgWaypoint waypoint) {
+ private static boolean saveWaypointInternal(int id, String geocode, cgWaypoint waypoint) {
if ((StringUtils.isBlank(geocode) && id <= 0) || waypoint == null) {
return false;
}
@@ -1234,7 +1213,7 @@ public class cgData {
return ok;
}
- public boolean deleteWaypoint(int id) {
+ public static boolean deleteWaypoint(int id) {
if (id == 0) {
return false;
}
@@ -1244,28 +1223,32 @@ public class cgData {
return database.delete(dbTableWaypoints, "_id = " + id, null) > 0;
}
- private void saveSpoilersWithoutTransaction(final cgCache cache) {
+ private static void saveSpoilersWithoutTransaction(final cgCache cache) {
String geocode = cache.getGeocode();
database.delete(dbTableSpoilers, "geocode = ?", new String[]{geocode});
List<cgImage> spoilers = cache.getSpoilers();
if (CollectionUtils.isNotEmpty(spoilers)) {
- ContentValues values = new ContentValues();
- long timeStamp = System.currentTimeMillis();
+ SQLiteStatement insertSpoiler = PreparedStatements.getInsertSpoiler();
+ final 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());
-
- database.insert(dbTableSpoilers, null, values);
+ insertSpoiler.bindString(1, geocode);
+ insertSpoiler.bindLong(2, timestamp);
+ insertSpoiler.bindString(3, spoiler.getUrl());
+ insertSpoiler.bindString(4, spoiler.getTitle());
+ final String description = spoiler.getDescription();
+ if (description != null) {
+ insertSpoiler.bindString(5, description);
+ }
+ else {
+ insertSpoiler.bindNull(5);
+ }
+ insertSpoiler.executeInsert();
}
}
}
- private void saveLogsWithoutTransaction(final String geocode, final Iterable<LogEntry> logs) {
+ private static void saveLogsWithoutTransaction(final String geocode, final Iterable<LogEntry> logs) {
// TODO delete logimages referring these logs
database.delete(dbTableLogs, "geocode = ?", new String[]{geocode});
@@ -1273,59 +1256,51 @@ public class cgData {
return;
}
- InsertHelper helper = new InsertHelper(database, dbTableLogs);
- long timeStamp = System.currentTimeMillis();
+ SQLiteStatement insertLog = PreparedStatements.getInsertLog();
+ final 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();
-
+ insertLog.bindString(1, geocode);
+ insertLog.bindLong(2, timestamp);
+ insertLog.bindLong(3, log.type.id);
+ insertLog.bindString(4, log.author);
+ insertLog.bindString(5, log.log);
+ insertLog.bindLong(6, log.date);
+ insertLog.bindLong(7, log.found);
+ insertLog.bindLong(8, log.friend ? 1 : 0);
+ long logId = insertLog.executeInsert();
if (log.hasLogImages()) {
- ContentValues values = new ContentValues();
+ SQLiteStatement insertImage = PreparedStatements.getInsertLogImage();
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);
+ insertImage.bindLong(1, logId);
+ insertImage.bindString(2, img.getTitle());
+ insertImage.bindString(3, img.getUrl());
+ insertImage.executeInsert();
}
}
}
- helper.close();
}
- private void saveLogCountsWithoutTransaction(final cgCache cache) {
+ private static void saveLogCountsWithoutTransaction(final cgCache cache) {
String geocode = cache.getGeocode();
database.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();
- long timeStamp = System.currentTimeMillis();
+ SQLiteStatement insertLogCounts = PreparedStatements.getInsertLogCounts();
+ final long timestamp = System.currentTimeMillis();
for (Entry<LogType, Integer> pair : logCountsItems) {
- values.clear();
- values.put("geocode", geocode);
- values.put("updated", timeStamp);
- values.put("type", pair.getKey().id);
- values.put("count", pair.getValue());
+ insertLogCounts.bindString(1, geocode);
+ insertLogCounts.bindLong(2, timestamp);
+ insertLogCounts.bindLong(3, pair.getKey().id);
+ insertLogCounts.bindLong(4, pair.getValue());
- database.insert(dbTableLogCount, null, values);
+ insertLogCounts.executeInsert();
}
}
}
- public boolean saveTrackable(final cgTrackable trackable) {
+ public static boolean saveTrackable(final cgTrackable trackable) {
init();
database.beginTransaction();
@@ -1339,7 +1314,7 @@ public class cgData {
return true;
}
- private void saveInventoryWithoutTransaction(final String geocode, final List<cgTrackable> trackables) {
+ private static void saveInventoryWithoutTransaction(final String geocode, final List<cgTrackable> trackables) {
if (geocode != null) {
database.delete(dbTableTrackables, "geocode = ?", new String[]{geocode});
}
@@ -1376,7 +1351,7 @@ public class cgData {
}
}
- public Viewport getBounds(final Set<String> geocodes) {
+ public static Viewport getBounds(final Set<String> geocodes) {
if (CollectionUtils.isEmpty(geocodes)) {
return null;
}
@@ -1392,7 +1367,7 @@ public class cgData {
* The Geocode GCXXXX
* @return the loaded cache (if found). Can be null
*/
- public cgCache loadCache(final String geocode, final EnumSet<LoadFlag> loadFlags) {
+ public static cgCache loadCache(final String geocode, final EnumSet<LoadFlag> loadFlags) {
if (StringUtils.isBlank(geocode)) {
throw new IllegalArgumentException("geocode must not be empty");
}
@@ -1407,7 +1382,7 @@ public class cgData {
* @param geocodes
* @return Set of loaded caches. Never null.
*/
- public Set<cgCache> loadCaches(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
+ public static Set<cgCache> loadCaches(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
if (CollectionUtils.isEmpty(geocodes)) {
return new HashSet<cgCache>();
}
@@ -1463,7 +1438,7 @@ public class cgData {
* @param loadFlags
* @return Set of loaded caches. Never null.
*/
- private Set<cgCache> loadCachesFromGeocodes(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
+ private static Set<cgCache> loadCachesFromGeocodes(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
if (CollectionUtils.isEmpty(geocodes)) {
return Collections.emptySet();
}
@@ -1689,7 +1664,7 @@ public class cgData {
return cache;
}
- public List<String> loadAttributes(String geocode) {
+ public static List<String> loadAttributes(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -1724,7 +1699,7 @@ public class cgData {
return attributes;
}
- public cgWaypoint loadWaypoint(int id) {
+ public static cgWaypoint loadWaypoint(int id) {
if (id == 0) {
return null;
}
@@ -1758,7 +1733,7 @@ public class cgData {
return waypoint;
}
- public List<cgWaypoint> loadWaypoints(final String geocode) {
+ public static List<cgWaypoint> loadWaypoints(final String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -1812,7 +1787,7 @@ public class cgData {
return waypoint;
}
- private List<cgImage> loadSpoilers(String geocode) {
+ private static List<cgImage> loadSpoilers(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -1857,7 +1832,7 @@ public class cgData {
*
* @return A list of previously entered destinations or an empty list.
*/
- public List<Destination> loadHistoryOfSearchedLocations() {
+ public static List<Destination> loadHistoryOfSearchedLocations() {
init();
Cursor cursor = database.query(dbTableSearchDestionationHistory,
@@ -1896,7 +1871,7 @@ public class cgData {
return destinations;
}
- public boolean clearSearchedDestinations() {
+ public static boolean clearSearchedDestinations() {
boolean success = true;
init();
database.beginTransaction();
@@ -1914,7 +1889,7 @@ public class cgData {
return success;
}
- public List<LogEntry> loadLogs(String geocode) {
+ public static List<LogEntry> loadLogs(String geocode) {
List<LogEntry> logs = new ArrayList<LogEntry>();
if (StringUtils.isBlank(geocode)) {
@@ -1967,7 +1942,7 @@ public class cgData {
return logs;
}
- public Map<LogType, Integer> loadLogCounts(String geocode) {
+ public static Map<LogType, Integer> loadLogCounts(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -2006,7 +1981,7 @@ public class cgData {
return logCounts;
}
- private List<cgTrackable> loadInventory(String geocode) {
+ private static List<cgTrackable> loadInventory(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -2042,7 +2017,7 @@ public class cgData {
return trackables;
}
- public cgTrackable loadTrackable(String geocode) {
+ public static cgTrackable loadTrackable(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -2073,7 +2048,7 @@ public class cgData {
return trackable;
}
- private cgTrackable createTrackableFromDatabaseContent(Cursor cursor) {
+ private static cgTrackable createTrackableFromDatabaseContent(Cursor cursor) {
cgTrackable trackable = new cgTrackable();
trackable.setGeocode(cursor.getString(cursor.getColumnIndex("tbcode")));
trackable.setGuid(cursor.getString(cursor.getColumnIndex("guid")));
@@ -2095,84 +2070,65 @@ public class cgData {
}
/**
- * Number of caches stored. The number is shown on the starting activity of c:geo
+ * Number of caches stored for a given type and/or list
*
- * @param detailedOnly
* @param cacheType
* @param list
* @return
*/
- public int getAllStoredCachesCount(final boolean detailedOnly, final CacheType cacheType, final int list) {
+ public static int getAllStoredCachesCount(final CacheType cacheType, final int list) {
if (cacheType == null) {
throw new IllegalArgumentException("cacheType must not be null");
}
- init();
-
- String listSql;
- String listSqlW;
- if (list == 0) {
- listSql = " where reason >= 1";
- listSqlW = " and reason >= 1";
- } else if (list >= 1) {
- listSql = " where reason = " + list;
- listSqlW = " and reason = " + list;
- } else {
- return 0;
+ if (list < 0) {
+ throw new IllegalArgumentException("list must be >= 0");
}
+ init();
- int count = 0;
try {
- String sql;
- if (!detailedOnly) {
- if (cacheType == CacheType.ALL) {
- sql = "select count(_id) from " + dbTableCaches + listSql;
- } else {
- sql = "select count(_id) from " + dbTableCaches + " where type = " + DatabaseUtils.sqlEscapeString(cacheType.id) + listSqlW;
- }
- } else {
- if (cacheType == CacheType.ALL) {
- sql = "select count(_id) from " + dbTableCaches + " where detailed = 1" + listSqlW;
- } else {
- sql = "select count(_id) from " + dbTableCaches + " where detailed = 1 and type = " + DatabaseUtils.sqlEscapeString(cacheType.id) + listSqlW;
- }
+ StringBuilder sql = new StringBuilder("select count(_id) from " + dbTableCaches + " where detailed = 1");
+ if (cacheType != CacheType.ALL) {
+ sql.append(" and type = " + DatabaseUtils.sqlEscapeString(cacheType.id));
+ }
+ if (list == 0) {
+ sql.append(" and reason > 0");
+ } else if (list >= 1) {
+ sql.append(" and reason = " + list);
}
- SQLiteStatement compiledStmnt = database.compileStatement(sql);
- count = (int) compiledStmnt.simpleQueryForLong();
- compiledStmnt.close();
+
+ String key = "CountCaches_" + cacheType.id + "_" + Integer.toString(list);
+
+ SQLiteStatement compiledStmnt = PreparedStatements.getStatement(key, sql.toString());
+ return (int) compiledStmnt.simpleQueryForLong();
} catch (Exception e) {
Log.e("cgData.loadAllStoredCachesCount: " + e.toString());
}
- return count;
+ return 0;
}
- public int getAllHistoricCachesCount() {
+ public static int getAllHistoryCachesCount() {
init();
- int count = 0;
-
try {
- SQLiteStatement sqlCount = database.compileStatement("select count(_id) from " + dbTableCaches + " where visiteddate > 0");
- count = (int) sqlCount.simpleQueryForLong();
- sqlCount.close();
+ return (int) PreparedStatements.getCountHistoryCaches().simpleQueryForLong();
} catch (Exception e) {
Log.e("cgData.getAllHistoricCachesCount: " + e.toString());
}
- return count;
+ return 0;
}
/**
* Return a batch of stored geocodes.
*
- * @param detailedOnly
* @param coords
* the current coordinates to sort by distance, or null to sort by geocode
* @param cacheType
* @param listId
* @return
*/
- public Set<String> loadBatchOfStoredGeocodes(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int listId) {
+ private static Set<String> loadBatchOfStoredGeocodes(final Geopoint coords, final CacheType cacheType, final int listId) {
if (cacheType == null) {
throw new IllegalArgumentException("cacheType must not be null");
}
@@ -2184,10 +2140,7 @@ public class cgData {
specifySql.append("reason ");
specifySql.append(listId != StoredList.ALL_LIST_ID ? "=" + Math.max(listId, 1) : ">= " + StoredList.STANDARD_LIST_ID);
-
- if (detailedOnly) {
- specifySql.append(" and detailed = 1 ");
- }
+ specifySql.append(" and detailed = 1 ");
if (cacheType != CacheType.ALL) {
specifySql.append(" and type = ");
@@ -2235,7 +2188,7 @@ public class cgData {
return geocodes;
}
- public Set<String> loadBatchOfHistoricGeocodes(final boolean detailedOnly, final CacheType cacheType) {
+ private static Set<String> loadBatchOfHistoricGeocodes(final boolean detailedOnly, final CacheType cacheType) {
init();
Set<String> geocodes = new HashSet<String>();
@@ -2285,12 +2238,12 @@ public class cgData {
}
/** Retrieve all stored caches from DB */
- public Set<String> loadCachedInViewport(final Viewport viewport, final CacheType cacheType) {
+ public static SearchResult loadCachedInViewport(final Viewport viewport, final CacheType cacheType) {
return loadInViewport(false, viewport, cacheType);
}
/** Retrieve stored caches from DB with listId >= 1 */
- public Set<String> loadStoredInViewport(final Viewport viewport, final CacheType cacheType) {
+ public static SearchResult loadStoredInViewport(final Viewport viewport, final CacheType cacheType) {
return loadInViewport(true, viewport, cacheType);
}
@@ -2306,7 +2259,7 @@ public class cgData {
* @param cacheType
* @return Set with geocodes
*/
- private Set<String> loadInViewport(final boolean stored, final Viewport viewport, final CacheType cacheType) {
+ private static SearchResult loadInViewport(final boolean stored, final Viewport viewport, final CacheType cacheType) {
init();
final Set<String> geocodes = new HashSet<String>();
@@ -2353,11 +2306,11 @@ public class cgData {
Log.e("cgData.loadInViewport: " + e.toString());
}
- return geocodes;
+ return new SearchResult(geocodes);
}
/** delete caches from the DB store 3 days or more before */
- public void clean() {
+ public static void clean() {
clean(false);
}
@@ -2367,7 +2320,11 @@ public class cgData {
* @param more
* true = all caches false = caches stored 3 days or more before
*/
- public void clean(boolean more) {
+ public static void clean(boolean more) {
+ if (databaseCleaned) {
+ return;
+ }
+
init();
Log.d("Database clean: started");
@@ -2420,24 +2377,20 @@ public class cgData {
removeCaches(geocodes, LoadFlags.REMOVE_ALL);
}
- final SQLiteStatement countSql = database.compileStatement("select count(_id) from " + dbTableCaches + " where reason = 0");
- final int count = (int) countSql.simpleQueryForLong();
- countSql.close();
- Log.d("Database clean: " + count + " geocaches remaining for listId=0");
-
} catch (Exception e) {
Log.w("cgData.clean: " + e.toString());
}
Log.d("Database clean: finished");
+ databaseCleaned = true;
}
- public void removeAllFromCache() {
+ public static void removeAllFromCache() {
// clean up CacheCache
cacheCache.removeAllFromCache();
}
- public void removeCache(final String geocode, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
+ public static void removeCache(final String geocode, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
removeCaches(Collections.singleton(geocode), removeFlags);
}
@@ -2447,7 +2400,7 @@ public class cgData {
* @param geocodes
* list of geocodes to drop from cache
*/
- public void removeCaches(final Set<String> geocodes, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
+ public static void removeCaches(final Set<String> geocodes, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
if (CollectionUtils.isEmpty(geocodes)) {
return;
}
@@ -2494,7 +2447,7 @@ public class cgData {
}
}
- public boolean saveLogOffline(String geocode, Date date, LogType type, String log) {
+ public static boolean saveLogOffline(String geocode, Date date, LogType type, String log) {
if (StringUtils.isBlank(geocode)) {
Log.e("cgData.saveLogOffline: cannot log a blank geocode");
return false;
@@ -2521,7 +2474,7 @@ public class cgData {
return id != -1;
}
- public LogEntry loadLogOffline(String geocode) {
+ public static LogEntry loadLogOffline(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
@@ -2557,7 +2510,7 @@ public class cgData {
return log;
}
- public void clearLogOffline(String geocode) {
+ public static void clearLogOffline(String geocode) {
if (StringUtils.isBlank(geocode)) {
return;
}
@@ -2567,35 +2520,14 @@ public class cgData {
database.delete(dbTableLogsOffline, "geocode = ?", new String[]{geocode});
}
- private SQLiteStatement getStatementLogCount() {
- return getStatement("LogCountFromGeocode", "SELECT count(_id) FROM " + dbTableLogsOffline + " WHERE geocode = ?");
- }
-
- private synchronized SQLiteStatement getStatement(final String key, final String query) {
- SQLiteStatement statement = statements.get(key);
- if (statement == null) {
- statement = database.compileStatement(query);
- statements.put(key, statement);
- }
- return statement;
- }
-
- private SQLiteStatement getStatementCountStandardList() {
- return getStatement("CountStandardList", "SELECT count(_id) FROM " + dbTableCaches + " WHERE reason = " + StoredList.STANDARD_LIST_ID);
- }
-
- private SQLiteStatement getStatementCountAllLists() {
- return getStatement("CountAllLists", "SELECT count(_id) FROM " + dbTableCaches + " WHERE reason >= " + StoredList.STANDARD_LIST_ID);
- }
-
- public boolean hasLogOffline(final String geocode) {
+ public static boolean hasLogOffline(final String geocode) {
if (StringUtils.isBlank(geocode)) {
return false;
}
init();
try {
- final SQLiteStatement logCount = getStatementLogCount();
+ final SQLiteStatement logCount = PreparedStatements.getLogCountOfGeocode();
synchronized (logCount) {
logCount.bindString(1, geocode);
return logCount.simpleQueryForLong() > 0;
@@ -2607,7 +2539,7 @@ public class cgData {
return false;
}
- public void setVisitDate(List<String> geocodes, long visitedDate) {
+ private static void setVisitDate(List<String> geocodes, long visitedDate) {
if (geocodes.isEmpty()) {
return;
}
@@ -2616,11 +2548,12 @@ public class cgData {
database.beginTransaction();
try {
- ContentValues values = new ContentValues();
- values.put("visiteddate", visitedDate);
+ SQLiteStatement setVisit = PreparedStatements.getUpdateVisitDate();
for (String geocode : geocodes) {
- database.update(dbTableCaches, values, "geocode = ?", new String[]{geocode});
+ setVisit.bindLong(1, visitedDate);
+ setVisit.bindString(2, geocode);
+ setVisit.execute();
}
database.setTransactionSuccessful();
} finally {
@@ -2628,11 +2561,12 @@ public class cgData {
}
}
- public List<StoredList> getLists(Resources res) {
+ public static List<StoredList> getLists() {
init();
+ Resources res = cgeoapplication.getInstance().getResources();
List<StoredList> lists = new ArrayList<StoredList>();
- lists.add(new StoredList(StoredList.STANDARD_LIST_ID, res.getString(R.string.list_inbox), (int) getStatementCountStandardList().simpleQueryForLong()));
+ lists.add(new StoredList(StoredList.STANDARD_LIST_ID, res.getString(R.string.list_inbox), (int) PreparedStatements.getCountCachesOnStandardList().simpleQueryForLong()));
try {
String query = "SELECT l._id as _id, l.title as title, COUNT(c._id) as count" +
@@ -2674,7 +2608,7 @@ public class cgData {
return result;
}
- public StoredList getList(int id, Resources res) {
+ public static StoredList getList(int id) {
init();
if (id >= customListIdOffset) {
Cursor cursor = database.query(
@@ -2691,18 +2625,23 @@ public class cgData {
}
}
+ Resources res = cgeoapplication.getInstance().getResources();
if (id == StoredList.ALL_LIST_ID) {
- return new StoredList(StoredList.ALL_LIST_ID, res.getString(R.string.list_all_lists), (int) getStatementCountAllLists().simpleQueryForLong());
+ return new StoredList(StoredList.ALL_LIST_ID, res.getString(R.string.list_all_lists), getAllCachesCount());
}
// fall back to standard list in case of invalid list id
if (id == StoredList.STANDARD_LIST_ID || id >= customListIdOffset) {
- return new StoredList(StoredList.STANDARD_LIST_ID, res.getString(R.string.list_inbox), (int) getStatementCountStandardList().simpleQueryForLong());
+ return new StoredList(StoredList.STANDARD_LIST_ID, res.getString(R.string.list_inbox), (int) PreparedStatements.getCountCachesOnStandardList().simpleQueryForLong());
}
return null;
}
+ public static int getAllCachesCount() {
+ return (int) PreparedStatements.getCountAllCaches().simpleQueryForLong();
+ }
+
/**
* Create a new list
*
@@ -2710,7 +2649,7 @@ public class cgData {
* Name
* @return new listId
*/
- public int createList(String name) {
+ public static int createList(String name) {
int id = -1;
if (StringUtils.isBlank(name)) {
return id;
@@ -2740,7 +2679,7 @@ public class cgData {
* New name of list
* @return Number of lists changed
*/
- public int renameList(final int listId, final String name) {
+ public static int renameList(final int listId, final String name) {
if (StringUtils.isBlank(name) || StoredList.STANDARD_LIST_ID == listId) {
return 0;
}
@@ -2769,23 +2708,23 @@ public class cgData {
* @param listId
* @return true if the list got deleted, false else
*/
- public boolean removeList(int listId) {
- boolean status = false;
+ public static boolean removeList(int listId) {
if (listId < customListIdOffset) {
- return status;
+ return false;
}
init();
+ boolean status = false;
database.beginTransaction();
try {
int cnt = database.delete(dbTableLists, "_id = " + (listId - customListIdOffset), null);
if (cnt > 0) {
// move caches from deleted list to standard list
- ContentValues values = new ContentValues();
- values.put("reason", StoredList.STANDARD_LIST_ID);
- database.update(dbTableCaches, values, "reason = " + listId, null);
+ SQLiteStatement moveToStandard = PreparedStatements.getMoveToStandardList();
+ moveToStandard.bindLong(1, listId);
+ moveToStandard.execute();
status = true;
}
@@ -2798,7 +2737,7 @@ public class cgData {
return status;
}
- public void moveToList(final List<cgCache> caches, final int listId) {
+ public static void moveToList(final List<cgCache> caches, final int listId) {
if (listId == StoredList.ALL_LIST_ID) {
return;
}
@@ -2807,13 +2746,14 @@ public class cgData {
}
init();
- final ContentValues values = new ContentValues();
- values.put("reason", listId);
+ SQLiteStatement move = PreparedStatements.getMoveToList();
database.beginTransaction();
try {
for (cgCache cache : caches) {
- database.update(dbTableCaches, values, "geocode = ?", new String[]{cache.getGeocode()});
+ move.bindLong(1, listId);
+ move.bindString(2, cache.getGeocode());
+ move.execute();
cache.setListId(listId);
}
database.setTransactionSuccessful();
@@ -2822,61 +2762,39 @@ public class cgData {
}
}
- public synchronized boolean status() {
+ public static boolean isInitialized() {
return database != null;
-
}
- public boolean removeSearchedDestination(Destination destination) {
- boolean success = true;
+ public static boolean removeSearchedDestination(Destination destination) {
if (destination == null) {
- success = false;
- } else {
- init();
-
- database.beginTransaction();
- try {
- database.delete(dbTableSearchDestionationHistory, "_id = " + destination.getId(), null);
- database.setTransactionSuccessful();
- } catch (Exception e) {
- Log.e("Unable to remove searched destination", e);
- success = false;
- } finally {
- database.endTransaction();
- }
+ return false;
}
+ init();
- return success;
- }
-
- private SQLiteStatement getStatementDescription() {
- return getStatement("descriptionFromGeocode", "SELECT description FROM " + dbTableCaches + " WHERE geocode = ?");
- }
-
- private SQLiteStatement getStatementListIdFromGeocode() {
- return getStatement("listFromGeocode", "SELECT reason FROM " + dbTableCaches + " WHERE geocode = ?");
- }
-
- private SQLiteStatement getStatementListIdFromGuid() {
- return getStatement("listFromGeocode", "SELECT reason FROM " + dbTableCaches + " WHERE guid = ?");
- }
-
- private SQLiteStatement getStatementCacheId() {
- return getStatement("cacheIdFromGeocode", "SELECT cacheid FROM " + dbTableCaches + " WHERE geocode = ?");
- }
+ boolean result = false;
+ database.beginTransaction();
+ try {
+ database.delete(dbTableSearchDestionationHistory, "_id = " + destination.getId(), null);
+ database.setTransactionSuccessful();
+ result = true;
+ } catch (Exception e) {
+ Log.e("Unable to remove searched destination", e);
+ } finally {
+ database.endTransaction();
+ }
- private SQLiteStatement getStatementGeocode() {
- return getStatement("geocodeFromGuid", "SELECT geocode FROM " + dbTableCaches + " WHERE guid = ?");
+ return result;
}
- public String getCacheDescription(String geocode) {
+ public static String getCacheDescription(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
init();
try {
- final SQLiteStatement description = getStatementDescription();
+ final SQLiteStatement description = PreparedStatements.getDescriptionOfGeocode();
synchronized (description) {
description.bindString(1, geocode);
return description.simpleQueryForString();
@@ -2933,7 +2851,7 @@ public class cgData {
* @return
*/
- public Set<cgWaypoint> loadWaypoints(final Viewport viewport, boolean excludeMine, boolean excludeDisabled, CacheType type) {
+ public static Set<cgWaypoint> loadWaypoints(final Viewport viewport, boolean excludeMine, boolean excludeDisabled, CacheType type) {
final StringBuilder where = new StringBuilder(buildCoordinateWhere(dbTableWaypoints, viewport));
if (excludeMine) {
where.append(" and ").append(dbTableCaches).append(".own == 0 and ").append(dbTableCaches).append(".found == 0");
@@ -2942,7 +2860,7 @@ public class cgData {
where.append(" and ").append(dbTableCaches).append(".disabled == 0");
}
if (type != CacheType.ALL) {
- where.append(" and ").append(dbTableCaches).append(".type == '").append(type.id).append("'");
+ where.append(" and ").append(dbTableCaches).append(".type == '").append(type.id).append('\'');
}
init();
@@ -2968,7 +2886,7 @@ public class cgData {
}
}
- public String[] getTrackableCodes() {
+ public static String[] getTrackableCodes() {
init();
final Cursor cursor = database.query(
@@ -2997,4 +2915,154 @@ public class cgData {
return list.toArray(new String[list.size()]);
}
+ public static boolean saveChangedCache(cgCache cache) {
+ return cgData.saveCache(cache, cache.getStorageLocation().contains(StorageLocation.DATABASE) ? LoadFlags.SAVE_ALL : EnumSet.of(SaveFlag.SAVE_CACHE));
+ }
+
+ private static class PreparedStatements {
+
+ private static HashMap<String, SQLiteStatement> statements = new HashMap<String, SQLiteStatement>();
+
+ private PreparedStatements() {
+ // utility class
+ }
+
+ public static SQLiteStatement getMoveToStandardList() {
+ return getStatement("MoveToStandardList", "UPDATE " + dbTableCaches + " SET reason = " + StoredList.STANDARD_LIST_ID + " WHERE reason = ?");
+ }
+
+ public static SQLiteStatement getMoveToList() {
+ return getStatement("MoveToList", "UPDATE " + dbTableCaches + " SET reason = ? WHERE geocode = ?");
+ }
+
+ public static SQLiteStatement getUpdateVisitDate() {
+ return getStatement("UpdateVisitDate", "UPDATE " + dbTableCaches + " SET visiteddate = ? WHERE geocode = ?");
+ }
+
+ public static SQLiteStatement getInsertLogImage() {
+ return getStatement("InsertLogImage", "INSERT INTO " + dbTableLogImages + " (log_id, title, url) VALUES (?, ?, ?)");
+ }
+
+ public static SQLiteStatement getInsertLogCounts() {
+ return getStatement("InsertLogCounts", "INSERT INTO " + dbTableLogCount + " (geocode, updated, type, count) VALUES (?, ?, ?, ?)");
+ }
+
+ public static SQLiteStatement getInsertSpoiler() {
+ return getStatement("InsertSpoiler", "INSERT INTO " + dbTableSpoilers + " (geocode, updated, url, title, description) VALUES (?, ?, ?, ?, ?)");
+ }
+
+ public static SQLiteStatement getInsertSearchDestination(Destination destination) {
+ final SQLiteStatement statement = getStatement("InsertSearch", "INSERT INTO " + dbTableSearchDestionationHistory + " (date, latitude, longitude) VALUES (?, ?, ?)");
+ statement.bindLong(1, destination.getDate());
+ final Geopoint coords = destination.getCoords();
+ statement.bindDouble(2, coords.getLatitude());
+ statement.bindDouble(3, coords.getLongitude());
+ return statement;
+ }
+
+ private static void clearPreparedStatements() {
+ for (SQLiteStatement statement : statements.values()) {
+ statement.close();
+ }
+ statements.clear();
+ }
+
+ private static synchronized SQLiteStatement getStatement(final String key, final String query) {
+ SQLiteStatement statement = statements.get(key);
+ if (statement == null) {
+ Log.i("Compiling " + key);
+ statement = database.compileStatement(query);
+ statements.put(key, statement);
+ }
+ return statement;
+ }
+
+ public static SQLiteStatement getCountHistoryCaches() {
+ return getStatement("HistoryCount", "select count(_id) from " + dbTableCaches + " where visiteddate > 0");
+ }
+
+ private static SQLiteStatement getLogCountOfGeocode() {
+ return getStatement("LogCountFromGeocode", "SELECT count(_id) FROM " + cgData.dbTableLogsOffline + " WHERE geocode = ?");
+ }
+
+ private static SQLiteStatement getCountCachesOnStandardList() {
+ return getStatement("CountStandardList", "SELECT count(_id) FROM " + dbTableCaches + " WHERE reason = " + StoredList.STANDARD_LIST_ID);
+ }
+
+ private static SQLiteStatement getCountAllCaches() {
+ return getStatement("CountAllLists", "SELECT count(_id) FROM " + dbTableCaches + " WHERE reason >= " + StoredList.STANDARD_LIST_ID);
+ }
+
+ private static SQLiteStatement getInsertLog() {
+ return getStatement("InsertLog", "INSERT INTO " + dbTableLogs + " (geocode, updated, type, author, log, date, found, friend) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
+ }
+
+ private static SQLiteStatement getInsertAttribute() {
+ return getStatement("InsertAttribute", "INSERT INTO " + dbTableAttributes + " (geocode, updated, attribute) VALUES (?, ?, ?)");
+ }
+
+ private static SQLiteStatement getDescriptionOfGeocode() {
+ return getStatement("descriptionFromGeocode", "SELECT description FROM " + dbTableCaches + " WHERE geocode = ?");
+ }
+
+ private static SQLiteStatement getListIdOfGeocode() {
+ return getStatement("listFromGeocode", "SELECT reason FROM " + dbTableCaches + " WHERE geocode = ?");
+ }
+
+ private static SQLiteStatement getListIdOfGuid() {
+ return getStatement("listFromGeocode", "SELECT reason FROM " + dbTableCaches + " WHERE guid = ?");
+ }
+
+ private static SQLiteStatement getCacheIdOfGeocode() {
+ return getStatement("cacheIdFromGeocode", "SELECT cacheid FROM " + dbTableCaches + " WHERE geocode = ?");
+ }
+
+ private static SQLiteStatement getGeocodeOfGuid() {
+ return getStatement("geocodeFromGuid", "SELECT geocode FROM " + dbTableCaches + " WHERE guid = ?");
+ }
+
+ }
+
+ public static void saveVisitDate(final String geocode) {
+ setVisitDate(Collections.singletonList(geocode), System.currentTimeMillis());
+ }
+
+ public static void markDropped(List<cgCache> caches) {
+ moveToList(caches, StoredList.TEMPORARY_LIST_ID);
+ }
+
+ public static Viewport getBounds(String geocode) {
+ if (geocode == null) {
+ return null;
+ }
+
+ return cgData.getBounds(Collections.singleton(geocode));
+ }
+
+ public static void clearVisitDate(List<cgCache> caches) {
+ ArrayList<String> geocodes = new ArrayList<String>(caches.size());
+ for (cgCache cache : caches) {
+ geocodes.add(cache.getGeocode());
+ }
+ setVisitDate(geocodes, 0);
+ }
+
+ public static SearchResult getBatchOfStoredCaches(Geopoint coords, CacheType cacheType, int listId) {
+ final Set<String> geocodes = cgData.loadBatchOfStoredGeocodes(coords, cacheType, listId);
+ return new SearchResult(geocodes, cgData.getAllStoredCachesCount(cacheType, listId));
+ }
+
+ public static SearchResult getHistoryOfCaches(boolean detailedOnly, CacheType cacheType) {
+ final Set<String> geocodes = cgData.loadBatchOfHistoricGeocodes(detailedOnly, cacheType);
+ return new SearchResult(geocodes, cgData.getAllHistoryCachesCount());
+ }
+
+ public static boolean saveWaypoint(int id, String geocode, cgWaypoint waypoint) {
+ if (cgData.saveWaypointInternal(id, geocode, waypoint)) {
+ cgData.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE));
+ return true;
+ }
+ return false;
+ }
+
}