mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 06:15:58 +00:00
Improve wireless ADB text input
This commit is contained in:
1 parent
63f7554f43
commit
90d7eede2e
1 file changed
+263
-133
@@ -1,39 +1,52 @@
|
|||||||
package com.droiddeck.launcher.ui
|
package com.droiddeck.launcher.ui
|
||||||
|
|
||||||
|
import android.text.Editable
|
||||||
|
import android.text.InputType
|
||||||
|
import android.text.TextWatcher
|
||||||
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.widget.EditText
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.border
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.layout.widthIn
|
import androidx.compose.foundation.layout.widthIn
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberUpdatedState
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusDirection
|
import androidx.compose.ui.focus.FocusDirection
|
||||||
import androidx.compose.ui.focus.FocusManager
|
import androidx.compose.ui.focus.FocusManager
|
||||||
|
import androidx.compose.ui.graphics.toArgb
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalFocusManager
|
import androidx.compose.ui.platform.LocalFocusManager
|
||||||
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
import androidx.compose.ui.text.font.FontFamily
|
import androidx.compose.ui.text.font.FontFamily
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
import androidx.compose.ui.text.input.KeyboardType
|
import androidx.compose.ui.text.input.KeyboardType
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.viewinterop.AndroidView
|
||||||
import com.droiddeck.launcher.core.PhantomProcessLimit
|
import com.droiddeck.launcher.core.PhantomProcessLimit
|
||||||
|
import kotlin.math.min
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun WirelessAdbFixPage(
|
fun WirelessAdbFixPage(
|
||||||
@@ -45,41 +58,48 @@ fun WirelessAdbFixPage(
|
|||||||
onApply: (String, Int, Boolean, (String?) -> Unit) -> Unit,
|
onApply: (String, Int, Boolean, (String?) -> Unit) -> Unit,
|
||||||
) {
|
) {
|
||||||
var step by rememberSaveable { mutableStateOf(0) }
|
var step by rememberSaveable { mutableStateOf(0) }
|
||||||
var host by rememberSaveable { mutableStateOf("") }
|
var pairingAddress by rememberSaveable { mutableStateOf("") }
|
||||||
var pairingPort by rememberSaveable { mutableStateOf("") }
|
var connectionAddress by rememberSaveable { mutableStateOf("") }
|
||||||
var pairingCode by rememberSaveable { mutableStateOf("") }
|
var pairingCode by rememberSaveable { mutableStateOf("") }
|
||||||
var connectPort by rememberSaveable { mutableStateOf("") }
|
|
||||||
var busy by remember { mutableStateOf(false) }
|
var busy by remember { mutableStateOf(false) }
|
||||||
|
var editing by remember { mutableStateOf(false) }
|
||||||
var message by remember { mutableStateOf<String?>(null) }
|
var message by remember { mutableStateOf<String?>(null) }
|
||||||
val focusManager = LocalFocusManager.current
|
val focusManager = LocalFocusManager.current
|
||||||
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
val pairingEndpoint = parseAdbAddress(pairingAddress)
|
||||||
|
|
||||||
val pairNow: () -> Unit = {
|
val pairNow: () -> Unit = {
|
||||||
val pairingHost = host
|
parseAdbAddress(pairingAddress)?.let { endpoint ->
|
||||||
busy = true
|
keyboardController?.hide()
|
||||||
message = null
|
focusManager.clearFocus()
|
||||||
onPair(pairingHost, pairingPort.toInt(), pairingCode) { error ->
|
busy = true
|
||||||
pairingCode = ""
|
editing = false
|
||||||
if (error != null) {
|
message = null
|
||||||
busy = false
|
onPair(endpoint.host, endpoint.port, pairingCode) { error ->
|
||||||
message = error
|
pairingCode = ""
|
||||||
} else {
|
if (error != null) {
|
||||||
step = 1
|
busy = false
|
||||||
busy = true
|
message = error
|
||||||
message = "Paired. Finding the Wireless debugging port…"
|
} else {
|
||||||
onFindConnectPort(pairingHost) { discoveredPort ->
|
step = 1
|
||||||
if (discoveredPort == null) {
|
busy = true
|
||||||
busy = false
|
message = "Paired. Finding the Wireless debugging port…"
|
||||||
message = "Enter the current Wireless debugging port from Settings."
|
onFindConnectPort(endpoint.host) { discoveredPort ->
|
||||||
} else {
|
if (discoveredPort == null) {
|
||||||
connectPort = discoveredPort.toString()
|
connectionAddress = formatAddressHost(endpoint.host) + ":"
|
||||||
message = "Device found. Applying the setting…"
|
|
||||||
onApply(pairingHost, discoveredPort, desiredEnabled) { applyError ->
|
|
||||||
busy = false
|
busy = false
|
||||||
if (applyError == null) {
|
message = "Enter the current Wireless debugging address from Settings."
|
||||||
step = 2
|
} else {
|
||||||
message = null
|
connectionAddress = formatAdbAddress(endpoint.host, discoveredPort)
|
||||||
} else {
|
message = "Device found. Applying the setting…"
|
||||||
message = "Could not apply automatically. Check the port below and retry. $applyError"
|
onApply(endpoint.host, discoveredPort, desiredEnabled) { applyError ->
|
||||||
|
busy = false
|
||||||
|
if (applyError == null) {
|
||||||
|
step = 2
|
||||||
|
message = null
|
||||||
|
} else {
|
||||||
|
message = "Could not apply automatically. Check the address below and retry. $applyError"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,16 +107,21 @@ fun WirelessAdbFixPage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val applyPort: () -> Unit = {
|
val applyAddress: () -> Unit = {
|
||||||
busy = true
|
parseAdbAddress(connectionAddress)?.let { endpoint ->
|
||||||
message = null
|
keyboardController?.hide()
|
||||||
onApply(host, connectPort.toInt(), desiredEnabled) { error ->
|
focusManager.clearFocus()
|
||||||
busy = false
|
busy = true
|
||||||
if (error == null) {
|
editing = false
|
||||||
step = 2
|
message = null
|
||||||
message = null
|
onApply(endpoint.host, endpoint.port, desiredEnabled) { error ->
|
||||||
} else {
|
busy = false
|
||||||
message = error
|
if (error == null) {
|
||||||
|
step = 2
|
||||||
|
message = null
|
||||||
|
} else {
|
||||||
|
message = error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,20 +133,22 @@ fun WirelessAdbFixPage(
|
|||||||
val form: @Composable () -> Unit = {
|
val form: @Composable () -> Unit = {
|
||||||
WirelessStepForm(
|
WirelessStepForm(
|
||||||
step = step,
|
step = step,
|
||||||
host = host,
|
pairingAddress = pairingAddress,
|
||||||
onHostChange = { host = it.trim() },
|
onPairingAddressChange = { pairingAddress = it },
|
||||||
pairingPort = pairingPort,
|
|
||||||
onPairingPortChange = { pairingPort = it.filter(Char::isDigit).take(5) },
|
|
||||||
pairingCode = pairingCode,
|
pairingCode = pairingCode,
|
||||||
onPairingCodeChange = { pairingCode = it.filter(Char::isDigit).take(6) },
|
onPairingCodeChange = { pairingCode = it.filter(Char::isDigit).take(6) },
|
||||||
connectPort = connectPort,
|
connectionAddress = connectionAddress,
|
||||||
onConnectPortChange = { connectPort = it.filter(Char::isDigit).take(5) },
|
onConnectionAddressChange = { connectionAddress = it },
|
||||||
busy = busy,
|
busy = busy,
|
||||||
message = message,
|
message = message,
|
||||||
|
compact = compactSplit,
|
||||||
|
editing = editing,
|
||||||
|
onEditingChanged = { editing = it },
|
||||||
focusManager = focusManager,
|
focusManager = focusManager,
|
||||||
desiredEnabled = desiredEnabled,
|
desiredEnabled = desiredEnabled,
|
||||||
|
canPair = pairingEndpoint != null && pairingCode.length == 6,
|
||||||
onPair = pairNow,
|
onPair = pairNow,
|
||||||
onApply = applyPort,
|
onApply = applyAddress,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
SettingsPage(
|
SettingsPage(
|
||||||
@@ -134,17 +161,18 @@ fun WirelessAdbFixPage(
|
|||||||
eyebrow = "Setup",
|
eyebrow = "Setup",
|
||||||
lede = when {
|
lede = when {
|
||||||
compactSplit && step == 0 -> "If the option is missing, pair over Wireless debugging."
|
compactSplit && step == 0 -> "If the option is missing, pair over Wireless debugging."
|
||||||
compactSplit && step == 1 -> "Enter the Wireless debugging connection port."
|
compactSplit && step == 1 -> "Enter the Wireless debugging connection address."
|
||||||
compactSplit -> "The child-process limit was updated."
|
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 == 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 -> "The pairing port and the Wireless debugging connection port are different."
|
step == 1 -> "The pairing and Wireless debugging connection addresses are different."
|
||||||
else -> "The child-process limit was updated over Wireless debugging."
|
else -> "The child-process limit was updated over Wireless debugging."
|
||||||
},
|
},
|
||||||
onBack = { if (!busy) onBack() },
|
onBack = { if (!busy) onBack() },
|
||||||
action = if (compactSplit && step < 2) {
|
action = if (compactSplit && step < 2) {
|
||||||
{ SecondaryButton("Developer options", enabled = !busy, onClick = onOpenDeveloperOptions) }
|
{ SecondaryButton("Developer options", compact = true, enabled = !busy, onClick = onOpenDeveloperOptions) }
|
||||||
} else null,
|
} else null,
|
||||||
scrollContent = compactSplit,
|
scrollContent = !compactSplit,
|
||||||
|
compactLayout = compactSplit,
|
||||||
) {
|
) {
|
||||||
BoxWithConstraints(Modifier.fillMaxWidth()) {
|
BoxWithConstraints(Modifier.fillMaxWidth()) {
|
||||||
val wide = maxWidth >= 620.dp
|
val wide = maxWidth >= 620.dp
|
||||||
@@ -153,7 +181,7 @@ fun WirelessAdbFixPage(
|
|||||||
modifier = Modifier.widthIn(max = 680.dp).fillMaxWidth(),
|
modifier = Modifier.widthIn(max = 680.dp).fillMaxWidth(),
|
||||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
) {
|
) {
|
||||||
SettingsGroup("Child-process limit") {
|
SettingsGroup("Child-process limit", compact = compactSplit) {
|
||||||
Text(
|
Text(
|
||||||
"Android confirmed the limit is ${if (desiredEnabled) "enabled" else "disabled"}.",
|
"Android confirmed the limit is ${if (desiredEnabled) "enabled" else "disabled"}.",
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||||
@@ -168,7 +196,7 @@ fun WirelessAdbFixPage(
|
|||||||
verticalArrangement = Arrangement.spacedBy(2.dp),
|
verticalArrangement = Arrangement.spacedBy(2.dp),
|
||||||
) {
|
) {
|
||||||
form()
|
form()
|
||||||
CompactComputerFallback(desiredEnabled)
|
if (!editing) CompactComputerFallback(desiredEnabled)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val routes: @Composable () -> Unit = {
|
val routes: @Composable () -> Unit = {
|
||||||
@@ -201,98 +229,91 @@ fun WirelessAdbFixPage(
|
|||||||
@Composable
|
@Composable
|
||||||
private fun WirelessStepForm(
|
private fun WirelessStepForm(
|
||||||
step: Int,
|
step: Int,
|
||||||
host: String,
|
pairingAddress: String,
|
||||||
onHostChange: (String) -> Unit,
|
onPairingAddressChange: (String) -> Unit,
|
||||||
pairingPort: String,
|
|
||||||
onPairingPortChange: (String) -> Unit,
|
|
||||||
pairingCode: String,
|
pairingCode: String,
|
||||||
onPairingCodeChange: (String) -> Unit,
|
onPairingCodeChange: (String) -> Unit,
|
||||||
connectPort: String,
|
connectionAddress: String,
|
||||||
onConnectPortChange: (String) -> Unit,
|
onConnectionAddressChange: (String) -> Unit,
|
||||||
busy: Boolean,
|
busy: Boolean,
|
||||||
message: String?,
|
message: String?,
|
||||||
|
compact: Boolean,
|
||||||
|
editing: Boolean,
|
||||||
|
onEditingChanged: (Boolean) -> Unit,
|
||||||
focusManager: FocusManager,
|
focusManager: FocusManager,
|
||||||
desiredEnabled: Boolean,
|
desiredEnabled: Boolean,
|
||||||
|
canPair: Boolean,
|
||||||
onPair: () -> Unit,
|
onPair: () -> Unit,
|
||||||
onApply: () -> Unit,
|
onApply: () -> Unit,
|
||||||
) {
|
) {
|
||||||
if (step == 0) {
|
if (step == 0) {
|
||||||
SettingsGroup("2 · Pair Wireless debugging") {
|
SettingsGroup("2 · Pair Wireless debugging", compact = compact) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.fillMaxWidth().padding(12.dp),
|
modifier = Modifier.fillMaxWidth().padding(horizontal = if (compact) 8.dp else 12.dp, vertical = if (compact) 6.dp else 12.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
verticalArrangement = Arrangement.spacedBy(if (compact) 5.dp else 6.dp),
|
||||||
) {
|
) {
|
||||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {
|
AdbTextField(
|
||||||
AdbTextField(
|
value = pairingAddress,
|
||||||
value = host,
|
onValueChange = onPairingAddressChange,
|
||||||
onValueChange = onHostChange,
|
label = "Pairing address",
|
||||||
label = "Device IP",
|
placeholder = "192.168.1.42:37123",
|
||||||
keyboardType = KeyboardType.Ascii,
|
keyboardType = KeyboardType.Ascii,
|
||||||
imeAction = ImeAction.Next,
|
imeAction = ImeAction.Next,
|
||||||
onNext = { focusManager.moveFocus(FocusDirection.Next) },
|
compact = compact,
|
||||||
modifier = Modifier.weight(1f),
|
onFocusChange = onEditingChanged,
|
||||||
)
|
onNext = { focusManager.moveFocus(FocusDirection.Next) },
|
||||||
AdbTextField(
|
)
|
||||||
value = pairingPort,
|
Row(
|
||||||
onValueChange = onPairingPortChange,
|
verticalAlignment = Alignment.Bottom,
|
||||||
label = "Pairing port",
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
keyboardType = KeyboardType.Number,
|
modifier = Modifier.fillMaxWidth(),
|
||||||
imeAction = ImeAction.Next,
|
) {
|
||||||
onNext = { focusManager.moveFocus(FocusDirection.Next) },
|
|
||||||
modifier = Modifier.weight(1f),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {
|
|
||||||
AdbTextField(
|
AdbTextField(
|
||||||
value = pairingCode,
|
value = pairingCode,
|
||||||
onValueChange = onPairingCodeChange,
|
onValueChange = onPairingCodeChange,
|
||||||
label = "Pairing code",
|
label = "Pairing code (PIN)",
|
||||||
placeholder = "6 digits",
|
placeholder = "6 digits",
|
||||||
keyboardType = KeyboardType.Number,
|
keyboardType = KeyboardType.Number,
|
||||||
imeAction = ImeAction.Done,
|
imeAction = ImeAction.Done,
|
||||||
onDone = focusManager::clearFocus,
|
compact = compact,
|
||||||
|
onFocusChange = onEditingChanged,
|
||||||
|
onDone = {
|
||||||
|
focusManager.clearFocus()
|
||||||
|
},
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
)
|
)
|
||||||
PrimaryButton(
|
PrimaryButton("Pair", compact = compact, enabled = !busy && canPair, onClick = onPair)
|
||||||
"Pair",
|
|
||||||
enabled = !busy && host.isNotBlank() && validPort(pairingPort) && pairingCode.length == 6,
|
|
||||||
onClick = onPair,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
StatusMessage(busy, message)
|
StatusMessage(busy, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
SettingsGroup("2 · Apply over Wireless debugging") {
|
SettingsGroup("2 · Apply over Wireless debugging", compact = compact) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.fillMaxWidth().padding(12.dp),
|
modifier = Modifier.fillMaxWidth().padding(horizontal = if (compact) 8.dp else 12.dp, vertical = if (compact) 6.dp else 12.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
verticalArrangement = Arrangement.spacedBy(if (compact) 5.dp else 6.dp),
|
||||||
) {
|
) {
|
||||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {
|
AdbTextField(
|
||||||
AdbTextField(
|
value = connectionAddress,
|
||||||
value = host,
|
onValueChange = onConnectionAddressChange,
|
||||||
onValueChange = onHostChange,
|
label = "Wireless debugging address",
|
||||||
label = "Device IP",
|
placeholder = "192.168.1.42:45678",
|
||||||
keyboardType = KeyboardType.Ascii,
|
keyboardType = KeyboardType.Ascii,
|
||||||
imeAction = ImeAction.Next,
|
imeAction = ImeAction.Done,
|
||||||
onNext = { focusManager.moveFocus(FocusDirection.Next) },
|
compact = compact,
|
||||||
modifier = Modifier.weight(1f),
|
onFocusChange = onEditingChanged,
|
||||||
)
|
onDone = { focusManager.clearFocus() },
|
||||||
AdbTextField(
|
)
|
||||||
value = connectPort,
|
Row(
|
||||||
onValueChange = onConnectPortChange,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
label = "Connection port",
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
keyboardType = KeyboardType.Number,
|
modifier = Modifier.fillMaxWidth(),
|
||||||
imeAction = ImeAction.Done,
|
) {
|
||||||
onDone = focusManager::clearFocus,
|
|
||||||
modifier = Modifier.weight(1f),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {
|
|
||||||
StatusMessage(busy, message, Modifier.weight(1f))
|
StatusMessage(busy, message, Modifier.weight(1f))
|
||||||
PrimaryButton(
|
PrimaryButton(
|
||||||
if (desiredEnabled) "Enable child-process limit" else "Disable child-process limit",
|
if (desiredEnabled) "Enable limit" else "Disable limit",
|
||||||
enabled = !busy && validPort(connectPort),
|
compact = compact,
|
||||||
|
enabled = !busy && parseAdbAddress(connectionAddress) != null,
|
||||||
onClick = onApply,
|
onClick = onApply,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -310,22 +331,99 @@ private fun AdbTextField(
|
|||||||
imeAction: ImeAction,
|
imeAction: ImeAction,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
placeholder: String? = null,
|
placeholder: String? = null,
|
||||||
|
compact: Boolean = false,
|
||||||
|
onFocusChange: (Boolean) -> Unit = {},
|
||||||
onNext: (() -> Unit)? = null,
|
onNext: (() -> Unit)? = null,
|
||||||
onDone: (() -> Unit)? = null,
|
onDone: (() -> Unit)? = null,
|
||||||
) {
|
) {
|
||||||
OutlinedTextField(
|
val context = LocalContext.current
|
||||||
value = value,
|
val palette = LocalPalette.current
|
||||||
onValueChange = onValueChange,
|
val onValueChangeLatest = rememberUpdatedState(onValueChange)
|
||||||
label = { Text(label) },
|
val onNextLatest = rememberUpdatedState(onNext)
|
||||||
placeholder = placeholder?.let { { Text(it) } },
|
val onDoneLatest = rememberUpdatedState(onDone)
|
||||||
singleLine = true,
|
val onFocusChangeLatest = rememberUpdatedState(onFocusChange)
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = keyboardType, imeAction = imeAction),
|
var focused by remember { mutableStateOf(false) }
|
||||||
keyboardActions = KeyboardActions(
|
val shape = RoundedCornerShape(8.dp)
|
||||||
onNext = { onNext?.invoke() },
|
val inputHeight = if (compact) 44.dp else 52.dp
|
||||||
onDone = { onDone?.invoke() },
|
val colors = MaterialTheme.colorScheme
|
||||||
),
|
|
||||||
modifier = modifier.fillMaxWidth().height(56.dp),
|
Column(modifier = modifier.fillMaxWidth()) {
|
||||||
)
|
Text(
|
||||||
|
label,
|
||||||
|
style = MaterialTheme.typography.labelSmall,
|
||||||
|
fontWeight = FontWeight.Medium,
|
||||||
|
color = colors.onSurfaceVariant,
|
||||||
|
maxLines = 1,
|
||||||
|
)
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.height(inputHeight)
|
||||||
|
.background(colors.surfaceVariant, shape)
|
||||||
|
.border(1.dp, if (focused) palette.signal else palette.line2, shape)
|
||||||
|
.padding(horizontal = 10.dp),
|
||||||
|
contentAlignment = Alignment.CenterStart,
|
||||||
|
) {
|
||||||
|
AndroidView(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
factory = { viewContext ->
|
||||||
|
EditText(viewContext).apply {
|
||||||
|
isSingleLine = true
|
||||||
|
setHorizontallyScrolling(true)
|
||||||
|
background = null
|
||||||
|
setPadding((2 * context.resources.displayMetrics.density).roundToInt(), 0, 0, 0)
|
||||||
|
setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, if (compact) 13f else 14f)
|
||||||
|
setTextColor(colors.onBackground.toArgb())
|
||||||
|
setHintTextColor(colors.onSurfaceVariant.toArgb())
|
||||||
|
contentDescription = label
|
||||||
|
inputType = if (keyboardType == KeyboardType.Number) {
|
||||||
|
InputType.TYPE_CLASS_NUMBER
|
||||||
|
} else {
|
||||||
|
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
|
||||||
|
}
|
||||||
|
imeOptions = imeActionFlag(imeAction) or EditorInfo.IME_FLAG_NO_FULLSCREEN or EditorInfo.IME_FLAG_NO_EXTRACT_UI
|
||||||
|
hint = placeholder
|
||||||
|
setOnEditorActionListener { _, actionId, event ->
|
||||||
|
val isEnter = event?.keyCode == android.view.KeyEvent.KEYCODE_ENTER
|
||||||
|
when {
|
||||||
|
imeAction == ImeAction.Next && (actionId == EditorInfo.IME_ACTION_NEXT || isEnter) -> {
|
||||||
|
onNextLatest.value?.invoke()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
imeAction == ImeAction.Done && (actionId == EditorInfo.IME_ACTION_DONE || isEnter) -> {
|
||||||
|
onDoneLatest.value?.invoke()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setOnFocusChangeListener { _, hasFocus ->
|
||||||
|
focused = hasFocus
|
||||||
|
onFocusChangeLatest.value(hasFocus)
|
||||||
|
}
|
||||||
|
addTextChangedListener(object : TextWatcher {
|
||||||
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
|
||||||
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
|
||||||
|
override fun afterTextChanged(s: Editable?) {
|
||||||
|
onValueChangeLatest.value(s?.toString().orEmpty())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
update = { editText ->
|
||||||
|
editText.setTextColor(colors.onBackground.toArgb())
|
||||||
|
editText.setHintTextColor(colors.onSurfaceVariant.toArgb())
|
||||||
|
editText.hint = placeholder
|
||||||
|
editText.contentDescription = label
|
||||||
|
if (editText.text.toString() != value) {
|
||||||
|
val cursor = if (editText.hasFocus()) editText.selectionStart.coerceAtLeast(0) else value.length
|
||||||
|
editText.setText(value)
|
||||||
|
editText.setSelection(min(cursor, value.length))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -355,7 +453,7 @@ private fun FallbackOrder(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun CompactComputerFallback(desiredEnabled: Boolean) {
|
private fun CompactComputerFallback(desiredEnabled: Boolean) {
|
||||||
SettingsGroup("3 · Computer ADB last resort") {
|
SettingsGroup("3 · Computer ADB last resort", compact = true) {
|
||||||
Text(
|
Text(
|
||||||
PhantomProcessLimit.adbCommand(desiredEnabled),
|
PhantomProcessLimit.adbCommand(desiredEnabled),
|
||||||
style = MaterialTheme.typography.labelSmall.copy(fontFamily = FontFamily.Monospace),
|
style = MaterialTheme.typography.labelSmall.copy(fontFamily = FontFamily.Monospace),
|
||||||
@@ -377,4 +475,36 @@ private fun StatusMessage(busy: Boolean, message: String?, modifier: Modifier =
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun validPort(value: String): Boolean = value.toIntOrNull()?.let { it in 1..65535 } == true
|
private data class AdbEndpoint(val host: String, val port: Int)
|
||||||
|
|
||||||
|
private fun parseAdbAddress(value: String): AdbEndpoint? {
|
||||||
|
val address = value.trim()
|
||||||
|
val host: String
|
||||||
|
val portText: String
|
||||||
|
if (address.startsWith("[")) {
|
||||||
|
val closingBracket = address.indexOf(']')
|
||||||
|
if (closingBracket <= 1 || address.getOrNull(closingBracket + 1) != ':') return null
|
||||||
|
host = address.substring(1, closingBracket)
|
||||||
|
if (':' !in host) return null
|
||||||
|
portText = address.substring(closingBracket + 2)
|
||||||
|
} else {
|
||||||
|
val colon = address.lastIndexOf(':')
|
||||||
|
if (colon <= 0 || address.indexOf(':') != colon) return null
|
||||||
|
host = address.substring(0, colon)
|
||||||
|
portText = address.substring(colon + 1)
|
||||||
|
}
|
||||||
|
if (host.isBlank() || host.any(Char::isWhitespace)) return null
|
||||||
|
val port = portText.toIntOrNull()?.takeIf { it in 1..65535 } ?: return null
|
||||||
|
return AdbEndpoint(host, port)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun formatAddressHost(host: String): String = if (':' in host) "[$host]" else host
|
||||||
|
private fun formatAdbAddress(host: String, port: Int): String = "${formatAddressHost(host)}:$port"
|
||||||
|
|
||||||
|
private fun imeActionFlag(action: ImeAction): Int = when (action) {
|
||||||
|
ImeAction.Next -> EditorInfo.IME_ACTION_NEXT
|
||||||
|
ImeAction.Done -> EditorInfo.IME_ACTION_DONE
|
||||||
|
ImeAction.Go -> EditorInfo.IME_ACTION_GO
|
||||||
|
ImeAction.Search -> EditorInfo.IME_ACTION_SEARCH
|
||||||
|
else -> EditorInfo.IME_ACTION_UNSPECIFIED
|
||||||
|
}
|
||||||
Reference in new issue
Block a user