aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/network
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2012-05-29 20:03:00 +0200
committerSamuel Tardieu <sam@rfc1149.net>2012-05-30 00:36:14 +0200
commit67da8b62ff0f414eb5dbde10fddb78a9ae4c208e (patch)
tree6b3faa3f6f7b462b66b78746be5a76c665c75a1d /main/src/cgeo/geocaching/network
parentd900291f83928f8929608eaa3b4d87faff258458 (diff)
downloadcgeo-67da8b62ff0f414eb5dbde10fddb78a9ae4c208e.zip
cgeo-67da8b62ff0f414eb5dbde10fddb78a9ae4c208e.tar.gz
cgeo-67da8b62ff0f414eb5dbde10fddb78a9ae4c208e.tar.bz2
Get message from the notification server
Diffstat (limited to 'main/src/cgeo/geocaching/network')
-rw-r--r--main/src/cgeo/geocaching/network/StatusUpdater.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/network/StatusUpdater.java b/main/src/cgeo/geocaching/network/StatusUpdater.java
new file mode 100644
index 0000000..c2e86f7
--- /dev/null
+++ b/main/src/cgeo/geocaching/network/StatusUpdater.java
@@ -0,0 +1,75 @@
+package cgeo.geocaching.network;
+
+import cgeo.geocaching.utils.MemorySubject;
+import cgeo.geocaching.utils.PeriodicHandler;
+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 {
+
+ 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;
+ }
+ }
+
+ private void requestUpdate() {
+ final JSONObject response =
+ Network.requestJSON("http://status.cgeo.org/api/status.json",
+ new Parameters("version_code", "" + Version.getVersionCode(),
+ "version_name", Version.getVersionName(),
+ "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() {
+ notifyObservers(defaultStatus());
+ Looper.prepare();
+ new PeriodicHandler(1800000L) {
+ @Override
+ public void act() {
+ requestUpdate();
+ }
+ }.start();
+ Looper.loop();
+ }
+
+ public static Status defaultStatus() {
+ switch (Version.getVersionKind()) {
+ case RELEASE_CANDIDATE:
+ return new Status(null, "status_default_rc", null, null);
+ case NIGHTLY_BUILD:
+ return new Status(null, "status_default_nightly", null, null);
+ case DEVELOPER_BUILD:
+ return new Status(null, "status_default_developer_build", null, null);
+ default:
+ return null;
+ }
+ }
+
+}