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 @@ -16,6 +16,7 @@ import de.robv.android.xposed.XposedHelpers
import ru.hepolise.volumekeytrackcontrol.module.util.LogHelper
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLongPressDuration
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.isSwapButtons
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.getVibrator
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.triggerVibration

Expand Down Expand Up @@ -150,17 +151,25 @@ object VolumeKeyControlModuleHandlers {
}

private fun doHook(keyCode: Int, event: KeyEvent, param: MethodHookParam) {
val isSwap = SharedPreferencesUtil.prefs().isSwapButtons()
log("isSwap: $isSwap")
val isDown = when (keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN -> !isSwap
KeyEvent.KEYCODE_VOLUME_UP -> isSwap
else -> throw IllegalStateException("Unknown key code: $keyCode")
}
when (event.action) {
KeyEvent.ACTION_DOWN -> handleDownAction(keyCode, param)
KeyEvent.ACTION_UP -> handleUpAction(keyCode, param)
KeyEvent.ACTION_DOWN -> handleDownAction(isDown, param)
KeyEvent.ACTION_UP -> handleUpAction(isDown, keyCode, param)
}
param.setResult(0)
}

private fun handleDownAction(keyCode: Int, param: MethodHookParam) {
when (keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN -> isDownPressed = true
KeyEvent.KEYCODE_VOLUME_UP -> isUpPressed = true
private fun handleDownAction(isDown: Boolean, param: MethodHookParam) {
if (isDown) {
isDownPressed = true
} else {
isUpPressed = true
}
log("down action received, down: $isDownPressed, up: $isUpPressed")
isLongPress = false
Expand All @@ -171,17 +180,18 @@ object VolumeKeyControlModuleHandlers {
// only one button pressed
if (isMusicActive()) {
log("music is active, creating delayed skip")
handleVolumeSkipPress(param.thisObject, keyCode)
handleVolumeSkipPress(param.thisObject, isDown)
}
log("creating delayed play pause")
handleVolumePlayPausePress(param.thisObject)
}
}

private fun handleUpAction(keyCode: Int, param: MethodHookParam) {
when (keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN -> isDownPressed = false
KeyEvent.KEYCODE_VOLUME_UP -> isUpPressed = false
private fun handleUpAction(isDown: Boolean, keyCode: Int, param: MethodHookParam) {
if (isDown) {
isDownPressed = false
} else {
isUpPressed = false
}
log("up action received, down: $isDownPressed, up: $isUpPressed")
handleVolumeAllPressAbort(param.thisObject)
Expand Down Expand Up @@ -235,14 +245,10 @@ object VolumeKeyControlModuleHandlers {
)
}

private fun handleVolumeSkipPress(instance: Any, keyCode: Int) {
private fun handleVolumeSkipPress(instance: Any, isDown: Boolean) {
val handler = instance.getHandler()
handler.postDelayed(
when (keyCode) {
KeyEvent.KEYCODE_VOLUME_UP -> getRunnable(instance, VOLUME_UP_LONG_PRESS)
KeyEvent.KEYCODE_VOLUME_DOWN -> getRunnable(instance, VOLUME_DOWN_LONG_PRESS)
else -> return
},
getRunnable(instance, if (isDown) VOLUME_DOWN_LONG_PRESS else VOLUME_UP_LONG_PRESS),
SharedPreferencesUtil.prefs().getLongPressDuration().toLong()
)
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ru.hepolise.volumekeytrackcontrol.ui.component

import android.content.SharedPreferences
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION
import ru.hepolise.volumekeytrackcontrolmodule.R

@Composable
fun LongPressSetting(
longPressDuration: Int,
sharedPreferences: SharedPreferences,
onValueChange: (Int) -> Unit
) {

Text(text = stringResource(R.string.long_press_settings), fontSize = 20.sp)

Slider(
value = longPressDuration.toFloat(),
onValueChange = { onValueChange(it.toInt()) },
valueRange = 100f..1000f,
onValueChangeFinished = {
sharedPreferences.edit().putInt(LONG_PRESS_DURATION, longPressDuration)
.apply()
},
modifier = Modifier.widthIn(max = 300.dp)
)

var showLongPressTimeoutDialog by remember { mutableStateOf(false) }
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = stringResource(R.string.long_press_duration, longPressDuration),
modifier = Modifier.clickable { showLongPressTimeoutDialog = true }
)
IconButton(
onClick = {
showLongPressTimeoutDialog = true
}
) {
Icon(
imageVector = Icons.Default.Edit,
contentDescription = stringResource(R.string.edit)
)
}
}

if (showLongPressTimeoutDialog) {
NumberAlertDialog(
title = stringResource(R.string.long_press_duration_dialog_title),
defaultValue = longPressDuration,
minValue = 100,
maxValue = 1000,
onDismissRequest = { showLongPressTimeoutDialog = false },
onConfirm = {
onValueChange(it)
sharedPreferences.edit().putInt(LONG_PRESS_DURATION, it).apply()
showLongPressTimeoutDialog = false
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.hepolise.volumekeytrackcontrol.ui.component

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink
import androidx.compose.ui.unit.dp
import ru.hepolise.volumekeytrackcontrol.util.Constants
import ru.hepolise.volumekeytrackcontrolmodule.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ModuleIsNotEnabled() {
Scaffold(
topBar = {
TopAppBar(title = { Text(stringResource(R.string.app_name)) })
}
) { padding ->
Box(
modifier = Modifier
.fillMaxSize()
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(padding),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = buildAnnotatedString {
append(stringResource(id = R.string.module_is_not_enabled))
append(" ")
withLink(
LinkAnnotation.Url(
url = Constants.GITHUB_NEW_ISSUE,
styles = TextLinkStyles(
style = SpanStyle(
color = MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline
)
)
)
) {
append(stringResource(id = R.string.open_issue))
}
}
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.hepolise.volumekeytrackcontrol.ui.component

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import ru.hepolise.volumekeytrackcontrolmodule.R

@Composable
fun NumberAlertDialog(
title: String,
defaultValue: Int,
onDismissRequest: () -> Unit,
onConfirm: (Int) -> Unit,
minValue: Int,
maxValue: Int
) {
fun validate(value: Int) = value in minValue..maxValue
var value by remember { mutableStateOf(defaultValue.toString()) }
val focusRequester = remember { FocusRequester() }
AlertDialog(
onDismissRequest = onDismissRequest,
title = { Text(text = title) },
text = {
Column {
OutlinedTextField(
value = value,
onValueChange = { value = it },
label = { Text(stringResource(R.string.value_in_range, minValue, maxValue)) },
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
isError = value.toIntOrNull() == null || !validate(value.toInt()),
modifier = Modifier.focusRequester(focusRequester)
)
}
},
confirmButton = {
Button(
onClick = {
onConfirm(value.toInt())
},
enabled = value.toIntOrNull() != null && validate(value.toInt())
) {
Text(text = stringResource(R.string.ok))
}
},
dismissButton = {
TextButton(onClick = onDismissRequest) {
Text(text = stringResource(R.string.cancel))
}
},
)
LaunchedEffect(true) {
focusRequester.requestFocus()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.hepolise.volumekeytrackcontrol.ui.component

import android.content.SharedPreferences
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.IS_SWAP_BUTTONS
import ru.hepolise.volumekeytrackcontrolmodule.R

@Composable
fun SwapButtonsSetting(
isSwapButtons: Boolean,
sharedPreferences: SharedPreferences,
onValueChange: (Boolean) -> Unit
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = isSwapButtons,
onCheckedChange = {
onValueChange(it)
sharedPreferences.edit().putBoolean(IS_SWAP_BUTTONS, it).apply()
}
)
Spacer(modifier = Modifier.width(4.dp))
Text(
text = stringResource(R.string.swap_buttons),
modifier = Modifier.clickable {
onValueChange(!isSwapButtons)
sharedPreferences.edit().putBoolean(IS_SWAP_BUTTONS, !isSwapButtons).apply()
}
)
}
}
Loading