Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Mono.Android/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated binding code mirrors Android platform API shapes and nullable metadata.
[obj/**/mcw/*.cs]
dotnet_diagnostic.CS0169.severity = none
dotnet_diagnostic.CS8603.severity = none
dotnet_diagnostic.CS8604.severity = none
dotnet_diagnostic.CS8625.severity = none
dotnet_diagnostic.CS8768.severity = none
6 changes: 3 additions & 3 deletions src/Mono.Android/Android.Animation/AnimatorSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Android.Animation
{
public partial class AnimatorSet
{
static Delegate cb_setDuration_SetDuration_J_Landroid_animation_Animator_;
static Delegate? cb_setDuration_SetDuration_J_Landroid_animation_Animator_;

[Register ("setDuration", "(J)Landroid/animation/Animator;", "GetSetDuration_JHandler")]
public override unsafe Android.Animation.Animator SetDuration (long duration)
Expand All @@ -16,7 +16,7 @@ public override unsafe Android.Animation.Animator SetDuration (long duration)
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
__args [0] = new JniArgumentValue (duration);
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, __args);
return global::Java.Lang.Object.GetObject<Android.Animation.Animator> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
return global::Java.Lang.Object.GetObject<Android.Animation.Animator> (__rm.Handle, JniHandleOwnership.TransferLocalRef)!;
} finally {
}
}
Expand All @@ -35,7 +35,7 @@ static IntPtr n_SetDuration_J (IntPtr jnienv, IntPtr native__this, long duration

try {
var __this = global::Java.Lang.Object.GetObject<Android.Animation.AnimatorSet> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return JNIEnv.ToLocalJniHandle (__this.SetDuration (duration));
return JNIEnv.ToLocalJniHandle (__this!.SetDuration (duration));
} catch (global::System.Exception __e) {
__r.OnUserUnhandledException (ref __envp, __e);
return default;
Expand Down
6 changes: 3 additions & 3 deletions src/Mono.Android/Android.Animation/ValueAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Android.Animation
{
public partial class ValueAnimator
{
static Delegate cb_setDuration_SetDuration_J_Landroid_animation_Animator_;
static Delegate? cb_setDuration_SetDuration_J_Landroid_animation_Animator_;

[Register ("setDuration", "(J)Landroid/animation/Animator;", "GetSetDuration_JHandler")]
public override unsafe Android.Animation.Animator SetDuration (long duration)
Expand All @@ -16,7 +16,7 @@ public override unsafe Android.Animation.Animator SetDuration (long duration)
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
__args [0] = new JniArgumentValue (duration);
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, __args);
return global::Java.Lang.Object.GetObject<Android.Animation.Animator> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
return global::Java.Lang.Object.GetObject<Android.Animation.Animator> (__rm.Handle, JniHandleOwnership.TransferLocalRef)!;
} finally {
}
}
Expand All @@ -35,7 +35,7 @@ static IntPtr n_SetDuration_J (IntPtr jnienv, IntPtr native__this, long duration

try {
var __this = global::Java.Lang.Object.GetObject<Android.Animation.ValueAnimator> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return JNIEnv.ToLocalJniHandle (__this.SetDuration (duration));
return JNIEnv.ToLocalJniHandle (__this!.SetDuration (duration));
} catch (global::System.Exception __e) {
__r.OnUserUnhandledException (ref __envp, __e);
return default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable disable
#nullable enable

using System;

Expand Down
3 changes: 2 additions & 1 deletion src/Mono.Android/Android.Bluetooth/BluetoothDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ public sealed partial class BluetoothDevice
// These methods were obsoleted as a warning in API-31
[global::System.Obsolete ("This method has the wrong enumeration. Use the version that takes an 'Android.Bluetooth.BluetoothPhy' instead.")]
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android26.0")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessage ("Interoperability", "CA1422:Validate platform compatibility", Justification = "Compatibility overload intentionally forwards to the generated API that replaced this incorrect enum overload.")]
public unsafe Android.Bluetooth.BluetoothGatt? ConnectGatt (Android.Content.Context? context, bool autoConnect, Android.Bluetooth.BluetoothGattCallback? @callback, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.BluetoothTransports transport, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.LE.ScanSettingsPhy phy)
=> ConnectGatt (context, autoConnect, @callback, transport, (Android.Bluetooth.BluetoothPhy) phy);

[global::System.Obsolete ("This method has the wrong enumeration. Use the version that takes an 'Android.Bluetooth.BluetoothPhy' instead.")]
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android26.0")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessage ("Interoperability", "CA1422:Validate platform compatibility", Justification = "Compatibility overload intentionally forwards to the generated API that replaced this incorrect enum overload.")]
public unsafe Android.Bluetooth.BluetoothGatt? ConnectGatt (Android.Content.Context? context, bool autoConnect, Android.Bluetooth.BluetoothGattCallback? @callback, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.BluetoothTransports transport, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.LE.ScanSettingsPhy phy, Android.OS.Handler? handler)
=> ConnectGatt (context, autoConnect, @callback, transport, (Android.Bluetooth.BluetoothPhy) phy, handler);
}
}

#endif

12 changes: 3 additions & 9 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpl
protected override IEnumerable<string> GetSimpleReferences (Type type)
{
string? j = JNIEnv.TypemapManagedToJava (type);
j = GetReplacementTypeCore (j) ?? j;
if (j != null) {
j = GetReplacementTypeCore (j) ?? j;
}

if (j != null) {
return new[]{j};
Expand Down Expand Up @@ -579,14 +581,6 @@ public override void RegisterNativeMembers (
JniEnvironment.Runtime.RaisePendingException (e);
}

bool ShouldRegisterDynamically (string callbackTypeName, string callbackString, string typeName, string callbackName)
{
if (String.Compare (typeName, callbackTypeName, StringComparison.Ordinal) != 0) {
return false;
}

return String.Compare (callbackName, callbackString, StringComparison.Ordinal) == 0;
}
}

static int CountMethods (ReadOnlySpan<char> methodsSpan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public override Expression CreateReturnValueFromManagedExpression (JniValueMarsh
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type? targetType)
{
if (targetType == null)
throw new ArgumentNullException (nameof (targetType));

var r = Expression.Variable (targetType, sourceValue.Name + "_val");
context.LocalVariables.Add (r);
context.CreationStatements.Add (
Expand Down
10 changes: 8 additions & 2 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public static unsafe IntPtr NewObject (IntPtr jclass, IntPtr jmethod, params JVa

public static string GetClassNameFromInstance (IntPtr jobject)
{
return JniEnvironment.Types.GetJniTypeNameFromInstance (new JniObjectReference (jobject));
return JniEnvironment.Types.GetJniTypeNameFromInstance (new JniObjectReference (jobject))!;
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand Down Expand Up @@ -494,7 +494,11 @@ static unsafe IntPtr monovm_typemap_managed_to_java (Type type, byte* mvidptr)
if (RuntimeFeature.IsMonoRuntime) {
ret = monovm_typemap_managed_to_java (type, mvidptr);
} else if (RuntimeFeature.IsCoreClrRuntime) {
ret = RuntimeNativeMethods.clr_typemap_managed_to_java (type.FullName, (IntPtr)mvidptr);
string? fullName = type.FullName;
if (fullName == null) {
return null;
}
ret = RuntimeNativeMethods.clr_typemap_managed_to_java (fullName, (IntPtr)mvidptr);
} else {
throw new NotSupportedException ("Internal error: unknown runtime not supported");
}
Expand Down Expand Up @@ -691,6 +695,8 @@ public static void CopyArray (IntPtr src, string[] dest)
AssertIsJavaObject (type);

IntPtr elem = GetObjectArrayElement (source, index);
if (type == null)
return Java.Lang.Object.GetObject (elem, JniHandleOwnership.TransferLocalRef);
return GetObject (elem, type);

// FIXME: Since a Dictionary<Type, Func> is used here, the trimmer will not be able to properly analyze `Type t`
Expand Down
4 changes: 1 addition & 3 deletions src/Mono.Android/Android.Runtime/JNIEnvInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using Java.Interop.Tools.TypeNameMappings;

using Microsoft.Android.Runtime;
using RuntimeFeature = Microsoft.Android.Runtime.RuntimeFeature;

namespace Android.Runtime
{
static internal partial class JNIEnvInit
Expand Down Expand Up @@ -63,7 +61,7 @@ static unsafe void RegisterJniNatives (IntPtr typeName_ptr, int typeName_len, In
// FIXME: https://github.com/xamarin/xamarin-android/issues/8724
[UnconditionalSuppressMessage ("Trimming", "IL2057", Justification = "Type should be preserved by the MarkJavaObjects trimmer step.")]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
static Type TypeGetType (string typeName) =>
static Type? TypeGetType (string typeName) =>
Type.GetType (typeName, throwOnError: false);

string typeName = new string ((char*) typeName_ptr, 0, typeName_len);
Expand Down
1 change: 1 addition & 0 deletions src/Mono.Android/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using RuntimeFeature = Microsoft.Android.Runtime.AndroidRuntimeFeature;
6 changes: 4 additions & 2 deletions src/Mono.Android/Java.Interop/JavaConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static Func<IntPtr, JniHandleOwnership, object> GetJniHandleConverterForType ([D
internal readonly struct ArrayElementConverter
{
readonly Type? elementType;
readonly Func<IntPtr, JniHandleOwnership, object>? converter;
readonly Func<IntPtr, JniHandleOwnership, object?>? converter;
readonly bool useRuntimeTypeMapping;

public ArrayElementConverter (Array array)
Expand Down Expand Up @@ -348,7 +348,9 @@ public static T? FromJniHandle<
{
var lref = JniEnvironment.Types.GetObjectClass (new JniObjectReference (handle));
try {
string className = JniEnvironment.Types.GetJniTypeNameFromClass (lref);
string? className = JniEnvironment.Types.GetJniTypeNameFromClass (lref);
if (className == null)
return null;
if (TypeMappings.TryGetValue (className, out var match))
return match;
if (JniEnvironment.Types.IsAssignableFrom (lref, new JniObjectReference (JavaDictionary.map_class)))
Expand Down
2 changes: 1 addition & 1 deletion src/Mono.Android/Java.Interop/JavaObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal static TResult? _JavaCast<
if (instance.Handle == IntPtr.Zero)
throw new ObjectDisposedException (instance.GetType ().FullName);

return (TResult) Java.Lang.Object.GetObject (instance.Handle, JniHandleOwnership.DoNotTransfer, typeof (TResult)) ??
return (TResult?) Java.Lang.Object.GetObject (instance.Handle, JniHandleOwnership.DoNotTransfer, typeof (TResult)) ??
throw new InvalidCastException (
FormattableString.Invariant ($"Unable to convert instance of type '{instance.GetType ().FullName}' to type '{typeof (TResult).FullName}'."));
}
Expand Down
11 changes: 9 additions & 2 deletions src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ static Type monovm_typemap_java_to_managed (string java_type_name)
return null;
}

string managedAssemblyName = Marshal.PtrToStringAnsi (managedAssemblyNamePointer);
string? managedAssemblyName = Marshal.PtrToStringAnsi (managedAssemblyNamePointer);
if (managedAssemblyName == null) {
return null;
}
Assembly assembly = Assembly.Load (managedAssemblyName);
Type? ret = null;
foreach (Module module in assembly.Modules) {
Expand Down Expand Up @@ -304,8 +307,12 @@ static Type monovm_typemap_java_to_managed (string java_type_name)
Type? type = null;
IntPtr class_ptr = JNIEnv.GetObjectClass (handle);
string? class_name = GetClassName (class_ptr);
if (class_name == null) {
JNIEnv.DeleteLocalRef (class_ptr);
return null;
}
lock (TypeManagerMapDictionaries.AccessLock) {
while (class_ptr != IntPtr.Zero) {
while (class_ptr != IntPtr.Zero && class_name != null) {
type = GetJavaToManagedTypeCore (class_name);
if (type != null) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public override void RemovePeer (IJavaPeerable value)

for (int i = peers.Count - 1; i >= 0; i--) {
ReferenceTrackingHandle peer = peers [i];
IJavaPeerable target = peer.Target;
IJavaPeerable? target = peer.Target;
if (ReferenceEquals (value, target)) {
peers.RemoveAt (i);
peer.Dispose ();
Expand Down Expand Up @@ -303,7 +303,9 @@ public void Dispose ()

GCHandle handle = HandleContext.GetAssociatedGCHandle (_context);
HandleContext.Free (ref _context);
#pragma warning disable CS8625 // Clear the weak reference target after the native control block has been freed.
_weakReference.SetTarget (null);
#pragma warning restore CS8625
if (handle.IsAllocated) {
handle.Free ();
}
Expand Down Expand Up @@ -335,13 +337,16 @@ public bool IsCollected
}

// This is an internal mirror of the Java.Interop.JniObjectReferenceControlBlock
// whose fields are written by Java.Interop native memory.
#pragma warning disable CS0649
private struct JniObjectReferenceControlBlock
{
public IntPtr handle;
public int handle_type;
public IntPtr weak_handle;
public int refs_added;
}
#pragma warning restore CS0649

public static GCHandle GetAssociatedGCHandle (HandleContext* context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ namespace Microsoft.Android.Runtime;

static class JniRemappingLookup
{
// Filled by native memory; managed code only reads the fields.
#pragma warning disable CS0649
struct JniRemappingReplacementMethod
{
public string? target_type;
public string? target_name;
public bool is_static;
}
#pragma warning restore CS0649

internal static IReadOnlyList<string> GetStaticMethodFallbackTypes (string jniSimpleReference, bool useReplacementTypes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

namespace Microsoft.Android.Runtime;

#if INSIDE_MONO_ANDROID_RUNTIME
static class RuntimeFeature
#else
static class AndroidRuntimeFeature
#endif
{
const bool ManagedTypeMapEnabledByDefault = false;
const bool IsMonoRuntimeEnabledByDefault = true;
Expand Down
1 change: 1 addition & 0 deletions src/Mono.Android/Mono.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="Android.App\UsesFeatureAttribute.Partial.cs" />
<Compile Include="Android.App\UsesLibraryAttribute.Partial.cs" />
<Compile Include="Android.App\UsesPermissionAttribute.Partial.cs" />
<Compile Include="GlobalUsings.cs" />
<Compile Include="Android.Content.PM\PackageManager.cs" />
<Compile Include="Android.Graphics\PathIterator.cs" />
<Compile Include="Android.Hardware\HardwareBuffer.cs" />
Expand Down
2 changes: 0 additions & 2 deletions src/Mono.Android/PublicAPI/API-36.1/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66141,10 +66141,8 @@ const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile0 = Android.Media
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile1 = Android.Media.MediaCodecProfileType.Aacobjectlc -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile2 = Android.Media.MediaCodecProfileType.Aacobjectltp -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile2HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10 -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile2HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10 -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3 = Android.Media.MediaCodecProfileType.Avcprofilehigh -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10plus -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10plus -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR = Android.Media.MediaCodecProfileType.Av1profilemain10hdr10plus -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR10Plus = Android.Media.MediaCodecProfileType.Mpeg4profileadvancedsimple -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.EncoderCapabilities.BitrateModeCbrFd = Android.Media.BitrateMode.CbrFd -> Android.Media.BitrateMode
Expand Down
2 changes: 0 additions & 2 deletions src/Mono.Android/PublicAPI/API-37/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66141,10 +66141,8 @@ const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile0 = Android.Media
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile1 = Android.Media.MediaCodecProfileType.Aacobjectlc -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile2 = Android.Media.MediaCodecProfileType.Aacobjectltp -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile2HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10 -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile2HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10 -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3 = Android.Media.MediaCodecProfileType.Avcprofilehigh -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10plus -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR = Android.Media.MediaCodecProfileType.Apvprofile42210hdr10plus -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR = Android.Media.MediaCodecProfileType.Av1profilemain10hdr10plus -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.CodecProfileLevel.VP9Profile3HDR10Plus = Android.Media.MediaCodecProfileType.Mpeg4profileadvancedsimple -> Android.Media.MediaCodecProfileType
const Android.Media.MediaCodecInfo.EncoderCapabilities.BitrateModeCbrFd = Android.Media.BitrateMode.CbrFd -> Android.Media.BitrateMode
Expand Down