From 04043a1174c2c7b24da41cd3b50a0599a9ce9ae4 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 25 Feb 2016 13:52:11 +0100 Subject: [PATCH] Improved handling of device addresses (fixes #508). --- .../activities/MainActivity.java | 3 ++- .../fragments/DeviceFragment.java | 21 +++++++++++------ .../syncthingandroid/syncthing/RestApi.java | 23 ++++++++----------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 3a4b9dff..21e6ee26 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -38,6 +38,7 @@ import android.widget.TextView; import android.widget.Toast; import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.fragments.DeviceFragment; import com.nutomic.syncthingandroid.fragments.DeviceListFragment; import com.nutomic.syncthingandroid.fragments.DrawerFragment; import com.nutomic.syncthingandroid.fragments.FolderListFragment; @@ -298,7 +299,7 @@ public class MainActivity extends SyncthingActivity public void onClick(DialogInterface dialog, int which) { RestApi.Device device = new RestApi.Device(); device.deviceID = deviceId; - device.addresses = ""; + device.addresses = DeviceFragment.DYNAMIC_ADDRESS; getApi().editDevice(device, MainActivity.this, null); } }) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java index c2b0297a..a8b655df 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java @@ -10,6 +10,7 @@ import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.SwitchCompat; import android.text.Editable; +import android.text.TextUtils; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; @@ -33,6 +34,8 @@ import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.util.Compression; import com.nutomic.syncthingandroid.util.TextWatcherAdapter; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -55,7 +58,7 @@ public class DeviceFragment extends Fragment implements private static final String TAG = "DeviceSettingsFragment"; - private static final String DYNAMIC_ADDRESSES = "dynamic"; + public static final List DYNAMIC_ADDRESS = Collections.singletonList("dynamic"); private SyncthingService mSyncthingService; @@ -286,7 +289,7 @@ public class DeviceFragment extends Fragment implements // Update views mIdView.setText(mDevice.deviceID); mNameView.setText((mDevice.name)); - mAddressesView.setText(displayableAddresses()); + mAddressesView.setText(TextUtils.join(" ", displayableAddresses())); mCompressionValueView.setText(Compression.fromValue(getActivity(), mDevice.compression).getTitle(getActivity())); mIntroducerView.setChecked(mDevice.introducer); @@ -372,7 +375,7 @@ public class DeviceFragment extends Fragment implements mDevice = new RestApi.Device(); mDevice.name = ""; mDevice.deviceID = ""; - mDevice.addresses = "dynamic"; + mDevice.addresses = DYNAMIC_ADDRESS; mDevice.compression = METADATA.getValue(getActivity()); mDevice.introducer = false; } @@ -397,12 +400,16 @@ public class DeviceFragment extends Fragment implements } } - private String persistableAddresses(CharSequence userInput) { - return isEmpty(userInput) ? DYNAMIC_ADDRESSES : userInput.toString(); + private List persistableAddresses(CharSequence userInput) { + return isEmpty(userInput) + ? DYNAMIC_ADDRESS + : Arrays.asList(userInput.toString().split(" ")); } - private String displayableAddresses() { - return DYNAMIC_ADDRESSES.equals(mDevice.addresses) ? "" : mDevice.addresses; + private List displayableAddresses() { + return DYNAMIC_ADDRESS.equals(mDevice.addresses) + ? DYNAMIC_ADDRESS + : mDevice.addresses; } @Override diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index c0dbbaf6..cd50146d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -23,6 +23,7 @@ import org.json.JSONObject; import java.io.Serializable; import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -58,7 +59,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, public static final String TOTAL_STATS = "total"; public static class Device implements Serializable { - public String addresses; + public List addresses; public String name; public String deviceID; public String compression; @@ -321,7 +322,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, public void setValue(String name, String key, T value, boolean isArray, Activity activity) { try { mConfig.getJSONObject(name).put(key, (isArray) - ? listToJson(((String) value).split(",")) + ? new JSONArray(Arrays.asList(((String) value).split(","))) : value); requireRestart(activity); } catch (JSONException e) { @@ -329,16 +330,12 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } } - /** - * Converts an array of strings to JSON array. Like JSONArray#JSONArray(Object array), but - * works on all API levels. - */ - private JSONArray listToJson(String[] list) { - JSONArray json = new JSONArray(); - for (String s : list) { - json.put(s.trim()); + private List jsonToList(JSONArray array) throws JSONException { + ArrayList list = new ArrayList<>(array.length()); + for (int i = 0; i < array.length(); i++) { + list.add(array.getString(i)); } - return json; + return list; } /** @@ -384,7 +381,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, for (int i = 0; i < devices.length(); i++) { JSONObject json = devices.getJSONObject(i); Device n = new Device(); - n.addresses = json.optJSONArray("addresses").join(" ").replace("\"", ""); + n.addresses = jsonToList(json.optJSONArray("addresses")); n.name = json.getString("name"); n.deviceID = json.getString("deviceID"); n.compression = json.getString("compression"); @@ -801,7 +798,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } n.put("deviceID", device.deviceID); n.put("name", device.name); - n.put("addresses", listToJson(device.addresses.split(" "))); + n.put("addresses", new JSONArray(device.addresses)); n.put("compression", device.compression); n.put("introducer", device.introducer); requireRestart(activity);