Skip to content

Commit

Permalink
#479 make Hold Down and Repeat action options mutually exclusive and …
Browse files Browse the repository at this point in the history
…migrate user data for this change
  • Loading branch information
sds100 committed Oct 27, 2020
1 parent 5ce89cc commit 743b3f2
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 4 deletions.
102 changes: 102 additions & 0 deletions app/schemas/io.github.sds100.keymapper.data.db.AppDatabase/8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"formatVersion": 1,
"database": {
"version": 8,
"identityHash": "ba6a25ab74ecd5a7b5b70684ce72d100",
"entities": [
{
"tableName": "keymaps",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `trigger` TEXT NOT NULL, `action_list` TEXT NOT NULL, `constraint_list` TEXT NOT NULL, `constraint_mode` INTEGER NOT NULL, `flags` INTEGER NOT NULL, `folder_name` TEXT, `is_enabled` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "trigger",
"columnName": "trigger",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "actionList",
"columnName": "action_list",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "constraintList",
"columnName": "constraint_list",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "constraintMode",
"columnName": "constraint_mode",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "flags",
"columnName": "flags",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "folderName",
"columnName": "folder_name",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "isEnabled",
"columnName": "is_enabled",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "deviceinfo",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`descriptor` TEXT NOT NULL, `name` TEXT NOT NULL, PRIMARY KEY(`descriptor`))",
"fields": [
{
"fieldPath": "descriptor",
"columnName": "descriptor",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"descriptor"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ba6a25ab74ecd5a7b5b70684ce72d100')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ object ServiceLocator {
AppDatabase.MIGRATION_3_4,
AppDatabase.MIGRATION_4_5,
AppDatabase.MIGRATION_5_6,
AppDatabase.MIGRATION_6_7).build()
AppDatabase.MIGRATION_6_7,
AppDatabase.MIGRATION_7_8).build()
database = result
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import io.github.sds100.keymapper.data.model.KeyMap
abstract class AppDatabase : RoomDatabase() {
companion object {
const val DATABASE_NAME = "key_map_database"
const val DATABASE_VERSION = 7
const val DATABASE_VERSION = 8

val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
Expand Down Expand Up @@ -66,6 +66,12 @@ abstract class AppDatabase : RoomDatabase() {
Migration_6_7.migrate(database)
}
}

val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
Migration_7_8.migrate(database)
}
}
}

abstract fun keymapDao(): KeyMapDao
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@file:Suppress("ClassName")

package io.github.sds100.keymapper.data.db.migration

import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteQueryBuilder
import com.github.salomonbrys.kotson.fromJson
import com.github.salomonbrys.kotson.registerTypeAdapter
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.github.sds100.keymapper.data.model.Action
import splitties.bitflags.hasFlag
import splitties.bitflags.minusFlag

/**
* Created by sds100 on 25/06/20.
*/

/**
* If an action has both the Repeat and Hold Down flags then remove the Hold Down flag and assume that the user wants
* the Repeat flag.
*/
object Migration_7_8 {

private const val ACTION_FLAG_REPEAT = 4
private const val ACTION_FLAG_HOLD_DOWN = 8

fun migrate(database: SupportSQLiteDatabase) = database.apply {
val query = SupportSQLiteQueryBuilder
.builder("keymaps")
.columns(arrayOf("id", "action_list"))
.create()

query(query).apply {
val gson = GsonBuilder().registerTypeAdapter(Action.DESERIALIZER).create()

while (moveToNext()) {
val idColumnIndex = getColumnIndex("id")
val id = getInt(idColumnIndex)

val actionListColumnIndex = getColumnIndex("action_list")

val actionList = gson.fromJson<List<Action>>(getString(actionListColumnIndex))

val newActionList = actionList.map {
if (it.flags.hasFlag(ACTION_FLAG_REPEAT) && it.flags.hasFlag(ACTION_FLAG_HOLD_DOWN)) {
return@map it.clone(flags = it.flags.minusFlag(ACTION_FLAG_HOLD_DOWN))
}

it
}

execSQL("UPDATE keymaps SET action_list='${newActionList.json}' WHERE id=$id")
}

close()
}
}

val Any.json: String
get() = Gson().toJson(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class TriggerBehavior(keys: List<Trigger.Key>, @Trigger.Mode mode: Int, flags: I
val vibrateDuration: BehaviorOption<Int>
val sequenceTriggerTimeout: BehaviorOption<Int>

/*
It is very important that any new options are only allowed with a valid combination of other options. Make sure
the "isAllowed" property considers all the other options.
*/

init {

val longPressDelayValue = extras.getData(EXTRA_LONG_PRESS_DELAY).valueOrNull()?.toInt()
Expand Down Expand Up @@ -218,7 +223,9 @@ class ActionBehavior(
val repeat = BehaviorOption(
id = ID_REPEAT,
value = action.flags.hasFlag(Action.ACTION_FLAG_REPEAT),
isAllowed = if (triggerKeys != null && triggerMode != null) {
isAllowed = if (triggerKeys != null && triggerMode != null
&& !action.flags.hasFlag(Action.ACTION_FLAG_HOLD_DOWN)) {

KeymapDetectionDelegate.performActionOnDown(triggerKeys, triggerMode)
} else {
false
Expand All @@ -240,7 +247,8 @@ class ActionBehavior(
val holdDown = BehaviorOption(
id = ID_HOLD_DOWN,
value = action.flags.hasFlag(Action.ACTION_FLAG_HOLD_DOWN),
isAllowed = if (triggerKeys != null && triggerMode != null) {
isAllowed = if (triggerKeys != null && triggerMode != null
&& !action.flags.hasFlag(Action.ACTION_FLAG_REPEAT)) {
KeymapDetectionDelegate.performActionOnDown(triggerKeys, triggerMode)
&& (action.type == ActionType.KEY_EVENT
|| (action.type == ActionType.TAP_COORDINATE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Expand All @@ -264,6 +272,11 @@ class ActionBehavior(
isAllowed = true
)

/*
It is very important that any new options are only allowed with a valid combination of other options. Make sure
the "isAllowed" property considers all the other options.
*/

init {
val repeatRateValue = action.extras.getData(Action.EXTRA_REPEAT_RATE).valueOrNull()?.toInt()

Expand Down Expand Up @@ -342,6 +355,8 @@ class ActionBehavior(
stopRepeatingWhenTriggerReleased.isAllowed = value

repeat.value = value

holdDown.isAllowed = !value
}

ID_SHOW_VOLUME_UI -> showVolumeUi.value = value
Expand All @@ -361,6 +376,8 @@ class ActionBehavior(

ID_HOLD_DOWN -> {
holdDown.value = value

repeat.isAllowed = !value
}
}

Expand Down Expand Up @@ -392,6 +409,11 @@ class TriggerKeyBehavior(
isAllowed = true
)

/*
It is very important that any new options are only allowed with a valid combination of other options. Make sure
the "isAllowed" property considers all the other options.
*/

fun setValue(id: String, value: Boolean): TriggerKeyBehavior {
when (id) {
ID_DO_NOT_CONSUME_KEY_EVENT -> doNotConsumeKeyEvents.value = value
Expand Down

0 comments on commit 743b3f2

Please sign in to comment.