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:
parent
b1af659e79
commit
877cf224a5
3 changed files with 39 additions and 38 deletions
13
src/main/java/com/nutomic/syncthingandroid/model/Event.java
Normal file
13
src/main/java/com/nutomic/syncthingandroid/model/Event.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
|
@ -19,6 +19,8 @@ import com.nutomic.syncthingandroid.activities.SettingsActivity;
|
||||||
import com.nutomic.syncthingandroid.fragments.DeviceFragment;
|
import com.nutomic.syncthingandroid.fragments.DeviceFragment;
|
||||||
import com.nutomic.syncthingandroid.fragments.FolderFragment;
|
import com.nutomic.syncthingandroid.fragments.FolderFragment;
|
||||||
import com.nutomic.syncthingandroid.model.Device;
|
import com.nutomic.syncthingandroid.model.Device;
|
||||||
|
import com.nutomic.syncthingandroid.model.Event;
|
||||||
|
import com.nutomic.syncthingandroid.model.Folder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.concurrent.TimeUnit;
|
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.
|
// If that's the case we've to start at zero because syncthing was restarted.
|
||||||
mApi.getEvents(0, 1, new RestApi.OnReceiveEventListener() {
|
mApi.getEvents(0, 1, new RestApi.OnReceiveEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onEvent(String eventType, JsonObject data) {
|
public void onEvent(Event event) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -88,10 +89,10 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene
|
||||||
* Performs the actual event handling.
|
* Performs the actual event handling.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onEvent(String type, JsonObject data) {
|
public void onEvent(Event event) {
|
||||||
switch (type) {
|
switch (event.type) {
|
||||||
case "DeviceRejected":
|
case "DeviceRejected":
|
||||||
String deviceId = data.get("device").getAsString();
|
String deviceId = (String) event.data.get("device");
|
||||||
Log.d(TAG, "Unknwon device " + deviceId + " wants to connect");
|
Log.d(TAG, "Unknwon device " + deviceId + " wants to connect");
|
||||||
|
|
||||||
Intent intent = new Intent(mContext, SettingsActivity.class)
|
Intent intent = new Intent(mContext, SettingsActivity.class)
|
||||||
|
@ -109,9 +110,9 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene
|
||||||
notify(title, pi);
|
notify(title, pi);
|
||||||
break;
|
break;
|
||||||
case "FolderRejected":
|
case "FolderRejected":
|
||||||
deviceId = data.get("device").getAsString();
|
deviceId = (String) event.data.get("device");
|
||||||
String folderId = data.get("folder").getAsString();
|
String folderId = (String) event.data.get("folder");
|
||||||
String folderLabel = data.get("folderLabel").getAsString();
|
String folderLabel = (String) event.data.get("folderLabel");
|
||||||
Log.d(TAG, "Device " + deviceId + " wants to share folder " + folderId);
|
Log.d(TAG, "Device " + deviceId + " wants to share folder " + folderId);
|
||||||
|
|
||||||
intent = new Intent(mContext, SettingsActivity.class)
|
intent = new Intent(mContext, SettingsActivity.class)
|
||||||
|
@ -136,8 +137,15 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene
|
||||||
notify(title, pi);
|
notify(title, pi);
|
||||||
break;
|
break;
|
||||||
case "ItemFinished":
|
case "ItemFinished":
|
||||||
File updatedFile = new File(data.get("folderpath").getAsString(),
|
String folder = (String) event.data.get("folder");
|
||||||
data.get("item").getAsString());
|
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());
|
Log.i(TAG, "Notified media scanner about " + updatedFile.toString());
|
||||||
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
|
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
|
||||||
Uri.fromFile(updatedFile)));
|
Uri.fromFile(updatedFile)));
|
||||||
|
@ -146,7 +154,7 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene
|
||||||
// Ignored.
|
// Ignored.
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Log.i(TAG, "Unhandled event " + type);
|
Log.i(TAG, "Unhandled event " + event.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import com.nutomic.syncthingandroid.http.PostTask;
|
||||||
import com.nutomic.syncthingandroid.model.Config;
|
import com.nutomic.syncthingandroid.model.Config;
|
||||||
import com.nutomic.syncthingandroid.model.Connection;
|
import com.nutomic.syncthingandroid.model.Connection;
|
||||||
import com.nutomic.syncthingandroid.model.Device;
|
import com.nutomic.syncthingandroid.model.Device;
|
||||||
|
import com.nutomic.syncthingandroid.model.Event;
|
||||||
import com.nutomic.syncthingandroid.model.Folder;
|
import com.nutomic.syncthingandroid.model.Folder;
|
||||||
import com.nutomic.syncthingandroid.model.Model;
|
import com.nutomic.syncthingandroid.model.Model;
|
||||||
import com.nutomic.syncthingandroid.model.Options;
|
import com.nutomic.syncthingandroid.model.Options;
|
||||||
|
@ -448,12 +449,8 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
public interface OnReceiveEventListener {
|
public interface OnReceiveEventListener {
|
||||||
/**
|
/**
|
||||||
* Called for each event.
|
* 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.
|
* Called after all available events have been processed.
|
||||||
|
@ -484,30 +481,13 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
long lastId = 0;
|
long lastId = 0;
|
||||||
|
|
||||||
for (int i = 0; i < jsonEvents.size(); i++) {
|
for (int i = 0; i < jsonEvents.size(); i++) {
|
||||||
JsonObject json = jsonEvents.get(i).getAsJsonObject();
|
JsonElement json = jsonEvents.get(i);
|
||||||
String type = json.get("type").getAsString();
|
Event event = new Gson().fromJson(json, Event.class);
|
||||||
long id = json.get("id").getAsLong();
|
|
||||||
|
|
||||||
if (lastId < id)
|
if (lastId < event.id)
|
||||||
lastId = id;
|
lastId = event.id;
|
||||||
|
|
||||||
JsonObject data = null;
|
listener.onEvent(event);
|
||||||
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.onDone(lastId);
|
listener.onDone(lastId);
|
||||||
|
|
Loading…
Reference in a new issue