-
Notifications
You must be signed in to change notification settings - Fork 11
/
screen.js
executable file
·352 lines (305 loc) · 10.9 KB
/
screen.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var real = {width:1080,height:1920};
var ratioWidth = 1,ratioHeight = 1;
var canvas = $('#mat_canvas');
var socketIds = {};
function ratio(real) {
ratioWidth = real.width/canvas.width();
ratioHeight = real.height/canvas.height();
}
ratio(real);
function mouseupListener(socketId) {
var str = ['u\n',
'c\n'
];
for (var s in str) {
var sTemp = str2ab(str[s], null, false);
chrome.sockets.tcp.send(socketId, sTemp, function (n) {
})
}
$('#mat_canvas').off('mousemove');
$('#mat_canvas').off('mouseleave');
}
function mousedownListener(socketId) {
if(event.button != 0) return;
$('#mat_canvas').off('mousemove');
$('#mat_canvas').off('mouseleave');
var x = Math.ceil(event.offsetX*ratioWidth), y = Math.ceil(event.offsetY*ratioHeight);
var str = ['d 0 ' + x + ' ' + y + ' 50\n',
'c\n'
];
for (var s in str) {
var sTemp = str2ab(str[s], null, false);
chrome.sockets.tcp.send(socketId, sTemp, function (n) {
})
}
$('#mat_canvas').on('mousemove',mousemoveListener.bind(null,socketId));
$('#mat_canvas').on('mouseleave',mouseupListener.bind(null,socketId));
}
function mousemoveListener(socketId) {
var x = Math.ceil(event.offsetX*ratioWidth), y = Math.ceil(event.offsetY*ratioHeight);
var str = ['m 0 ' + x + ' ' + y + ' 50\n',
'c\n'
];
for (var s in str) {
var sTemp = str2ab(str[s], null, false);
chrome.sockets.tcp.send(socketId, sTemp, function (n) {
})
}
}
var client,deviceObj;
$(window).ready(function () {
deviceObj = JSON.parse(chrome.app.window.current().id)
client = new Tcp();
var device = deviceObj.device;
sendCommands('client',"shell:find /data/local/tmp/minicap",deviceObj.serialNumber,()=>{
console.log('findMinicap'+client.socketId);
socketIds['findMinicap'] = client.socketId;
});
minicapSocket(device);
minitouchSocket(device);
});
function closeSocket() {
for (id in socketIds) {
console.log('close socket:', socketIds[id]);
chrome.sockets.tcp.close(socketIds[id], function () {
if(chrome.runtime.lastError) console.log(chrome.runtime.lastError);
})
}
socketIds = {};
}
function minitouchSocket(device) {
if (socketIds['minitouch'])
return;
chrome.sockets.tcp.create(function (createInfo) {
chrome.sockets.tcp.connect(createInfo.socketId, '127.0.0.1', 1111+device, function () {
console.log(1111+device)
socketIds['minitouch'] = createInfo.socketId;
$('#mat_canvas').on('mousedown',mousedownListener.bind(null,createInfo.socketId));
$('#mat_canvas').on('mouseup',mouseupListener.bind(null,createInfo.socketId));
});
});
}
function minicapSocket(device) {
if (socketIds['minicap'])
return;
chrome.sockets.tcp.create(function (createInfo) {
chrome.sockets.tcp.connect(createInfo.socketId, "127.0.0.1", 3131+device, function (result) {
console.log(3131+device)
socketIds['minicap'] = createInfo.socketId;
});
});
}
chrome.sockets.tcp.onReceive.addListener(function (message) {
if (message.socketId) {
if(message.socketId == socketIds['findMinicap']){
ab2str(message.data, function (e) {
console.log('返回值'+e)
if (e.startsWith('OKAY')) {
return null;
}else if(e.indexOf('No such file') != -1){
//弹出提示信息
var opt = {
type: "basic",
iconUrl: '/assets/ss_icon11.png',
title: '设备异常',
message:"无法正常启动设备,请尝试重新连接设备或重新打开应用...",
}
chrome.notifications.create(opt,()=>{})
//加哭脸
var div = document.createElement('div');
$(div).attr('id','mat_error');
$('body').append(div);
}
})
}
if (socketIds['minicap'] && (socketIds['minicap'] == message.socketId)) {
tryRead(message);//minicap receive callback
}else if (socketIds['minitouch'] && (socketIds['minitouch'] == message.socketId)) {
//touchReceive(message.data);
}else if (socketIds['tcp5037'] && message.socketId == socketIds['tcp5037']) {
ab2str(message.data,function (e) {
if(!e.startsWith('OKAY')){
socketReceive(e);
}
});
}
}
});
//未完成方法
function socketReceive(e){
console.log('receive:'+e)
// 这里准备推送文件
// if(e == 'arm64-v8a'){
// sendCommands("getprop ro.build.version.sdk | tr -d '\r'",'RO5PBM5LQWJZDQCM',()=>{})
// } else if (!isNaN(e)){
// sendCommands('')
// }
}
var readBannerBytes = 0
var bannerLength = 2
var readFrameBytes = 0
var frameBodyLength = 0
var frameBody = new ArrayBuffer(0);
var banner = {
version: 0
, length: 0
, pid: 0
, realWidth: 0
, realHeight: 0
, virtualWidth: 0
, virtualHeight: 0
, orientation: 0
, quirks: 0
}
function tryRead(message) {
var chunk = new Uint8Array(message.data)
//console.log('chunk.byteLength:',chunk.byteLength)
for (var cursor = 0, len = chunk.byteLength; cursor < len;) {
//console.log(readBannerBytes +' '+ bannerLength);
if (readBannerBytes < bannerLength) {
//console.log(readBannerBytes);
switch (readBannerBytes) {
case 0:
// version
banner.version = chunk[cursor]
break
case 1:
// length
banner.length = bannerLength = chunk[cursor]
break
case 2:
case 3:
case 4:
case 5:
// pid
banner.pid +=
(chunk[cursor] << ((readBannerBytes - 2) * 8)) >>> 0
break
case 6:
case 7:
case 8:
case 9:
// real width
banner.realWidth +=
(chunk[cursor] << ((readBannerBytes - 6) * 8)) >>> 0
break
case 10:
case 11:
case 12:
case 13:
// real height
banner.realHeight +=
(chunk[cursor] << ((readBannerBytes - 10) * 8)) >>> 0
break
case 14:
case 15:
case 16:
case 17:
// virtual width
banner.virtualWidth +=
(chunk[cursor] << ((readBannerBytes - 14) * 8)) >>> 0
break
case 18:
case 19:
case 20:
case 21:
// virtual height
banner.virtualHeight +=
(chunk[cursor] << ((readBannerBytes - 18) * 8)) >>> 0
break
case 22:
// orientation
banner.orientation += chunk[cursor] * 90
break
case 23:
// quirks
banner.quirks = chunk[cursor]
break
}
cursor += 1
readBannerBytes += 1;
//readBannerBytes == 1 ? console.log('read1:',banner) :'';
//readBannerBytes == 2 ? console.log('read2:',banner) :'';
if (readBannerBytes === bannerLength) {
//console.log('banner', banner)
}
}
else if (readFrameBytes < 4) {//0,1,2,3
frameBodyLength += (chunk[cursor] << (readFrameBytes * 8)) >>> 0
cursor += 1//4,5
readFrameBytes += 1//0,1,2,3
//console.info('headerbyte%d(val=%d,cursor=%d)', readFrameBytes, frameBodyLength,cursor)
}
else {
if (len - cursor >= frameBodyLength) {
// console.info('bodyfin(len=%d,cursor=%d)', frameBodyLength, cursor)
var abTemp = chunk.subarray(cursor, cursor + frameBodyLength)
var temp = new Uint8Array(abTemp.length + frameBody.byteLength);
temp.set(new Uint8Array(frameBody));
temp.set(abTemp, frameBody.byteLength);
frameBody = temp.buffer;
//console.log(frameBody.byteLength)
DrawImage(frameBody)
//console.log(frameBody.length)
cursor += frameBodyLength
frameBodyLength = readFrameBytes = 0
frameBody = new ArrayBuffer(0)
}
else {
//console.info('body(len=%d),cursor=%d', len - cursor,cursor)
var abTemp = chunk.subarray(cursor, len)
var temp = new Uint8Array(abTemp.length + frameBody.byteLength);
temp.set(new Uint8Array(frameBody));
temp.set(abTemp, frameBody.byteLength);
frameBody = temp.buffer;
frameBodyLength -= len - cursor
readFrameBytes += len - cursor
cursor = len
}
}
/*jshint browser:true*/
}
}
function DrawImage(frameBody) {
var BLANK_IMG = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
var canvas = document.getElementById('mat_canvas');
var g = canvas.getContext('2d')
var blob = new Blob([frameBody], {type: 'image/jpeg'})
var URL = window.URL || window.webkitURL
var img = new Image()
img. () {
canvas.width = img.width
canvas.height = img.height
//ratio(real,img);
g.drawImage(img, 0, 0)
img.>
img.src = BLANK_IMG
img = null
u = null
blob = null
}
var u = URL.createObjectURL(blob)
img.src = u
}
/*buttons*/
//187多任务 3主页 4返回 26锁屏
document.getElementById('mat_back').addEventListener('click',function(){
sendCommands('client',"shell:input keyevent 4",deviceObj.serialNumber,()=>{
});
});
document.getElementById('mat_home').addEventListener('click',function(){
sendCommands('client',"shell:input keyevent 3",deviceObj.serialNumber,()=>{
});
})
document.getElementById('mat_tasks').addEventListener('click',function(){
sendCommands('client',"shell:input keyevent 187",deviceObj.serialNumber,()=>{
});
})
/*监听拔调设备*/
if (chrome.usb.onDeviceRemoved) {
chrome.usb.onDeviceRemoved.addListener(function (device) {
if((device.serialNumber+device.device) == (deviceObj.serialNumber+deviceObj.device)){
chrome.app.window.current().close();
}
});
}