From 617ea5aa5839866cd6f8f0453d54f7db8b0d94bd Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 5 Dec 2026 15:34:18 -0500 Subject: [PATCH] Use a keypad for wireless debugging pairing codes --- README.md | 7 ++ .../core/WirelessAdbPairingService.kt | 83 ++++++++++++++----- .../launcher/core/WirelessPairingKeypad.kt | 29 +++++++ .../res/layout/wireless_pairing_keypad.xml | 35 ++++++++ .../main/res/values/pairing_keypad_styles.xml | 14 ++++ app/src/main/res/values/strings.xml | 8 +- .../core/WirelessPairingKeypadTest.kt | 38 +++++++++ 7 files changed, 190 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/core/WirelessPairingKeypad.kt create mode 100644 app/src/main/res/layout/wireless_pairing_keypad.xml create mode 100644 app/src/main/res/values/pairing_keypad_styles.xml create mode 100644 app/src/test/java/com/droiddeck/launcher/core/WirelessPairingKeypadTest.kt diff --git a/README.md b/README.md index 021fed17b3..d6616b1ab7 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,13 @@ DroidDeck is [GPL-3.0](LICENSE). Steam and Proton belong to Valve Corporation. V +
+Setup and tools + +- The wireless debugging pairing keypad was inspired by Ryan Retro’s Handheld Toolkit. + +
+ ## Star history diff --git a/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt b/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt index f7097dce7c..a4492f24d3 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt @@ -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 diff --git a/app/src/main/java/com/droiddeck/launcher/core/WirelessPairingKeypad.kt b/app/src/main/java/com/droiddeck/launcher/core/WirelessPairingKeypad.kt new file mode 100644 index 0000000000..942911a78d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/WirelessPairingKeypad.kt @@ -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 + } +} diff --git a/app/src/main/res/layout/wireless_pairing_keypad.xml b/app/src/main/res/layout/wireless_pairing_keypad.xml new file mode 100644 index 0000000000..8ca29fea9e --- /dev/null +++ b/app/src/main/res/layout/wireless_pairing_keypad.xml @@ -0,0 +1,35 @@ + + + + +