-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFormOpenViewServerWindow.pas
255 lines (215 loc) · 6.73 KB
/
FormOpenViewServerWindow.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
unit FormOpenViewServerWindow;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Forms, Controls,
StdCtrls, AndroidDebugBridge;
type
{ TOpenViewServerWindowForm }
TOpenViewServerWindowForm = class(TForm)
ButtonCancel: TButton;
ButtonOpen: TButton;
ButtonRefreshWindows: TButton;
ButtonRefreshDevices: TButton;
Label1: TLabel;
Label2: TLabel;
ListBoxDevices: TListBox;
ListBoxWindows: TListBox;
procedure ButtonRefreshDevicesClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: char);
procedure ListBoxDevicesSelectionChange(Sender: TObject; {%H-}User: boolean);
procedure ListBoxWindowsDblClick(Sender: TObject);
procedure ListBoxWindowsSelectionChange(Sender: TObject; {%H-}User: boolean);
private
FAdbInterface: TAdbInterface;
FSelectedDevice: TDeviceInterface;
FWindowHashes: array of string;
function GetSelectedDeviceSerial: string;
function GetSelectedWindowHash: string;
function GetSelectedWindowTitle: string;
procedure SetSelectedDevice(V: TDeviceInterface);
procedure SetWindowList(const WindowList: TWindowManagerEntryArray);
protected
procedure AdbDeviceListComplete(Sender: TObject;
const DeviceList: TAdbDeviceEntryArray);
procedure AdbDeviceListError(Sender: TObject);
procedure DeviceWindowListComplete(Sender: TDeviceInterface;
const WindowList: TWindowManagerEntryArray);
procedure DeviceWindowListError(Sender: TObject);
property SelectedDevice: TDeviceInterface
read FSelectedDevice write SetSelectedDevice;
property WindowList: TWindowManagerEntryArray write SetWindowList;
public
property SelectedWindowTitle: string read GetSelectedWindowTitle;
property SelectedDeviceSerial: string read GetSelectedDeviceSerial;
property SelectedWindowHash: string read GetSelectedWindowHash;
end;
var
OpenViewServerWindowForm: TOpenViewServerWindowForm;
implementation
const
WindowNoTitle = '<untitled>';
{$R *.lfm}
procedure FreeListBoxObjects(ListBox: TCustomListBox); inline;
var
I: integer;
begin
for I := 0 to ListBox.Items.Count - 1 do
ListBox.Items.Objects[I].Free;
end;
{ TOpenViewServerWindowForm }
procedure TOpenViewServerWindowForm.FormCreate(Sender: TObject);
begin
KeyPreview := True;
FAdbInterface := TAdbInterface.Create;
FAdbInterface.OnDeviceListComplete := @AdbDeviceListComplete;
FAdbInterface.OnDeviceListError := @AdbDeviceListError;
FAdbInterface.GetDeviceList;
ButtonRefreshDevices.Enabled := False;
end;
procedure TOpenViewServerWindowForm.ButtonRefreshDevicesClick(Sender: TObject);
begin
ButtonRefreshDevices.Enabled := False;
FAdbInterface.GetDeviceList;
end;
procedure TOpenViewServerWindowForm.FormDestroy(Sender: TObject);
begin
FreeListBoxObjects(ListBoxDevices);
FAdbInterface.Free;
end;
procedure TOpenViewServerWindowForm.FormKeyPress(Sender: TObject; var Key: char);
begin
if Key = #27 then
ModalResult := mrCancel;
end;
procedure TOpenViewServerWindowForm.ListBoxDevicesSelectionChange(Sender: TObject;
User: boolean);
var
I: integer;
begin
I := ListBoxDevices.ItemIndex;
if I > -1 then
SelectedDevice := TDeviceInterface(ListBoxDevices.Items.Objects[I])
else
SelectedDevice := nil;
end;
procedure TOpenViewServerWindowForm.ListBoxWindowsDblClick(Sender: TObject);
begin
if (SelectedWindowHash <> EmptyStr) and ButtonOpen.Enabled then
ButtonOpen.Click;
end;
procedure TOpenViewServerWindowForm.ListBoxWindowsSelectionChange(Sender: TObject;
User: boolean);
begin
ButtonOpen.Enabled := ListBoxWindows.ItemIndex > -1;
end;
procedure TOpenViewServerWindowForm.SetSelectedDevice(V: TDeviceInterface);
begin
if FSelectedDevice = V then
Exit;
FSelectedDevice := V;
ListBoxWindows.Clear;
ButtonRefreshWindows.Enabled := False;
if Assigned(V) then
V.GetWindowList;
end;
procedure TOpenViewServerWindowForm.SetWindowList(
const WindowList: TWindowManagerEntryArray);
var
I: integer;
WindowTitle: string;
begin
// We need to associate another string (window hash) with each item
// in ListBoxWindows (window title).
// But since with TCustomListBox.AddItem we can only associate a TObject,
// we keep the hashes in a separate string array.
SetLength(FWindowHashes, Length(WindowList));
ListBoxWindows.Items.BeginUpdate;
try
ListBoxWindows.Clear;
for I := 0 to Length(WindowList) - 1 do
begin
WindowTitle := WindowList[I].Title;
if WindowTitle = EmptyStr then
WindowTitle := WindowNoTitle;
ListBoxWindows.AddItem(WindowTitle, nil);
FWindowHashes[I] := WindowList[I].HashCode;
end;
finally
ListBoxWindows.Items.EndUpdate;
end;
end;
procedure TOpenViewServerWindowForm.AdbDeviceListComplete(Sender: TObject;
const DeviceList: TAdbDeviceEntryArray);
var
E: TAdbDeviceEntry;
DeviceInterface: TDeviceInterface;
S: string = '';
I: integer;
begin
if Assigned(SelectedDevice) then
S := SelectedDevice.SerialNumber;
FreeListBoxObjects(ListBoxDevices);
ListBoxDevices.Clear;
for E in DeviceList do
begin
DeviceInterface := TDeviceInterface.Create(E.SerialNumber);
DeviceInterface.OnWindowListComplete := @DeviceWindowListComplete;
DeviceInterface.OnWindowListError := @DeviceWindowListError;
ListBoxDevices.AddItem(E.SerialNumber, DeviceInterface);
end;
// Select the previously selected device, the first one or nothing.
I := ListBoxDevices.Items.IndexOf(S);
if I > -1 then
ListBoxDevices.ItemIndex := I
else
if ListBoxDevices.Count > 0 then
ListBoxDevices.ItemIndex := 0
else
SelectedDevice := nil;
ButtonRefreshDevices.Enabled := True;
end;
procedure TOpenViewServerWindowForm.AdbDeviceListError(Sender: TObject);
begin
ButtonRefreshDevices.Enabled := True;
end;
function TOpenViewServerWindowForm.GetSelectedDeviceSerial: string;
begin
if Assigned(FSelectedDevice) then
Result := FSelectedDevice.SerialNumber
else
Result := EmptyStr;
end;
function TOpenViewServerWindowForm.GetSelectedWindowHash: string;
var
I: integer;
begin
I := ListBoxWindows.ItemIndex;
if I > -1 then
Result := FWindowHashes[I]
else
Result := EmptyStr;
end;
function TOpenViewServerWindowForm.GetSelectedWindowTitle: string;
var
I: integer;
begin
I := ListBoxWindows.ItemIndex;
if I > -1 then
Result := ListBoxWindows.Items[I]
else
Result := EmptyStr;
end;
procedure TOpenViewServerWindowForm.DeviceWindowListComplete(Sender: TDeviceInterface;
const WindowList: TWindowManagerEntryArray);
begin
if SelectedDevice = Sender then
Self.WindowList := WindowList;
end;
procedure TOpenViewServerWindowForm.DeviceWindowListError(Sender: TObject);
begin
ButtonRefreshWindows.Enabled := Assigned(SelectedDevice);
end;
end.