SharpAdb / AdvancedSharpAdbClient

AdvancedSharpAdbClient is a .NET library that allows .NET, Mono and Unity applications to communicate with Android devices. It's improved version of SharpAdbClient.
https://sharpadb.github.io
Apache License 2.0
201 stars 54 forks source link

SendText works wierdly on special characters #82

Closed graysuit closed 1 year ago

graysuit commented 1 year ago

Describe the bug

Is that expected we need to escape special characters ? or may be better library should do itself to be safer, right ?

Steps to reproduce the bug

Sub Main()
    Dim client = New AdbClient()
    client.Connect("emulator-5554")
    Dim device = client.GetDevices().FirstOrDefault()
    Console.WriteLine(device.Model)

    PassElement.ClearInput()
    PassElement.SendText("#")     'Result = AdvancedSharpAdbClient.Exceptions.InvalidTextException: Text contains invalid symbols
    PassElement.SendText("\#")    'Result = #
    PassElement.SendText("$")     'Result = $
    PassElement.SendText("a$a")   'Result = a
    PassElement.SendText("a\$a")  'Result = a$a
End Sub

Expected behavior

No response

Screenshots

No response

NuGet package version

Latest Source

.NET Platform

.NET Framework 4.6.x

Platform type

Windows

System version

windows 10

IDE

Visual Studio 2017

Additional context

No response

graysuit commented 1 year ago

Currently I use this as workaround:

Function Escape(Str As String) As String
    Dim RestrictedChars = "~@#$%^&*()_+=-!".ToCharArray()
    For Each R In RestrictedChars
        Str = Str.Replace(R.ToString(), $"\{ R.ToString()}")
    Next
    Return Str
End Function
PassElement.SendText(Escape("~!@#$%^&*()_+=-"))
wherewhere commented 1 year ago

Because it is send command to Android Shell. So these spaical characters should add \ to escape...

graysuit commented 1 year ago

Per my analysis this is adb issue. Currently using ADbkeybaord as helper. It works.

Function SendText(client As AdbClient, device As DeviceData, Text As String)
    Return RunAdb(client, device, $"am broadcast -a ADB_INPUT_CHARS --eia chars '{ConvertToUnicode(Text)}'")
End Function

Function ConvertToUnicode(Input As String) As String
    Dim Output = String.Empty
    For Each c As Char In Input
        Output += AscW(c).ToString() + ","
    Next
    Return Output.TrimEnd(","c)
End Function

Function RunAdb(client As AdbClient, device As DeviceData, cmd As String) As String
    Dim receiver As New ConsoleOutputReceiver()
    client.ExecuteRemoteCommand(cmd, device, receiver)
    Dim Out = receiver.ToString()
    receiver.Flush()
    Return Out
End Function

Thank so much for great work!