Skip to content
12 changes: 12 additions & 0 deletions src/Xamarin.Android.Tools.AndroidSdk/EnvironmentVariableNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@ internal static class EnvironmentVariableNames
/// Executable file extensions (Windows).
/// </summary>
public const string PathExt = "PATHEXT";

/// <summary>
/// Overrides the default location for Android user-specific data
/// (AVDs, preferences, etc.). Defaults to $HOME/.android.
/// </summary>
public const string AndroidUserHome = "ANDROID_USER_HOME";

/// <summary>
/// Overrides the AVD storage directory. Takes precedence over
/// <see cref="AndroidUserHome"/>/avd.
/// </summary>
public const string AndroidAvdHome = "ANDROID_AVD_HOME";
}
}
62 changes: 62 additions & 0 deletions src/Xamarin.Android.Tools.AndroidSdk/Models/AdbDeviceInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Xamarin.Android.Tools;

/// <summary>
/// Represents an Android device or emulator from 'adb devices -l' output.
/// Mirrors the metadata produced by dotnet/android's GetAvailableAndroidDevices task.
/// </summary>
public class AdbDeviceInfo
{
/// <summary>
/// Serial number of the device (e.g., "emulator-5554", "0A041FDD400327").
/// For non-running emulators, this is the AVD name.
/// </summary>
public string Serial { get; set; } = string.Empty;

/// <summary>
/// Human-friendly description of the device (e.g., "Pixel 7 API 35", "Pixel 6 Pro").
/// </summary>
public string Description { get; set; } = string.Empty;

/// <summary>
/// Device type: Device or Emulator.
/// </summary>
public AdbDeviceType Type { get; set; }

/// <summary>
/// Device status: Online, Offline, Unauthorized, NoPermissions, NotRunning, Unknown.
/// </summary>
public AdbDeviceStatus Status { get; set; }

/// <summary>
/// AVD name for emulators (e.g., "pixel_7_api_35"). Null for physical devices.
/// </summary>
public string? AvdName { get; set; }

/// <summary>
/// Device model from adb properties (e.g., "Pixel_6_Pro").
/// </summary>
public string? Model { get; set; }

/// <summary>
/// Product name from adb properties (e.g., "raven").
/// </summary>
public string? Product { get; set; }

/// <summary>
/// Device code name from adb properties (e.g., "raven").
/// </summary>
public string? Device { get; set; }

/// <summary>
/// Transport ID from adb properties.
/// </summary>
public string? TransportId { get; set; }

/// <summary>
/// Whether this device is an emulator.
/// </summary>
public bool IsEmulator => Type == AdbDeviceType.Emulator;
}
17 changes: 17 additions & 0 deletions src/Xamarin.Android.Tools.AndroidSdk/Models/AdbDeviceStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Xamarin.Android.Tools;

/// <summary>
/// Represents the status of an Android device.
/// </summary>
public enum AdbDeviceStatus
{
Online,
Offline,
Unauthorized,
NoPermissions,
NotRunning,
Unknown
}
13 changes: 13 additions & 0 deletions src/Xamarin.Android.Tools.AndroidSdk/Models/AdbDeviceType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Xamarin.Android.Tools;

/// <summary>
/// Represents the type of an Android device.
/// </summary>
public enum AdbDeviceType
{
Device,
Emulator
}
19 changes: 19 additions & 0 deletions src/Xamarin.Android.Tools.AndroidSdk/ProcessUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ static string JoinArguments (string[] args)
}
#endif

/// <summary>
/// Throws <see cref="InvalidOperationException"/> when <paramref name="exitCode"/> is non-zero.
/// Includes stderr/stdout context in the message when available.
/// </summary>
internal static void ThrowIfFailed (int exitCode, string command, string? stderr = null, string? stdout = null)
{
if (exitCode == 0)
return;

var message = $"'{command}' failed with exit code {exitCode}.";

if (!string.IsNullOrEmpty (stderr))
message += $" stderr:{Environment.NewLine}{stderr!.Trim ()}";
if (!string.IsNullOrEmpty (stdout))
message += $" stdout:{Environment.NewLine}{stdout!.Trim ()}";

throw new InvalidOperationException (message);
}

internal static IEnumerable<string> FindExecutablesInPath (string executable)
{
var path = Environment.GetEnvironmentVariable (EnvironmentVariableNames.Path) ?? "";
Expand Down
Loading