加强 MachineInfo 对 Android 的支持

This commit is contained in:
大石头 2022-04-24 11:34:13 +08:00
parent f9dc423c6a
commit e80c9b6edf
7 changed files with 114 additions and 15 deletions

View File

@ -87,6 +87,10 @@
<AndroidResource Include="Resources\drawable\icon_feed.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NewLife.Core\NewLife.Core.csproj">
<Project>{5813c22e-eeb3-4dee-a45c-bb218041193a}</Project>
<Name>NewLife.Core</Name>
</ProjectReference>
<ProjectReference Include="..\App1\App1.csproj">
<Project>{5E74306D-4BC7-4989-A59D-A0C0AF0DA15A}</Project>
<Name>App1</Name>

View File

@ -14,7 +14,7 @@ namespace App1.Droid
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.2.0.155")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.2.4.160")]
public partial class Resource
{

View File

@ -3,14 +3,16 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading.Tasks;
using NewLife.Collections;
using NewLife.Log;
using NewLife.Model;
using NewLife.Reflection;
using NewLife.Serialization;
using System.Net.NetworkInformation;
using NewLife.Collections;
namespace NewLife
{
@ -213,6 +215,8 @@ namespace NewLife
var str = GetLinuxName();
if (!str.IsNullOrEmpty()) OSName = str;
var android = ReadAndroidInfo();
// 树莓派的Hardware无法区分P0/P4
var dic = ReadInfo("/proc/cpuinfo");
if (dic != null)
@ -220,18 +224,38 @@ namespace NewLife
if (dic.TryGetValue("Hardware", out str) ||
dic.TryGetValue("cpu model", out str) ||
dic.TryGetValue("model name", out str))
Processor = str;
Processor = str?.TrimStart("vendor ");
if (dic.TryGetValue("Model", out str)) Product = str;
if (dic.TryGetValue("Serial", out str)) CpuID = str;
if (android.TryGetValue("Product", out str))
Product = str;
else if (dic.TryGetValue("vendor_id", out str))
Product = str;
else if (dic.TryGetValue("Model", out str))
Product = str;
//if (android.TryGetValue("Device", out str))
// CpuID = str;
if (dic.TryGetValue("Serial", out str))
CpuID = str;
}
var mid = "/etc/machine-id";
if (!File.Exists(mid)) mid = "/var/lib/dbus/machine-id";
if (TryRead(mid, out var value)) Guid = value;
if (TryRead(mid, out var value))
Guid = value;
//else if (android.TryGetValue("Serial", out str) && str != "unknown")
// Guid = str;
//else if (android.TryGetValue("Id", out str))
// Guid = str;
var file = "/sys/class/dmi/id/product_uuid";
if (TryRead(file, out value)) UUID = value;
if (!File.Exists(file)) file = "/proc/serial_num"; // miui12支持/proc/serial_num
if (TryRead(file, out value))
UUID = value;
else
// 支持 Android
UUID = ReadAndroidSecure("android_id");
file = "/sys/class/dmi/id/product_name";
if (TryRead(file, out value)) Product = value;
@ -443,7 +467,17 @@ namespace NewLife
if (TryRead(sr, out value)) return value?.SplitAsDictionary("=", "\n", true)["PRETTY_NAME"].Trim();
var uname = Execute("uname", "-sr")?.Trim();
if (!uname.IsNullOrEmpty()) return uname;
if (!uname.IsNullOrEmpty())
{
// 支持Android系统名
var ss = uname.Split('-');
foreach (var item in ss)
{
if (!item.IsNullOrEmpty() && item.StartsWithIgnoreCase("Android")) return item;
}
return uname;
}
return null;
}
@ -578,6 +612,34 @@ namespace NewLife
return dic2;
}
private static IDictionary<String, String> ReadAndroidInfo()
{
var dic = new Dictionary<String, String>();
if (!Runtime.Mono) return dic;
var type = "Android.OS.Build".GetTypeEx();
if (type == null) return dic;
foreach (var item in type.GetProperties(BindingFlags.Public | BindingFlags.Static))
{
dic[item.Name] = item.GetValue(null) + "";
}
return dic;
}
private static String ReadAndroidSecure(String name)
{
var type = "Android.Provider.Settings".GetTypeEx()?.GetNestedType("Secure");
if (type == null) return null;
//var aid = type.GetValue("AndroidId");
var resolver = "Android.App.Application".GetTypeEx()?.GetValue("Context")?.GetValue("ContentResolver");
if (resolver == null) return null;
return type.Invoke("GetString", resolver, name) as String;
}
#endregion
#region

View File

@ -1,4 +1,5 @@
using System;
using NewLife.Reflection;
using Android.App;
using Android.Content.PM;
@ -6,6 +7,7 @@ using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Runtime.InteropServices;
namespace MobileApp.Droid
{
@ -19,6 +21,26 @@ namespace MobileApp.Droid
base.OnCreate(savedInstanceState);
var id = Build.Id;
var serial = Build.Serial;
var model = Build.Model;
var prd = Build.Product;
var deviceId = Android.Provider.Settings.Secure.GetString(Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
var type = "Android.Provider.Settings".GetTypeEx();
type = type.GetNestedType("Secure");
var aid = type.GetValue("AndroidId");
var resolver = "Android.App.Application".GetTypeEx().GetValue("Context").GetValue("ContentResolver");
var did = type.Invoke("GetString", resolver, aid);
var str1 = RuntimeInformation.FrameworkDescription;
var str2 = RuntimeInformation.ProcessArchitecture;
var str3 = RuntimeInformation.OSArchitecture;
var str4 = RuntimeInformation.OSDescription;
//var osName = typeof(RuntimeInformation).Invoke("GetOSName") as String;
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());

View File

@ -32,6 +32,10 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<BundleAssemblies>false</BundleAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -92,6 +96,10 @@
<AndroidResource Include="Resources\drawable\xamarin_logo.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\NewLife.Core\NewLife.Core.csproj">
<Project>{5813c22e-eeb3-4dee-a45c-bb218041193a}</Project>
<Name>NewLife.Core</Name>
</ProjectReference>
<ProjectReference Include="..\MobileApp\MobileApp.csproj">
<Project>{A6D90DEE-201E-4329-BDB4-68767EE910A9}</Project>
<Name>MobileApp</Name>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.mobileapp">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="MobileApp.Android"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.newlife.mobileapp" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="MobileApp.Android"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

View File

@ -18,6 +18,9 @@ namespace MobileApp
XTrace.UseConsole(false, false);
#endif
var log = new NetworkLog { Server = "udp://255.255.255.255:514" };
XTrace.Log = new CompositeLog(XTrace.Log, log);
var js = MachineInfo.GetCurrent().ToJson(true);
XTrace.WriteLine(js);