2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 18:07:55 +00:00

Deprecate packet id field

This commit is contained in:
Albert Vaca Cintora 2025-02-23 17:27:34 +01:00
parent 9b2e4bcf56
commit e9e406de88
2 changed files with 3 additions and 7 deletions

View File

@ -16,14 +16,12 @@ import java.net.Socket
import kotlin.concurrent.Volatile import kotlin.concurrent.Volatile
class NetworkPacket private constructor( class NetworkPacket private constructor(
val id: Long,
val type: String, val type: String,
private val mBody: JSONObject, private val mBody: JSONObject,
var payload: Payload?, var payload: Payload?,
var payloadTransferInfo: JSONObject, var payloadTransferInfo: JSONObject,
) { ) {
constructor(type: String) : this( constructor(type: String) : this(
id = System.currentTimeMillis(),
type = type, type = type,
mBody = JSONObject(), mBody = JSONObject(),
payload = null, payload = null,
@ -209,7 +207,7 @@ class NetworkPacket private constructor(
@Throws(JSONException::class) @Throws(JSONException::class)
fun serialize(): String { fun serialize(): String {
val jo = JSONObject() val jo = JSONObject()
jo.put("id", id) jo.put("id", System.currentTimeMillis())
jo.put("type", type) jo.put("type", type)
jo.put("body", mBody) jo.put("body", mBody)
if (hasPayload()) { if (hasPayload()) {
@ -285,14 +283,13 @@ class NetworkPacket private constructor(
@Throws(JSONException::class) @Throws(JSONException::class)
fun unserialize(s: String): NetworkPacket { fun unserialize(s: String): NetworkPacket {
val jo = JSONObject(s) val jo = JSONObject(s)
val id = jo.getLong("id")
val type = jo.getString("type") val type = jo.getString("type")
val mBody = jo.getJSONObject("body") val mBody = jo.getJSONObject("body")
val hasPayload = jo.has("payloadSize") val hasPayload = jo.has("payloadSize")
val payloadTransferInfo = if (hasPayload) jo.getJSONObject("payloadTransferInfo") else JSONObject() val payloadTransferInfo = if (hasPayload) jo.getJSONObject("payloadTransferInfo") else JSONObject()
val payload = if (hasPayload) Payload(jo.getLong("payloadSize")) else null val payload = if (hasPayload) Payload(jo.getLong("payloadSize")) else null
return NetworkPacket(id, type, mBody, payload, payloadTransferInfo) return NetworkPacket(type, mBody, payload, payloadTransferInfo)
} }
} }
} }

View File

@ -37,9 +37,8 @@ class NetworkPacketTest {
Assert.assertEquals(np.getString("type"), np2.getString("type")) Assert.assertEquals(np.getString("type"), np2.getString("type"))
Assert.assertEquals(np.getJSONArray("body"), np2.getJSONArray("body")) Assert.assertEquals(np.getJSONArray("body"), np2.getJSONArray("body"))
val json = "{\"id\":123,\"type\":\"test\",\"body\":{\"testing\":true}}" val json = "{\"type\":\"test\",\"body\":{\"testing\":true}}"
np2 = unserialize(json) np2 = unserialize(json)
Assert.assertEquals(np2.id, 123)
Assert.assertTrue(np2.getBoolean("testing")) Assert.assertTrue(np2.getBoolean("testing"))
Assert.assertFalse(np2.getBoolean("not_testing")) Assert.assertFalse(np2.getBoolean("not_testing"))
Assert.assertTrue(np2.getBoolean("not_testing", true)) Assert.assertTrue(np2.getBoolean("not_testing", true))