Skip to content

Commit

Permalink
#UnityAdsAdapter Don't check for activity context when it is not actu…
Browse files Browse the repository at this point in the history
…ally needed.

PiperOrigin-RevId: 716074319
  • Loading branch information
Mobile Ads Developer Relations authored and copybara-github committed Jan 16, 2025
1 parent ade51c9 commit 6c9f593
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 41 deletions.
3 changes: 3 additions & 0 deletions ThirdPartyAdapters/unity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unity Ads Android Mediation Adapter Changelog

#### Next version
- Updated to not check for activity context when it is not actually needed.

#### Version 4.12.5.1
- Fixed bidding banner ad load failures by setting the object ID when loading ads.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import static com.google.ads.mediation.unity.UnityAdsAdapterUtils.createSDKError;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ADAPTER_ERROR_DOMAIN;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ERROR_CONTEXT_NOT_ACTIVITY;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ERROR_INVALID_SERVER_PARAMETERS;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ERROR_MSG_CONTEXT_NULL;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ERROR_MSG_MISSING_PARAMETERS;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ERROR_MSG_NON_ACTIVITY;
import static com.google.ads.mediation.unity.UnityMediationAdapter.ERROR_NULL_CONTEXT;
import static com.google.ads.mediation.unity.UnityMediationAdapter.KEY_GAME_ID;
import static com.google.ads.mediation.unity.UnityMediationAdapter.KEY_PLACEMENT_ID;
import static com.google.ads.mediation.unity.UnityMediationAdapter.KEY_WATERMARK;
Expand All @@ -32,7 +28,6 @@
import com.unity3d.ads.UnityAds.UnityAdsShowError;
import com.unity3d.ads.UnityAdsLoadOptions;
import com.unity3d.ads.UnityAdsShowOptions;
import java.lang.ref.WeakReference;
import java.util.UUID;

/**
Expand All @@ -44,8 +39,6 @@ public class UnityInterstitialAd

static final String ERROR_MSG_INTERSTITIAL_INITIALIZATION_FAILED =
"Unity Ads initialization failed for game ID '%s' with error message: %s";
/** An Android {@link Activity} weak reference used to show ads. */
private WeakReference<Activity> activityWeakReference;

/** Object ID used to track loaded/shown ads. */
private String objectId;
Expand Down Expand Up @@ -170,15 +163,6 @@ public void loadAd() {
return;
}

if (!(context instanceof Activity)) {
AdError adError =
new AdError(ERROR_CONTEXT_NOT_ACTIVITY, ERROR_MSG_NON_ACTIVITY, ADAPTER_ERROR_DOMAIN);
adLoadCallback.onFailure(adError);
return;
}
Activity activity = (Activity) context;
activityWeakReference = new WeakReference<>(activity);

final String adMarkup = adConfiguration.getBidResponse();

unityInitializer.initializeUnityAds(
Expand Down Expand Up @@ -222,21 +206,6 @@ public void onInitializationFailed(

@Override
public void showAd(Context context) {
Activity activityReference = activityWeakReference == null ? null : activityWeakReference.get();
if (activityReference == null) {
Log.w(
UnityMediationAdapter.TAG,
"Failed to show interstitial ad for placement ID '"
+ placementId
+ "' from Unity Ads: Activity context is null.");
if (interstitialAdCallback != null) {
AdError adError =
new AdError(ERROR_NULL_CONTEXT, ERROR_MSG_CONTEXT_NULL, ADAPTER_ERROR_DOMAIN);
interstitialAdCallback.onAdFailedToShow(adError);
}
return;
}

if (placementId == null) {
Log.w(
UnityMediationAdapter.TAG,
Expand All @@ -247,6 +216,9 @@ public void showAd(Context context) {
unityAdsLoader.createUnityAdsShowOptionsWithId(objectId);
unityAdsShowOptions.set(KEY_WATERMARK, adConfiguration.getWatermark());
// UnityAds can handle a null placement ID so show is always called here.
unityAdsLoader.show(activityReference, placementId, unityAdsShowOptions, this);
// Note: Context here is the activity that the publisher passed to GMA SDK's show() method
// (https://developers.google.com/android/reference/com/google/android/gms/ads/appopen/AppOpenAd#show(android.app.Activity)).
// So, this is guaranteed to be an activity context.
unityAdsLoader.show((Activity) context, placementId, unityAdsShowOptions, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ public UnityRewardedAd(
public void loadAd() {

Context context = mediationRewardedAdConfiguration.getContext();

if (!(context instanceof Activity)) {
AdError adError =
new AdError(ERROR_CONTEXT_NOT_ACTIVITY, ERROR_MSG_NON_ACTIVITY, ADAPTER_ERROR_DOMAIN);
Log.w(TAG, adError.toString());
mediationAdLoadCallback.onFailure(adError);
return;
}

Bundle serverParameters = mediationRewardedAdConfiguration.getServerParameters();
final String gameId = serverParameters.getString(UnityMediationAdapter.KEY_GAME_ID);
final String placementId = serverParameters.getString(UnityMediationAdapter.KEY_PLACEMENT_ID);
Expand Down

0 comments on commit 6c9f593

Please sign in to comment.