Skip to content

Commit e12a572

Browse files
Fix null dereferences
1 parent 71e940b commit e12a572

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ static void PlatformSetStyle(StatusBarStyle style)
8383
static void SetStatusBarAppearance(Activity activity, bool isLightStatusBars)
8484
{
8585
var window = activity.GetCurrentWindow();
86-
var windowController = WindowCompat.GetInsetsController(window, window.DecorView);
87-
windowController.AppearanceLightStatusBars = isLightStatusBars;
86+
if (WindowCompat.GetInsetsController(window, window.DecorView) is WindowInsetsControllerCompat windowController)
87+
{
88+
windowController.AppearanceLightStatusBars = isLightStatusBars;
89+
}
8890
}
8991
}

src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sealed partial class MediaControlsService : Service
2222
bool isDisposed;
2323

2424
PlayerNotificationManager? playerNotificationManager;
25-
NotificationCompat.Builder? notification;
25+
NotificationCompat.Builder? notificationBuilder;
2626

2727
public event EventHandler TaskRemoved
2828
{
@@ -77,10 +77,10 @@ public override void OnRebind(Intent? intent)
7777
StartForegroundServices();
7878
}
7979

80-
[MemberNotNull(nameof(NotificationManager), nameof(notification))]
80+
[MemberNotNull(nameof(NotificationManager), nameof(notificationBuilder))]
8181
public void UpdateNotifications(in MediaSession session, in PlatformMediaElement mediaElement)
8282
{
83-
ArgumentNullException.ThrowIfNull(notification);
83+
ArgumentNullException.ThrowIfNull(notificationBuilder);
8484
ArgumentNullException.ThrowIfNull(NotificationManager);
8585

8686
var style = new MediaStyleNotificationHelper.MediaStyle(session);
@@ -89,8 +89,8 @@ public void UpdateNotifications(in MediaSession session, in PlatformMediaElement
8989
SetLegacyNotifications(session, mediaElement);
9090
}
9191

92-
notification.SetStyle(style);
93-
NotificationManagerCompat.From(Platform.AppContext).Notify(1, notification.Build());
92+
notificationBuilder.SetStyle(style);
93+
NotificationManagerCompat.From(Platform.AppContext)?.Notify(1, notificationBuilder.Build());
9494
}
9595

9696
[MemberNotNull(nameof(playerNotificationManager))]
@@ -152,26 +152,29 @@ static void CreateNotificationChannel(in NotificationManager notificationMnaMana
152152
notificationMnaManager.CreateNotificationChannel(channel);
153153
}
154154

155-
[MemberNotNull(nameof(notification), nameof(NotificationManager))]
155+
[MemberNotNull(nameof(notificationBuilder), nameof(NotificationManager))]
156156
void StartForegroundServices()
157157
{
158158
NotificationManager ??= GetSystemService(NotificationService) as NotificationManager ?? throw new InvalidOperationException($"{nameof(NotificationManager)} cannot be null");
159-
notification ??= new NotificationCompat.Builder(Platform.AppContext, "1");
159+
notificationBuilder ??= new NotificationCompat.Builder(Platform.AppContext, "1");
160160

161-
notification.SetSmallIcon(Resource.Drawable.media3_notification_small_icon);
162-
notification.SetAutoCancel(false);
163-
notification.SetForegroundServiceBehavior(NotificationCompat.ForegroundServiceImmediate);
164-
notification.SetVisibility(NotificationCompat.VisibilityPublic);
161+
notificationBuilder.SetSmallIcon(Resource.Drawable.media3_notification_small_icon);
162+
notificationBuilder.SetAutoCancel(false);
163+
notificationBuilder.SetForegroundServiceBehavior(NotificationCompat.ForegroundServiceImmediate);
164+
notificationBuilder.SetVisibility(NotificationCompat.VisibilityPublic);
165165

166166
CreateNotificationChannel(NotificationManager);
167167

168168
if (OperatingSystem.IsAndroidVersionAtLeast(29))
169169
{
170-
StartForeground(1, notification.Build(), ForegroundService.TypeMediaPlayback);
170+
if (notificationBuilder.Build() is Notification notification)
171+
{
172+
StartForeground(1, notification, ForegroundService.TypeMediaPlayback);
173+
}
171174
}
172175
else
173176
{
174-
StartForeground(1, notification.Build());
177+
StartForeground(1, notificationBuilder.Build());
175178
}
176179
}
177180
}

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ void SetSystemBarsVisibility()
173173
| SystemUiFlags.Fullscreen
174174
| SystemUiFlags.Immersive;
175175
}
176-
177-
windowInsetsControllerCompat.Hide(barTypes);
178-
windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;
176+
177+
if(windowInsetsControllerCompat is not null)
178+
{
179+
windowInsetsControllerCompat.Hide(barTypes);
180+
windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;
181+
}
179182

180183
}
181184
else
@@ -192,8 +195,12 @@ void SetSystemBarsVisibility()
192195
currentWindow.DecorView.SystemUiFlags = (SystemUiFlags)defaultSystemUiVisibility;
193196
}
194197

195-
windowInsetsControllerCompat.Show(barTypes);
196-
windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorDefault;
198+
if(windowInsetsControllerCompat is not null)
199+
{
200+
windowInsetsControllerCompat.Show(barTypes);
201+
windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorDefault;
202+
}
203+
197204
WindowCompat.SetDecorFitsSystemWindows(currentWindow, true);
198205
}
199206
}

src/CommunityToolkit.Maui/PlatformConfiguration/AndroidSpecific/NavigationBar.android.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ public static void MapNavigationStyleProperty(IPageHandler handler, IContentView
6464
static void SetSystemNavigationBarAppearance(Activity activity, bool isLightSystemNavigationBars)
6565
{
6666
var window = activity.GetCurrentWindow();
67-
var windowController = WindowCompat.GetInsetsController(window, window.DecorView);
68-
windowController.AppearanceLightNavigationBars = isLightSystemNavigationBars;
67+
if (WindowCompat.GetInsetsController(window, window.DecorView) is WindowInsetsControllerCompat insetsController)
68+
{
69+
insetsController.AppearanceLightNavigationBars = isLightSystemNavigationBars;
70+
}
6971
}
7072
}
7173

0 commit comments

Comments
 (0)