diff --git a/SharpAdbClient.Tests/DummyAdbClient.cs b/SharpAdbClient.Tests/DummyAdbClient.cs index 4cc6196f..07d05ac4 100644 --- a/SharpAdbClient.Tests/DummyAdbClient.cs +++ b/SharpAdbClient.Tests/DummyAdbClient.cs @@ -39,28 +39,7 @@ public int CreateForward(DeviceData device, string local, string remote, bool al public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellOutputReceiver receiver, CancellationToken cancellationToken, int maxTimeToOutputResponse) { - this.ReceivedCommands.Add(command); - - if (this.Commands.ContainsKey(command)) - { - if (receiver != null) - { - StringReader reader = new StringReader(this.Commands[command]); - - while (reader.Peek() != -1) - { - receiver.AddOutput(reader.ReadLine()); - } - - receiver.Flush(); - } - } - else - { - throw new ArgumentOutOfRangeException(nameof(command), $"The command '{command}' was unexpected"); - } - - return Task.FromResult(true); + return this.ExecuteRemoteCommandAsync(command, device, receiver, cancellationToken, maxTimeToOutputResponse, AdbClient.Encoding); } public int GetAdbVersion() @@ -150,7 +129,28 @@ public List GetFeatureSet(DeviceData device) public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellOutputReceiver receiver, CancellationToken cancellationToken, int maxTimeToOutputResponse, Encoding encoding) { - throw new NotImplementedException(); + this.ReceivedCommands.Add(command); + + if (this.Commands.ContainsKey(command)) + { + if (receiver != null) + { + StringReader reader = new StringReader(this.Commands[command]); + + while (reader.Peek() != -1) + { + receiver.AddOutput(encoding.GetString(encoding.GetBytes(reader.ReadLine()))); + } + + receiver.Flush(); + } + } + else + { + throw new ArgumentOutOfRangeException(nameof(command), $"The command '{command}' was unexpected"); + } + + return Task.FromResult(true); } } } diff --git a/SharpAdbClient/AdbClientExtensions.cs b/SharpAdbClient/AdbClientExtensions.cs index 443a7ba1..27e000ea 100644 --- a/SharpAdbClient/AdbClientExtensions.cs +++ b/SharpAdbClient/AdbClientExtensions.cs @@ -76,10 +76,10 @@ public static int CreateForward(this IAdbClient client, DeviceData device, int l /// /// The command to execute /// The device to execute on - /// The shell output receiver - public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver rcvr) + /// The shell output receiver + public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver receiver) { - ExecuteRemoteCommand(client, command, device, rcvr, AdbClient.Encoding); + ExecuteRemoteCommand(client, command, device, receiver, AdbClient.Encoding); } /// @@ -90,13 +90,13 @@ public static void ExecuteRemoteCommand(this IAdbClient client, string command, /// /// The command to execute /// The device to execute on - /// The shell output receiver + /// The shell output receiver /// The encoding to use. - public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver rcvr, Encoding encoding) + public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver receiver, Encoding encoding) { try { - client.ExecuteRemoteCommandAsync(command, device, rcvr, CancellationToken.None, int.MaxValue).Wait(); + client.ExecuteRemoteCommandAsync(command, device, receiver, CancellationToken.None, int.MaxValue, encoding).Wait(); } catch (AggregateException ex) { diff --git a/SharpAdbClient/DeviceCommands/DeviceExtensions.cs b/SharpAdbClient/DeviceCommands/DeviceExtensions.cs index c48b0b6e..3240aa28 100644 --- a/SharpAdbClient/DeviceCommands/DeviceExtensions.cs +++ b/SharpAdbClient/DeviceCommands/DeviceExtensions.cs @@ -2,6 +2,8 @@ // Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion. All rights reserved. // +using System.Threading.Tasks; + namespace SharpAdbClient.DeviceCommands { using Receivers; @@ -35,6 +37,27 @@ public static void ExecuteShellCommand(this DeviceData device, string command, I device.ExecuteShellCommand(AdbClient.Instance, command, receiver); } + /// + /// Executes a shell command on the device. + /// + /// + /// The device on which to run the command. + /// + /// + /// The command to execute. + /// + /// + /// Optionally, a that processes the command output. + /// + /// A that can be used to cancel the Task. + /// The max time to output response. + public static Task ExecuteShellCommandAsync( + this DeviceData device, string command, IShellOutputReceiver receiver, CancellationToken cancellationToken, + int maxTimeToOutputResponse = int.MaxValue) + { + return AdbClient.Instance.ExecuteRemoteCommandAsync(command, device, receiver, cancellationToken, maxTimeToOutputResponse); + } + /// /// Executes a shell command on the device. /// @@ -55,6 +78,30 @@ public static void ExecuteShellCommand(this DeviceData device, IAdbClient client client.ExecuteRemoteCommand(command, device, receiver); } + /// + /// Executes a shell command on the device. + /// + /// + /// The device on which to run the command. + /// + /// + /// The to use when executing the command. + /// + /// + /// The command to execute. + /// + /// + /// Optionally, a that processes the command output. + /// + /// A that can be used to cancel the Task. + /// The max time to output response. + public static Task ExecuteShellCommandAsync( + this DeviceData device, IAdbClient client, string command, IShellOutputReceiver receiver, CancellationToken cancellationToken, + int maxTimeToOutputResponse = int.MaxValue) + { + return client.ExecuteRemoteCommandAsync(command, device, receiver, cancellationToken, maxTimeToOutputResponse); + } + /// /// Gets the file statistics of a given file. ///