Add controller and desktop quality-of-life options (#194)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-04 00:56:09 -04:00
1 parent 336b8851f7
commit 6f1fb462ed
22 files changed
+536 -19

No files matched your search

@@ -171,6 +171,70 @@ class OnScreenControlsTest {
}
private fun control(id: String): Any = (field(view, "controls") as List<*>).first { field(it!!, "id") == id }!!
@Test fun keyboardShortcutWorksWithHiddenPadAndButtonsOnly() {
var opened = 0
view = OnScreenControls(context, null, onKeyboard = { opened++ })
view.layout(0, 0, 1200, 800)
view.setQuickHidden(true)
assertNotEquals(0, pixel(90f, 766f))
touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f))
assertEquals(0, opened)
touch(MotionEvent.ACTION_UP, 1 to (90f to 766f))
assertEquals(1, opened)
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 400f)))
view.setButtonsOnly(true)
touch(MotionEvent.ACTION_DOWN, 1 to (600f to 756f))
touch(MotionEvent.ACTION_UP, 1 to (600f to 756f))
assertEquals(2, opened)
assertEquals(-1, field(control("guide"), "pressedBy"))
}
@Test fun cancelledDraggedOrDisabledKeyboardShortcutDoesNotOpen() {
var opened = 0
view = OnScreenControls(context, null, onKeyboard = { opened++ })
view.layout(0, 0, 1200, 800)
touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f))
touch(MotionEvent.ACTION_CANCEL, 1 to (90f to 766f))
touch(MotionEvent.ACTION_UP, 1 to (90f to 766f))
assertEquals(0, opened)
touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f))
touch(MotionEvent.ACTION_MOVE, 1 to (180f to 766f))
touch(MotionEvent.ACTION_UP, 1 to (180f to 766f))
assertEquals(0, opened)
ControllerPrefs.setKeyboardButton(context, false)
view.reload()
view.setQuickHidden(true)
assertEquals(0, pixel(90f, 766f))
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)))
}
@Test fun hidingSystemButtonsRemovesHitTargetsInBothOverlayModes() {
for (buttonsOnly in listOf(false, true)) {
ControllerPrefs.setSteamButton(context, true)
ControllerPrefs.setQamButton(context, true)
view.reload()
view.setButtonsOnly(buttonsOnly)
val guide = control("guide")
val qam = control("qam")
val gx = value(guide, "cx"); val gy = value(guide, "cy")
val qx = value(qam, "cx"); val qy = value(qam, "cy")
assertNotEquals(0, pixel(gx, gy))
assertNotEquals(0, pixel(qx, qy))
touch(MotionEvent.ACTION_DOWN, 1 to (gx to gy))
ControllerPrefs.setSteamButton(context, false)
view.reload()
assertEquals(-1, field(guide, "pressedBy"))
assertEquals(0, pixel(gx, gy))
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (gx to gy)))
assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (qx to qy)))
touch(MotionEvent.ACTION_UP, 1 to (qx to qy))
ControllerPrefs.setQamButton(context, false)
view.reload()
assertEquals(0, pixel(qx, qy))
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (qx to qy)))
}
}
private fun field(owner: Any, name: String): Any = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner)!!
private fun value(owner: Any, name: String) = field(owner, name) as Float
@@ -0,0 +1,83 @@
package com.droiddeck.launcher.session
import android.content.Context
import android.net.LocalServerSocket
import android.net.LocalSocket
import android.os.Build
import android.os.Looper
import android.os.Vibrator
import android.os.VibratorManager
import com.droiddeck.launcher.input.ControllerPrefs
import java.io.IOException
import org.junit.After
import org.junit.Before
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28, 31], manifest = Config.NONE, shadows = [RumbleComponentTest.NoPacketsSocket::class])
class RumbleComponentTest {
private lateinit var context: Context
private lateinit var vibrator: Vibrator
private lateinit var rumble: RumbleComponent
@Before fun startListener() {
context = RuntimeEnvironment.getApplication()
ControllerPrefs.prefs(context).edit().clear().commit()
vibrator = if (Build.VERSION.SDK_INT >= 31) context.getSystemService(VibratorManager::class.java).defaultVibrator
else context.getSystemService(Vibrator::class.java)
shadowOf(vibrator).setHasVibrator(true)
rumble = RumbleComponent().also { it.attach(context); it.start() }
}
@After fun stopListener() { rumble.stop() }
@Test fun disablingCancelsCurrentEffectAndDropsSubsequentEffectsUntilEnabled() {
effect()
assertTrue(shadowOf(vibrator).isVibrating)
ControllerPrefs.setRumble(context, false)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(shadowOf(vibrator).isVibrating)
effect()
assertFalse(shadowOf(vibrator).isVibrating)
ControllerPrefs.setRumble(context, true)
shadowOf(Looper.getMainLooper()).idle()
effect()
assertTrue(shadowOf(vibrator).isVibrating)
rumble.stop()
assertFalse(shadowOf(vibrator).isVibrating)
effect()
assertFalse(shadowOf(vibrator).isVibrating)
}
@Test fun resettingControllerRestoresRumbleInAnExistingSession() {
ControllerPrefs.setRumble(context, false)
shadowOf(Looper.getMainLooper()).idle()
effect()
assertFalse(shadowOf(vibrator).isVibrating)
ControllerPrefs.resetAll(context)
shadowOf(Looper.getMainLooper()).idle()
effect()
assertTrue(shadowOf(vibrator).isVibrating)
}
private fun effect() {
// Inject a decoded force-feedback packet; transport is outside this preference test.
RumbleComponent::class.java.getDeclaredMethod("buzz", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
.apply { isAccessible = true }.invoke(rumble, 65535, 5000)
}
@Implements(LocalServerSocket::class)
class NoPacketsSocket {
@Implementation fun __constructor__(name: String) = Unit
@Implementation fun accept(): LocalSocket = throw IOException("No incoming packets")
@Implementation fun close() = Unit
}
}
@@ -0,0 +1,43 @@
package com.droiddeck.launcher.ui
import android.content.Context
import android.provider.Settings
import com.droiddeck.launcher.session.SessionPrefs
import org.junit.Assert.*
import org.junit.Test
import org.junit.After
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class MotionTest {
@After fun restoreMotion() {
val context = RuntimeEnvironment.getApplication() as Context
SessionPrefs.setAnimationsEnabled(context, true)
Settings.Global.putFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
Motion.refresh(context)
}
@Test fun appToggleAppliesImmediatelyAndCannotOverrideSystemReducedMotion() {
val context = RuntimeEnvironment.getApplication() as Context
context.getSharedPreferences("session", Context.MODE_PRIVATE).edit().clear().commit()
Settings.Global.putFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1.5f)
Motion.refresh(context)
assertEquals(1.5f, Motion.scale, 0f)
SessionPrefs.setAnimationsEnabled(context, false)
Motion.refresh(context)
assertEquals(0f, Motion.scale, 0f)
assertEquals(0, Motion.ms(300))
// The app's toggle never writes Android's preference.
assertEquals(1.5f, Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE), 0f)
SessionPrefs.setAnimationsEnabled(context, true)
Motion.refresh(context)
assertEquals(1.5f, Motion.scale, 0f)
Settings.Global.putFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 0f)
Motion.refresh(context)
assertEquals(0f, Motion.scale, 0f)
}
}