22
33import com .facebook .react .bridge .ReadableArray ;
44import com .facebook .react .bridge .ReadableMap ;
5- import com .google .ads .interactivemedia .v3 .api .FriendlyObstruction ;
6- import com .google .ads .interactivemedia .v3 .api .ImaSdkFactory ;
7- import com .google .ads .interactivemedia .v3 .api .ImaSdkSettings ;
85import com .jwplayer .pub .api .configuration .ads .AdRules ;
96import com .jwplayer .pub .api .configuration .ads .AdvertisingConfig ;
107import com .jwplayer .pub .api .configuration .ads .VastAdvertisingConfig ;
11- import com .jwplayer .pub .api .configuration .ads .dai .ImaDaiAdvertisingConfig ;
12- import com .jwplayer .pub .api .configuration .ads .ima .ImaAdvertisingConfig ;
138import com .jwplayer .pub .api .media .ads .AdBreak ;
14- import com .jwplayer .pub .api .media .ads .dai .ImaDaiSettings ;
159
1610import java .util .ArrayList ;
17- import java .util .HashMap ;
1811import java .util .List ;
19- import java .util .Map ;
2012import java .util .Objects ;
2113
2214public class RNJWPlayerAds {
@@ -27,103 +19,43 @@ public static AdvertisingConfig getAdvertisingConfig(ReadableMap ads) {
2719 return null ;
2820 }
2921
30- String adClientType = ads .getString ("adClient" );
22+ // Check both "client" (JWPlayer JSON format) and "adClient" (RN wrapper format)
23+ String adClientType = null ;
24+ if (ads .hasKey ("adClient" )) {
25+ adClientType = ads .getString ("adClient" );
26+ } else if (ads .hasKey ("client" )) {
27+ adClientType = ads .getString ("client" );
28+ }
29+
30+ // Validate client type exists and is not null
31+ if (adClientType == null ) {
32+ throw new IllegalArgumentException ("Missing required 'adClient' or 'client' field in advertising config" );
33+ }
34+
35+ // Normalize to lowercase for case-insensitive matching
36+ adClientType = adClientType .toLowerCase ();
37+
3138 switch (adClientType ) {
3239 case "ima" :
33- try {
34- return configureImaAdvertising (ads );
35- } catch (Exception e ) {
36- throw new RuntimeException (e );
37- }
40+ case "googima" : // Support legacy "googima" format
3841 case "ima_dai" :
42+ // Delegate to ImaHelper (implementation selected by Gradle)
43+ // Note: Only parse adSchedule for regular IMA, not for DAI (ads are embedded in stream)
44+ List <AdBreak > adSchedule = ("ima" .equals (adClientType ) || "googima" .equals (adClientType ))
45+ ? getAdSchedule (ads )
46+ : new ArrayList <>();
3947 try {
40- return configureImaDaiAdvertising (ads );
41- } catch (Exception e ) {
42- throw new RuntimeException (e );
48+ return ImaHelper .configureImaOrDai (ads , adSchedule );
49+ } catch (RuntimeException e ) {
50+ // IMA not enabled - log error and return null (graceful degradation)
51+ android .util .Log .e ("RNJWPlayerAds" , "Failed to configure IMA ads: " + e .getMessage ());
52+ return null ;
4353 }
4454 default : // Defaulting to VAST
4555 return configureVastAdvertising (ads );
4656 }
4757 }
4858
49- // Configure IMA Advertising
50- private static ImaAdvertisingConfig configureImaAdvertising (ReadableMap ads ) throws Exception {
51- if (!BuildConfig .USE_IMA ) {
52- throw new Exception ("Error: Google ads services is not installed. Add RNJWPlayerUseGoogleIMA = true to your app/build.gradle ext {}" );
53- }
54-
55- ImaAdvertisingConfig .Builder builder = new ImaAdvertisingConfig .Builder ();
56-
57- List <AdBreak > adScheduleList = getAdSchedule (ads );
58- builder .schedule (adScheduleList );
59-
60- if (ads .hasKey ("imaSettings" )) {
61- builder .imaSdkSettings (getImaSettings (Objects .requireNonNull (ads .getMap ("imaSettings" ))));
62- }
63-
64- // companionSlots
65-
66- return builder .build ();
67- }
68-
69- // Configure IMA DAI Advertising
70- private static ImaDaiAdvertisingConfig configureImaDaiAdvertising (ReadableMap ads ) throws Exception {
71- if (!BuildConfig .USE_IMA ) {
72- throw new Exception ("Error: Google ads services is not installed. Add RNJWPlayerUseGoogleIMA = true to your app/build.gradle ext {}" );
73- }
74-
75- ImaDaiAdvertisingConfig .Builder builder = new ImaDaiAdvertisingConfig .Builder ();
76-
77- if (ads .hasKey ("imaSettings" )) {
78- builder .imaSdkSettings (getImaSettings (Objects .requireNonNull (ads .getMap ("imaSettings" ))));
79- }
80-
81- if (ads .hasKey ("imaDaiSettings" )) {
82- builder .imaDaiSettings (getImaDaiSettings (Objects .requireNonNull (ads .getMap ("imaDaiSettings" ))));
83- }
84-
85- return builder .build ();
86- }
87-
88- // You'll need to implement this method based on how you pass ImaDaiSettings from React Native
89- private static ImaDaiSettings getImaDaiSettings (ReadableMap imaDaiSettingsMap ) {
90- String videoId = imaDaiSettingsMap .hasKey ("videoId" ) ? imaDaiSettingsMap .getString ("videoId" ) : null ;
91- String cmsId = imaDaiSettingsMap .hasKey ("cmsId" ) ? imaDaiSettingsMap .getString ("cmsId" ) : null ;
92- String assetKey = imaDaiSettingsMap .hasKey ("assetKey" ) ? imaDaiSettingsMap .getString ("assetKey" ) : null ;
93- String apiKey = imaDaiSettingsMap .hasKey ("apiKey" ) ? imaDaiSettingsMap .getString ("apiKey" ) : null ;
94-
95- // Extracting adTagParameters from imaDaiSettingsMap if present
96- Map <String , String > adTagParameters = null ;
97- if (imaDaiSettingsMap .hasKey ("adTagParameters" ) && imaDaiSettingsMap .getMap ("adTagParameters" ) != null ) {
98- adTagParameters = new HashMap <>();
99- ReadableMap adTagParamsMap = imaDaiSettingsMap .getMap ("adTagParameters" );
100- for (Map .Entry <String , Object > entry : adTagParamsMap .toHashMap ().entrySet ()) {
101- if (entry .getValue () instanceof String ) {
102- adTagParameters .put (entry .getKey (), (String ) entry .getValue ());
103- }
104- }
105- }
106-
107- // Handling streamType
108- ImaDaiSettings .StreamType streamType = ImaDaiSettings .StreamType .HLS ; // Default to HLS
109- if (imaDaiSettingsMap .hasKey ("streamType" )) {
110- String streamTypeStr = imaDaiSettingsMap .getString ("streamType" );
111- if ("DASH" .equalsIgnoreCase (streamTypeStr )) {
112- streamType = ImaDaiSettings .StreamType .DASH ;
113- }
114- }
115- // Create ImaDaiSettings based on the provided values
116- ImaDaiSettings imaDaiSettings = (assetKey != null ) ?
117- new ImaDaiSettings (assetKey , streamType , apiKey ) :
118- new ImaDaiSettings (videoId , cmsId , streamType , apiKey );
119-
120- if (adTagParameters != null ) {
121- imaDaiSettings .setAdTagParameters (adTagParameters );
122- }
123-
124- return imaDaiSettings ;
125- }
126-
12759 // Configure VAST Advertising
12860 private static VastAdvertisingConfig configureVastAdvertising (ReadableMap ads ) {
12961 VastAdvertisingConfig .Builder builder = new VastAdvertisingConfig .Builder ();
@@ -154,9 +86,23 @@ private static VastAdvertisingConfig configureVastAdvertising(ReadableMap ads) {
15486
15587 private static List <AdBreak > getAdSchedule (ReadableMap ads ) {
15688 List <AdBreak > adScheduleList = new ArrayList <>();
89+
90+ // Check if adSchedule exists
91+ if (!ads .hasKey ("adSchedule" )) {
92+ return adScheduleList ; // Return empty list
93+ }
94+
15795 ReadableArray adSchedule = ads .getArray ("adSchedule" );
96+ if (adSchedule == null ) {
97+ return adScheduleList ; // Return empty list if null
98+ }
99+
158100 for (int i = 0 ; i < adSchedule .size (); i ++) {
159101 ReadableMap adBreakProp = adSchedule .getMap (i );
102+ // Skip null entries in the adSchedule array
103+ if (adBreakProp == null ) {
104+ continue ;
105+ }
160106 String offset = adBreakProp .hasKey ("offset" ) ? adBreakProp .getString ("offset" ) : "pre" ;
161107 if (adBreakProp .hasKey ("tag" )) {
162108 AdBreak adBreak = new AdBreak .Builder ()
@@ -202,38 +148,4 @@ private static String mapStartOnSeek(String startOnSeek) {
202148 return AdRules .RULES_START_ON_SEEK_NONE ;
203149 }
204150
205- // public static List<FriendlyObstruction> getFriendlyObstructions(ReadableArray obstructionsArray) {
206- // List<FriendlyObstruction> obstructions = new ArrayList<>();
207- // // Example: Parse and create FriendlyObstruction objects from obstructionsArray
208- // return obstructions;
209- // }
210-
211- public static ImaSdkSettings getImaSettings (ReadableMap imaSettingsMap ) {
212- ImaSdkSettings settings = ImaSdkFactory .getInstance ().createImaSdkSettings ();
213-
214- if (imaSettingsMap .hasKey ("maxRedirects" )) {
215- settings .setMaxRedirects (imaSettingsMap .getInt ("maxRedirects" ));
216- }
217- if (imaSettingsMap .hasKey ("language" )) {
218- settings .setLanguage (imaSettingsMap .getString ("language" ));
219- }
220- if (imaSettingsMap .hasKey ("ppid" )) {
221- settings .setPpid (imaSettingsMap .getString ("ppid" ));
222- }
223- if (imaSettingsMap .hasKey ("playerType" )) {
224- settings .setPlayerType (imaSettingsMap .getString ("playerType" ));
225- }
226- if (imaSettingsMap .hasKey ("playerVersion" )) {
227- settings .setPlayerVersion (imaSettingsMap .getString ("playerVersion" ));
228- }
229- if (imaSettingsMap .hasKey ("sessionId" )) {
230- settings .setSessionId (imaSettingsMap .getString ("sessionId" ));
231- }
232- if (imaSettingsMap .hasKey ("debugMode" )) {
233- settings .setDebugMode (imaSettingsMap .getBoolean ("debugMode" ));
234- }
235- // Add other settings as needed
236-
237- return settings ;
238- }
239151}
0 commit comments