Skip to content

Commit 6e91776

Browse files
[native, Mono.Android] Remove internal/obsolete System.AndroidPlatform methods (#10385)
Fixes: #10381 This PR removes obsolete managed code that references `System.AndroidPlatform`, which was part of the Xamarin.Android runtime but was never used in modern .NET. The issue was identified by @filipnavara and confirmed safe to remove by @jonathanpeppers after verifying that these APIs don't exist in dotnet/runtime. ## Changes Made **Removed Methods from AndroidEnvironment.cs:** - `GetDisplayDPI()` - Previously called by `System.Drawing.GraphicsAndroid.FromAndroidSurface()` (already marked as obsolete for android31.0) - `GetDefaultTimeZone()` - Previously called by `System.Core!System.AndroidPlatform.GetDefaultTimeZone()` - `GetDefaultSyncContext()` - Previously called by `mscorlib.dll!System.AndroidPlatform.GetDefaultSyncContext()` - `GetInterfaceAddresses()` - Previously called by `System.dll!System.AndroidPlatform.getifaddrs` - `FreeInterfaceAddresses()` - Previously called by `System.dll!System.AndroidPlatform.freeifaddrs` - `GetDefaultProxy()` - Previously called by `System.dll!System.AndroidPlatform.GetDefaultProxy()` **Additional Cleanup:** - Removed the `_Proxy` class that was only used by `GetDefaultProxy()` - Removed unused P/Invoke declarations from `RuntimeNativeMethods.cs`: - `_monodroid_timezone_get_default_id()` - `_monodroid_getifaddrs()` - `_monodroid_freeifaddrs()` **Native Code Cleanup:** - Removed P/Invoke method entries from both CLR and Mono P/Invoke table generators - Regenerated P/Invoke tables using `generate-pinvoke-tables.sh` - Removed unused native C++ implementations: - `_monodroid_timezone_get_default_id()` functions (CLR & Mono) - Complete `xamarin_getifaddrs.cc` files and header (CLR & Mono - 2,200+ lines) - Updated CMakeLists.txt files to remove build references - Fixed linker error by removing obsolete symbols from Mono version script ## Impact This is a **breaking change** that removes internal APIs that were never intended for public use in modern .NET for Android. No external code should be affected as these were internal implementation details for obsolete `System.AndroidPlatform` methods. Co-authored-by: Jonathan Peppers <[email protected]>
1 parent ca8c3a2 commit 6e91776

18 files changed

+6
-2464
lines changed

src/Mono.Android/Android.Runtime/AndroidEnvironment.cs

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -228,85 +228,6 @@ static void NotifyTimeZoneChanged ()
228228
}
229229
}
230230

231-
// This is invoked by
232-
// System.Drawing!System.Drawing.GraphicsAndroid.FromAndroidSurface ()
233-
// DO NOT REMOVE
234-
//
235-
// Exception audit:
236-
//
237-
// Verdict
238-
// No need to wrap thrown exceptions in a BCL class
239-
//
240-
// Rationale
241-
// No longer called by the indicated caller, however we keep it for backward compatibility.
242-
[global::System.Runtime.Versioning.ObsoletedOSPlatform ("android31.0")]
243-
static void GetDisplayDPI (out float x_dpi, out float y_dpi)
244-
{
245-
var wm = Application.Context.GetSystemService (Context.WindowService).JavaCast <IWindowManager> ();
246-
var metrics = new DisplayMetrics ();
247-
#if ANDROID_17
248-
wm?.DefaultDisplay?.GetRealMetrics (metrics);
249-
#else
250-
wm.DefaultDisplay.GetMetrics (metrics);
251-
#endif
252-
x_dpi = metrics.Xdpi;
253-
y_dpi = metrics.Ydpi;
254-
}
255-
256-
// This is invoked by
257-
// System.Core!System.AndroidPlatform.GetDefaultTimeZone ()
258-
// DO NOT REMOVE
259-
//
260-
// Exception audit:
261-
//
262-
// Verdict
263-
// No need to wrap thrown exceptions in a BCL class
264-
//
265-
// Rationale
266-
// Java code (invoked from our native runtime) will always return the default timezone and no
267-
// exceptions are documented for the Java API.
268-
//
269-
static string GetDefaultTimeZone ()
270-
{
271-
IntPtr id = RuntimeNativeMethods._monodroid_timezone_get_default_id ();
272-
try {
273-
return Marshal.PtrToStringAnsi (id)!;
274-
} finally {
275-
RuntimeNativeMethods.monodroid_free (id);
276-
}
277-
}
278-
279-
// This is invoked by
280-
// mscorlib.dll!System.AndroidPlatform.GetDefaultSyncContext()
281-
// DO NOT REMOVE
282-
static SynchronizationContext? GetDefaultSyncContext ()
283-
{
284-
var looper = Android.OS.Looper.MainLooper;
285-
try {
286-
if (Android.OS.Looper.MyLooper() == looper)
287-
return Android.App.Application.SynchronizationContext;
288-
} catch (System.Exception ex) {
289-
Logger.Log (LogLevel.Warn, "MonoAndroid", $"GetDefaultSyncContext caught a Java exception: {ex}");
290-
}
291-
return null;
292-
}
293-
294-
// These are invoked by
295-
// System.dll!System.AndroidPlatform.getifaddrs
296-
// DO NOT REMOVE
297-
static int GetInterfaceAddresses (out IntPtr ifap)
298-
{
299-
return RuntimeNativeMethods._monodroid_getifaddrs (out ifap);
300-
}
301-
302-
// These are invoked by
303-
// System.dll!System.AndroidPlatform.freeifaddrs
304-
// DO NOT REMOVE
305-
static void FreeInterfaceAddresses (IntPtr ifap)
306-
{
307-
RuntimeNativeMethods._monodroid_freeifaddrs (ifap);
308-
}
309-
310231
static void DetectCPUAndArchitecture (out ushort builtForCPU, out ushort runningOnCPU, out bool is64bit)
311232
{
312233
ushort built_for_cpu = 0;
@@ -319,18 +240,6 @@ static void DetectCPUAndArchitecture (out ushort builtForCPU, out ushort running
319240
is64bit = _is64bit != 0;
320241
}
321242

322-
// This is invoked by
323-
// System.dll!System.AndroidPlatform.GetDefaultProxy()
324-
// DO NOT REMOVE
325-
static IWebProxy GetDefaultProxy ()
326-
{
327-
#if ANDROID_14
328-
return new _Proxy ();
329-
#else
330-
return null;
331-
#endif
332-
}
333-
334243
// This is invoked by
335244
// System.Net.Http.dll!System.Net.Http.HttpClient.cctor
336245
// DO NOT REMOVE
@@ -395,61 +304,5 @@ static Type GetFallbackHttpMessageHandlerType ()
395304
return handlerType;
396305
}
397306

398-
class _Proxy : IWebProxy {
399-
readonly ProxySelector selector = ProxySelector.Default!;
400-
401-
// Exception audit:
402-
//
403-
// Verdict
404-
// Exception wrapping required
405-
//
406-
// Rationale
407-
// Java code may throw URISyntaxException which we map to the managed UriFormatException
408-
//
409-
static URI CreateJavaUri (Uri destination)
410-
{
411-
try {
412-
return new URI (destination.Scheme, destination.UserInfo, destination.Host, destination.Port, destination.AbsolutePath, destination.Query, destination.Fragment);
413-
} catch (Java.Lang.Throwable ex) when (JNIEnv.ShouldWrapJavaException (ex)) {
414-
throw new UriFormatException (ex.Message, ex);
415-
}
416-
}
417-
418-
public Uri GetProxy (Uri destination)
419-
{
420-
IList<Java.Net.Proxy> list;
421-
using (var uri = CreateJavaUri (destination))
422-
list = selector.Select (uri)!;
423-
if (list.Count < 1)
424-
return destination;
425-
426-
var proxy = list [0];
427-
if (proxy.Equals (Proxy.NoProxy))
428-
return destination;
429-
430-
var address = proxy.Address () as InetSocketAddress;
431-
if (address == null) // FIXME
432-
return destination;
433-
434-
return new Uri (FormattableString.Invariant ($"http://{address.HostString}:{address.Port}/"));
435-
}
436-
437-
public bool IsBypassed (Uri host)
438-
{
439-
IList<Java.Net.Proxy> list;
440-
using (var uri = CreateJavaUri (host))
441-
list = selector.Select (uri)!;
442-
443-
if (list.Count < 1)
444-
return true;
445-
446-
return list [0].Equals (Proxy.NoProxy);
447-
}
448-
449-
public ICredentials? Credentials {
450-
get;
451-
set;
452-
}
453-
}
454307
}
455308
}

src/Mono.Android/Android.Runtime/RuntimeNativeMethods.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ internal unsafe static class RuntimeNativeMethods
4545
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
4646
internal static extern IntPtr _monodroid_lookup_replacement_method_info (string jniSourceType, string jniMethodName, string jniMethodSignature);
4747

48-
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
49-
internal static extern IntPtr _monodroid_timezone_get_default_id ();
50-
51-
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
52-
internal static extern int _monodroid_getifaddrs (out IntPtr ifap);
53-
54-
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
55-
internal static extern void _monodroid_freeifaddrs (IntPtr ifap);
5648

5749
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
5850
internal static extern void _monodroid_detect_cpu_and_architecture (ref ushort built_for_cpu, ref ushort running_on_cpu, ref byte is64bit);

src/native/clr/host/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ set(XAMARIN_MONODROID_SOURCES
3939
runtime-environment.cc
4040
runtime-util.cc
4141
typemap.cc
42-
xamarin_getifaddrs.cc
4342
)
4443

4544
if(DEBUG_BUILD)

src/native/clr/host/internal-pinvokes-clr.cc

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,3 @@ void monodroid_timing_stop (managed_timing_sequence *sequence, const char *messa
6565
Timing::info (sequence, message == nullptr ? DEFAULT_MESSAGE.data () : message);
6666
timing->release_sequence (sequence);
6767
}
68-
69-
void* _monodroid_timezone_get_default_id ()
70-
{
71-
JNIEnv *env = OSBridge::ensure_jnienv ();
72-
jmethodID getDefault = env->GetStaticMethodID (Host::get_java_class_TimeZone (), "getDefault", "()Ljava/util/TimeZone;");
73-
jmethodID getID = env->GetMethodID (Host::get_java_class_TimeZone (), "getID", "()Ljava/lang/String;");
74-
jobject d = env->CallStaticObjectMethod (Host::get_java_class_TimeZone (), getDefault);
75-
jstring id = reinterpret_cast<jstring> (env->CallObjectMethod (d, getID));
76-
const char *mutf8 = env->GetStringUTFChars (id, nullptr);
77-
if (mutf8 == nullptr) {
78-
log_error (LOG_DEFAULT, "Failed to convert Java TimeZone ID to UTF8 (out of memory?)"sv);
79-
env->DeleteLocalRef (id);
80-
env->DeleteLocalRef (d);
81-
return nullptr;
82-
}
83-
char *def_id = strdup (mutf8);
84-
env->ReleaseStringUTFChars (id, mutf8);
85-
env->DeleteLocalRef (id);
86-
env->DeleteLocalRef (d);
87-
return def_id;
88-
}

0 commit comments

Comments
 (0)