1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-10 20:15:54 +00:00

Use Gson to parse events

This commit is contained in:
Felix Ableitner 2016-10-27 03:29:41 +09:00
parent b1af659e79
commit 877cf224a5
3 changed files with 39 additions and 38 deletions

View file

@ -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<String, Object> data;
}

View file

@ -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);
}
}

View file

@ -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);