react-native-applovin-max 5.7.1 → 5.7.2

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.
@@ -0,0 +1,1711 @@
1
+ package com.applovin.mediation.adapters;
2
+
3
+ import android.annotation.SuppressLint;
4
+ import android.app.Activity;
5
+ import android.content.Context;
6
+ import android.graphics.drawable.Drawable;
7
+ import android.os.Bundle;
8
+ import android.preference.PreferenceManager;
9
+ import android.text.TextUtils;
10
+ import android.util.DisplayMetrics;
11
+ import android.view.Display;
12
+ import android.view.View;
13
+ import android.view.ViewGroup;
14
+ import android.view.WindowManager;
15
+ import android.widget.ImageView;
16
+
17
+ import com.applovin.impl.sdk.utils.BundleUtils;
18
+ import com.applovin.mediation.MaxAdFormat;
19
+ import com.applovin.mediation.MaxReward;
20
+ import com.applovin.mediation.adapter.MaxAdViewAdapter;
21
+ import com.applovin.mediation.adapter.MaxAdapterError;
22
+ import com.applovin.mediation.adapter.MaxInterstitialAdapter;
23
+ import com.applovin.mediation.adapter.MaxRewardedAdapter;
24
+ import com.applovin.mediation.adapter.MaxRewardedInterstitialAdapter;
25
+ import com.applovin.mediation.adapter.MaxSignalProvider;
26
+ import com.applovin.mediation.adapter.listeners.MaxAdViewAdapterListener;
27
+ import com.applovin.mediation.adapter.listeners.MaxAppOpenAdapterListener;
28
+ import com.applovin.mediation.adapter.listeners.MaxInterstitialAdapterListener;
29
+ import com.applovin.mediation.adapter.listeners.MaxNativeAdAdapterListener;
30
+ import com.applovin.mediation.adapter.listeners.MaxRewardedAdapterListener;
31
+ import com.applovin.mediation.adapter.listeners.MaxRewardedInterstitialAdapterListener;
32
+ import com.applovin.mediation.adapter.listeners.MaxSignalCollectionListener;
33
+ import com.applovin.mediation.adapter.parameters.MaxAdapterInitializationParameters;
34
+ import com.applovin.mediation.adapter.parameters.MaxAdapterParameters;
35
+ import com.applovin.mediation.adapter.parameters.MaxAdapterResponseParameters;
36
+ import com.applovin.mediation.adapter.parameters.MaxAdapterSignalCollectionParameters;
37
+ //import com.applovin.mediation.adapters.google.BuildConfig;
38
+ import com.applovin.mediation.nativeAds.MaxNativeAd;
39
+ import com.applovin.mediation.nativeAds.MaxNativeAdView;
40
+ import com.applovin.sdk.AppLovinSdk;
41
+ import com.applovin.sdk.AppLovinSdkUtils;
42
+ import com.google.ads.mediation.admob.AdMobAdapter;
43
+ import com.google.android.gms.ads.AdError;
44
+ import com.google.android.gms.ads.AdFormat;
45
+ import com.google.android.gms.ads.AdListener;
46
+ import com.google.android.gms.ads.AdLoader;
47
+ import com.google.android.gms.ads.AdRequest;
48
+ import com.google.android.gms.ads.AdSize;
49
+ import com.google.android.gms.ads.AdView;
50
+ import com.google.android.gms.ads.FullScreenContentCallback;
51
+ import com.google.android.gms.ads.LoadAdError;
52
+ import com.google.android.gms.ads.MediaContent;
53
+ import com.google.android.gms.ads.MobileAds;
54
+ import com.google.android.gms.ads.OnUserEarnedRewardListener;
55
+ import com.google.android.gms.ads.RequestConfiguration;
56
+ import com.google.android.gms.ads.ResponseInfo;
57
+ import com.google.android.gms.ads.appopen.AppOpenAd;
58
+ import com.google.android.gms.ads.initialization.AdapterStatus;
59
+ import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
60
+ import com.google.android.gms.ads.interstitial.InterstitialAd;
61
+ import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
62
+ import com.google.android.gms.ads.nativead.MediaView;
63
+ import com.google.android.gms.ads.nativead.NativeAd;
64
+ import com.google.android.gms.ads.nativead.NativeAd.OnNativeAdLoadedListener;
65
+ import com.google.android.gms.ads.nativead.NativeAdOptions;
66
+ import com.google.android.gms.ads.nativead.NativeAdView;
67
+ import com.google.android.gms.ads.query.QueryInfo;
68
+ import com.google.android.gms.ads.query.QueryInfoGenerationCallback;
69
+ import com.google.android.gms.ads.rewarded.RewardItem;
70
+ import com.google.android.gms.ads.rewarded.RewardedAd;
71
+ import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
72
+ import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
73
+ import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
74
+
75
+ import java.lang.ref.WeakReference;
76
+ import java.util.Arrays;
77
+ import java.util.Collections;
78
+ import java.util.List;
79
+ import java.util.Map;
80
+ import java.util.concurrent.atomic.AtomicBoolean;
81
+
82
+ import androidx.annotation.NonNull;
83
+ import androidx.annotation.Nullable;
84
+
85
+ import static com.applovin.sdk.AppLovinSdkUtils.runOnUiThread;
86
+
87
+ /**
88
+ * This is a mediation adapterWrapper for Google Play Services
89
+ * <p>
90
+ * Created by basil on 12/7/16.
91
+ */
92
+ public class GoogleMediationAdapter
93
+ extends MediationAdapterBase
94
+ implements MaxSignalProvider, MaxInterstitialAdapter, /* MaxAppOpenAdapter */ MaxRewardedInterstitialAdapter, MaxRewardedAdapter, MaxAdViewAdapter /* MaxNativeAdAdapter */
95
+ {
96
+ private static final int TITLE_LABEL_TAG = 1;
97
+ private static final int MEDIA_VIEW_CONTAINER_TAG = 2;
98
+ private static final int ICON_VIEW_TAG = 3;
99
+ private static final int BODY_VIEW_TAG = 4;
100
+ private static final int CALL_TO_ACTION_VIEW_TAG = 5;
101
+ private static final int ADVERTISER_VIEW_TAG = 8;
102
+
103
+ private static final AtomicBoolean initialized = new AtomicBoolean();
104
+ private static InitializationStatus status;
105
+
106
+ private InterstitialAd interstitialAd;
107
+ private AppOpenAd appOpenAd;
108
+ private InterstitialAd appOpenInterstitialAd;
109
+ private RewardedInterstitialAd rewardedInterstitialAd;
110
+ private RewardedAd rewardedAd;
111
+ private AdView adView;
112
+ private NativeAd nativeAd;
113
+ private NativeAdView nativeAdView;
114
+
115
+ private AppOpenAdListener appOpenAdListener;
116
+ private AppOpenAdListener appOpenInterstitialAdListener;
117
+ private RewardedInterstitialAdListener rewardedInterstitialAdListener;
118
+ private RewardedAdListener rewardedAdListener;
119
+
120
+ // Explicit default constructor declaration
121
+ public GoogleMediationAdapter(final AppLovinSdk sdk) { super( sdk ); }
122
+
123
+ //region MaxAdapter Methods
124
+
125
+ @SuppressLint("MissingPermission")
126
+ @Override
127
+ public void initialize(final MaxAdapterInitializationParameters parameters, final Activity activity, final OnCompletionListener onCompletionListener)
128
+ {
129
+ log( "Initializing Google SDK..." );
130
+
131
+ if ( initialized.compareAndSet( false, true ) )
132
+ {
133
+ Context context = getContext( activity );
134
+
135
+ // Prevent AdMob SDK from auto-initing its adapters in AB testing environments.
136
+ // NOTE: If MAX makes an ad request to AdMob, and the AdMob account has AL enabled (e.g. AppLovin Bidding) _and_ detects the AdMob<->AppLovin adapter, AdMob will still attempt to initialize AppLovin
137
+ MobileAds.disableMediationAdapterInitialization( context );
138
+
139
+ if ( parameters.getServerParameters().getBoolean( "init_without_callback", false ) )
140
+ {
141
+ status = InitializationStatus.DOES_NOT_APPLY;
142
+
143
+ MobileAds.initialize( context );
144
+
145
+ onCompletionListener.onCompletion( status, null );
146
+ }
147
+ else
148
+ {
149
+ status = InitializationStatus.INITIALIZING;
150
+
151
+ MobileAds.initialize( context, new OnInitializationCompleteListener()
152
+ {
153
+ @Override
154
+ public void onInitializationComplete(@NonNull final com.google.android.gms.ads.initialization.InitializationStatus initializationStatus)
155
+ {
156
+ final AdapterStatus googleAdsStatus = initializationStatus.getAdapterStatusMap().get( "com.google.android.gms.ads.MobileAds" );
157
+ final AdapterStatus.State googleAdsState = googleAdsStatus != null ? googleAdsStatus.getInitializationState() : null;
158
+ log( "Initialization complete with status " + googleAdsState );
159
+
160
+ // NOTE: We were able to load ads even when SDK is in "not ready" init state...
161
+ // AdMob SDK when status "not ready": "The mediation adapter is LESS likely to fill ad requests."
162
+ status = ( AdapterStatus.State.READY == googleAdsState ) ? InitializationStatus.INITIALIZED_SUCCESS : InitializationStatus.INITIALIZED_UNKNOWN;
163
+ onCompletionListener.onCompletion( status, null );
164
+ }
165
+ } );
166
+ }
167
+ }
168
+ else
169
+ {
170
+ onCompletionListener.onCompletion( status, null );
171
+ }
172
+ }
173
+
174
+ @Override
175
+ public String getSdkVersion()
176
+ {
177
+ return String.valueOf( MobileAds.getVersion() );
178
+ }
179
+
180
+ @Override
181
+ public String getAdapterVersion()
182
+ {
183
+ return "22.1.0.2"; //BuildConfig.VERSION_NAME;
184
+ }
185
+
186
+ @Override
187
+ public void onDestroy()
188
+ {
189
+ log( "Destroy called for adapter " + this );
190
+
191
+ if ( interstitialAd != null )
192
+ {
193
+ interstitialAd.setFullScreenContentCallback( null );
194
+ interstitialAd = null;
195
+ }
196
+
197
+ if ( appOpenAd != null )
198
+ {
199
+ appOpenAd.setFullScreenContentCallback( null );
200
+ appOpenAd = null;
201
+ appOpenAdListener = null;
202
+ }
203
+
204
+ if ( appOpenInterstitialAd != null )
205
+ {
206
+ appOpenInterstitialAd.setFullScreenContentCallback( null );
207
+ appOpenInterstitialAd = null;
208
+ appOpenInterstitialAdListener = null;
209
+ }
210
+
211
+ if ( rewardedInterstitialAd != null )
212
+ {
213
+ rewardedInterstitialAd.setFullScreenContentCallback( null );
214
+ rewardedInterstitialAd = null;
215
+ rewardedInterstitialAdListener = null;
216
+ }
217
+
218
+ if ( rewardedAd != null )
219
+ {
220
+ rewardedAd.setFullScreenContentCallback( null );
221
+ rewardedAd = null;
222
+ rewardedAdListener = null;
223
+ }
224
+
225
+ if ( adView != null )
226
+ {
227
+ adView.destroy();
228
+ adView = null;
229
+ }
230
+
231
+ if ( nativeAd != null )
232
+ {
233
+ nativeAd.destroy();
234
+ nativeAd = null;
235
+ }
236
+
237
+ if ( nativeAdView != null )
238
+ {
239
+ nativeAdView.destroy();
240
+ nativeAdView = null;
241
+ }
242
+ }
243
+
244
+ //endregion
245
+
246
+ //region MaxSignalProvider Methods
247
+
248
+ @Override
249
+ public void collectSignal(final MaxAdapterSignalCollectionParameters parameters, final Activity activity, final MaxSignalCollectionListener callback)
250
+ {
251
+ setRequestConfiguration( parameters );
252
+
253
+ Context context = getContext( activity );
254
+
255
+ AdRequest adRequest = createAdRequestWithParameters( true, parameters.getAdFormat(), parameters, context );
256
+
257
+ QueryInfo.generate( context, toAdFormat( parameters ), adRequest, new QueryInfoGenerationCallback()
258
+ {
259
+ @Override
260
+ public void onSuccess(@NonNull final QueryInfo queryInfo)
261
+ {
262
+ log( "Signal collection successful" );
263
+ callback.onSignalCollected( queryInfo.getQuery() );
264
+ }
265
+
266
+ @Override
267
+ public void onFailure(@NonNull final String errorMessage)
268
+ {
269
+ log( "Signal collection failed with error: " + errorMessage );
270
+ callback.onSignalCollectionFailed( errorMessage );
271
+ }
272
+ } );
273
+ }
274
+
275
+ //endregion
276
+
277
+ //region MaxInterstitialAdapter Methods
278
+
279
+ @Override
280
+ public void loadInterstitialAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxInterstitialAdapterListener listener)
281
+ {
282
+ final String placementId = parameters.getThirdPartyAdPlacementId();
283
+ boolean isBiddingAd = AppLovinSdkUtils.isValidString( parameters.getBidResponse() );
284
+ log( "Loading " + ( isBiddingAd ? "bidding " : "" ) + "interstitial ad: " + placementId + "..." );
285
+
286
+ updateMuteState( parameters );
287
+ setRequestConfiguration( parameters );
288
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, MaxAdFormat.INTERSTITIAL, parameters, activity );
289
+
290
+ InterstitialAd.load( activity, placementId, adRequest, new InterstitialAdLoadCallback()
291
+ {
292
+ @Override
293
+ public void onAdLoaded(@NonNull final InterstitialAd ad)
294
+ {
295
+ log( "Interstitial ad loaded: " + placementId + "..." );
296
+
297
+ interstitialAd = ad;
298
+ interstitialAd.setFullScreenContentCallback( new InterstitialAdListener( placementId, listener ) );
299
+
300
+ ResponseInfo responseInfo = interstitialAd.getResponseInfo();
301
+ String responseId = ( responseInfo != null ) ? responseInfo.getResponseId() : null;
302
+ if ( AppLovinSdk.VERSION_CODE >= 9150000 && AppLovinSdkUtils.isValidString( responseId ) )
303
+ {
304
+ Bundle extraInfo = new Bundle( 1 );
305
+ extraInfo.putString( "creative_id", responseId );
306
+
307
+ listener.onInterstitialAdLoaded( extraInfo );
308
+ }
309
+ else
310
+ {
311
+ listener.onInterstitialAdLoaded();
312
+ }
313
+ }
314
+
315
+ @Override
316
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
317
+ {
318
+ MaxAdapterError adapterError = toMaxError( loadAdError );
319
+ log( "Interstitial ad (" + placementId + ") failed to load with error: " + adapterError );
320
+ listener.onInterstitialAdLoadFailed( adapterError );
321
+ }
322
+ } );
323
+ }
324
+
325
+ @Override
326
+ public void showInterstitialAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxInterstitialAdapterListener listener)
327
+ {
328
+ String placementId = parameters.getThirdPartyAdPlacementId();
329
+ log( "Showing interstitial ad: " + placementId + "..." );
330
+
331
+ if ( interstitialAd != null )
332
+ {
333
+ interstitialAd.show( activity );
334
+ }
335
+ else
336
+ {
337
+ log( "Interstitial ad failed to show: " + placementId );
338
+ listener.onInterstitialAdDisplayFailed( new MaxAdapterError( -4205, "Ad Display Failed", 0, "Interstitial ad not ready" ) );
339
+ }
340
+ }
341
+
342
+ //endregion
343
+
344
+ //region MaxAppOpenAdapter Methods
345
+
346
+ // @Override
347
+ public void loadAppOpenAd(final MaxAdapterResponseParameters parameters, @Nullable Activity activity, final MaxAppOpenAdapterListener listener)
348
+ {
349
+ final String placementId = parameters.getThirdPartyAdPlacementId();
350
+ boolean isBiddingAd = AppLovinSdkUtils.isValidString( parameters.getBidResponse() );
351
+ boolean isInterstitial = parameters.getServerParameters().getBoolean( "is_inter_placement" );
352
+ log( "Loading " + ( isBiddingAd ? "bidding " : "" ) + "app open " + ( isInterstitial ? "interstitial " : "" ) + "ad: " + placementId + "..." );
353
+
354
+ updateMuteState( parameters );
355
+ setRequestConfiguration( parameters );
356
+ Context context = getContext( activity );
357
+
358
+ if ( isInterstitial )
359
+ {
360
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, MaxAdFormat.INTERSTITIAL, parameters, activity );
361
+ InterstitialAd.load( context, placementId, adRequest, new InterstitialAdLoadCallback()
362
+ {
363
+ @Override
364
+ public void onAdLoaded(@NonNull final InterstitialAd ad)
365
+ {
366
+ log( "App open interstitial ad loaded: " + placementId + "..." );
367
+
368
+ appOpenInterstitialAd = ad;
369
+ appOpenInterstitialAdListener = new AppOpenAdListener( placementId, listener );
370
+ appOpenInterstitialAd.setFullScreenContentCallback( appOpenInterstitialAdListener );
371
+
372
+ ResponseInfo responseInfo = appOpenInterstitialAd.getResponseInfo();
373
+ String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
374
+ if ( AppLovinSdkUtils.isValidString( responseId ) )
375
+ {
376
+ Bundle extraInfo = new Bundle( 1 );
377
+ extraInfo.putString( "creative_id", responseId );
378
+
379
+ listener.onAppOpenAdLoaded( extraInfo );
380
+ }
381
+ else
382
+ {
383
+ listener.onAppOpenAdLoaded();
384
+ }
385
+ }
386
+
387
+ @Override
388
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
389
+ {
390
+ MaxAdapterError adapterError = toMaxError( loadAdError );
391
+ log( "App open interstitial ad (" + placementId + ") failed to load with error: " + adapterError );
392
+ listener.onAppOpenAdLoadFailed( adapterError );
393
+ }
394
+ } );
395
+ }
396
+ else
397
+ {
398
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, MaxAdFormat.APP_OPEN, parameters, activity );
399
+ int orientation = AppLovinSdkUtils.getOrientation( context );
400
+ AppOpenAd.load( context, placementId, adRequest, orientation, new AppOpenAd.AppOpenAdLoadCallback()
401
+ {
402
+ @Override
403
+ public void onAdLoaded(@NonNull final AppOpenAd ad)
404
+ {
405
+ log( "App open ad loaded: " + placementId + "..." );
406
+
407
+ appOpenAd = ad;
408
+ appOpenAdListener = new AppOpenAdListener( placementId, listener );
409
+ ad.setFullScreenContentCallback( appOpenAdListener );
410
+
411
+ ResponseInfo responseInfo = appOpenAd.getResponseInfo();
412
+ String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
413
+ if ( AppLovinSdkUtils.isValidString( responseId ) )
414
+ {
415
+ Bundle extraInfo = new Bundle( 1 );
416
+ extraInfo.putString( "creative_id", responseId );
417
+
418
+ listener.onAppOpenAdLoaded( extraInfo );
419
+ }
420
+ else
421
+ {
422
+ listener.onAppOpenAdLoaded();
423
+ }
424
+ }
425
+
426
+ @Override
427
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
428
+ {
429
+ MaxAdapterError adapterError = toMaxError( loadAdError );
430
+ log( "App open ad (" + placementId + ") failed to load with error: " + adapterError );
431
+ listener.onAppOpenAdLoadFailed( adapterError );
432
+ }
433
+ } );
434
+ }
435
+ }
436
+
437
+ // @Override
438
+ public void showAppOpenAd(final MaxAdapterResponseParameters parameters, @Nullable final Activity activity, final MaxAppOpenAdapterListener listener)
439
+ {
440
+ String placementId = parameters.getThirdPartyAdPlacementId();
441
+ boolean isInterstitial = parameters.getServerParameters().getBoolean( "is_inter_placement" );
442
+ log( "Showing app open " + ( isInterstitial ? "interstitial " : "" ) + "ad: " + placementId + "..." );
443
+
444
+ // Shows ad with null activity properly as tested in SDK version 21.1.0
445
+ if ( appOpenInterstitialAd != null )
446
+ {
447
+ appOpenInterstitialAd.show( activity );
448
+ }
449
+ else if ( appOpenAd != null )
450
+ {
451
+ appOpenAd.show( activity );
452
+ }
453
+ else
454
+ {
455
+ log( "App open ad failed to show: " + placementId );
456
+ listener.onAppOpenAdLoadFailed( new MaxAdapterError( -4205, "Ad display failed", 0, "App open ad not ready" ) );
457
+ }
458
+ }
459
+
460
+ //endregion
461
+
462
+ //region MaxRewardedInterstitialAdapter Methods
463
+
464
+ @Override
465
+ public void loadRewardedInterstitialAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxRewardedInterstitialAdapterListener listener)
466
+ {
467
+ final String placementId = parameters.getThirdPartyAdPlacementId();
468
+ boolean isBiddingAd = AppLovinSdkUtils.isValidString( parameters.getBidResponse() );
469
+ log( "Loading " + ( isBiddingAd ? "bidding " : "" ) + "rewarded interstitial ad: " + placementId + "..." );
470
+
471
+ updateMuteState( parameters );
472
+ setRequestConfiguration( parameters );
473
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, MaxAdFormat.REWARDED_INTERSTITIAL, parameters, activity );
474
+
475
+ RewardedInterstitialAd.load( activity, placementId, adRequest, new RewardedInterstitialAdLoadCallback()
476
+ {
477
+ @Override
478
+ public void onAdLoaded(@NonNull final RewardedInterstitialAd ad)
479
+ {
480
+ log( "Rewarded interstitial ad loaded: " + placementId );
481
+
482
+ rewardedInterstitialAd = ad;
483
+ rewardedInterstitialAdListener = new RewardedInterstitialAdListener( placementId, listener );
484
+ rewardedInterstitialAd.setFullScreenContentCallback( rewardedInterstitialAdListener );
485
+
486
+ ResponseInfo responseInfo = rewardedInterstitialAd.getResponseInfo();
487
+ String responseId = ( responseInfo != null ) ? responseInfo.getResponseId() : null;
488
+ if ( AppLovinSdk.VERSION_CODE > 9150000 && AppLovinSdkUtils.isValidString( responseId ) )
489
+ {
490
+ Bundle extraInfo = new Bundle( 1 );
491
+ extraInfo.putString( "creative_id", responseId );
492
+
493
+ listener.onRewardedInterstitialAdLoaded( extraInfo );
494
+ }
495
+ else
496
+ {
497
+ listener.onRewardedInterstitialAdLoaded();
498
+ }
499
+ }
500
+
501
+ @Override
502
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
503
+ {
504
+ MaxAdapterError adapterError = toMaxError( loadAdError );
505
+ log( "Rewarded interstitial ad (" + placementId + ") failed to load with error: " + adapterError );
506
+ listener.onRewardedInterstitialAdLoadFailed( adapterError );
507
+ }
508
+ } );
509
+ }
510
+
511
+ @Override
512
+ public void showRewardedInterstitialAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxRewardedInterstitialAdapterListener listener)
513
+ {
514
+ final String placementId = parameters.getThirdPartyAdPlacementId();
515
+ log( "Showing rewarded interstitial ad: " + placementId + "..." );
516
+
517
+ if ( rewardedInterstitialAd != null )
518
+ {
519
+ configureReward( parameters );
520
+ rewardedInterstitialAd.show( activity, new OnUserEarnedRewardListener()
521
+ {
522
+ @Override
523
+ public void onUserEarnedReward(@NonNull final RewardItem rewardItem)
524
+ {
525
+ log( "Rewarded interstitial ad user earned reward: " + placementId );
526
+ rewardedInterstitialAdListener.hasGrantedReward = true;
527
+ }
528
+ } );
529
+ }
530
+ else
531
+ {
532
+ log( "Rewarded interstitial ad failed to show: " + placementId );
533
+ listener.onRewardedInterstitialAdDisplayFailed( new MaxAdapterError( -4205, "Ad Display Failed", 0, "Rewarded Interstitial ad not ready" ) );
534
+ }
535
+ }
536
+
537
+ //endregion
538
+
539
+ //region MaxRewardedAdapter Methods
540
+
541
+ @Override
542
+ public void loadRewardedAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxRewardedAdapterListener listener)
543
+ {
544
+ final String placementId = parameters.getThirdPartyAdPlacementId();
545
+ boolean isBiddingAd = AppLovinSdkUtils.isValidString( parameters.getBidResponse() );
546
+ log( "Loading " + ( isBiddingAd ? "bidding " : "" ) + "rewarded ad: " + placementId + "..." );
547
+
548
+ updateMuteState( parameters );
549
+ setRequestConfiguration( parameters );
550
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, MaxAdFormat.REWARDED, parameters, activity );
551
+
552
+ RewardedAd.load( activity, placementId, adRequest, new RewardedAdLoadCallback()
553
+ {
554
+ @Override
555
+ public void onAdLoaded(@NonNull final RewardedAd ad)
556
+ {
557
+ log( "Rewarded ad loaded: " + placementId + "..." );
558
+
559
+ rewardedAd = ad;
560
+ rewardedAdListener = new RewardedAdListener( placementId, listener );
561
+ rewardedAd.setFullScreenContentCallback( rewardedAdListener );
562
+
563
+ ResponseInfo responseInfo = rewardedAd.getResponseInfo();
564
+ String responseId = ( responseInfo != null ) ? responseInfo.getResponseId() : null;
565
+ if ( AppLovinSdk.VERSION_CODE >= 9150000 && AppLovinSdkUtils.isValidString( responseId ) )
566
+ {
567
+ Bundle extraInfo = new Bundle( 1 );
568
+ extraInfo.putString( "creative_id", responseId );
569
+
570
+ listener.onRewardedAdLoaded( extraInfo );
571
+ }
572
+ else
573
+ {
574
+ listener.onRewardedAdLoaded();
575
+ }
576
+ }
577
+
578
+ @Override
579
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
580
+ {
581
+ MaxAdapterError adapterError = toMaxError( loadAdError );
582
+ log( "Rewarded ad (" + placementId + ") failed to load with error: " + adapterError );
583
+ listener.onRewardedAdLoadFailed( adapterError );
584
+ }
585
+ } );
586
+ }
587
+
588
+ @Override
589
+ public void showRewardedAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxRewardedAdapterListener listener)
590
+ {
591
+ final String placementId = parameters.getThirdPartyAdPlacementId();
592
+ log( "Showing rewarded ad: " + placementId + "..." );
593
+
594
+ if ( rewardedAd != null )
595
+ {
596
+ configureReward( parameters );
597
+
598
+ rewardedAd.show( activity, new OnUserEarnedRewardListener()
599
+ {
600
+ @Override
601
+ public void onUserEarnedReward(@NonNull final RewardItem rewardItem)
602
+ {
603
+ log( "Rewarded ad user earned reward: " + placementId );
604
+ rewardedAdListener.hasGrantedReward = true;
605
+ }
606
+ } );
607
+ }
608
+ else
609
+ {
610
+ log( "Rewarded ad failed to show: " + placementId );
611
+ listener.onRewardedAdDisplayFailed( new MaxAdapterError( -4205, "Ad Display Failed", 0, "Rewarded ad not ready" ) );
612
+ }
613
+ }
614
+
615
+ //endregion
616
+
617
+ //region MaxAdViewAdapter Methods
618
+
619
+ @SuppressLint("MissingPermission")
620
+ @Override
621
+ public void loadAdViewAd(final MaxAdapterResponseParameters parameters, final MaxAdFormat adFormat, final Activity activity, final MaxAdViewAdapterListener listener)
622
+ {
623
+ String placementId = parameters.getThirdPartyAdPlacementId();
624
+ boolean isBiddingAd = AppLovinSdkUtils.isValidString( parameters.getBidResponse() );
625
+ boolean isNative = parameters.getServerParameters().getBoolean( "is_native" );
626
+ log( "Loading " + ( isBiddingAd ? "bidding " : "" ) + ( isNative ? "native " : "" ) + adFormat.getLabel() + " ad for placement id: " + placementId + "..." );
627
+
628
+ setRequestConfiguration( parameters );
629
+
630
+ Context context = getContext( activity );
631
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, adFormat, parameters, context );
632
+
633
+ if ( isNative )
634
+ {
635
+ NativeAdOptions.Builder nativeAdOptionsBuilder = new NativeAdOptions.Builder();
636
+ nativeAdOptionsBuilder.setAdChoicesPlacement( getAdChoicesPlacement( parameters ) );
637
+ nativeAdOptionsBuilder.setRequestMultipleImages( adFormat == MaxAdFormat.MREC ); // MRECs can handle multiple images via AdMob's media view
638
+
639
+ // NOTE: Activity context needed on older SDKs
640
+ NativeAdViewListener nativeAdViewListener = new NativeAdViewListener( parameters, adFormat, activity, listener );
641
+ AdLoader adLoader = new AdLoader.Builder( context, placementId )
642
+ .withNativeAdOptions( nativeAdOptionsBuilder.build() )
643
+ .forNativeAd( nativeAdViewListener )
644
+ .withAdListener( nativeAdViewListener )
645
+ .build();
646
+
647
+ adLoader.loadAd( adRequest );
648
+ }
649
+ else
650
+ {
651
+ adView = new AdView( context );
652
+ adView.setAdUnitId( placementId );
653
+ adView.setAdListener( new AdViewListener( placementId, adFormat, listener ) );
654
+
655
+ // Check if adaptive banner sizes should be used
656
+ boolean isAdaptiveBanner;
657
+ if ( isBiddingAd )
658
+ {
659
+ // Temporarily manually disable adaptive banner traffic for Google bidding until they resolve sizing issue
660
+ isAdaptiveBanner = false;
661
+ }
662
+ else
663
+ {
664
+ isAdaptiveBanner = parameters.getServerParameters().getBoolean( "adaptive_banner", false );
665
+ }
666
+
667
+ adView.setAdSize( toAdSize( adFormat, isAdaptiveBanner, parameters, context ) );
668
+
669
+ adView.loadAd( adRequest );
670
+ }
671
+ }
672
+
673
+ //endregion
674
+
675
+ //region MaxNativeAdAdapter Methods
676
+
677
+ @Override
678
+ @SuppressLint("MissingPermission")
679
+ public void loadNativeAd(final MaxAdapterResponseParameters parameters, final Activity activity, final MaxNativeAdAdapterListener listener)
680
+ {
681
+ String placementId = parameters.getThirdPartyAdPlacementId();
682
+ boolean isBiddingAd = AppLovinSdkUtils.isValidString( parameters.getBidResponse() );
683
+ log( "Loading " + ( isBiddingAd ? "bidding " : "" ) + " native ad for placement id: " + placementId + "..." );
684
+
685
+ setRequestConfiguration( parameters );
686
+
687
+ // NOTE: `activity` can only be null in 11.1.0+, and `getApplicationContext()` is introduced in 11.1.0
688
+ Context applicationContext = ( activity != null ) ? activity.getApplicationContext() : getApplicationContext();
689
+ AdRequest adRequest = createAdRequestWithParameters( isBiddingAd, MaxAdFormat.NATIVE, parameters, applicationContext );
690
+
691
+ NativeAdOptions.Builder nativeAdOptionsBuilder = new NativeAdOptions.Builder();
692
+ nativeAdOptionsBuilder.setAdChoicesPlacement( getAdChoicesPlacement( parameters ) );
693
+
694
+ // Medium templates can handle multiple images via AdMob's media view
695
+ String template = BundleUtils.getString( "template", "", parameters.getServerParameters() );
696
+ nativeAdOptionsBuilder.setRequestMultipleImages( template.contains( "medium" ) );
697
+
698
+ NativeAdListener nativeAdListener = new NativeAdListener( parameters, applicationContext, listener );
699
+ AdLoader adLoader = new AdLoader.Builder( applicationContext, placementId )
700
+ .withNativeAdOptions( nativeAdOptionsBuilder.build() )
701
+ .forNativeAd( nativeAdListener )
702
+ .withAdListener( nativeAdListener )
703
+ .build();
704
+
705
+ adLoader.loadAd( adRequest );
706
+ }
707
+
708
+ //endregion
709
+
710
+ //region Helper Methods
711
+
712
+ private static MaxAdapterError toMaxError(final AdError googleAdsError)
713
+ {
714
+ int googleErrorCode = googleAdsError.getCode();
715
+ MaxAdapterError adapterError = MaxAdapterError.UNSPECIFIED;
716
+ switch ( googleErrorCode )
717
+ {
718
+ case AdRequest.ERROR_CODE_NO_FILL:
719
+ case AdRequest.ERROR_CODE_MEDIATION_NO_FILL:
720
+ adapterError = MaxAdapterError.NO_FILL;
721
+ break;
722
+ case AdRequest.ERROR_CODE_NETWORK_ERROR:
723
+ adapterError = MaxAdapterError.NO_CONNECTION;
724
+ break;
725
+ case AdRequest.ERROR_CODE_INTERNAL_ERROR:
726
+ adapterError = MaxAdapterError.INTERNAL_ERROR;
727
+ break;
728
+ case AdRequest.ERROR_CODE_INVALID_REQUEST:
729
+ case AdRequest.ERROR_CODE_REQUEST_ID_MISMATCH:
730
+ adapterError = MaxAdapterError.BAD_REQUEST;
731
+ break;
732
+ case AdRequest.ERROR_CODE_APP_ID_MISSING:
733
+ case AdRequest.ERROR_CODE_INVALID_AD_STRING:
734
+ adapterError = MaxAdapterError.INVALID_CONFIGURATION;
735
+ break;
736
+ }
737
+
738
+ return new MaxAdapterError( adapterError.getErrorCode(),
739
+ adapterError.getErrorMessage(),
740
+ googleErrorCode,
741
+ googleAdsError.getMessage() );
742
+ }
743
+
744
+ private AdSize toAdSize(final MaxAdFormat adFormat,
745
+ final boolean isAdaptiveBanner,
746
+ final MaxAdapterParameters parameters,
747
+ final Context context)
748
+ {
749
+ if ( adFormat == MaxAdFormat.BANNER || adFormat == MaxAdFormat.LEADER )
750
+ {
751
+ if ( isAdaptiveBanner )
752
+ {
753
+ return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( context, getAdaptiveBannerWidth( parameters, context ) );
754
+ }
755
+ else
756
+ {
757
+ return adFormat == MaxAdFormat.BANNER ? AdSize.BANNER : AdSize.LEADERBOARD;
758
+ }
759
+ }
760
+ else if ( adFormat == MaxAdFormat.MREC )
761
+ {
762
+ return AdSize.MEDIUM_RECTANGLE;
763
+ }
764
+ else
765
+ {
766
+ throw new IllegalArgumentException( "Unsupported ad format: " + adFormat );
767
+ }
768
+ }
769
+
770
+ private int getAdaptiveBannerWidth(final MaxAdapterParameters parameters, final Context context)
771
+ {
772
+ if ( AppLovinSdk.VERSION_CODE >= 11_00_00_00 )
773
+ {
774
+ final Map<String, Object> localExtraParameters = parameters.getLocalExtraParameters();
775
+ Object widthObj = localExtraParameters.get( "adaptive_banner_width" );
776
+ if ( widthObj instanceof Integer )
777
+ {
778
+ return (int) widthObj;
779
+ }
780
+ else if ( widthObj != null )
781
+ {
782
+ e( "Expected parameter \"adaptive_banner_width\" to be of type Integer, received: " + widthObj.getClass() );
783
+ }
784
+ }
785
+
786
+ WindowManager windowManager = (WindowManager) context.getSystemService( Context.WINDOW_SERVICE );
787
+ Display display = windowManager.getDefaultDisplay();
788
+ DisplayMetrics outMetrics = new DisplayMetrics();
789
+ display.getMetrics( outMetrics );
790
+
791
+ return AppLovinSdkUtils.pxToDp( context, outMetrics.widthPixels );
792
+ }
793
+
794
+ private AdFormat toAdFormat(final MaxAdapterSignalCollectionParameters parameters)
795
+ {
796
+ MaxAdFormat adFormat = parameters.getAdFormat();
797
+ boolean isNative = parameters.getServerParameters().getBoolean( "is_native" ) || adFormat == MaxAdFormat.NATIVE;
798
+ if ( isNative )
799
+ {
800
+ return AdFormat.NATIVE;
801
+ }
802
+ else if ( adFormat.isAdViewAd() )
803
+ {
804
+ return AdFormat.BANNER;
805
+ }
806
+ else if ( adFormat == MaxAdFormat.INTERSTITIAL )
807
+ {
808
+ return AdFormat.INTERSTITIAL;
809
+ }
810
+ else if ( adFormat == MaxAdFormat.REWARDED )
811
+ {
812
+ return AdFormat.REWARDED;
813
+ }
814
+ else if ( adFormat == MaxAdFormat.REWARDED_INTERSTITIAL )
815
+ {
816
+ return AdFormat.REWARDED_INTERSTITIAL;
817
+ }
818
+ else
819
+ {
820
+ return AdFormat.UNKNOWN;
821
+ }
822
+ }
823
+
824
+ private void setRequestConfiguration(final MaxAdapterParameters parameters)
825
+ {
826
+ RequestConfiguration.Builder requestConfigurationBuilder = MobileAds.getRequestConfiguration().toBuilder();
827
+
828
+ Boolean isAgeRestrictedUser = parameters.isAgeRestrictedUser();
829
+ if ( isAgeRestrictedUser != null )
830
+ {
831
+ int ageRestrictedUserTag = isAgeRestrictedUser ? RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE : RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE;
832
+ requestConfigurationBuilder.setTagForChildDirectedTreatment( ageRestrictedUserTag );
833
+ }
834
+
835
+ MobileAds.setRequestConfiguration( requestConfigurationBuilder.build() );
836
+ }
837
+
838
+ @SuppressLint("ApplySharedPref")
839
+ private AdRequest createAdRequestWithParameters(final boolean isBiddingAd, final MaxAdFormat adFormat, final MaxAdapterParameters parameters, final Context context)
840
+ {
841
+ AdRequest.Builder requestBuilder = new AdRequest.Builder();
842
+ Bundle serverParameters = parameters.getServerParameters();
843
+ Bundle networkExtras = new Bundle( 6 );
844
+ boolean isDv360Bidding = false;
845
+
846
+ if ( isBiddingAd )
847
+ {
848
+ String bidderType = BundleUtils.getString( "bidder", "", serverParameters );
849
+ if ( "dv360".equalsIgnoreCase( bidderType ) )
850
+ {
851
+ isDv360Bidding = true;
852
+ }
853
+
854
+ // Requested by Google for signal collection
855
+ networkExtras.putString( "query_info_type", isDv360Bidding ? "requester_type_3" : "requester_type_2" );
856
+
857
+ // Temporarily manually disable adaptive banner traffic for Google bidding until they resolve sizing issue
858
+ // if ( AppLovinSdk.VERSION_CODE >= 11_00_00_00 && adFormat.isAdViewAd() )
859
+ // {
860
+ // Object isAdaptiveBannerObj = parameters.getLocalExtraParameters().get( "adaptive_banner" );
861
+ // if ( isAdaptiveBannerObj instanceof String && "true".equalsIgnoreCase( (String) isAdaptiveBannerObj ) )
862
+ // {
863
+ // AdSize adaptiveAdSize = toAdSize( adFormat, true, parameters, context );
864
+ // networkExtras.putInt( "adaptive_banner_w", adaptiveAdSize.getWidth() );
865
+ // networkExtras.putInt( "adaptive_banner_h", adaptiveAdSize.getHeight() );
866
+ // }
867
+ // }
868
+
869
+ if ( parameters instanceof MaxAdapterResponseParameters )
870
+ {
871
+ String bidResponse = ( (MaxAdapterResponseParameters) parameters ).getBidResponse();
872
+ if ( AppLovinSdkUtils.isValidString( bidResponse ) )
873
+ {
874
+ requestBuilder.setAdString( bidResponse );
875
+ }
876
+ }
877
+ }
878
+
879
+ if ( serverParameters.getBoolean( "set_mediation_identifier", true ) )
880
+ {
881
+ // Use "applovin" instead of mediationTag for Google's specs
882
+ // "applovin_dv360" is for DV360_BIDDING, which is a separate bidder from regular ADMOB_BIDDING
883
+ requestBuilder.setRequestAgent( isDv360Bidding ? "applovin_dv360" : "applovin" );
884
+ }
885
+
886
+ // Use event id as AdMob's placement request id
887
+ String eventId = BundleUtils.getString( "event_id", serverParameters );
888
+ if ( AppLovinSdkUtils.isValidString( eventId ) )
889
+ {
890
+ networkExtras.putString( "placement_req_id", eventId );
891
+ }
892
+
893
+ Boolean hasUserConsent = parameters.hasUserConsent();
894
+ if ( hasUserConsent != null && !hasUserConsent )
895
+ {
896
+ networkExtras.putString( "npa", "1" ); // Non-personalized ads
897
+ }
898
+
899
+ Boolean isDoNotSell = parameters.isDoNotSell();
900
+ if ( isDoNotSell != null && isDoNotSell )
901
+ {
902
+ networkExtras.putInt( "rdp", 1 ); // Restrict data processing - https://developers.google.com/admob/android/ccpa
903
+
904
+ PreferenceManager.getDefaultSharedPreferences( context )
905
+ .edit()
906
+ .putInt( "gad_rdp", 1 )
907
+ .commit();
908
+ }
909
+
910
+ if ( AppLovinSdk.VERSION_CODE >= 11_00_00_00 )
911
+ {
912
+ Map<String, Object> localExtraParameters = parameters.getLocalExtraParameters();
913
+
914
+ Object maxContentRating = localExtraParameters.get( "google_max_ad_content_rating" );
915
+ if ( maxContentRating instanceof String )
916
+ {
917
+ networkExtras.putString( "max_ad_content_rating", (String) maxContentRating );
918
+ }
919
+
920
+ Object contentUrlString = localExtraParameters.get( "google_content_url" );
921
+ if ( contentUrlString instanceof String )
922
+ {
923
+ requestBuilder.setContentUrl( (String) contentUrlString );
924
+ }
925
+
926
+ Object neighbouringContentUrlStringsObject = localExtraParameters.get( "google_neighbouring_content_url_strings" );
927
+ if ( neighbouringContentUrlStringsObject instanceof List )
928
+ {
929
+ // try-catching unsafe cast to List<String> in case an incorrect list type is set.
930
+ try
931
+ {
932
+ requestBuilder.setNeighboringContentUrls( (List<String>) neighbouringContentUrlStringsObject );
933
+ }
934
+ catch ( Throwable th )
935
+ {
936
+ e( "Neighbouring content URL strings extra param needs to be of type List<String>.", th );
937
+ }
938
+ }
939
+ }
940
+
941
+ requestBuilder.addNetworkExtrasBundle( AdMobAdapter.class, networkExtras );
942
+
943
+ return requestBuilder.build();
944
+ }
945
+
946
+ /**
947
+ * Update the global mute state for AdMob - must be done _before_ ad load to restrict inventory which requires playing with volume.
948
+ */
949
+ private static void updateMuteState(final MaxAdapterResponseParameters parameters)
950
+ {
951
+ Bundle serverParameters = parameters.getServerParameters();
952
+ // Overwritten by `mute_state` setting, unless `mute_state` is disabled
953
+ if ( serverParameters.containsKey( "is_muted" ) ) // Introduced in 9.10.0
954
+ {
955
+ MobileAds.setAppMuted( serverParameters.getBoolean( "is_muted" ) );
956
+ }
957
+ }
958
+
959
+ private int getAdChoicesPlacement(MaxAdapterResponseParameters parameters)
960
+ {
961
+ // Publishers can set via nativeAdLoader.setLocalExtraParameter( "admob_ad_choices_placement", ADCHOICES_BOTTOM_LEFT );
962
+ // Note: This feature requires AppLovin v11.0.0+
963
+ if ( AppLovinSdk.VERSION_CODE >= 11_00_00_00 )
964
+ {
965
+ final Map<String, Object> localExtraParams = parameters.getLocalExtraParameters();
966
+ final Object adChoicesPlacementObj = localExtraParams != null ? localExtraParams.get( "admob_ad_choices_placement" ) : null;
967
+
968
+ return isValidAdChoicesPlacement( adChoicesPlacementObj ) ? (Integer) adChoicesPlacementObj : NativeAdOptions.ADCHOICES_TOP_RIGHT;
969
+ }
970
+
971
+ return NativeAdOptions.ADCHOICES_TOP_RIGHT;
972
+ }
973
+
974
+ private boolean isValidAdChoicesPlacement(Object placementObj)
975
+ {
976
+ return ( placementObj instanceof Integer ) &&
977
+ ( (Integer) placementObj == NativeAdOptions.ADCHOICES_TOP_LEFT ||
978
+ (Integer) placementObj == NativeAdOptions.ADCHOICES_TOP_RIGHT ||
979
+ (Integer) placementObj == NativeAdOptions.ADCHOICES_BOTTOM_LEFT ||
980
+ (Integer) placementObj == NativeAdOptions.ADCHOICES_BOTTOM_RIGHT );
981
+ }
982
+
983
+ private Context getContext(@Nullable Activity activity)
984
+ {
985
+ // NOTE: `activity` can only be null in 11.1.0+, and `getApplicationContext()` is introduced in 11.1.0
986
+ return ( activity != null ) ? activity.getApplicationContext() : getApplicationContext();
987
+ }
988
+
989
+ //endregion
990
+
991
+ private class InterstitialAdListener
992
+ extends FullScreenContentCallback
993
+ {
994
+ private final String placementId;
995
+ private final MaxInterstitialAdapterListener listener;
996
+
997
+ InterstitialAdListener(final String placementId, final MaxInterstitialAdapterListener listener)
998
+ {
999
+ this.placementId = placementId;
1000
+ this.listener = listener;
1001
+ }
1002
+
1003
+ @Override
1004
+ public void onAdShowedFullScreenContent()
1005
+ {
1006
+ log( "Interstitial ad shown: " + placementId );
1007
+ }
1008
+
1009
+ @Override
1010
+ public void onAdFailedToShowFullScreenContent(@NonNull final AdError adError)
1011
+ {
1012
+ MaxAdapterError adapterError = new MaxAdapterError( -4205, "Ad Display Failed", adError.getCode(), adError.getMessage() );
1013
+ log( "Interstitial ad (" + placementId + ") failed to show with error: " + adapterError );
1014
+ listener.onInterstitialAdDisplayFailed( adapterError );
1015
+ }
1016
+
1017
+ @Override
1018
+ public void onAdImpression()
1019
+ {
1020
+ log( "Interstitial ad impression recorded: " + placementId );
1021
+ listener.onInterstitialAdDisplayed();
1022
+ }
1023
+
1024
+ @Override
1025
+ public void onAdClicked()
1026
+ {
1027
+ log( "Interstitial ad clicked: " + placementId );
1028
+ listener.onInterstitialAdClicked();
1029
+ }
1030
+
1031
+ @Override
1032
+ public void onAdDismissedFullScreenContent()
1033
+ {
1034
+ log( "Interstitial ad hidden: " + placementId );
1035
+ listener.onInterstitialAdHidden();
1036
+ }
1037
+ }
1038
+
1039
+ private class AppOpenAdListener
1040
+ extends FullScreenContentCallback
1041
+ {
1042
+ private final String placementId;
1043
+ private final MaxAppOpenAdapterListener listener;
1044
+
1045
+ AppOpenAdListener(final String placementId, final MaxAppOpenAdapterListener listener)
1046
+ {
1047
+ this.placementId = placementId;
1048
+ this.listener = listener;
1049
+ }
1050
+
1051
+ @Override
1052
+ public void onAdShowedFullScreenContent()
1053
+ {
1054
+ log( "App open ad shown: " + placementId );
1055
+ }
1056
+
1057
+ @Override
1058
+ public void onAdFailedToShowFullScreenContent(@NonNull final AdError adError)
1059
+ {
1060
+ MaxAdapterError adapterError = new MaxAdapterError( -4205, "Ad display failed", adError.getCode(), adError.getMessage() );
1061
+ log( "App open ad (" + placementId + ") failed to show with error: " + adapterError );
1062
+ listener.onAppOpenAdDisplayFailed( adapterError );
1063
+ }
1064
+
1065
+ @Override
1066
+ public void onAdImpression()
1067
+ {
1068
+ log( "App open ad impression recorded: " + placementId );
1069
+ listener.onAppOpenAdDisplayed();
1070
+ }
1071
+
1072
+ @Override
1073
+ public void onAdClicked()
1074
+ {
1075
+ log( "App open ad clicked: " + placementId );
1076
+ listener.onAppOpenAdClicked();
1077
+ }
1078
+
1079
+ @Override
1080
+ public void onAdDismissedFullScreenContent()
1081
+ {
1082
+ log( "App open ad hidden: " + placementId );
1083
+ listener.onAppOpenAdHidden();
1084
+ }
1085
+ }
1086
+
1087
+ private class RewardedInterstitialAdListener
1088
+ extends FullScreenContentCallback
1089
+ {
1090
+ private final String placementId;
1091
+ private final MaxRewardedInterstitialAdapterListener listener;
1092
+
1093
+ private boolean hasGrantedReward;
1094
+
1095
+ private RewardedInterstitialAdListener(final String placementId, final MaxRewardedInterstitialAdapterListener listener)
1096
+ {
1097
+ this.placementId = placementId;
1098
+ this.listener = listener;
1099
+ }
1100
+
1101
+ @Override
1102
+ public void onAdShowedFullScreenContent()
1103
+ {
1104
+ log( "Rewarded interstitial ad shown: " + placementId );
1105
+ listener.onRewardedInterstitialAdVideoStarted();
1106
+ }
1107
+
1108
+ @Override
1109
+ public void onAdFailedToShowFullScreenContent(@NonNull final AdError adError)
1110
+ {
1111
+ MaxAdapterError adapterError = new MaxAdapterError( -4205, "Ad Display Failed", adError.getCode(), adError.getMessage() );
1112
+ log( "Rewarded interstitial ad (" + placementId + ") failed to show with error: " + adapterError );
1113
+ listener.onRewardedInterstitialAdDisplayFailed( adapterError );
1114
+ }
1115
+
1116
+ @Override
1117
+ public void onAdImpression()
1118
+ {
1119
+ log( "Rewarded interstitial ad impression recorded: " + placementId );
1120
+ listener.onRewardedInterstitialAdDisplayed();
1121
+ }
1122
+
1123
+ @Override
1124
+ public void onAdClicked()
1125
+ {
1126
+ log( "Rewarded interstitial ad clicked: " + placementId );
1127
+ listener.onRewardedInterstitialAdClicked();
1128
+ }
1129
+
1130
+ @Override
1131
+ public void onAdDismissedFullScreenContent()
1132
+ {
1133
+ listener.onRewardedInterstitialAdVideoCompleted();
1134
+
1135
+ if ( hasGrantedReward || shouldAlwaysRewardUser() )
1136
+ {
1137
+ MaxReward reward = getReward();
1138
+ log( "Rewarded interstitial ad rewarded user with reward: " + reward );
1139
+ listener.onUserRewarded( reward );
1140
+ }
1141
+
1142
+ log( "Rewarded interstitial ad hidden: " + placementId );
1143
+ listener.onRewardedInterstitialAdHidden();
1144
+ }
1145
+ }
1146
+
1147
+ private class RewardedAdListener
1148
+ extends FullScreenContentCallback
1149
+ {
1150
+ private final String placementId;
1151
+ private final MaxRewardedAdapterListener listener;
1152
+
1153
+ private boolean hasGrantedReward;
1154
+
1155
+ RewardedAdListener(final String placementId, final MaxRewardedAdapterListener listener)
1156
+ {
1157
+ this.placementId = placementId;
1158
+ this.listener = listener;
1159
+ }
1160
+
1161
+ @Override
1162
+ public void onAdShowedFullScreenContent()
1163
+ {
1164
+ log( "Rewarded ad shown: " + placementId );
1165
+ listener.onRewardedAdVideoStarted();
1166
+ }
1167
+
1168
+ @Override
1169
+ public void onAdFailedToShowFullScreenContent(@NonNull final AdError adError)
1170
+ {
1171
+ MaxAdapterError adapterError = new MaxAdapterError( -4205, "Ad Display Failed", adError.getCode(), adError.getMessage() );
1172
+ log( "Rewarded ad (" + placementId + ") failed to show with error: " + adapterError );
1173
+ listener.onRewardedAdDisplayFailed( adapterError );
1174
+ }
1175
+
1176
+ @Override
1177
+ public void onAdImpression()
1178
+ {
1179
+ log( "Rewarded ad impression recorded: " + placementId );
1180
+ listener.onRewardedAdDisplayed();
1181
+ }
1182
+
1183
+ @Override
1184
+ public void onAdClicked()
1185
+ {
1186
+ log( "Rewarded ad clicked: " + placementId );
1187
+ listener.onRewardedAdClicked();
1188
+ }
1189
+
1190
+ @Override
1191
+ public void onAdDismissedFullScreenContent()
1192
+ {
1193
+ listener.onRewardedAdVideoCompleted();
1194
+
1195
+ if ( hasGrantedReward || shouldAlwaysRewardUser() )
1196
+ {
1197
+ MaxReward reward = getReward();
1198
+ log( "Rewarded user with reward: " + reward );
1199
+ listener.onUserRewarded( reward );
1200
+ }
1201
+
1202
+ log( "Rewarded ad hidden: " + placementId );
1203
+ listener.onRewardedAdHidden();
1204
+ }
1205
+ }
1206
+
1207
+ private class AdViewListener
1208
+ extends AdListener
1209
+ {
1210
+ final String placementId;
1211
+ final MaxAdFormat adFormat;
1212
+ final MaxAdViewAdapterListener listener;
1213
+
1214
+ AdViewListener(final String placementId, final MaxAdFormat adFormat, final MaxAdViewAdapterListener listener)
1215
+ {
1216
+ this.placementId = placementId;
1217
+ this.adFormat = adFormat;
1218
+ this.listener = listener;
1219
+ }
1220
+
1221
+ @Override
1222
+ public void onAdLoaded()
1223
+ {
1224
+ log( adFormat.getLabel() + " ad loaded: " + placementId );
1225
+
1226
+ if ( AppLovinSdk.VERSION_CODE >= 9150000 )
1227
+ {
1228
+ Bundle extraInfo = new Bundle( 3 );
1229
+
1230
+ ResponseInfo responseInfo = adView.getResponseInfo();
1231
+ String responseId = ( responseInfo != null ) ? responseInfo.getResponseId() : null;
1232
+ if ( AppLovinSdkUtils.isValidString( responseId ) )
1233
+ {
1234
+ extraInfo.putString( "creative_id", responseId );
1235
+ }
1236
+
1237
+ AdSize adSize = adView.getAdSize();
1238
+ if ( adSize != null )
1239
+ {
1240
+ extraInfo.putInt( "ad_width", adSize.getWidth() );
1241
+ extraInfo.putInt( "ad_height", adSize.getHeight() );
1242
+ }
1243
+
1244
+ listener.onAdViewAdLoaded( adView, extraInfo );
1245
+ }
1246
+ else
1247
+ {
1248
+ listener.onAdViewAdLoaded( adView );
1249
+ }
1250
+ }
1251
+
1252
+ @Override
1253
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
1254
+ {
1255
+ MaxAdapterError adapterError = toMaxError( loadAdError );
1256
+ log( adFormat.getLabel() + " ad (" + placementId + ") failed to load with error code: " + adapterError );
1257
+ listener.onAdViewAdLoadFailed( adapterError );
1258
+ }
1259
+
1260
+ @Override
1261
+ public void onAdImpression()
1262
+ {
1263
+ log( adFormat.getLabel() + " ad shown: " + placementId );
1264
+ listener.onAdViewAdDisplayed();
1265
+ }
1266
+
1267
+ @Override
1268
+ public void onAdOpened()
1269
+ {
1270
+ // Do not track ad view ad opened events (besides clicks) on Android, but do so on iOS
1271
+ log( adFormat.getLabel() + " ad opened" );
1272
+ listener.onAdViewAdClicked();
1273
+ }
1274
+
1275
+ @Override
1276
+ public void onAdClosed()
1277
+ {
1278
+ // NOTE: Do not track ad view ad closed events on Android, but do so on iOS
1279
+ log( adFormat.getLabel() + " ad closed" );
1280
+ }
1281
+ }
1282
+
1283
+ private class NativeAdViewListener
1284
+ extends AdListener
1285
+ implements OnNativeAdLoadedListener
1286
+ {
1287
+ final String placementId;
1288
+ final MaxAdFormat adFormat;
1289
+ final Bundle serverParameters;
1290
+ final WeakReference<Activity> activityRef;
1291
+ final MaxAdViewAdapterListener listener;
1292
+
1293
+ NativeAdViewListener(final MaxAdapterResponseParameters parameters, final MaxAdFormat adFormat, final Activity activity, final MaxAdViewAdapterListener listener)
1294
+ {
1295
+ placementId = parameters.getThirdPartyAdPlacementId();
1296
+ serverParameters = parameters.getServerParameters();
1297
+ activityRef = new WeakReference<>( activity );
1298
+
1299
+ this.adFormat = adFormat;
1300
+ this.listener = listener;
1301
+ }
1302
+
1303
+ @Override
1304
+ public void onNativeAdLoaded(@NonNull final NativeAd nativeAd)
1305
+ {
1306
+ log( "Native " + adFormat.getLabel() + " ad loaded: " + placementId );
1307
+
1308
+ if ( TextUtils.isEmpty( nativeAd.getHeadline() ) )
1309
+ {
1310
+ log( "Native " + adFormat.getLabel() + " ad failed to load: Google native ad is missing one or more required assets" );
1311
+ listener.onAdViewAdLoadFailed( MaxAdapterError.INVALID_CONFIGURATION );
1312
+
1313
+ nativeAd.destroy();
1314
+
1315
+ return;
1316
+ }
1317
+
1318
+ GoogleMediationAdapter.this.nativeAd = nativeAd;
1319
+
1320
+ final Activity activity = activityRef.get();
1321
+ final Context context = getContext( activity );
1322
+
1323
+ final MediaView mediaView = new MediaView( context );
1324
+ MediaContent mediaContent = nativeAd.getMediaContent();
1325
+ if ( mediaContent != null )
1326
+ {
1327
+ mediaView.setMediaContent( mediaContent );
1328
+ }
1329
+
1330
+ final NativeAd.Image icon = nativeAd.getIcon();
1331
+ MaxNativeAd.MaxNativeAdImage maxNativeAdImage = null;
1332
+ if ( icon != null )
1333
+ {
1334
+ if ( icon.getDrawable() != null )
1335
+ {
1336
+ maxNativeAdImage = new MaxNativeAd.MaxNativeAdImage( icon.getDrawable() );
1337
+ }
1338
+ else
1339
+ {
1340
+ maxNativeAdImage = new MaxNativeAd.MaxNativeAdImage( icon.getUri() );
1341
+ }
1342
+ }
1343
+
1344
+ final MaxNativeAd maxNativeAd = new MaxNativeAd.Builder()
1345
+ .setAdFormat( adFormat )
1346
+ .setTitle( nativeAd.getHeadline() )
1347
+ .setBody( nativeAd.getBody() )
1348
+ .setCallToAction( nativeAd.getCallToAction() )
1349
+ .setIcon( maxNativeAdImage )
1350
+ .setMediaView( mediaView )
1351
+ .build();
1352
+
1353
+ final String templateName = BundleUtils.getString( "template", "", serverParameters );
1354
+ if ( templateName.contains( "vertical" ) && AppLovinSdk.VERSION_CODE < 9140500 )
1355
+ {
1356
+ log( "Vertical native banners are only supported on MAX SDK 9.14.5 and above. Default horizontal native template will be used." );
1357
+ }
1358
+
1359
+ runOnUiThread( new Runnable()
1360
+ {
1361
+ @Override
1362
+ public void run()
1363
+ {
1364
+ MaxNativeAdView maxNativeAdView;
1365
+ if ( AppLovinSdk.VERSION_CODE >= 11010000 )
1366
+ {
1367
+ maxNativeAdView = new MaxNativeAdView( maxNativeAd, templateName, context );
1368
+ }
1369
+ else
1370
+ {
1371
+ maxNativeAdView = new MaxNativeAdView( maxNativeAd, templateName, activity );
1372
+ }
1373
+
1374
+ nativeAdView = new NativeAdView( context );
1375
+ nativeAdView.setIconView( maxNativeAdView.getIconContentView() );
1376
+ nativeAdView.setHeadlineView( maxNativeAdView.getTitleTextView() );
1377
+ nativeAdView.setBodyView( maxNativeAdView.getBodyTextView() );
1378
+ nativeAdView.setMediaView( mediaView );
1379
+ nativeAdView.setCallToActionView( maxNativeAdView.getCallToActionButton() );
1380
+ nativeAdView.setNativeAd( nativeAd );
1381
+
1382
+ nativeAdView.addView( maxNativeAdView );
1383
+
1384
+ ResponseInfo responseInfo = nativeAd.getResponseInfo();
1385
+ String responseId = ( responseInfo != null ) ? responseInfo.getResponseId() : null;
1386
+ if ( AppLovinSdk.VERSION_CODE >= 9150000 && AppLovinSdkUtils.isValidString( responseId ) )
1387
+ {
1388
+ Bundle extraInfo = new Bundle( 1 );
1389
+ extraInfo.putString( "creative_id", responseId );
1390
+
1391
+ listener.onAdViewAdLoaded( nativeAdView, extraInfo );
1392
+ }
1393
+ else
1394
+ {
1395
+ listener.onAdViewAdLoaded( nativeAdView );
1396
+ }
1397
+ }
1398
+ } );
1399
+ }
1400
+
1401
+ @Override
1402
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
1403
+ {
1404
+ MaxAdapterError adapterError = toMaxError( loadAdError );
1405
+ log( "Native " + adFormat.getLabel() + " ad (" + placementId + ") failed to load with error: " + adapterError );
1406
+ listener.onAdViewAdLoadFailed( adapterError );
1407
+ }
1408
+
1409
+ @Override
1410
+ public void onAdImpression()
1411
+ {
1412
+ log( "Native " + adFormat.getLabel() + " ad shown" );
1413
+ listener.onAdViewAdDisplayed();
1414
+ }
1415
+
1416
+ @Override
1417
+ public void onAdClicked()
1418
+ {
1419
+ log( "Native " + adFormat.getLabel() + " ad clicked" );
1420
+ listener.onAdViewAdClicked();
1421
+ }
1422
+
1423
+ @Override
1424
+ public void onAdOpened()
1425
+ {
1426
+ log( "Native " + adFormat.getLabel() + " ad opened" );
1427
+ listener.onAdViewAdExpanded();
1428
+ }
1429
+
1430
+ @Override
1431
+ public void onAdClosed()
1432
+ {
1433
+ log( "Native " + adFormat.getLabel() + " ad closed" );
1434
+ listener.onAdViewAdCollapsed();
1435
+ }
1436
+ }
1437
+
1438
+ private class NativeAdListener
1439
+ extends AdListener
1440
+ implements OnNativeAdLoadedListener
1441
+ {
1442
+ final String placementId;
1443
+ final Bundle serverParameters;
1444
+ final Context context;
1445
+ final MaxNativeAdAdapterListener listener;
1446
+
1447
+ public NativeAdListener(final MaxAdapterResponseParameters parameters, final Context context, final MaxNativeAdAdapterListener listener)
1448
+ {
1449
+ placementId = parameters.getThirdPartyAdPlacementId();
1450
+ serverParameters = parameters.getServerParameters();
1451
+
1452
+ this.context = context;
1453
+ this.listener = listener;
1454
+ }
1455
+
1456
+ @Override
1457
+ public void onNativeAdLoaded(@NonNull final NativeAd nativeAd)
1458
+ {
1459
+ log( "Native ad loaded: " + placementId );
1460
+
1461
+ GoogleMediationAdapter.this.nativeAd = nativeAd;
1462
+
1463
+ String templateName = BundleUtils.getString( "template", "", serverParameters );
1464
+ final boolean isTemplateAd = AppLovinSdkUtils.isValidString( templateName );
1465
+ if ( isTemplateAd && TextUtils.isEmpty( nativeAd.getHeadline() ) )
1466
+ {
1467
+ e( "Native ad (" + nativeAd + ") does not have required assets." );
1468
+ listener.onNativeAdLoadFailed( new MaxAdapterError( -5400, "Missing Native Ad Assets" ) );
1469
+
1470
+ return;
1471
+ }
1472
+
1473
+ runOnUiThread( new Runnable()
1474
+ {
1475
+ @Override
1476
+ public void run()
1477
+ {
1478
+ View mediaView = null;
1479
+ MediaContent mediaContent = nativeAd.getMediaContent();
1480
+ List<NativeAd.Image> images = nativeAd.getImages();
1481
+ Drawable mainImage = null;
1482
+ float mediaContentAspectRatio = 0.0f;
1483
+
1484
+ if ( mediaContent != null )
1485
+ {
1486
+ MediaView googleMediaView = new MediaView( context );
1487
+ googleMediaView.setMediaContent( mediaContent );
1488
+ mediaView = googleMediaView;
1489
+
1490
+ mainImage = mediaContent.getMainImage();
1491
+ mediaContentAspectRatio = mediaContent.getAspectRatio();
1492
+ }
1493
+ else if ( images != null && images.size() > 0 )
1494
+ {
1495
+ NativeAd.Image mediaImage = images.get( 0 );
1496
+ ImageView mediaImageView = new ImageView( context );
1497
+ Drawable mediaImageDrawable = mediaImage.getDrawable();
1498
+
1499
+ if ( mediaImageDrawable != null )
1500
+ {
1501
+ mediaImageView.setImageDrawable( mediaImageDrawable );
1502
+ mediaView = mediaImageView;
1503
+
1504
+ mediaContentAspectRatio = (float) mediaImageDrawable.getIntrinsicWidth() / (float) mediaImageDrawable.getIntrinsicHeight();
1505
+ }
1506
+ }
1507
+
1508
+ NativeAd.Image icon = nativeAd.getIcon();
1509
+ MaxNativeAd.MaxNativeAdImage iconImage = null;
1510
+ if ( icon != null )
1511
+ {
1512
+ if ( icon.getDrawable() != null )
1513
+ {
1514
+ iconImage = new MaxNativeAd.MaxNativeAdImage( icon.getDrawable() );
1515
+ }
1516
+ else
1517
+ {
1518
+ iconImage = new MaxNativeAd.MaxNativeAdImage( icon.getUri() );
1519
+ }
1520
+ }
1521
+
1522
+ MaxNativeAd.Builder builder = new MaxNativeAd.Builder()
1523
+ .setAdFormat( MaxAdFormat.NATIVE )
1524
+ .setTitle( nativeAd.getHeadline() )
1525
+ .setAdvertiser( nativeAd.getAdvertiser() )
1526
+ .setBody( nativeAd.getBody() )
1527
+ .setCallToAction( nativeAd.getCallToAction() )
1528
+ .setIcon( iconImage )
1529
+ .setMediaView( mediaView );
1530
+
1531
+ if ( AppLovinSdk.VERSION_CODE >= 11_04_03_99 )
1532
+ {
1533
+ builder.setMainImage( new MaxNativeAd.MaxNativeAdImage( mainImage ) );
1534
+ }
1535
+
1536
+ if ( AppLovinSdk.VERSION_CODE >= 11_04_00_00 )
1537
+ {
1538
+ builder.setMediaContentAspectRatio( mediaContentAspectRatio );
1539
+ }
1540
+
1541
+ if ( AppLovinSdk.VERSION_CODE >= 11_07_00_00 )
1542
+ {
1543
+ builder.setStarRating( nativeAd.getStarRating() );
1544
+ }
1545
+
1546
+ MaxNativeAd maxNativeAd = new MaxGoogleNativeAd( builder );
1547
+
1548
+ ResponseInfo responseInfo = nativeAd.getResponseInfo();
1549
+ String responseId = ( responseInfo != null ) ? responseInfo.getResponseId() : null;
1550
+ Bundle extraInfo = new Bundle( 1 );
1551
+ extraInfo.putString( "creative_id", responseId );
1552
+
1553
+ listener.onNativeAdLoaded( maxNativeAd, extraInfo );
1554
+ }
1555
+ } );
1556
+ }
1557
+
1558
+ @Override
1559
+ public void onAdFailedToLoad(@NonNull final LoadAdError loadAdError)
1560
+ {
1561
+ MaxAdapterError adapterError = toMaxError( loadAdError );
1562
+ log( "Native ad (" + placementId + ") failed to load with error: " + adapterError );
1563
+ listener.onNativeAdLoadFailed( adapterError );
1564
+ }
1565
+
1566
+ @Override
1567
+ public void onAdImpression()
1568
+ {
1569
+ log( "Native ad shown" );
1570
+ listener.onNativeAdDisplayed( null );
1571
+ }
1572
+
1573
+ @Override
1574
+ public void onAdClicked()
1575
+ {
1576
+ log( "Native ad clicked" );
1577
+ listener.onNativeAdClicked();
1578
+ }
1579
+
1580
+ @Override
1581
+ public void onAdOpened()
1582
+ {
1583
+ log( "Native ad opened" );
1584
+ }
1585
+
1586
+ @Override
1587
+ public void onAdClosed()
1588
+ {
1589
+ log( "Native ad closed" );
1590
+ }
1591
+ }
1592
+
1593
+ private class MaxGoogleNativeAd
1594
+ extends MaxNativeAd
1595
+ {
1596
+ public MaxGoogleNativeAd(final Builder builder) { super( builder ); }
1597
+
1598
+ @Override
1599
+ public void prepareViewForInteraction(final MaxNativeAdView maxNativeAdView)
1600
+ {
1601
+ prepareForInteraction( Collections.<View>emptyList(), maxNativeAdView );
1602
+ }
1603
+
1604
+ @Override
1605
+ public boolean prepareForInteraction(final List<View> clickableViews, final ViewGroup container)
1606
+ {
1607
+ final NativeAd nativeAd = GoogleMediationAdapter.this.nativeAd;
1608
+ if ( nativeAd == null )
1609
+ {
1610
+ e( "Failed to register native ad views: native ad is null." );
1611
+ return false;
1612
+ }
1613
+
1614
+ nativeAdView = new NativeAdView( container.getContext() );
1615
+
1616
+ // Native integrations
1617
+ if ( container instanceof MaxNativeAdView )
1618
+ {
1619
+ MaxNativeAdView maxNativeAdView = (MaxNativeAdView) container;
1620
+
1621
+ // The Google Native Ad View needs to be wrapped around the main native ad view.
1622
+ View mainView = maxNativeAdView.getMainView();
1623
+ maxNativeAdView.removeView( mainView );
1624
+ nativeAdView.addView( mainView );
1625
+ maxNativeAdView.addView( nativeAdView );
1626
+
1627
+ nativeAdView.setIconView( maxNativeAdView.getIconImageView() );
1628
+ nativeAdView.setHeadlineView( maxNativeAdView.getTitleTextView() );
1629
+ nativeAdView.setAdvertiserView( maxNativeAdView.getAdvertiserTextView() );
1630
+ nativeAdView.setBodyView( maxNativeAdView.getBodyTextView() );
1631
+ nativeAdView.setCallToActionView( maxNativeAdView.getCallToActionButton() );
1632
+
1633
+ View mediaView = getMediaView();
1634
+ if ( mediaView instanceof MediaView )
1635
+ {
1636
+ nativeAdView.setMediaView( (MediaView) mediaView );
1637
+ }
1638
+ else if ( mediaView instanceof ImageView )
1639
+ {
1640
+ nativeAdView.setImageView( mediaView );
1641
+ }
1642
+
1643
+ nativeAdView.setNativeAd( nativeAd );
1644
+ }
1645
+ // Plugins
1646
+ else
1647
+ {
1648
+ for ( View view : clickableViews )
1649
+ {
1650
+ Object viewTag = view.getTag();
1651
+ if ( viewTag == null ) continue;
1652
+
1653
+ int tag = (int) viewTag;
1654
+
1655
+ if ( tag == TITLE_LABEL_TAG )
1656
+ {
1657
+ nativeAdView.setHeadlineView( view );
1658
+ }
1659
+ else if ( tag == ICON_VIEW_TAG )
1660
+ {
1661
+ nativeAdView.setIconView( view );
1662
+ }
1663
+ else if ( tag == BODY_VIEW_TAG )
1664
+ {
1665
+ nativeAdView.setBodyView( view );
1666
+ }
1667
+ else if ( tag == CALL_TO_ACTION_VIEW_TAG )
1668
+ {
1669
+ nativeAdView.setCallToActionView( view );
1670
+ }
1671
+ else if ( tag == ADVERTISER_VIEW_TAG )
1672
+ {
1673
+ nativeAdView.setAdvertiserView( view );
1674
+ }
1675
+ }
1676
+
1677
+ View mediaView = getMediaView();
1678
+ ViewGroup midContainer = ( mediaView != null ) ? (ViewGroup) mediaView.getParent() : null;
1679
+
1680
+ // mediaView must be already added to the container unless the user skipped it, but the
1681
+ // parent may not be the container, although it should be within the hierarchy
1682
+ if ( midContainer != null /*&& container.findViewById( midContainer.getId() ) != null */ )
1683
+ {
1684
+ // The Google Native Ad View needs to be inserted between mediaView and midContainer
1685
+ // for mediaView to be visible
1686
+ midContainer.removeView( mediaView );
1687
+ nativeAdView.addView( mediaView );
1688
+ midContainer.addView( nativeAdView );
1689
+
1690
+ if ( mediaView instanceof MediaView )
1691
+ {
1692
+ nativeAdView.setMediaView( (MediaView) mediaView );
1693
+ }
1694
+ else if ( mediaView instanceof ImageView )
1695
+ {
1696
+ nativeAdView.setImageView( mediaView );
1697
+ }
1698
+
1699
+ nativeAdView.setNativeAd( nativeAd );
1700
+
1701
+ // stretch nativeAdView out to midContainer
1702
+ nativeAdView.measure( View.MeasureSpec.makeMeasureSpec( midContainer.getWidth(), View.MeasureSpec.EXACTLY ),
1703
+ View.MeasureSpec.makeMeasureSpec( midContainer.getHeight(), View.MeasureSpec.EXACTLY ) );
1704
+ nativeAdView.layout( 0, 0, midContainer.getWidth(), midContainer.getHeight() );
1705
+ }
1706
+ }
1707
+
1708
+ return true;
1709
+ }
1710
+ }
1711
+ }