Wear cwf Refactoring Rename CustomWatchface by Cwf everywhere

This commit is contained in:
Philoul 2023-08-22 00:55:19 +02:00
parent fb9991ad4b
commit 984767d80f
11 changed files with 100 additions and 101 deletions

View file

@ -1,5 +1,5 @@
package info.nightscout.rx.events package info.nightscout.rx.events
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
class EventWearUpdateGui(val customWatchfaceData: CustomWatchfaceData? = null, val exportFile: Boolean = false) : Event() class EventWearUpdateGui(val customWatchfaceData: CwfData? = null, val exportFile: Boolean = false) : Event()

View file

@ -19,7 +19,7 @@ import java.util.zip.ZipOutputStream
val CUSTOM_VERSION = "0.10" val CUSTOM_VERSION = "0.10"
enum class CustomWatchfaceDrawableDataKey(val key: String, @DrawableRes val icon: Int?, val fileName: String) { enum class CwfDrawableFileMap(val key: String, @DrawableRes val icon: Int?, val fileName: String) {
UNKNOWN("unknown", null, "Unknown"), UNKNOWN("unknown", null, "Unknown"),
CUSTOM_WATCHFACE("customWatchface", R.drawable.watchface_custom, "CustomWatchface"), CUSTOM_WATCHFACE("customWatchface", R.drawable.watchface_custom, "CustomWatchface"),
BACKGROUND(ViewKeys.BACKGROUND.key, R.drawable.background, "Background"), BACKGROUND(ViewKeys.BACKGROUND.key, R.drawable.background, "Background"),
@ -33,10 +33,10 @@ enum class CustomWatchfaceDrawableDataKey(val key: String, @DrawableRes val icon
companion object { companion object {
fun fromKey(key: String): CustomWatchfaceDrawableDataKey = fun fromKey(key: String): CwfDrawableFileMap =
values().firstOrNull { it.key == key } ?: UNKNOWN values().firstOrNull { it.key == key } ?: UNKNOWN
fun fromFileName(file: String): CustomWatchfaceDrawableDataKey = values().firstOrNull { it.fileName == file.substringBeforeLast(".") } ?: UNKNOWN fun fromFileName(file: String): CwfDrawableFileMap = values().firstOrNull { it.fileName == file.substringBeforeLast(".") } ?: UNKNOWN
} }
} }
@ -87,13 +87,13 @@ data class DrawableData(val value: ByteArray, val format: DrawableFormat) {
} }
} }
typealias CustomWatchfaceDrawableDataMap = MutableMap<CustomWatchfaceDrawableDataKey, DrawableData> typealias CwfDrawableDataMap = MutableMap<CwfDrawableFileMap, DrawableData>
typealias CustomWatchfaceMetadataMap = MutableMap<CustomWatchfaceMetadataKey, String> typealias CwfMetadataMap = MutableMap<CwfMetadataKey, String>
@Serializable @Serializable
data class CustomWatchfaceData(val json: String, var metadata: CustomWatchfaceMetadataMap, val drawableDatas: CustomWatchfaceDrawableDataMap) data class CwfData(val json: String, var metadata: CwfMetadataMap, val drawableDatas: CwfDrawableDataMap)
enum class CustomWatchfaceMetadataKey(val key: String, @StringRes val label: Int, val isPref: Boolean) { enum class CwfMetadataKey(val key: String, @StringRes val label: Int, val isPref: Boolean) {
CWF_NAME("name", R.string.metadata_label_watchface_name, false), CWF_NAME("name", R.string.metadata_label_watchface_name, false),
CWF_FILENAME("filename", R.string.metadata_wear_import_filename, false), CWF_FILENAME("filename", R.string.metadata_wear_import_filename, false),
@ -120,7 +120,7 @@ enum class CustomWatchfaceMetadataKey(val key: String, @StringRes val label: Int
companion object { companion object {
fun fromKey(key: String): CustomWatchfaceMetadataKey? = fun fromKey(key: String): CwfMetadataKey? =
values().firstOrNull { it.key == key } values().firstOrNull { it.key == key }
} }
} }
@ -223,13 +223,13 @@ enum class ViewType(@StringRes val comment: Int?) {
class ZipWatchfaceFormat { class ZipWatchfaceFormat {
companion object { companion object {
const val CUSTOM_WF_EXTENTION = ".zip" const val CWF_EXTENTION = ".zip"
const val CUSTOM_JSON_FILE = "CustomWatchface.json" const val CWF_JSON_FILE = "CustomWatchface.json"
fun loadCustomWatchface(cwfFile: File, authorization: Boolean): CustomWatchfaceData? { fun loadCustomWatchface(cwfFile: File, authorization: Boolean): CwfData? {
var json = JSONObject() var json = JSONObject()
var metadata: CustomWatchfaceMetadataMap = mutableMapOf() var metadata: CwfMetadataMap = mutableMapOf()
val drawableDatas: CustomWatchfaceDrawableDataMap = mutableMapOf() val drawableDatas: CwfDrawableDataMap = mutableMapOf()
try { try {
val zipInputStream = ZipInputStream(cwfFile.inputStream()) val zipInputStream = ZipInputStream(cwfFile.inputStream())
@ -246,25 +246,25 @@ class ZipWatchfaceFormat {
} }
zipInputStream.closeEntry() zipInputStream.closeEntry()
if (entryName == CUSTOM_JSON_FILE) { if (entryName == CWF_JSON_FILE) {
val jsonString = byteArrayOutputStream.toByteArray().toString(Charsets.UTF_8) val jsonString = byteArrayOutputStream.toByteArray().toString(Charsets.UTF_8)
json = JSONObject(jsonString) json = JSONObject(jsonString)
metadata = loadMetadata(json) metadata = loadMetadata(json)
metadata[CustomWatchfaceMetadataKey.CWF_FILENAME] = cwfFile.name metadata[CwfMetadataKey.CWF_FILENAME] = cwfFile.name
metadata[CustomWatchfaceMetadataKey.CWF_AUTHORIZATION] = authorization.toString() metadata[CwfMetadataKey.CWF_AUTHORIZATION] = authorization.toString()
} else { } else {
val customWatchfaceDrawableData = CustomWatchfaceDrawableDataKey.fromFileName(entryName) val cwfDrawableFileMap = CwfDrawableFileMap.fromFileName(entryName)
val drawableFormat = DrawableFormat.fromFileName(entryName) val drawableFormat = DrawableFormat.fromFileName(entryName)
if (customWatchfaceDrawableData != CustomWatchfaceDrawableDataKey.UNKNOWN && drawableFormat != DrawableFormat.UNKNOWN) { if (cwfDrawableFileMap != CwfDrawableFileMap.UNKNOWN && drawableFormat != DrawableFormat.UNKNOWN) {
drawableDatas[customWatchfaceDrawableData] = DrawableData(byteArrayOutputStream.toByteArray(), drawableFormat) drawableDatas[cwfDrawableFileMap] = DrawableData(byteArrayOutputStream.toByteArray(), drawableFormat)
} }
} }
zipEntry = zipInputStream.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
if (metadata.containsKey(CustomWatchfaceMetadataKey.CWF_NAME) && drawableDatas.containsKey(CustomWatchfaceDrawableDataKey.CUSTOM_WATCHFACE)) if (metadata.containsKey(CwfMetadataKey.CWF_NAME) && drawableDatas.containsKey(CwfDrawableFileMap.CUSTOM_WATCHFACE))
return CustomWatchfaceData(json.toString(4), metadata, drawableDatas) return CwfData(json.toString(4), metadata, drawableDatas)
else else
return null return null
@ -273,14 +273,14 @@ class ZipWatchfaceFormat {
} }
} }
fun saveCustomWatchface(file: File, customWatchface: CustomWatchfaceData) { fun saveCustomWatchface(file: File, customWatchface: CwfData) {
try { try {
val outputStream = FileOutputStream(file) val outputStream = FileOutputStream(file)
val zipOutputStream = ZipOutputStream(BufferedOutputStream(outputStream)) val zipOutputStream = ZipOutputStream(BufferedOutputStream(outputStream))
// Ajouter le fichier JSON au ZIP // Ajouter le fichier JSON au ZIP
val jsonEntry = ZipEntry(CUSTOM_JSON_FILE) val jsonEntry = ZipEntry(CWF_JSON_FILE)
zipOutputStream.putNextEntry(jsonEntry) zipOutputStream.putNextEntry(jsonEntry)
zipOutputStream.write(customWatchface.json.toByteArray()) zipOutputStream.write(customWatchface.json.toByteArray())
zipOutputStream.closeEntry() zipOutputStream.closeEntry()
@ -299,13 +299,13 @@ class ZipWatchfaceFormat {
} }
fun loadMetadata(contents: JSONObject): CustomWatchfaceMetadataMap { fun loadMetadata(contents: JSONObject): CwfMetadataMap {
val metadata: CustomWatchfaceMetadataMap = mutableMapOf() val metadata: CwfMetadataMap = mutableMapOf()
if (contents.has(JsonKeys.METADATA.key)) { if (contents.has(JsonKeys.METADATA.key)) {
val meta = contents.getJSONObject(JsonKeys.METADATA.key) val meta = contents.getJSONObject(JsonKeys.METADATA.key)
for (key in meta.keys()) { for (key in meta.keys()) {
val metaKey = CustomWatchfaceMetadataKey.fromKey(key) val metaKey = CwfMetadataKey.fromKey(key)
if (metaKey != null) { if (metaKey != null) {
metadata[metaKey] = meta.getString(key) metadata[metaKey] = meta.getString(key)
} }

View file

@ -284,7 +284,7 @@ sealed class EventData : Event() {
} }
@Serializable @Serializable
data class ActionSetCustomWatchface( data class ActionSetCustomWatchface(
val customWatchfaceData: CustomWatchfaceData val customWatchfaceData: CwfData
) : EventData() ) : EventData()
@Serializable @Serializable

View file

@ -2,7 +2,7 @@ package info.nightscout.interfaces.maintenance
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
interface ImportExportPrefs { interface ImportExportPrefs {
@ -11,7 +11,7 @@ interface ImportExportPrefs {
fun importSharedPreferences(fragment: Fragment) fun importSharedPreferences(fragment: Fragment)
fun importCustomWatchface(activity: FragmentActivity) fun importCustomWatchface(activity: FragmentActivity)
fun importCustomWatchface(fragment: Fragment) fun importCustomWatchface(fragment: Fragment)
fun exportCustomWatchface(customWatchface: CustomWatchfaceData) fun exportCustomWatchface(customWatchface: CwfData)
fun prefsFileExists(): Boolean fun prefsFileExists(): Boolean
fun verifyStoragePermissions(fragment: Fragment, onGranted: Runnable) fun verifyStoragePermissions(fragment: Fragment, onGranted: Runnable)
fun exportSharedPreferences(f: Fragment) fun exportSharedPreferences(f: Fragment)

View file

@ -1,6 +1,6 @@
package info.nightscout.interfaces.maintenance package info.nightscout.interfaces.maintenance
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import java.io.File import java.io.File
interface PrefFileListProvider { interface PrefFileListProvider {
@ -13,7 +13,7 @@ interface PrefFileListProvider {
fun newExportCsvFile(): File fun newExportCsvFile(): File
fun newCwfFile(filename: String): File fun newCwfFile(filename: String): File
fun listPreferenceFiles(loadMetadata: Boolean = false): MutableList<PrefsFile> fun listPreferenceFiles(loadMetadata: Boolean = false): MutableList<PrefsFile>
fun listCustomWatchfaceFiles(): MutableList<CustomWatchfaceData> fun listCustomWatchfaceFiles(): MutableList<CwfData>
fun checkMetadata(metadata: Map<PrefsMetadataKey, PrefMetadata>): Map<PrefsMetadataKey, PrefMetadata> fun checkMetadata(metadata: Map<PrefsMetadataKey, PrefMetadata>): Map<PrefsMetadataKey, PrefMetadata>
fun formatExportedAgo(utcTime: String): String fun formatExportedAgo(utcTime: String): String
} }

View file

@ -55,8 +55,8 @@ import info.nightscout.rx.events.EventAppExit
import info.nightscout.rx.events.EventDiaconnG8PumpLogReset import info.nightscout.rx.events.EventDiaconnG8PumpLogReset
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import info.nightscout.rx.weardata.CustomWatchfaceMetadataKey import info.nightscout.rx.weardata.CwfMetadataKey
import info.nightscout.rx.weardata.ZipWatchfaceFormat import info.nightscout.rx.weardata.ZipWatchfaceFormat
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -315,9 +315,9 @@ class ImportExportPrefsImpl @Inject constructor(
} }
} }
override fun exportCustomWatchface(customWatchface: CustomWatchfaceData) { override fun exportCustomWatchface(customWatchface: CwfData) {
prefFileList.ensureExportDirExists() prefFileList.ensureExportDirExists()
val newFile = prefFileList.newCwfFile(customWatchface.metadata[CustomWatchfaceMetadataKey.CWF_FILENAME] ?:"") val newFile = prefFileList.newCwfFile(customWatchface.metadata[CwfMetadataKey.CWF_FILENAME] ?:"")
ZipWatchfaceFormat.saveCustomWatchface(newFile, customWatchface) ZipWatchfaceFormat.saveCustomWatchface(newFile, customWatchface)
} }

View file

@ -17,7 +17,7 @@ import info.nightscout.interfaces.maintenance.PrefsMetadataKey
import info.nightscout.interfaces.maintenance.PrefsStatus import info.nightscout.interfaces.maintenance.PrefsStatus
import info.nightscout.interfaces.storage.Storage import info.nightscout.interfaces.storage.Storage
import info.nightscout.interfaces.versionChecker.VersionCheckerUtils import info.nightscout.interfaces.versionChecker.VersionCheckerUtils
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import info.nightscout.rx.weardata.ZipWatchfaceFormat import info.nightscout.rx.weardata.ZipWatchfaceFormat
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -92,11 +92,11 @@ class PrefFileListProviderImpl @Inject constructor(
return prefFiles return prefFiles
} }
override fun listCustomWatchfaceFiles(): MutableList<CustomWatchfaceData> { override fun listCustomWatchfaceFiles(): MutableList<CwfData> {
val customWatchfaceFiles = mutableListOf<CustomWatchfaceData>() val customWatchfaceFiles = mutableListOf<CwfData>()
val customAwtchfaceAuthorization = sp.getBoolean(info.nightscout.core.utils.R.string.key_wear_custom_watchface_autorization, false) val customAwtchfaceAuthorization = sp.getBoolean(info.nightscout.core.utils.R.string.key_wear_custom_watchface_autorization, false)
// searching dedicated dir, only for new CWF format // searching dedicated dir, only for new CWF format
exportsPath.walk().filter { it.isFile && it.name.endsWith(ZipWatchfaceFormat.CUSTOM_WF_EXTENTION) }.forEach { file -> exportsPath.walk().filter { it.isFile && it.name.endsWith(ZipWatchfaceFormat.CWF_EXTENTION) }.forEach { file ->
// Here loadCustomWatchface will unzip, check and load CustomWatchface // Here loadCustomWatchface will unzip, check and load CustomWatchface
ZipWatchfaceFormat.loadCustomWatchface(file, customAwtchfaceAuthorization)?.also { customWatchface -> ZipWatchfaceFormat.loadCustomWatchface(file, customAwtchfaceAuthorization)?.also { customWatchface ->
customWatchfaceFiles.add(customWatchface) customWatchfaceFiles.add(customWatchface)
@ -148,7 +148,7 @@ class PrefFileListProviderImpl @Inject constructor(
} }
override fun newCwfFile(filename: String): File { override fun newCwfFile(filename: String): File {
val timeLocal = LocalDateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd'_'HHmmss")) val timeLocal = LocalDateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd'_'HHmmss"))
return File(exportsPath, "${filename}_$timeLocal${ZipWatchfaceFormat.CUSTOM_WF_EXTENTION}") return File(exportsPath, "${filename}_$timeLocal${ZipWatchfaceFormat.CWF_EXTENTION}")
} }
// check metadata for known issues, change their status and add info with explanations // check metadata for known issues, change their status and add info with explanations

View file

@ -19,10 +19,10 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventMobileDataToWear import info.nightscout.rx.events.EventMobileDataToWear
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.weardata.CUSTOM_VERSION import info.nightscout.rx.weardata.CUSTOM_VERSION
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import info.nightscout.rx.weardata.CustomWatchfaceDrawableDataKey import info.nightscout.rx.weardata.CwfDrawableFileMap
import info.nightscout.rx.weardata.CustomWatchfaceMetadataKey.* import info.nightscout.rx.weardata.CwfMetadataKey.*
import info.nightscout.rx.weardata.CustomWatchfaceMetadataMap import info.nightscout.rx.weardata.CwfMetadataMap
import info.nightscout.rx.weardata.EventData import info.nightscout.rx.weardata.EventData
import info.nightscout.shared.extensions.toVisibility import info.nightscout.shared.extensions.toVisibility
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
@ -55,7 +55,7 @@ class CustomWatchfaceImportListActivity: TranslatedDaggerAppCompatActivity() {
binding.recyclerview.adapter = RecyclerViewAdapter(prefFileListProvider.listCustomWatchfaceFiles()) binding.recyclerview.adapter = RecyclerViewAdapter(prefFileListProvider.listCustomWatchfaceFiles())
} }
inner class RecyclerViewAdapter internal constructor(private var customWatchfaceFileList: List<CustomWatchfaceData>) : RecyclerView.Adapter<RecyclerViewAdapter.PrefFileViewHolder>() { inner class RecyclerViewAdapter internal constructor(private var customWatchfaceFileList: List<CwfData>) : RecyclerView.Adapter<RecyclerViewAdapter.PrefFileViewHolder>() {
inner class PrefFileViewHolder(val customWatchfaceImportListItemBinding: CustomWatchfaceImportListItemBinding) : RecyclerView.ViewHolder(customWatchfaceImportListItemBinding.root) { inner class PrefFileViewHolder(val customWatchfaceImportListItemBinding: CustomWatchfaceImportListItemBinding) : RecyclerView.ViewHolder(customWatchfaceImportListItemBinding.root) {
@ -63,7 +63,7 @@ class CustomWatchfaceImportListActivity: TranslatedDaggerAppCompatActivity() {
with(customWatchfaceImportListItemBinding) { with(customWatchfaceImportListItemBinding) {
root.isClickable = true root.isClickable = true
customWatchfaceImportListItemBinding.root.setOnClickListener { customWatchfaceImportListItemBinding.root.setOnClickListener {
val customWatchfaceFile = filelistName.tag as CustomWatchfaceData val customWatchfaceFile = filelistName.tag as CwfData
val customWF = EventData.ActionSetCustomWatchface(customWatchfaceFile) val customWF = EventData.ActionSetCustomWatchface(customWatchfaceFile)
val i = Intent() val i = Intent()
setResult(FragmentActivity.RESULT_OK, i) setResult(FragmentActivity.RESULT_OK, i)
@ -86,7 +86,7 @@ class CustomWatchfaceImportListActivity: TranslatedDaggerAppCompatActivity() {
override fun onBindViewHolder(holder: PrefFileViewHolder, position: Int) { override fun onBindViewHolder(holder: PrefFileViewHolder, position: Int) {
val customWatchfaceFile = customWatchfaceFileList[position] val customWatchfaceFile = customWatchfaceFileList[position]
val metadata = customWatchfaceFile.metadata val metadata = customWatchfaceFile.metadata
val drawable = customWatchfaceFile.drawableDatas[CustomWatchfaceDrawableDataKey val drawable = customWatchfaceFile.drawableDatas[CwfDrawableFileMap
.CUSTOM_WATCHFACE]?.toDrawable(resources) .CUSTOM_WATCHFACE]?.toDrawable(resources)
with(holder.customWatchfaceImportListItemBinding) { with(holder.customWatchfaceImportListItemBinding) {
filelistName.text = rh.gs(info.nightscout.shared.R.string.metadata_wear_import_filename, metadata[CWF_FILENAME]) filelistName.text = rh.gs(info.nightscout.shared.R.string.metadata_wear_import_filename, metadata[CWF_FILENAME])
@ -115,7 +115,7 @@ class CustomWatchfaceImportListActivity: TranslatedDaggerAppCompatActivity() {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
private fun checkCustomVersion(metadata: CustomWatchfaceMetadataMap): Boolean { private fun checkCustomVersion(metadata: CwfMetadataMap): Boolean {
metadata[CWF_VERSION]?.let { version -> metadata[CWF_VERSION]?.let { version ->
val currentAppVer = versionCheckerUtils.versionDigits(CUSTOM_VERSION) val currentAppVer = versionCheckerUtils.versionDigits(CUSTOM_VERSION)
val metadataVer = versionCheckerUtils.versionDigits(version) val metadataVer = versionCheckerUtils.versionDigits(version)

View file

@ -4,7 +4,6 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.annotation.StringRes
import dagger.android.support.DaggerFragment import dagger.android.support.DaggerFragment
import info.nightscout.core.ui.toast.ToastUtils import info.nightscout.core.ui.toast.ToastUtils
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
@ -16,9 +15,9 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventMobileToWear import info.nightscout.rx.events.EventMobileToWear
import info.nightscout.rx.events.EventWearUpdateGui import info.nightscout.rx.events.EventWearUpdateGui
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import info.nightscout.rx.weardata.CustomWatchfaceDrawableDataKey import info.nightscout.rx.weardata.CwfDrawableFileMap
import info.nightscout.rx.weardata.CustomWatchfaceMetadataKey import info.nightscout.rx.weardata.CwfMetadataKey
import info.nightscout.rx.weardata.EventData import info.nightscout.rx.weardata.EventData
import info.nightscout.shared.extensions.toVisibility import info.nightscout.shared.extensions.toVisibility
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
@ -106,8 +105,8 @@ class WearFragment : DaggerFragment() {
_binding ?: return _binding ?: return
wearPlugin.savedCustomWatchface?.let { wearPlugin.savedCustomWatchface?.let {
wearPlugin.checkCustomWatchfacePreferences() wearPlugin.checkCustomWatchfacePreferences()
binding.customName.text = rh.gs(R.string.wear_custom_watchface, it.metadata[CustomWatchfaceMetadataKey.CWF_NAME]) binding.customName.text = rh.gs(R.string.wear_custom_watchface, it.metadata[CwfMetadataKey.CWF_NAME])
binding.coverChart.setImageDrawable(it.drawableDatas[CustomWatchfaceDrawableDataKey.CUSTOM_WATCHFACE]?.toDrawable(resources)) binding.coverChart.setImageDrawable(it.drawableDatas[CwfDrawableFileMap.CUSTOM_WATCHFACE]?.toDrawable(resources))
} ?:apply { } ?:apply {
binding.customName.text = rh.gs(R.string.wear_custom_watchface, rh.gs(info.nightscout.shared.R.string.wear_default_watchface)) binding.customName.text = rh.gs(R.string.wear_custom_watchface, rh.gs(info.nightscout.shared.R.string.wear_default_watchface))
binding.coverChart.setImageDrawable(null) binding.coverChart.setImageDrawable(null)
@ -116,7 +115,7 @@ class WearFragment : DaggerFragment() {
binding.customWatchfaceLayout.visibility = (wearPlugin.connectedDevice != rh.gs(R.string.no_watch_connected)).toVisibility() binding.customWatchfaceLayout.visibility = (wearPlugin.connectedDevice != rh.gs(R.string.no_watch_connected)).toVisibility()
} }
private fun loadCustom(cwf: CustomWatchfaceData) { private fun loadCustom(cwf: CwfData) {
wearPlugin.savedCustomWatchface = cwf wearPlugin.savedCustomWatchface = cwf
} }
} }

View file

@ -20,8 +20,8 @@ import info.nightscout.rx.events.EventOverviewBolusProgress
import info.nightscout.rx.events.EventPreferenceChange import info.nightscout.rx.events.EventPreferenceChange
import info.nightscout.rx.events.EventWearUpdateGui import info.nightscout.rx.events.EventWearUpdateGui
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import info.nightscout.rx.weardata.CustomWatchfaceMetadataKey import info.nightscout.rx.weardata.CwfMetadataKey
import info.nightscout.rx.weardata.EventData import info.nightscout.rx.weardata.EventData
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -58,7 +58,7 @@ class WearPlugin @Inject constructor(
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
var connectedDevice = "---" var connectedDevice = "---"
var savedCustomWatchface: CustomWatchfaceData? = null var savedCustomWatchface: CwfData? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -113,10 +113,10 @@ class WearPlugin @Inject constructor(
fun checkCustomWatchfacePreferences() { fun checkCustomWatchfacePreferences() {
savedCustomWatchface?.let { cwf -> savedCustomWatchface?.let { cwf ->
val cwf_authorization = sp.getBoolean(info.nightscout.core.utils.R.string.key_wear_custom_watchface_autorization, false) val cwf_authorization = sp.getBoolean(info.nightscout.core.utils.R.string.key_wear_custom_watchface_autorization, false)
if (cwf_authorization != cwf.metadata[CustomWatchfaceMetadataKey.CWF_AUTHORIZATION]?.toBooleanStrictOrNull()) { if (cwf_authorization != cwf.metadata[CwfMetadataKey.CWF_AUTHORIZATION]?.toBooleanStrictOrNull()) {
// resend new customWatchface to Watch with updated authorization for preferences update // resend new customWatchface to Watch with updated authorization for preferences update
val newCwf = cwf.copy() val newCwf = cwf.copy()
newCwf.metadata[CustomWatchfaceMetadataKey.CWF_AUTHORIZATION] = sp.getBoolean(info.nightscout.core.utils.R.string.key_wear_custom_watchface_autorization, false).toString() newCwf.metadata[CwfMetadataKey.CWF_AUTHORIZATION] = sp.getBoolean(info.nightscout.core.utils.R.string.key_wear_custom_watchface_autorization, false).toString()
rxBus.send(EventMobileDataToWear(EventData.ActionSetCustomWatchface(newCwf))) rxBus.send(EventMobileDataToWear(EventData.ActionSetCustomWatchface(newCwf)))
} }
} }

View file

@ -33,11 +33,11 @@ import info.nightscout.androidaps.databinding.ActivityCustomBinding
import info.nightscout.androidaps.watchfaces.utils.BaseWatchFace import info.nightscout.androidaps.watchfaces.utils.BaseWatchFace
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.rx.weardata.CUSTOM_VERSION import info.nightscout.rx.weardata.CUSTOM_VERSION
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CwfData
import info.nightscout.rx.weardata.CustomWatchfaceDrawableDataKey import info.nightscout.rx.weardata.CwfDrawableFileMap
import info.nightscout.rx.weardata.CustomWatchfaceDrawableDataMap import info.nightscout.rx.weardata.CwfDrawableDataMap
import info.nightscout.rx.weardata.CustomWatchfaceMetadataKey import info.nightscout.rx.weardata.CwfMetadataKey
import info.nightscout.rx.weardata.CustomWatchfaceMetadataMap import info.nightscout.rx.weardata.CwfMetadataMap
import info.nightscout.rx.weardata.DrawableData import info.nightscout.rx.weardata.DrawableData
import info.nightscout.rx.weardata.DrawableFormat import info.nightscout.rx.weardata.DrawableFormat
import info.nightscout.rx.weardata.EventData import info.nightscout.rx.weardata.EventData
@ -166,10 +166,10 @@ class CustomWatchface : BaseWatchFace() {
else -> midColor else -> midColor
} }
val backGroundDrawable = when (singleBg.sgvLevel) { val backGroundDrawable = when (singleBg.sgvLevel) {
1L -> drawableDataMap[CustomWatchfaceDrawableDataKey.BACKGROUND_HIGH]?.toDrawable(resources) ?: drawableDataMap[CustomWatchfaceDrawableDataKey.BACKGROUND]?.toDrawable(resources) 1L -> drawableDataMap[CwfDrawableFileMap.BACKGROUND_HIGH]?.toDrawable(resources) ?: drawableDataMap[CwfDrawableFileMap.BACKGROUND]?.toDrawable(resources)
0L -> drawableDataMap[CustomWatchfaceDrawableDataKey.BACKGROUND]?.toDrawable(resources) 0L -> drawableDataMap[CwfDrawableFileMap.BACKGROUND]?.toDrawable(resources)
-1L -> drawableDataMap[CustomWatchfaceDrawableDataKey.BACKGROUND_LOW]?.toDrawable(resources) ?: drawableDataMap[CustomWatchfaceDrawableDataKey.BACKGROUND]?.toDrawable(resources) -1L -> drawableDataMap[CwfDrawableFileMap.BACKGROUND_LOW]?.toDrawable(resources) ?: drawableDataMap[CwfDrawableFileMap.BACKGROUND]?.toDrawable(resources)
else -> drawableDataMap[CustomWatchfaceDrawableDataKey.BACKGROUND]?.toDrawable(resources) else -> drawableDataMap[CwfDrawableFileMap.BACKGROUND]?.toDrawable(resources)
} }
binding.mainLayout.forEach { view -> binding.mainLayout.forEach { view ->
@ -201,10 +201,10 @@ class CustomWatchface : BaseWatchFace() {
if (view is ImageView) { if (view is ImageView) {
view.clearColorFilter() view.clearColorFilter()
val drawable = if (id.key == CustomWatchfaceDrawableDataKey.BACKGROUND.key) val drawable = if (id.key == CwfDrawableFileMap.BACKGROUND.key)
backGroundDrawable backGroundDrawable
else else
drawableDataMap[CustomWatchfaceDrawableDataKey.fromKey(id.key)]?.toDrawable(resources) drawableDataMap[CwfDrawableFileMap.fromKey(id.key)]?.toDrawable(resources)
drawable?.let { drawable?.let {
if (viewJson.has(COLOR.key)) if (viewJson.has(COLOR.key))
it.colorFilter = changeDrawableColor(getColor(viewJson.getString(COLOR.key))) it.colorFilter = changeDrawableColor(getColor(viewJson.getString(COLOR.key)))
@ -212,7 +212,7 @@ class CustomWatchface : BaseWatchFace() {
it.clearColorFilter() it.clearColorFilter()
view.setImageDrawable(it) view.setImageDrawable(it)
} ?: apply { } ?: apply {
view.setImageDrawable(CustomWatchfaceDrawableDataKey.fromKey(id.key).icon?.let { context.getDrawable(it) }) view.setImageDrawable(CwfDrawableFileMap.fromKey(id.key).icon?.let { context.getDrawable(it) })
if (viewJson.has(COLOR.key)) if (viewJson.has(COLOR.key))
view.setColorFilter(getColor(viewJson.getString(COLOR.key))) view.setColorFilter(getColor(viewJson.getString(COLOR.key)))
else else
@ -237,12 +237,12 @@ class CustomWatchface : BaseWatchFace() {
} }
} }
private fun updatePref(metadata: CustomWatchfaceMetadataMap) { private fun updatePref(metadata: CwfMetadataMap) {
val cwf_authorization = metadata[CustomWatchfaceMetadataKey.CWF_AUTHORIZATION]?.toBooleanStrictOrNull() val cwf_authorization = metadata[CwfMetadataKey.CWF_AUTHORIZATION]?.toBooleanStrictOrNull()
cwf_authorization?.let { authorization -> cwf_authorization?.let { authorization ->
if (authorization) { if (authorization) {
PrefMap.values().forEach { pref -> PrefMap.values().forEach { pref ->
metadata[CustomWatchfaceMetadataKey.fromKey(pref.key)]?.toBooleanStrictOrNull()?.let { sp.putBoolean(pref.prefKey, it) } metadata[CwfMetadataKey.fromKey(pref.key)]?.toBooleanStrictOrNull()?.let { sp.putBoolean(pref.prefKey, it) }
} }
} }
} }
@ -250,13 +250,13 @@ class CustomWatchface : BaseWatchFace() {
private fun defaultWatchface(): EventData.ActionSetCustomWatchface { private fun defaultWatchface(): EventData.ActionSetCustomWatchface {
val metadata = JSONObject() val metadata = JSONObject()
.put(CustomWatchfaceMetadataKey.CWF_NAME.key, getString(info.nightscout.shared.R.string.wear_default_watchface)) .put(CwfMetadataKey.CWF_NAME.key, getString(info.nightscout.shared.R.string.wear_default_watchface))
.put(CustomWatchfaceMetadataKey.CWF_FILENAME.key, getString(info.nightscout.shared.R.string.wear_default_watchface)) .put(CwfMetadataKey.CWF_FILENAME.key, getString(info.nightscout.shared.R.string.wear_default_watchface))
.put(CustomWatchfaceMetadataKey.CWF_AUTHOR.key, "Philoul") .put(CwfMetadataKey.CWF_AUTHOR.key, "Philoul")
.put(CustomWatchfaceMetadataKey.CWF_CREATED_AT.key, dateUtil.dateString(dateUtil.now())) .put(CwfMetadataKey.CWF_CREATED_AT.key, dateUtil.dateString(dateUtil.now()))
.put(CustomWatchfaceMetadataKey.CWF_AUTHOR_VERSION.key, CUSTOM_VERSION) .put(CwfMetadataKey.CWF_AUTHOR_VERSION.key, CUSTOM_VERSION)
.put(CustomWatchfaceMetadataKey.CWF_VERSION.key, CUSTOM_VERSION) .put(CwfMetadataKey.CWF_VERSION.key, CUSTOM_VERSION)
.put(CustomWatchfaceMetadataKey.CWF_COMMENT.key, getString(info.nightscout.shared.R.string.default_custom_watchface_comment)) .put(CwfMetadataKey.CWF_COMMENT.key, getString(info.nightscout.shared.R.string.default_custom_watchface_comment))
val json = JSONObject() val json = JSONObject()
.put(METADATA.key, metadata) .put(METADATA.key, metadata)
.put(HIGHCOLOR.key, String.format("#%06X", 0xFFFFFF and highColor)) .put(HIGHCOLOR.key, String.format("#%06X", 0xFFFFFF and highColor))
@ -304,12 +304,12 @@ class CustomWatchface : BaseWatchFace() {
} }
} }
val metadataMap = ZipWatchfaceFormat.loadMetadata(json) val metadataMap = ZipWatchfaceFormat.loadMetadata(json)
val drawableDataMap: CustomWatchfaceDrawableDataMap = mutableMapOf() val drawableDataMap: CwfDrawableDataMap = mutableMapOf()
getResourceByteArray(info.nightscout.shared.R.drawable.watchface_custom)?.let { getResourceByteArray(info.nightscout.shared.R.drawable.watchface_custom)?.let {
val drawableData = DrawableData(it, DrawableFormat.PNG) val drawableData = DrawableData(it, DrawableFormat.PNG)
drawableDataMap[CustomWatchfaceDrawableDataKey.CUSTOM_WATCHFACE] = drawableData drawableDataMap[CwfDrawableFileMap.CUSTOM_WATCHFACE] = drawableData
} }
return EventData.ActionSetCustomWatchface(CustomWatchfaceData(json.toString(4), metadataMap, drawableDataMap)) return EventData.ActionSetCustomWatchface(CwfData(json.toString(4), metadataMap, drawableDataMap))
} }
private fun setDefaultColors() { private fun setDefaultColors() {
@ -483,20 +483,20 @@ class CustomWatchface : BaseWatchFace() {
// This class containt mapping between keys used within json of Custom Watchface and preferences // This class containt mapping between keys used within json of Custom Watchface and preferences
private enum class PrefMap(val key: String, @StringRes val prefKey: Int) { private enum class PrefMap(val key: String, @StringRes val prefKey: Int) {
SHOW_IOB(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_IOB.key, R.string.key_show_iob), SHOW_IOB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_IOB.key, R.string.key_show_iob),
SHOW_DETAILED_IOB(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_DETAILED_IOB.key, R.string.key_show_detailed_iob), SHOW_DETAILED_IOB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_DETAILED_IOB.key, R.string.key_show_detailed_iob),
SHOW_COB(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_COB.key, R.string.key_show_cob), SHOW_COB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_COB.key, R.string.key_show_cob),
SHOW_DELTA(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_DELTA.key, R.string.key_show_delta), SHOW_DELTA(CwfMetadataKey.CWF_PREF_WATCH_SHOW_DELTA.key, R.string.key_show_delta),
SHOW_AVG_DELTA(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_AVG_DELTA.key, R.string.key_show_avg_delta), SHOW_AVG_DELTA(CwfMetadataKey.CWF_PREF_WATCH_SHOW_AVG_DELTA.key, R.string.key_show_avg_delta),
SHOW_DETAILED_DELTA(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_DETAILED_DELTA.key, R.string.key_show_detailed_delta), SHOW_DETAILED_DELTA(CwfMetadataKey.CWF_PREF_WATCH_SHOW_DETAILED_DELTA.key, R.string.key_show_detailed_delta),
SHOW_UPLOADER_BATTERY(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_UPLOADER_BATTERY.key, R.string.key_show_uploader_battery), SHOW_UPLOADER_BATTERY(CwfMetadataKey.CWF_PREF_WATCH_SHOW_UPLOADER_BATTERY.key, R.string.key_show_uploader_battery),
SHOW_RIG_BATTERY(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_RIG_BATTERY.key, R.string.key_show_rig_battery), SHOW_RIG_BATTERY(CwfMetadataKey.CWF_PREF_WATCH_SHOW_RIG_BATTERY.key, R.string.key_show_rig_battery),
SHOW_TEMP_BASAL(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_TEMP_BASAL.key, R.string.key_show_temp_basal), SHOW_TEMP_BASAL(CwfMetadataKey.CWF_PREF_WATCH_SHOW_TEMP_BASAL.key, R.string.key_show_temp_basal),
SHOW_DIRECTION(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_DIRECTION.key, R.string.key_show_direction), SHOW_DIRECTION(CwfMetadataKey.CWF_PREF_WATCH_SHOW_DIRECTION.key, R.string.key_show_direction),
SHOW_AGO(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_AGO.key, R.string.key_show_ago), SHOW_AGO(CwfMetadataKey.CWF_PREF_WATCH_SHOW_AGO.key, R.string.key_show_ago),
SHOW_BG(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_BG.key, R.string.key_show_bg), SHOW_BG(CwfMetadataKey.CWF_PREF_WATCH_SHOW_BG.key, R.string.key_show_bg),
SHOW_BGI(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_BGI.key, R.string.key_show_bgi), SHOW_BGI(CwfMetadataKey.CWF_PREF_WATCH_SHOW_BGI.key, R.string.key_show_bgi),
SHOW_LOOP_STATUS(CustomWatchfaceMetadataKey.CWF_PREF_WATCH_SHOW_LOOP_STATUS.key, R.string.key_show_external_status) SHOW_LOOP_STATUS(CwfMetadataKey.CWF_PREF_WATCH_SHOW_LOOP_STATUS.key, R.string.key_show_external_status)
} }
} }