mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
Reuse saved wireless ADB pairing and connection
This commit is contained in:
1 parent
f5f5a8ec3b
commit
e69ed55356
4 files changed
+68
-14
No files matched your search
@@ -18,6 +18,9 @@ object WirelessAdbFix {
|
||||
private const val CERT_FILE = "wireless-adb-cert.pem"
|
||||
private const val KEY_FILE = "wireless-adb-key.pem"
|
||||
private const val HOST_FILE = "wireless-adb-host.txt"
|
||||
private const val CONNECTION_FILE = "wireless-adb-connection.txt"
|
||||
|
||||
private data class SavedConnection(val host: String, val port: Int?)
|
||||
|
||||
@Synchronized
|
||||
private fun loadOrCreateIdentity(context: Context) {
|
||||
@@ -45,6 +48,31 @@ object WirelessAdbFix {
|
||||
loadOrCreateIdentity(context.applicationContext)
|
||||
Kadb.pair(host, port, pairingCode, "DroidDeck")
|
||||
writeAtomically(File(context.noBackupFilesDir, HOST_FILE), host.toByteArray())
|
||||
writeAtomically(File(context.noBackupFilesDir, CONNECTION_FILE), ByteArray(0))
|
||||
}
|
||||
|
||||
private fun savedConnection(context: Context): SavedConnection? {
|
||||
val directory = context.applicationContext.noBackupFilesDir
|
||||
if (!File(directory, CERT_FILE).isFile || !File(directory, KEY_FILE).isFile) return null
|
||||
val host = File(directory, HOST_FILE).takeIf(File::isFile)?.readText()?.trim()
|
||||
?.takeIf(String::isNotEmpty) ?: return null
|
||||
val lines = File(directory, CONNECTION_FILE).takeIf(File::isFile)?.readLines()
|
||||
val port = lines?.takeIf { it.size == 2 && it[0] == host }
|
||||
?.get(1)?.toIntOrNull()?.takeIf { it in 1..65535 }
|
||||
return SavedConnection(host, port)
|
||||
}
|
||||
|
||||
/** A saved pairing skips the PIN; an unknown connection port can be filled in from Settings. */
|
||||
fun savedConnectionAddress(context: Context): String? = savedConnection(context)?.let { saved ->
|
||||
val host = if (':' in saved.host) "[${saved.host}]" else saved.host
|
||||
"$host:${saved.port ?: ""}"
|
||||
}
|
||||
|
||||
private fun saveConnection(context: Context, host: String, port: Int) {
|
||||
writeAtomically(
|
||||
File(context.applicationContext.noBackupFilesDir, CONNECTION_FILE),
|
||||
"$host\n$port".toByteArray(),
|
||||
)
|
||||
}
|
||||
|
||||
/** Finds this paired device's separate TLS connection port from Android's ADB mDNS record. */
|
||||
@@ -128,17 +156,30 @@ object WirelessAdbFix {
|
||||
"Android did not confirm the child-process limit was changed: ${result.allOutput.trim()}"
|
||||
}
|
||||
}
|
||||
saveConnection(context, host, port)
|
||||
}
|
||||
|
||||
/** Tries the app's saved pairing without asking for a new pairing code. */
|
||||
fun setUsingSavedPairing(context: Context, enabled: Boolean) {
|
||||
val appContext = context.applicationContext
|
||||
val hostFile = File(appContext.noBackupFilesDir, HOST_FILE)
|
||||
check(hostFile.isFile) { "No Wireless debugging pairing is saved" }
|
||||
val host = hostFile.readText().trim()
|
||||
check(host.isNotEmpty()) { "No Wireless debugging host is saved" }
|
||||
val port = findConnectPort(appContext, host)
|
||||
check(port != null) { "Wireless debugging is unavailable on this network" }
|
||||
setChildProcessLimit(appContext, host, port, enabled)
|
||||
val saved = savedConnection(appContext)
|
||||
?: error("No Wireless debugging pairing is saved")
|
||||
val cachedPort = saved.port
|
||||
if (cachedPort != null) {
|
||||
val cachedResult = runCatching {
|
||||
setChildProcessLimit(appContext, saved.host, cachedPort, enabled)
|
||||
}
|
||||
if (cachedResult.isSuccess) return
|
||||
val discoveredPort = findConnectPort(appContext, saved.host)
|
||||
if (discoveredPort != null && discoveredPort != cachedPort) {
|
||||
setChildProcessLimit(appContext, saved.host, discoveredPort, enabled)
|
||||
} else {
|
||||
cachedResult.getOrThrow()
|
||||
}
|
||||
} else {
|
||||
val discoveredPort = findConnectPort(appContext, saved.host)
|
||||
?: error("Enter the current Wireless debugging IP address & Port")
|
||||
setChildProcessLimit(appContext, saved.host, discoveredPort, enabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1088,7 +1088,7 @@ private fun SetupPanel(
|
||||
if (error == null) {
|
||||
a.onRefreshPhantomStatus()
|
||||
} else {
|
||||
processLimitMessage = "Wireless ADB is unavailable. Pair this device or use Developer options."
|
||||
processLimitMessage = "Check the Wireless debugging IP address & Port, or pair again if Android removed this device."
|
||||
onRequestWirelessAdb(enabled)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ private fun CompactGateInstructions(
|
||||
)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
PrimaryButton("Open Developer options", compact = true, onClick = onOpenDeveloperOptions)
|
||||
SecondaryButton("Set up on-device ADB", compact = true, onClick = onSetUpWirelessAdb)
|
||||
SecondaryButton("Use on-device ADB", compact = true, onClick = onSetUpWirelessAdb)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,7 @@ private fun GateInstructions(
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
PrimaryButton("Open Developer options", onClick = onOpenDeveloperOptions)
|
||||
SecondaryButton("Set up on-device ADB", onClick = onSetUpWirelessAdb)
|
||||
SecondaryButton("Use on-device ADB", onClick = onSetUpWirelessAdb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import com.droiddeck.launcher.core.PhantomProcessLimit
|
||||
import com.droiddeck.launcher.core.WirelessAdbFix
|
||||
import kotlin.math.min
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@@ -57,9 +58,11 @@ fun WirelessAdbFixPage(
|
||||
onFindConnectPort: (String, (Int?) -> Unit) -> Unit,
|
||||
onApply: (String, Int, Boolean, (String?) -> Unit) -> Unit,
|
||||
) {
|
||||
var step by rememberSaveable { mutableStateOf(0) }
|
||||
val context = LocalContext.current
|
||||
val savedAddress = remember { WirelessAdbFix.savedConnectionAddress(context) }
|
||||
var step by rememberSaveable { mutableStateOf(if (savedAddress == null) 0 else 1) }
|
||||
var pairingAddress by rememberSaveable { mutableStateOf("") }
|
||||
var connectionAddress by rememberSaveable { mutableStateOf("") }
|
||||
var connectionAddress by rememberSaveable { mutableStateOf(savedAddress.orEmpty()) }
|
||||
var pairingCode by rememberSaveable { mutableStateOf("") }
|
||||
var busy by remember { mutableStateOf(false) }
|
||||
var editing by remember { mutableStateOf(false) }
|
||||
@@ -131,6 +134,13 @@ fun WirelessAdbFixPage(
|
||||
}
|
||||
}
|
||||
}
|
||||
val pairAgain: () -> Unit = {
|
||||
step = 0
|
||||
pairingAddress = ""
|
||||
pairingCode = ""
|
||||
message = null
|
||||
messageIsError = false
|
||||
}
|
||||
|
||||
BackHandler { if (!busy) onBack() }
|
||||
|
||||
@@ -156,6 +166,7 @@ fun WirelessAdbFixPage(
|
||||
canPair = pairingEndpoint != null && pairingCode.length == 6,
|
||||
onPair = pairNow,
|
||||
onApply = applyAddress,
|
||||
onPairAgain = pairAgain,
|
||||
)
|
||||
}
|
||||
SettingsPage(
|
||||
@@ -168,10 +179,10 @@ fun WirelessAdbFixPage(
|
||||
eyebrow = "Setup",
|
||||
lede = when {
|
||||
compactSplit && step == 0 -> "If the option is missing, pair over Wireless debugging."
|
||||
compactSplit && step == 1 -> "Use IP address & Port on the main Wireless debugging screen."
|
||||
compactSplit && step == 1 -> "Paired. Check IP address & Port in Wireless debugging."
|
||||
compactSplit -> "The child-process limit was updated."
|
||||
step == 0 -> "Use step 2 if Developer options has no child-process setting. In Android Settings, open Developer options → Wireless debugging → Pair device with pairing code."
|
||||
step == 1 -> "Use IP address & Port on the main Wireless debugging screen. The pairing pop-up uses a different port."
|
||||
step == 1 -> "Paired. Use the address below; update it from Wireless debugging → IP address & Port if needed."
|
||||
else -> "The child-process limit was updated over Wireless debugging."
|
||||
},
|
||||
onBack = { if (!busy) onBack() },
|
||||
@@ -253,6 +264,7 @@ private fun WirelessStepForm(
|
||||
canPair: Boolean,
|
||||
onPair: () -> Unit,
|
||||
onApply: () -> Unit,
|
||||
onPairAgain: () -> Unit,
|
||||
) {
|
||||
if (step == 0) {
|
||||
SettingsGroup("2 · Pair Wireless debugging", compact = compact) {
|
||||
@@ -318,6 +330,7 @@ private fun WirelessStepForm(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
StatusMessage(busy, message, Modifier.weight(1f), isError = messageIsError)
|
||||
SecondaryButton("Pair again", compact = compact, enabled = !busy, onClick = onPairAgain)
|
||||
PrimaryButton(
|
||||
if (desiredEnabled) "Enable limit" else "Disable limit",
|
||||
compact = compact,
|
||||
|
||||
Reference in new issue
Block a user