From b74cb59602d68d76ef585f3c9e86b4efa24ea6e5 Mon Sep 17 00:00:00 2001 From: Krzysztof Moch Date: Sat, 14 Sep 2024 15:20:50 +0200 Subject: [PATCH] chore(android): add null checks (#4168) --- .../exoplayer/ReactExoplayerView.java | 18 +++++++++++++----- .../exoplayer/VideoPlaybackService.kt | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index d00945bc..86e6e3d2 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -899,17 +899,25 @@ public class ReactExoplayerView extends FrameLayout implements playbackServiceBinder = (PlaybackServiceBinder) service; try { - playbackServiceBinder.getService().registerPlayer(player, - Objects.requireNonNull((Class) (themedReactContext.getCurrentActivity()).getClass())); + Activity currentActivity = themedReactContext.getCurrentActivity(); + if (currentActivity != null) { + playbackServiceBinder.getService().registerPlayer(player, + (Class) currentActivity.getClass()); + } else { + // Handle the case where currentActivity is null + DebugLog.w(TAG, "Could not register ExoPlayer: currentActivity is null"); + } } catch (Exception e) { - DebugLog.e(TAG, "Cloud not register ExoPlayer"); + DebugLog.e(TAG, "Could not register ExoPlayer: " + e.getMessage()); } } @Override public void onServiceDisconnected(ComponentName name) { try { - playbackServiceBinder.getService().unregisterPlayer(player); + if (playbackServiceBinder != null) { + playbackServiceBinder.getService().unregisterPlayer(player); + } } catch (Exception ignored) {} playbackServiceBinder = null; @@ -917,7 +925,7 @@ public class ReactExoplayerView extends FrameLayout implements @Override public void onNullBinding(ComponentName name) { - DebugLog.e(TAG, "Cloud not register ExoPlayer"); + DebugLog.e(TAG, "Could not register ExoPlayer"); } }; diff --git a/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt b/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt index 6cf6f7d2..77bd4eaf 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt @@ -121,7 +121,7 @@ class VideoPlaybackService : MediaSessionService() { } private fun buildNotification(session: MediaSession): Notification { - val returnToPlayer = Intent(this, sourceActivity).apply { + val returnToPlayer = Intent(this, sourceActivity ?: this.javaClass).apply { flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP }