aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/network/Send2CgeoDownloader.java
blob: 5f5be561829563ed6dbe67da9fe525eb03b2233f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cgeo.geocaching.network;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.RxUtils;

import ch.boye.httpclientandroidlib.HttpResponse;

import org.apache.commons.lang3.StringUtils;

import rx.Scheduler.Worker;
import rx.functions.Action0;

import java.util.concurrent.TimeUnit;

public class Send2CgeoDownloader {

    private Send2CgeoDownloader() {
        // Do not instantiate
    }

    /**
     * Asynchronously load caches from the send2cgeo server.
     *
     * @param handler the handler to which progress information will be sent
     * @param listId the list into which caches will be stored
     */
    public static void loadFromWeb(final CancellableHandler handler, final int listId) {
        final Worker worker = RxUtils.networkScheduler.createWorker();
        handler.unsubscribeIfCancelled(worker);
        worker.schedule(new Action0() {
            private final Parameters PARAMS = new Parameters("code", StringUtils.defaultString(Settings.getWebDeviceCode()));
            private long baseTime = System.currentTimeMillis();

            @Override
            public void call() {
                if (System.currentTimeMillis() - baseTime >= 3 * 60000) { // maximum: 3 minutes
                    handler.sendEmptyMessage(DownloadProgress.MSG_DONE);
                    return;
                }

                // Download new code
                final HttpResponse responseFromWeb = Network.getRequest("http://send2.cgeo.org/read.html", PARAMS);

                if (responseFromWeb != null && responseFromWeb.getStatusLine().getStatusCode() == 200) {
                    final String response = Network.getResponseData(responseFromWeb);
                    if (response != null && response.length() > 2) {
                        handler.sendMessage(handler.obtainMessage(DownloadProgress.MSG_LOADING, response));
                        Geocache.storeCache(null, response, listId, false, null);
                        handler.sendMessage(handler.obtainMessage(DownloadProgress.MSG_LOADED, response));
                        baseTime = System.currentTimeMillis();
                        worker.schedule(this);
                    } else if ("RG".equals(response)) {
                        //Server returned RG (registration) and this device no longer registered.
                        Settings.setWebNameCode(null, null);
                        handler.sendEmptyMessage(DownloadProgress.MSG_NO_REGISTRATION);
                        handler.cancel();
                    } else {
                        worker.schedule(this, 5, TimeUnit.SECONDS);
                        handler.sendEmptyMessage(DownloadProgress.MSG_WAITING);
                    }
                } else {
                    handler.sendEmptyMessage(DownloadProgress.MSG_SERVER_FAIL);
                    handler.cancel();
                }
            }
        });
    }
}