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
|
package cgeo.geocaching.network;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.utils.MemorySubject;
import cgeo.geocaching.utils.PeriodicHandler;
import cgeo.geocaching.utils.PeriodicHandler.PeriodicHandlerListener;
import cgeo.geocaching.utils.Version;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Looper;
import java.util.Locale;
public class StatusUpdater extends MemorySubject<StatusUpdater.Status> implements Runnable, PeriodicHandlerListener {
static public class Status {
final public String message;
final public String messageId;
final public String icon;
final public String url;
Status(final String message, final String messageId, final String icon, final String url) {
this.message = message;
this.messageId = messageId;
this.icon = icon;
this.url = url;
}
}
@Override
public void onPeriodic() {
final JSONObject response =
Network.requestJSON("http://status.cgeo.org/api/status.json",
new Parameters("version_code", String.valueOf(Version.getVersionCode(cgeoapplication.getInstance())),
"version_name", Version.getVersionName(cgeoapplication.getInstance()),
"locale", Locale.getDefault().toString()));
if (response != null) {
notifyObservers(new Status(get(response, "message"), get(response, "message_id"), get(response, "icon"), get(response, "url")));
}
}
private static String get(final JSONObject json, final String key) {
try {
return json.getString(key);
} catch (final JSONException e) {
return null;
}
}
@Override
public void run() {
Looper.prepare();
new PeriodicHandler(1800000L, this).start();
Looper.loop();
}
}
|