Skip to content
This repository was archived by the owner on Dec 31, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions SharpAdbClient.Tests/DummyAdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -150,7 +129,28 @@ public List<string> 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);
}
}
}
12 changes: 6 additions & 6 deletions SharpAdbClient/AdbClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public static int CreateForward(this IAdbClient client, DeviceData device, int l
/// </param>
/// <param name="command">The command to execute</param>
/// <param name="device">The device to execute on</param>
/// <param name="rcvr">The shell output receiver</param>
public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver rcvr)
/// <param name="receiver">The shell output receiver</param>
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);
}

/// <summary>
Expand All @@ -90,13 +90,13 @@ public static void ExecuteRemoteCommand(this IAdbClient client, string command,
/// </param>
/// <param name="command">The command to execute</param>
/// <param name="device">The device to execute on</param>
/// <param name="rcvr">The shell output receiver</param>
/// <param name="receiver">The shell output receiver</param>
/// <param name="encoding">The encoding to use.</param>
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)
{
Expand Down
47 changes: 47 additions & 0 deletions SharpAdbClient/DeviceCommands/DeviceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion. All rights reserved.
// </copyright>

using System.Threading.Tasks;

namespace SharpAdbClient.DeviceCommands
{
using Receivers;
Expand Down Expand Up @@ -35,6 +37,27 @@ public static void ExecuteShellCommand(this DeviceData device, string command, I
device.ExecuteShellCommand(AdbClient.Instance, command, receiver);
}

/// <summary>
/// Executes a shell command on the device.
/// </summary>
/// <param name="device">
/// The device on which to run the command.
/// </param>
/// <param name="command">
/// The command to execute.
/// </param>
/// <param name="receiver">
/// Optionally, a <see cref="IShellOutputReceiver"/> that processes the command output.
/// </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the Task.</param>
/// <param name="maxTimeToOutputResponse">The max time to output response.</param>
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);
}

/// <summary>
/// Executes a shell command on the device.
/// </summary>
Expand All @@ -55,6 +78,30 @@ public static void ExecuteShellCommand(this DeviceData device, IAdbClient client
client.ExecuteRemoteCommand(command, device, receiver);
}

/// <summary>
/// Executes a shell command on the device.
/// </summary>
/// <param name="device">
/// The device on which to run the command.
/// </param>
/// <param name="client">
/// The <see cref="IAdbClient"/> to use when executing the command.
/// </param>
/// <param name="command">
/// The command to execute.
/// </param>
/// <param name="receiver">
/// Optionally, a <see cref="IShellOutputReceiver"/> that processes the command output.
/// </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the Task.</param>
/// <param name="maxTimeToOutputResponse">The max time to output response.</param>
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);
}

/// <summary>
/// Gets the file statistics of a given file.
/// </summary>
Expand Down