diff --git a/app/src/main/java/de/langerhans/odintools/data/SharedPrefsRepo.kt b/app/src/main/java/de/langerhans/odintools/data/SharedPrefsRepo.kt index 891cd47..6919d59 100644 --- a/app/src/main/java/de/langerhans/odintools/data/SharedPrefsRepo.kt +++ b/app/src/main/java/de/langerhans/odintools/data/SharedPrefsRepo.kt @@ -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) @@ -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" } } \ No newline at end of file diff --git a/app/src/main/java/de/langerhans/odintools/main/MainActivity.kt b/app/src/main/java/de/langerhans/odintools/main/MainActivity.kt index 4edd033..9f85bf5 100644 --- a/app/src/main/java/de/langerhans/odintools/main/MainActivity.kt +++ b/app/src/main/java/de/langerhans/odintools/main/MainActivity.kt @@ -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, diff --git a/app/src/main/java/de/langerhans/odintools/main/MainUiModel.kt b/app/src/main/java/de/langerhans/odintools/main/MainUiModel.kt index 36e8615..0f7f8af 100644 --- a/app/src/main/java/de/langerhans/odintools/main/MainUiModel.kt +++ b/app/src/main/java/de/langerhans/odintools/main/MainUiModel.kt @@ -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, diff --git a/app/src/main/java/de/langerhans/odintools/main/MainViewModel.kt b/app/src/main/java/de/langerhans/odintools/main/MainViewModel.kt index 525dcf8..5a95568 100644 --- a/app/src/main/java/de/langerhans/odintools/main/MainViewModel.kt +++ b/app/src/main/java/de/langerhans/odintools/main/MainViewModel.kt @@ -45,7 +45,8 @@ class MainViewModel @Inject constructor( deviceVersion = deviceUtils.getDeviceVersion(), showNotAnOdinDialog = !isOdin2, singleHomeEnabled = !preventHomePressSetting, - showPServerNotAvailableDialog = !deviceUtils.isPServerAvailable() + showPServerNotAvailableDialog = !deviceUtils.isPServerAvailable(), + overrideDelayEnabled = prefs.overrideDelay ) } } @@ -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) + } + } } \ No newline at end of file diff --git a/app/src/main/java/de/langerhans/odintools/service/ForegroundAppWatcherService.kt b/app/src/main/java/de/langerhans/odintools/service/ForegroundAppWatcherService.kt index 1a73dfd..983bd80 100644 --- a/app/src/main/java/de/langerhans/odintools/service/ForegroundAppWatcherService.kt +++ b/app/src/main/java/de/langerhans/odintools/service/ForegroundAppWatcherService.kt @@ -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 @@ -42,6 +41,7 @@ class ForegroundAppWatcherService @Inject constructor(): AccessibilityService() private var currentForegroundPackage: CharSequence = "" private lateinit var overrides: List private var overridesEnabled = true + private var overridesDelay = false private var hasSetOverride = false private var savedControllerStyle: ControllerStyle? = null @@ -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 { @@ -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() @@ -180,5 +190,7 @@ class ForegroundAppWatcherService @Inject constructor(): AccessibilityService() "com.android.systemui", "android" ) + + const val OVERRIDE_DELAY = 500L } } \ No newline at end of file diff --git a/app/src/main/res/drawable-night/ic_more_time.xml b/app/src/main/res/drawable-night/ic_more_time.xml new file mode 100644 index 0000000..7d150d0 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_more_time.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_more_time.xml b/app/src/main/res/drawable/ic_more_time.xml new file mode 100644 index 0000000..720de56 --- /dev/null +++ b/app/src/main/res/drawable/ic_more_time.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b630b8c..d407712 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -52,4 +52,8 @@ No change You already added overrides for all your apps Fan mode is required when setting a Performance mode + Override delay + Delay applying overrides by 0.5s. This can help if apps crash when applying an + override. + \ No newline at end of file