|
| 1 | +package com.enaboapps.switchify.pc |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.enaboapps.switchify.R |
| 5 | +import com.enaboapps.switchify.service.window.MessageSeverity |
| 6 | +import com.enaboapps.switchify.service.window.ServiceMessageHUD |
| 7 | +import kotlinx.coroutines.CoroutineScope |
| 8 | +import kotlinx.coroutines.Job |
| 9 | +import kotlinx.coroutines.delay |
| 10 | +import kotlinx.coroutines.isActive |
| 11 | +import kotlinx.coroutines.launch |
| 12 | + |
| 13 | +class PcMouseRepeatManager internal constructor( |
| 14 | + private var settings: PcMouseRepeatSettings? = null, |
| 15 | + private var showHudMessage: (Int, MessageSeverity) -> Unit = defaultHudMessageHandler() |
| 16 | +) { |
| 17 | + private var repeatJob: Job? = null |
| 18 | + private var repeatedCommand: PcControlCommand? = null |
| 19 | + private var repeatArmed = false |
| 20 | + |
| 21 | + companion object { |
| 22 | + val instance: PcMouseRepeatManager by lazy { PcMouseRepeatManager() } |
| 23 | + |
| 24 | + fun isRepeatable(command: PcControlCommand): Boolean { |
| 25 | + return command is PcControlCommand.Move || command is PcControlCommand.Scroll |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + fun init(context: Context) { |
| 30 | + settings = PreferencePcMouseRepeatSettings(context.applicationContext) |
| 31 | + } |
| 32 | + |
| 33 | + fun canRepeat(command: PcControlCommand): Boolean { |
| 34 | + return isRepeatable(command) && currentSettings().isEnabled() |
| 35 | + } |
| 36 | + |
| 37 | + fun armForInitialSend(command: PcControlCommand): Boolean { |
| 38 | + if (!canRepeat(command)) return false |
| 39 | + |
| 40 | + stop(showMessage = false) |
| 41 | + repeatedCommand = command |
| 42 | + repeatArmed = true |
| 43 | + showMessage(R.string.pc_mouse_repeat_started, MessageSeverity.Success) |
| 44 | + return true |
| 45 | + } |
| 46 | + |
| 47 | + fun startAfterInitialSend( |
| 48 | + command: PcControlCommand, |
| 49 | + scope: CoroutineScope, |
| 50 | + sendRepeatedCommand: suspend (PcControlCommand) -> PcCommandResult |
| 51 | + ): Boolean { |
| 52 | + if (!isRepeatable(command)) return false |
| 53 | + if (!currentSettings().isEnabled()) { |
| 54 | + stop() |
| 55 | + return false |
| 56 | + } |
| 57 | + if (!repeatArmed || repeatedCommand != command) return false |
| 58 | + |
| 59 | + repeatJob?.cancel() |
| 60 | + repeatedCommand = command |
| 61 | + repeatArmed = true |
| 62 | + repeatJob = scope.launch { |
| 63 | + while (isActive) { |
| 64 | + delay(currentSettings().intervalMs()) |
| 65 | + if (!isActive) return@launch |
| 66 | + if (!currentSettings().isEnabled()) { |
| 67 | + stop() |
| 68 | + return@launch |
| 69 | + } |
| 70 | + if (!sendAndContinue(command, sendRepeatedCommand)) return@launch |
| 71 | + } |
| 72 | + } |
| 73 | + return true |
| 74 | + } |
| 75 | + |
| 76 | + fun cancelPendingStart(showMessage: Boolean = false): Boolean { |
| 77 | + if (!isRepeating()) return false |
| 78 | + |
| 79 | + repeatJob?.cancel() |
| 80 | + clearRepeatState() |
| 81 | + if (showMessage) { |
| 82 | + showMessage(R.string.pc_mouse_repeat_stopped, MessageSeverity.Info) |
| 83 | + } |
| 84 | + return true |
| 85 | + } |
| 86 | + |
| 87 | + fun stop(showMessage: Boolean = true): Boolean { |
| 88 | + if (!isRepeating()) { |
| 89 | + clearRepeatState() |
| 90 | + return false |
| 91 | + } |
| 92 | + repeatJob?.cancel() |
| 93 | + clearRepeatState() |
| 94 | + if (showMessage) { |
| 95 | + showMessage(R.string.pc_mouse_repeat_stopped, MessageSeverity.Info) |
| 96 | + } |
| 97 | + return true |
| 98 | + } |
| 99 | + |
| 100 | + fun stopForSwitchPress(): Boolean = stop() |
| 101 | + |
| 102 | + fun isRepeating(): Boolean { |
| 103 | + return repeatArmed && repeatedCommand != null |
| 104 | + } |
| 105 | + |
| 106 | + fun clearServiceState(showMessage: Boolean = false) { |
| 107 | + stop(showMessage) |
| 108 | + } |
| 109 | + |
| 110 | + private suspend fun sendAndContinue( |
| 111 | + command: PcControlCommand, |
| 112 | + sendCommand: suspend (PcControlCommand) -> PcCommandResult |
| 113 | + ): Boolean { |
| 114 | + return when (sendCommand(command)) { |
| 115 | + PcCommandResult.Ack -> true |
| 116 | + is PcCommandResult.AuthFailed, |
| 117 | + is PcCommandResult.Failed -> { |
| 118 | + clearRepeatState() |
| 119 | + showMessage(R.string.pc_mouse_repeat_stopped, MessageSeverity.Info) |
| 120 | + false |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private fun currentSettings(): PcMouseRepeatSettings { |
| 126 | + return settings ?: object : PcMouseRepeatSettings { |
| 127 | + override fun isEnabled(): Boolean = true |
| 128 | + override fun intervalMs(): Long = PcMouseRepeatDefaults.DEFAULT_INTERVAL_MS |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private fun clearRepeatState() { |
| 133 | + repeatJob = null |
| 134 | + repeatedCommand = null |
| 135 | + repeatArmed = false |
| 136 | + } |
| 137 | + |
| 138 | + private fun showMessage(messageResId: Int, severity: MessageSeverity) { |
| 139 | + showHudMessage(messageResId, severity) |
| 140 | + } |
| 141 | + |
| 142 | + internal fun resetForTesting() { |
| 143 | + repeatJob?.cancel() |
| 144 | + repeatJob = null |
| 145 | + repeatedCommand = null |
| 146 | + repeatArmed = false |
| 147 | + settings = null |
| 148 | + showHudMessage = defaultHudMessageHandler() |
| 149 | + } |
| 150 | + |
| 151 | + internal fun setSettingsForTesting(settings: PcMouseRepeatSettings) { |
| 152 | + this.settings = settings |
| 153 | + } |
| 154 | + |
| 155 | + internal fun setHudMessageHandlerForTesting(showHudMessage: (Int, MessageSeverity) -> Unit) { |
| 156 | + this.showHudMessage = showHudMessage |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +private fun defaultHudMessageHandler(): (Int, MessageSeverity) -> Unit = { messageResId, severity -> |
| 161 | + ServiceMessageHUD.instance.showMessage( |
| 162 | + messageResId, |
| 163 | + ServiceMessageHUD.MessageType.DISAPPEARING, |
| 164 | + severity = severity |
| 165 | + ) |
| 166 | +} |
0 commit comments