1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +00:00

Use Gson to parse devices

This commit is contained in:
Felix Ableitner 2016-10-15 17:44:03 +09:00
parent 9b3968683b
commit 396370a71b
2 changed files with 17 additions and 22 deletions

View file

@ -6,10 +6,11 @@ import java.io.Serializable;
import java.util.List; import java.util.List;
public class Device implements Serializable { public class Device implements Serializable {
public List<String> addresses;
public String name;
public String deviceID; public String deviceID;
public String name;
public List<String> addresses;
public String compression; public String compression;
public String certName;
public boolean introducer; public boolean introducer;
/** /**

View file

@ -6,9 +6,9 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.google.common.base.Objects;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -31,11 +31,12 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.Serializable;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -309,25 +310,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
return new ArrayList<>(); return new ArrayList<>();
try { try {
JSONArray devices = mConfig.getJSONArray("devices"); String json = mConfig.getJSONArray("devices").toString();
List<Device> ret = new ArrayList<>(devices.length()); List<Device> devices = new ArrayList<>();
for (int i = 0; i < devices.length(); i++) { Collections.addAll(devices, new Gson().fromJson(json, Device[].class));
JSONObject json = devices.getJSONObject(i);
Device n = new Device(); Iterator<Device> it = devices.iterator();
n.addresses = jsonToList(json.optJSONArray("addresses")); while (it.hasNext()) {
n.name = json.getString("name"); Device device = it.next();
n.deviceID = json.getString("deviceID"); boolean isLocalDevice = Objects.equal(mLocalDeviceId, device.deviceID);
n.compression = json.getString("compression"); if (!includeLocal && isLocalDevice)
n.introducer = json.getBoolean("introducer"); it.remove();
// Use null-save check because mLocalDeviceId might not be initialized yet.
// Should be replaced with Object.equals() when that becomes available in Android.
boolean sameId = (mLocalDeviceId == n.deviceID) ||
(mLocalDeviceId != null && mLocalDeviceId.equals(n.deviceID));
if (includeLocal || !sameId) {
ret.add(n);
}
} }
return ret; return devices;
} catch (JSONException e) { } catch (JSONException e) {
Log.w(TAG, "Failed to read devices", e); Log.w(TAG, "Failed to read devices", e);
return new ArrayList<>(); return new ArrayList<>();