fix: address review feedback on the Bluetooth controller

This commit is contained in:
Kurt Himebauch committed 2026-11-02 21:14:55 -05:00
1 parent 6ead4af37e
commit 88a648c273
5 files changed
+69 -34

No files matched your search

@@ -1,6 +1,7 @@
package com.droiddeck.launcher
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
@@ -80,6 +81,7 @@ class BtControllerActivity : ComponentActivity() {
private var exitArmed = false
private var profile by mutableStateOf(BtPadProfile.GENERIC)
private var showTouch by mutableStateOf(true)
private var permissionDenied by mutableStateOf(false)
override fun attachBaseContext(newBase: Context) = super.attachBaseContext(AppLanguage.wrap(newBase))
@@ -95,6 +97,20 @@ class BtControllerActivity : ComponentActivity() {
setContent { DroidDeckTheme { Screen() } }
}
private fun openAppInfo() {
runCatching {
startActivity(Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, android.net.Uri.fromParts("package", packageName, null)))
}
}
override fun onResume() {
super.onResume()
if (permissionDenied && Build.VERSION.SDK_INT >= 31 && NEARBY.all { checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED }) {
permissionDenied = false
startPad()
}
}
private fun startPad() {
BtGamepad.start(this, profile)
BtGamepad.openPairingWindow()
@@ -102,7 +118,12 @@ class BtControllerActivity : ComponentActivity() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_NEARBY) startPad()
if (requestCode != REQUEST_NEARBY) return
val nearbyDenied = permissions.indices.any { i ->
permissions[i] in NEARBY && grantResults.getOrNull(i) != PackageManager.PERMISSION_GRANTED
}
permissionDenied = nearbyDenied
if (!nearbyDenied) startPad()
}
override fun onDestroy() {
@@ -213,7 +234,8 @@ class BtControllerActivity : ComponentActivity() {
Text(stringResource(R.string.btpad_title), fontSize = 20.sp, fontWeight = FontWeight.Black, color = colors.onBackground)
Text(statusText(status), fontSize = 13.sp, color = colors.onSurfaceVariant)
}
if (connected) SecondaryButton(stringResource(R.string.btpad_disconnect), compact = true) { BtGamepad.disconnect() }
if (permissionDenied) PrimaryButton(stringResource(R.string.btpad_permission_open), compact = true) { openAppInfo() }
else if (connected) SecondaryButton(stringResource(R.string.btpad_disconnect), compact = true) { BtGamepad.disconnect() }
else PrimaryButton(stringResource(R.string.btpad_pair), compact = true, enabled = status.phase == BtGamepad.Phase.READY) { BtGamepad.openPairingWindow() }
SecondaryButton(stringResource(R.string.btpad_close), compact = true) { finish() }
}
@@ -229,7 +251,7 @@ class BtControllerActivity : ComponentActivity() {
}
@Composable
private fun statusText(status: BtGamepad.Status): String = when (status.phase) {
private fun statusText(status: BtGamepad.Status): String = if (permissionDenied) stringResource(R.string.btpad_status_permission) else when (status.phase) {
BtGamepad.Phase.OFF, BtGamepad.Phase.STARTING -> stringResource(R.string.btpad_status_starting)
BtGamepad.Phase.BLUETOOTH_OFF -> stringResource(R.string.btpad_status_bt_off)
BtGamepad.Phase.UNSUPPORTED -> stringResource(R.string.btpad_status_unsupported)
@@ -332,15 +354,21 @@ class BtControllerActivity : ComponentActivity() {
dot = last
touchPad(left, last, true)
var upTime = started
while (true) {
val change = awaitPointerEvent().changes.firstOrNull { it.id == first.id } ?: break
if (!change.pressed) { upTime = change.uptimeMillis; break }
change.consume()
val p = norm(change.position)
moved += (p - last).getDistance()
last = p
dot = p
touchPad(left, p, true)
try {
while (true) {
val change = awaitPointerEvent().changes.firstOrNull { it.id == first.id } ?: break
if (!change.pressed) { upTime = change.uptimeMillis; break }
change.consume()
val p = norm(change.position)
moved += (p - last).getDistance()
last = p
dot = p
touchPad(left, p, true)
}
} catch (e: kotlinx.coroutines.CancellationException) {
dot = null
touchPad(left, last, false)
throw e
}
dot = null
if (upTime - started < TAP_MS && moved < 0.15f) clickPad(left, last)
@@ -414,8 +442,11 @@ class BtControllerActivity : ComponentActivity() {
awaitEachGesture {
awaitFirstDown(requireUnconsumed = false)
down = true; touchButton(bit, true)
waitForUpOrCancellation()
down = false; touchButton(bit, false)
try {
waitForUpOrCancellation()
} finally {
down = false; touchButton(bit, false)
}
}
},
) {
@@ -446,14 +477,17 @@ class BtControllerActivity : ComponentActivity() {
touchStick(left, d.x, d.y)
}
move(first.position)
while (true) {
val change = awaitPointerEvent().changes.firstOrNull { it.id == first.id } ?: break
if (!change.pressed) break
change.consume()
move(change.position)
try {
while (true) {
val change = awaitPointerEvent().changes.firstOrNull { it.id == first.id } ?: break
if (!change.pressed) break
change.consume()
move(change.position)
}
} finally {
knob = Offset.Zero
touchStick(left, 0f, 0f)
}
knob = Offset.Zero
touchStick(left, 0f, 0f)
}
},
) {
@@ -75,7 +75,8 @@ object BtGamepad {
h.post {
running = true
if (server != null && p == profile) {
publish(Phase.READY)
val attached = host
if (attached != null) publish(Phase.CONNECTED, attached) else publish(Phase.READY)
startAdvertising()
} else {
close()
@@ -404,19 +405,19 @@ object BtGamepad {
override fun onCharacteristicWriteRequest(device: BluetoothDevice, requestId: Int, chr: BluetoothGattCharacteristic,
preparedWrite: Boolean, responseNeeded: Boolean, offset: Int, value: ByteArray) {
handler?.post {
if (preparedWrite) {
if (device == host && preparedWrite) {
val old = preparedWrites[chr] ?: ByteArray(0)
val merged = old.copyOf(maxOf(old.size, offset + value.size))
value.copyInto(merged, offset)
preparedWrites[chr] = merged
} else written(chr, value)
} else if (device == host) written(chr, value)
if (responseNeeded) server?.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value)
}
}
override fun onExecuteWrite(device: BluetoothDevice, requestId: Int, execute: Boolean) {
handler?.post {
if (execute) preparedWrites.forEach { (chr, v) -> written(chr, v) }
if (execute && device == host) preparedWrites.forEach { (chr, v) -> written(chr, v) }
preparedWrites.clear()
server?.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null)
}
@@ -459,7 +460,7 @@ object BtGamepad {
}
private fun attach(device: BluetoothDevice) {
if (host == device) return
if (host != null) return
host = device
stopAdvertising()
pairingUntil = 0
@@ -134,8 +134,6 @@ abstract class BtPadEncoder {
return this
}
protected fun ByteArray.payload(id: Int): ByteArray =
if (isNotEmpty() && this[0].toInt() and 0xFF == id) copyOfRange(1, size) else this
companion object {
const val INPUT = 1
@@ -222,7 +220,7 @@ private class XboxEncoder : BtPadEncoder() {
override fun output(id: Int, data: ByteArray): BtFeedback? {
if (id != 3) return null
val d = data.payload(id)
val d = data
if (d.size < 5) return null
val enable = d[0].toInt()
val strong = if (enable and 0x02 != 0) d[3].toInt() and 0xFF else 0
@@ -361,7 +359,7 @@ private class DualShock4Encoder : SonyEncoder() {
override fun output(id: Int, data: ByteArray): BtFeedback? {
if (id != 0x11) return null
enhanced = true
val d = data.payload(id)
val d = data
if (d.size < 10) return null
val flags = d[2].toInt()
val light = if (flags and 0x02 != 0) rgb(d[7], d[8], d[9]) else null
@@ -429,7 +427,7 @@ private class DualSenseEncoder : SonyEncoder() {
override fun output(id: Int, data: ByteArray): BtFeedback? {
if (id != 0x31) return null
enhanced = true
val d = data.payload(id)
val d = data
if (d.size < 49) return null
val flags0 = d[2].toInt()
val flags1 = d[3].toInt()
@@ -486,7 +484,7 @@ private class SteamControllerEncoder : BtPadEncoder() {
}
override fun setFeature(id: Int, data: ByteArray) {
val d = data.payload(id)
val d = data
if (id == 1 && d.isNotEmpty()) pending = d[0].toInt() and 0xFF
}
@@ -508,7 +506,7 @@ private class SteamControllerEncoder : BtPadEncoder() {
}
override fun output(id: Int, data: ByteArray): BtFeedback? {
val d = data.payload(id)
val d = data
return when (id) {
0x80 -> if (d.size >= 8) {
val left = (d[3].toInt() and 0xFF) or ((d[4].toInt() and 0xFF) shl 8)
@@ -157,7 +157,7 @@ internal fun SideRail(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier.width(if (iconOnly) 64.dp else 92.dp).background(colors.surface).padding(vertical = if (compact) 8.dp else 12.dp),
) {
val count = 6 + (if (s.gameStoresEnabled) 1 else 0) + (if (s.isHomeApp) 1 else 0)
val count = 7 + (if (s.gameStoresEnabled) 1 else 0) + (if (s.isHomeApp) 1 else 0)
BoxWithConstraints(Modifier.weight(1f).fillMaxWidth()) {
val fit = (maxHeight / count - 4.dp).coerceIn(44.dp, if (iconOnly) 48.dp else if (compact) 52.dp else 60.dp)
val shown = Hops.railPreview ?: selected