How perform a drag (based in X,Y mouse coordinates) on Android using AccessibilityService?

后端 未结 2 566
执笔经年
执笔经年 2021-02-01 13:14

I want know how to perform a drag on android based in X, Y mouse coordinates? consider as two simple examples, the Team Viewer/QuickSupport drawing the \"password pattern\" on r

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 14:08

    Here is a example of a solution based on Edit 3 of question.


    C# Windows Froms Application "formMain.cs":

    using System.Net.Sockets;
    
    private List lstPoints;
    
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    {
        if (e.Button == MouseButtons.Left)
        {
            lstPoints = new List();
            lstPoints.Add(new Point(e.X, e.Y));
        }
    }
    
    private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            lstPoints.Add(new Point(e.X, e.Y));
        }
    }
    
    private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        lstPoints.Add(new Point(e.X, e.Y));
    
        StringBuilder sb = new StringBuilder();
    
        foreach (Point obj in lstPoints)
        {
            sb.Append(Convert.ToString(obj) + ":");
        }
    
        serverSocket.Send("MDRAWEVENT" + sb.ToString() + Environment.NewLine);
    }
    

    android service "SocketBackground.java":

    import java.net.Socket;
    
    String xline;
    
    while (clientSocket.isConnected()) {
    
        BufferedReader xreader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
    
        if (xreader.ready()) {
    
            while ((xline = xreader.readLine()) != null) {
                    xline = xline.trim();
    
                if (xline != null && !xline.trim().isEmpty()) {
    
                    if (xline.contains("MDRAWEVENT")) {
    
                        String coordinates = xline.replace("MDRAWEVENT", "");
                        String[] tokens = coordinates.split(Pattern.quote(":"));
                        Point[] moviments = new Point[tokens.length];
    
                        for (int i = 0; i < tokens.length; i++) {
                           String[] coordinates = tokens[i].replace("{", "").replace("}", "").split(",");
    
                           int x = Integer.parseInt(coordinates[0].split("=")[1]);
                           int y = Integer.parseInt(coordinates[1].split("=")[1]);
    
                           moviments[i] = new Point(x, y);
                        }
    
                        MyAccessibilityService.instance.mouseDraw(moviments, 2000);
                    }
                }
            }
        }
    }
    

    android AccessibilityService "MyAccessibilityService.java":

    public void mouseDraw(Point[] segments, int time) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
            Path path = new Path();
            path.moveTo(segments[0].x, segments[0].y);
    
            for (int i = 1; i < segments.length; i++) {
    
                path.lineTo(segments[i].x, segments[i].y);
    
                GestureDescription.StrokeDescription sd = new GestureDescription.StrokeDescription(path, 0, time);
    
                dispatchGesture(new GestureDescription.Builder().addStroke(sd).build(), new AccessibilityService.GestureResultCallback() {
    
                    @Override
                    public void onCompleted(GestureDescription gestureDescription) {
                        super.onCompleted(gestureDescription);
                    }
    
                    @Override
                    public void onCancelled(GestureDescription gestureDescription) {
                        super.onCancelled(gestureDescription);
                    }
                }, null);
            }
        }
    }
    

提交回复
热议问题