From 0a3df9144a86a396318c57a089d41da449d284dc Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Sat, 26 Jan 2019 21:42:17 +0100 Subject: [PATCH] WIP --- .../syncthingandroid/service/RestApi.java | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 6a7935a39..10f45e759 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -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. */ @@ -144,6 +156,8 @@ public interface OnResultListener2 { */ private Completion mCompletion = new Completion(); + private Gson mGson; + @Inject NotificationHandler mNotificationHandler; public RestApi(Context context, URL url, String apiKey, OnApiAvailableListener apiListener, @@ -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 { @@ -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()); @@ -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 + "]"); @@ -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(); @@ -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 -> { @@ -600,7 +618,7 @@ public void getSystemStatus(OnResultListener1 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); @@ -620,7 +638,7 @@ public boolean isConfigLoaded() { public void getFolderIgnoreList(String folderId, OnResultListener1 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); }); } @@ -632,7 +650,7 @@ 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); } /** @@ -640,7 +658,7 @@ public void postFolderIgnoreList(String folderId, String[] ignore) { */ public void getSystemVersion(OnResultListener1 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); }); } @@ -658,7 +676,7 @@ public void getConnections(final OnResultListener1 listener) { } mPreviousConnectionTime = now; - Connections connections = new Gson().fromJson(result, Connections.class); + Connections connections = mGson.fromJson(result, Connections.class); for (Map.Entry e : connections.connections.entrySet()) { e.getValue().completion = mCompletion.getDeviceCompletion(e.getKey()); @@ -682,7 +700,7 @@ public void getConnections(final OnResultListener1 listener) { public void getFolderStatus(final String folderId, final OnResultListener2 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); }); @@ -702,7 +720,7 @@ public void getDiskEvents(int limit, OnResultListener1> 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) { @@ -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; @@ -882,6 +900,40 @@ public void applyCustomRunConditions(RunConditionMonitor runConditionMonitor) { } } + private Gson getGson() { + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(Float.class, new JsonSerializer() { + @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() { + @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() { + @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);