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 setting to delay applying an override #11

Merged
merged 1 commit into from
Feb 1, 2024
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
14 changes: 12 additions & 2 deletions app/src/main/java/de/langerhans/odintools/data/SharedPrefsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ class SharedPrefsRepo @Inject constructor(
get() = prefs.getBoolean(KEY_APP_OVERRIDE_ENABLED, true)
set(value) = prefs.edit().putBoolean(KEY_APP_OVERRIDE_ENABLED, value).apply()

var overrideDelay
get() = prefs.getBoolean(KEY_OVERRIDE_DELAY, false)
set(value) = prefs.edit().putBoolean(KEY_OVERRIDE_DELAY, value).apply()

private var appOverrideEnabledListener: OnSharedPreferenceChangeListener? = null

fun observeAppOverrideEnabledState(onChange: (newState: Boolean) -> Unit) {
fun observeAppOverrideEnabledState(
overridesEnabled: (newState: Boolean) -> Unit,
overrideDelayEnabled: (newState: Boolean) -> Unit
) {
appOverrideEnabledListener = OnSharedPreferenceChangeListener { _, key ->
if (key == KEY_APP_OVERRIDE_ENABLED) {
onChange(appOverridesEnabled)
overridesEnabled(appOverridesEnabled)
} else if (key == KEY_OVERRIDE_DELAY) {
overrideDelayEnabled(overrideDelay)
}
}
prefs.registerOnSharedPreferenceChangeListener(appOverrideEnabledListener)
Expand All @@ -50,5 +59,6 @@ class SharedPrefsRepo @Inject constructor(
private const val KEY_DISABLED_L2R2_STYLE = "disabled_l2r2_style"
private const val KEY_SATURATION_OVERRIDE = "saturation_override"
private const val KEY_APP_OVERRIDE_ENABLED = "app_override_enabled"
private const val KEY_OVERRIDE_DELAY = "override_delay"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ fun SettingsScreen(
) { newValue ->
viewModel.appOverridesEnabled(newValue)
}
SwitchPreference(
icon = R.drawable.ic_more_time,
title = R.string.overrideDelay,
description = R.string.overrideDelayDesc,
state = uiState.overrideDelayEnabled
) {
viewModel.overrideDelayEnabled(it)
}
SettingsHeader(R.string.quicksettings)
TriggerPreference(
icon = R.drawable.ic_controllerstyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class MainUiModel(

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

val showControllerStyleDialog: Boolean = false,
val showL2r2StyleDialog: Boolean = false,
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/de/langerhans/odintools/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class MainViewModel @Inject constructor(
deviceVersion = deviceUtils.getDeviceVersion(),
showNotAnOdinDialog = !isOdin2,
singleHomeEnabled = !preventHomePressSetting,
showPServerNotAvailableDialog = !deviceUtils.isPServerAvailable()
showPServerNotAvailableDialog = !deviceUtils.isPServerAvailable(),
overrideDelayEnabled = prefs.overrideDelay
)
}
}
Expand Down Expand Up @@ -143,4 +144,11 @@ class MainViewModel @Inject constructor(
it.copy(appOverridesEnabled = newValue)
}
}

fun overrideDelayEnabled(newValue: Boolean) {
prefs.overrideDelay = newValue
_uiState.update {
it.copy(overrideDelayEnabled = newValue)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import de.langerhans.odintools.data.SharedPrefsRepo
import de.langerhans.odintools.models.ControllerStyle
import de.langerhans.odintools.models.ControllerStyle.Unknown
import de.langerhans.odintools.models.FanMode
import de.langerhans.odintools.models.FanMode.Companion.getDisabledFanModes
import de.langerhans.odintools.models.L2R2Style
import de.langerhans.odintools.models.PerfMode
import de.langerhans.odintools.tools.ShellExecutor
Expand Down Expand Up @@ -42,6 +41,7 @@ class ForegroundAppWatcherService @Inject constructor(): AccessibilityService()
private var currentForegroundPackage: CharSequence = ""
private lateinit var overrides: List<AppOverrideEntity>
private var overridesEnabled = true
private var overridesDelay = false

private var hasSetOverride = false
private var savedControllerStyle: ControllerStyle? = null
Expand All @@ -61,8 +61,15 @@ class ForegroundAppWatcherService @Inject constructor(): AccessibilityService()
override fun onAccessibilityEvent(event: AccessibilityEvent) {
if (shouldIgnore(event)) return

currentForegroundPackage = event.packageName
if (overridesDelay) {
Handler(Looper.getMainLooper()).postDelayed({ handleEvent(event)}, OVERRIDE_DELAY)
} else {
handleEvent(event)
}
}

private fun handleEvent(event: AccessibilityEvent) {
currentForegroundPackage = event.packageName
overrides.find { it.packageName == currentForegroundPackage }?.let { override ->
applyOverride(override)
} ?: run {
Expand Down Expand Up @@ -155,7 +162,10 @@ class ForegroundAppWatcherService @Inject constructor(): AccessibilityService()
)

overridesEnabled = sharedPrefsRepo.appOverridesEnabled
sharedPrefsRepo.observeAppOverrideEnabledState { overridesEnabled = it }
sharedPrefsRepo.observeAppOverrideEnabledState(
{ overridesEnabled = it },
{ overridesDelay = it }
)

scope.launch {
appOverrideDao.getAll()
Expand All @@ -180,5 +190,7 @@ class ForegroundAppWatcherService @Inject constructor(): AccessibilityService()
"com.android.systemui",
"android"
)

const val OVERRIDE_DELAY = 500L
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-night/ic_more_time.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M440,840Q365,840 299.5,812Q234,784 185,735Q136,686 108,620.5Q80,555 80,480Q80,405 108,339.5Q136,274 185,225Q234,176 299.5,148Q365,120 440,120Q461,120 480.5,122.5Q500,125 520,130L520,212Q500,206 480.5,203Q461,200 440,200Q322,200 241,281Q160,362 160,480Q160,598 241,679Q322,760 440,760Q558,760 639,679Q720,598 720,480Q720,469 719,460Q718,451 716,440L798,440Q800,451 800,460Q800,469 800,480Q800,555 772,620.5Q744,686 695,735Q646,784 580.5,812Q515,840 440,840ZM552,648L400,496L400,280L480,280L480,464L608,592L552,648ZM720,360L720,240L600,240L600,160L720,160L720,40L800,40L800,160L920,160L920,240L800,240L800,360L720,360Z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_more_time.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/black"
android:pathData="M440,840Q365,840 299.5,812Q234,784 185,735Q136,686 108,620.5Q80,555 80,480Q80,405 108,339.5Q136,274 185,225Q234,176 299.5,148Q365,120 440,120Q461,120 480.5,122.5Q500,125 520,130L520,212Q500,206 480.5,203Q461,200 440,200Q322,200 241,281Q160,362 160,480Q160,598 241,679Q322,760 440,760Q558,760 639,679Q720,598 720,480Q720,469 719,460Q718,451 716,440L798,440Q800,451 800,460Q800,469 800,480Q800,555 772,620.5Q744,686 695,735Q646,784 580.5,812Q515,840 440,840ZM552,648L400,496L400,280L480,280L480,464L608,592L552,648ZM720,360L720,240L600,240L600,160L720,160L720,40L800,40L800,160L920,160L920,240L800,240L800,360L720,360Z"/>
</vector>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@
<string name="noChange">No change</string>
<string name="noOverrideCandidates">You already added overrides for all your apps</string>
<string name="fanModeRequired">Fan mode is required when setting a Performance mode</string>
<string name="overrideDelay">Override delay</string>
<string name="overrideDelayDesc">Delay applying overrides by 0.5s. This can help if apps crash when applying an
override.
</string>
</resources>