use json instead of parcelable

This commit is contained in:
badaix 2016-03-02 22:44:49 +01:00
parent 6b67d80bc7
commit 8aecd477fa
10 changed files with 177 additions and 171 deletions

View file

@ -36,8 +36,8 @@ public class ClientSettingsActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("clientInfo", clientSettingsFragment.getClientInfo());
intent.putExtra("clientInfoOriginal", clientSettingsFragment.getOriginalClientInfo());
intent.putExtra("client", clientSettingsFragment.getClientInfo().toJson().toString());
intent.putExtra("clientOriginal", clientSettingsFragment.getOriginalClientInfo().toJson().toString());
setResult(Activity.RESULT_OK, intent);
finish();
// super.onBackPressed();

View file

@ -8,6 +8,10 @@ import android.preference.PreferenceFragment;
import android.text.TextUtils;
import android.text.format.DateUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import de.badaix.snapcast.control.ClientInfo;
@ -34,9 +38,21 @@ public class ClientSettingsFragment extends PreferenceFragment {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
clientInfo = (ClientInfo) bundle.getParcelable("clientInfo");
try {
clientInfo = new ClientInfo(new JSONObject(bundle.getString("client")));
} catch (JSONException e) {
e.printStackTrace();
}
clientInfoOriginal = new ClientInfo(clientInfo.toJson());
final ArrayList<Stream> streams = bundle.getParcelableArrayList("streams");
final ArrayList<Stream> streams = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(bundle.getString("streams"));
for (int i = 0; i < jsonArray.length(); i++)
streams.add(new Stream(jsonArray.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
final CharSequence[] streamNames = new CharSequence[streams.size()];
final CharSequence[] streamIds = new CharSequence[streams.size()];
for (int i = 0; i < streams.size(); ++i) {

View file

@ -37,6 +37,9 @@ import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.UnknownHostException;
import java.util.Vector;
@ -435,8 +438,18 @@ public class MainActivity extends AppCompatActivity implements ClientListFragmen
return;
}
if (requestCode == 1) {
ClientInfo clientInfo = data.getParcelableExtra("clientInfo");
ClientInfo clientInfoOriginal = data.getParcelableExtra("clientInfoOriginal");
ClientInfo clientInfo = null;
try {
clientInfo = new ClientInfo(new JSONObject(data.getStringExtra("client")));
} catch (JSONException e) {
e.printStackTrace();
}
ClientInfo clientInfoOriginal = null;
try {
clientInfoOriginal = new ClientInfo(new JSONObject(data.getStringExtra("clientOriginal")));
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, "new name: " + clientInfo.getName() + ", old name: " + clientInfoOriginal.getName());
if (!clientInfo.getName().equals(clientInfoOriginal.getName()))
remoteControl.setName(clientInfo, clientInfo.getName());
@ -614,8 +627,8 @@ public class MainActivity extends AppCompatActivity implements ClientListFragmen
@Override
public void onPropertiesClicked(ClientInfoItem clientInfoItem) {
Intent intent = new Intent(this, ClientSettingsActivity.class);
intent.putExtra("clientInfo", clientInfoItem.getClientInfo());
intent.putParcelableArrayListExtra("streams", serverInfo.getStreams());
intent.putExtra("client", clientInfoItem.getClientInfo().toJson().toString());
intent.putExtra("streams", serverInfo.getJsonStreams().toString());
intent.setFlags(0);
startActivityForResult(intent, 1);
}

View file

@ -1,7 +1,5 @@
package de.badaix.snapcast.control;
import android.os.Parcel;
import org.json.JSONException;
import org.json.JSONObject;
@ -9,17 +7,6 @@ import org.json.JSONObject;
* Created by johannes on 06.01.16.
*/
public class ClientInfo implements JsonSerialisable {
public static final Creator<ClientInfo> CREATOR = new Creator<ClientInfo>() {
@Override
public ClientInfo createFromParcel(Parcel in) {
return new ClientInfo(in);
}
@Override
public ClientInfo[] newArray(int size) {
return new ClientInfo[size];
}
};
private String mac;
private String ip;
private String host;
@ -36,19 +23,6 @@ public class ClientInfo implements JsonSerialisable {
fromJson(json);
}
protected ClientInfo(Parcel in) {
mac = in.readString();
ip = in.readString();
host = in.readString();
version = in.readString();
name = in.readString();
volume = in.readParcelable(Volume.class.getClassLoader());
lastSeen = in.readParcelable(Time_t.class.getClassLoader());
connected = in.readByte() != 0;
latency = in.readInt();
stream = in.readString();
}
@Override
public void fromJson(JSONObject json) {
try {
@ -211,25 +185,5 @@ public class ClientInfo implements JsonSerialisable {
result = 31 * result + (stream != null ? stream.hashCode() : 0);
return result;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mac);
dest.writeString(ip);
dest.writeString(host);
dest.writeString(version);
dest.writeString(name);
dest.writeParcelable(volume, flags);
dest.writeParcelable(lastSeen, flags);
dest.writeByte((byte) (connected ? 1 : 0));
dest.writeInt(latency);
dest.writeString(stream);
}
}

View file

@ -1,13 +1,11 @@
package de.badaix.snapcast.control;
import android.os.Parcelable;
import org.json.JSONObject;
/**
* Created by johannes on 08.01.16.
*/
public interface JsonSerialisable extends Parcelable {
public interface JsonSerialisable {
public void fromJson(JSONObject json);
public JSONObject toJson();

View file

@ -0,0 +1,74 @@
package de.badaix.snapcast.control;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by johannes on 02.03.16.
*/
public class Server implements JsonSerialisable {
private String host;
private String version;
public Server(JSONObject json) {
fromJson(json);
}
@Override
public void fromJson(JSONObject json) {
try {
host = json.getString("host");
version = json.getString("version");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public JSONObject toJson() {
JSONObject json = new JSONObject();
try {
json.put("host", host);
json.put("version", version);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
public String getHost() {
return host;
}
public String getVersion() {
return version;
}
@Override
public String toString() {
return "Server{" +
"host='" + host + '\'' +
", version='" + version + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Server server = (Server) o;
if (host != null ? !host.equals(server.host) : server.host != null) return false;
return version != null ? version.equals(server.version) : server.version == null;
}
@Override
public int hashCode() {
int result = host != null ? host.hashCode() : 0;
result = 31 * result + (version != null ? version.hashCode() : 0);
return result;
}
}

View file

@ -1,26 +1,64 @@
package de.badaix.snapcast.control;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by johannes on 06.01.16.
*/
public class ServerInfo {
private ArrayList<ClientInfo> clientInfos = new ArrayList<ClientInfo>();
public class ServerInfo implements JsonSerialisable {
private ArrayList<ClientInfo> clients = new ArrayList<ClientInfo>();
private ArrayList<Stream> streams = new ArrayList<Stream>();
private Server server = null;
public ServerInfo(JSONObject json) {
fromJson(json);
}
public ServerInfo() {
}
@Override
public void fromJson(JSONObject json) {
try {
clear();
server = new Server(json.getJSONObject("server"));
JSONArray jStreams = json.getJSONArray("streams");
for (int i = 0; i < jStreams.length(); i++)
streams.add(new Stream(jStreams.getJSONObject(i)));
JSONArray jClients = json.getJSONArray("clients");
for (int i = 0; i < jClients.length(); i++)
clients.add(new ClientInfo(jClients.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public JSONObject toJson() {
JSONObject json = new JSONObject();
try {
json.put("server", server.toJson());
json.put("streams", getJsonStreams());
json.put("clients", getJsonClients());
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
public void clear() {
clientInfos.clear();
clients.clear();
streams.clear();
}
public boolean removeClient(ClientInfo client) {
for (int i = 0; i < clientInfos.size(); ++i) {
if (clientInfos.get(i).getMac().equals(client.getMac())) {
clientInfos.remove(i);
for (int i = 0; i < clients.size(); ++i) {
if (clients.get(i).getMac().equals(client.getMac())) {
clients.remove(i);
return true;
}
}
@ -31,19 +69,19 @@ public class ServerInfo {
if (client == null)
return false;
for (int i = 0; i < clientInfos.size(); ++i) {
ClientInfo clientInfo = clientInfos.get(i);
for (int i = 0; i < clients.size(); ++i) {
ClientInfo clientInfo = clients.get(i);
if (clientInfo == null)
continue;
if (client.getMac().equals(clientInfo.getMac())) {
if (clientInfo.equals(client))
return false;
clientInfos.set(i, client);
clients.set(i, client);
return true;
}
}
clientInfos.add(client);
clients.add(client);
return true;
}
@ -69,10 +107,25 @@ public class ServerInfo {
}
public ArrayList<ClientInfo> getClientInfos() {
return clientInfos;
return clients;
}
public ArrayList<Stream> getStreams() {
return streams;
}
public JSONArray getJsonStreams() {
JSONArray jsonArray = new JSONArray();
for (Stream stream : streams)
jsonArray.put(stream.toJson());
return jsonArray;
}
public JSONArray getJsonClients() {
JSONArray jsonArray = new JSONArray();
for (ClientInfo client : clients)
jsonArray.put(client.toJson());
return jsonArray;
}
}

View file

@ -1,8 +1,5 @@
package de.badaix.snapcast.control;
import android.os.Bundle;
import android.os.Parcel;
import org.json.JSONException;
import org.json.JSONObject;
@ -13,18 +10,6 @@ import java.util.Map;
* Created by johannes on 06.01.16.
*/
public class Stream implements JsonSerialisable {
public static final Creator<Stream> CREATOR = new Creator<Stream>() {
@Override
public Stream createFromParcel(Parcel in) {
return new Stream(in);
}
@Override
public Stream[] newArray(int size) {
return new Stream[size];
}
};
private String uri;
private String scheme;
private String host;
@ -37,17 +22,6 @@ public class Stream implements JsonSerialisable {
fromJson(json);
}
protected Stream(Parcel in) {
uri = in.readString();
scheme = in.readString();
host = in.readString();
path = in.readString();
fragment = in.readString();
id = in.readString();
Bundle bundle = in.readBundle();
query = (HashMap<String, String>) bundle.getSerializable("query");
}
@Override
public void fromJson(JSONObject json) {
try {
@ -79,7 +53,7 @@ public class Stream implements JsonSerialisable {
JSONObject jQuery = new JSONObject();
for (Map.Entry<String, String> entry : query.entrySet())
jQuery.put(entry.getKey(), entry.getValue());
json.put("query", query);
json.put("query", jQuery);
} catch (JSONException e) {
e.printStackTrace();
}
@ -190,22 +164,4 @@ public class Stream implements JsonSerialisable {
", query=" + query +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(uri);
dest.writeString(scheme);
dest.writeString(host);
dest.writeString(path);
dest.writeString(fragment);
dest.writeString(id);
Bundle bundle = new Bundle();
bundle.putSerializable("query", query);
dest.writeBundle(bundle);
}
}

View file

@ -1,7 +1,5 @@
package de.badaix.snapcast.control;
import android.os.Parcel;
import org.json.JSONException;
import org.json.JSONObject;
@ -9,17 +7,6 @@ import org.json.JSONObject;
* Created by johannes on 06.01.16.
*/
public class Time_t implements JsonSerialisable {
public static final Creator<Time_t> CREATOR = new Creator<Time_t>() {
@Override
public Time_t createFromParcel(Parcel in) {
return new Time_t(in);
}
@Override
public Time_t[] newArray(int size) {
return new Time_t[size];
}
};
private long sec;
private long usec;
@ -32,11 +19,6 @@ public class Time_t implements JsonSerialisable {
this.usec = usec;
}
protected Time_t(Parcel in) {
sec = in.readLong();
usec = in.readLong();
}
@Override
public void fromJson(JSONObject json) {
try {
@ -101,15 +83,4 @@ public class Time_t implements JsonSerialisable {
result = (int) (31 * result + usec);
return result;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(sec);
dest.writeLong(usec);
}
}

View file

@ -1,7 +1,5 @@
package de.badaix.snapcast.control;
import android.os.Parcel;
import org.json.JSONException;
import org.json.JSONObject;
@ -9,17 +7,6 @@ import org.json.JSONObject;
* Created by johannes on 06.01.16.
*/
public class Volume implements JsonSerialisable {
public static final Creator<Volume> CREATOR = new Creator<Volume>() {
@Override
public Volume createFromParcel(Parcel in) {
return new Volume(in);
}
@Override
public Volume[] newArray(int size) {
return new Volume[size];
}
};
private boolean muted;
private int percent;
@ -32,11 +19,6 @@ public class Volume implements JsonSerialisable {
this.muted = muted;
}
protected Volume(Parcel in) {
muted = in.readByte() != 0;
percent = in.readInt();
}
@Override
public void fromJson(JSONObject json) {
try {
@ -101,15 +83,4 @@ public class Volume implements JsonSerialisable {
result = 31 * result + percent;
return result;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (muted ? 1 : 0));
dest.writeInt(percent);
}
}