1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-30 08:11:17 +00:00

Improved handling of device addresses (fixes #508).

This commit is contained in:
Felix Ableitner 2016-02-25 13:52:11 +01:00
parent 63a7342bfc
commit 04043a1174
3 changed files with 26 additions and 21 deletions

View file

@ -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);
}
})

View file

@ -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<String> 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<String> 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<String> displayableAddresses() {
return DYNAMIC_ADDRESS.equals(mDevice.addresses)
? DYNAMIC_ADDRESS
: mDevice.addresses;
}
@Override

View file

@ -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<String> addresses;
public String name;
public String deviceID;
public String compression;
@ -321,7 +322,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
public <T> 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<String> jsonToList(JSONArray array) throws JSONException {
ArrayList<String> 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);