mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-23 11:21:29 +00:00
Reordered RestApi methods
This commit is contained in:
parent
90aa29d81d
commit
a64392922d
1 changed files with 91 additions and 104 deletions
|
@ -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<Device> getDevices(boolean includeLocal) {
|
||||
List<Device> devices = deepCopy(mConfig.devices, new TypeToken<List<Device>>(){}.getType());
|
||||
|
||||
Iterator<Device> 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> 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<Folder> getFolders() {
|
||||
return deepCopy(mConfig.folders, new TypeToken<List<Folder>>(){}.getType());
|
||||
}
|
||||
|
||||
public void addFolder(Folder folder) {
|
||||
mConfig.folders.add(folder);
|
||||
sendConfig();
|
||||
}
|
||||
|
||||
private void removeDeviceInternal(String deviceId) {
|
||||
Iterator<Device> 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<String> 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<Device> getDevices(boolean includeLocal) {
|
||||
List<Device> devices = deepCopy(mConfig.devices, new TypeToken<List<Device>>(){}.getType());
|
||||
|
||||
Iterator<Device> 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<String> 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<Device> 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> 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<SystemInfo> 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<SystemVersion> 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<Folder> getFolders() {
|
||||
return deepCopy(mConfig.folders, new TypeToken<List<Folder>>(){}.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns connection info for the local device and all connected devices.
|
||||
* <p/>
|
||||
|
@ -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<String, Model> 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<String, Model> 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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue