Skip to content

Commit

Permalink
get device list on background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
naman14 committed Mar 12, 2021
1 parent 075ec4e commit 5b18f18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
16 changes: 10 additions & 6 deletions adbconnect/AdbHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ class AdbHelper {
}

func makeTCPConnection(deviceId: String) {
let deviceIp = getDeviceIp(deviceId: deviceId);
let tcpCommand = "-s " + deviceId + " tcpip 5555"
_ = runAdbCommand(tcpCommand)
let connectCommand = "-s " + deviceId + " connect " + deviceIp + ":5555"
_ = runAdbCommand(connectCommand)
DispatchQueue.global(qos: .background).async {
let deviceIp = self.getDeviceIp(deviceId: deviceId);
let tcpCommand = "-s " + deviceId + " tcpip 5555"
_ = self.runAdbCommand(tcpCommand)
let connectCommand = "-s " + deviceId + " connect " + deviceIp + ":5555"
_ = self.runAdbCommand(connectCommand)
}
}

func disconnectTCPConnection(deviceId: String) {
_ = runAdbCommand("-s " + deviceId + " disconnect")
DispatchQueue.global(qos: .background).async {
_ = self.runAdbCommand("-s " + deviceId + " disconnect")
}
}

func getDeviceIp(deviceId: String) -> String {
Expand Down
22 changes: 10 additions & 12 deletions adbconnect/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ struct ContentView: View {

@State private var statusMessage: String = ""

init() {
_devices = State(initialValue: adb.getDevices())
}

var body: some View {
DispatchQueue.global(qos: .background).async {
devices = adb.getDevices()
}
return VStack {
ScrollView(.vertical) {
if (devices.isEmpty) {
Expand Down Expand Up @@ -77,12 +76,11 @@ struct DeviceActionsView: View {
}.contentShape(Rectangle())
.onTapGesture {
if(isTcpConnected()) {
adb.disconnectTCPConnection(deviceId: device.id)
statusMessaage = "Disconnected remoted connection"

adb.disconnectTCPConnection(deviceId: device.id)
} else {
adb.makeTCPConnection(deviceId: device.id)
statusMessaage = "Connected to adb remotely"
adb.makeTCPConnection(deviceId: device.id)
}
// refresh device list
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
Expand All @@ -96,8 +94,8 @@ struct DeviceActionsView: View {
Text("Take screenshot")
}.contentShape(Rectangle())
.onTapGesture {
statusMessaage = "Screenshot will be saved in Desktop"
adb.takeScreenshot(deviceId: device.id)
statusMessaage = "Screenshot saved in Desktop"
}

// record screen
Expand All @@ -107,13 +105,13 @@ struct DeviceActionsView: View {
}.contentShape(Rectangle())
.onTapGesture {
if (isRecordingScreen) {
statusMessaage = "Recording will be saved in Desktop"
adb.stopScreenRecording(deviceId: device.id)
isRecordingScreen = false
statusMessaage = "Recording saved in Desktop"
} else {
statusMessaage = "Started recording screen.."
adb.recordScreen(deviceId: device.id)
isRecordingScreen = true
statusMessaage = "Started recording screen.."
}
}

Expand All @@ -132,8 +130,8 @@ struct DeviceActionsView: View {
Image("DeeplinkIcon").resizable().frame(width: 18.0, height: 18.0)
TextField("deeplink", text: $deeplink).padding(.leading, 5)
Button(action: {
adb.openDeeplink(deviceId: device.id, deeplink: deeplink)
statusMessaage = "Opening deeplink.."
adb.openDeeplink(deviceId: device.id, deeplink: deeplink)
}, label: {
Text("Open")
})
Expand All @@ -145,8 +143,8 @@ struct DeviceActionsView: View {
Text("Capture logcat")
}.contentShape(Rectangle()).padding(.leading, 20)
.onTapGesture {
adb.captureBugReport(deviceId: device.id)
statusMessaage = "Logcat saved in Desktop"
adb.captureBugReport(deviceId: device.id)
}
}

Expand Down

0 comments on commit 5b18f18

Please sign in to comment.