Skip to content

Commit 7773f90

Browse files
authored
[Android Connectivity] Set app package on Intent used to invoke context receiver for network callback (#20651)
* No need to export app specific broadcast receivers We are the only ones invoking these, so there's no need to export. The export made things 'work' on android 14 because of changes to that api level, but that wasn't the most correct fix. * Set package on connectivity intent With changes in android 14, we need to scope the connectivity intent we use to trigger our own internal broadcast receiver to the current app package (which we get from the application context).
1 parent 961f00f commit 7773f90

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

src/Essentials/src/Battery/Battery.android.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ partial class BatteryImplementation : IBattery
2121
void StartEnergySaverListeners()
2222
{
2323
powerReceiver = new EnergySaverBroadcastReceiver(OnEnergySaverChanged);
24-
PlatformUtils.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged), false);
24+
PlatformUtils.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged));
2525
}
2626

2727
void StopEnergySaverListeners()
@@ -52,7 +52,7 @@ void StartBatteryListeners()
5252
Permissions.EnsureDeclared<Permissions.Battery>();
5353

5454
batteryReceiver = new BatteryBroadcastReceiver(OnBatteryInfoChanged);
55-
PlatformUtils.RegisterBroadcastReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged), false);
55+
PlatformUtils.RegisterBroadcastReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged));
5656
}
5757

5858
void StopBatteryListeners()
@@ -76,7 +76,7 @@ public double ChargeLevel
7676
Permissions.EnsureDeclared<Permissions.Battery>();
7777

7878
using (var filter = new IntentFilter(Intent.ActionBatteryChanged))
79-
using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter, false))
79+
using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter))
8080
{
8181
if (battery is null)
8282
return -1; // Unknown
@@ -99,7 +99,7 @@ public BatteryState State
9999
Permissions.EnsureDeclared<Permissions.Battery>();
100100

101101
using (var filter = new IntentFilter(Intent.ActionBatteryChanged))
102-
using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter, false))
102+
using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter))
103103
{
104104
if (battery is null)
105105
return BatteryState.Unknown;
@@ -129,7 +129,7 @@ public BatteryPowerSource PowerSource
129129
Permissions.EnsureDeclared<Permissions.Battery>();
130130

131131
using (var filter = new IntentFilter(Intent.ActionBatteryChanged))
132-
using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter, false))
132+
using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter))
133133
{
134134
if (battery is null)
135135
return BatteryPowerSource.Unknown;

src/Essentials/src/Connectivity/Connectivity.android.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ partial class ConnectivityImplementation : IConnectivity
1515
/// Unique identifier for the connectivity changed action on Android.
1616
/// </summary>
1717
public const string ConnectivityChangedAction = "com.maui.essentials.ESSENTIALS_CONNECTIVITY_CHANGED";
18-
static Intent connectivityIntent = new Intent(ConnectivityChangedAction);
19-
18+
2019
static ConnectivityManager connectivityManager;
2120

2221
static ConnectivityManager ConnectivityManager =>
@@ -45,13 +44,15 @@ void StartListeners()
4544

4645
conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);
4746

48-
PlatformUtils.RegisterBroadcastReceiver(conectivityReceiver, filter, true);
47+
PlatformUtils.RegisterBroadcastReceiver(conectivityReceiver, filter);
4948
}
5049

5150
void StopListeners()
5251
{
5352
if (conectivityReceiver == null)
53+
{
5454
return;
55+
}
5556

5657
try
5758
{
@@ -77,11 +78,15 @@ void StopListeners()
7778
void RegisterNetworkCallback()
7879
{
7980
if (!OperatingSystem.IsAndroidVersionAtLeast(24))
81+
{
8082
return;
83+
}
8184

8285
var manager = ConnectivityManager;
8386
if (manager == null)
87+
{
8488
return;
89+
}
8590

8691
var request = new NetworkRequest.Builder().Build();
8792
networkCallback = new EssentialsNetworkCallback();
@@ -91,11 +96,15 @@ void RegisterNetworkCallback()
9196
void UnregisterNetworkCallback()
9297
{
9398
if (!OperatingSystem.IsAndroidVersionAtLeast(24))
99+
{
94100
return;
101+
}
95102

96103
var manager = ConnectivityManager;
97104
if (manager == null || networkCallback == null)
105+
{
98106
return;
107+
}
99108

100109
manager.UnregisterNetworkCallback(networkCallback);
101110

@@ -104,6 +113,14 @@ void UnregisterNetworkCallback()
104113

105114
class EssentialsNetworkCallback : ConnectivityManager.NetworkCallback
106115
{
116+
readonly Intent connectivityIntent;
117+
118+
public EssentialsNetworkCallback()
119+
{
120+
connectivityIntent = new Intent(ConnectivityChangedAction);
121+
connectivityIntent.SetPackage(Application.Context.PackageName);
122+
}
123+
107124
public override void OnAvailable(Network network) =>
108125
Application.Context.SendBroadcast(connectivityIntent);
109126

@@ -159,15 +176,19 @@ public NetworkAccess NetworkAccess
159176
var capabilities = manager.GetNetworkCapabilities(network);
160177

161178
if (capabilities == null)
179+
{
162180
continue;
181+
}
163182

164183
#pragma warning disable CS0618 // Type or member is obsolete
165184
#pragma warning disable CA1416 // Validate platform compatibility
166185
#pragma warning disable CA1422 // Validate platform compatibility
167186
var info = manager.GetNetworkInfo(network);
168187

169188
if (info == null || !info.IsAvailable)
189+
{
170190
continue;
191+
}
171192
#pragma warning restore CS0618 // Type or member is obsolete
172193

173194
// Check to see if it has the internet capability
@@ -200,12 +221,18 @@ void ProcessAllNetworkInfo()
200221
void ProcessNetworkInfo(NetworkInfo info)
201222
{
202223
if (info == null || !info.IsAvailable)
224+
{
203225
return;
226+
}
204227

205228
if (info.IsConnected)
229+
{
206230
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet);
231+
}
207232
else if (info.IsConnectedOrConnecting)
233+
{
208234
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet);
235+
}
209236
#pragma warning restore CA1422 // Validate platform compatibility
210237
#pragma warning restore CA1416 // Validate platform compatibility
211238
#pragma warning restore CS0618 // Type or member is obsolete
@@ -250,15 +277,19 @@ public IEnumerable<ConnectionProfile> ConnectionProfiles
250277

251278
var p = ProcessNetworkInfo(info);
252279
if (p.HasValue)
280+
{
253281
yield return p.Value;
282+
}
254283
}
255284

256285
#pragma warning disable CS0618 // Type or member is obsolete
257286
static ConnectionProfile? ProcessNetworkInfo(NetworkInfo info)
258287
{
259288

260289
if (info == null || !info.IsAvailable || !info.IsConnectedOrConnecting)
290+
{
261291
return null;
292+
}
262293

263294

264295
return GetConnectionType(info.Type, info.TypeName);
@@ -289,22 +320,34 @@ internal static ConnectionProfile GetConnectionType(ConnectivityType connectivit
289320
return ConnectionProfile.Unknown;
290321
default:
291322
if (string.IsNullOrWhiteSpace(typeName))
323+
{
292324
return ConnectionProfile.Unknown;
325+
}
293326

294327
if (typeName.Contains("mobile", StringComparison.OrdinalIgnoreCase))
328+
{
295329
return ConnectionProfile.Cellular;
330+
}
296331

297332
if (typeName.Contains("wimax", StringComparison.OrdinalIgnoreCase))
333+
{
298334
return ConnectionProfile.Cellular;
335+
}
299336

300337
if (typeName.Contains("wifi", StringComparison.OrdinalIgnoreCase))
338+
{
301339
return ConnectionProfile.WiFi;
340+
}
302341

303342
if (typeName.Contains("ethernet", StringComparison.OrdinalIgnoreCase))
343+
{
304344
return ConnectionProfile.Ethernet;
345+
}
305346

306347
if (typeName.Contains("bluetooth", StringComparison.OrdinalIgnoreCase))
348+
{
307349
return ConnectionProfile.Bluetooth;
350+
}
308351

309352
return ConnectionProfile.Unknown;
310353
}
@@ -335,7 +378,9 @@ public override async void OnReceive(Context context, Intent intent)
335378
#pragma warning disable CS0618 // Type or member is obsolete
336379
if (intent.Action != ConnectivityManager.ConnectivityAction && intent.Action != ConnectivityImplementation.ConnectivityChangedAction)
337380
#pragma warning restore CS0618 // Type or member is obsolete
381+
{
338382
return;
383+
}
339384

340385
// await 1500ms to ensure that the the connection manager updates
341386
await Task.Delay(1500);

src/Essentials/src/Platform/PlatformUtils.android.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ internal static int NextRequestCode()
2626
return requestCode;
2727
}
2828

29-
internal static Intent? RegisterBroadcastReceiver(BroadcastReceiver? receiver, IntentFilter filter, bool exported)
29+
internal static Intent? RegisterBroadcastReceiver(BroadcastReceiver? receiver, IntentFilter filter)
3030
{
3131
#if ANDROID34_0_OR_GREATER
3232
if (OperatingSystem.IsAndroidVersionAtLeast(34))
3333
{
34-
var flags = exported ? ReceiverFlags.Exported : ReceiverFlags.NotExported;
35-
return Application.Context.RegisterReceiver(receiver, filter, flags);
34+
return Application.Context.RegisterReceiver(receiver, filter, ReceiverFlags.NotExported);
3635
}
3736
#endif
3837
return Application.Context.RegisterReceiver(receiver, filter);

0 commit comments

Comments
 (0)