Wear CWF Refactor communication

This commit is contained in:
Philoul 2023-10-12 22:23:32 +02:00
parent cde1be75ba
commit 9cc7ff5cc2
10 changed files with 52 additions and 69 deletions

View file

@ -135,9 +135,7 @@ data class ResData(val value: ByteArray, val format: ResFormat) {
typealias CwfResDataMap = MutableMap<String, ResData> typealias CwfResDataMap = MutableMap<String, ResData>
typealias CwfMetadataMap = MutableMap<CwfMetadataKey, String> typealias CwfMetadataMap = MutableMap<CwfMetadataKey, String>
fun CwfResDataMap.sameRes(dataMap: CwfResDataMap) = (this.size == dataMap.size) && this.all { (key, resData) -> dataMap[key]?.value.contentEquals(resData.value) == true } fun CwfResDataMap.isEquals(dataMap: CwfResDataMap) = (this.size == dataMap.size) && this.all { (key, resData) -> dataMap[key]?.value.contentEquals(resData.value) == true }
fun CwfMetadataMap.sameMeta(metaDataMap: CwfMetadataMap) = (this.size == metaDataMap.size) && this.all { (key, string) -> metaDataMap[key] == string }
@Serializable @Serializable
data class CwfData(val json: String, var metadata: CwfMetadataMap, val resDatas: CwfResDataMap) { data class CwfData(val json: String, var metadata: CwfMetadataMap, val resDatas: CwfResDataMap) {
fun simplify(): CwfData? = resDatas[ResFileMap.CUSTOM_WATCHFACE.fileName]?.let { fun simplify(): CwfData? = resDatas[ResFileMap.CUSTOM_WATCHFACE.fileName]?.let {
@ -295,24 +293,24 @@ class ZipWatchfaceFormat {
const val CWF_EXTENTION = ".zip" const val CWF_EXTENTION = ".zip"
const val CWF_JSON_FILE = "CustomWatchface.json" const val CWF_JSON_FILE = "CustomWatchface.json"
fun loadCustomWatchface(zipInputStream: ZipInputStream, zipName: String, authorization: Boolean): CwfFile? { fun loadCustomWatchface(byteArray: ByteArray, zipName: String, authorization: Boolean): CwfFile? {
var json = JSONObject() var json = JSONObject()
var metadata: CwfMetadataMap = mutableMapOf() var metadata: CwfMetadataMap = mutableMapOf()
val resDatas: CwfResDataMap = mutableMapOf() val resDatas: CwfResDataMap = mutableMapOf()
val testZip = byteArrayToZipInputStream(zipInputStreamToByteArray(zipInputStream)) val zipInputStream = byteArrayToZipInputStream(byteArray)
try { try {
var zipEntry: ZipEntry? = testZip.nextEntry var zipEntry: ZipEntry? = zipInputStream.nextEntry
while (zipEntry != null) { while (zipEntry != null) {
val entryName = zipEntry.name val entryName = zipEntry.name
val buffer = ByteArray(2048) val buffer = ByteArray(2048)
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
var count = testZip.read(buffer) var count = zipInputStream.read(buffer)
while (count != -1) { while (count != -1) {
byteArrayOutputStream.write(buffer, 0, count) byteArrayOutputStream.write(buffer, 0, count)
count = testZip.read(buffer) count = zipInputStream.read(buffer)
} }
testZip.closeEntry() zipInputStream.closeEntry()
if (entryName == CWF_JSON_FILE) { if (entryName == CWF_JSON_FILE) {
val jsonString = byteArrayOutputStream.toByteArray().toString(Charsets.UTF_8) val jsonString = byteArrayOutputStream.toByteArray().toString(Charsets.UTF_8)
@ -328,12 +326,12 @@ class ZipWatchfaceFormat {
} else if (drawableFormat != ResFormat.UNKNOWN) } else if (drawableFormat != ResFormat.UNKNOWN)
resDatas[entryName.substringBeforeLast(".")] = ResData(byteArrayOutputStream.toByteArray(), drawableFormat) resDatas[entryName.substringBeforeLast(".")] = ResData(byteArrayOutputStream.toByteArray(), drawableFormat)
} }
zipEntry = testZip.nextEntry zipEntry = zipInputStream.nextEntry
} }
// Valid CWF file must contains a valid json file with a name within metadata and a custom watchface image // Valid CWF file must contains a valid json file with a name within metadata and a custom watchface image
return if (metadata.containsKey(CwfMetadataKey.CWF_NAME) && resDatas.containsKey(ResFileMap.CUSTOM_WATCHFACE.fileName)) return if (metadata.containsKey(CwfMetadataKey.CWF_NAME) && resDatas.containsKey(ResFileMap.CUSTOM_WATCHFACE.fileName))
CwfFile(CwfData(json.toString(4), metadata, resDatas), zipInputStreamToByteArray(zipInputStream)) CwfFile(CwfData(json.toString(4), metadata, resDatas), byteArray)
else else
null null
@ -378,16 +376,6 @@ class ZipWatchfaceFormat {
return metadata return metadata
} }
fun zipInputStreamToByteArray(zipInputStream: ZipInputStream): ByteArray {
val buffer = ByteArray(1024)
val byteArrayOutputStream = ByteArrayOutputStream()
var len: Int
while (zipInputStream.read(buffer).also { len = it } > 0) {
byteArrayOutputStream.write(buffer, 0, len)
}
return byteArrayOutputStream.toByteArray()
}
fun byteArrayToZipInputStream(byteArray: ByteArray): ZipInputStream { fun byteArrayToZipInputStream(byteArray: ByteArray): ZipInputStream {
val byteArrayInputStream = ByteArrayInputStream(byteArray) val byteArrayInputStream = ByteArrayInputStream(byteArray)
return ZipInputStream(byteArrayInputStream) return ZipInputStream(byteArrayInputStream)

View file

@ -116,7 +116,9 @@
<string name="key_wearwizard_cob" translatable="false">wearwizard_cob</string> <string name="key_wearwizard_cob" translatable="false">wearwizard_cob</string>
<string name="key_wearwizard_iob" translatable="false">wearwizard_iob</string> <string name="key_wearwizard_iob" translatable="false">wearwizard_iob</string>
<string name="key_wear_custom_watchface_autorization" translatable="false">wear_custom_watchface_autorization</string> <string name="key_wear_custom_watchface_autorization" translatable="false">wear_custom_watchface_autorization</string>
<string name="key_wear_custom_watchface_save_cwfData" translatable="false">wear_custom_watchface_save_cwfdata</string> <string name="key_wear_cwf_watchface_name" translatable="false">wear_cwf_watchface_name</string>
<string name="key_wear_cwf_author_version" translatable="false">wear_cwf_author_version</string>
<string name="key_wear_cwf_filename" translatable="false">wear_cwf_filename</string>
<string name="key_objectives_bg_is_available_in_ns" translatable="false">ObjectivesbgIsAvailableInNS</string> <string name="key_objectives_bg_is_available_in_ns" translatable="false">ObjectivesbgIsAvailableInNS</string>
<string name="key_objectives_pump_status_is_available_in_ns" translatable="false">ObjectivespumpStatusIsAvailableInNS</string> <string name="key_objectives_pump_status_is_available_in_ns" translatable="false">ObjectivespumpStatusIsAvailableInNS</string>
<string name="key_statuslights_cage_warning" translatable="false">statuslights_cage_warning</string> <string name="key_statuslights_cage_warning" translatable="false">statuslights_cage_warning</string>

View file

@ -102,7 +102,7 @@ class PrefFileListProviderImpl @Inject constructor(
val customWatchfaceFiles = mutableListOf<CwfFile>() val customWatchfaceFiles = mutableListOf<CwfFile>()
val customWatchfaceAuthorization = sp.getBoolean(app.aaps.core.utils.R.string.key_wear_custom_watchface_autorization, false) val customWatchfaceAuthorization = sp.getBoolean(app.aaps.core.utils.R.string.key_wear_custom_watchface_autorization, false)
exportsPath.walk().filter { it.isFile && it.name.endsWith(ZipWatchfaceFormat.CWF_EXTENTION) }.forEach { file -> exportsPath.walk().filter { it.isFile && it.name.endsWith(ZipWatchfaceFormat.CWF_EXTENTION) }.forEach { file ->
ZipWatchfaceFormat.loadCustomWatchface(ZipInputStream(file.inputStream()), file.name, customWatchfaceAuthorization)?.also { customWatchface -> ZipWatchfaceFormat.loadCustomWatchface(file.readBytes(), file.name, customWatchfaceAuthorization)?.also { customWatchface ->
customWatchfaceFiles.add(customWatchface) customWatchfaceFiles.add(customWatchface)
} }
} }
@ -111,12 +111,11 @@ class PrefFileListProviderImpl @Inject constructor(
val assetFiles = context.assets.list("") ?: arrayOf() val assetFiles = context.assets.list("") ?: arrayOf()
for (assetFileName in assetFiles) { for (assetFileName in assetFiles) {
if (assetFileName.endsWith(ZipWatchfaceFormat.CWF_EXTENTION)) { if (assetFileName.endsWith(ZipWatchfaceFormat.CWF_EXTENTION)) {
val assetInputStream = context.assets.open(assetFileName) val assetByteArray = context.assets.open(assetFileName).readBytes()
ZipWatchfaceFormat.loadCustomWatchface(ZipInputStream(assetInputStream), assetFileName, customWatchfaceAuthorization)?.also { customWatchface -> ZipWatchfaceFormat.loadCustomWatchface(assetByteArray, assetFileName, customWatchfaceAuthorization)?.also { customWatchface ->
customWatchfaceFiles.add(customWatchface) customWatchfaceFiles.add(customWatchface)
//rxBus.send(EventData.ActionGetCustomWatchface(EventData.ActionSetCustomWatchface(customWatchface.cwfData), exportFile = true, withDate = false)) rxBus.send(EventData.ActionGetCustomWatchface(EventData.ActionSetCustomWatchface(customWatchface.cwfData), exportFile = true, withDate = false))
} }
assetInputStream.close()
} }
} }
} catch (e: Exception) { } catch (e: Exception) {

View file

@ -70,13 +70,13 @@ class CustomWatchfaceImportListActivity : TranslatedDaggerAppCompatActivity() {
root.isClickable = true root.isClickable = true
customWatchfaceImportListItemBinding.root.setOnClickListener { customWatchfaceImportListItemBinding.root.setOnClickListener {
val customWatchfaceFile = filelistName.tag as CwfFile val customWatchfaceFile = filelistName.tag as CwfFile
val cwfData = CwfData(customWatchfaceFile.cwfData.json, customWatchfaceFile.cwfData.metadata, mutableMapOf()) sp.putString(app.aaps.core.utils.R.string.key_wear_cwf_watchface_name, customWatchfaceFile.cwfData.metadata[CWF_NAME] ?:"")
//Save json and metadata sp.putString(app.aaps.core.utils.R.string.key_wear_cwf_author_version, customWatchfaceFile.cwfData.metadata[CWF_AUTHOR_VERSION] ?:"")
sp.putString(app.aaps.core.utils.R.string.key_wear_custom_watchface_save_cwfData, EventData.ActionSetCustomWatchface(cwfData).serialize()) sp.putString(app.aaps.core.utils.R.string.key_wear_cwf_filename, customWatchfaceFile.cwfData.metadata[CWF_FILENAME] ?:"")
val i = Intent() val i = Intent()
setResult(FragmentActivity.RESULT_OK, i) setResult(FragmentActivity.RESULT_OK, i)
rxBus.send(EventMobileDataToWear(customWatchfaceFile.zipByteArray)) rxBus.send(EventMobileDataToWear(customWatchfaceFile.zipByteArray))
aapsLogger.debug("XXXXX: ${customWatchfaceFile.zipByteArray.size}")
finish() finish()
} }
} }

View file

@ -11,7 +11,6 @@ import app.aaps.core.interfaces.rx.bus.RxBus
import app.aaps.core.interfaces.rx.events.EventAutosensCalculationFinished import app.aaps.core.interfaces.rx.events.EventAutosensCalculationFinished
import app.aaps.core.interfaces.rx.events.EventDismissBolusProgressIfRunning import app.aaps.core.interfaces.rx.events.EventDismissBolusProgressIfRunning
import app.aaps.core.interfaces.rx.events.EventLoopUpdateGui import app.aaps.core.interfaces.rx.events.EventLoopUpdateGui
import app.aaps.core.interfaces.rx.events.EventMobileDataToWear
import app.aaps.core.interfaces.rx.events.EventMobileToWear import app.aaps.core.interfaces.rx.events.EventMobileToWear
import app.aaps.core.interfaces.rx.events.EventOverviewBolusProgress import app.aaps.core.interfaces.rx.events.EventOverviewBolusProgress
import app.aaps.core.interfaces.rx.events.EventPreferenceChange import app.aaps.core.interfaces.rx.events.EventPreferenceChange
@ -112,14 +111,23 @@ class WearPlugin @Inject constructor(
fun checkCustomWatchfacePreferences() { fun checkCustomWatchfacePreferences() {
savedCustomWatchface?.let { cwf -> savedCustomWatchface?.let { cwf ->
val cwf_authorization = sp.getBoolean(app.aaps.core.utils.R.string.key_wear_custom_watchface_autorization, false) val cwfAuthorization = sp.getBoolean(app.aaps.core.utils.R.string.key_wear_custom_watchface_autorization, false)
if (cwf_authorization != cwf.metadata[CwfMetadataKey.CWF_AUTHORIZATION]?.toBooleanStrictOrNull()) { val cwfName = sp.getString(app.aaps.core.utils.R.string.key_wear_cwf_watchface_name, "")
// update new customWatchface to Watch with updated authorization for preferences update val authorVersion = sp.getString(app.aaps.core.utils.R.string.key_wear_cwf_author_version, "")
CwfData(cwf.json, cwf.metadata, mutableMapOf()).also { val fileName = sp.getString(app.aaps.core.utils.R.string.key_wear_cwf_filename, "")
it.metadata[CwfMetadataKey.CWF_AUTHORIZATION] = sp.getBoolean(app.aaps.core.utils.R.string.key_wear_custom_watchface_autorization, false).toString() var toUpdate = false
sp.putString(app.aaps.core.utils.R.string.key_wear_custom_watchface_save_cwfData, EventData.ActionSetCustomWatchface(it).serialize()) CwfData("", cwf.metadata, mutableMapOf()).also {
rxBus.send(EventMobileToWear(EventData.ActionUpdateCustomWatchface(it))) if (cwfAuthorization != cwf.metadata[CwfMetadataKey.CWF_AUTHORIZATION]?.toBooleanStrictOrNull()) {
it.metadata[CwfMetadataKey.CWF_AUTHORIZATION] = cwfAuthorization.toString()
toUpdate = true
} }
if (cwfName == cwf.metadata[CwfMetadataKey.CWF_NAME] && authorVersion == cwf.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] && fileName != cwf.metadata[CwfMetadataKey.CWF_FILENAME]) {
it.metadata[CwfMetadataKey.CWF_FILENAME] = fileName
toUpdate = true
}
if (toUpdate)
rxBus.send(EventMobileToWear(EventData.ActionUpdateCustomWatchface(it)))
} }
} }
} }

View file

@ -1268,27 +1268,19 @@ class DataHandlerMobile @Inject constructor(
private fun handleGetCustomWatchface(command: EventData.ActionGetCustomWatchface) { private fun handleGetCustomWatchface(command: EventData.ActionGetCustomWatchface) {
val customWatchface = command.customWatchface val customWatchface = command.customWatchface
aapsLogger.debug(LTag.WEAR, "Custom Watchface received from ${command.sourceNodeId}: ${customWatchface.customWatchfaceData.json}") aapsLogger.debug(LTag.WEAR, "Custom Watchface received from ${command.sourceNodeId}")
try { val cwfData = customWatchface.customWatchfaceData
rxBus.send(EventWearUpdateGui(cwfData, command.exportFile))
val watchfaceName = sp.getString(app.aaps.core.utils.R.string.key_wear_cwf_watchface_name, "")
val authorVersion = sp.getString(app.aaps.core.utils.R.string.key_wear_cwf_author_version, "")
if (cwfData.metadata[CwfMetadataKey.CWF_NAME] != watchfaceName || cwfData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] != authorVersion) {
sp.putString(app.aaps.core.utils.R.string.key_wear_cwf_watchface_name, cwfData.metadata[CwfMetadataKey.CWF_NAME] ?:"")
sp.putString(app.aaps.core.utils.R.string.key_wear_cwf_author_version, cwfData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] ?:"")
sp.putString(app.aaps.core.utils.R.string.key_wear_cwf_filename, cwfData.metadata[CwfMetadataKey.CWF_FILENAME] ?:"")
}
var s = sp.getStringOrNull(app.aaps.core.utils.R.string.key_wear_custom_watchface_save_cwfData, null)
if (s != null) {
(EventData.deserialize(s) as EventData.ActionSetCustomWatchface).also { savedCwData ->
if (customWatchface.customWatchfaceData.json != savedCwData.customWatchfaceData.json &&
customWatchface.customWatchfaceData.metadata[CwfMetadataKey.CWF_NAME] == savedCwData.customWatchfaceData.metadata[CwfMetadataKey.CWF_NAME] &&
customWatchface.customWatchfaceData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] == savedCwData.customWatchfaceData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION]
) {
// if different json but same name and author version, then resync json and metadata to watch to update filename and authorization
rxBus.send(EventMobileToWear(EventData.ActionUpdateCustomWatchface(savedCwData.customWatchfaceData)))
}
}
}
} catch (exception: Exception) {
aapsLogger.error(LTag.WEAR, exception.toString())
}
rxBus.send(EventWearUpdateGui(customWatchface.customWatchfaceData, command.exportFile))
if (command.exportFile) if (command.exportFile)
importExportPrefs.exportCustomWatchface(customWatchface.customWatchfaceData, command.withDate) importExportPrefs.exportCustomWatchface(cwfData, command.withDate)
} }
} }

View file

@ -214,7 +214,6 @@ class DataLayerListenerServiceMobile : WearableListenerService() {
private fun sendMessage(path: String, data: ByteArray) { private fun sendMessage(path: String, data: ByteArray) {
aapsLogger.debug(LTag.WEAR, "sendMessage: $path") aapsLogger.debug(LTag.WEAR, "sendMessage: $path")
aapsLogger.debug("XXXXX: $path, ${data.size}")
transcriptionNodeId?.also { nodeId -> transcriptionNodeId?.also { nodeId ->
messageClient messageClient
.sendMessage(nodeId, path, data).apply { .sendMessage(nodeId, path, data).apply {

View file

@ -129,11 +129,9 @@ class DataLayerListenerServiceWear : WearableListenerService() {
rxDataPath -> { rxDataPath -> {
aapsLogger.debug(LTag.WEAR, "onMessageReceived: ${messageEvent.data.size}") aapsLogger.debug(LTag.WEAR, "onMessageReceived: ${messageEvent.data.size}")
ZipWatchfaceFormat.loadCustomWatchface(ZipWatchfaceFormat.byteArrayToZipInputStream(messageEvent.data), "NewWatchface", false)?.let { ZipWatchfaceFormat.loadCustomWatchface(messageEvent.data, "", false)?.let {
val command = EventData.ActionSetCustomWatchface(it.cwfData) val command = EventData.ActionSetCustomWatchface(it.cwfData)
rxBus.send(command.also { it.sourceNodeId = messageEvent.sourceNodeId }) rxBus.send(command.also { it.sourceNodeId = messageEvent.sourceNodeId })
aapsLogger.debug("XXXXX: ${it.cwfData.json}")
} }
// Use this sender // Use this sender
transcriptionNodeId = messageEvent.sourceNodeId transcriptionNodeId = messageEvent.sourceNodeId

View file

@ -203,8 +203,8 @@ open class Persistence @Inject constructor(
if (customWatchface.customWatchfaceData.metadata[CwfMetadataKey.CWF_NAME] == savedCwData.customWatchfaceData.metadata[CwfMetadataKey.CWF_NAME] && if (customWatchface.customWatchfaceData.metadata[CwfMetadataKey.CWF_NAME] == savedCwData.customWatchfaceData.metadata[CwfMetadataKey.CWF_NAME] &&
customWatchface.customWatchfaceData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] == savedCwData.customWatchfaceData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] customWatchface.customWatchfaceData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION] == savedCwData.customWatchfaceData.metadata[CwfMetadataKey.CWF_AUTHOR_VERSION]
) { ) {
// if different json but same name and author version, then resync json and metadata to watch to update filename and authorization // if same name and author version, then resync metadata to watch to update filename and authorization
val newCwfData = CwfData(customWatchface.customWatchfaceData.json, customWatchface.customWatchfaceData.metadata, savedCwData.customWatchfaceData.resDatas) val newCwfData = CwfData(savedCwData.customWatchfaceData.json, customWatchface.customWatchfaceData.metadata, savedCwData.customWatchfaceData.resDatas)
EventData.ActionSetCustomWatchface(newCwfData).also { EventData.ActionSetCustomWatchface(newCwfData).also {
putString(CUSTOM_WATCHFACE, it.serialize()) putString(CUSTOM_WATCHFACE, it.serialize())
aapsLogger.debug(LTag.WEAR, "Update Custom Watchface ${it.customWatchfaceData} : $customWatchface") aapsLogger.debug(LTag.WEAR, "Update Custom Watchface ${it.customWatchfaceData} : $customWatchface")

View file

@ -44,8 +44,7 @@ import app.aaps.core.interfaces.rx.weardata.ResFileMap
import app.aaps.core.interfaces.rx.weardata.ResFormat import app.aaps.core.interfaces.rx.weardata.ResFormat
import app.aaps.core.interfaces.rx.weardata.ViewKeys import app.aaps.core.interfaces.rx.weardata.ViewKeys
import app.aaps.core.interfaces.rx.weardata.ZipWatchfaceFormat import app.aaps.core.interfaces.rx.weardata.ZipWatchfaceFormat
import app.aaps.core.interfaces.rx.weardata.sameMeta import app.aaps.core.interfaces.rx.weardata.isEquals
import app.aaps.core.interfaces.rx.weardata.sameRes
import app.aaps.wear.R import app.aaps.wear.R
import app.aaps.wear.databinding.ActivityCustomBinding import app.aaps.wear.databinding.ActivityCustomBinding
import app.aaps.wear.watchfaces.utils.BaseWatchFace import app.aaps.wear.watchfaces.utils.BaseWatchFace
@ -67,7 +66,6 @@ class CustomWatchface : BaseWatchFace() {
private var lowBatColor = Color.RED private var lowBatColor = Color.RED
private var resDataMap: CwfResDataMap = mutableMapOf() private var resDataMap: CwfResDataMap = mutableMapOf()
private var json = JSONObject() private var json = JSONObject()
private var metadata: CwfMetadataMap = mutableMapOf()
private var jsonString = "" private var jsonString = ""
private val bgColor: Int private val bgColor: Int
get() = when (singleBg.sgvLevel) { get() = when (singleBg.sgvLevel) {
@ -155,9 +153,8 @@ class CustomWatchface : BaseWatchFace() {
updatePref(it.customWatchfaceData.metadata) updatePref(it.customWatchfaceData.metadata)
try { try {
json = JSONObject(it.customWatchfaceData.json) json = JSONObject(it.customWatchfaceData.json)
if (!resDataMap.sameRes(it.customWatchfaceData.resDatas) || !metadata.sameMeta(it.customWatchfaceData.metadata) || jsonString != it.customWatchfaceData.json) { if (!resDataMap.isEquals(it.customWatchfaceData.resDatas) || jsonString != it.customWatchfaceData.json) {
resDataMap = it.customWatchfaceData.resDatas resDataMap = it.customWatchfaceData.resDatas
metadata = it.customWatchfaceData.metadata
jsonString = it.customWatchfaceData.json jsonString = it.customWatchfaceData.json
FontMap.init(this) FontMap.init(this)
ViewMap.init(this) ViewMap.init(this)