Use a keypad for wireless debugging pairing codes

This commit is contained in:
Kurt Himebauch committed 2026-12-05 15:34:18 -05:00
1 parent caae385924
commit 617ea5aa58
7 files changed
+190 -24

No files matched your search

+7
View File
@@ -157,6 +157,13 @@ DroidDeck is [GPL-3.0](LICENSE). Steam and Proton belong to Valve Corporation. V
</details>
<details>
<summary><b>Setup and tools</b></summary>
- The wireless debugging pairing keypad was inspired by Ryan Retro’s Handheld Toolkit.
</details>
## Star history
<a href="https://www.star-history.com/#Droid-Deck/DroidDeck&Date">
@@ -4,20 +4,22 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.RemoteInput
import android.app.Service
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.widget.RemoteViews
import com.droiddeck.launcher.FrontEndActivity
import com.droiddeck.launcher.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.util.UUID
class WirelessAdbPairingService : Service() {
override fun attachBaseContext(newBase: android.content.Context) =
@@ -37,6 +39,8 @@ class WirelessAdbPairingService : Service() {
private var discovery: NsdManager.DiscoveryListener? = null
private var pairingHost: String? = null
private var pairingPort: Int? = null
private val keypad = WirelessPairingKeypad()
private var keypadToken = UUID.randomUUID().toString()
@Volatile private var working = false
@Volatile private var limitEnabled = false
@Volatile private var session = 0
@@ -46,13 +50,21 @@ class WirelessAdbPairingService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
ACTION_CODE -> {
val code = RemoteInput.getResultsFromIntent(intent)?.getCharSequence(KEY_CODE)
?.filter(Char::isDigit)?.toString().orEmpty()
onCode(code)
ACTION_KEYPAD -> {
if (intent.data?.host == keypadToken && current.value is Stage.CodeNeeded && !working) {
val key = intent.data?.lastPathSegment?.toIntOrNull()
if (key == WirelessPairingKeypad.SUBMIT) {
if (keypad.ready && pairingPort != null) onCode(keypad.digits)
} else if (key != null && keypad.press(key)) {
show(current.value, quietUpdate = true)
}
} else if (current.value == Stage.Idle) {
stopSelf(startId)
}
}
ACTION_CANCEL -> finish(Stage.Idle)
else -> begin(intent?.getBooleanExtra(EXTRA_ENABLE_LIMIT, false) == true)
null -> begin(intent?.getBooleanExtra(EXTRA_ENABLE_LIMIT, false) == true)
else -> if (discovery == null && !working) stopSelf(startId)
}
return START_NOT_STICKY
}
@@ -60,6 +72,7 @@ class WirelessAdbPairingService : Service() {
private fun begin(enableLimit: Boolean) {
if (working) return
session++
resetKeypad()
limitEnabled = enableLimit
pairingHost = null
pairingPort = null
@@ -101,9 +114,13 @@ class WirelessAdbPairingService : Service() {
if (!WirelessAdbFix.isLocalAddress(address)) return
main.post {
if (owner != session || working || current.value == Stage.Done) return@post
val endpointChanged = pairingHost != address.hostAddress || pairingPort != serviceInfo.port
pairingHost = address.hostAddress
pairingPort = serviceInfo.port
if (current.value !is Stage.CodeNeeded) show(Stage.CodeNeeded())
if (endpointChanged) {
resetKeypad()
show(Stage.CodeNeeded())
}
}
}
}
@@ -117,11 +134,12 @@ class WirelessAdbPairingService : Service() {
show(Stage.Waiting)
return
}
if (code.length != 6) {
if (code.length != WirelessPairingKeypad.LENGTH) {
show(Stage.CodeNeeded(getString(R.string.adbpair_six_digits)))
return
}
working = true
resetKeypad()
val owner = session
val enable = limitEnabled
show(Stage.Working(getString(R.string.adbpair_pairing)))
@@ -155,13 +173,19 @@ class WirelessAdbPairingService : Service() {
}, "wireless-adb-pairing").start()
}
private fun show(stage: Stage) {
private fun show(stage: Stage, quietUpdate: Boolean = false) {
publish(stage)
getSystemService(NotificationManager::class.java)?.notify(NOTIFICATION_ID, notification(stage))
getSystemService(NotificationManager::class.java)?.notify(NOTIFICATION_ID, notification(stage, quietUpdate))
}
private fun resetKeypad() {
keypad.clear()
keypadToken = UUID.randomUUID().toString()
}
private fun finish(stage: Stage) {
session++
resetKeypad()
main.removeCallbacks(timeout)
stopDiscovery()
publish(stage)
@@ -189,7 +213,7 @@ class WirelessAdbPairingService : Service() {
super.onDestroy()
}
private fun notification(stage: Stage): Notification {
private fun notification(stage: Stage, quietUpdate: Boolean = false): Notification {
val manager = getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(NotificationChannel(CHANNEL_ID, getString(R.string.adbpair_channel_name),
NotificationManager.IMPORTANCE_HIGH).apply {
@@ -220,18 +244,19 @@ class WirelessAdbPairingService : Service() {
.setOnlyAlertOnce(true)
.addAction(Notification.Action.Builder(null, getString(R.string.common_cancel), cancel).build())
is Stage.CodeNeeded -> {
val reply = PendingIntent.getService(this, 2,
Intent(this, WirelessAdbPairingService::class.java).setAction(ACTION_CODE),
if (Build.VERSION.SDK_INT >= 31) PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
else PendingIntent.FLAG_UPDATE_CURRENT)
val input = RemoteInput.Builder(KEY_CODE).setLabel(getString(R.string.adbpair_code_label)).build()
val text = stage.error ?: getString(R.string.adbpair_code_hint)
val keys = pairingKeypad()
builder
.setContentTitle(getString(if (stage.error == null) R.string.adbpair_title_code else R.string.adbpair_title_retry))
.setContentText(text)
.setStyle(Notification.BigTextStyle().bigText(text))
.setContentIntent(null)
.setStyle(Notification.DecoratedCustomViewStyle())
.setCustomBigContentView(keys)
.setCustomHeadsUpContentView(keys)
.setVisibility(Notification.VISIBILITY_SECRET)
.setLocalOnly(true)
.setOngoing(true)
.addAction(Notification.Action.Builder(null, getString(R.string.adbpair_enter_code), reply).addRemoteInput(input).build())
.setOnlyAlertOnce(quietUpdate)
.addAction(Notification.Action.Builder(null, getString(R.string.common_cancel), cancel).build())
}
is Stage.Working -> builder
@@ -254,14 +279,30 @@ class WirelessAdbPairingService : Service() {
return builder.build()
}
private fun pairingKeypad(): RemoteViews = RemoteViews(packageName, R.layout.wireless_pairing_keypad).apply {
val ids = intArrayOf(R.id.pair_key_0, R.id.pair_key_1, R.id.pair_key_2, R.id.pair_key_3, R.id.pair_key_4,
R.id.pair_key_5, R.id.pair_key_6, R.id.pair_key_7, R.id.pair_key_8, R.id.pair_key_9,
R.id.pair_key_delete, R.id.pair_key_submit)
ids.forEachIndexed { key, id ->
val intent = Intent(this@WirelessAdbPairingService, WirelessAdbPairingService::class.java)
.setAction(ACTION_KEYPAD)
.setData(Uri.Builder().scheme("droiddeck-pairing").authority(keypadToken).appendPath(key.toString()).build())
val press = PendingIntent.getService(this@WirelessAdbPairingService, key, intent, PendingIntent.FLAG_IMMUTABLE)
setOnClickPendingIntent(id, press)
}
setTextViewText(R.id.pair_key_code, keypad.display)
setContentDescription(R.id.pair_key_code, getString(R.string.adbpair_keypad_entered, keypad.digits.length))
setBoolean(R.id.pair_key_delete, "setEnabled", keypad.digits.isNotEmpty())
setBoolean(R.id.pair_key_submit, "setEnabled", keypad.ready && pairingPort != null)
}
companion object {
private const val CHANNEL_ID = "wireless-adb-pairing"
private const val PROGRESS_CHANNEL_ID = "wireless-adb-pairing-progress"
private const val NOTIFICATION_ID = 3
private const val PAIRING_TYPE = "_adb-tls-pairing._tcp"
private const val ACTION_CODE = "com.droiddeck.launcher.action.WIRELESS_ADB_CODE"
private const val ACTION_KEYPAD = "com.droiddeck.launcher.action.WIRELESS_ADB_KEYPAD"
private const val ACTION_CANCEL = "com.droiddeck.launcher.action.WIRELESS_ADB_CANCEL"
private const val KEY_CODE = "code"
private const val EXTRA_ENABLE_LIMIT = "enable_limit"
private const val TIMEOUT_MS = 5 * 60_000L
@@ -288,7 +329,7 @@ class WirelessAdbPairingService : Service() {
fun notificationsEnabled(context: Context): Boolean =
context.getSystemService(NotificationManager::class.java)?.areNotificationsEnabled() != false && !channelBlocked(context)
/** The code is typed into a notification on this channel; a channel the user switched off leaves pairing with nothing to type into. */
/** Pairing needs this notification channel to show the keypad over Android Settings. */
fun channelBlocked(context: Context): Boolean =
context.getSystemService(NotificationManager::class.java)?.getNotificationChannel(CHANNEL_ID)?.importance == NotificationManager.IMPORTANCE_NONE
@@ -0,0 +1,29 @@
package com.droiddeck.launcher.core
internal class WirelessPairingKeypad {
var digits = ""
private set
val ready: Boolean get() = digits.length == LENGTH
val display: String get() = digits.padEnd(LENGTH, '–')
fun press(key: Int): Boolean {
val next = when {
key == DELETE -> digits.dropLast(1)
key in 0..9 && !ready -> digits + key
else -> digits
}
if (next == digits) return false
digits = next
return true
}
fun clear() {
digits = ""
}
companion object {
const val LENGTH = 6
const val DELETE = 10
const val SUBMIT = 11
}
}
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center_vertical" android:layoutDirection="ltr">
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_1" android:text="1" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_2" android:text="2" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_3" android:text="3" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_7" android:text="7" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_8" android:text="8" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_9" android:text="9" />
</LinearLayout>
</LinearLayout>
<TextView android:id="@+id/pair_key_code"
android:layout_width="64dp" android:layout_height="wrap_content"
android:gravity="center" android:maxLines="1" android:fontFamily="monospace"
android:textAppearance="@android:style/TextAppearance.Material.Notification.Title"
android:textSize="14sp" />
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_4" android:text="4" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_5" android:text="5" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_6" android:text="6" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_0" android:text="0" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_delete" android:text="⌫" android:contentDescription="@string/adbpair_keypad_delete" />
<Button style="@style/DroidDeckPairKey" android:id="@+id/pair_key_submit" android:text="✓" android:contentDescription="@string/agent_action_pair" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DroidDeckPairKey" parent="@android:style/Widget.Material.Button.Borderless">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:layout_weight">1</item>
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
<item name="android:padding">0dp</item>
<item name="android:textSize">16sp</item>
</style>
</resources>
+5 -3
View File
@@ -1468,7 +1468,7 @@
<string name="gate_needs_wifi">Wireless debugging needs Wi-Fi. Any network works; it doesn\'t need internet.</string>
<string name="gate_step1">Open Settings → Developer options → Wireless debugging and turn it on</string>
<string name="gate_step2">Tap “Pair device with pairing code”</string>
<string name="gate_step3">Type the code into the DroidDeck notification</string>
<string name="gate_step3">Enter the code on the DroidDeck notification keypad</string>
<string name="pairguide_build_number">In About phone, tap Build number 7 times</string>
<string name="pairguide_wireless">Turn on Wireless debugging</string>
<string name="pairguide_pair">Open Wireless debugging, then tap “Pair device with pairing code”</string>
@@ -2639,10 +2639,12 @@
<string name="adbpair_waiting_text">Turn on Wireless debugging, then tap “Pair device with pairing code”.</string>
<string name="adbpair_waiting_long">Turn on Wireless debugging, then tap “Pair device with pairing code”. Stay in Settings: the code goes in this notification.</string>
<string name="adbpair_code_label">6-digit pairing code</string>
<string name="adbpair_code_hint">Tap Enter code and type the 6 digits shown in Settings.</string>
<string name="adbpair_code_hint">Expand if needed. Enter the 6 digits on the keypad, then tap Pair.</string>
<string name="adbpair_keypad_entered">%1$d of 6 digits entered</string>
<string name="adbpair_keypad_delete">Delete last digit</string>
<string name="adbpair_title_code">Enter the Wi-Fi pairing code</string>
<string name="adbpair_title_retry">Try the code again</string>
<string name="adbpair_enter_code">Enter code</string>
<string name="adbpair_enter_code">Use the notification keypad</string>
<string name="adbpair_title_working">Setting up Steam</string>
<string name="adbpair_title_done">Steam is ready</string>
<string name="adbpair_done_text">Tap to go back to DroidDeck. You can turn Wireless debugging off now.</string>
@@ -0,0 +1,38 @@
package com.droiddeck.launcher.core
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class WirelessPairingKeypadTest {
@Test fun sixDigitsKeepLeadingZeroesAndRejectExtraKeys() {
val pad = WirelessPairingKeypad()
assertFalse(pad.ready)
listOf(0, 0, 1, 2, 3, 4).forEach { assertTrue(pad.press(it)) }
assertTrue(pad.ready)
assertFalse(pad.press(5))
assertFalse(pad.press(WirelessPairingKeypad.SUBMIT))
assertEquals("001234", pad.digits)
}
@Test fun deleteLetsYouCorrectTheLastDigit() {
val pad = WirelessPairingKeypad()
assertFalse(pad.press(WirelessPairingKeypad.DELETE))
"321987".forEach { pad.press(it.digitToInt()) }
assertTrue(pad.press(WirelessPairingKeypad.DELETE))
assertFalse(pad.ready)
assertEquals("32198–", pad.display)
assertTrue(pad.press(6))
assertEquals("321986", pad.digits)
}
@Test fun startingAnotherAttemptClearsTheCode() {
val pad = WirelessPairingKeypad()
assertFalse(pad.press(-1))
pad.press(7)
pad.clear()
assertEquals("", pad.digits)
assertEquals("––––––", pad.display)
}
}