aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/Go4Cache.java
blob: d916e3215da493b742ca40c5ca5e839d9e8a994f (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cgeo.geocaching;

import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter.Format;
import cgeo.geocaching.utils.CryptUtils;

import org.apache.commons.lang3.StringUtils;

import android.util.Log;

import java.util.concurrent.ArrayBlockingQueue;

/**
 *
 * Thread to send location information to go4cache.com. The singleton will be created
 * only if, at any time, the user opts in to send this information. Then the same thread
 * will take care of sending updated positions when available.
 *
 */

public class Go4Cache extends Thread {

    private static Go4Cache instance;

    final private ArrayBlockingQueue<Geopoint> queue = new ArrayBlockingQueue<Geopoint>(1);
    final private cgeoapplication app;
    final private cgBase base;

    private static Go4Cache getInstance(final cgeoapplication app) {
        if (instance == null) {
            synchronized(Go4Cache.class) {
                instance = new Go4Cache(app);
                instance.start();
            }
        }
        return instance;
    }

    private Go4Cache(final cgeoapplication app) {
        this.app = app;
        base = cgBase.getInstance(app);
        setPriority(Thread.MIN_PRIORITY);
    }

    /**
     * Send the coordinates to go4cache.com if the user opted in to do so.
     *
     * @param app
     *            the current application
     * @param coords
     *            the current coordinates
     */
    public static void signalCoordinates(final cgeoapplication app, final Geopoint coords) {
        if (Settings.isPublicLoc()) {
            getInstance(app).queue.offer(coords);
        }
    }

    @Override
    public void run() {
        Log.d(Settings.tag, "Go4Cache task started");
        Geopoint latestCoords = null;
        String latestAction = null;

        try {
            for (;;) {
                final Geopoint currentCoords = queue.take();
                final String currentAction = app.getAction();

                // If we are too close and we haven't changed our current action, no need
                // to update our situation.
                if (latestCoords != null && latestCoords.distanceTo(currentCoords) < 0.75 && StringUtils.equals(latestAction, currentAction)) {
                    continue;
                }

                final String username = Settings.getUsername();
                if (StringUtils.isBlank(username)) {
                    continue;
                }

                final String latStr = currentCoords.format(Format.LAT_DECDEGREE_RAW);
                final String lonStr = currentCoords.format(Format.LON_DECDEGREE_RAW);
                final Parameters params = new Parameters(
                        "u", username,
                        "lt", latStr,
                        "ln", lonStr,
                        "a", currentAction,
                        "s", (CryptUtils.sha1(username + "|" + latStr + "|" + lonStr + "|" + currentAction + "|" + CryptUtils.md5("carnero: developing your dreams"))).toLowerCase());
                if (base.version != null) {
                    params.put("v", base.version);
                }

                cgBase.postRequest("http://api.go4cache.com/", params);

                // Update our coordinates even if the request was not succesful, as not to hammer the server
                // with invalid requests for every new GPS position.
                latestCoords = currentCoords;
                latestAction = currentAction;
            }
        } catch (InterruptedException e) {
            Log.e(Settings.tag, "Go4Cache.run: interrupted", e);
        }
    }
}