Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SettingsRepo, refactoring & setting for battery limit #28

Merged
merged 34 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9cdc836
Standardize formatting and color values of icons
miguelguzmanr Feb 14, 2024
2ca10a8
Merge remote-tracking branch 'upstream/main' into charging-separation
miguelguzmanr Feb 14, 2024
8474896
Rename face buttons
miguelguzmanr Feb 15, 2024
36f4b41
Allow title in CheckBoxDialogPreference
miguelguzmanr Feb 15, 2024
890cf4f
Attempt consistency in titles & descriptions
miguelguzmanr Feb 15, 2024
b8e7196
Add SettingsRepo & refactoring
miguelguzmanr Feb 15, 2024
dec5050
Add padding in RemapButtonDialog
miguelguzmanr Feb 15, 2024
390b5c6
Add setting to bypass charge after 80%
miguelguzmanr Feb 15, 2024
e048cb7
Merge pull request #1 from miguelguzmanr/charging-separation
miguelguzmanr Feb 15, 2024
c00bd51
Rename function getDeviceCodename
miguelguzmanr Feb 20, 2024
0e8b1b7
Remove debug println
miguelguzmanr Feb 20, 2024
37ef775
Add explicit function for Settings initialization
miguelguzmanr Feb 20, 2024
18bc583
Add constant for accessibility services setting
miguelguzmanr Feb 20, 2024
ad7bc88
Refactor saturation property to a function
miguelguzmanr Feb 20, 2024
8150976
Remove unused properties
miguelguzmanr Feb 20, 2024
75a492d
Merge branch 'main' into main
miguelguzmanr Apr 18, 2024
9c4521c
Optimize imports
miguelguzmanr Apr 21, 2024
f465920
Update gradle.properties
miguelguzmanr Apr 21, 2024
1860873
Update dependencies
miguelguzmanr Apr 21, 2024
f13a20a
Add setting for charging separation
miguelguzmanr Apr 21, 2024
d7f98ea
Fix typo in preference key
miguelguzmanr Apr 21, 2024
4c35532
Create .gitattributes
miguelguzmanr Apr 21, 2024
26094cb
Add battery level broadcast receiver
miguelguzmanr Apr 21, 2024
9844999
Remove unused preference
miguelguzmanr Apr 21, 2024
cc32a96
Enable Aggregating Task
miguelguzmanr Apr 27, 2024
a980356
Make context private in SharedPrefsRepo
miguelguzmanr Apr 27, 2024
d91acac
Make deviceUtils private in MainViewModel
miguelguzmanr Apr 27, 2024
d9a5af6
Add setting to dump a log file
miguelguzmanr Apr 28, 2024
3a3548d
Fix incorrect setting restrictCharge
miguelguzmanr Apr 28, 2024
46a6c57
Add battery level monitor service
miguelguzmanr Apr 28, 2024
884c8dc
Remove debug requirement for dump log to file
miguelguzmanr May 15, 2024
9b1e42f
Add Debug settings header
miguelguzmanr May 15, 2024
b62e33c
Add get & set float values in ShellExecutor
miguelguzmanr May 15, 2024
8d99eea
Migrate battery level monitor to accessibility service
miguelguzmanr May 15, 2024
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
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<service
android:name=".tiles.ControllerTileService"
android:exported="true"
android:icon="@drawable/ic_controllerstyle"
android:icon="@drawable/ic_face_buttons"
android:label="@string/controllerStyle"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
Expand Down
32 changes: 22 additions & 10 deletions app/src/main/java/de/langerhans/odintools/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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.tools.SettingsRepo
import de.langerhans.odintools.ui.composables.*
import de.langerhans.odintools.ui.theme.OdinToolsTheme

Expand Down Expand Up @@ -62,13 +63,14 @@ fun SettingsScreen(viewModel: MainViewModel = hiltViewModel(), navigateToOverrid

if (uiState.showPServerNotAvailableDialog) {
PServerNotAvailableDialog()
} else if (uiState.showNotAnOdinDialog) {
} else if (uiState.showIncompatibleDeviceDialog) {
NotAnOdinDialog { viewModel.incompatibleDeviceDialogDismissed() }
}

if (uiState.showControllerStyleDialog) {
CheckBoxDialogPreference(
items = viewModel.controllerStyleOptions,
title = R.string.controllerStyle,
onCancel = {
viewModel.hideControllerStylePreference()
},
Expand All @@ -81,6 +83,7 @@ fun SettingsScreen(viewModel: MainViewModel = hiltViewModel(), navigateToOverrid
if (uiState.showL2r2StyleDialog) {
CheckBoxDialogPreference(
items = viewModel.l2r2StyleOptions,
title = R.string.l2r2mode,
onCancel = {
viewModel.hideL2r2StylePreference()
},
Expand Down Expand Up @@ -135,14 +138,14 @@ fun SettingsScreen(viewModel: MainViewModel = hiltViewModel(), navigateToOverrid
SwitchPreference(
icon = R.drawable.ic_more_time,
title = R.string.overrideDelay,
description = R.string.overrideDelayDesc,
description = R.string.overrideDelayDescription,
state = uiState.overrideDelayEnabled,
) {
viewModel.overrideDelayEnabled(it)
}
SettingsHeader(R.string.quicksettings)
SettingsHeader(R.string.quickSettings)
TriggerPreference(
icon = R.drawable.ic_controllerstyle,
icon = R.drawable.ic_face_buttons,
title = R.string.controllerStyle,
description = R.string.controllerStyleDesc,
) {
Expand All @@ -158,26 +161,26 @@ fun SettingsScreen(viewModel: MainViewModel = hiltViewModel(), navigateToOverrid
SettingsHeader(R.string.buttons)
SwitchPreference(
icon = R.drawable.ic_home,
title = R.string.doubleHomeTitle,
description = R.string.doubleHomeDescription,
state = uiState.singleHomeEnabled,
title = R.string.singlePressHome,
description = R.string.singlePressHomeDescription,
state = uiState.singlePressHomeEnabled,
) {
viewModel.updateSingleHomePreference(it)
viewModel.updateSinglePressHomePreference(it)
}
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")
viewModel.remapButtonClicked(SettingsRepo.KEY_CUSTOM_M1_VALUE)
}
TriggerPreference(
icon = R.drawable.ic_gamepad,
title = R.string.m2Button,
description = R.string.remapButtonDescription,
) {
viewModel.remapButtonClicked("remap_custom_to_m2_value")
viewModel.remapButtonClicked(SettingsRepo.KEY_CUSTOM_M2_VALUE)
}
}
SettingsHeader(name = R.string.display)
Expand All @@ -199,6 +202,15 @@ fun SettingsScreen(viewModel: MainViewModel = hiltViewModel(), navigateToOverrid
) {
viewModel.updateVibrationPreference(it)
}
SettingsHeader(R.string.battery)
SwitchPreference(
icon = R.drawable.ic_electrical_services,
title = R.string.chargeLimit,
description = R.string.chargeLimitDescription,
state = uiState.chargeLimitEnabled,
) {
viewModel.updateChargeLimitPreference(it)
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/de/langerhans/odintools/main/MainUiModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import de.langerhans.odintools.tools.DeviceType.ODIN2
data class MainUiModel(
val deviceType: DeviceType = ODIN2,
val deviceVersion: String = "",
val showNotAnOdinDialog: Boolean = false,
val showIncompatibleDeviceDialog: Boolean = false,
val showPServerNotAvailableDialog: Boolean = false,

val singleHomeEnabled: Boolean = false,
val singlePressHomeEnabled: Boolean = false,
val appOverridesEnabled: Boolean = true,
val overrideDelayEnabled: Boolean = false,

Expand All @@ -30,6 +30,8 @@ data class MainUiModel(
val showRemapButtonDialog: Boolean = false,
val currentButtonSetting: String = "",
val currentButtonKeyCode: Int = 0,

val chargeLimitEnabled: Boolean = false,
)

class CheckboxPreferenceUiModel(
Expand Down
45 changes: 25 additions & 20 deletions app/src/main/java/de/langerhans/odintools/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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.SettingsRepo
import de.langerhans.odintools.tools.ShellExecutor
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -21,6 +22,7 @@ import javax.inject.Inject
class MainViewModel @Inject constructor(
deviceUtils: DeviceUtils,
private val executor: ShellExecutor,
private val settings: SettingsRepo,
private val prefs: SharedPrefsRepo,
) : ViewModel() {

Expand All @@ -36,39 +38,35 @@ class MainViewModel @Inject constructor(
get() = _l2r2StyleOptions

init {
executor.applyRequiredSettings()

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 = deviceType != ODIN2,
singleHomeEnabled = !preventHomePressSetting,
showIncompatibleDeviceDialog = deviceType != ODIN2,
singlePressHomeEnabled = !settings.preventPressHome,
showPServerNotAvailableDialog = !executor.pServerAvailable,
overrideDelayEnabled = prefs.overrideDelay,
vibrationEnabled = vibrationOnSetting,
currentVibration = vibrationStrength,
vibrationEnabled = settings.vibrationEnabled,
currentVibration = settings.vibrationStrength,
chargeLimitEnabled = settings.chargingLimit80Enabled,
)
}
}

fun incompatibleDeviceDialogDismissed() {
_uiState.update { current ->
current.copy(showNotAnOdinDialog = false)
current.copy(showIncompatibleDeviceDialog = false)
}
}

fun updateSingleHomePreference(newValue: Boolean) {
fun updateSinglePressHomePreference(newValue: Boolean) {
// Invert here as prevent == double press
executor.setBooleanSystemSetting("prevent_press_home_accidentally", !newValue)
settings.preventPressHome = !newValue

_uiState.update { current ->
current.copy(singleHomeEnabled = newValue)
current.copy(singlePressHomeEnabled = newValue)
}
}

Expand Down Expand Up @@ -138,23 +136,22 @@ class MainViewModel @Inject constructor(

fun saveSaturation(newValue: Float) {
prefs.saturationOverride = newValue
executor.setSfSaturation(newValue)
settings.saturation = newValue
_uiState.update {
it.copy(showSaturationDialog = false)
}
}

fun updateVibrationPreference(newValue: Boolean) {
executor.setBooleanSystemSetting("vibrate_on", newValue)

settings.vibrationEnabled = newValue
_uiState.update { current ->
current.copy(vibrationEnabled = newValue)
}
}

fun vibrationClicked() {
_uiState.update {
it.copy(showVibrationDialog = true, currentVibration = executor.getVibrationStrength())
it.copy(showVibrationDialog = true, currentVibration = settings.vibrationStrength)
}
}

Expand All @@ -166,7 +163,7 @@ class MainViewModel @Inject constructor(

fun saveVibration(newValue: Int) {
prefs.vibrationStrength = newValue
executor.setVibrationStrength(newValue)
settings.vibrationStrength = newValue
_uiState.update {
it.copy(showVibrationDialog = false, currentVibration = newValue)
}
Expand All @@ -189,10 +186,10 @@ class MainViewModel @Inject constructor(
}

private fun getDefaultKeyCode(setting: String): Int {
if (setting == "remap_custom_to_m1_value") {
if (setting == SettingsRepo.KEY_CUSTOM_M1_VALUE) {
return KeyEvent.KEYCODE_BUTTON_C
}
if (setting == "remap_custom_to_m2_value") {
if (setting == SettingsRepo.KEY_CUSTOM_M2_VALUE) {
return KeyEvent.KEYCODE_BUTTON_Z
}
return KeyEvent.KEYCODE_UNKNOWN
Expand Down Expand Up @@ -226,4 +223,12 @@ class MainViewModel @Inject constructor(
it.copy(overrideDelayEnabled = newValue)
}
}

fun updateChargeLimitPreference(newValue: Boolean) {
settings.chargingLimit80Enabled = newValue

_uiState.update { current ->
current.copy(chargeLimitEnabled = newValue)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sealed class PerfMode(
) {
data object Standard : PerfMode("standard", 0, R.string.standard)
data object Performance : PerfMode("performance", 1, R.string.performance)
data object HighPerformance : PerfMode("highPerformance", 2, R.string.highperformance)
data object HighPerformance : PerfMode("highPerformance", 2, R.string.highPerformance)
data object Unknown : PerfMode("unknown", -1, R.string.unknown)

fun enable(executor: ShellExecutor) {
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/de/langerhans/odintools/tools/BootReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import javax.inject.Inject
class BootReceiver : BroadcastReceiver() {

@Inject
lateinit var sharedPrefsRepo: SharedPrefsRepo
lateinit var prefs: SharedPrefsRepo

@Inject
lateinit var executor: ShellExecutor
lateinit var settings: SettingsRepo

override fun onReceive(context: Context, intent: Intent) {
if (intent.action != Intent.ACTION_BOOT_COMPLETED) {
return
}

val saturation = sharedPrefsRepo.saturationOverride
val saturation = prefs.saturationOverride
if (saturation != 1.0f) {
executor.setSfSaturation(saturation)
settings.saturation = saturation
}
val vibrationStrength = sharedPrefsRepo.vibrationStrength
val vibrationStrength = prefs.vibrationStrength
if (vibrationStrength != 0) {
executor.setVibrationStrength(vibrationStrength)
settings.vibrationStrength = vibrationStrength
}
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/de/langerhans/odintools/tools/DeviceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import de.langerhans.odintools.tools.DeviceType.*
import javax.inject.Inject

class DeviceUtils @Inject constructor(
private val shellExecutor: ShellExecutor,
private val executor: ShellExecutor,
) {

fun getDeviceVersion() = executor.getStringProperty(SettingsRepo.KEY_BUILD_VERSION, "")

fun getDeviceVendor() = executor.getStringProperty(SettingsRepo.KEY_VENDOR_NAME, "")
miguelguzmanr marked this conversation as resolved.
Show resolved Hide resolved

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

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

enum class DeviceType {
Expand Down
Loading
Loading