-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDAW.Adb.pas
165 lines (145 loc) · 4.21 KB
/
DAW.Adb.pas
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
unit DAW.Adb;
interface
uses
DAW.Model.Device.New,
DAW.Utils.DosCMD,
DAW.Adb.Parser;
type
TdawAdb = class
private const
TCPIP_PORT = '5555';
private
FCommandLine: TDosCMD;
FAdbParser: TdawAdbParser;
function DisconnectDevice(ADeviceIp: string): Boolean;
procedure enableTCPCommand();
function checkTCPCommandExecuted: Boolean;
function connectDeviceByIP(deviceIp: string): Boolean;
function connectDevice(ADevice: TdawDevice): Boolean;
public
procedure Upgrade(ADevice: TdawDevice);
constructor Create(ACommandLine: TDosCMD; AAdbParser: TdawAdbParser);
function IsInstalled: Boolean;
function getDevicesConnectedByUSB: TArray<TdawDevice>;
function connectDevices(ADevices: TArray<TdawDevice>): TArray<TdawDevice>;
function disconnectDevices(ADevices: TArray<TdawDevice>): TArray<TdawDevice>;
function getDeviceIp(ADevice: TdawDevice): string;
function getDeviceModel(ADevice: TdawDevice): string;
end;
implementation
uses
System.IOUtils,
System.SysUtils,
DAW.Tools;
{ TdawAdb }
function TdawAdb.checkTCPCommandExecuted: Boolean;
var
getPropCommand: string;
getPropOutput: string;
adbTcpPort: string;
begin
getPropCommand := 'adb shell getprop | grep adb';
getPropOutput := FCommandLine.Execute(getPropCommand);
adbTcpPort := FAdbParser.parseAdbServiceTcpPort(getPropOutput);
Result := TCPIP_PORT.equals(adbTcpPort);
end;
function TdawAdb.connectDeviceByIP(deviceIp: string): Boolean;
var
enableTCPCommand: string;
connectDeviceCommand: string;
connectOutput: string;
begin
enableTCPCommand := 'adb tcpip 5555';
FCommandLine.Execute(enableTCPCommand);
connectDeviceCommand := 'adb connect ' + deviceIp;
connectOutput := FCommandLine.Execute(connectDeviceCommand);
Result := connectOutput.contains('connected');
end;
function TdawAdb.connectDevice(ADevice: TdawDevice): Boolean;
begin
if ADevice.IP.IsEmpty then
ADevice.IP := getDeviceIp(ADevice);
if ADevice.IP.IsEmpty() then
Result := False
else
begin
ADevice.ID := ADevice.IP;
Result := connectDeviceByIP(ADevice.IP);
end;
end;
function TdawAdb.connectDevices(ADevices: TArray<TdawDevice>): TArray<TdawDevice>;
var
LConnected: Boolean;
I: Integer;
begin
Result := ADevices;
for I := Low(Result) to High(Result) do
begin
LConnected := connectDevice(Result[I]);
Result[I].IsConnected := (LConnected);
end;
end;
constructor TdawAdb.Create(ACommandLine: TDosCMD; AAdbParser: TdawAdbParser);
begin
FCommandLine := ACommandLine;
FAdbParser := AAdbParser;
end;
function TdawAdb.DisconnectDevice(ADeviceIp: string): Boolean;
var
connectDeviceCommand: string;
begin
enableTCPCommand();
connectDeviceCommand := 'adb disconnect ' + ADeviceIp;
Result := FCommandLine.Execute(connectDeviceCommand).IsEmpty();
end;
function TdawAdb.disconnectDevices(ADevices: TArray<TdawDevice>): TArray<TdawDevice>;
var
LDevice: TdawDevice;
LDisconnected: Boolean;
begin
for LDevice in ADevices do
begin
LDisconnected := DisconnectDevice(LDevice.IP);
LDevice.IsConnected := LDisconnected;
end;
Result := ADevices;
end;
procedure TdawAdb.enableTCPCommand;
var
LEnableTCPCommand: string;
begin
if not checkTCPCommandExecuted() then
begin
LEnableTCPCommand := ('adb tcpip ' + TCPIP_PORT);
FCommandLine.Execute(LEnableTCPCommand);
end;
end;
function TdawAdb.getDeviceIp(ADevice: TdawDevice): string;
var
getDeviceIpCommand: string;
ipInfoOutput: string;
begin
getDeviceIpCommand := 'adb -s ' + ADevice.ID + ' shell ip -f inet addr show wlan0';
ipInfoOutput := FCommandLine.Execute(getDeviceIpCommand);
Result := FAdbParser.parseGetDeviceIp(ipInfoOutput);
end;
function TdawAdb.getDeviceModel(ADevice: TdawDevice): string;
begin
Result := FCommandLine.Execute('adb -s %s shell getprop ro.product.model', [ADevice.ID]);
end;
function TdawAdb.getDevicesConnectedByUSB: TArray<TdawDevice>;
var
adbDevicesOutput: string;
begin
adbDevicesOutput := FCommandLine.Execute('adb devices -l');
Result := FAdbParser.parseGetDevicesOutput(adbDevicesOutput);
end;
function TdawAdb.IsInstalled: Boolean;
begin
Result := TFile.Exists(TDawTools.AdbExe);
end;
procedure TdawAdb.Upgrade(ADevice: TdawDevice);
begin
ADevice.IP := getDeviceIp(ADevice);
end;
end.