55using System ;
66using System . Collections . Generic ;
77using System . Diagnostics ;
8+ using System . Globalization ;
89using System . IO ;
910using System . Linq ;
1011using System . Runtime . InteropServices ;
@@ -21,9 +22,18 @@ private enum AdbProperty
2122 {
2223 Architecture ,
2324 ApiVersion ,
25+ BuildFingerprint ,
2426 SupportedArchitectures ,
2527 InstalledApps ,
2628 BootCompletion ,
29+ CpuInfo ,
30+ CpuMaxFrequency ,
31+ CpuMaxFrequencyFallback ,
32+ Manufacturer ,
33+ MemInfo ,
34+ Model ,
35+ OperatingSystemVersion ,
36+ ProductName ,
2737 }
2838
2939 #region Constructor and state variables
@@ -33,8 +43,17 @@ private enum AdbProperty
3343 { AdbProperty . SupportedArchitectures , new [ ] { "shell" , "getprop" , "ro.product.cpu.abilist" } } ,
3444 { AdbProperty . ApiVersion , new [ ] { "shell" , "getprop" , "ro.build.version.sdk" } } ,
3545 { AdbProperty . Architecture , new [ ] { "shell" , "getprop" , "ro.product.cpu.abi" } } ,
46+ { AdbProperty . BuildFingerprint , new [ ] { "shell" , "getprop" , "ro.build.fingerprint" } } ,
3647 { AdbProperty . InstalledApps , new [ ] { "shell" , "pm" , "list" , "packages" , "-3" } } ,
3748 { AdbProperty . BootCompletion , new [ ] { "shell" , "getprop" , "sys.boot_completed" } } ,
49+ { AdbProperty . CpuInfo , new [ ] { "shell" , "cat" , "/proc/cpuinfo" } } ,
50+ { AdbProperty . CpuMaxFrequency , new [ ] { "shell" , "cat" , "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" } } ,
51+ { AdbProperty . CpuMaxFrequencyFallback , new [ ] { "shell" , "cat" , "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" } } ,
52+ { AdbProperty . Manufacturer , new [ ] { "shell" , "getprop" , "ro.product.manufacturer" } } ,
53+ { AdbProperty . MemInfo , new [ ] { "shell" , "cat" , "/proc/meminfo" } } ,
54+ { AdbProperty . Model , new [ ] { "shell" , "getprop" , "ro.product.model" } } ,
55+ { AdbProperty . OperatingSystemVersion , new [ ] { "shell" , "getprop" , "ro.build.version.release" } } ,
56+ { AdbProperty . ProductName , new [ ] { "shell" , "getprop" , "ro.product.name" } } ,
3857 } ;
3958
4059 private const string AdbEnvironmentVariableName = "ADB_EXE_PATH" ;
@@ -92,6 +111,28 @@ public AdbRunner(ILogger log, IAdbProcessManager processManager, string adbExePa
92111 }
93112 }
94113
114+ public void PopulateEnvironmentInfo ( AndroidDevice device )
115+ {
116+ if ( device is null )
117+ {
118+ throw new ArgumentNullException ( nameof ( device ) ) ;
119+ }
120+
121+ device . Manufacturer ??= GetDeviceProperty ( AdbProperty . Manufacturer , device . DeviceSerial ) ;
122+ device . Model ??= GetDeviceProperty ( AdbProperty . Model , device . DeviceSerial ) ;
123+ device . ProductName ??= GetDeviceProperty ( AdbProperty . ProductName , device . DeviceSerial ) ;
124+ device . OperatingSystemVersion ??= GetDeviceProperty ( AdbProperty . OperatingSystemVersion , device . DeviceSerial ) ;
125+ device . BuildFingerprint ??= GetDeviceProperty ( AdbProperty . BuildFingerprint , device . DeviceSerial ) ;
126+ device . Architecture ??= GetDeviceProperty ( AdbProperty . Architecture , device . DeviceSerial ) ;
127+ device . SupportedArchitectures ??= GetDeviceProperty ( AdbProperty . SupportedArchitectures , device . DeviceSerial ) ? . Split ( new [ ] { ',' , '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) ;
128+ device . CpuModel ??= ParseCpuModel ( GetDeviceProperty ( AdbProperty . CpuInfo , device . DeviceSerial ) ) ;
129+ device . TotalMemoryBytes ??= ParseTotalMemoryBytes ( GetDeviceProperty ( AdbProperty . MemInfo , device . DeviceSerial ) ) ;
130+ device . CpuMaxFrequencyKiloHertz ??=
131+ ParseFrequencyKiloHertz ( GetDeviceProperty ( AdbProperty . CpuMaxFrequency , device . DeviceSerial , logOnError : false ) )
132+ ?? ParseFrequencyKiloHertz ( GetDeviceProperty ( AdbProperty . CpuMaxFrequencyFallback , device . DeviceSerial , logOnError : false ) )
133+ ?? ParseCpuFrequencyFromCpuInfoKiloHertz ( GetDeviceProperty ( AdbProperty . CpuInfo , device . DeviceSerial ) ) ;
134+ }
135+
95136 private static string GetCliAdbExePath ( )
96137 {
97138 var currentAssemblyDirectory = Path . GetDirectoryName ( typeof ( AdbRunner ) . Assembly . Location ) ;
@@ -1224,7 +1265,7 @@ private IReadOnlyCollection<AndroidDevice> GetDevices(params AdbProperty[] prope
12241265 return devices ;
12251266 }
12261267
1227- private string ? GetDeviceProperty ( AdbProperty property , string ? deviceName = null )
1268+ private string ? GetDeviceProperty ( AdbProperty property , string ? deviceName = null , bool logOnError = true )
12281269 {
12291270 IEnumerable < string > args = s_commandList [ property ] ;
12301271
@@ -1251,15 +1292,135 @@ private IReadOnlyCollection<AndroidDevice> GetDevices(params AdbProperty[] prope
12511292
12521293 if ( ! result . Succeeded )
12531294 {
1254- _log . LogError ( $ "Failed to get device's property { property } . Check if a device is attached / emulator is started" +
1255- Environment . NewLine + result . StandardError ) ;
1295+ if ( logOnError )
1296+ {
1297+ _log . LogError ( $ "Failed to get device's property { property } . Check if a device is attached / emulator is started" +
1298+ Environment . NewLine + result . StandardError ) ;
1299+ }
12561300
12571301 return null ;
12581302 }
12591303
12601304 return result . StandardOutput . Trim ( ) ;
12611305 }
12621306
1307+ internal static string ? ParseCpuModel ( string ? cpuInfo )
1308+ {
1309+ if ( string . IsNullOrWhiteSpace ( cpuInfo ) )
1310+ {
1311+ return null ;
1312+ }
1313+
1314+ foreach ( var line in cpuInfo . Split ( new [ ] { '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
1315+ {
1316+ if ( TryParseColonValue ( line , "Hardware" , out var hardware ) )
1317+ {
1318+ return hardware ;
1319+ }
1320+
1321+ if ( TryParseColonValue ( line , "model name" , out var modelName ) )
1322+ {
1323+ return modelName ;
1324+ }
1325+
1326+ if ( TryParseColonValue ( line , "Processor" , out var processor ) )
1327+ {
1328+ return processor ;
1329+ }
1330+ }
1331+
1332+ return null ;
1333+ }
1334+
1335+ internal static long ? ParseCpuFrequencyFromCpuInfoKiloHertz ( string ? cpuInfo )
1336+ {
1337+ if ( string . IsNullOrWhiteSpace ( cpuInfo ) )
1338+ {
1339+ return null ;
1340+ }
1341+
1342+ foreach ( var line in cpuInfo . Split ( new [ ] { '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
1343+ {
1344+ if ( TryParseColonValue ( line , "cpu MHz" , out var cpuFrequency ) )
1345+ {
1346+ return double . TryParse ( cpuFrequency , NumberStyles . Float , CultureInfo . InvariantCulture , out var megaHertz )
1347+ ? ( long ) ( megaHertz * 1000 )
1348+ : null ;
1349+ }
1350+ }
1351+
1352+ return null ;
1353+ }
1354+
1355+ internal static long ? ParseFrequencyKiloHertz ( string ? value )
1356+ {
1357+ if ( string . IsNullOrWhiteSpace ( value ) )
1358+ {
1359+ return null ;
1360+ }
1361+
1362+ var trimmed = value . Trim ( ) ;
1363+ if ( long . TryParse ( trimmed . Split ( ' ' , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] , out var kiloHertz ) )
1364+ {
1365+ return kiloHertz ;
1366+ }
1367+
1368+ if ( trimmed . EndsWith ( "MHz" , StringComparison . OrdinalIgnoreCase )
1369+ && double . TryParse ( trimmed . Replace ( "MHz" , "" , StringComparison . OrdinalIgnoreCase ) . Trim ( ) , out var megaHertz ) )
1370+ {
1371+ return ( long ) ( megaHertz * 1000 ) ;
1372+ }
1373+
1374+ if ( trimmed . EndsWith ( "GHz" , StringComparison . OrdinalIgnoreCase )
1375+ && double . TryParse ( trimmed . Replace ( "GHz" , "" , StringComparison . OrdinalIgnoreCase ) . Trim ( ) , out var gigaHertz ) )
1376+ {
1377+ return ( long ) ( gigaHertz * 1_000_000 ) ;
1378+ }
1379+
1380+ return null ;
1381+ }
1382+
1383+ internal static long ? ParseTotalMemoryBytes ( string ? memInfo )
1384+ {
1385+ if ( string . IsNullOrWhiteSpace ( memInfo ) )
1386+ {
1387+ return null ;
1388+ }
1389+
1390+ foreach ( var line in memInfo . Split ( new [ ] { '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
1391+ {
1392+ if ( ! TryParseColonValue ( line , "MemTotal" , out var totalMemory ) )
1393+ {
1394+ continue ;
1395+ }
1396+
1397+ if ( long . TryParse ( totalMemory . Split ( ' ' , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] , out var kiloBytes ) )
1398+ {
1399+ return kiloBytes * 1024 ;
1400+ }
1401+ }
1402+
1403+ return null ;
1404+ }
1405+
1406+ private static bool TryParseColonValue ( string line , string key , out string value )
1407+ {
1408+ value = string . Empty ;
1409+ if ( ! line . StartsWith ( key , StringComparison . OrdinalIgnoreCase ) )
1410+ {
1411+ return false ;
1412+ }
1413+
1414+ var separatorIndex = line . IndexOf ( ':' ) ;
1415+ if ( separatorIndex < 0 || separatorIndex == line . Length - 1 )
1416+ {
1417+ return false ;
1418+ }
1419+
1420+ value = line . Substring ( separatorIndex + 1 ) . Trim ( ) ;
1421+ return true ;
1422+ }
1423+
12631424 private bool TestFileExists ( string path , string ? deviceName = null )
12641425 {
12651426 var deviceTestPath = GlobalReadWriteDirectory + Path . AltDirectorySeparatorChar + new DirectoryInfo ( path ) . Name ;
0 commit comments