aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/DataStore.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/DataStore.java')
-rw-r--r--main/src/cgeo/geocaching/DataStore.java132
1 files changed, 67 insertions, 65 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java
index eb4c32e..32a4b64 100644
--- a/main/src/cgeo/geocaching/DataStore.java
+++ b/main/src/cgeo/geocaching/DataStore.java
@@ -20,8 +20,6 @@ import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.dialog.Dialogs;
import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.Log;
-import cgeo.geocaching.utils.MiscUtils;
-import cgeo.geocaching.utils.RxUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
@@ -29,9 +27,11 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
+import rx.android.observables.AndroidObservable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
+import rx.schedulers.Schedulers;
import rx.util.async.Async;
import android.app.Activity;
@@ -395,7 +395,7 @@ public class DataStore {
*/
public static void moveDatabase(final Activity fromActivity) {
final ProgressDialog dialog = ProgressDialog.show(fromActivity, fromActivity.getString(R.string.init_dbmove_dbmove), fromActivity.getString(R.string.init_dbmove_running), true, false);
- RxUtils.subscribeOnIOThenUI(Async.fromCallable(new Func0<Boolean>() {
+ AndroidObservable.bindActivity(fromActivity, Async.fromCallable(new Func0<Boolean>() {
@Override
public Boolean call() {
if (!LocalStorage.isExternalStorageAvailable()) {
@@ -420,14 +420,14 @@ public class DataStore {
init();
return true;
}
- }), new Action1<Boolean>() {
+ })).subscribe(new Action1<Boolean>() {
@Override
public void call(final Boolean success) {
dialog.dismiss();
final String message = success ? fromActivity.getString(R.string.init_dbmove_success) : fromActivity.getString(R.string.init_dbmove_failed);
Dialogs.message(fromActivity, R.string.init_dbmove_dbmove, message);
}
- });
+ }, Schedulers.io());
}
private static File databasePath(final boolean internal) {
@@ -1197,7 +1197,6 @@ public class DataStore {
saveAttributesWithoutTransaction(cache);
saveWaypointsWithoutTransaction(cache);
saveSpoilersWithoutTransaction(cache);
- saveLogsWithoutTransaction(cache.getGeocode(), cache.getLogs());
saveLogCountsWithoutTransaction(cache);
saveInventoryWithoutTransaction(cache.getGeocode(), cache.getInventory());
@@ -1322,7 +1321,7 @@ public class DataStore {
*/
private static void removeOutdatedWaypointsOfCache(final @NonNull Geocache cache, final @NonNull Collection<String> remainingWaypointIds) {
final String idList = StringUtils.join(remainingWaypointIds, ',');
- database.delete(dbTableWaypoints, "geocode = ? AND _id NOT in (" + idList + ")", new String[]{cache.getGeocode()});
+ database.delete(dbTableWaypoints, "geocode = ? AND _id NOT in (" + idList + ")", new String[] { cache.getGeocode() });
}
/**
@@ -1430,7 +1429,7 @@ public class DataStore {
}
}
- private static void saveLogsWithoutTransaction(final String geocode, final List<LogEntry> logs) {
+ public static void saveLogsWithoutTransaction(final String geocode, final List<LogEntry> logs) {
// TODO delete logimages referring these logs
database.delete(dbTableLogs, "geocode = ?", new String[]{geocode});
@@ -1567,8 +1566,8 @@ public class DataStore {
return new HashSet<Geocache>();
}
- final Set<Geocache> result = new HashSet<Geocache>();
- final List<String> remaining = new LinkedList<String>(geocodes);
+ Set<Geocache> result = new HashSet<Geocache>();
+ Set<String> remaining = new HashSet<String>(geocodes);
if (loadFlags.contains(LoadFlag.LOAD_CACHE_BEFORE)) {
for (String geocode : new HashSet<String>(remaining)) {
@@ -1588,7 +1587,7 @@ public class DataStore {
loadFlags.contains(LoadFlag.LOAD_INVENTORY) ||
loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
- final Collection<Geocache> cachesFromDB = loadCachesFromGeocodes(remaining, loadFlags);
+ final Set<Geocache> cachesFromDB = loadCachesFromGeocodes(remaining, loadFlags);
result.addAll(cachesFromDB);
for (final Geocache cache : cachesFromDB) {
remaining.remove(cache.getGeocode());
@@ -1614,11 +1613,15 @@ public class DataStore {
/**
* Load caches.
*
- * @param allGeocodes
+ * @param geocodes
* @param loadFlags
* @return Set of loaded caches. Never null.
*/
- private static Collection<Geocache> loadCachesFromGeocodes(final List<String> allGeocodes, final EnumSet<LoadFlag> loadFlags) {
+ private static Set<Geocache> loadCachesFromGeocodes(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
+ if (CollectionUtils.isEmpty(geocodes)) {
+ return Collections.emptySet();
+ }
+
// 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();
@@ -1633,73 +1636,67 @@ public class DataStore {
}
query.append(" WHERE ").append(dbTableCaches).append('.');
+ query.append(DataStore.whereGeocodeIn(geocodes));
- final String queryBegin = query.toString();
- final List<Geocache> result = new ArrayList<Geocache>(allGeocodes.size());
-
- for (List<String> geocodes: MiscUtils.buffer(allGeocodes, 50)) {
- final Cursor cursor = database.rawQuery(queryBegin + DataStore.whereGeocodeIn(geocodes), null);
- try {
- final Set<Geocache> caches = new HashSet<Geocache>();
- int logIndex = -1;
+ Cursor cursor = database.rawQuery(query.toString(), null);
+ try {
+ final Set<Geocache> caches = new HashSet<Geocache>();
+ int logIndex = -1;
- while (cursor.moveToNext()) {
- final Geocache cache = DataStore.createCacheFromDatabaseContent(cursor);
+ while (cursor.moveToNext()) {
+ Geocache cache = DataStore.createCacheFromDatabaseContent(cursor);
- if (loadFlags.contains(LoadFlag.LOAD_ATTRIBUTES)) {
- cache.setAttributes(loadAttributes(cache.getGeocode()));
- }
+ if (loadFlags.contains(LoadFlag.LOAD_ATTRIBUTES)) {
+ cache.setAttributes(loadAttributes(cache.getGeocode()));
+ }
- if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) {
- final List<Waypoint> waypoints = loadWaypoints(cache.getGeocode());
- if (CollectionUtils.isNotEmpty(waypoints)) {
- cache.setWaypoints(waypoints, false);
- }
+ if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) {
+ final List<Waypoint> waypoints = loadWaypoints(cache.getGeocode());
+ if (CollectionUtils.isNotEmpty(waypoints)) {
+ cache.setWaypoints(waypoints, false);
}
+ }
- if (loadFlags.contains(LoadFlag.LOAD_SPOILERS)) {
- final List<Image> spoilers = loadSpoilers(cache.getGeocode());
- cache.setSpoilers(spoilers);
- }
+ if (loadFlags.contains(LoadFlag.LOAD_SPOILERS)) {
+ final List<Image> spoilers = loadSpoilers(cache.getGeocode());
+ cache.setSpoilers(spoilers);
+ }
- if (loadFlags.contains(LoadFlag.LOAD_LOGS)) {
- cache.setLogs(loadLogs(cache.getGeocode()));
- final Map<LogType, Integer> logCounts = loadLogCounts(cache.getGeocode());
- if (MapUtils.isNotEmpty(logCounts)) {
- cache.getLogCounts().clear();
- cache.getLogCounts().putAll(logCounts);
- }
+ if (loadFlags.contains(LoadFlag.LOAD_LOGS)) {
+ final Map<LogType, Integer> logCounts = loadLogCounts(cache.getGeocode());
+ if (MapUtils.isNotEmpty(logCounts)) {
+ cache.getLogCounts().clear();
+ cache.getLogCounts().putAll(logCounts);
}
+ }
- if (loadFlags.contains(LoadFlag.LOAD_INVENTORY)) {
- final List<Trackable> inventory = loadInventory(cache.getGeocode());
- if (CollectionUtils.isNotEmpty(inventory)) {
- if (cache.getInventory() == null) {
- cache.setInventory(new ArrayList<Trackable>());
- } else {
- cache.getInventory().clear();
- }
- cache.getInventory().addAll(inventory);
+ if (loadFlags.contains(LoadFlag.LOAD_INVENTORY)) {
+ final List<Trackable> inventory = loadInventory(cache.getGeocode());
+ if (CollectionUtils.isNotEmpty(inventory)) {
+ if (cache.getInventory() == null) {
+ cache.setInventory(new ArrayList<Trackable>());
+ } else {
+ cache.getInventory().clear();
}
+ cache.getInventory().addAll(inventory);
}
+ }
- if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
- if (logIndex < 0) {
- logIndex = cursor.getColumnIndex("log");
- }
- cache.setLogOffline(!cursor.isNull(logIndex));
+ if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
+ if (logIndex < 0) {
+ logIndex = cursor.getColumnIndex("log");
}
- cache.addStorageLocation(StorageLocation.DATABASE);
- cacheCache.putCacheInCache(cache);
-
- caches.add(cache);
+ cache.setLogOffline(!cursor.isNull(logIndex));
}
- result.addAll(caches);
- } finally {
- cursor.close();
+ cache.addStorageLocation(StorageLocation.DATABASE);
+ cacheCache.putCacheInCache(cache);
+
+ caches.add(cache);
}
+ return caches;
+ } finally {
+ cursor.close();
}
- return result;
}
@@ -1927,6 +1924,11 @@ public class DataStore {
return false;
}
+ /**
+ * @param geocode
+ * @return an immutable, non null list of logs
+ */
+ @NonNull
public static List<LogEntry> loadLogs(String geocode) {
List<LogEntry> logs = new ArrayList<LogEntry>();
@@ -1962,7 +1964,7 @@ public class DataStore {
cursor.close();
- return logs;
+ return Collections.unmodifiableList(logs);
}
public static Map<LogType, Integer> loadLogCounts(String geocode) {