react-native-applovin-max 3.2.2 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,7 @@ buildscript {
16
16
  }
17
17
 
18
18
  dependencies {
19
- classpath 'com.android.tools.build:gradle:3.5.2'
19
+ classpath 'com.android.tools.build:gradle:3.5.4'
20
20
  // noinspection DifferentKotlinGradleVersion
21
21
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
22
22
 
@@ -41,8 +41,8 @@ android {
41
41
  defaultConfig {
42
42
  minSdkVersion 16
43
43
  targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
44
- versionCode 3020200
45
- versionName "3.2.2"
44
+ versionCode 3030100
45
+ versionName "3.3.1"
46
46
  }
47
47
 
48
48
  flavorDimensions("default")
@@ -150,5 +150,5 @@ dependencies {
150
150
 
151
151
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
152
152
 
153
- implementation 'com.applovin:applovin-sdk:11.4.4'
153
+ implementation 'com.applovin:applovin-sdk:11.5.1'
154
154
  }
@@ -19,9 +19,14 @@ class AppLovinMAXAdView
19
19
  {
20
20
  private final ThemedReactContext reactContext;
21
21
 
22
- private MaxAdView adView;
23
- private int currentWidthPx;
24
- private int currentHeightPx;
22
+ private @Nullable MaxAdView adView;
23
+
24
+ private String adUnitId;
25
+ private MaxAdFormat adFormat;
26
+ private @Nullable String placement;
27
+ private @Nullable String customData;
28
+ private boolean adaptiveBannerEnabled;
29
+ private boolean autoRefresh;
25
30
 
26
31
  public AppLovinMAXAdView(final Context context)
27
32
  {
@@ -29,6 +34,93 @@ class AppLovinMAXAdView
29
34
  this.reactContext = (ThemedReactContext) context;
30
35
  }
31
36
 
37
+ public void setAdUnitId(final String value)
38
+ {
39
+ // Ad Unit ID must be set prior to creating MaxAdView
40
+ if ( adView != null )
41
+ {
42
+ AppLovinMAXModule.e( "Attempting to set Ad Unit ID " + value + " after MaxAdView is created" );
43
+ return;
44
+ }
45
+
46
+ adUnitId = value;
47
+
48
+ maybeAttachAdView();
49
+ }
50
+
51
+ public void setAdFormat(final String value)
52
+ {
53
+ // Ad format must be set prior to creating MaxAdView
54
+ if ( adView != null )
55
+ {
56
+ AppLovinMAXModule.e( "Attempting to set ad format " + value + " after MaxAdView is created" );
57
+ return;
58
+ }
59
+
60
+ if ( "banner".equals( value ) )
61
+ {
62
+ adFormat = AppLovinMAXModule.getDeviceSpecificBannerAdViewAdFormat( reactContext );
63
+ }
64
+ else if ( "mrec".equals( value ) )
65
+ {
66
+ adFormat = MaxAdFormat.MREC;
67
+ }
68
+ else
69
+ {
70
+ AppLovinMAXModule.e( "Attempting to set an invalid ad format of \"" + value + "\" for " + adUnitId );
71
+ return;
72
+ }
73
+
74
+ maybeAttachAdView();
75
+ }
76
+
77
+ public void setPlacement(@Nullable final String value)
78
+ {
79
+ placement = value;
80
+
81
+ if ( adView != null )
82
+ {
83
+ adView.setPlacement( placement );
84
+ }
85
+ }
86
+
87
+ public void setCustomData(@Nullable final String value)
88
+ {
89
+ customData = value;
90
+
91
+ if ( adView != null )
92
+ {
93
+ adView.setCustomData( customData );
94
+ }
95
+ }
96
+
97
+ public void setAdaptiveBannerEnabled(final boolean enabled)
98
+ {
99
+ adaptiveBannerEnabled = enabled;
100
+
101
+ if ( adView != null )
102
+ {
103
+ adView.setExtraParameter( "adaptive_banner", Boolean.toString( enabled ) );
104
+ }
105
+ }
106
+
107
+ public void setAutoRefresh(final boolean enabled)
108
+ {
109
+ autoRefresh = enabled;
110
+
111
+ if ( adView != null )
112
+ {
113
+ if ( autoRefresh )
114
+ {
115
+ adView.startAutoRefresh();
116
+ }
117
+ else
118
+ {
119
+ adView.stopAutoRefresh();
120
+ }
121
+ }
122
+ }
123
+
32
124
  @Override
33
125
  public void requestLayout()
34
126
  {
@@ -38,6 +130,9 @@ class AppLovinMAXAdView
38
130
  // This is required to ensure ad refreshes render correctly in RN Android due to known issue where `getWidth()` and `getHeight()` return 0 on attach
39
131
  if ( adView != null )
40
132
  {
133
+ int currentWidthPx = getWidth();
134
+ int currentHeightPx = getHeight();
135
+
41
136
  adView.measure(
42
137
  MeasureSpec.makeMeasureSpec( currentWidthPx, MeasureSpec.EXACTLY ),
43
138
  MeasureSpec.makeMeasureSpec( currentHeightPx, MeasureSpec.EXACTLY )
@@ -68,13 +163,7 @@ class AppLovinMAXAdView
68
163
  }
69
164
  }
70
165
 
71
- @Nullable
72
- protected MaxAdView getAdView()
73
- {
74
- return adView;
75
- }
76
-
77
- public void maybeAttachAdView(final String placement, final String customData, final String adaptiveBannerEnabledStr, final String adUnitId, final MaxAdFormat adFormat)
166
+ private void maybeAttachAdView()
78
167
  {
79
168
  final Activity currentActivity = reactContext.getCurrentActivity();
80
169
  if ( currentActivity == null )
@@ -83,42 +172,70 @@ class AppLovinMAXAdView
83
172
  return;
84
173
  }
85
174
 
86
- // Run in next run loop when view is laid out and `getWidth()` / `getHeight()` has appropriate values
87
- post( new Runnable()
88
- {
89
- @Override
90
- public void run()
175
+ // Re-assign in case of race condition
176
+ final String adUnitId = this.adUnitId;
177
+ final MaxAdFormat adFormat = this.adFormat;
178
+
179
+ // Run after 0.25 sec delay to allow all properties to set
180
+ postDelayed( () -> {
181
+
182
+ if ( TextUtils.isEmpty( adUnitId ) )
183
+ {
184
+ AppLovinMAXModule.e( "Attempting to attach MaxAdView without Ad Unit ID" );
185
+ return;
186
+ }
187
+
188
+ if ( adFormat == null )
189
+ {
190
+ AppLovinMAXModule.e( "Attempting to attach MaxAdView without ad format" );
191
+ return;
192
+ }
193
+
194
+ if ( adView != null )
195
+ {
196
+ AppLovinMAXModule.e( "Attempting to re-attach with existing MaxAdView: " + adView );
197
+ return;
198
+ }
199
+
200
+ AppLovinMAXModule.d( "Attaching MaxAdView for " + adUnitId );
201
+
202
+ adView = new MaxAdView( adUnitId, adFormat, AppLovinMAXModule.getInstance().getSdk(), currentActivity );
203
+ adView.setListener( AppLovinMAXModule.getInstance() );
204
+ adView.setRevenueListener( AppLovinMAXModule.getInstance() );
205
+ adView.setPlacement( placement );
206
+ adView.setCustomData( customData );
207
+ adView.setExtraParameter( "adaptive_banner", Boolean.toString( adaptiveBannerEnabled ) );
208
+ // Set this extra parameter to work around a SDK bug that ignores calls to stopAutoRefresh()
209
+ adView.setExtraParameter( "allow_pause_auto_refresh_immediately", "true" );
210
+
211
+ if ( autoRefresh )
212
+ {
213
+ adView.startAutoRefresh();
214
+ }
215
+ else
91
216
  {
92
- // If ad unit id and format has been set - create and attach AdView
93
- if ( !TextUtils.isEmpty( adUnitId ) && adFormat != null )
94
- {
95
- adView = new MaxAdView( adUnitId, adFormat, AppLovinMAXModule.getInstance().getSdk(), currentActivity );
96
- adView.setListener( AppLovinMAXModule.getInstance() );
97
- adView.setRevenueListener( AppLovinMAXModule.getInstance() );
98
-
99
- if ( placement != null )
100
- {
101
- adView.setPlacement( placement );
102
- }
103
-
104
- if ( customData != null )
105
- {
106
- adView.setCustomData( customData );
107
- }
108
-
109
- if ( adaptiveBannerEnabledStr != null )
110
- {
111
- adView.setExtraParameter( "adaptive_banner", adaptiveBannerEnabledStr );
112
- }
113
-
114
- adView.loadAd();
115
-
116
- currentWidthPx = getWidth();
117
- currentHeightPx = getHeight();
118
-
119
- addView( adView );
120
- }
217
+ adView.stopAutoRefresh();
121
218
  }
122
- } );
219
+
220
+ adView.loadAd();
221
+
222
+ addView( adView );
223
+ }, 250 );
224
+ }
225
+
226
+ public void destroy()
227
+ {
228
+ if ( adView != null )
229
+ {
230
+ AppLovinMAXModule.d( "Unmounting MaxAdView: " + adView );
231
+
232
+ removeView( adView );
233
+
234
+ adView.setListener( null );
235
+ adView.setRevenueListener( null );
236
+ adView.destroy();
237
+
238
+ adView = null;
239
+ }
123
240
  }
124
241
  }
@@ -1,18 +1,12 @@
1
1
  package com.applovin.reactnative;
2
2
 
3
- import com.applovin.mediation.MaxAdFormat;
4
- import com.applovin.mediation.ads.MaxAdView;
5
3
  import com.facebook.react.bridge.ReactApplicationContext;
6
- import com.facebook.react.bridge.ReadableArray;
7
4
  import com.facebook.react.uimanager.SimpleViewManager;
8
5
  import com.facebook.react.uimanager.ThemedReactContext;
6
+ import com.facebook.react.uimanager.annotations.ReactProp;
9
7
 
10
8
  import org.jetbrains.annotations.NotNull;
11
9
 
12
- import java.util.HashMap;
13
- import java.util.Map;
14
-
15
- import androidx.annotation.NonNull;
16
10
  import androidx.annotation.Nullable;
17
11
 
18
12
  /**
@@ -21,167 +15,63 @@ import androidx.annotation.Nullable;
21
15
  class AppLovinMAXAdViewManager
22
16
  extends SimpleViewManager<AppLovinMAXAdView>
23
17
  {
24
- // Parent fields
25
- private final ReactApplicationContext reactApplicationContext;
26
-
27
- // Maps from the view to the corresponding ad unit id and ad format.
28
- // Both must be set before the MaxAdView is created.
29
- private final Map<AppLovinMAXAdView, String> adUnitIdRegistry = new HashMap<>();
30
- private final Map<AppLovinMAXAdView, MaxAdFormat> adFormatRegistry = new HashMap<>();
31
-
32
- // Storage for placement and extra parameters if set before the MAAdView is created
33
- private final Map<AppLovinMAXAdView, String> placementRegistry = new HashMap<>();
34
- private final Map<AppLovinMAXAdView, String> customDataRegistry = new HashMap<>();
35
- private final Map<AppLovinMAXAdView, String> adaptiveBannerEnabledRegistry = new HashMap<>();
36
-
37
- public AppLovinMAXAdViewManager(final ReactApplicationContext reactApplicationContext)
38
- {
39
- this.reactApplicationContext = reactApplicationContext;
40
- }
18
+ public AppLovinMAXAdViewManager(final ReactApplicationContext reactApplicationContext) { }
41
19
 
20
+ @NotNull
42
21
  @Override
43
- public @NotNull String getName()
22
+ public String getName()
44
23
  {
45
24
  return "AppLovinMAXAdView";
46
25
  }
47
26
 
27
+ @NotNull
48
28
  @Override
49
- protected @NotNull AppLovinMAXAdView createViewInstance(@NotNull final ThemedReactContext reactContext)
29
+ protected AppLovinMAXAdView createViewInstance(@NotNull final ThemedReactContext reactContext)
50
30
  {
51
31
  // NOTE: Do not set frame or backgroundColor as RN will overwrite the values set by your custom class in order to match your JavaScript component's layout props - hence wrapper
52
32
  return new AppLovinMAXAdView( reactContext );
53
33
  }
54
34
 
55
- @Override
56
- public void receiveCommand(@NonNull AppLovinMAXAdView view, String commandId, @Nullable ReadableArray args)
35
+ @ReactProp(name = "placement")
36
+ public void setPlacement(final AppLovinMAXAdView view, @Nullable final String placement)
57
37
  {
58
- if ( args == null ) return;
59
-
60
- String arg = args.getString( 0 );
61
- if ( arg == null ) return;
62
-
63
- if ( "setPlacement".equals( commandId ) )
64
- {
65
- setPlacement( view, arg );
66
- }
67
- else if ( "setAdaptiveBannerEnabled".equals( commandId ) )
68
- {
69
- setAdaptiveBannerEnabled( view, arg );
70
- }
71
- else if ( "setCustomData".equals( commandId ) )
72
- {
73
- setCustomData( view, arg );
74
- }
75
- else if ( "setAdUnitId".equals( commandId ) )
76
- {
77
- setAdUnitId( view, arg );
78
- }
79
- else if ( "setAdFormat".equals( commandId ) )
80
- {
81
- setAdFormat( view, arg );
82
- }
83
- else
84
- {
85
- AppLovinMAXModule.e( "Unable to parse command: " + commandId + " for AdView: + " + view + " with args: " + args );
86
- }
38
+ view.setPlacement( placement );
87
39
  }
88
40
 
89
- public void setPlacement(final AppLovinMAXAdView view, final String placement)
41
+ @ReactProp(name = "customData")
42
+ public void setCustomData(final AppLovinMAXAdView view, @Nullable final String customData)
90
43
  {
91
- // Post to main thread to avoid race condition with actual creation of MaxAdView in maybeAttachAdView()
92
- view.post( () -> {
93
-
94
- MaxAdView adView = view.getAdView();
95
- if ( adView != null )
96
- {
97
- adView.setPlacement( placement );
98
- }
99
- else
100
- {
101
- placementRegistry.put( view, placement );
102
- }
103
- } );
44
+ view.setCustomData( customData );
104
45
  }
105
46
 
106
- public void setCustomData(final AppLovinMAXAdView view, final String customData)
47
+ @ReactProp(name = "adaptiveBannerEnabled")
48
+ public void setAdaptiveBannerEnabled(final AppLovinMAXAdView view, final boolean enabled)
107
49
  {
108
- // Post to main thread to avoid race condition with actual creation of MaxAdView in maybeAttachAdView()
109
- view.post( () -> {
110
-
111
- MaxAdView adView = view.getAdView();
112
- if ( adView != null )
113
- {
114
- adView.setCustomData( customData );
115
- }
116
- else
117
- {
118
- customDataRegistry.put( view, customData );
119
- }
120
- } );
50
+ view.setAdaptiveBannerEnabled( enabled );
121
51
  }
122
52
 
123
- public void setAdaptiveBannerEnabled(final AppLovinMAXAdView view, final String enabledStr)
53
+ @ReactProp(name = "autoRefresh")
54
+ public void setAutoRefresh(final AppLovinMAXAdView view, final boolean enabled)
124
55
  {
125
- // Post to main thread to avoid race condition with actual creation of MaxAdView in maybeAttachAdView()
126
- view.post( () -> {
127
-
128
- MaxAdView adView = view.getAdView();
129
- if ( adView != null )
130
- {
131
- adView.setExtraParameter( "adaptive_banner", enabledStr );
132
- }
133
- else
134
- {
135
- adaptiveBannerEnabledRegistry.put( view, enabledStr );
136
- }
137
- } );
56
+ view.setAutoRefresh( enabled );
138
57
  }
139
58
 
59
+ @ReactProp(name = "adUnitId")
140
60
  public void setAdUnitId(final AppLovinMAXAdView view, final String adUnitId)
141
61
  {
142
- adUnitIdRegistry.put( view, adUnitId );
143
- maybeAttachAdView( view );
62
+ view.setAdUnitId( adUnitId );
144
63
  }
145
64
 
65
+ @ReactProp(name = "adFormat")
146
66
  public void setAdFormat(final AppLovinMAXAdView view, final String adFormatStr)
147
67
  {
148
- if ( "banner".equals( adFormatStr ) )
149
- {
150
- adFormatRegistry.put( view, AppLovinMAXModule.getDeviceSpecificBannerAdViewAdFormat( reactApplicationContext ) );
151
- }
152
- else if ( "mrec".equals( adFormatStr ) )
153
- {
154
- adFormatRegistry.put( view, MaxAdFormat.MREC );
155
- }
156
-
157
- maybeAttachAdView( view );
158
- }
159
-
160
- private void maybeAttachAdView(final AppLovinMAXAdView view)
161
- {
162
- String placement = placementRegistry.remove( view );
163
- String customData = customDataRegistry.remove( view );
164
- String adaptiveBannerEnabledStr = adaptiveBannerEnabledRegistry.remove( view );
165
-
166
- view.maybeAttachAdView( placement,
167
- customData,
168
- adaptiveBannerEnabledStr,
169
- adUnitIdRegistry.get( view ),
170
- adFormatRegistry.get( view ) );
68
+ view.setAdFormat( adFormatStr );
171
69
  }
172
70
 
173
71
  @Override
174
72
  public void onDropViewInstance(@NotNull AppLovinMAXAdView view)
175
73
  {
176
- // NOTE: Android destroys the native MaxAdView and calls this method while iOS caches it when you remove it from screen
177
- adUnitIdRegistry.remove( view );
178
-
179
- // HACK ALERT: Since current SDK does not respect auto-refresh APIs until _after_ `onAdLoaded()`, explicitly expose view validity to the main module
180
- MaxAdView adView = view.getAdView();
181
- if ( adView != null )
182
- {
183
- AppLovinMAXModule.sAdViewsToRemove.put( adView.getAdUnitId(), adView );
184
- }
74
+ view.destroy();
185
75
 
186
76
  super.onDropViewInstance( view );
187
77
  }