From 9a0da9ef674d95c1435d031a815b8d1230ff8594 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 10 Feb 2020 16:34:01 -0800 Subject: [PATCH] @Parcelize is busted it seems - for now just use IDE generated code --- TODO.md | 6 +- .../java/com/geeksville/mesh/MainActivity.kt | 6 +- .../geeksville/mesh/service/MeshService.kt | 107 ++++++++++++++++-- 3 files changed, 106 insertions(+), 13 deletions(-) diff --git a/TODO.md b/TODO.md index 0d6c67e1b..8d35d7561 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,8 @@ # High priority MVP features required for first public alpha -* show nodeinfo list on gui - one card per node +* make nodeinfo card not look like ass * when a text arrives, move that node info card to the bottom on the window - put the text to the left of the card. with a small arrow/distance/shortname -* parcels are busted - something wrong with the Parcelize kotlin magic * all chat in the app defaults to group chat * make my android app show mesh state * add app icon @@ -84,4 +83,5 @@ Don't leave device discoverable. Don't let unpaired users do things with device * have phone use our local node number as its node number (instead of hardwired) * if radio disconnects, we need to requeue a new connect attempt in RadioService * don't do mesh based algoritm for node id assignment (initially) - instead just store in flash - possibly even in the initial alpha release do this hack -* show connection state on gui \ No newline at end of file +* show connection state on gui +* parcels are busted - something wrong with the Parcelize kotlin magic diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 30ca6933c..0a09ca5a5 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -132,7 +132,9 @@ class MainActivity : AppCompatActivity(), Logging { requestPermission() val filter = IntentFilter() - filter.addAction("") + filter.addAction(MeshService.ACTION_MESH_CONNECTED) + filter.addAction(MeshService.ACTION_NODE_CHANGE) + filter.addAction(MeshService.ACTION_RECEIVED_DATA) registerReceiver(meshServiceReceiver, filter) } @@ -161,7 +163,7 @@ class MainActivity : AppCompatActivity(), Logging { } MeshService.ACTION_RECEIVED_DATA -> { - warn("TODO rxopaqe") + warn("TODO rxdata") val sender = intent.getStringExtra(EXTRA_SENDER)!! val payload = intent.getByteArrayExtra(EXTRA_PAYLOAD)!! val typ = intent.getIntExtra(EXTRA_TYP, -1) diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index 87a6701bd..8ea737254 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -8,6 +8,7 @@ import android.content.* import android.graphics.Color import android.os.Build import android.os.IBinder +import android.os.Parcel import android.os.Parcelable import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat @@ -22,28 +23,108 @@ import com.geeksville.util.exceptionReporter import com.geeksville.util.toOneLineString import com.geeksville.util.toRemoteExceptions import com.google.protobuf.ByteString -import kotlinx.android.parcel.Parcelize import java.nio.charset.Charset class RadioNotConnectedException() : Exception("Can't find radio") // model objects that directly map to the corresponding protobufs -@Parcelize data class MeshUser(val id: String, val longName: String, val shortName: String) : - Parcelable + Parcelable { + constructor(parcel: Parcel) : this( + parcel.readString()!!, + parcel.readString()!!, + parcel.readString()!! + ) { + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeString(id) + parcel.writeString(longName) + parcel.writeString(shortName) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): MeshUser { + return MeshUser(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} -@Parcelize data class Position(val latitude: Double, val longitude: Double, val altitude: Int) : - Parcelable + Parcelable { + constructor(parcel: Parcel) : this( + parcel.readDouble(), + parcel.readDouble(), + parcel.readInt() + ) { + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeDouble(latitude) + parcel.writeDouble(longitude) + parcel.writeInt(altitude) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): Position { + return Position(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} + -@Parcelize data class NodeInfo( val num: Int, // This is immutable, and used as a key var user: MeshUser? = null, var position: Position? = null, var lastSeen: Long? = null -) : Parcelable +) : Parcelable { + constructor(parcel: Parcel) : this( + parcel.readInt(), + parcel.readParcelable(MeshUser::class.java.classLoader), + parcel.readParcelable(Position::class.java.classLoader), + parcel.readValue(Long::class.java.classLoader) as? Long + ) { + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeInt(num) + parcel.writeParcelable(user, flags) + parcel.writeParcelable(position, flags) + parcel.writeValue(lastSeen) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): NodeInfo { + return NodeInfo(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} /** * Handles all the communication with android apps. Also keeps an internal model @@ -113,6 +194,16 @@ class MeshService : Service(), Logging { private fun broadcastNodeChange(info: NodeInfo) { debug("Broadcasting node change $info") val intent = Intent(ACTION_NODE_CHANGE) + + /* + if (info.user == null) + info.user = MeshUser("x", "y", "z") + + if (info.position == null) + info.position = Position(1.5, 1.6, 3) + + */ + intent.putExtra(EXTRA_NODEINFO, info) explicitBroadcast(intent) } @@ -298,7 +389,7 @@ class MeshService : Service(), Logging { nodeDBbyID[userId] = info // parcelable is busted - // broadcastNodeChange(info) + broadcastNodeChange(info) } /// Generate a new mesh packet builder with our node as the sender, and the specified node num