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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Works when the screen is off.

* OnePlus 10 Pro (Android 13 & 14)

[Open an issue](https://github.com/Hepolise/VolumeKeyMusicManagerModule/issues/new) if you are experiencing some troubles on your device
[Open an issue](https://github.com/Hepolise/VolumeKeyTrackControlModule/issues/new) if you are
experiencing some troubles on your device
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.hepolise.volumekeytrackcontrol.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
Expand Down Expand Up @@ -64,10 +63,17 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalContext
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.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import ru.hepolise.volumekeytrackcontrol.util.Constants
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION_DEFAULT_VALUE
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.SELECTED_EFFECT
Expand All @@ -78,14 +84,14 @@ import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_AM
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_LENGTH
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_LENGTH_DEFAULT_VALUE
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLongPressDuration
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getSelectedEffect
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationAmplitude
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationLength
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationType
import ru.hepolise.volumekeytrackcontrol.util.VibrationType
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.getVibrator
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.triggerVibration
import ru.hepolise.volumekeytrackcontrolmodule.R
import kotlin.system.exitProcess


class SettingsActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -130,14 +136,12 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {
val sharedPreferences = try {
context.getSharedPreferences(SETTINGS_PREFS_NAME, Context.MODE_WORLD_READABLE)
} catch (e: SecurityException) {
Toast.makeText(context, R.string.module_is_not_enabled, Toast.LENGTH_LONG).show()
// Clear the application stack and exit
(context as? Activity)?.finishAffinity()
exitProcess(0)
ModuleIsNotEnabled()
return
}

var longPressDuration by remember { mutableIntStateOf(sharedPreferences.getLongPressDuration()) }
var selectedEffect by remember { mutableIntStateOf(sharedPreferences.getSelectedEffect()) }
var vibrationType by remember { mutableStateOf(sharedPreferences.getVibrationType()) }
var vibrationLength by remember { mutableIntStateOf(sharedPreferences.getVibrationLength()) }
var vibrationAmplitude by remember { mutableIntStateOf(sharedPreferences.getVibrationAmplitude()) }

Expand Down Expand Up @@ -210,7 +214,6 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {

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

val vibrationType = VibrationType.values[selectedEffect]
var effectExpanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = effectExpanded,
Expand All @@ -228,12 +231,12 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {
ExposedDropdownMenu(
expanded = effectExpanded,
onDismissRequest = { effectExpanded = false }) {
VibrationType.values.forEachIndexed { index, effect ->
VibrationType.values.forEach { effect ->
DropdownMenuItem(
text = { Text(stringResource(VibrationEffectTitles[effect]!!)) },
onClick = {
selectedEffect = index
sharedPreferences.edit().putInt(SELECTED_EFFECT, index)
vibrationType = effect
sharedPreferences.edit().putString(SELECTED_EFFECT, effect.key)
.apply()
effectExpanded = false
}
Expand Down Expand Up @@ -350,7 +353,7 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {

Button(onClick = {
sharedPreferences.edit().clear().apply()
selectedEffect = SELECTED_EFFECT_DEFAULT_VALUE
vibrationType = VibrationType.fromKey(SELECTED_EFFECT_DEFAULT_VALUE)
vibrationLength = VIBRATION_LENGTH_DEFAULT_VALUE
vibrationAmplitude = VIBRATION_AMPLITUDE_DEFAULT_VALUE
longPressDuration = LONG_PRESS_DURATION_DEFAULT_VALUE
Expand All @@ -367,8 +370,7 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {

Button(onClick = {
val intent = Intent(Intent.ACTION_VIEW)
intent.data =
Uri.parse("https://github.com/Hepolise/VolumeKeyTrackControlModule")
intent.data = Uri.parse(Constants.GITHUB_URL)
context.startActivity(intent)
}) {
Text(stringResource(R.string.about))
Expand Down Expand Up @@ -439,6 +441,49 @@ fun NumberAlertDialog(
}
}

@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))
}
}
)
}
}
}
}

@Preview(showBackground = true)
@Composable
fun PreviewVibrationSettingsScreen() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.hepolise.volumekeytrackcontrol.util

object Constants {
const val GITHUB_URL = "https://github.com/Hepolise/VolumeKeyTrackControlModule"
const val GITHUB_NEW_ISSUE = "$GITHUB_URL/issues/new"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.hepolise.volumekeytrackcontrol.util

import android.content.SharedPreferences
import android.os.Build
import android.view.ViewConfiguration
import de.robv.android.xposed.XSharedPreferences
import ru.hepolise.volumekeytrackcontrolmodule.BuildConfig
Expand All @@ -13,18 +14,15 @@ object SharedPreferencesUtil {
const val VIBRATION_AMPLITUDE = "vibrationAmplitude"
const val LONG_PRESS_DURATION = "longPressDuration"

const val SELECTED_EFFECT_DEFAULT_VALUE = 0
val SELECTED_EFFECT_DEFAULT_VALUE =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) VibrationType.Click.key else VibrationType.Manual.key
const val VIBRATION_LENGTH_DEFAULT_VALUE = 50
const val VIBRATION_AMPLITUDE_DEFAULT_VALUE = 128
val LONG_PRESS_DURATION_DEFAULT_VALUE = ViewConfiguration.getLongPressTimeout()

fun SharedPreferences?.getSelectedEffect(): Int {
val defaultValue = SELECTED_EFFECT_DEFAULT_VALUE
return this?.getInt(SELECTED_EFFECT, defaultValue) ?: defaultValue
}

fun SharedPreferences?.getVibrationType(): VibrationType {
return VibrationType.values[getSelectedEffect()]
val defaultValue = SELECTED_EFFECT_DEFAULT_VALUE
return VibrationType.fromKey(this?.getString(SELECTED_EFFECT, defaultValue) ?: defaultValue)
}

fun SharedPreferences?.getVibrationLength(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ object VibratorUtil {
}


sealed class VibrationType(val value: Int) {
data object Disabled : VibrationType(-1)
data object Manual : VibrationType(-1)
sealed class VibrationType(val value: Int, val key: String) {
data object Disabled : VibrationType(-1, "disabled")
data object Manual : VibrationType(-1, "manual")

companion object {
val values: List<VibrationType> by lazy {
Expand All @@ -59,17 +59,19 @@ sealed class VibrationType(val value: Int) {
add(Disabled)
}
}

fun fromKey(key: String): VibrationType = values.single { it.key == key }
}

@RequiresApi(Build.VERSION_CODES.Q)
data object Click : VibrationType(VibrationEffect.EFFECT_CLICK)
data object Click : VibrationType(VibrationEffect.EFFECT_CLICK, "click")

@RequiresApi(Build.VERSION_CODES.Q)
data object DoubleClick : VibrationType(VibrationEffect.EFFECT_DOUBLE_CLICK)
data object DoubleClick : VibrationType(VibrationEffect.EFFECT_DOUBLE_CLICK, "double_click")

@RequiresApi(Build.VERSION_CODES.Q)
data object HeavyClick : VibrationType(VibrationEffect.EFFECT_HEAVY_CLICK)
data object HeavyClick : VibrationType(VibrationEffect.EFFECT_HEAVY_CLICK, "heavy_click")

@RequiresApi(Build.VERSION_CODES.Q)
data object Tick : VibrationType(VibrationEffect.EFFECT_TICK)
data object Tick : VibrationType(VibrationEffect.EFFECT_TICK, "tick")
}
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<string name="settings_reset_toast">Settings are reset to default</string>
<string name="about">About</string>

<string name="module_is_not_enabled">Module is not enabled in LSPosed</string>
<string name="module_is_not_enabled">Module is not enabled in LSPosed.\nRestart the app after enabling the module.\nIf you think this is a bug, please</string>
<string name="open_issue">open an issue</string>

<string name="ok">OK</string>
<string name="cancel">Cancel</string>
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ dependencyResolutionManagement {
mavenCentral()
}
}
rootProject.name = "VolumeKeyMusicManagerModule"
rootProject.name = "VolumeKeyTrackControlModule"
include ':app'
Loading