2026-10-15 08:08:37 +02:00
|
|
|
using Android.App;
|
2026-10-15 04:39:03 +02:00
|
|
|
using Android.Content;
|
2026-10-15 08:08:37 +02:00
|
|
|
using Android.OS;
|
2026-10-15 04:39:03 +02:00
|
|
|
using Android.Net;
|
|
|
|
|
using Android.Net.Wifi;
|
|
|
|
|
|
|
|
|
|
using MeWeb.Core.Net;
|
|
|
|
|
|
|
|
|
|
namespace MeWeb.Android;
|
|
|
|
|
|
|
|
|
|
public class AndroidPlatform : SystemPlatform
|
|
|
|
|
{
|
|
|
|
|
static Context Context => global::Android.App.Application.Context;
|
|
|
|
|
|
|
|
|
|
public override DeviceKind Device => DeviceKind.Phone;
|
|
|
|
|
|
2026-10-15 05:41:44 +02:00
|
|
|
public override string Name => "android";
|
|
|
|
|
|
|
|
|
|
public override bool? HasBattery => true;
|
|
|
|
|
|
2026-10-15 08:08:37 +02:00
|
|
|
public override string SystemName => $"Android {Build.VERSION.Release} (API {(int)Build.VERSION.SdkInt}, {System.Runtime.InteropServices.RuntimeInformation.OSArchitecture})";
|
|
|
|
|
|
|
|
|
|
public override string Model
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var maker = Build.Manufacturer?.Trim() ?? "";
|
|
|
|
|
var model = Build.Model?.Trim() ?? "";
|
|
|
|
|
if (model.Length == 0) return null;
|
|
|
|
|
if (maker.Length == 0 || model.StartsWith(maker, StringComparison.OrdinalIgnoreCase)) return model;
|
|
|
|
|
return $"{char.ToUpperInvariant(maker[0])}{maker[1..]} {model}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override long MemoryBytes
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Context.GetSystemService(Context.ActivityService) is not ActivityManager activities) return base.MemoryBytes;
|
|
|
|
|
var memory = new ActivityManager.MemoryInfo();
|
|
|
|
|
activities.GetMemoryInfo(memory);
|
|
|
|
|
return memory.TotalMem > 0 ? memory.TotalMem : base.MemoryBytes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 05:41:44 +02:00
|
|
|
public override bool ShareLink(string url, string title)
|
|
|
|
|
{
|
|
|
|
|
var send = new Intent(Intent.ActionSend);
|
|
|
|
|
send.SetType("text/plain");
|
|
|
|
|
send.PutExtra(Intent.ExtraText, url);
|
|
|
|
|
send.PutExtra(Intent.ExtraSubject, title);
|
|
|
|
|
var chooser = Intent.CreateChooser(send, title);
|
|
|
|
|
if (chooser is null) return false;
|
|
|
|
|
chooser.AddFlags(ActivityFlags.NewTask);
|
2026-10-15 09:10:37 +02:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Context.StartActivity(chooser);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (ActivityNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-10-15 05:41:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-15 04:39:03 +02:00
|
|
|
public override IDisposable AllowMulticast()
|
|
|
|
|
{
|
|
|
|
|
if (Context.GetSystemService(Context.WifiService) is not WifiManager wifi) return base.AllowMulticast();
|
|
|
|
|
var multicast = wifi.CreateMulticastLock("meweb");
|
|
|
|
|
multicast.SetReferenceCounted(false);
|
|
|
|
|
multicast.Acquire();
|
|
|
|
|
return new Held(multicast);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override LocalSnapshot ReadNetwork()
|
|
|
|
|
{
|
|
|
|
|
var snapshot = new LocalSnapshot();
|
|
|
|
|
if (Context.GetSystemService(Context.ConnectivityService) is not ConnectivityManager connectivity) return base.ReadNetwork();
|
|
|
|
|
var network = connectivity.ActiveNetwork;
|
|
|
|
|
if (network is null) return snapshot;
|
|
|
|
|
var link = connectivity.GetLinkProperties(network);
|
|
|
|
|
var capabilities = connectivity.GetNetworkCapabilities(network);
|
|
|
|
|
if (link is null) return base.ReadNetwork();
|
|
|
|
|
|
|
|
|
|
var item = new LocalInterface
|
|
|
|
|
{
|
|
|
|
|
Name = link.InterfaceName,
|
|
|
|
|
Kind = capabilities switch
|
|
|
|
|
{
|
|
|
|
|
not null when capabilities.HasTransport(TransportType.Wifi) => LinkKind.Wifi,
|
|
|
|
|
not null when capabilities.HasTransport(TransportType.Cellular) => LinkKind.Mobile,
|
|
|
|
|
not null when capabilities.HasTransport(TransportType.Ethernet) => LinkKind.Cable,
|
|
|
|
|
_ => LinkKind.Unknown,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
foreach (var address in link.LinkAddresses)
|
|
|
|
|
{
|
|
|
|
|
var text = Usable(address.Address?.HostAddress);
|
|
|
|
|
if (text is not null) item.Addresses.Add(text);
|
|
|
|
|
}
|
|
|
|
|
foreach (var route in link.Routes)
|
|
|
|
|
{
|
|
|
|
|
var gateway = route.Gateway?.HostAddress;
|
|
|
|
|
if (!route.IsDefaultRoute || gateway is null || gateway is "0.0.0.0" or "::") continue;
|
|
|
|
|
if (!item.Gateways.Contains(gateway)) item.Gateways.Add(gateway);
|
|
|
|
|
}
|
|
|
|
|
item.Gateways = item.Gateways.OrderBy(g => g.Contains(':') ? 1 : 0).ToList();
|
|
|
|
|
if (item.Kind == LinkKind.Wifi) item.SpeedMbps = WifiSpeed(capabilities);
|
|
|
|
|
snapshot.Interfaces.Add(item);
|
|
|
|
|
snapshot.Dns = link.DnsServers.Select(d => d.HostAddress).Where(d => d is not null).Distinct().ToList();
|
|
|
|
|
return snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string Usable(string text)
|
|
|
|
|
{
|
|
|
|
|
var address = Addresses.Parse(text?.Split('%')[0]);
|
|
|
|
|
if (address is null) return null;
|
|
|
|
|
return Addresses.Classify(address) is AddressKind.Loopback or AddressKind.LinkLocal or AddressKind.Unspecified ? null : address.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static long WifiSpeed(NetworkCapabilities capabilities)
|
|
|
|
|
{
|
2026-10-15 05:41:44 +02:00
|
|
|
if (!OperatingSystem.IsAndroidVersionAtLeast(29))
|
|
|
|
|
return Context.GetSystemService(Context.WifiService) is WifiManager wifi ? Math.Max(0, wifi.ConnectionInfo?.LinkSpeed ?? 0) : 0;
|
|
|
|
|
return capabilities?.TransportInfo is WifiInfo info ? Math.Max(0, info.LinkSpeed) : 0;
|
2026-10-15 04:39:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed class Held(WifiManager.MulticastLock multicast) : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (multicast.IsHeld) multicast.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|