From 20f10e8aadebe83be6ae2c2ee1a55aa4bf0b4286 Mon Sep 17 00:00:00 2001 From: Codex Mozc Autonomy Date: Thu, 19 Nov 2026 12:58:10 -0500 Subject: [PATCH] Restrict custom long-press actions to special keys --- .../custom_keyboard/data/KeyDefinition.kt | 3 ++ .../import_export/KeyboardBackupPipeline.kt | 18 +++++--- .../KeyboardLayoutJsonExporter.kt | 3 +- .../custom_keyboard/ui/KeyEditorFragment.kt | 44 +++++++++++++------ .../repository/KeyboardRepository.kt | 20 ++++++--- .../main/res/layout/fragment_key_editor.xml | 3 +- .../KeyboardLayoutJsonImporterTest.kt | 28 +++++++----- .../KeyboardRepositoryDeleteImpactTest.kt | 9 ++-- .../KeyboardRepositorySaveLayoutTest.kt | 30 ++++++++++--- .../custom_keyboard/data/KeyGestureBinding.kt | 3 ++ .../custom_keyboard/view/FlickKeyboardView.kt | 3 +- .../view/FlickKeyboardViewLifecycleTest.kt | 37 +++++++++++++--- 12 files changed, 145 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt index b9c15bb5f..6b508fed3 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt @@ -50,3 +50,6 @@ data class KeyDefinition( val qwertyConfigJson: String? = null, val longPressAction: String? = null ) + +internal val KeyDefinition.specialKeyLongPressAction: String? + get() = longPressAction.takeIf { isSpecialKey } diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt index 02e05dbea..3911f5693 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt @@ -326,14 +326,18 @@ object KeyboardBackupNormalizer { keyIndex = keyIndex, warnings = warnings ) - val normalizedLongPressAction = remapKeyAction( - action = keyDto.longPressAction, - stableIdsByOldLayoutId = stableIdsByOldLayoutId, - layoutIndex = layoutIndex, - keyIndex = keyIndex, - warnings = warnings - ) val isSpecialKey = keyDto.isSpecialKey ?: false + val normalizedLongPressAction = if (isSpecialKey) { + remapKeyAction( + action = keyDto.longPressAction, + stableIdsByOldLayoutId = stableIdsByOldLayoutId, + layoutIndex = layoutIndex, + keyIndex = keyIndex, + warnings = warnings + ) + } else { + null + } val normalizedDoubleTapAction = when { !isSpecialKey -> null keyDto.doubleTapEnabled == false -> null diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt index 6c2cf60b3..25e9be4e6 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt @@ -7,6 +7,7 @@ import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.FullKeyboardLayout import com.google.gson.JsonParser import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.KeyWithFlicks +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.specialKeyLongPressAction /** * カスタムキーボードレイアウトを schemaVersion 付き object 形式の JSON に @@ -125,7 +126,7 @@ internal fun KeyWithFlicks.toExportDto(): KeyWithFlicksExportDto { }, qwerty = this.key.qwertyConfigJson?.let(JsonParser::parseString), textInputBehavior = this.key.textInputBehavior, - longPressAction = this.key.longPressAction + longPressAction = this.key.specialKeyLongPressAction ), flicks = this.flicks.map { FlickMappingDto( diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt index 5c8bef89a..ba59316e1 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt @@ -54,6 +54,7 @@ import com.kazumaproject.custom_keyboard.data.SpecialKeyColorStyle import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy import com.kazumaproject.custom_keyboard.data.compatibleColumnSpan import com.kazumaproject.custom_keyboard.data.compatibleRowSpan +import com.kazumaproject.custom_keyboard.data.effectiveLongPressAction import com.kazumaproject.custom_keyboard.data.toCircularFlickMap import com.kazumaproject.custom_keyboard.data.toCellSpanCeilFromGridUnits import com.kazumaproject.custom_keyboard.data.usesFlexiblePlacement @@ -154,6 +155,10 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { private var selectedTargetCustomKeyboardStableId: String? = null private var selectedDoubleTapTargetStableId: String? = null private var longPressAction: KeyAction? = null + private val isEditingSpecialKey: Boolean + get() = _binding?.keyTypeChipGroup?.checkedChipId == R.id.chip_special + private val selectedLongPressAction: KeyAction? + get() = longPressAction.takeIf { isEditingSpecialKey } private var selectedIconRef: KeyIconRef? = null private var originalIconRef: KeyIconRef? = null private val pendingUserIconPaths = mutableSetOf() @@ -382,7 +387,11 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { binding.buttonDone.setOnClickListener { onDone() } binding.keyTypeChipGroup.setOnCheckedStateChangeListener { _, checkedIds -> - if (checkedIds.isEmpty()) return@setOnCheckedStateChangeListener + updateLongPressControlsVisibility() + if (checkedIds.isEmpty()) { + updateDoneButtonState() + return@setOnCheckedStateChangeListener + } val selectedChipId = checkedIds.first() val isSpecialKey = selectedChipId == R.id.chip_special @@ -1117,15 +1126,20 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { getString(R.string.custom_key_long_press_priority) } val targetId = (action as? KeyAction.MoveToCustomKeyboard)?.stableId - binding.longPressTargetLayout.isVisible = targetId != null val targetLabel = targetId?.takeIf { it.isNotBlank() }?.let { id -> customKeyboardTargetOptions.firstOrNull { it.stableId == id }?.label ?: getString(R.string.deleted_custom_keyboard_target) } binding.longPressTargetSpinner.setText(targetLabel.orEmpty(), false) + updateLongPressControlsVisibility() updateDoneButtonState() } + private fun updateLongPressControlsVisibility() { + binding.longPressActionLayout.isVisible = isEditingSpecialKey + binding.longPressTargetLayout.isVisible = selectedLongPressAction is KeyAction.MoveToCustomKeyboard + } + private fun selectTargetCustomKeyboard(stableId: String?) { val id = stableId.orEmpty() if (id.isBlank()) { @@ -1238,7 +1252,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { if (outputEditMode != OutputEditMode.TOGGLE) { binding.textCharInputLayout.error = null } - if (!longPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) { + if (!selectedLongPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) { binding.buttonDone.isEnabled = false return } @@ -1369,12 +1383,13 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { val key = currentKeyData!! val savedLongPress = savedInstanceState?.takeIf { - selectedId == restoredEditingKeyIdentifier && it.containsKey(STATE_LONG_PRESS_ACTION) + key.isSpecialKey && selectedId == restoredEditingKeyIdentifier && + it.containsKey(STATE_LONG_PRESS_ACTION) } val savedAction = savedLongPress?.getString(STATE_LONG_PRESS_ACTION) updateLongPressAction( when { - savedLongPress == null -> key.longPressAction + savedLongPress == null -> key.effectiveLongPressAction savedAction == "" -> KeyAction.MoveToCustomKeyboard("") else -> KeyActionMapper.toKeyAction(savedAction) } @@ -1951,7 +1966,8 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { private fun onDone() { val originalKey = currentKeyData ?: return - if (!longPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) return + val newLongPressAction = selectedLongPressAction + if (!newLongPressAction.isValidMoveToCustomKeyboardTarget(validTargetStableIds())) return if ( binding.keyTypeChipGroup.checkedChipId == R.id.chip_normal && @@ -2218,7 +2234,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { ) } if (newAction == KeyAction.VoiceInput || newDoubleTapBinding?.action == KeyAction.VoiceInput || - longPressAction == KeyAction.VoiceInput + newLongPressAction == KeyAction.VoiceInput ) { val hasPermission = ContextCompat.checkSelfPermission( requireContext(), @@ -2257,7 +2273,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { SpecialKeyColorStyle.SPECIAL }, doubleTapBinding = newDoubleTapBinding, - longPressAction = longPressAction, + longPressAction = newLongPressAction, textInputBehavior = if ( !isSpecial && newKeyType == KeyType.PETAL_FLICK && @@ -2301,11 +2317,13 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { override fun onSaveInstanceState(outState: Bundle) { editingKeyIdentifier?.let { outState.putString(STATE_EDITING_KEY, it) - outState.putString( - STATE_LONG_PRESS_ACTION, - KeyActionMapper.fromKeyAction(longPressAction) - ?: (longPressAction as? KeyAction.MoveToCustomKeyboard)?.stableId - ) + if (isEditingSpecialKey) { + outState.putString( + STATE_LONG_PRESS_ACTION, + KeyActionMapper.fromKeyAction(longPressAction) + ?: (longPressAction as? KeyAction.MoveToCustomKeyboard)?.stableId + ) + } } freeFlickDraft?.let { outState.putString("freeFlickDraft", PopupKeyConfigCodec.encode(it)) } if (_binding != null && freeFlickDraft != null) { diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt index 35d95ac17..24f5fbf7b 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt @@ -26,6 +26,7 @@ import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy import com.kazumaproject.custom_keyboard.data.copyWithItems import com.kazumaproject.custom_keyboard.data.copyWithKeys import com.kazumaproject.custom_keyboard.data.effectiveDoubleTapBinding +import com.kazumaproject.custom_keyboard.data.effectiveLongPressAction import com.kazumaproject.custom_keyboard.data.toCircularFlickDirection import com.kazumaproject.custom_keyboard.data.toKeyItem import com.kazumaproject.custom_keyboard.data.usesFlexiblePlacement @@ -40,6 +41,7 @@ import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.LongPressFl import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.SpacerDefinition import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.TwoStepFlickMapping import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.TwoStepLongPressMappingEntity +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.specialKeyLongPressAction import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.toDbStrings import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.toFlickAction import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.database.KeyboardLayoutDao @@ -196,7 +198,11 @@ class KeyboardRepository @Inject constructor( // replace existing rows. val normalizedKeysWithFlicks = importable.keysWithFlicks.map { keyWithFlicks -> keyWithFlicks.copy( - key = keyWithFlicks.key.copy(keyId = 0, ownerLayoutId = 0), + key = keyWithFlicks.key.copy( + keyId = 0, + ownerLayoutId = 0, + longPressAction = keyWithFlicks.key.specialKeyLongPressAction + ), flicks = keyWithFlicks.flicks.map { it.copy(ownerKeyId = 0) }, circularFlicks = keyWithFlicks.circularFlicks.map { it.copy(ownerKeyId = 0) }, twoStepFlicks = keyWithFlicks.twoStepFlicks.map { it.copy(ownerKeyId = 0) }, @@ -593,7 +599,7 @@ class KeyboardRepository @Inject constructor( val keyIdentifier = key.keyIdentifier val keyLabel = key.label.takeIf { it.isNotBlank() } - for (storedAction in listOf(key.action, key.doubleTapAction, key.longPressAction)) { + for (storedAction in listOf(key.action, key.doubleTapAction, key.specialKeyLongPressAction)) { val action = KeyActionMapper.toKeyAction(storedAction) if (action is KeyAction.MoveToCustomKeyboard && action.stableId == targetStableId) { references += MoveToCustomKeyboardReference( @@ -680,7 +686,11 @@ class KeyboardRepository @Inject constructor( ) val newKeys = originalLayout.keysWithFlicks.map { keyWithFlicks -> - keyWithFlicks.key.copy(keyId = 0, ownerLayoutId = 0) + keyWithFlicks.key.copy( + keyId = 0, + ownerLayoutId = 0, + longPressAction = keyWithFlicks.key.specialKeyLongPressAction + ) } val newFlicksMap = originalLayout.keysWithFlicks.associate { keyWithFlicks -> @@ -908,7 +918,7 @@ class KeyboardRepository @Inject constructor( val iconRef = keyIconRefFromDb(dbKey.iconType, dbKey.iconValue) val specialKeyColorStyle = SpecialKeyColorStyle.fromDbValue(dbKey.specialKeyColorStyle) - val longPressAction = KeyActionMapper.toKeyAction(dbKey.longPressAction) + val longPressAction = KeyActionMapper.toKeyAction(dbKey.specialKeyLongPressAction) val doubleTapBinding = KeyActionMapper.toKeyAction(dbKey.doubleTapAction) ?.takeIf { dbKey.isSpecialKey } ?.let { @@ -1237,7 +1247,7 @@ class KeyboardRepository @Inject constructor( doubleTapBinding?.action ), doubleTapPolicy = doubleTapBinding?.policy?.serializedName, - longPressAction = KeyActionMapper.fromKeyAction(keyData.longPressAction), + longPressAction = KeyActionMapper.fromKeyAction(keyData.effectiveLongPressAction), popupMapConfigJson = keyData.popupMapConfig?.takeIf { keyData.keyType == KeyType.FREE_FLICK } ?.let(PopupKeyConfigCodec::encode), qwertyConfigJson = keyData.qwertyConfig?.let(QwertyKeyConfigCodec::encode), diff --git a/app/src/main/res/layout/fragment_key_editor.xml b/app/src/main/res/layout/fragment_key_editor.xml index ae05451b9..f7bd01979 100644 --- a/app/src/main/res/layout/fragment_key_editor.xml +++ b/app/src/main/res/layout/fragment_key_editor.xml @@ -586,7 +586,8 @@ style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginTop="24dp"> + android:layout_marginTop="24dp" + android:visibility="gone"> + KeyWithFlicks( + key.copy(longPressAction = "SwitchToNextIme"), + emptyList(), emptyList(), emptyList(), emptyList(), emptyList() + ) + } + whenever(dao.getFullLayoutById(1L)).thenReturn(flowOf(original.copy(keysWithFlicks = storedKeys))) + val restored = repository.getFullLayout(1L).first().keys + assertNull(restored.single { !it.isSpecialKey }.longPressAction) + assertEquals(shortcut, restored.single { it.isSpecialKey }.longPressAction) } // --------------------------------------------------------- diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyGestureBinding.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyGestureBinding.kt index 5dfe81706..f726f6630 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyGestureBinding.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyGestureBinding.kt @@ -28,6 +28,9 @@ data class DoubleTapBinding( val KeyData.supportsDoubleTap: Boolean get() = isSpecialKey +val KeyData.effectiveLongPressAction: KeyAction? + get() = longPressAction.takeIf { isSpecialKey } + fun automaticDoubleTapPolicy( normalAction: KeyAction?, doubleTapAction: KeyAction diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt index 5fdf9e2e3..75b9b73f0 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt @@ -99,6 +99,7 @@ import com.kazumaproject.custom_keyboard.data.buildEvenCircularRanges import com.kazumaproject.custom_keyboard.data.dispatchResolvedSumireSpecialKeyAction import com.kazumaproject.custom_keyboard.data.dispatchSumireSpecialKeyRuntimeAction import com.kazumaproject.custom_keyboard.data.effectiveDoubleTapBinding +import com.kazumaproject.custom_keyboard.data.effectiveLongPressAction import com.kazumaproject.custom_keyboard.data.refreshSumireSpecialKeyTap import com.kazumaproject.custom_keyboard.data.toCircularFlickKeyMaps import com.kazumaproject.custom_keyboard.data.toLegacyFlickDirection @@ -3303,7 +3304,7 @@ class FlickKeyboardView @JvmOverloads constructor( ) { if (layoutOrigin != KeyboardLayoutOrigin.CUSTOM) return val keyData = keyInfos.firstOrNull { it.view === target.view }?.keyData ?: return - val action = keyData.longPressAction ?: return + val action = keyData.effectiveLongPressAction ?: return val config = gestureSessionConfigSource.snapshot() val task = Runnable { target.pendingLongPress = null diff --git a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewLifecycleTest.kt b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewLifecycleTest.kt index 6fe0debbb..7cbe556bd 100644 --- a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewLifecycleTest.kt +++ b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardViewLifecycleTest.kt @@ -149,12 +149,12 @@ class FlickKeyboardViewLifecycleTest { } } - @Test fun assignedLongPressUsesConfiguredTimeoutAndConsumesRelease() { + @Test fun specialKeyLongPressUsesConfiguredTimeoutAndConsumesRelease() { val shortcut = KeyAction.MoveToCustomKeyboard("target") - for (type in listOf(KeyType.NORMAL, KeyType.CROSS_FLICK, KeyType.QWERTY)) { + for (type in listOf(KeyType.NORMAL, KeyType.CROSS_FLICK)) { val layout = actionLayout(type) val keyData = layout.keys.single().copy( - isSpecialKey = type != KeyType.QWERTY, + isSpecialKey = true, longPressAction = shortcut ) install(layout.copyWithKeys(listOf(keyData)), KeyboardLayoutOrigin.CUSTOM) @@ -171,8 +171,35 @@ class FlickKeyboardViewLifecycleTest { assertTrue(holds.isEmpty()) tap(0) - val tapAction = if (type == KeyType.QWERTY) KeyAction.Text(keyData.label) else KeyAction.Paste - assertEquals(listOf(shortcut, tapAction), events) + assertEquals(listOf(shortcut, KeyAction.Paste), events) + } + } + + @Test fun normalKeysKeepTextOutputWithAnOldLongPressAction() { + for (type in listOf(KeyType.QWERTY, KeyType.PETAL_FLICK)) { + val keyData = KeyData( + label = "あ", + row = 0, + column = 0, + isFlickable = type == KeyType.PETAL_FLICK, + action = KeyAction.Text("あ"), + keyId = "character", + keyType = type, + longPressAction = KeyAction.MoveCustomKeyboardTab + ) + val layout = KeyboardLayout( + keys = listOf(keyData), + flickKeyMaps = mapOf("character" to listOf(mapOf(FlickDirection.TAP to FlickAction.Input("あ")))), + columnCount = 1, + rowCount = 1, + longPressFlickKeyMaps = mapOf("character" to mapOf(FlickDirection.TAP to "ぁ")) + ) + install(layout, KeyboardLayoutOrigin.CUSTOM) + + hold(0) + + val expectedOutput = if (type == KeyType.PETAL_FLICK) "ぁ" else "あ" + assertEquals(type.name, listOf(KeyAction.Text(expectedOutput)), events) } }