Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15817,6 +15817,20 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
cancelOngoingLongPressForAction(action)
}

override fun usesDefaultActionAfterLongPress(action: KeyAction): Boolean = when (action) {
KeyAction.Delete,
KeyAction.MoveCursorLeft,
KeyAction.MoveCursorRight,
KeyAction.Space,
KeyAction.Convert,
KeyAction.SwitchToNextIme,
KeyAction.Cancel,
KeyAction.DoNothing,
KeyAction.Backspace,
is KeyAction.Text -> false
else -> true
}

override fun onActionLongPress(action: KeyAction) {
if (deferCursorInput { onActionLongPress(action) }) return
flushPendingBackspacesBefore(action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@ package com.kazumaproject.markdownhelperkeyboard.ime_service

import android.app.Activity
import android.content.Context
import android.os.Looper
import android.os.SystemClock
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import com.kazumaproject.core.domain.state.InputMode
import com.kazumaproject.core.domain.state.TenKeyQWERTYMode
import com.kazumaproject.custom_keyboard.data.FlickAction
import com.kazumaproject.custom_keyboard.data.FlickDirection
import com.kazumaproject.custom_keyboard.data.KeyAction
import com.kazumaproject.custom_keyboard.data.KeyData
import com.kazumaproject.custom_keyboard.data.KeyType
import com.kazumaproject.custom_keyboard.data.KeyboardInputMode
import com.kazumaproject.custom_keyboard.data.KeyboardLayout
import com.kazumaproject.custom_keyboard.view.FlickKeyboardView
import com.kazumaproject.markdownhelperkeyboard.R
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.CustomKeyboardLayout
import com.kazumaproject.markdownhelperkeyboard.databinding.MainLayoutBinding
import com.kazumaproject.markdownhelperkeyboard.ime_service.models.SymbolKeyboardState
import com.kazumaproject.markdownhelperkeyboard.repository.KeyboardRepository
import com.kazumaproject.markdownhelperkeyboard.setting_activity.AppPreference
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.emptyFlow
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.*
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.util.ReflectionHelpers as Reflect
import org.robolectric.util.ReflectionHelpers.ClassParameter
import java.time.Duration
import java.util.concurrent.atomic.AtomicBoolean

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [35])
Expand Down Expand Up @@ -92,6 +106,58 @@ class SumireInputLifecycleTest {
}
}

@Test fun heldCustomKeyboardSwitchRunsOnReleaseInsideTheKey() {
val source = CustomKeyboardLayout(layoutId = 1L, name = "Source", columnCount = 1, rowCount = 1)
val target = source.copy(layoutId = 2L, name = "Target", stableId = "target")
Reflect.setField(service, "customLayouts", listOf(source, target))
Reflect.setField(service, "currentCustomKeyboardStableId", source.stableId)
service.keyboardRepository = mock(KeyboardRepository::class.java).also {
doReturn(emptyFlow<KeyboardLayout>()).`when`(it).getFullLayout(target.layoutId)
}
val key = displayCustomActionKey(KeyAction.MoveToCustomKeyboard(target.stableId))

val draggedHold = hold(key)
touch(key, MotionEvent.ACTION_MOVE, draggedHold, y = -100f)
touch(key, MotionEvent.ACTION_UP, draggedHold, y = -100f)
assertEquals(source.stableId, Reflect.getField<String>(service, "currentCustomKeyboardStableId"))

val hold = hold(key)
assertEquals(source.stableId, Reflect.getField<String>(service, "currentCustomKeyboardStableId"))
touch(key, MotionEvent.ACTION_UP, hold)
assertEquals(target.stableId, Reflect.getField<String>(service, "currentCustomKeyboardStableId"))
}

@Test fun heldCustomFlickActionRunsOnceOnRelease() {
val key = displayCustomActionKey(
KeyAction.DoNothing,
mapOf(FlickDirection.UP to FlickAction.Action(KeyAction.ShowEmojiKeyboard))
)
val symbols = Reflect.getField<MutableStateFlow<SymbolKeyboardState>>(service, "_keyboardSymbolViewState")

val hold = hold(key)
touch(key, MotionEvent.ACTION_MOVE, hold, y = -100f)
assertFalse(symbols.value.isShown)
touch(key, MotionEvent.ACTION_UP, hold, y = -100f)
assertTrue(symbols.value.isShown)
}

@Test fun heldCustomDeleteRepeatsUntilRelease() {
val key = displayCustomActionKey(KeyAction.Delete)
val repeat = Reflect.getField<AtomicBoolean>(service, "deleteKeyLongKeyPressed")
val input = Reflect.getField<MutableStateFlow<String>>(service, "_inputString")
input.value = "abcd"

val hold = hold(key)
assertTrue(repeat.get())
val textAtRelease = input.value
assertTrue(textAtRelease.length < "abcd".length)

touch(key, MotionEvent.ACTION_UP, hold)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(300))
assertFalse(repeat.get())
assertEquals(textAtRelease, input.value)
}

@Test fun renderingCurrentSurfaceKeepsControllersAndAnActiveKeyPressed() {
binding.customLayoutDefault.visibility = View.VISIBLE
val key = binding.customLayoutDefault.getChildAt(0)
Expand All @@ -103,6 +169,47 @@ class SumireInputLifecycleTest {
assertEquals(controllers, Reflect.getField<List<Any>>(binding.customLayoutDefault, "crossFlickControllers"))
}

private fun displayCustomActionKey(
action: KeyAction,
flicks: Map<FlickDirection, FlickAction> = emptyMap()
): View {
Reflect.getField<MutableStateFlow<TenKeyQWERTYMode>>(service, "_tenKeyQWERTYMode").value = TenKeyQWERTYMode.Custom
val id = "custom_special_key"
val key = KeyData(
label = "Special", row = 0, column = 0, isFlickable = flicks.isNotEmpty(),
action = action, isSpecialKey = true, keyId = id,
keyType = if (flicks.isEmpty()) KeyType.NORMAL else KeyType.CROSS_FLICK
)
val maps = if (flicks.isEmpty()) emptyMap()
else mapOf(id to listOf(flicks + (FlickDirection.TAP to FlickAction.Action(action))))
val keyboard = binding.customLayoutDefault
keyboard.clearSumireSpecialKeyActionResolver()
keyboard.visibility = View.VISIBLE
keyboard.setKeyboard(KeyboardLayout(listOf(key), maps, columnCount = 1, rowCount = 1))
keyboard.measure(
View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)
)
keyboard.layout(0, 0, 200, 100)
return keyboard.getChildAt(0)
}

private fun hold(key: View): Long {
val downTime = SystemClock.uptimeMillis()
touch(key, MotionEvent.ACTION_DOWN, downTime)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(310))
return downTime
}

private fun touch(key: View, action: Int, downTime: Long, y: Float = key.height / 2f) {
val event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, key.width / 2f, y, 0)
try {
key.dispatchTouchEvent(event)
} finally {
event.recycle()
}
}

private fun keys() = Reflect.getField<List<Any>>(binding.customLayoutDefault, "keyInfos").map {
Reflect.getField<KeyData>(it, "keyData")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class CrossFlickInputController(
private val controllerScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
private var longPressJob: Job? = null
private var isLongPressMode = false
private var isLongPressTriggered = false
private var longPressSelection: Pair<KeyAction, FlickDirection>? = null

private var popupColorTheme: FlickPopupColorTheme? = null
private var inputTextTransform: (String) -> String = { it }
Expand Down Expand Up @@ -196,10 +196,13 @@ class CrossFlickInputController(

// コントローラを破棄する。ビューのデタッチやキーボードビューの再構築時に FlickKeyboardView から呼ばれる。
fun cancel() {
listener?.onCanceled()
activeGestureConfig = null
longPressJob?.cancel()
longPressJob = null
controllerScope.cancel()
isLongPressMode = false
cancelLongPressAction()
listener?.onCanceled()
activeGestureConfig = null
restoreOriginalButtonText()
dismissAllPopups(clearDirectionalCache = true)
}
Expand Down Expand Up @@ -235,13 +238,14 @@ class CrossFlickInputController(
private fun handleTouchEvent(view: View, event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
longPressJob?.cancel()
isLongPressMode = false
cancelLongPressAction()
val gestureConfig = gestureConfigSource.snapshot()
activeGestureConfig = gestureConfig
view.isPressed = true
view.drawableHotspotChanged(event.x, event.y)

isLongPressMode = false
isLongPressTriggered = false
anchorView = view
initialTouchPoint.set(event.rawX, event.rawY)
currentDirection = FlickDirection.TAP
Expand All @@ -256,10 +260,8 @@ class CrossFlickInputController(
showDirectionalPopup(FlickDirection.TAP)
}

longPressJob?.cancel()
longPressJob = controllerScope.launch {
delay(gestureConfig.longPressTimeoutMillis)
isLongPressTriggered = true
isLongPressMode = true
onLongPressTriggered()
}
Expand Down Expand Up @@ -312,12 +314,14 @@ class CrossFlickInputController(
longPressJob = null

if (event.action == MotionEvent.ACTION_UP) {
longPressSelection = null
commitAction()
} else {
notifyLongPressCanceledIfNeeded()
cancelLongPressAction()
listener?.onCanceled()
}

isLongPressMode = false
restoreOriginalButtonText()
dismissAllPopups()
anchorView = null
Expand Down Expand Up @@ -345,7 +349,7 @@ class CrossFlickInputController(
} else {
dismissAllActionPopups()
}
notifyLongPressActionPreview()
updateLongPressAction()
}

InputMode.TEXT -> {
Expand Down Expand Up @@ -390,7 +394,7 @@ class CrossFlickInputController(
commitCrossFlickAction(
currentDirection = currentDirection,
flickActionMap = flickActionMap,
isLongPressTriggered = isLongPressTriggered,
isLongPressTriggered = isLongPressMode,
listener = listener
)
}
Expand All @@ -410,18 +414,10 @@ class CrossFlickInputController(
}
}

private fun notifyLongPressCanceledIfNeeded() {
if (!isLongPressTriggered || inputMode != InputMode.ACTION) {
isLongPressTriggered = false
isLongPressMode = false
return
}

val action = resolveCrossFlickAction(currentDirection, flickActionMap)?.toKeyAction()
?: KeyAction.Cancel
listener?.onFlickLongPressCanceled(action, currentDirection)
isLongPressTriggered = false
isLongPressMode = false
private fun cancelLongPressAction() {
val (action, direction) = longPressSelection ?: return
longPressSelection = null
listener?.onFlickLongPressCanceled(action, direction)
}

// 通常フリック中(長押し前)に方向が変わったときのポップアップ更新。ACTION_MOVE から呼ばれる。
Expand All @@ -442,7 +438,7 @@ class CrossFlickInputController(
when (inputMode) {
InputMode.ACTION -> {
highlightActionPopup(direction)
notifyLongPressActionPreview()
updateLongPressAction()
}

InputMode.TEXT -> {
Expand All @@ -456,10 +452,14 @@ class CrossFlickInputController(
}
}

// ACTION モードの長押し中、現在ハイライトされている方向のアクションを listener へプレビュー通知する。
private fun notifyLongPressActionPreview() {
resolveAction(currentDirection)?.let {
listener?.onFlickLongPress(it.toKeyAction(), currentDirection)
private fun updateLongPressAction() {
val action = resolveAction(currentDirection)?.toKeyAction()
if (action != longPressSelection?.first) {
cancelLongPressAction()
}
if (action != null && isLongPressMode) {
longPressSelection = action to currentDirection
listener?.onFlickLongPress(action, currentDirection)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TapLongPressInputController(
fun onPress()
fun onTap()
fun onLongPress()
fun onUpAfterLongPress()
fun onUpAfterLongPress(tapCanceled: Boolean)
fun onLongPressCanceled()
}

Expand Down Expand Up @@ -114,12 +114,13 @@ class TapLongPressInputController(
MotionEvent.ACTION_UP -> {
view.removeCallbacks(longPressRunnable)
view.isPressed = false
isTouchActive = false
val wasLongPress = isLongPressTriggered
val tapCanceled = isTapCanceled
clearGesture()
when {
isLongPressTriggered -> listener?.onUpAfterLongPress()
!isTapCanceled -> view.performClick()
wasLongPress -> listener?.onUpAfterLongPress(tapCanceled)
!tapCanceled -> view.performClick()
}
clearGesture()
return true
}

Expand Down
Loading
Loading