Skip to content

Commit d8952dd

Browse files
authored
Merge pull request #137 from teamclouday/adb-fixes
Adb fixes
2 parents 9ca2aef + b6a0060 commit d8952dd

9 files changed

Lines changed: 46 additions & 46 deletions

File tree

‎Android/app/src/main/java/io/github/teamclouday/androidMic/domain/streaming/MicStreamManager.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MicStreamManager(
2222
}
2323

2424
Mode.ADB -> {
25-
TcpStreamer.adb(scope)
25+
TcpStreamer.adb(scope, port)
2626
}
2727

2828
Mode.USB -> {

‎Android/app/src/main/java/io/github/teamclouday/androidMic/domain/streaming/TcpStreamer.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ class TcpStreamer(
6060

6161
fun adb(
6262
scope: CoroutineScope,
63+
port: Int?
6364
) = TcpStreamer(
6465
scope = scope,
6566
tag = "AdbStreamer",
6667
ip = "127.0.0.1",
67-
port = 55555
68+
port = port
6869
)
6970
}
7071

‎Android/app/src/main/java/io/github/teamclouday/androidMic/ui/home/HomeScreen.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private fun ConnectButton(
263263
}
264264
}
265265

266-
DialogIpPort(vm = vm, expanded = dialogIpPortExpanded)
266+
DialogIpPort(vm = vm, expanded = dialogIpPortExpanded, portOnly = false)
267267

268268

269269

‎Android/app/src/main/java/io/github/teamclouday/androidMic/ui/home/NavigationDrawer.kt‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,21 @@ fun DrawerBody(vm: MainViewModel) {
7676
expanded = dialogModeExpanded
7777
)
7878

79-
when (mode.value) {
80-
Mode.WIFI, Mode.UDP -> {
81-
val dialogIpPortExpanded = rememberSaveable {
82-
mutableStateOf(false)
83-
}
84-
DialogIpPort(vm = vm, expanded = dialogIpPortExpanded)
85-
SettingsItem(
86-
title = stringResource(id = R.string.drawerIpPort),
87-
subTitle = vm.prefs.ip.getAsState().value + ":" + vm.prefs.port.getAsState().value,
88-
contentDescription = "set ip and port",
89-
icon = Icons.Rounded.Wifi,
90-
expanded = dialogIpPortExpanded
91-
)
79+
if (mode.value != Mode.USB) {
80+
val dialogIpPortExpanded = rememberSaveable {
81+
mutableStateOf(false)
9282
}
93-
94-
else -> {
95-
96-
}
97-
98-
// Modes.USB, Modes.BLUETOOTH -> {
99-
//
100-
// }
83+
DialogIpPort(vm = vm, expanded = dialogIpPortExpanded, portOnly = mode.value == Mode.ADB)
84+
SettingsItem(
85+
title = stringResource(if (mode.value == Mode.ADB) R.string.dialog_port else R.string.drawerIpPort),
86+
subTitle = (if(mode.value != Mode.ADB) (vm.prefs.ip.getAsState().value + ":") else "") + vm.prefs.port.getAsState().value,
87+
contentDescription = "set ip and port",
88+
icon = Icons.Rounded.Wifi,
89+
expanded = dialogIpPortExpanded
90+
)
10191
}
10292

93+
10394
// Audio
10495
SettingsItemsSubtitle(R.string.drawer_subtitle_record)
10596

‎Android/app/src/main/java/io/github/teamclouday/androidMic/ui/home/dialog/ipPort.kt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import io.github.teamclouday.androidMic.ui.components.ManagerButton
2020
import io.github.teamclouday.androidMic.ui.components.ManagerOutlinedTextField
2121

2222
@Composable
23-
fun DialogIpPort(vm: MainViewModel, expanded: MutableState<Boolean>) {
23+
fun DialogIpPort(vm: MainViewModel, expanded: MutableState<Boolean>, portOnly: Boolean) {
2424

2525
val tempIp = rememberSaveable {
2626
mutableStateOf(vm.prefs.ip.getBlocking())
@@ -49,9 +49,11 @@ fun DialogIpPort(vm: MainViewModel, expanded: MutableState<Boolean>) {
4949
horizontalAlignment = Alignment.CenterHorizontally
5050
) {
5151
// ip field
52-
ManagerOutlinedTextField(tempIp, R.string.dialog_ip)
52+
if(!portOnly) {
53+
ManagerOutlinedTextField(tempIp, R.string.dialog_ip)
5354

54-
Spacer(modifier = Modifier.height(10.dp))
55+
Spacer(modifier = Modifier.height(10.dp))
56+
}
5557

5658
// port field
5759
ManagerOutlinedTextField(tempPort, R.string.dialog_port)

‎RustApp/src/streamer/adb_streamer.rs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tokio::process::Command;
33

44
use crate::{
55
config::ConnectionMode,
6-
streamer::{DEFAULT_PC_PORT, StreamerMsg, tcp_streamer},
6+
streamer::{StreamerMsg, tcp_streamer},
77
};
88

99
use super::{
@@ -15,8 +15,6 @@ pub struct AdbStreamer {
1515
tcp_streamer: TcpStreamer,
1616
}
1717

18-
const ANDROID_REMOTE_PORT: u16 = 55555;
19-
2018
async fn get_connected_devices() -> Result<Vec<String>, ConnectError> {
2119
let mut cmd = Command::new("adb");
2220
cmd.arg("devices");
@@ -35,13 +33,13 @@ async fn get_connected_devices() -> Result<Vec<String>, ConnectError> {
3533
Ok(devices)
3634
}
3735

38-
async fn remove_adb_reverse_proxy(device_id: &str) -> Result<(), ConnectError> {
36+
async fn remove_adb_reverse_proxy(device_id: &str, port: u16) -> Result<(), ConnectError> {
3937
let mut cmd = Command::new("adb");
4038
cmd.arg("-s")
4139
.arg(device_id)
4240
.arg("reverse")
4341
.arg("--remove")
44-
.arg(format!("tcp:{ANDROID_REMOTE_PORT}"));
42+
.arg(format!("tcp:{}", port));
4543

4644
exec_cmd(cmd).await?;
4745

@@ -67,25 +65,26 @@ async fn exec_cmd(mut cmd: Command) -> Result<String, ConnectError> {
6765
Ok(stdout)
6866
}
6967

70-
pub async fn new(stream_config: AudioStream) -> Result<AdbStreamer, ConnectError> {
71-
let tcp_streamer =
72-
tcp_streamer::new("127.0.0.1".parse().unwrap(), DEFAULT_PC_PORT, stream_config).await?;
68+
pub async fn new(port: u16, stream_config: AudioStream) -> Result<AdbStreamer, ConnectError> {
69+
let tcp_streamer = tcp_streamer::new("127.0.0.1".parse().unwrap(), port, stream_config).await?;
7370

7471
let devices = get_connected_devices().await?;
7572
if devices.is_empty() {
7673
return Err(ConnectError::NoAdbDevice);
7774
}
7875

7976
for device_id in &devices {
80-
if let Err(e) = remove_adb_reverse_proxy(device_id).await {
81-
warn!("cannot remove adb proxy for device {device_id}: {e}");
77+
if let Err(e) = remove_adb_reverse_proxy(device_id, tcp_streamer.port).await {
78+
if !e.to_string().contains("not found") {
79+
warn!("cannot remove adb proxy for device {device_id}: {e}");
80+
}
8281
}
8382

8483
let mut cmd = Command::new("adb");
8584
cmd.arg("-s")
8685
.arg(device_id)
8786
.arg("reverse")
88-
.arg(format!("tcp:{ANDROID_REMOTE_PORT}"))
87+
.arg(format!("tcp:{}", tcp_streamer.port))
8988
.arg(format!("tcp:{}", tcp_streamer.port));
9089
exec_cmd(cmd).await?;
9190
}
@@ -120,11 +119,12 @@ impl StreamerTrait for AdbStreamer {
120119

121120
impl Drop for AdbStreamer {
122121
fn drop(&mut self) {
123-
tokio::task::spawn_blocking(|| async {
124-
let devices = get_connected_devices().await.unwrap_or_default();
122+
let port = self.tcp_streamer.port;
123+
tokio::spawn(async move {
124+
let devices: Vec<String> = get_connected_devices().await.unwrap_or_default();
125125

126126
for device_id in devices {
127-
if let Err(e) = remove_adb_reverse_proxy(&device_id).await {
127+
if let Err(e) = remove_adb_reverse_proxy(&device_id, port).await {
128128
warn!("cannot remove adb proxy for device {device_id}: {e}");
129129
}
130130
}

‎RustApp/src/streamer/streamer_runner.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ pub enum ConnectOption {
2828
port: u16,
2929
},
3030
#[cfg(feature = "adb")]
31-
Adb,
31+
Adb {
32+
port: u16,
33+
},
3234
#[cfg(feature = "usb")]
3335
Usb,
3436
}
@@ -150,8 +152,8 @@ pub fn sub() -> impl Stream<Item = StreamerMsg> {
150152
.map(Streamer::from)
151153
}
152154
#[cfg(feature = "adb")]
153-
ConnectOption::Adb => {
154-
crate::streamer::adb_streamer::new(stream_config)
155+
ConnectOption::Adb { port } => {
156+
crate::streamer::adb_streamer::new(port, stream_config)
155157
.await
156158
.map(Streamer::from)
157159
}
@@ -171,6 +173,7 @@ pub fn sub() -> impl Stream<Item = StreamerMsg> {
171173
match new_streamer {
172174
Ok(new_streamer) => {
173175
send(&mut sender, new_streamer.status()).await;
176+
drop(streamer);
174177
streamer = new_streamer;
175178
}
176179
Err(e) => {
@@ -190,6 +193,7 @@ pub fn sub() -> impl Stream<Item = StreamerMsg> {
190193
streamer.reconfigure_stream(stream_config);
191194
}
192195
StreamerCommand::Stop => {
196+
drop(streamer);
193197
streamer = DummyStreamer::new();
194198
}
195199
}

‎RustApp/src/ui/app.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl AppState {
235235
}
236236
}
237237
#[cfg(feature = "adb")]
238-
ConnectionMode::Adb => ConnectOption::Adb,
238+
ConnectionMode::Adb => ConnectOption::Adb { port: config.port },
239239
#[cfg(feature = "usb")]
240240
ConnectionMode::Usb => ConnectOption::Usb,
241241
};

‎RustApp/src/ui/view.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use crate::{
2929
pub static SCROLLABLE_ID: LazyLock<cosmic::widget::Id> = LazyLock::new(cosmic::widget::Id::unique);
3030

3131
pub fn main_window(app: &AppState) -> Element<'_, AppMsg> {
32+
let connection_mode = app.config.data().connection_mode;
33+
3234
row()
3335
.padding(50)
3436
.spacing(50)
@@ -46,7 +48,7 @@ pub fn main_window(app: &AppState) -> Element<'_, AppMsg> {
4648
.height(Length::Fill)
4749
.spacing(35)
4850
.align_x(Horizontal::Center)
49-
.push(network_adapter(app))
51+
.push_maybe((connection_mode == ConnectionMode::Tcp || connection_mode == ConnectionMode::Udp).then(|| network_adapter(app)))
5052
.push(audio(app))
5153
.push(vertical_space())
5254
.push(connection_type(app)),

0 commit comments

Comments
 (0)