Skip to content

Commit

Permalink
Merge pull request #543 from ironsource-mobile:release/iAds-rtb-banner
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 703258828
  • Loading branch information
copybara-github committed Dec 5, 2024
2 parents 231803b + 0ea1220 commit 45aa3c3
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 22 deletions.
3 changes: 3 additions & 0 deletions ThirdPartyAdapters/ironsource/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## IronSource Android Mediation Adapter Changelog

#### Next Version
- Added bidding support for banner ad formats.

#### Version 8.5.0.0
- Verified compatibility with ironSource SDK version 8.5.0.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.MediationUtils;
import com.google.android.gms.ads.MobileAds;
import com.ironsource.mediationsdk.ISBannerSize;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -64,6 +66,33 @@ public static ISBannerSize getISBannerSizeFromGoogleAdSize(
return new ISBannerSize(closestSize.getWidth(), closestSize.getHeight());
}

@NonNull
public static com.unity3d.ironsourceads.AdSize getAdSizeFromGoogleAdSize(
@NonNull Context context, @NonNull AdSize adSize) {
ArrayList<AdSize> potentials =
new ArrayList<>(
Arrays.asList(
AdSize.BANNER, AdSize.MEDIUM_RECTANGLE, AdSize.LARGE_BANNER, AdSize.LEADERBOARD));

AdSize closestSize = MediationUtils.findClosestSize(context, adSize, potentials);
if (closestSize == null) {
return com.unity3d.ironsourceads.AdSize.banner();
}

if (AdSize.BANNER.equals(closestSize)) {
return com.unity3d.ironsourceads.AdSize.banner();
} else if (AdSize.MEDIUM_RECTANGLE.equals(closestSize)) {
return com.unity3d.ironsourceads.AdSize.mediumRectangle();
} else if (AdSize.LARGE_BANNER.equals(closestSize)) {
return com.unity3d.ironsourceads.AdSize.large();
} else if (AdSize.LEADERBOARD.equals(closestSize)) {
return com.unity3d.ironsourceads.AdSize.leaderboard();
}

// If none of the predefined sizes are matched, return a banner size
return com.unity3d.ironsourceads.AdSize.banner();
}

public static AdError buildAdErrorAdapterDomain(int code, @NonNull String message) {
return new AdError(code, message, ADAPTER_ERROR_DOMAIN);
}
Expand Down Expand Up @@ -107,4 +136,17 @@ public static <T> boolean canLoadIronSourceAdInstance(
public static String getAdapterVersion() {
return BuildConfig.ADAPTER_VERSION;
}

public static String prepareVersionToiAdsSdk(@NonNull String version) {
return version.replace(".", "");
}

public static String getMediationType() {
return IronSourceConstants.MEDIATION_NAME
+ prepareVersionToiAdsSdk(getAdapterVersion())
+ IronSourceConstants.SDK
+ prepareVersionToiAdsSdk(MobileAds.getVersion().toString())
+ IronSourceConstants.IADS
+ IronSourceConstants.IADS_ADAPTER_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@

public class IronSourceConstants {

/**
* Adapter class name for logging.
*/
/** Adapter class name for logging. */
static final String TAG = IronSourceMediationAdapter.class.getSimpleName();

/**
* Key to obtain App Key, required for initializing IronSource SDK.
*/
/** Key to obtain App Key, required for initializing IronSource SDK. */
static final String KEY_APP_KEY = "appKey";

/**
* Key to obtain the IronSource Instance ID, required to show IronSource ads.
*/
/** Key to obtain the IronSource Instance ID, required to show IronSource ads. */
static final String KEY_INSTANCE_ID = "instanceId";

/** Default non bidder IronSource instance ID. */
static final String DEFAULT_NON_RTB_INSTANCE_ID = "0";

/**
* Constant used for IronSource internal reporting.
*/
/** Constant used for IronSource internal reporting. */
static final String MEDIATION_NAME = "AdMob";

/** Constant used for IronSource adapter version internal reporting. */
static final String ADAPTER_VERSION_NAME = "500";
static final String IADS_ADAPTER_VERSION = "510";

static final String IADS = "iAds";

static final String WATERMARK = "google_watermark";

static final String SDK = "SDK";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

package com.google.ads.mediation.ironsource;

import static com.google.ads.mediation.ironsource.IronSourceConstants.ADAPTER_VERSION_NAME;
import static com.google.ads.mediation.ironsource.IronSourceConstants.KEY_APP_KEY;
import static com.google.ads.mediation.ironsource.IronSourceConstants.MEDIATION_NAME;
import static com.google.ads.mediation.ironsource.IronSourceConstants.TAG;

import android.app.Activity;
Expand Down Expand Up @@ -196,13 +194,13 @@ public void initialize(
if (count > 1) {
String message =
String.format(
"Multiple '%s' entries found: %s. Using app key '%s' to initialize the IronSource"
+ " SDK.",
"Multiple '%s' entries found: %s. Using app key '%s' to initialize "
+ "the IronSource SDK.",
KEY_APP_KEY, appKeys, appKey);
Log.w(TAG, message);
}

IronSource.setMediationType(MEDIATION_NAME + ADAPTER_VERSION_NAME);
IronSource.setMediationType(IronSourceAdapterUtils.getMediationType());
Log.d(TAG, "Initializing IronSource SDK with app key: " + appKey);

List<IronSourceAds.AdFormat> adFormatsToInitialize =
Expand Down Expand Up @@ -351,6 +349,29 @@ public void loadRtbInterstitialAd(
ironSourceRtbInterstitialAd.loadRtbAd();
}

@Override
public void loadRtbBannerAd(
@NonNull MediationBannerAdConfiguration mediationBannerAdConfiguration,
@NonNull
MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
mediationAdLoadCallback) {
if (!isInitialized.get()) {
AdError loadError =
new AdError(
ERROR_SDK_NOT_INITIALIZED,
"Failed to load IronSource RTB interstitial ad since IronSource SDK is not "
+ "initialized.",
IRONSOURCE_SDK_ERROR_DOMAIN);
Log.w(TAG, loadError.getMessage());
mediationAdLoadCallback.onFailure(loadError);
return;
}

IronSourceRtbBannerAd ironSourceRtbBannerAd =
new IronSourceRtbBannerAd(mediationBannerAdConfiguration, mediationAdLoadCallback);
ironSourceRtbBannerAd.loadRtbAd();
}

@Override
public void loadBannerAd(
@NonNull MediationBannerAdConfiguration mediationBannerAdConfiguration,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ads.mediation.ironsource;

import static com.google.ads.mediation.ironsource.IronSourceAdapterUtils.getAdSizeFromGoogleAdSize;
import static com.google.ads.mediation.ironsource.IronSourceConstants.KEY_INSTANCE_ID;
import static com.google.ads.mediation.ironsource.IronSourceConstants.WATERMARK;
import static com.google.ads.mediation.ironsource.IronSourceMediationAdapter.ERROR_INVALID_SERVER_PARAMETERS;
import static com.google.ads.mediation.ironsource.IronSourceMediationAdapter.IRONSOURCE_SDK_ERROR_DOMAIN;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationBannerAd;
import com.google.android.gms.ads.mediation.MediationBannerAdCallback;
import com.google.android.gms.ads.mediation.MediationBannerAdConfiguration;
import com.ironsource.mediationsdk.logger.IronSourceError;
import com.unity3d.ironsourceads.banner.BannerAdLoader;
import com.unity3d.ironsourceads.banner.BannerAdLoaderListener;
import com.unity3d.ironsourceads.banner.BannerAdRequest;
import com.unity3d.ironsourceads.banner.BannerAdView;
import com.unity3d.ironsourceads.banner.BannerAdViewListener;

/**
* Used to load ironSource RTB Banner ads and mediate callbacks between Google Mobile Ads SDK and
* ironSource SDK.
*/
public class IronSourceRtbBannerAd
implements MediationBannerAd, BannerAdLoaderListener, BannerAdViewListener {

@VisibleForTesting private MediationBannerAdCallback adLifecycleCallback;

private final Context context;

private final String instanceID;

private final String bidToken;

private final com.google.android.gms.ads.AdSize adSize;

private final MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
adLoadCallback;

private final String watermark;

private FrameLayout ironSourceAdView;

public IronSourceRtbBannerAd(
@NonNull MediationBannerAdConfiguration bannerAdConfig,
@NonNull
MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
mediationAdLoadCallback) {
Bundle serverParameters = bannerAdConfig.getServerParameters();
instanceID = serverParameters.getString(KEY_INSTANCE_ID, "");
context = bannerAdConfig.getContext();
adSize = bannerAdConfig.getAdSize();
adLoadCallback = mediationAdLoadCallback;
watermark = bannerAdConfig.getWatermark();
bidToken = bannerAdConfig.getBidResponse();
}

public void loadRtbAd() {
if (TextUtils.isEmpty(instanceID)) {
AdError loadError =
IronSourceAdapterUtils.buildAdErrorAdapterDomain(
ERROR_INVALID_SERVER_PARAMETERS, "Missing or invalid instance ID.");
adLoadCallback.onFailure(loadError);
return;
}

Bundle watermarkBundle = new Bundle();
watermarkBundle.putString(WATERMARK, watermark);
BannerAdRequest adRequest =
new BannerAdRequest.Builder(
context, instanceID, bidToken, getAdSizeFromGoogleAdSize(context, adSize))
.withExtraParams(watermarkBundle)
.build();
ironSourceAdView = new FrameLayout(context);
BannerAdLoader.loadAd(adRequest, this);
}

@NonNull
@Override
public View getView() {
return ironSourceAdView;
}

@Override
public void onBannerAdLoaded(@NonNull BannerAdView bannerAdView) {
if (this.ironSourceAdView == null || this.adLoadCallback == null) {
return;
}
bannerAdView.setListener(this);
this.ironSourceAdView.addView(bannerAdView);
adLifecycleCallback = adLoadCallback.onSuccess(this);
}

@Override
public void onBannerAdLoadFailed(@NonNull IronSourceError ironSourceError) {
if (adLoadCallback == null) {
return;
}
final AdError loadError =
new AdError(
ironSourceError.getErrorCode(),
ironSourceError.getErrorMessage(),
IRONSOURCE_SDK_ERROR_DOMAIN);
adLoadCallback.onFailure(loadError);
}

@Override
public void onBannerAdClicked(@NonNull BannerAdView bannerAdView) {
if (adLifecycleCallback == null) {
return;
}
adLifecycleCallback.onAdOpened();
adLifecycleCallback.reportAdClicked();
}

@Override
public void onBannerAdShown(@NonNull BannerAdView bannerAdView) {
if (adLifecycleCallback != null) {
adLifecycleCallback.reportAdImpression();
}
}
}
Loading

0 comments on commit 45aa3c3

Please sign in to comment.