-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathConnectedDevice+Device.swift
More file actions
123 lines (100 loc) · 2.94 KB
/
ConnectedDevice+Device.swift
File metadata and controls
123 lines (100 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// ConnectedDevice+Device.swift
// AndroidDeviceKit
//
// Created by Lukas Romsicki on 2022-10-24.
// Copyright © 2022 Shopify. All rights reserved.
//
import Foundation
import TophatFoundation
extension ConnectedDevice: Device {
var id: String {
serial
}
var runtime: Runtime {
// We aren't yet fetching the version for ADB devices.
.init(platform: .android, version: .unknown)
}
var name: String {
model?.replacingOccurrences(of: "_", with: " ") ?? serial
}
var type: DeviceType {
guard let product else {
// In case the product name is not available, fall back to checking the serial.
return serial.contains("emulator") ? .simulator : .device
}
return product.hasPrefix("sdk_") ? .simulator : .device
}
var connection: Connection {
if usb != nil {
return .direct
}
let adbServiceIndex = try! Regex("_.+\\._tcp")
// mDNS connections
if serial.contains(adbServiceIndex) {
return .network
}
// These expressions are very lenient, do not use to check validity.
let ipv4Address = try! Regex("(?:[0-9]{1,3}\\.){3}[0-9]{1,3}")
let ipv6Address = try! Regex("(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}")
// TCP/UDP connections
if serial.contains(ipv4Address) || serial.contains(ipv6Address) {
return .network
}
return .internal
}
var state: DeviceState {
DeviceState(from: _state)
}
var isLocked: Bool {
false
}
func install(application: Application) throws {
do {
try Adb.install(serial: id, apkUrl: application.url)
} catch {
throw DiagnosticError(DeviceError.failedToInstallApp(bundleUrl: application.url, deviceType: type), technicalDetails: error.shellErrorDiagnosticMessage)
}
}
func launch(application: Application, arguments: [String]? = nil) throws {
let bundleIdentifier = try application.bundleIdentifier
do {
let componentName = try Adb.resolveActivity(serial: id, packageName: bundleIdentifier)
try Adb.launch(serial: id, componentName: componentName, arguments: arguments ?? [])
} catch {
throw DiagnosticError(DeviceError.failedToLaunchApp(bundleId: bundleIdentifier, reason: .unexpected, deviceType: type), technicalDetails: error.shellErrorDiagnosticMessage)
}
}
func stream() throws {
try Scrcpy.connect(serial: id)
}
func openLogs() throws {
let appleScript = """
tell application "Terminal"
activate
do script "\(PathResolver.adb.path(percentEncoded: false)) -s \(serial) logcat -v long -v color time"
end tell
"""
guard let script = NSAppleScript(source: appleScript) else {
throw DeviceError.failedToOpenLogs
}
var error: NSDictionary?
script.executeAndReturnError(&error)
if error != nil {
throw DeviceError.failedToOpenLogs
}
}
func waitUntilUnlocked() async throws {
// Does not apply to Android devices.
}
}
private extension DeviceState {
init(from connectedDeviceState: ConnectedDevice.State) {
switch connectedDeviceState {
case .device:
self = .ready
default:
self = .unavailable
}
}
}