Skip to content

Commit

Permalink
Merge pull request langerhans#22 from langerhans/bugfix/rp4crash
Browse files Browse the repository at this point in the history
Fix crash on devices without vibration setting
  • Loading branch information
langerhans authored Feb 11, 2024
2 parents 761978d + 0e0ddd0 commit adafc16
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# OdinTools
Collection of utilities for the AYN Odin 2.

Latest release: **[1.2.0](https://github.com/langerhans/OdinTools/releases/latest)**
Latest release: **[1.2.1](https://github.com/langerhans/OdinTools/releases/latest)**

### Features
#### App overrides
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "de.langerhans.odintools"
minSdk = 33
targetSdk = 34
versionCode = 8
versionName = "1.2.0"
versionCode = 9
versionName = "1.2.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
49 changes: 27 additions & 22 deletions app/src/main/java/de/langerhans/odintools/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dagger.hilt.android.AndroidEntryPoint
import de.langerhans.odintools.R
import de.langerhans.odintools.appsettings.AppOverrideListScreen
import de.langerhans.odintools.appsettings.AppOverridesScreen
import de.langerhans.odintools.tools.DeviceType.ODIN2
import de.langerhans.odintools.ui.composables.*
import de.langerhans.odintools.ui.theme.OdinToolsTheme

Expand Down Expand Up @@ -160,19 +161,21 @@ fun SettingsScreen(
) {
viewModel.updateSingleHomePreference(it)
}
TriggerPreference(
icon = R.drawable.ic_gamepad,
title = R.string.m1Button,
description = R.string.remapButtonDescription
) {
viewModel.remapButtonClicked("remap_custom_to_m1_value")
}
TriggerPreference(
icon = R.drawable.ic_gamepad,
title = R.string.m2Button,
description = R.string.remapButtonDescription
) {
viewModel.remapButtonClicked("remap_custom_to_m2_value")
if (uiState.deviceType == ODIN2) {
TriggerPreference(
icon = R.drawable.ic_gamepad,
title = R.string.m1Button,
description = R.string.remapButtonDescription
) {
viewModel.remapButtonClicked("remap_custom_to_m1_value")
}
TriggerPreference(
icon = R.drawable.ic_gamepad,
title = R.string.m2Button,
description = R.string.remapButtonDescription
) {
viewModel.remapButtonClicked("remap_custom_to_m2_value")
}
}
SettingsHeader(name = R.string.display)
TriggerPreference(
Expand All @@ -182,15 +185,17 @@ fun SettingsScreen(
) {
viewModel.saturationClicked()
}
SettingsHeader(name = R.string.haptics)
SwitchableTriggerPreference(
icon = R.drawable.ic_vibration,
title = R.string.vibrationStrength,
description = R.string.vibrationStrengthDescription,
state = uiState.vibrationEnabled,
onClick = { viewModel.vibrationClicked() }
) {
viewModel.updateVibrationPreference(it)
if (uiState.deviceType == ODIN2) {
SettingsHeader(name = R.string.haptics)
SwitchableTriggerPreference(
icon = R.drawable.ic_vibration,
title = R.string.vibrationStrength,
description = R.string.vibrationStrengthDescription,
state = uiState.vibrationEnabled,
onClick = { viewModel.vibrationClicked() }
) {
viewModel.updateVibrationPreference(it)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/de/langerhans/odintools/main/MainUiModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import androidx.annotation.StringRes
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import de.langerhans.odintools.tools.DeviceType
import de.langerhans.odintools.tools.DeviceType.ODIN2

data class MainUiModel(
val deviceType: DeviceType = ODIN2,
val deviceVersion: String = "",
val showNotAnOdinDialog: Boolean = false,
val showPServerNotAvailableDialog: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.langerhans.odintools.main

import android.view.KeyEvent
import androidx.compose.runtime.toMutableStateList
import androidx.lifecycle.ViewModel
import android.view.KeyEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import de.langerhans.odintools.R
import de.langerhans.odintools.data.SharedPrefsRepo
import de.langerhans.odintools.models.ControllerStyle.*
import de.langerhans.odintools.models.L2R2Style.*
import de.langerhans.odintools.tools.DeviceType.ODIN2
import de.langerhans.odintools.tools.DeviceUtils
import de.langerhans.odintools.tools.ShellExecutor
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -38,15 +39,16 @@ class MainViewModel @Inject constructor(
executor.enableA11yService()
executor.grantAllAppsPermission()

val isOdin2 = deviceUtils.isOdin2()
val deviceType = deviceUtils.getDeviceType()
val preventHomePressSetting = executor.getBooleanSystemSetting("prevent_press_home_accidentally", true)
val vibrationOnSetting = executor.getBooleanSystemSetting("vibrate_on", true)
val vibrationStrength = executor.getVibrationStrength()

_uiState.update { _ ->
MainUiModel(
deviceType = deviceType,
deviceVersion = deviceUtils.getDeviceVersion(),
showNotAnOdinDialog = !isOdin2,
showNotAnOdinDialog = deviceType != ODIN2,
singleHomeEnabled = !preventHomePressSetting,
showPServerNotAvailableDialog = !deviceUtils.isPServerAvailable(),
overrideDelayEnabled = prefs.overrideDelay,
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/de/langerhans/odintools/tools/DeviceUtils.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package de.langerhans.odintools.tools

import de.langerhans.odintools.tools.DeviceType.*
import javax.inject.Inject

class DeviceUtils @Inject constructor(
private val shellExecutor: ShellExecutor
) {
fun isOdin2() = shellExecutor.executeAsRoot("getprop ro.vendor.retro.name").map { it == "Q9" }
.getOrDefault(false)

fun getDeviceType(): DeviceType {
val deviceName = shellExecutor.executeAsRoot("getprop ro.vendor.retro.name").getOrNull()
return when (deviceName) {
"Q9" -> ODIN2
"4.0", "4.0P" -> RP4
else -> OTHER
}
}

fun isPServerAvailable() = shellExecutor.pServerAvailable

fun getDeviceVersion() = shellExecutor.executeAsRoot("getprop ro.build.odin2.ota.version").map { it ?: "" }
.getOrDefault("")
}

enum class DeviceType {
ODIN2,
RP4,
OTHER
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ShellExecutor @Inject constructor() {
val defaultValue = 0

return executeAsRoot("cat /d/haptics/user_vmax_mv")
.map { it?.toInt() ?: defaultValue }
.mapCatching { it?.toInt() ?: defaultValue } // This throws on RP4 because it has no rumble
.getOrDefault(defaultValue)
}

Expand Down

0 comments on commit adafc16

Please sign in to comment.