Skip to content

Commit 517797a

Browse files
author
朱辉\harry
committed
修复横屏状态下的坐标异常
1 parent 6278055 commit 517797a

8 files changed

Lines changed: 145 additions & 45 deletions

File tree

‎src/main/java/com/yeetor/Main.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class Main {
1818
* [client ip port]
1919
*/
2020
public static void main(String[] args) {
21-
2221
try {
2322
Config config = new Config(args);
2423

‎src/main/java/com/yeetor/androidcontrol/client/LocalClient.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.yeetor.minicap.MinicapListener;
1414
import com.yeetor.minitouch.Minitouch;
1515
import com.yeetor.minitouch.MinitouchListener;
16+
import com.yeetor.util.Constant;
1617
import io.netty.buffer.Unpooled;
1718
import io.netty.channel.ChannelHandlerContext;
1819
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
@@ -204,7 +205,7 @@ private void pushCommand(Command command) {
204205

205206
IDevice device = AdbServer.server().getDevice(protocol.getSn());
206207
try {
207-
device.pushFile(new File(name).getAbsolutePath(), path + "/" + name);
208+
device.pushFile(Constant.getTmpFile(name).getAbsolutePath(), path + "/" + name);
208209
} catch (Exception e) {
209210
}
210211
if (protocol != null) {

‎src/main/java/com/yeetor/androidcontrol/client/RemoteClient.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
import com.neovisionaries.ws.client.*;
66
import com.yeetor.adb.AdbServer;
77
import com.yeetor.androidcontrol.Command;
8+
import com.yeetor.androidcontrol.message.BinaryMessage;
9+
import com.yeetor.androidcontrol.message.FileMessage;
810
import com.yeetor.minicap.Banner;
911
import com.yeetor.minicap.Minicap;
1012
import com.yeetor.minicap.MinicapListener;
1113
import com.yeetor.minitouch.Minitouch;
1214
import com.yeetor.minitouch.MinitouchListener;
15+
import com.yeetor.util.Constant;
16+
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
1317

18+
import java.io.File;
19+
import java.io.FileNotFoundException;
20+
import java.io.FileOutputStream;
1421
import java.io.IOException;
22+
import java.util.Arrays;
1523
import java.util.List;
1624
import java.util.Map;
1725
import java.util.concurrent.BlockingQueue;
@@ -163,6 +171,9 @@ void executeCommand(Command command) {
163171
case INPUT:
164172
inputCommand(command);
165173
break;
174+
case PUSH:
175+
pushCommand(command);
176+
break;
166177
}
167178
}
168179

@@ -197,6 +208,18 @@ private void inputCommand(Command command) {
197208
if (minitouch != null) minitouch.inputText(str);
198209
}
199210

211+
private void pushCommand(Command command) {
212+
String name = command.getString("name", null);
213+
String path = command.getString("path", null);
214+
215+
IDevice device = AdbServer.server().getDevice(serialNumber);
216+
try {
217+
device.pushFile(Constant.getTmpFile(name).getAbsolutePath(), path + "/" + name);
218+
} catch (Exception e) {
219+
}
220+
ws.sendText("message://pushfile success");
221+
}
222+
200223
private void startMinicap(Command command) {
201224
if (minicap != null) {
202225
minicap.kill();
@@ -246,12 +269,41 @@ public void onTextMessage(WebSocket websocket, String text) throws Exception {
246269
case TOUCH:
247270
case KEYEVENT:
248271
case INPUT:
272+
case PUSH:
249273
executeCommand(command);
250274
break;
251275
}
252276
}
253277
}
254278

279+
@Override
280+
public void onBinaryMessage(WebSocket websocket, byte[] data) throws Exception {
281+
int headlen = (data[1] & 0xFF) << 8 | (data[0] & 0xFF);
282+
String infoJSON = new String(data, 2, headlen);
283+
BinaryMessage message = BinaryMessage.parse(infoJSON);
284+
System.out.println(infoJSON);
285+
if (message.getType().equals("file")) {
286+
FileMessage fileMessage = (FileMessage) message;
287+
File file = Constant.getTmpFile(fileMessage.name);
288+
if (fileMessage.offset == 0 && file.exists()) {
289+
file.delete();
290+
}
291+
try {
292+
FileOutputStream os = new FileOutputStream(file, true);
293+
byte[] bs = Arrays.copyOfRange(data, 2 + headlen, data.length);
294+
os.write(bs);
295+
os.close();
296+
} catch (FileNotFoundException e) {
297+
e.printStackTrace();
298+
} catch (IOException e) {
299+
e.printStackTrace();
300+
}
301+
if (fileMessage.offset + fileMessage.packagesize == fileMessage.filesize) {
302+
ws.sendText("message://upload file success");
303+
}
304+
}
305+
}
306+
255307
@Override
256308
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
257309
System.out.println("Server disconnected");

‎src/main/java/com/yeetor/androidcontrol/server/LocalServer.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.yeetor.androidcontrol.message.BinaryMessage;
99
import com.yeetor.androidcontrol.message.FileMessage;
1010
import com.yeetor.minicap.*;
11+
import com.yeetor.util.Constant;
12+
import com.yeetor.util.Util;
1113
import io.netty.bootstrap.ServerBootstrap;
1214
import io.netty.buffer.*;
1315
import io.netty.channel.*;
@@ -86,6 +88,11 @@ public void onDisconnect(ChannelHandlerContext ctx) {
8688
@Override
8789
public void onTextMessage(ChannelHandlerContext ctx, String text) {
8890
Command command = Command.ParseCommand(text);
91+
if (command.getSchem() != Command.Schem.WAITTING &&
92+
command.getSchem() != Command.Schem.INPUT &&
93+
command.getSchem() != Command.Schem.KEYEVENT) {
94+
System.out.println(command.getCommandString());
95+
}
8996
if (command != null) {
9097
switch (command.getSchem()) {
9198
case WAIT:
@@ -119,7 +126,7 @@ public void onBinaryMessage(ChannelHandlerContext ctx, byte[] data) {
119126

120127
if (message.getType().equals("file")) {
121128
FileMessage fileMessage = (FileMessage) message;
122-
File file = new File(fileMessage.name);
129+
File file = Constant.getTmpFile(fileMessage.name);
123130
if (fileMessage.offset == 0 && file.exists()) {
124131
file.delete();
125132
}

‎src/main/java/com/yeetor/androidcontrol/server/RemoteServer.java‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public void onDisconnect(ChannelHandlerContext ctx) {
7979
@Override
8080
public void onTextMessage(ChannelHandlerContext ctx, String text) {
8181
Command command = Command.ParseCommand(text);
82+
if (command.getSchem() != Command.Schem.WAITTING &&
83+
command.getSchem() != Command.Schem.INPUT &&
84+
command.getSchem() != Command.Schem.KEYEVENT &&
85+
command.getSchem() != Command.Schem.TOUCH) {
86+
System.out.println(command.getCommandString());
87+
}
88+
8289
if (command != null) {
8390
switch (command.getSchem()) {
8491
case WAIT:
@@ -96,6 +103,8 @@ public void onTextMessage(ChannelHandlerContext ctx, String text) {
96103
case DEVICES:
97104
case MINICAP:
98105
case MINITOUCH:
106+
case PUSH:
107+
case MESSAGE:
99108
forwardCommand(ctx, command);
100109
break;
101110
}
@@ -107,11 +116,7 @@ public void onTextMessage(ChannelHandlerContext ctx, String text) {
107116
@Override
108117
public void onBinaryMessage(ChannelHandlerContext ctx, byte[] data) {
109118
// 二进制直接转发
110-
Protocol protocol = findProtocolByClient(ctx);
111-
if (protocol == null || protocol.getBroswerSocket() == null) {
112-
return;
113-
}
114-
protocol.getBroswerSocket().channel().writeAndFlush(new BinaryWebSocketFrame(Unpooled.copiedBuffer(data)));
119+
forwardBinary(ctx, data);
115120
}
116121

117122
@Override
@@ -186,6 +191,26 @@ void forwardCommand(ChannelHandlerContext ctx, Command command) {
186191
sendTo.channel().writeAndFlush(new TextWebSocketFrame(command.getCommandString()));
187192
}
188193

194+
void forwardBinary(ChannelHandlerContext ctx, byte[] data) {
195+
Protocol protocol = findProtocolByBrowser(ctx);
196+
if (protocol == null) {
197+
protocol = findProtocolByClient(ctx);
198+
if (protocol == null) {
199+
return;
200+
}
201+
}
202+
203+
ChannelHandlerContext sendTo = null;
204+
205+
if (ctx == protocol.getBroswerSocket()) {
206+
sendTo = protocol.getClientSocket();
207+
} else {
208+
sendTo = protocol.getBroswerSocket();
209+
}
210+
211+
sendTo.channel().writeAndFlush(new BinaryWebSocketFrame(Unpooled.copiedBuffer(data)));
212+
}
213+
189214
DefaultFullHttpResponse onHttpGet(String uri) {
190215
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
191216
}

‎src/main/java/com/yeetor/minicap/Minicap.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public static void installMinicap(IDevice device) throws MinicapInstallException
7676

7777
// minicap.so
7878
File minicap_so = Constant.getMinicapSo(abi, sdk);
79-
System.out.println(minicap_bin);
8079
if (!minicap_so.exists()) {
8180
throw new MinicapInstallException("File: " + minicap_so.getAbsolutePath() + " not exists!");
8281
}
@@ -148,7 +147,6 @@ public String getMinicapCommand(int ow, int oh, int dw, int dh, int rotate, bool
148147
}
149148
}
150149
String command = StringUtils.join(commands, " ");
151-
System.out.println(command);
152150
return command;
153151
}
154152

‎src/main/java/com/yeetor/util/Constant.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ public static File getMinitouchBin(String abi) {
4949
return null;
5050
}
5151

52+
public static File getTmpFile(String fileName) {
53+
String tmpdir = System.getProperty("java.io.tmpdir");
54+
File tmp = new File(tmpdir);
55+
tmp = new File(tmp, "AndroidControl");
56+
if (!tmp.exists()) {
57+
tmp.mkdirs();
58+
}
59+
return new File(tmp, fileName);
60+
}
61+
5262
}

‎web/js/device.js‎

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var util = {
1515
},
1616
getMinicapRotate:function () {
1717
return localStorage.getItem("minicap_rotate");
18-
}
18+
},
19+
isRotate: false
1920
};
2021
var webSocket = null;
2122

@@ -298,6 +299,7 @@ $("#uploadtofile").on("click", function() {
298299
function requestStartMinicap() {
299300
var rotate = parseInt(util.getMinicapRotate());
300301
var scale = parseFloat(util.getMinicapScale());
302+
util.isRotate = (rotate === 90 || rotate === 270);
301303
webSocket.send("start://" + JSON.stringify({type:"minicap", config:{'rotate':rotate, 'scale':scale}}));
302304
}
303305

@@ -317,37 +319,59 @@ function sendKeyEvent(keyevent) {
317319
webSocket.send("keyevent://" + convertAndroidKeyCode(keyevent));
318320
}
319321

320-
canvas.onmousedown = function (event) {
321-
if (!util.serverConnected) {
322-
return;
322+
function sendDown(argx, argy, isRo) {
323+
var scalex = 1080.0 / canvas.width;
324+
var scaley = 1920.0 / canvas.height;
325+
var x = argx, y = argy;
326+
if (isRo) {
327+
x = (canvas.height - argy) * (canvas.width / canvas.height);
328+
y = argx * (canvas.height / canvas.width);
323329
}
324-
isDown = true;
330+
x = Math.round(x * scalex);
331+
y = Math.round(y * scaley);
332+
var command = "d 0 " + x + " " + y + " 50\n";
333+
command += "c\n";
334+
sendTouchEvent(command);
335+
}
325336

337+
function sendMove(argx, argy, isRo) {
326338
var scalex = 1080.0 / canvas.width;
327339
var scaley = 1920.0 / canvas.height;
328-
var pos = getXAndY(canvas, event);
329-
var x = Math.round(pos.x * scalex);
330-
var y = Math.round(pos.y * scaley);
340+
var x = argx, y = argy;
341+
if (isRo) {
342+
x = (canvas.height - argy) * (canvas.width / canvas.height);
343+
y = argx * (canvas.height / canvas.width);
344+
}
345+
x = Math.round(x * scalex);
346+
y = Math.round(y * scaley);
331347

332-
var command = "d 0 " + x + " " + y + " 50\n";
348+
var command = "m 0 " + x + " " + y + " 50\n";
349+
command += "c\n";
350+
sendTouchEvent(command);
351+
}
352+
353+
function sendUp() {
354+
var command = "u 0\n";
333355
command += "c\n";
334356
sendTouchEvent(command);
357+
}
358+
359+
canvas.onmousedown = function (event) {
360+
if (!util.serverConnected) {
361+
return;
362+
}
363+
isDown = true;
364+
var pos = getXAndY(canvas, event);
365+
sendDown(pos.x, pos.y, util.isRotate);
335366
};
336367

337368
canvas.onmousemove = function (event) {
338-
339369
if (!util.serverConnected || !isDown) {
340370
return;
341371
}
342-
var scalex = 1080.0 / canvas.width;
343-
var scaley = 1920.0 / canvas.height;
344372
var pos = getXAndY(canvas, event);
345-
var x = Math.round(pos.x * scalex);
346-
var y = Math.round(pos.y * scaley);
347373

348-
var command = "m 0 " + x + " " + y + " 50\n";
349-
command += "c\n";
350-
sendTouchEvent(command);
374+
sendMove(pos.x, pos.y, util.isRotate);
351375
};
352376

353377
canvas.onmouseover = function (event) {
@@ -359,29 +383,13 @@ canvas.onmouseout = function (event) {
359383
return;
360384
}
361385
isDown = false;
362-
var scalex = 1080.0 / canvas.width;
363-
var scaley = 1920.0 / canvas.height;
364-
var pos = getXAndY(canvas, event);
365-
var x = Math.round(pos.x * scalex);
366-
var y = Math.round(pos.y * scaley);
367-
368-
var command = "u 0\n";
369-
command += "c\n";
370-
sendTouchEvent(command);
386+
sendUp();
371387
};
372388

373389
canvas.onmouseup = function (event) {
374390
if (!util.serverConnected || !isDown) {
375391
return;
376392
}
377393
isDown = false;
378-
var scalex = 1080.0 / canvas.width;
379-
var scaley = 1920.0 / canvas.height;
380-
var pos = getXAndY(canvas, event);
381-
var x = Math.round(pos.x * scalex);
382-
var y = Math.round(pos.y * scaley);
383-
384-
var command = "u 0\n";
385-
command += "c\n";
386-
sendTouchEvent(command);
394+
sendUp();
387395
};

0 commit comments

Comments
 (0)