Skip to content

Commit

Permalink
Allow action try stop with volume down key
Browse files Browse the repository at this point in the history
  • Loading branch information
Nain57 committed Sep 22, 2024
1 parent 27217ff commit 8e7bb57
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2024 Kevin Buzeau
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.buzbuz.smartautoclicker.core.base

import android.view.KeyEvent

/** Physical key that will stops the running scenario or element try. */
fun KeyEvent.isStopScenarioKey(): Boolean =
keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.lifecycle.repeatOnLifecycle

import com.buzbuz.smartautoclicker.core.base.extensions.showAsOverlay
import com.buzbuz.smartautoclicker.core.base.identifier.Identifier
import com.buzbuz.smartautoclicker.core.base.isStopScenarioKey
import com.buzbuz.smartautoclicker.core.common.overlays.base.viewModels
import com.buzbuz.smartautoclicker.core.common.overlays.menu.OverlayMenu
import com.buzbuz.smartautoclicker.core.ui.utils.AnimatedStatesImageButtonController
Expand Down Expand Up @@ -97,7 +98,7 @@ class DumbMainMenu(
}

override fun onKeyEvent(keyEvent: KeyEvent): Boolean {
if (keyEvent.keyCode != KeyEvent.KEYCODE_VOLUME_DOWN) return false
if (!keyEvent.isStopScenarioKey()) return false

when (keyEvent.action) {
KeyEvent.ACTION_DOWN -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package com.buzbuz.smartautoclicker.feature.dumb.config.ui.brief

import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.ViewGroup

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle

import com.buzbuz.smartautoclicker.core.base.isStopScenarioKey
import com.buzbuz.smartautoclicker.core.common.overlays.menu.implementation.brief.ItemBriefMenu
import com.buzbuz.smartautoclicker.core.common.overlays.base.viewModels
import com.buzbuz.smartautoclicker.core.common.overlays.dialog.implementation.MoveToDialog
Expand Down Expand Up @@ -60,6 +62,12 @@ class DumbScenarioBriefMenu(
private lateinit var createCopyActionUiFlowListener: DumbActionUiFlowListener
private lateinit var updateActionUiFlowListener: DumbActionUiFlowListener

/**
* Tells if this service has handled onKeyEvent with ACTION_DOWN for a key in order to return
* the correct value when ACTION_UP is received.
*/
private var keyDownHandled: Boolean = false

override fun onCreate() {
super.onCreate()

Expand Down Expand Up @@ -131,6 +139,28 @@ class DumbScenarioBriefMenu(
showDumbActionEditionUiFlow((item.data as DumbActionDetails).action)
}

override fun onKeyEvent(keyEvent: KeyEvent): Boolean {
if (!keyEvent.isStopScenarioKey()) return false

when (keyEvent.action) {
KeyEvent.ACTION_DOWN -> {
if (viewModel.stopAction()) {
keyDownHandled = true
return true
}
}

KeyEvent.ACTION_UP -> {
if (keyDownHandled) {
keyDownHandled = false
return true
}
}
}

return false
}

override fun onMenuItemClicked(viewId: Int) {
when (viewId) {
R.id.btn_back -> onBackClicked()
Expand All @@ -146,6 +176,8 @@ class DumbScenarioBriefMenu(
return
}

if (viewModel.stopAction()) return

onConfigSaved()
back()
}
Expand Down Expand Up @@ -187,7 +219,7 @@ class DumbScenarioBriefMenu(

private fun updateReplayingState(isReplaying: Boolean) {
setOverlayViewVisibility(!isReplaying)
setMenuItemViewEnabled(menuViewBinding.btnBack, !isReplaying)
setMenuItemViewEnabled(menuViewBinding.btnBack, true)
setMenuItemViewEnabled(menuViewBinding.btnAdd, !isReplaying)
setMenuItemViewEnabled(menuViewBinding.btnHideOverlay, !isReplaying)
setMenuItemViewEnabled(menuViewBinding.btnMove, !isReplaying)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class DumbScenarioBriefViewModel @Inject constructor(
}
}

fun stopAction(): Boolean {
if (!dumbEngine.isRunning.value) return false

dumbEngine.stopDumbScenario()
return true
}

fun createNewDumbClick(context: Context, position: Point): DumbAction.DumbClick =
dumbEditionRepository.dumbActionBuilder.createNewDumbClick(context, position)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle

import com.buzbuz.smartautoclicker.core.base.extensions.showAsOverlay
import com.buzbuz.smartautoclicker.core.base.isStopScenarioKey
import com.buzbuz.smartautoclicker.core.common.overlays.base.viewModels
import com.buzbuz.smartautoclicker.core.common.overlays.menu.OverlayMenu
import com.buzbuz.smartautoclicker.core.ui.utils.AnimatedStatesImageButtonController
Expand Down Expand Up @@ -143,7 +144,7 @@ class MainMenu(private val onStopClicked: () -> Unit) : OverlayMenu() {
}

override fun onKeyEvent(keyEvent: KeyEvent): Boolean {
if (keyEvent.keyCode != KeyEvent.KEYCODE_VOLUME_DOWN) return false
if (!keyEvent.isStopScenarioKey()) return false

when (keyEvent.action) {
KeyEvent.ACTION_DOWN -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.buzbuz.smartautoclicker.feature.smart.config.ui.action.brief

import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -24,6 +25,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle

import com.buzbuz.smartautoclicker.core.base.isStopScenarioKey
import com.buzbuz.smartautoclicker.core.common.overlays.base.viewModels
import com.buzbuz.smartautoclicker.core.common.overlays.dialog.implementation.MoveToDialog
import com.buzbuz.smartautoclicker.core.common.overlays.menu.implementation.brief.ItemBrief
Expand Down Expand Up @@ -52,6 +54,12 @@ class SmartActionsBriefMenu(initialItemIndex: Int) : ItemBriefMenu(

private lateinit var viewBinding: OverlayActionsBriefMenuBinding

/**
* Tells if this service has handled onKeyEvent with ACTION_DOWN for a key in order to return
* the correct value when ACTION_UP is received.
*/
private var keyDownHandled: Boolean = false


override fun onCreate() {
super.onCreate()
Expand Down Expand Up @@ -98,6 +106,29 @@ class SmartActionsBriefMenu(initialItemIndex: Int) : ItemBriefMenu(
showActionConfigDialog((item.data as UiAction).action)
}

override fun onKeyEvent(keyEvent: KeyEvent): Boolean {
if (!keyEvent.isStopScenarioKey()) return false

when (keyEvent.action) {
KeyEvent.ACTION_DOWN -> {
if (viewModel.stopAction()) {
keyDownHandled = true
updateReplayingState(false)
return true
}
}

KeyEvent.ACTION_UP -> {
if (keyDownHandled) {
keyDownHandled = false
return true
}
}
}

return false
}

override fun onMenuItemClicked(viewId: Int) {
when (viewId) {
R.id.btn_back -> onBackClicked()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ class SmartActionsBriefViewModel @Inject constructor(
}
}

fun stopAction(): Boolean {
if (!detectionRepository.isRunning()) return false

detectionRepository.stopDetection()
return true
}

fun swapActions(i: Int, j: Int) {
val actions = editionRepository.editionState.getEditedEventActions<Action>()?.toMutableList() ?: return
Collections.swap(actions, i, j)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.buzbuz.smartautoclicker.core.base.isStopScenarioKey

import com.buzbuz.smartautoclicker.core.domain.model.scenario.Scenario
import com.buzbuz.smartautoclicker.core.common.overlays.base.viewModels
Expand Down Expand Up @@ -89,7 +90,7 @@ class TryElementOverlayMenu(
}

override fun onKeyEvent(keyEvent: KeyEvent): Boolean {
if (keyEvent.keyCode != KeyEvent.KEYCODE_VOLUME_DOWN) return false
if (!keyEvent.isStopScenarioKey()) return false

if (keyEvent.action == KeyEvent.ACTION_DOWN) {
viewModel.stopTry()
Expand Down

0 comments on commit 8e7bb57

Please sign in to comment.