From 408cfb2c1c927359b6784605b5dd5cb267f6089f Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:07:53 +0100 Subject: [PATCH] chore: various code clean (#3591) * chore: remove media3 dependancy from common * chore: remove unused * chore: fix linter change --------- Co-authored-by: Olivier Bouillet --- .../media3/exoplayer/ima/ImaAdsLoader.java | 2 - .../common/react/VideoEventEmitter.java | 10 ++-- .../common/toolbox/ReactBridgeUtils.kt | 58 ++++++------------- .../exoplayer/ReactExoplayerView.java | 4 +- 4 files changed, 26 insertions(+), 48 deletions(-) diff --git a/android/src/main/java/androidx/media3/exoplayer/ima/ImaAdsLoader.java b/android/src/main/java/androidx/media3/exoplayer/ima/ImaAdsLoader.java index 3119d09a..8d30b2b1 100644 --- a/android/src/main/java/androidx/media3/exoplayer/ima/ImaAdsLoader.java +++ b/android/src/main/java/androidx/media3/exoplayer/ima/ImaAdsLoader.java @@ -11,8 +11,6 @@ import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.exoplayer.source.ads.AdsLoader; import androidx.media3.exoplayer.source.ads.AdsMediaSource; -import com.facebook.react.uimanager.ThemedReactContext; - import java.io.IOException; public class ImaAdsLoader implements AdsLoader { diff --git a/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java b/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java index d87b6d84..8010bf65 100644 --- a/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java +++ b/android/src/main/java/com/brentvatne/common/react/VideoEventEmitter.java @@ -14,8 +14,6 @@ import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; import com.facebook.react.uimanager.UIManagerHelper; import com.facebook.react.uimanager.common.ViewUtil; -import com.facebook.react.uimanager.events.RCTEventEmitter; -import com.google.ads.interactivemedia.v3.api.AdError; import java.io.PrintWriter; import java.io.StringWriter; @@ -449,14 +447,14 @@ public class VideoEventEmitter { receiveEvent(EVENT_ON_RECEIVE_AD_EVENT, map); } - public void receiveAdErrorEvent(AdError error) { + public void receiveAdErrorEvent(String message, String code, String type) { WritableMap map = Arguments.createMap(); map.putString("event", "ERROR"); WritableMap dataMap = Arguments.createMap(); - dataMap.putString("message", error.getMessage()); - dataMap.putString("code", String.valueOf(error.getErrorCode())); - dataMap.putString("type", String.valueOf(error.getErrorType())); + dataMap.putString("message", message); + dataMap.putString("code", code); + dataMap.putString("type", type); map.putMap("data", dataMap); receiveEvent(EVENT_ON_RECEIVE_AD_EVENT, map); diff --git a/android/src/main/java/com/brentvatne/common/toolbox/ReactBridgeUtils.kt b/android/src/main/java/com/brentvatne/common/toolbox/ReactBridgeUtils.kt index c2ee2492..10f4752a 100644 --- a/android/src/main/java/com/brentvatne/common/toolbox/ReactBridgeUtils.kt +++ b/android/src/main/java/com/brentvatne/common/toolbox/ReactBridgeUtils.kt @@ -12,67 +12,47 @@ import java.util.HashMap object ReactBridgeUtils { @JvmStatic - fun safeGetString(map: ReadableMap?, key: String?, fallback: String?): String? { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getString(key) else fallback - } + fun safeGetString(map: ReadableMap?, key: String?, fallback: String?): String? = + if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getString(key) else fallback @JvmStatic - fun safeGetString(map: ReadableMap?, key: String?): String? { - return safeGetString(map, key, null) - } + fun safeGetString(map: ReadableMap?, key: String?): String? = safeGetString(map, key, null) @JvmStatic - fun safeGetDynamic(map: ReadableMap?, key: String?, fallback: Dynamic?): Dynamic? { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDynamic(key) else fallback - } + fun safeGetDynamic(map: ReadableMap?, key: String?, fallback: Dynamic?): Dynamic? = + if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDynamic(key) else fallback @JvmStatic - fun safeGetDynamic(map: ReadableMap?, key: String?): Dynamic? { - return safeGetDynamic(map, key, null) - } + fun safeGetDynamic(map: ReadableMap?, key: String?): Dynamic? = safeGetDynamic(map, key, null) @JvmStatic - fun safeGetBool(map: ReadableMap?, key: String?, fallback: Boolean): Boolean { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getBoolean(key) else fallback - } + fun safeGetBool(map: ReadableMap?, key: String?, fallback: Boolean): Boolean = + if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getBoolean(key) else fallback @JvmStatic - fun safeGetMap(map: ReadableMap?, key: String?): ReadableMap? { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getMap(key) else null - } + fun safeGetMap(map: ReadableMap?, key: String?): ReadableMap? = if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getMap(key) else null @JvmStatic - fun safeGetArray(map: ReadableMap?, key: String?): ReadableArray? { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getArray(key) else null - } + fun safeGetArray(map: ReadableMap?, key: String?): ReadableArray? = if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getArray(key) else null @JvmStatic - fun safeGetInt(map: ReadableMap?, key: String?, fallback: Int): Int { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getInt(key) else fallback - } + fun safeGetInt(map: ReadableMap?, key: String?, fallback: Int): Int = + if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getInt(key) else fallback @JvmStatic - fun safeGetInt(map: ReadableMap?, key: String?): Int { - return safeGetInt(map, key, 0) - } + fun safeGetInt(map: ReadableMap?, key: String?): Int = safeGetInt(map, key, 0) @JvmStatic - fun safeGetDouble(map: ReadableMap?, key: String?, fallback: Double): Double { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key) else fallback - } + fun safeGetDouble(map: ReadableMap?, key: String?, fallback: Double): Double = + if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key) else fallback @JvmStatic - fun safeGetDouble(map: ReadableMap?, key: String?): Double { - return safeGetDouble(map, key, 0.0) - } + fun safeGetDouble(map: ReadableMap?, key: String?): Double = safeGetDouble(map, key, 0.0) - @JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?, fallback: Float): Float { - return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key).toFloat() else fallback - } + @JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?, fallback: Float): Float = + if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key).toFloat() else fallback - @JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?): Float { - return safeGetFloat(map, key, 0.0f) - } + @JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?): Float = safeGetFloat(map, key, 0.0f) /** * toStringMap converts a [ReadableMap] into a HashMap. diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index bb09f1a2..7d5bba1a 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -108,6 +108,7 @@ import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.uimanager.ThemedReactContext; +import com.google.ads.interactivemedia.v3.api.AdError; import com.google.ads.interactivemedia.v3.api.AdEvent; import com.google.ads.interactivemedia.v3.api.AdErrorEvent; import com.google.common.collect.ImmutableList; @@ -2124,6 +2125,7 @@ public class ReactExoplayerView extends FrameLayout implements @Override public void onAdError(AdErrorEvent adErrorEvent) { - eventEmitter.receiveAdErrorEvent(adErrorEvent.getError()); + AdError error = adErrorEvent.getError(); + eventEmitter.receiveAdErrorEvent(error.getMessage(), String.valueOf(error.getErrorCode()), String.valueOf(error.getErrorType())); } }