diff --git a/app/src/main/java/io/github/sds100/keymapper/data/db/SeedDatabaseWorker.kt b/app/src/main/java/io/github/sds100/keymapper/data/db/SeedDatabaseWorker.kt index 3fba3334c5..4a8ef823d7 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/db/SeedDatabaseWorker.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/db/SeedDatabaseWorker.kt @@ -9,6 +9,7 @@ import io.github.sds100.keymapper.ServiceLocator import io.github.sds100.keymapper.data.entities.ActionEntity import io.github.sds100.keymapper.data.entities.KeyMapEntity import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity import kotlinx.coroutines.coroutineScope /** @@ -45,25 +46,25 @@ class SeedDatabaseWorker( private fun createRandomTrigger(): TriggerEntity { val keys = sequence { yield( - TriggerEntity.KeyEntity( + TriggerKeyEntity( KeyEvent.KEYCODE_CTRL_LEFT, - TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE, + TriggerKeyEntity.DEVICE_ID_THIS_DEVICE, null, TriggerEntity.SHORT_PRESS, ), ) yield( - TriggerEntity.KeyEntity( + TriggerKeyEntity( KeyEvent.KEYCODE_ALT_LEFT, - TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE, + TriggerKeyEntity.DEVICE_ID_ANY_DEVICE, null, TriggerEntity.LONG_PRESS, ), ) yield( - TriggerEntity.KeyEntity( + TriggerKeyEntity( KeyEvent.KEYCODE_DEL, - TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE, + TriggerKeyEntity.DEVICE_ID_THIS_DEVICE, null, TriggerEntity.SHORT_PRESS, ), diff --git a/app/src/main/java/io/github/sds100/keymapper/data/db/typeconverter/TriggerTypeConverter.kt b/app/src/main/java/io/github/sds100/keymapper/data/db/typeconverter/TriggerTypeConverter.kt index 497e31718a..392e2d9015 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/db/typeconverter/TriggerTypeConverter.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/db/typeconverter/TriggerTypeConverter.kt @@ -7,6 +7,7 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import io.github.sds100.keymapper.data.entities.Extra import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity /** * Created by sds100 on 05/09/2018. @@ -17,7 +18,7 @@ class TriggerTypeConverter { fun toTrigger(json: String): TriggerEntity { val gson = GsonBuilder() .registerTypeAdapter(TriggerEntity.DESERIALIZER) - .registerTypeAdapter(TriggerEntity.KeyEntity.DESERIALIZER) + .registerTypeAdapter(TriggerKeyEntity.DESERIALIZER) .registerTypeAdapter(Extra.DESERIALIZER).create() return gson.fromJson(json) diff --git a/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerEntity.kt b/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerEntity.kt index 650712d408..1b5a86df39 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerEntity.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerEntity.kt @@ -5,12 +5,9 @@ import androidx.annotation.IntDef import com.github.salomonbrys.kotson.byArray import com.github.salomonbrys.kotson.byInt import com.github.salomonbrys.kotson.byNullableInt -import com.github.salomonbrys.kotson.byNullableString -import com.github.salomonbrys.kotson.byString import com.github.salomonbrys.kotson.jsonDeserializer import com.google.gson.annotations.SerializedName import kotlinx.android.parcel.Parcelize -import java.util.UUID /** * Created by sds100 on 16/07/2018. @@ -19,7 +16,7 @@ import java.util.UUID @Parcelize data class TriggerEntity( @SerializedName(NAME_KEYS) - val keys: List = listOf(), + val keys: List = listOf(), @SerializedName(NAME_EXTRAS) val extras: List = listOf(), @@ -64,7 +61,7 @@ data class TriggerEntity( val DESERIALIZER = jsonDeserializer { val triggerKeysJsonArray by it.json.byArray(NAME_KEYS) - val keys = it.context.deserialize>(triggerKeysJsonArray) + val keys = it.context.deserialize>(triggerKeysJsonArray) val extrasJsonArray by it.json.byArray(NAME_EXTRAS) val extraList = it.context.deserialize>(extrasJsonArray) ?: listOf() @@ -77,65 +74,6 @@ data class TriggerEntity( } } - // TODO move to separate file - @Parcelize - data class KeyEntity( - @SerializedName(NAME_KEYCODE) - val keyCode: Int, - @SerializedName(NAME_DEVICE_ID) - val deviceId: String = DEVICE_ID_THIS_DEVICE, - - @SerializedName(NAME_DEVICE_NAME) - val deviceName: String? = null, - - @ClickType - @SerializedName(NAME_CLICK_TYPE) - val clickType: Int = SHORT_PRESS, - - @SerializedName(NAME_FLAGS) - val flags: Int = 0, - - @SerializedName(NAME_UID) - val uid: String = UUID.randomUUID().toString(), - ) : Parcelable { - - companion object { - // DON'T CHANGE THESE. Used for JSON serialization and parsing. - const val NAME_KEYCODE = "keyCode" - const val NAME_DEVICE_ID = "deviceId" - const val NAME_DEVICE_NAME = "deviceName" - const val NAME_CLICK_TYPE = "clickType" - const val NAME_FLAGS = "flags" - const val NAME_UID = "uid" - - // IDS! DON'T CHANGE - const val DEVICE_ID_THIS_DEVICE = "io.github.sds100.keymapper.THIS_DEVICE" - const val DEVICE_ID_ANY_DEVICE = "io.github.sds100.keymapper.ANY_DEVICE" - - const val FLAG_DO_NOT_CONSUME_KEY_EVENT = 1 - - val DESERIALIZER = jsonDeserializer { - val keycode by it.json.byInt(NAME_KEYCODE) - val deviceId by it.json.byString(NAME_DEVICE_ID) - val deviceName by it.json.byNullableString(NAME_DEVICE_NAME) - val clickType by it.json.byInt(NAME_CLICK_TYPE) - - // nullable because this property was added after backup and restore was released. - val flags by it.json.byNullableInt(NAME_FLAGS) - val uid by it.json.byNullableString(NAME_UID) - - KeyEntity( - keycode, - deviceId, - deviceName, - clickType, - flags ?: 0, - uid ?: UUID.randomUUID().toString(), - ) - } - } - } - @IntDef(value = [PARALLEL, SEQUENCE, UNDEFINED]) annotation class Mode diff --git a/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerKeyEntity.kt b/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerKeyEntity.kt new file mode 100644 index 0000000000..e21ff95a15 --- /dev/null +++ b/app/src/main/java/io/github/sds100/keymapper/data/entities/TriggerKeyEntity.kt @@ -0,0 +1,69 @@ +package io.github.sds100.keymapper.data.entities + +import android.os.Parcelable +import com.github.salomonbrys.kotson.byInt +import com.github.salomonbrys.kotson.byNullableInt +import com.github.salomonbrys.kotson.byNullableString +import com.github.salomonbrys.kotson.byString +import com.github.salomonbrys.kotson.jsonDeserializer +import com.google.gson.annotations.SerializedName +import kotlinx.android.parcel.Parcelize +import java.util.UUID + +@Parcelize +data class TriggerKeyEntity( + @SerializedName(NAME_KEYCODE) + val keyCode: Int, + @SerializedName(NAME_DEVICE_ID) + val deviceId: String = DEVICE_ID_THIS_DEVICE, + + @SerializedName(NAME_DEVICE_NAME) + val deviceName: String? = null, + + @TriggerEntity.ClickType + @SerializedName(NAME_CLICK_TYPE) + val clickType: Int = TriggerEntity.SHORT_PRESS, + + @SerializedName(NAME_FLAGS) + val flags: Int = 0, + + @SerializedName(NAME_UID) + val uid: String = UUID.randomUUID().toString(), +) : Parcelable { + + companion object { + // DON'T CHANGE THESE. Used for JSON serialization and parsing. + const val NAME_KEYCODE = "keyCode" + const val NAME_DEVICE_ID = "deviceId" + const val NAME_DEVICE_NAME = "deviceName" + const val NAME_CLICK_TYPE = "clickType" + const val NAME_FLAGS = "flags" + const val NAME_UID = "uid" + + // IDS! DON'T CHANGE + const val DEVICE_ID_THIS_DEVICE = "io.github.sds100.keymapper.THIS_DEVICE" + const val DEVICE_ID_ANY_DEVICE = "io.github.sds100.keymapper.ANY_DEVICE" + + const val FLAG_DO_NOT_CONSUME_KEY_EVENT = 1 + + val DESERIALIZER = jsonDeserializer { + val keycode by it.json.byInt(NAME_KEYCODE) + val deviceId by it.json.byString(NAME_DEVICE_ID) + val deviceName by it.json.byNullableString(NAME_DEVICE_NAME) + val clickType by it.json.byInt(NAME_CLICK_TYPE) + + // nullable because this property was added after backup and restore was released. + val flags by it.json.byNullableInt(NAME_FLAGS) + val uid by it.json.byNullableString(NAME_UID) + + TriggerKeyEntity( + keycode, + deviceId, + deviceName, + clickType, + flags ?: 0, + uid ?: UUID.randomUUID().toString(), + ) + } + } +} diff --git a/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration1To2.kt b/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration1To2.kt index 30e5928f55..1f073afdb8 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration1To2.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration1To2.kt @@ -13,7 +13,7 @@ import com.google.gson.JsonArray import com.google.gson.JsonObject import com.google.gson.JsonParser import io.github.sds100.keymapper.data.entities.ActionEntity -import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity import splitties.bitflags.hasFlag import timber.log.Timber @@ -107,7 +107,7 @@ object Migration1To2 { createTriggerKey2( it.asInt, - TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE, + TriggerKeyEntity.DEVICE_ID_ANY_DEVICE, clickType, ) } diff --git a/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration6To7.kt b/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration6To7.kt index 73cca21b58..e8e8693a1c 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration6To7.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/migration/Migration6To7.kt @@ -9,6 +9,7 @@ import com.github.salomonbrys.kotson.registerTypeAdapter import com.google.gson.Gson import com.google.gson.GsonBuilder import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity import splitties.bitflags.hasFlag import splitties.bitflags.minusFlag import splitties.bitflags.withFlag @@ -40,7 +41,7 @@ object Migration6To7 { val newTriggerKeys = trigger.keys.map { if (trigger.flags.hasFlag(TRIGGER_FLAG_DONT_OVERRIDE_DEFAULT_ACTION)) { - it.copy(flags = it.flags.withFlag(TriggerEntity.KeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT)) + it.copy(flags = it.flags.withFlag(TriggerKeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT)) } else { it } diff --git a/app/src/main/java/io/github/sds100/keymapper/data/repositories/RoomKeyMapRepository.kt b/app/src/main/java/io/github/sds100/keymapper/data/repositories/RoomKeyMapRepository.kt index 8ab8745a38..a1545f9583 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/repositories/RoomKeyMapRepository.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/repositories/RoomKeyMapRepository.kt @@ -4,7 +4,7 @@ import io.github.sds100.keymapper.data.db.dao.KeyMapDao import io.github.sds100.keymapper.data.entities.ActionEntity import io.github.sds100.keymapper.data.entities.Extra import io.github.sds100.keymapper.data.entities.KeyMapEntity -import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity import io.github.sds100.keymapper.mappings.keymaps.KeyMapRepository import io.github.sds100.keymapper.system.devices.DevicesAdapter import io.github.sds100.keymapper.util.DefaultDispatcherProvider @@ -149,8 +149,8 @@ class RoomKeyMapRepository( var updateKeyMap = false val newTriggerKeys = keyMap.trigger.keys.map { triggerKey -> - if (triggerKey.deviceId != TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE || - triggerKey.deviceId != TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE + if (triggerKey.deviceId != TriggerKeyEntity.DEVICE_ID_THIS_DEVICE || + triggerKey.deviceId != TriggerKeyEntity.DEVICE_ID_ANY_DEVICE ) { val deviceDescriptor = triggerKey.deviceId diff --git a/app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/trigger/TriggerKey.kt b/app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/trigger/TriggerKey.kt index b21eeecc5b..51094185d8 100644 --- a/app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/trigger/TriggerKey.kt +++ b/app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/trigger/TriggerKey.kt @@ -1,6 +1,7 @@ package io.github.sds100.keymapper.mappings.keymaps.trigger import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity import io.github.sds100.keymapper.mappings.ClickType import kotlinx.serialization.Serializable import splitties.bitflags.hasFlag @@ -32,13 +33,13 @@ data class TriggerKey( object TriggerKeyEntityMapper { fun fromEntity( - entity: TriggerEntity.KeyEntity, + entity: TriggerKeyEntity, ): TriggerKey = TriggerKey( uid = entity.uid, keyCode = entity.keyCode, device = when (entity.deviceId) { - TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE -> TriggerKeyDevice.Internal - TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE -> TriggerKeyDevice.Any + TriggerKeyEntity.DEVICE_ID_THIS_DEVICE -> TriggerKeyDevice.Internal + TriggerKeyEntity.DEVICE_ID_ANY_DEVICE -> TriggerKeyDevice.Any else -> TriggerKeyDevice.External( entity.deviceId, entity.deviceName ?: "", @@ -50,14 +51,14 @@ object TriggerKeyEntityMapper { TriggerEntity.DOUBLE_PRESS -> ClickType.DOUBLE_PRESS else -> ClickType.SHORT_PRESS }, - consumeKeyEvent = !entity.flags.hasFlag(TriggerEntity.KeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT), + consumeKeyEvent = !entity.flags.hasFlag(TriggerKeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT), ) - fun toEntity(key: TriggerKey): TriggerEntity.KeyEntity { + fun toEntity(key: TriggerKey): TriggerKeyEntity { val deviceId = when (key.device) { - TriggerKeyDevice.Any -> TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE + TriggerKeyDevice.Any -> TriggerKeyEntity.DEVICE_ID_ANY_DEVICE is TriggerKeyDevice.External -> key.device.descriptor - TriggerKeyDevice.Internal -> TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE + TriggerKeyDevice.Internal -> TriggerKeyEntity.DEVICE_ID_THIS_DEVICE } val deviceName = if (key.device is TriggerKeyDevice.External) { @@ -75,10 +76,10 @@ object TriggerKeyEntityMapper { var flags = 0 if (!key.consumeKeyEvent) { - flags = flags.withFlag(TriggerEntity.KeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT) + flags = flags.withFlag(TriggerKeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT) } - return TriggerEntity.KeyEntity( + return TriggerKeyEntity( keyCode = key.keyCode, deviceId = deviceId, deviceName = deviceName, diff --git a/app/src/test/java/io/github/sds100/keymapper/data/repositories/KeyMapRepositoryTest.kt b/app/src/test/java/io/github/sds100/keymapper/data/repositories/KeyMapRepositoryTest.kt index 9a390733d9..1ec372147b 100644 --- a/app/src/test/java/io/github/sds100/keymapper/data/repositories/KeyMapRepositoryTest.kt +++ b/app/src/test/java/io/github/sds100/keymapper/data/repositories/KeyMapRepositoryTest.kt @@ -6,6 +6,7 @@ import io.github.sds100.keymapper.data.entities.ActionEntity import io.github.sds100.keymapper.data.entities.Extra import io.github.sds100.keymapper.data.entities.KeyMapEntity import io.github.sds100.keymapper.data.entities.TriggerEntity +import io.github.sds100.keymapper.data.entities.TriggerKeyEntity import io.github.sds100.keymapper.system.devices.FakeDevicesAdapter import io.github.sds100.keymapper.system.devices.InputDeviceInfo import io.github.sds100.keymapper.util.State @@ -254,7 +255,7 @@ class KeyMapRepositoryTest { fun `key map with device name for trigger key, if device for trigger key is connected, do not update trigger key device name`() = runTest(testDispatcher) { // GIVEN - val triggerKey = TriggerEntity.KeyEntity( + val triggerKey = TriggerKeyEntity( keyCode = 1, deviceId = FAKE_KEYBOARD.descriptor, deviceName = FAKE_KEYBOARD.name, @@ -277,7 +278,7 @@ class KeyMapRepositoryTest { fun `key map with device name for trigger key, if device for trigger key is disconnected, do not update trigger key device name`() = runTest(testDispatcher) { // GIVEN - val triggerKey = TriggerEntity.KeyEntity( + val triggerKey = TriggerKeyEntity( keyCode = 1, deviceId = FAKE_KEYBOARD.descriptor, deviceName = FAKE_KEYBOARD.name, @@ -298,7 +299,7 @@ class KeyMapRepositoryTest { fun `key map with no device name for trigger key, if device for trigger key is connected, update trigger key device name`() = runTest(testDispatcher) { // GIVEN - val triggerKey = TriggerEntity.KeyEntity( + val triggerKey = TriggerKeyEntity( keyCode = 1, deviceId = FAKE_KEYBOARD.descriptor, deviceName = "",