From a64392922d1987da4c4a66fa36ce51377fd423a8 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 31 Oct 2016 18:20:13 +0900 Subject: [PATCH] Reordered RestApi methods --- .../syncthingandroid/syncthing/RestApi.java | 195 ++++++++---------- 1 file changed, 91 insertions(+), 104 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index f2ea662c..a5746a32 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -160,23 +160,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } } - /** - * Returns the version name, or a (text) error message on failure. - */ - public String getVersion() { - return mVersion; - } - - /** - * Stops syncthing and cancels notification. For use by {@link SyncthingService}. - */ - public void shutdown() { - NotificationManager nm = (NotificationManager) - mContext.getSystemService(Context.NOTIFICATION_SERVICE); - nm.cancel(RestartActivity.NOTIFICATION_RESTART); - mRestartPostponed = false; - } - /** * Either shows a restart dialog, or only updates the config, depending on * {@link #mRestartPostponed}. @@ -210,54 +193,34 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } /** - * Returns a list of all existing devices. - * - * @param includeLocal True if the local device should be included in the result. + * Stops syncthing and cancels notification. */ - public List getDevices(boolean includeLocal) { - List devices = deepCopy(mConfig.devices, new TypeToken>(){}.getType()); - - Iterator it = devices.iterator(); - while (it.hasNext()) { - Device device = it.next(); - boolean isLocalDevice = Objects.equal(mLocalDeviceId, device.deviceID); - if (!includeLocal && isLocalDevice) - it.remove(); - } - return devices; - } - - public Options getOptions() { - return deepCopy(mConfig.options, Options.class); - } - - public Config.Gui getGui() { - return deepCopy(mConfig.gui, Config.Gui.class); + public void shutdown() { + NotificationManager nm = (NotificationManager) + mContext.getSystemService(Context.NOTIFICATION_SERVICE); + nm.cancel(RestartActivity.NOTIFICATION_RESTART); + mRestartPostponed = false; } /** - * Returns a deep copy of object. - * - * This method uses Gson and only works with objects that can be converted with Gson. + * Returns the version name, or a (text) error message on failure. */ - public T deepCopy(T object, Type type) { - Gson gson = new Gson(); - return gson.fromJson(gson.toJson(object, type), type); + public String getVersion() { + return mVersion; } - public void removeDevice(String deviceId) { - removeDeviceInternal(deviceId); + public List getFolders() { + return deepCopy(mConfig.folders, new TypeToken>(){}.getType()); + } + + public void addFolder(Folder folder) { + mConfig.folders.add(folder); sendConfig(); } - private void removeDeviceInternal(String deviceId) { - Iterator it = mConfig.devices.iterator(); - while (it.hasNext()) { - Device d = it.next(); - if (d.deviceID.equals(deviceId)) { - it.remove(); - } - } + public void editFolder(Folder newFolder) { + removeFolderInternal(newFolder.id); + addFolder(newFolder); } public void removeFolder(String id) { @@ -275,16 +238,38 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } } - public void addDevice(Device device, OnResultListener1 errorListener) { - normalizeDeviceId(device.deviceID, normalizedId -> { - mConfig.devices.add(device); - sendConfig(); - }, errorListener); + /** + * Returns a list of all existing devices. + * + * @param includeLocal True if the local device should be included in the result. + */ + public List getDevices(boolean includeLocal) { + List devices = deepCopy(mConfig.devices, new TypeToken>(){}.getType()); + + Iterator it = devices.iterator(); + while (it.hasNext()) { + Device device = it.next(); + boolean isLocalDevice = Objects.equal(mLocalDeviceId, device.deviceID); + if (!includeLocal && isLocalDevice) + it.remove(); + } + return devices; } - public void addFolder(Folder folder) { - mConfig.folders.add(folder); - sendConfig(); + public Device getLocalDevice() { + for (Device d : getDevices(true)) { + if (d.deviceID.equals(mLocalDeviceId)) { + return deepCopy(d, Device.class); + } + } + throw new RuntimeException(); + } + + public void addDevice(Device device, OnResultListener1 errorListener) { + normalizeDeviceId(device.deviceID, normalizedId -> { + mConfig.devices.add(device); + sendConfig(); + }, errorListener); } public void editDevice(Device newDevice) { @@ -293,9 +278,27 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, sendConfig(); } - public void editFolder(Folder newFolder) { - removeFolderInternal(newFolder.id); - addFolder(newFolder); + public void removeDevice(String deviceId) { + removeDeviceInternal(deviceId); + sendConfig(); + } + + private void removeDeviceInternal(String deviceId) { + Iterator it = mConfig.devices.iterator(); + while (it.hasNext()) { + Device d = it.next(); + if (d.deviceID.equals(deviceId)) { + it.remove(); + } + } + } + + public Options getOptions() { + return deepCopy(mConfig.options, Options.class); + } + + public Config.Gui getGui() { + return deepCopy(mConfig.gui, Config.Gui.class); } public void editSettings(Config.Gui newGui, Options newOptions, Activity activity) { @@ -305,9 +308,17 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } /** - * Requests and parses information about current system status and resource usage. + * Returns a deep copy of object. * - * @param listener Callback invoked when the result is received. + * This method uses Gson and only works with objects that can be converted with Gson. + */ + public T deepCopy(T object, Type type) { + Gson gson = new Gson(); + return gson.fromJson(gson.toJson(object, type), type); + } + + /** + * Requests and parses information about current system status and resource usage. */ public void getSystemInfo(OnResultListener1 listener) { new GetTask(mUrl, GetTask.URI_SYSTEM, mHttpsCertPath, mApiKey, null, result -> { @@ -317,8 +328,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, /** * Requests and parses system version information. - * - * @param listener Callback invoked when the result is received. */ public void getSystemVersion(OnResultListener1 listener) { new GetTask(mUrl, GetTask.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> { @@ -327,13 +336,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, }).execute(); } - /** - * Returns a list of all existing folders. - */ - public List getFolders() { - return deepCopy(mConfig.folders, new TypeToken>(){}.getType()); - } - /** * Returns connection info for the local device and all connected devices. *

@@ -430,6 +432,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, : 100; } + /** + * Returns status information about the folder with the given id. + */ + public void getModel(final String folderId, final OnResultListener2 listener) { + new GetTask(mUrl, GetTask.URI_MODEL, mHttpsCertPath, mApiKey, + ImmutableMap.of("folder", folderId), result -> { + Model m = new Gson().fromJson(result, Model.class); + mCachedModelInfo.put(folderId, m); + listener.onResult(folderId, m); + }).execute(); + } + /** * Listener for {@link #getEvents}. */ @@ -447,18 +461,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, void onDone(long lastId); } - /** - * Returns status information about the folder with the given id. - */ - public void getModel(final String folderId, final OnResultListener2 listener) { - new GetTask(mUrl, GetTask.URI_MODEL, mHttpsCertPath, mApiKey, - ImmutableMap.of("folder", folderId), result -> { - Model m = new Gson().fromJson(result, Model.class); - mCachedModelInfo.put(folderId, m); - listener.onResult(folderId, m); - }).execute(); - } - /** * Retrieves the events that have accumulated since the given event id. * @@ -511,18 +513,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, .execute(); } - /** - * Returns the object representing the local device. - */ - public Device getLocalDevice() { - for (Device d : getDevices(true)) { - if (d.deviceID.equals(mLocalDeviceId)) { - return deepCopy(d, Device.class); - } - } - throw new RuntimeException(); - } - /** * Returns prettyfied usage report. */ @@ -534,9 +524,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, }).execute(); } - /** - * Sets {@link #mRestartPostponed} to true. - */ public void setRestartPostponed() { mRestartPostponed = true; }