From df330216bbdc9e4d071ccb7bf5063604f590ac47 Mon Sep 17 00:00:00 2001
From: Jonathan Pobst <jonathan.pobst@microsoft.com>
Date: Tue, 24 Jan 2023 12:13:49 -1000
Subject: [PATCH] =?UTF-8?q?=EF=BB=BF[Mono.Android]=20Fix=20incorrect=20Con?=
 =?UTF-8?q?text.RegisterReceiver=20enumification.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Mono.Android/Android.Content/Context.cs            | 10 ++++++++++
 src/Mono.Android/Mono.Android.csproj                   |  5 +++++
 .../PublicAPI/API-34/PublicAPI.Unshipped.txt           |  2 ++
 src/Mono.Android/metadata                              |  8 ++++++++
 4 files changed, 25 insertions(+)

diff --git a/src/Mono.Android/Android.Content/Context.cs b/src/Mono.Android/Android.Content/Context.cs
index d397c171207..62896ab8dd9 100644
--- a/src/Mono.Android/Android.Content/Context.cs
+++ b/src/Mono.Android/Android.Content/Context.cs
@@ -1,4 +1,5 @@
 using System;
+using Android.OS;
 using Android.Runtime;
 
 namespace Android.Content {
@@ -16,5 +17,14 @@ public void StartActivity (Type type)
 		[Obsolete ("This constant will be removed in the future version. Use Android.Content.ReceiverFlags enum directly instead of this field.")]
 		public const int ReceiverVisibleToInstantApps = 1;
 #endif
+
+#if ANDROID_34
+		// Add correctly enumified overloads
+		public Intent? RegisterReceiver (BroadcastReceiver? receiver, IntentFilter? filter, ReceiverFlags flags)
+			=> RegisterReceiver (receiver, filter, (ActivityFlags)flags);
+
+		public Intent? RegisterReceiver (BroadcastReceiver? receiver, IntentFilter? filter, string? broadcastPermission, Handler? scheduler, ReceiverFlags flags)
+			=> RegisterReceiver (receiver, filter, broadcastPermission, scheduler, (ActivityFlags)flags);
+#endif
 	}
 }
diff --git a/src/Mono.Android/Mono.Android.csproj b/src/Mono.Android/Mono.Android.csproj
index b4444de2a54..71da67f2a0c 100644
--- a/src/Mono.Android/Mono.Android.csproj
+++ b/src/Mono.Android/Mono.Android.csproj
@@ -27,7 +27,12 @@
   <PropertyGroup>
     <DefineConstants Condition=" '$(AndroidApiLevel)' &gt; '$(AndroidLatestStableApiLevel)' ">$(DefineConstants);ANDROID_UNSTABLE</DefineConstants>
     <OutputPath>$(_MonoAndroidNETDefaultOutDir)</OutputPath>
+
+    <!-- Allow PublicApiAnalyzers to be turned off -->
     <RunAnalyzers Condition=" '$(DisableApiCompatibilityCheck)' == 'True' ">false</RunAnalyzers>
+
+    <!-- PublicApiAnalyzers warnings should be errors, but building in VS throws incorrect extra warnings -->
+    <WarningsAsErrors Condition=" '$(BuildingInsideVisualStudio)' != 'True' ">$(WarningsAsErrors);RS0016,RS0017</WarningsAsErrors>
   </PropertyGroup>
 
   <PropertyGroup Condition=" '$(IncludeAndroidJavadoc)' == 'True' ">
diff --git a/src/Mono.Android/PublicAPI/API-34/PublicAPI.Unshipped.txt b/src/Mono.Android/PublicAPI/API-34/PublicAPI.Unshipped.txt
index 4b0aea8a008..cdec2d9529d 100644
--- a/src/Mono.Android/PublicAPI/API-34/PublicAPI.Unshipped.txt
+++ b/src/Mono.Android/PublicAPI/API-34/PublicAPI.Unshipped.txt
@@ -8448,6 +8448,8 @@ Android.Content.Context.ObtainStyledAttributes(Android.Util.IAttributeSet? set,
 Android.Content.Context.ObtainStyledAttributes(Android.Util.IAttributeSet? set, int[]! attrs, int defStyleAttr, int defStyleRes) -> Android.Content.Res.TypedArray!
 Android.Content.Context.ObtainStyledAttributes(int resid, int[]! attrs) -> Android.Content.Res.TypedArray!
 Android.Content.Context.ObtainStyledAttributes(int[]! attrs) -> Android.Content.Res.TypedArray!
+Android.Content.Context.RegisterReceiver(Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, Android.Content.ReceiverFlags flags) -> Android.Content.Intent?
+Android.Content.Context.RegisterReceiver(Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, string? broadcastPermission, Android.OS.Handler? scheduler, Android.Content.ReceiverFlags flags) -> Android.Content.Intent?
 Android.Content.Context.StartActivity(System.Type! type) -> void
 Android.Content.ContextParams
 Android.Content.ContextParams.AttributionTag.get -> string?
diff --git a/src/Mono.Android/metadata b/src/Mono.Android/metadata
index 31c85dc9e61..0ca93013863 100644
--- a/src/Mono.Android/metadata
+++ b/src/Mono.Android/metadata
@@ -1809,6 +1809,14 @@
 
   <!-- 2 conflicting copies of ErrorEventArgs -->
   <attr api-since="34" path="/api/package[@name='android.speech']/interface[@name='ModelDownloadListener']/method[@name='onError' and count(parameter)=1 and parameter[1][@type='int']]" name="argsType">ModelDownloadErrorEventArgs</attr>
+
+  <!-- Fix some incorrectly enumified methods -->
+  <attr api-since="34" path="/api/package[@name='android.content']/class[@name='Context']/method[@name='registerReceiver' and count(parameter)=3 and parameter[1][@type='android.content.BroadcastReceiver'] and parameter[2][@type='android.content.IntentFilter'] and parameter[3][@type='int']]" name="deprecated">This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.</attr>
+  <attr api-since="34" path="/api/package[@name='android.content']/class[@name='Context']/method[@name='registerReceiver' and count(parameter)=5 and parameter[1][@type='android.content.BroadcastReceiver'] and parameter[2][@type='android.content.IntentFilter'] and parameter[3][@type='java.lang.String'] and parameter[4][@type='android.os.Handler'] and parameter[5][@type='int']]" name="deprecated">This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.</attr>
+  <attr api-since="34" path="/api/package[@name='android.content']/class[@name='ContextWrapper']/method[@name='registerReceiver' and count(parameter)=3 and parameter[1][@type='android.content.BroadcastReceiver'] and parameter[2][@type='android.content.IntentFilter'] and parameter[3][@type='int']]" name="deprecated">This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.</attr>
+  <attr api-since="34" path="/api/package[@name='android.content']/class[@name='ContextWrapper']/method[@name='registerReceiver' and count(parameter)=5 and parameter[1][@type='android.content.BroadcastReceiver'] and parameter[2][@type='android.content.IntentFilter'] and parameter[3][@type='java.lang.String'] and parameter[4][@type='android.os.Handler'] and parameter[5][@type='int']]" name="deprecated">This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.</attr>
+  <attr api-since="34" path="/api/package[@name='android.test.mock']/class[@name='MockContext']/method[@name='registerReceiver' and count(parameter)=3 and parameter[1][@type='android.content.BroadcastReceiver'] and parameter[2][@type='android.content.IntentFilter'] and parameter[3][@type='int']]" name="deprecated">This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.</attr>
+  <attr api-since="34" path="/api/package[@name='android.test.mock']/class[@name='MockContext']/method[@name='registerReceiver' and count(parameter)=5 and parameter[1][@type='android.content.BroadcastReceiver'] and parameter[2][@type='android.content.IntentFilter'] and parameter[3][@type='java.lang.String'] and parameter[4][@type='android.os.Handler'] and parameter[5][@type='int']]" name="deprecated">This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.</attr>
 	
   <!--
     ***********************************************************************
