forked from googleads/googleads-mobile-android-mediation
-
Notifications
You must be signed in to change notification settings - Fork 1
UnityAds adapter: context handling (GH-3077) + null/empty token to failure #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c00f1a
UnityAds adapter: handle non-Activity context on show (GH-3077)
vishaldhiman-unity f3bb9ce
UnityAds adapter: route null/empty bidding token to onFailure
vishaldhiman-unity a0ce9fa
Address PR feedback: remove banner Activity-context requirement
vishaldhiman-unity eea4fd8
Potential fix for pull request finding
vishaldhiman-unity 3fe5a2e
Remove unused ERROR_CONTEXT_NOT_ACTIVITY and ERROR_MSG_NON_ACTIVITY
vishaldhiman-unity 7ce21a7
Address PR feedback: assert no unexpected callbacks in banner non-Act…
vishaldhiman-unity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...ters/unity/unity/src/main/java/com/google/ads/mediation/unity/UnityBannerViewFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| package com.google.ads.mediation.unity; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Context; | ||
| import com.unity3d.services.banners.BannerView; | ||
| import com.unity3d.services.banners.UnityBannerSize; | ||
|
|
||
| /** A factory to create UnityAds {@link BannerView} for Banner Ads */ | ||
| class UnityBannerViewFactory { | ||
| UnityBannerViewWrapper createBannerView( | ||
| Activity activity, String placementId, UnityBannerSize bannerSize) { | ||
| BannerView bannerView = new BannerView(activity, placementId, bannerSize); | ||
| Context context, String placementId, UnityBannerSize bannerSize) { | ||
| BannerView bannerView = new BannerView(context, placementId, bannerSize); | ||
| return new UnityBannerViewWrapper(bannerView); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ | |
| import static com.google.ads.mediation.unity.UnityAdsAdapterUtils.createSDKError; | ||
| import static com.google.ads.mediation.unity.UnityAdsAdapterUtils.getAdFormat; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Context; | ||
| import android.os.Bundle; | ||
| import android.text.TextUtils; | ||
|
|
@@ -44,6 +43,7 @@ | |
| import com.google.android.gms.ads.mediation.rtb.RtbSignalData; | ||
| import com.google.android.gms.ads.mediation.rtb.SignalCallbacks; | ||
| import com.unity3d.ads.IUnityAdsInitializationListener; | ||
| import com.unity3d.ads.IUnityAdsTokenListener; | ||
| import com.unity3d.ads.TokenConfiguration; | ||
| import com.unity3d.ads.UnityAds; | ||
| import java.lang.annotation.Retention; | ||
|
|
@@ -82,12 +82,12 @@ public class UnityMediationAdapter extends RtbAdapter { | |
| ERROR_PLACEMENT_STATE_NO_FILL, | ||
| ERROR_PLACEMENT_STATE_DISABLED, | ||
| ERROR_NULL_CONTEXT, | ||
| ERROR_CONTEXT_NOT_ACTIVITY, | ||
| ERROR_AD_NOT_READY, | ||
| ERROR_UNITY_ADS_NOT_SUPPORTED, | ||
| ERROR_FINISH, | ||
| ERROR_BANNER_SIZE_MISMATCH, | ||
| ERROR_INITIALIZATION_FAILURE | ||
| ERROR_INITIALIZATION_FAILURE, | ||
| ERROR_TOKEN_GENERATION_FAILED | ||
| }) | ||
| @interface AdapterError { | ||
|
|
||
|
|
@@ -113,9 +113,6 @@ public class UnityMediationAdapter extends RtbAdapter { | |
| */ | ||
| static final int ERROR_NULL_CONTEXT = 104; | ||
|
|
||
| /** Tried to load or show an ad with a non-Activity context. */ | ||
| static final int ERROR_CONTEXT_NOT_ACTIVITY = 105; | ||
|
|
||
| /** | ||
| * Tried to show an ad that's not ready to be shown. | ||
| */ | ||
|
|
@@ -141,12 +138,10 @@ public class UnityMediationAdapter extends RtbAdapter { | |
| */ | ||
| static final int ERROR_INITIALIZATION_FAILURE = 111; | ||
|
|
||
| static final String ERROR_MSG_MISSING_PARAMETERS = "Missing or invalid server parameters."; | ||
|
|
||
| static final String ERROR_MSG_NON_ACTIVITY = | ||
| "Unity Ads requires an Activity context to load ads."; | ||
| /** UnityAds returned no usable bidding token; routed to onFailure so the bidder skips Unity. */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: format this line |
||
| static final int ERROR_TOKEN_GENERATION_FAILED = 112; | ||
|
|
||
| static final String ERROR_MSG_CONTEXT_NULL = "Activity context is null."; | ||
| static final String ERROR_MSG_MISSING_PARAMETERS = "Missing or invalid server parameters."; | ||
|
|
||
| static final String ERROR_MSG_INITIALIZATION_FAILURE = "Unity Ads initialization failed: [%s] %s"; | ||
|
|
||
|
|
@@ -208,18 +203,6 @@ public void collectSignals( | |
| AdFormat adFormat = getAdFormat(rtbSignalData); | ||
| com.unity3d.ads.AdFormat unityAdFormat = null; | ||
|
|
||
| // For banner ad format, Unity Ads SDK requires an activity context to load the banner ad. So, | ||
| // fail here so that Unity bidder will not bid if the ad request was made with a non-activity | ||
| // context. | ||
| if (adFormat == AdFormat.BANNER && !(rtbSignalData.getContext() instanceof Activity)) { | ||
| signalCallbacks.onFailure( | ||
| new AdError( | ||
| ERROR_CONTEXT_NOT_ACTIVITY, | ||
| "Unity Ads RTB Banner ads require activity context", | ||
| ADAPTER_ERROR_DOMAIN)); | ||
| return; | ||
| } | ||
|
|
||
| if (adFormat == AdFormat.BANNER) { | ||
| unityAdFormat = com.unity3d.ads.AdFormat.BANNER; | ||
| } else if (adFormat == AdFormat.REWARDED || adFormat == AdFormat.REWARDED_INTERSTITIAL) { | ||
|
|
@@ -230,24 +213,35 @@ public void collectSignals( | |
| Log.w(TAG, "Unsupported ad format for Unity Ads: " + adFormat); | ||
| } | ||
|
|
||
| IUnityAdsTokenListener listener = new RoutingTokenListener(signalCallbacks); | ||
| if (unityAdFormat != null) { | ||
| TokenConfiguration tokenConfiguration = new TokenConfiguration(unityAdFormat); | ||
| unityAdsWrapper.getToken( | ||
| tokenConfiguration, | ||
| token -> { | ||
| if (token == null) { | ||
| token = ""; | ||
| } | ||
| signalCallbacks.onSuccess(token); | ||
| }); | ||
| unityAdsWrapper.getToken(tokenConfiguration, listener); | ||
| } else { | ||
| unityAdsWrapper.getToken( | ||
| token -> { | ||
| if (token == null) { | ||
| token = ""; | ||
| } | ||
| signalCallbacks.onSuccess(token); | ||
| }); | ||
| unityAdsWrapper.getToken(listener); | ||
| } | ||
| } | ||
|
|
||
| /** Routes a null or empty token to onFailure and a non-empty token to onSuccess. */ | ||
| @VisibleForTesting | ||
| static final class RoutingTokenListener implements IUnityAdsTokenListener { | ||
| private final SignalCallbacks signalCallbacks; | ||
|
|
||
| RoutingTokenListener(@NonNull SignalCallbacks signalCallbacks) { | ||
| this.signalCallbacks = signalCallbacks; | ||
| } | ||
|
|
||
| @Override | ||
| public void onUnityAdsTokenReady(String token) { | ||
| if (TextUtils.isEmpty(token)) { | ||
| signalCallbacks.onFailure( | ||
| new AdError( | ||
| ERROR_TOKEN_GENERATION_FAILED, | ||
| "Unity Ads returned a null or empty bidding token.", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the other errors have a named constant for an error message. should we do the same? |
||
| ADAPTER_ERROR_DOMAIN)); | ||
| return; | ||
| } | ||
| signalCallbacks.onSuccess(token); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.