aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-03-11 05:33:54 +0100
committerSamuel Tardieu <sam@rfc1149.net>2014-03-11 05:33:54 +0100
commita614e570900e5decc36290a6aa99d055e4ef77c5 (patch)
treeda6e0cc419873d9aebf830cab4a988edbee9501b
parent28696373b58e664d2f14d015698de42ac6eec8f0 (diff)
downloadcgeo-a614e570900e5decc36290a6aa99d055e4ef77c5.zip
cgeo-a614e570900e5decc36290a6aa99d055e4ef77c5.tar.gz
cgeo-a614e570900e5decc36290a6aa99d055e4ef77c5.tar.bz2
refactoring: factor out code for cache refreshing
-rw-r--r--main/src/cgeo/geocaching/CacheDetailActivity.java24
-rw-r--r--main/src/cgeo/geocaching/CacheListActivity.java7
-rw-r--r--main/src/cgeo/geocaching/CachePopup.java17
-rw-r--r--main/src/cgeo/geocaching/Geocache.java17
-rw-r--r--main/src/cgeo/geocaching/utils/CancellableHandler.java1
5 files changed, 22 insertions, 44 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java
index dd095f5..b125e14 100644
--- a/main/src/cgeo/geocaching/CacheDetailActivity.java
+++ b/main/src/cgeo/geocaching/CacheDetailActivity.java
@@ -889,8 +889,6 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
*/
private LinearLayout detailsList;
- // TODO Do we need this thread-references?
- private RefreshCacheThread refreshThread;
private Thread watchlistThread;
@Override
@@ -1063,27 +1061,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
progress.show(CacheDetailActivity.this, res.getString(R.string.cache_dialog_refresh_title), res.getString(R.string.cache_dialog_refresh_message), true, refreshCacheHandler.cancelMessage());
- if (refreshThread != null) {
- refreshThread.interrupt();
- }
-
- refreshThread = new RefreshCacheThread(refreshCacheHandler);
- refreshThread.start();
- }
- }
-
- private class RefreshCacheThread extends Thread {
- final private CancellableHandler handler;
-
- public RefreshCacheThread(final CancellableHandler handler) {
- this.handler = handler;
- }
-
- @Override
- public void run() {
- cache.refresh(cache.getListId(), handler);
- refreshThread = null;
- handler.sendEmptyMessage(0);
+ cache.refresh(cache.getListId(), refreshCacheHandler, Schedulers.io());
}
}
diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java
index f07c771..7e45477 100644
--- a/main/src/cgeo/geocaching/CacheListActivity.java
+++ b/main/src/cgeo/geocaching/CacheListActivity.java
@@ -36,6 +36,7 @@ import cgeo.geocaching.maps.CGeoMap;
import cgeo.geocaching.network.Cookies;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
+import cgeo.geocaching.sensors.GeoDirHandler;
import cgeo.geocaching.sensors.IGeoData;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.sorting.CacheComparator;
@@ -47,18 +48,16 @@ import cgeo.geocaching.ui.dialog.Dialogs;
import cgeo.geocaching.utils.AsyncTaskWithProgress;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.DateUtils;
-import cgeo.geocaching.sensors.GeoDirHandler;
import cgeo.geocaching.utils.Log;
import ch.boye.httpclientandroidlib.HttpResponse;
-
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
-
import rx.Subscription;
import rx.functions.Action1;
+import rx.schedulers.Schedulers;
import android.app.Activity;
import android.app.AlertDialog;
@@ -1142,7 +1141,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA
throw new InterruptedException("Stopped storing process.");
}
detailProgress++;
- cache.refresh(listIdLD, null);
+ cache.refreshSynchronous(listIdLD, null);
handler.sendEmptyMessage(cacheList.indexOf(cache));
} catch (final InterruptedException e) {
Log.i(e.getMessage());
diff --git a/main/src/cgeo/geocaching/CachePopup.java b/main/src/cgeo/geocaching/CachePopup.java
index c6c7c1c..38c6e65 100644
--- a/main/src/cgeo/geocaching/CachePopup.java
+++ b/main/src/cgeo/geocaching/CachePopup.java
@@ -12,6 +12,7 @@ import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import rx.functions.Action1;
+import rx.schedulers.Schedulers;
import android.content.Context;
import android.content.Intent;
@@ -159,21 +160,7 @@ public class CachePopup extends AbstractPopupActivity {
final StoreCacheHandler refreshCacheHandler = new StoreCacheHandler(R.string.cache_dialog_offline_save_message);
progress.show(CachePopup.this, res.getString(R.string.cache_dialog_refresh_title), res.getString(R.string.cache_dialog_refresh_message), true, refreshCacheHandler.cancelMessage());
- new RefreshCacheThread(refreshCacheHandler).start();
- }
- }
-
- private class RefreshCacheThread extends Thread {
- final private CancellableHandler handler;
-
- public RefreshCacheThread(final CancellableHandler handler) {
- this.handler = handler;
- }
-
- @Override
- public void run() {
- cache.refresh(cache.getListId(), handler);
- handler.sendEmptyMessage(0);
+ cache.refresh(cache.getListId(), refreshCacheHandler, Schedulers.io());
}
}
diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java
index 79b4425..f3c2ecd 100644
--- a/main/src/cgeo/geocaching/Geocache.java
+++ b/main/src/cgeo/geocaching/Geocache.java
@@ -33,13 +33,16 @@ import cgeo.geocaching.utils.MatcherWrapper;
import cgeo.geocaching.utils.UncertainProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.Nullable;
+import rx.Scheduler;
+import rx.Scheduler.Inner;
+import rx.Subscription;
+import rx.functions.Action1;
import android.app.Activity;
import android.content.Intent;
@@ -1495,7 +1498,17 @@ public class Geocache implements ICache, IWaypoint {
}
}
- public void refresh(int newListId, CancellableHandler handler) {
+ public Subscription refresh(final int newListId, final CancellableHandler handler, final Scheduler scheduler) {
+ return scheduler.schedule(new Action1<Inner>() {
+ @Override
+ public void call(final Inner inner) {
+ refreshSynchronous(newListId, handler);
+ handler.sendEmptyMessage(CancellableHandler.DONE);
+ }
+ });
+ }
+
+ public void refreshSynchronous(final int newListId, final CancellableHandler handler) {
DataStore.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE));
storeCache(null, geocode, newListId, true, handler);
}
diff --git a/main/src/cgeo/geocaching/utils/CancellableHandler.java b/main/src/cgeo/geocaching/utils/CancellableHandler.java
index 01fb568..3ed233a 100644
--- a/main/src/cgeo/geocaching/utils/CancellableHandler.java
+++ b/main/src/cgeo/geocaching/utils/CancellableHandler.java
@@ -14,6 +14,7 @@ import android.os.Message;
*/
public abstract class CancellableHandler extends Handler {
+ public static final int DONE = -1000;
protected static final int UPDATE_LOAD_PROGRESS_DETAIL = 42186;
private volatile boolean cancelled = false;
private static CompositeSubscription subscriptions = new CompositeSubscription();