mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-11 04:25:53 +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
|
* Either shows a restart dialog, or only updates the config, depending on
|
||||||
* {@link #mRestartPostponed}.
|
* {@link #mRestartPostponed}.
|
||||||
|
@ -210,54 +193,34 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of all existing devices.
|
* Stops syncthing and cancels notification.
|
||||||
*
|
|
||||||
* @param includeLocal True if the local device should be included in the result.
|
|
||||||
*/
|
*/
|
||||||
public List<Device> getDevices(boolean includeLocal) {
|
public void shutdown() {
|
||||||
List<Device> devices = deepCopy(mConfig.devices, new TypeToken<List<Device>>(){}.getType());
|
NotificationManager nm = (NotificationManager)
|
||||||
|
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
Iterator<Device> it = devices.iterator();
|
nm.cancel(RestartActivity.NOTIFICATION_RESTART);
|
||||||
while (it.hasNext()) {
|
mRestartPostponed = false;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a deep copy of object.
|
* Returns the version name, or a (text) error message on failure.
|
||||||
*
|
|
||||||
* This method uses Gson and only works with objects that can be converted with Gson.
|
|
||||||
*/
|
*/
|
||||||
public <T> T deepCopy(T object, Type type) {
|
public String getVersion() {
|
||||||
Gson gson = new Gson();
|
return mVersion;
|
||||||
return gson.fromJson(gson.toJson(object, type), type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeDevice(String deviceId) {
|
public List<Folder> getFolders() {
|
||||||
removeDeviceInternal(deviceId);
|
return deepCopy(mConfig.folders, new TypeToken<List<Folder>>(){}.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFolder(Folder folder) {
|
||||||
|
mConfig.folders.add(folder);
|
||||||
sendConfig();
|
sendConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDeviceInternal(String deviceId) {
|
public void editFolder(Folder newFolder) {
|
||||||
Iterator<Device> it = mConfig.devices.iterator();
|
removeFolderInternal(newFolder.id);
|
||||||
while (it.hasNext()) {
|
addFolder(newFolder);
|
||||||
Device d = it.next();
|
|
||||||
if (d.deviceID.equals(deviceId)) {
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeFolder(String id) {
|
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 -> {
|
* Returns a list of all existing devices.
|
||||||
mConfig.devices.add(device);
|
*
|
||||||
sendConfig();
|
* @param includeLocal True if the local device should be included in the result.
|
||||||
}, errorListener);
|
*/
|
||||||
|
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) {
|
public Device getLocalDevice() {
|
||||||
mConfig.folders.add(folder);
|
for (Device d : getDevices(true)) {
|
||||||
sendConfig();
|
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) {
|
public void editDevice(Device newDevice) {
|
||||||
|
@ -293,9 +278,27 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
sendConfig();
|
sendConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void editFolder(Folder newFolder) {
|
public void removeDevice(String deviceId) {
|
||||||
removeFolderInternal(newFolder.id);
|
removeDeviceInternal(deviceId);
|
||||||
addFolder(newFolder);
|
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) {
|
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) {
|
public void getSystemInfo(OnResultListener1<SystemInfo> listener) {
|
||||||
new GetTask(mUrl, GetTask.URI_SYSTEM, mHttpsCertPath, mApiKey, null, result -> {
|
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.
|
* Requests and parses system version information.
|
||||||
*
|
|
||||||
* @param listener Callback invoked when the result is received.
|
|
||||||
*/
|
*/
|
||||||
public void getSystemVersion(OnResultListener1<SystemVersion> listener) {
|
public void getSystemVersion(OnResultListener1<SystemVersion> listener) {
|
||||||
new GetTask(mUrl, GetTask.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> {
|
new GetTask(mUrl, GetTask.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> {
|
||||||
|
@ -327,13 +336,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
}).execute();
|
}).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.
|
* Returns connection info for the local device and all connected devices.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
@ -430,6 +432,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
: 100;
|
: 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}.
|
* Listener for {@link #getEvents}.
|
||||||
*/
|
*/
|
||||||
|
@ -447,18 +461,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
void onDone(long lastId);
|
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.
|
* Retrieves the events that have accumulated since the given event id.
|
||||||
*
|
*
|
||||||
|
@ -511,18 +513,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
.execute();
|
.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.
|
* Returns prettyfied usage report.
|
||||||
*/
|
*/
|
||||||
|
@ -534,9 +524,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
}).execute();
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets {@link #mRestartPostponed} to true.
|
|
||||||
*/
|
|
||||||
public void setRestartPostponed() {
|
public void setRestartPostponed() {
|
||||||
mRestartPostponed = true;
|
mRestartPostponed = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue