Skip to content

Commit

Permalink
feat/add_support_for_adaptive_banner (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
alhiwatan authored Dec 27, 2024
1 parent 2001db1 commit fc92415
Show file tree
Hide file tree
Showing 13 changed files with 547 additions and 240 deletions.
2 changes: 2 additions & 0 deletions applovin_max/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Versions

## x.x.x
* Add support for adaptive banners. For more info, check out our [docs](https://developers.applovin.com/en/max/flutter/ad-formats/banner-and-mrec-ads#adaptive-banners).
## 4.1.2
* Fix preloaded banners and MRECs not resuming auto-refresh on iOS.
## 4.1.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
Expand Down Expand Up @@ -69,6 +70,16 @@ public class AppLovinMAX
private static final String USER_GEOGRAPHY_OTHER = "O";
private static final String USER_GEOGRAPHY_UNKNOWN = "U";

private static final String TOP_CENTER = "top_center";
private static final String TOP_LEFT = "top_left";
private static final String TOP_RIGHT = "top_right";
private static final String CENTERED = "centered";
private static final String CENTER_LEFT = "center_left";
private static final String CENTER_RIGHT = "center_right";
private static final String BOTTOM_LEFT = "bottom_left";
private static final String BOTTOM_CENTER = "bottom_center";
private static final String BOTTOM_RIGHT = "bottom_right";

private static final Map<String, String> ALCompatibleNativeSdkVersions = new HashMap<>();

static
Expand Down Expand Up @@ -109,6 +120,8 @@ public class AppLovinMAX
private final Map<String, String> mAdViewPositions = new HashMap<>( 2 );
private final List<String> mAdUnitIdsToShowAfterCreate = new ArrayList<>( 2 );
private final Set<String> mDisabledAutoRefreshAdViewAdUnitIds = new HashSet<>( 2 );
private final Map<String, Integer> mAdViewWidths = new HashMap<>( 2 );
private final Set<String> mDisabledAdaptiveBannerAdUnitIds = new HashSet<>( 2 );

public static AppLovinMAX getInstance()
{
Expand Down Expand Up @@ -424,6 +437,11 @@ public void setBannerPlacement(final String adUnitId, final String placement)
setAdViewPlacement( adUnitId, getDeviceSpecificBannerAdViewAdFormat(), placement );
}

public void setBannerWidth(final String adUnitId, final int widthDp)
{
setAdViewWidth( adUnitId, widthDp, getDeviceSpecificBannerAdViewAdFormat() );
}

public void updateBannerPosition(final String adUnitId, final String bannerPosition)
{
updateAdViewPosition( adUnitId, bannerPosition, getDeviceSpecificBannerAdViewAdFormat() );
Expand Down Expand Up @@ -985,6 +1003,20 @@ private void setAdViewPlacement(final String adUnitId, final MaxAdFormat adForma
adView.setPlacement( placement );
}

private void setAdViewWidth(final String adUnitId, final int widthDp, final MaxAdFormat adFormat)
{
d( "Setting width " + widthDp + " for \"" + adFormat + "\" with ad unit identifier \"" + adUnitId + "\"" );

int minWidthDp = adFormat.getSize().getWidth();
if ( widthDp < minWidthDp )
{
e( "The provided width: " + widthDp + "dp is smaller than the minimum required width: " + minWidthDp + "dp for ad format: " + adFormat + ". Please set the width higher than the minimum required." );
}

mAdViewWidths.put( adUnitId, widthDp );
positionAdView( adUnitId, adFormat );
}

private void updateAdViewPosition(final String adUnitId, final String adViewPosition, final MaxAdFormat adFormat)
{
d( "Updating " + adFormat.getLabel() + " position to \"" + adViewPosition + "\" for ad unit id \"" + adUnitId + "\"" );
Expand Down Expand Up @@ -1116,6 +1148,20 @@ private void setAdViewExtraParameters(final String adUnitId, final MaxAdFormat a
mAdViewAdFormats.put( adUnitId, forcedAdFormat );
positionAdView( adUnitId, forcedAdFormat );
}
else if ( "adaptive_banner".equalsIgnoreCase( key ) )
{
boolean useAdaptiveBannerAdSize = Boolean.parseBoolean( value );
if ( useAdaptiveBannerAdSize )
{
mDisabledAdaptiveBannerAdUnitIds.remove( adUnitId );
}
else
{
mDisabledAdaptiveBannerAdUnitIds.add( adUnitId );
}

positionAdView( adUnitId, adFormat );
}
}

// Utility Methods
Expand Down Expand Up @@ -1253,21 +1299,60 @@ private void positionAdView(String adUnitId, MaxAdFormat adFormat)
return;
}

final String adViewPosition = mAdViewPositions.get( adUnitId );
final RelativeLayout relativeLayout = (RelativeLayout) adView.getParent();
if ( relativeLayout == null )
{
e( adFormat.getLabel() + "'s parent does not exist" );
return;
}

// Size the ad
final DisplayMetrics displayMetrics = new DisplayMetrics();
getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics( displayMetrics );

final String adViewPosition = mAdViewPositions.get( adUnitId );
final AdViewSize adViewSize = getAdViewSize( adFormat );
final int width = AppLovinSdkUtils.dpToPx( getCurrentActivity(), adViewSize.widthDp );
final int height = AppLovinSdkUtils.dpToPx( getCurrentActivity(), adViewSize.heightDp );
final boolean isAdaptiveBannerDisabled = mDisabledAdaptiveBannerAdUnitIds.contains( adUnitId );
final boolean isWidthDpOverridden = mAdViewWidths.containsKey( adUnitId );

//
// Determine ad width
//
final int adViewWidthDp;

// Check if publisher has overridden width as dp
if ( isWidthDpOverridden )
{
adViewWidthDp = mAdViewWidths.get( adUnitId );
}
else if ( TOP_CENTER.equalsIgnoreCase( adViewPosition ) || BOTTOM_CENTER.equalsIgnoreCase( adViewPosition ) )
{
int adViewWidthPx = displayMetrics.widthPixels;
adViewWidthDp = AppLovinSdkUtils.pxToDp( getCurrentActivity(), adViewWidthPx );
}
else
{
adViewWidthDp = adViewSize.widthDp;
}

//
// Determine ad height
//
final int adViewHeightDp;

if ( ( adFormat == MaxAdFormat.BANNER || adFormat == MaxAdFormat.LEADER ) && !isAdaptiveBannerDisabled )
{
adViewHeightDp = adFormat.getAdaptiveSize( adViewWidthDp, getCurrentActivity() ).getHeight();
}
else
{
adViewHeightDp = adViewSize.heightDp;
}

final int widthPx = AppLovinSdkUtils.dpToPx( getCurrentActivity(), adViewWidthDp );
final int heightPx = AppLovinSdkUtils.dpToPx( getCurrentActivity(), adViewHeightDp );

final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) adView.getLayoutParams();
params.height = height;
params.height = heightPx;
adView.setLayoutParams( params );

// Parse gravity
Expand Down Expand Up @@ -1298,11 +1383,11 @@ else if ( adViewPosition.contains( "bottom" ) )
if ( adViewPosition.contains( "center" ) )
{
gravity |= Gravity.CENTER_HORIZONTAL;
params.width = ( MaxAdFormat.MREC == adFormat ) ? width : RelativeLayout.LayoutParams.MATCH_PARENT; // Stretch width if banner
params.width = ( MaxAdFormat.MREC == adFormat ) ? widthPx : RelativeLayout.LayoutParams.MATCH_PARENT; // Stretch width if banner
}
else
{
params.width = width;
params.width = widthPx;

if ( adViewPosition.contains( "left" ) )
{
Expand Down Expand Up @@ -1331,6 +1416,8 @@ public Map<String, Object> getAdInfo(final MaxAd ad)
adInfo.put( "revenuePrecision", ad.getRevenuePrecision() );
adInfo.put( "dspName", AppLovinSdkUtils.isValidString( ad.getDspName() ) ? ad.getDspName() : "" );
adInfo.put( "waterfall", createAdWaterfallInfo( ad.getWaterfall() ) );
adInfo.put( "width", ad.getSize().getWidth() );
adInfo.put( "height", ad.getSize().getHeight() );

return adInfo;
}
Expand Down Expand Up @@ -1729,6 +1816,14 @@ else if ( "setBannerPlacement".equals( call.method ) )

result.success( null );
}
else if ( "setBannerWidth".equals( call.method ) )
{
String adUnitId = call.argument( "ad_unit_id" );
Integer width = call.argument( "width" );
setBannerWidth( adUnitId, width );

result.success( null );
}
else if ( "updateBannerPosition".equals( call.method ) )
{
String adUnitId = call.argument( "ad_unit_id" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ else if ( "stopAutoRefresh".equals( call.method ) )
@Override
public View getView()
{
return widget != null ? widget.getAdView() : null;
return widget;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.applovin.applovin_max;

import android.content.Context;
import android.widget.FrameLayout;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdFormat;
Expand All @@ -17,6 +18,7 @@
import androidx.annotation.Nullable;

class AppLovinMAXAdViewWidget
extends FrameLayout
implements MaxAdListener, MaxAdViewAdListener, MaxAdRevenueListener
{
private final MaxAdView adView;
Expand All @@ -32,6 +34,8 @@ public AppLovinMAXAdViewWidget(final String adUnitId, final MaxAdFormat adFormat

public AppLovinMAXAdViewWidget(final String adUnitId, final MaxAdFormat adFormat, final boolean shouldPreloadWidget, final AppLovinSdk sdk, final Context context)
{
super( context );

this.shouldPreloadWidget = shouldPreloadWidget;

adView = new MaxAdView( adUnitId, adFormat, sdk, context );
Expand All @@ -42,6 +46,15 @@ public AppLovinMAXAdViewWidget(final String adUnitId, final MaxAdFormat adFormat
adView.setExtraParameter( "allow_pause_auto_refresh_immediately", "true" );

adView.stopAutoRefresh();

addView( adView );

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
);

adView.setLayoutParams( params );
}

public MaxAdView getAdView()
Expand Down
Loading

0 comments on commit fc92415

Please sign in to comment.