react-native-applovin-max 6.1.1 → 6.2.0

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