Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Catfriend1 committed Jan 26, 2019
1 parent 831bc6e commit 0a3df91
Showing 1 changed file with 67 additions and 15 deletions.
82 changes: 67 additions & 15 deletions app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@

import static com.nutomic.syncthingandroid.service.Constants.ENABLE_TEST_DATA;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
* Provides functions to interact with the syncthing REST API.
*/
Expand Down Expand Up @@ -144,6 +156,8 @@ public interface OnResultListener2<T, R> {
*/
private Completion mCompletion = new Completion();

private Gson mGson;

@Inject NotificationHandler mNotificationHandler;

public RestApi(Context context, URL url, String apiKey, OnApiAvailableListener apiListener,
Expand All @@ -154,6 +168,7 @@ public RestApi(Context context, URL url, String apiKey, OnApiAvailableListener a
mApiKey = apiKey;
mOnApiAvailableListener = apiListener;
mOnConfigChangedListener = configListener;
mGson = getGson();
}

public interface OnApiAvailableListener {
Expand Down Expand Up @@ -215,15 +230,15 @@ public void reloadConfig() {
private void onReloadConfigComplete(String result) {
Boolean configParseSuccess;
synchronized(mConfigLock) {
mConfig = new Gson().fromJson(result, Config.class);
mConfig = mGson.fromJson(result, Config.class);
configParseSuccess = mConfig != null;
}
if (!configParseSuccess) {
throw new RuntimeException("config is null: " + result);
}
Log.d(TAG, "onReloadConfigComplete: Successfully parsed configuration.");
LogV("mConfig.pendingDevices = " + new Gson().toJson(mConfig.pendingDevices));
LogV("mConfig.remoteIgnoredDevices = " + new Gson().toJson(mConfig.remoteIgnoredDevices));
LogV("mConfig.pendingDevices = " + mGson.toJson(mConfig.pendingDevices));
LogV("mConfig.remoteIgnoredDevices = " + mGson.toJson(mConfig.remoteIgnoredDevices));

// Update cached device and folder information stored in the mCompletion model.
mCompletion.updateFromConfig(getDevices(true), getFolders());
Expand Down Expand Up @@ -340,8 +355,8 @@ public void ignoreFolder(String deviceId, String folderId) {
}
}
device.ignoredFolders.add(ignoredFolder);
LogV("device.pendingFolders = " + new Gson().toJson(device.pendingFolders));
LogV("device.ignoredFolders = " + new Gson().toJson(device.ignoredFolders));
LogV("device.pendingFolders = " + mGson.toJson(device.pendingFolders));
LogV("device.ignoredFolders = " + mGson.toJson(device.ignoredFolders));
sendConfig();
Log.d(TAG, "Ignored folder [" + folderId + "] announced by device [" + deviceId + "]");

Expand Down Expand Up @@ -383,8 +398,11 @@ public void overrideChanges(String folderId) {
private void sendConfig() {
String jsonConfig;
synchronized (mConfigLock) {
jsonConfig = new Gson().toJson(mConfig);
jsonConfig = mGson.toJson(mConfig);
}
Log.v(TAG, "A" + mConfig.folders.get(0).minDiskFree.value);
Log.v(TAG, "A" + mConfig.folders.get(1).minDiskFree.value);
Log.v(TAG, "sendConfig: " + jsonConfig);
new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_CONFIG, mApiKey,
null, jsonConfig, null);
mOnConfigChangedListener.onConfigChanged();
Expand All @@ -396,7 +414,7 @@ private void sendConfig() {
public void saveConfigAndRestart() {
String jsonConfig;
synchronized (mConfigLock) {
jsonConfig = new Gson().toJson(mConfig);
jsonConfig = mGson.toJson(mConfig);
}
new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_CONFIG, mApiKey,
null, jsonConfig, result -> {
Expand Down Expand Up @@ -600,7 +618,7 @@ public void getSystemStatus(OnResultListener1<SystemStatus> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_SYSTEM_STATUS, mApiKey, null, result -> {
SystemStatus systemStatus;
try {
systemStatus = new Gson().fromJson(result, SystemStatus.class);
systemStatus = mGson.fromJson(result, SystemStatus.class);
listener.onResult(systemStatus);
} catch (Exception e) {
Log.e(TAG, "getSystemStatus: Parsing REST API result failed. result=" + result);
Expand All @@ -620,7 +638,7 @@ public boolean isConfigLoaded() {
public void getFolderIgnoreList(String folderId, OnResultListener1<FolderIgnoreList> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_DB_IGNORES, mApiKey,
ImmutableMap.of("folder", folderId), result -> {
FolderIgnoreList folderIgnoreList = new Gson().fromJson(result, FolderIgnoreList.class);
FolderIgnoreList folderIgnoreList = mGson.fromJson(result, FolderIgnoreList.class);
listener.onResult(folderIgnoreList);
});
}
Expand All @@ -632,15 +650,15 @@ public void postFolderIgnoreList(String folderId, String[] ignore) {
FolderIgnoreList folderIgnoreList = new FolderIgnoreList();
folderIgnoreList.ignore = ignore;
new PostRequest(mContext, mUrl, PostRequest.URI_DB_IGNORES, mApiKey,
ImmutableMap.of("folder", folderId), new Gson().toJson(folderIgnoreList), null);
ImmutableMap.of("folder", folderId), mGson.toJson(folderIgnoreList), null);
}

/**
* Requests and parses system version information.
*/
public void getSystemVersion(OnResultListener1<SystemVersion> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_VERSION, mApiKey, null, result -> {
SystemVersion systemVersion = new Gson().fromJson(result, SystemVersion.class);
SystemVersion systemVersion = mGson.fromJson(result, SystemVersion.class);
listener.onResult(systemVersion);
});
}
Expand All @@ -658,7 +676,7 @@ public void getConnections(final OnResultListener1<Connections> listener) {
}

mPreviousConnectionTime = now;
Connections connections = new Gson().fromJson(result, Connections.class);
Connections connections = mGson.fromJson(result, Connections.class);
for (Map.Entry<String, Connections.Connection> e : connections.connections.entrySet()) {
e.getValue().completion = mCompletion.getDeviceCompletion(e.getKey());

Expand All @@ -682,7 +700,7 @@ public void getConnections(final OnResultListener1<Connections> listener) {
public void getFolderStatus(final String folderId, final OnResultListener2<String, FolderStatus> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_DB_STATUS, mApiKey,
ImmutableMap.of("folder", folderId), result -> {
FolderStatus m = new Gson().fromJson(result, FolderStatus.class);
FolderStatus m = mGson.fromJson(result, FolderStatus.class);
mCachedFolderStatuses.put(folderId, m);
listener.onResult(folderId, m);
});
Expand All @@ -702,7 +720,7 @@ public void getDiskEvents(int limit, OnResultListener1<List<DiskEvent>> listener
JsonArray jsonDiskEvents = new JsonParser().parse(result).getAsJsonArray();
for (int i = jsonDiskEvents.size()-1; i >= 0; i--) {
JsonElement jsonDiskEvent = jsonDiskEvents.get(i);
diskEvents.add(new Gson().fromJson(jsonDiskEvent, DiskEvent.class));
diskEvents.add(mGson.fromJson(jsonDiskEvent, DiskEvent.class));
}
listener.onResult(diskEvents);
} catch (Exception e) {
Expand Down Expand Up @@ -743,7 +761,7 @@ public final void getEvents(final long sinceId, final long limit, final OnReceiv

for (int i = 0; i < jsonEvents.size(); i++) {
JsonElement json = jsonEvents.get(i);
Event event = new Gson().fromJson(json, Event.class);
Event event = mGson.fromJson(json, Event.class);

if (lastId < event.id)
lastId = event.id;
Expand Down Expand Up @@ -882,6 +900,40 @@ public void applyCustomRunConditions(RunConditionMonitor runConditionMonitor) {
}
}

private Gson getGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Float.class, new JsonSerializer<Float>() {
@Override
public JsonElement serialize(final Float src, final Type typeOfSrc,
final JsonSerializationContext context) {
BigDecimal value = BigDecimal.valueOf(src);
Log.v(TAG, "serialize: " + value);
return new JsonPrimitive(value);
}
});
gsonBuilder.registerTypeAdapter(Float.class, new JsonDeserializer<Float>() {
@Override
public Float deserialize(final JsonElement json, final Type typeOfT,
final JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
Log.v(TAG, "deserialize: " + jsonObject.getAsFloat());
return jsonObject.getAsFloat();
}
});
gsonBuilder.registerTypeAdapter(Number.class, new JsonDeserializer<Number>() {
@Override
public Number deserialize(final JsonElement json, final Type typeOfT,
final JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
Log.v(TAG, "deserialize Number: " + jsonObject.getAsFloat());
return jsonObject.getAsFloat();
}
});
return gsonBuilder.create();
}

private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
Expand Down

0 comments on commit 0a3df91

Please sign in to comment.