From 877cf224a55b192cf18b58c27fb61a9becbd0c28 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 27 Oct 2016 03:29:41 +0900 Subject: [PATCH] Use Gson to parse events --- .../nutomic/syncthingandroid/model/Event.java | 13 +++++++ .../syncthing/EventProcessor.java | 30 ++++++++++------ .../syncthingandroid/syncthing/RestApi.java | 34 ++++--------------- 3 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/nutomic/syncthingandroid/model/Event.java diff --git a/src/main/java/com/nutomic/syncthingandroid/model/Event.java b/src/main/java/com/nutomic/syncthingandroid/model/Event.java new file mode 100644 index 00000000..eb563955 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/model/Event.java @@ -0,0 +1,13 @@ +package com.nutomic.syncthingandroid.model; + +import java.util.Map; + +public class Event { + + public int id; + public int globalID; + public String type; + public String time; + public Map data; + +} diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java index 34489b84..a8652d4a 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java @@ -19,6 +19,8 @@ import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.fragments.DeviceFragment; import com.nutomic.syncthingandroid.fragments.FolderFragment; import com.nutomic.syncthingandroid.model.Device; +import com.nutomic.syncthingandroid.model.Event; +import com.nutomic.syncthingandroid.model.Folder; import java.io.File; import java.util.concurrent.TimeUnit; @@ -69,8 +71,7 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene // If that's the case we've to start at zero because syncthing was restarted. mApi.getEvents(0, 1, new RestApi.OnReceiveEventListener() { @Override - public void onEvent(String eventType, JsonObject data) { - + public void onEvent(Event event) { } @Override @@ -88,10 +89,10 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene * Performs the actual event handling. */ @Override - public void onEvent(String type, JsonObject data) { - switch (type) { + public void onEvent(Event event) { + switch (event.type) { case "DeviceRejected": - String deviceId = data.get("device").getAsString(); + String deviceId = (String) event.data.get("device"); Log.d(TAG, "Unknwon device " + deviceId + " wants to connect"); Intent intent = new Intent(mContext, SettingsActivity.class) @@ -109,9 +110,9 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene notify(title, pi); break; case "FolderRejected": - deviceId = data.get("device").getAsString(); - String folderId = data.get("folder").getAsString(); - String folderLabel = data.get("folderLabel").getAsString(); + deviceId = (String) event.data.get("device"); + String folderId = (String) event.data.get("folder"); + String folderLabel = (String) event.data.get("folderLabel"); Log.d(TAG, "Device " + deviceId + " wants to share folder " + folderId); intent = new Intent(mContext, SettingsActivity.class) @@ -136,8 +137,15 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene notify(title, pi); break; case "ItemFinished": - File updatedFile = new File(data.get("folderpath").getAsString(), - data.get("item").getAsString()); + String folder = (String) event.data.get("folder"); + String folderPath = null; + for (Folder f : mApi.getFolders()) { + if (f.id.equals(folder)) { + folderPath = f.path; + } + } + File updatedFile = new File(folderPath, + (String) event.data.get("item")); Log.i(TAG, "Notified media scanner about " + updatedFile.toString()); mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(updatedFile))); @@ -146,7 +154,7 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene // Ignored. break; default: - Log.i(TAG, "Unhandled event " + type); + Log.i(TAG, "Unhandled event " + event.type); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 562b7918..cb6de59d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -22,6 +22,7 @@ import com.nutomic.syncthingandroid.http.PostTask; import com.nutomic.syncthingandroid.model.Config; import com.nutomic.syncthingandroid.model.Connection; import com.nutomic.syncthingandroid.model.Device; +import com.nutomic.syncthingandroid.model.Event; import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.model.Model; import com.nutomic.syncthingandroid.model.Options; @@ -448,12 +449,8 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, public interface OnReceiveEventListener { /** * Called for each event. - * - * Events with a "folder" field in the data have an extra "folderpath" element added. - * @param eventType Name of the event. (See Syncthing documentation) - * @param data Contains the data fields of the event. */ - void onEvent(String eventType, JsonObject data); + void onEvent(Event event); /** * Called after all available events have been processed. @@ -484,30 +481,13 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, long lastId = 0; for (int i = 0; i < jsonEvents.size(); i++) { - JsonObject json = jsonEvents.get(i).getAsJsonObject(); - String type = json.get("type").getAsString(); - long id = json.get("id").getAsLong(); + JsonElement json = jsonEvents.get(i); + Event event = new Gson().fromJson(json, Event.class); - if (lastId < id) - lastId = id; + if (lastId < event.id) + lastId = event.id; - JsonObject data = null; - if (json.has("data")) - data = json.get("data").getAsJsonObject(); - - // Add folder path to data. - if (data != null && data.has("folder")) { - String folder = data.get("folder").getAsString(); - String folderPath = null; - for (Folder f : mConfig.folders) { - if (f.id.equals(folder)) { - folderPath = f.path; - } - } - data.addProperty("folderpath", folderPath); - } - - listener.onEvent(type, data); + listener.onEvent(event); } listener.onDone(lastId);