Skip to content

Commit 9be4527

Browse files
authored
Merge pull request #73 from Hepolise/dev
Merge dev to main
2 parents 638bc45 + e47de1f commit 9be4527

7 files changed

Lines changed: 85 additions & 32 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Works when the screen is off.
2222

2323
* OnePlus 10 Pro (Android 13 & 14)
2424

25-
[Open an issue](https://github.com/Hepolise/VolumeKeyMusicManagerModule/issues/new) if you are experiencing some troubles on your device
25+
[Open an issue](https://github.com/Hepolise/VolumeKeyTrackControlModule/issues/new) if you are
26+
experiencing some troubles on your device

app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/SettingsActivity.kt

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ru.hepolise.volumekeytrackcontrol.ui
22

33
import android.annotation.SuppressLint
4-
import android.app.Activity
54
import android.content.Context
65
import android.content.Intent
76
import android.net.Uri
@@ -64,10 +63,17 @@ import androidx.compose.ui.focus.FocusRequester
6463
import androidx.compose.ui.focus.focusRequester
6564
import androidx.compose.ui.platform.LocalContext
6665
import androidx.compose.ui.res.stringResource
66+
import androidx.compose.ui.text.LinkAnnotation
67+
import androidx.compose.ui.text.SpanStyle
68+
import androidx.compose.ui.text.TextLinkStyles
69+
import androidx.compose.ui.text.buildAnnotatedString
6770
import androidx.compose.ui.text.input.KeyboardType
71+
import androidx.compose.ui.text.style.TextDecoration
72+
import androidx.compose.ui.text.withLink
6873
import androidx.compose.ui.tooling.preview.Preview
6974
import androidx.compose.ui.unit.dp
7075
import androidx.compose.ui.unit.sp
76+
import ru.hepolise.volumekeytrackcontrol.util.Constants
7177
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION
7278
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION_DEFAULT_VALUE
7379
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.SELECTED_EFFECT
@@ -78,14 +84,14 @@ import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_AM
7884
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_LENGTH
7985
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_LENGTH_DEFAULT_VALUE
8086
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLongPressDuration
81-
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getSelectedEffect
8287
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationAmplitude
8388
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationLength
89+
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationType
8490
import ru.hepolise.volumekeytrackcontrol.util.VibrationType
8591
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.getVibrator
8692
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.triggerVibration
8793
import ru.hepolise.volumekeytrackcontrolmodule.R
88-
import kotlin.system.exitProcess
94+
8995

9096
class SettingsActivity : ComponentActivity() {
9197
override fun onCreate(savedInstanceState: Bundle?) {
@@ -130,14 +136,12 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {
130136
val sharedPreferences = try {
131137
context.getSharedPreferences(SETTINGS_PREFS_NAME, Context.MODE_WORLD_READABLE)
132138
} catch (e: SecurityException) {
133-
Toast.makeText(context, R.string.module_is_not_enabled, Toast.LENGTH_LONG).show()
134-
// Clear the application stack and exit
135-
(context as? Activity)?.finishAffinity()
136-
exitProcess(0)
139+
ModuleIsNotEnabled()
140+
return
137141
}
138142

139143
var longPressDuration by remember { mutableIntStateOf(sharedPreferences.getLongPressDuration()) }
140-
var selectedEffect by remember { mutableIntStateOf(sharedPreferences.getSelectedEffect()) }
144+
var vibrationType by remember { mutableStateOf(sharedPreferences.getVibrationType()) }
141145
var vibrationLength by remember { mutableIntStateOf(sharedPreferences.getVibrationLength()) }
142146
var vibrationAmplitude by remember { mutableIntStateOf(sharedPreferences.getVibrationAmplitude()) }
143147

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

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

213-
val vibrationType = VibrationType.values[selectedEffect]
214217
var effectExpanded by remember { mutableStateOf(false) }
215218
ExposedDropdownMenuBox(
216219
expanded = effectExpanded,
@@ -228,12 +231,12 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {
228231
ExposedDropdownMenu(
229232
expanded = effectExpanded,
230233
onDismissRequest = { effectExpanded = false }) {
231-
VibrationType.values.forEachIndexed { index, effect ->
234+
VibrationType.values.forEach { effect ->
232235
DropdownMenuItem(
233236
text = { Text(stringResource(VibrationEffectTitles[effect]!!)) },
234237
onClick = {
235-
selectedEffect = index
236-
sharedPreferences.edit().putInt(SELECTED_EFFECT, index)
238+
vibrationType = effect
239+
sharedPreferences.edit().putString(SELECTED_EFFECT, effect.key)
237240
.apply()
238241
effectExpanded = false
239242
}
@@ -350,7 +353,7 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {
350353

351354
Button(onClick = {
352355
sharedPreferences.edit().clear().apply()
353-
selectedEffect = SELECTED_EFFECT_DEFAULT_VALUE
356+
vibrationType = VibrationType.fromKey(SELECTED_EFFECT_DEFAULT_VALUE)
354357
vibrationLength = VIBRATION_LENGTH_DEFAULT_VALUE
355358
vibrationAmplitude = VIBRATION_AMPLITUDE_DEFAULT_VALUE
356359
longPressDuration = LONG_PRESS_DURATION_DEFAULT_VALUE
@@ -367,8 +370,7 @@ fun VibrationSettingsScreen(vibrator: Vibrator?) {
367370

368371
Button(onClick = {
369372
val intent = Intent(Intent.ACTION_VIEW)
370-
intent.data =
371-
Uri.parse("https://github.com/Hepolise/VolumeKeyTrackControlModule")
373+
intent.data = Uri.parse(Constants.GITHUB_URL)
372374
context.startActivity(intent)
373375
}) {
374376
Text(stringResource(R.string.about))
@@ -439,6 +441,49 @@ fun NumberAlertDialog(
439441
}
440442
}
441443

444+
@OptIn(ExperimentalMaterial3Api::class)
445+
@Composable
446+
fun ModuleIsNotEnabled() {
447+
Scaffold(
448+
topBar = {
449+
TopAppBar(title = { Text(stringResource(R.string.app_name)) })
450+
}
451+
) { padding ->
452+
Box(
453+
modifier = Modifier
454+
.fillMaxSize()
455+
) {
456+
Column(
457+
modifier = Modifier
458+
.fillMaxSize()
459+
.padding(padding),
460+
verticalArrangement = Arrangement.spacedBy(16.dp),
461+
horizontalAlignment = Alignment.CenterHorizontally
462+
) {
463+
Text(
464+
text = buildAnnotatedString {
465+
append(stringResource(id = R.string.module_is_not_enabled))
466+
append(" ")
467+
withLink(
468+
LinkAnnotation.Url(
469+
url = Constants.GITHUB_NEW_ISSUE,
470+
styles = TextLinkStyles(
471+
style = SpanStyle(
472+
color = MaterialTheme.colorScheme.primary,
473+
textDecoration = TextDecoration.Underline
474+
)
475+
)
476+
)
477+
) {
478+
append(stringResource(id = R.string.open_issue))
479+
}
480+
}
481+
)
482+
}
483+
}
484+
}
485+
}
486+
442487
@Preview(showBackground = true)
443488
@Composable
444489
fun PreviewVibrationSettingsScreen() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.hepolise.volumekeytrackcontrol.util
2+
3+
object Constants {
4+
const val GITHUB_URL = "https://github.com/Hepolise/VolumeKeyTrackControlModule"
5+
const val GITHUB_NEW_ISSUE = "$GITHUB_URL/issues/new"
6+
}

app/src/main/java/ru/hepolise/volumekeytrackcontrol/util/SharedPreferencesUtil.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.hepolise.volumekeytrackcontrol.util
22

33
import android.content.SharedPreferences
4+
import android.os.Build
45
import android.view.ViewConfiguration
56
import de.robv.android.xposed.XSharedPreferences
67
import ru.hepolise.volumekeytrackcontrolmodule.BuildConfig
@@ -13,18 +14,15 @@ object SharedPreferencesUtil {
1314
const val VIBRATION_AMPLITUDE = "vibrationAmplitude"
1415
const val LONG_PRESS_DURATION = "longPressDuration"
1516

16-
const val SELECTED_EFFECT_DEFAULT_VALUE = 0
17+
val SELECTED_EFFECT_DEFAULT_VALUE =
18+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) VibrationType.Click.key else VibrationType.Manual.key
1719
const val VIBRATION_LENGTH_DEFAULT_VALUE = 50
1820
const val VIBRATION_AMPLITUDE_DEFAULT_VALUE = 128
1921
val LONG_PRESS_DURATION_DEFAULT_VALUE = ViewConfiguration.getLongPressTimeout()
2022

21-
fun SharedPreferences?.getSelectedEffect(): Int {
22-
val defaultValue = SELECTED_EFFECT_DEFAULT_VALUE
23-
return this?.getInt(SELECTED_EFFECT, defaultValue) ?: defaultValue
24-
}
25-
2623
fun SharedPreferences?.getVibrationType(): VibrationType {
27-
return VibrationType.values[getSelectedEffect()]
24+
val defaultValue = SELECTED_EFFECT_DEFAULT_VALUE
25+
return VibrationType.fromKey(this?.getString(SELECTED_EFFECT, defaultValue) ?: defaultValue)
2826
}
2927

3028
fun SharedPreferences?.getVibrationLength(): Int {

app/src/main/java/ru/hepolise/volumekeytrackcontrol/util/VibratorUtil.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ object VibratorUtil {
4141
}
4242

4343

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

4848
companion object {
4949
val values: List<VibrationType> by lazy {
@@ -59,17 +59,19 @@ sealed class VibrationType(val value: Int) {
5959
add(Disabled)
6060
}
6161
}
62+
63+
fun fromKey(key: String): VibrationType = values.single { it.key == key }
6264
}
6365

6466
@RequiresApi(Build.VERSION_CODES.Q)
65-
data object Click : VibrationType(VibrationEffect.EFFECT_CLICK)
67+
data object Click : VibrationType(VibrationEffect.EFFECT_CLICK, "click")
6668

6769
@RequiresApi(Build.VERSION_CODES.Q)
68-
data object DoubleClick : VibrationType(VibrationEffect.EFFECT_DOUBLE_CLICK)
70+
data object DoubleClick : VibrationType(VibrationEffect.EFFECT_DOUBLE_CLICK, "double_click")
6971

7072
@RequiresApi(Build.VERSION_CODES.Q)
71-
data object HeavyClick : VibrationType(VibrationEffect.EFFECT_HEAVY_CLICK)
73+
data object HeavyClick : VibrationType(VibrationEffect.EFFECT_HEAVY_CLICK, "heavy_click")
7274

7375
@RequiresApi(Build.VERSION_CODES.Q)
74-
data object Tick : VibrationType(VibrationEffect.EFFECT_TICK)
76+
data object Tick : VibrationType(VibrationEffect.EFFECT_TICK, "tick")
7577
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<string name="settings_reset_toast">Settings are reset to default</string>
2626
<string name="about">About</string>
2727

28-
<string name="module_is_not_enabled">Module is not enabled in LSPosed</string>
28+
<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>
29+
<string name="open_issue">open an issue</string>
2930

3031
<string name="ok">OK</string>
3132
<string name="cancel">Cancel</string>

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ dependencyResolutionManagement {
1515
mavenCentral()
1616
}
1717
}
18-
rootProject.name = "VolumeKeyMusicManagerModule"
18+
rootProject.name = "VolumeKeyTrackControlModule"
1919
include ':app'

0 commit comments

Comments
 (0)