11package ru.hepolise.volumekeytrackcontrol.ui
22
33import android.annotation.SuppressLint
4- import android.app.Activity
54import android.content.Context
65import android.content.Intent
76import android.net.Uri
@@ -64,10 +63,17 @@ import androidx.compose.ui.focus.FocusRequester
6463import androidx.compose.ui.focus.focusRequester
6564import androidx.compose.ui.platform.LocalContext
6665import 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
6770import androidx.compose.ui.text.input.KeyboardType
71+ import androidx.compose.ui.text.style.TextDecoration
72+ import androidx.compose.ui.text.withLink
6873import androidx.compose.ui.tooling.preview.Preview
6974import androidx.compose.ui.unit.dp
7075import androidx.compose.ui.unit.sp
76+ import ru.hepolise.volumekeytrackcontrol.util.Constants
7177import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION
7278import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LONG_PRESS_DURATION_DEFAULT_VALUE
7379import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.SELECTED_EFFECT
@@ -78,14 +84,14 @@ import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_AM
7884import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_LENGTH
7985import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.VIBRATION_LENGTH_DEFAULT_VALUE
8086import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLongPressDuration
81- import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getSelectedEffect
8287import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationAmplitude
8388import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationLength
89+ import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getVibrationType
8490import ru.hepolise.volumekeytrackcontrol.util.VibrationType
8591import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.getVibrator
8692import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.triggerVibration
8793import ru.hepolise.volumekeytrackcontrolmodule.R
88- import kotlin.system.exitProcess
94+
8995
9096class 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
444489fun PreviewVibrationSettingsScreen () {
0 commit comments