2424import android .text .TextUtils ;
2525import androidx .annotation .NonNull ;
2626import androidx .annotation .Nullable ;
27+
2728import com .google .android .gms .ads .AdError ;
2829import com .google .android .gms .ads .AdFormat ;
30+ import com .google .android .gms .ads .MobileAds ;
2931import com .google .android .gms .ads .AdSize ;
3032import com .google .android .gms .ads .RequestConfiguration ;
3133import com .google .android .gms .ads .mediation .MediationConfiguration ;
3234import com .google .android .gms .ads .mediation .rtb .RtbSignalData ;
35+ import com .unity3d .ads .BannerSize ;
36+ import com .unity3d .ads .MediationInfo ;
3337import com .unity3d .ads .UnityAds ;
34- import com .unity3d .ads .UnityAds . UnityAdsInitializationError ;
38+ import com .unity3d .ads .UnityAdsError ;
3539import com .unity3d .ads .metadata .MetaData ;
36- import com .unity3d .services .banners .BannerErrorInfo ;
37- import com .unity3d .services .banners .UnityBannerSize ;
3840import java .util .ArrayList ;
3941
4042/** Utility class for the Unity adapter. */
4143public class UnityAdsAdapterUtils {
4244
45+ static final String ADMOB = "AdMob" ;
46+
47+ static final String WATERMARK = "watermark" ;
48+
4349 /** Enumeration of ad events that get forwarded to AdMob */
4450 public enum AdEvent {
4551 LOADED ,
@@ -57,43 +63,45 @@ public enum AdEvent {
5763 private UnityAdsAdapterUtils () {}
5864
5965 /**
60- * Creates an {@link AdError} object based on the specified {@link
61- * UnityAds.UnityAdsInitializationError }.
66+ * Creates an {@link AdError} object based on the specified Initialization {@link
67+ * UnityAdsError }.
6268 *
6369 * @param unityAdsError error object from Unity.
6470 * @param description the error message.
6571 * @return the {@link AdError} object.
6672 */
6773 @ NonNull
68- static AdError createSDKError (
69- @ NonNull UnityAds . UnityAdsInitializationError unityAdsError , @ NonNull String description ) {
70- return createAdError (getMediationErrorCode (unityAdsError ), description );
74+ static AdError createSDKInitializationError (
75+ @ NonNull UnityAdsError unityAdsError , @ NonNull String description ) {
76+ return createAdError (getMediationInitializationErrorCode (unityAdsError ), description );
7177 }
7278
7379 /**
74- * Creates an {@link AdError} object based on the specified {@link UnityAds.UnityAdsLoadError}.
80+ * Creates an {@link AdError} object based on the specified load {@link
81+ * UnityAdsError}.
7582 *
7683 * @param unityAdsError error object from Unity.
7784 * @param description the error message.
7885 * @return the {@link AdError} object.
7986 */
8087 @ NonNull
81- static AdError createSDKError (
82- @ NonNull UnityAds . UnityAdsLoadError unityAdsError , @ NonNull String description ) {
83- return createAdError (getMediationErrorCode (unityAdsError ), description );
88+ static AdError createSDKLoadError (
89+ @ NonNull UnityAdsError unityAdsError , @ NonNull String description ) {
90+ return createAdError (getMediationLoadErrorCode (unityAdsError ), description );
8491 }
8592
8693 /**
87- * Creates an {@link AdError} object based on the specified {@link UnityAds.UnityAdsShowError}.
94+ * Creates an {@link AdError} object based on the specified show {@link
95+ * UnityAdsError}.
8896 *
8997 * @param unityAdsError error object from Unity.
9098 * @param description the error message.
9199 * @return the {@link AdError} object.
92100 */
93101 @ NonNull
94- static AdError createSDKError (
95- @ NonNull UnityAds . UnityAdsShowError unityAdsError , @ NonNull String description ) {
96- return createAdError (getMediationErrorCode (unityAdsError ), description );
102+ static AdError createSDKShowError (
103+ @ NonNull UnityAdsError unityAdsError , @ NonNull String description ) {
104+ return createAdError (getMediationShowErrorCode (unityAdsError ), description );
97105 }
98106
99107 /**
@@ -109,47 +117,26 @@ static AdError createAdError(int errorCode, @NonNull String description) {
109117 }
110118
111119 /**
112- * Gets the mediation specific error code for the specified {@link BannerErrorInfo}.
113- *
114- * @param errorInfo error object from Unity.
115- * @return mediation specific banner error code.
116- */
117- static int getMediationErrorCode (@ NonNull BannerErrorInfo errorInfo ) {
118- int errorCode = 200 ;
119- switch (errorInfo .errorCode ) {
120- case UNKNOWN :
121- errorCode = 201 ;
122- break ;
123- case NATIVE_ERROR :
124- errorCode = 202 ;
125- break ;
126- case WEBVIEW_ERROR :
127- errorCode = 203 ;
128- break ;
129- case NO_FILL :
130- errorCode = 204 ;
131- break ;
132- }
133- return errorCode ;
134- }
135-
136- /**
137- * Gets the mediation specific error code for the specified {@link
138- * UnityAds.UnityAdsInitializationError}.
120+ * Gets the mediation specific error code for the specified {@link UnityAds.UnityAdsLoadError}.
139121 *
140122 * @param unityAdsError error object from Unity.
141- * @return mediation specific initialization error code.
123+ * @return mediation specific load error code.
142124 */
143- static int getMediationErrorCode (@ NonNull UnityAdsInitializationError unityAdsError ) {
144- switch (unityAdsError ) {
145- case INTERNAL_ERROR :
125+ static int getMediationInitializationErrorCode (@ NonNull UnityAdsError unityAdsError ) {
126+ switch (unityAdsError .getCode ()) {
127+ case 2 :
128+ case 52000 :
129+ case 52003 :
130+ case 52004 :
131+ case 52005 :
132+ case 52006 : // Internal Error
146133 return 301 ;
147- case INVALID_ARGUMENT :
134+ case 52001 :
135+ case 52002 : // Invalid argument
148136 return 302 ;
149- case AD_BLOCKER_DETECTED :
150- return 303 ;
151- // Excluding default to allow for compile warnings if UnityAdsInitializationError is
152- // expanded in the future.
137+
138+ // Excluding default to allow for compile warnings if UnityAdsInitializationError is
139+ // expanded in the future.
153140 }
154141 return 300 ;
155142 }
@@ -160,56 +147,53 @@ static int getMediationErrorCode(@NonNull UnityAdsInitializationError unityAdsEr
160147 * @param unityAdsError error object from Unity.
161148 * @return mediation specific load error code.
162149 */
163- static int getMediationErrorCode (@ NonNull UnityAds . UnityAdsLoadError unityAdsError ) {
164- switch (unityAdsError ) {
165- case INITIALIZE_FAILED :
150+ static int getMediationLoadErrorCode (@ NonNull UnityAdsError unityAdsError ) {
151+ switch (unityAdsError . getCode () ) {
152+ case 52101 : // Not initialized
166153 return 401 ;
167- case INTERNAL_ERROR :
168- return 402 ;
169- case INVALID_ARGUMENT :
154+ case 52102 :
155+ case 52104 : // Invalid arguments
170156 return 403 ;
171- case NO_FILL :
157+ case 52100 : // No fill
172158 return 404 ;
173- case TIMEOUT :
159+ case 2 : // Time out
174160 return 405 ;
175- // Excluding default to allow for compile warnings if UnityAdsLoadError is expanded
176- // in the future.
161+ case 52103 :
162+ case 52105 :
163+ case 52106 :
164+ case 52107 :// Internal error
165+ return 402 ;
166+ // Excluding default to allow for compile warnings if UnityAdsLoadError is expanded
167+ // in the future.
177168 }
169+
178170 return 400 ;
179171 }
180172
181173 /**
182- * Gets the mediation specific error code for the specified {@link UnityAds.UnityAdsShowError }.
174+ * Gets the mediation specific error code for the specified {@link UnityAds.UnityAdsLoadError }.
183175 *
184176 * @param unityAdsError error object from Unity.
185- * @return mediation specific show error code.
177+ * @return mediation specific load error code.
186178 */
187- static int getMediationErrorCode (@ NonNull UnityAds .UnityAdsShowError unityAdsError ) {
188- switch (unityAdsError ) {
189- case NOT_INITIALIZED :
190- return 501 ;
191- case NOT_READY :
192- return 502 ;
193- case VIDEO_PLAYER_ERROR :
194- return 503 ;
195- case INVALID_ARGUMENT :
196- return 504 ;
197- case NO_CONNECTION :
198- return 505 ;
199- case ALREADY_SHOWING :
179+ static int getMediationShowErrorCode (@ NonNull UnityAdsError unityAdsError ) {
180+ switch (unityAdsError .getCode ()) {
181+ case 52201 : // already showing
200182 return 506 ;
201- case INTERNAL_ERROR :
183+ case 52200 : // Expired
184+ case 52202 : // internal error:
202185 return 507 ;
203- case TIMEOUT :
186+ case 2 : // timeout :
204187 return 508 ;
205- // Excluding default to allow for compile warnings if UnityAdsShowError is expanded
206- // in the future.
188+
189+ // Excluding default to allow for compile warnings if UnityAdsShowError is expanded
190+ // in the future.
207191 }
208192 return 500 ;
209193 }
210194
211195 @ Nullable
212- public static UnityBannerSize getUnityBannerSize (
196+ public static BannerSize getUnityBannerSize (
213197 @ NonNull Context context ,
214198 @ NonNull AdSize adSize ,
215199 boolean isRtb ,
@@ -220,11 +204,11 @@ public static UnityBannerSize getUnityBannerSize(
220204
221205 AdSize closestSize = mediationUtils .findClosestSize (context , adSize , potentials );
222206 if (closestSize != null ) {
223- return new UnityBannerSize (closestSize .getWidth (), closestSize .getHeight ());
207+ return new BannerSize (closestSize .getWidth (), closestSize .getHeight ());
224208 }
225209
226210 if (isRtb ) {
227- return new UnityBannerSize (adSize .getWidth (), adSize .getHeight ());
211+ return new BannerSize (adSize .getWidth (), adSize .getHeight ());
228212 } else {
229213 return null ;
230214 }
@@ -235,21 +219,9 @@ public static UnityBannerSize getUnityBannerSize(
235219 *
236220 * @param requestConfiguration used to read the value that indicates whether the app should be
237221 * treated as child-directed for purposes of the COPPA or under age consent.
238- * @param userMetaData used to save changes on configuration values in the Unity3D SDK.
239222 */
240- public static void setUnityAdsPrivacy (
241- RequestConfiguration requestConfiguration , MetaData userMetaData ) {
242-
243- if (shouldTreatAsAdult (requestConfiguration )) {
244- // If no signal is tagged as child and one signal indicates adult session is treated as an
245- // adult.
246- userMetaData .set ("user.nonbehavioral" , false );
247- } else {
248- // Otherwise, if any signal is child or signals are conflicting or both are UNSPECIFIED,
249- // session is treated as a child.
250- userMetaData .set ("user.nonbehavioral" , true );
251- }
252- userMetaData .commit ();
223+ public static void setUnityAdsPrivacy (RequestConfiguration requestConfiguration ) {
224+ UnityAds .setNonBehavioral (!shouldTreatAsAdult (requestConfiguration ));
253225 }
254226
255227 /**
@@ -288,4 +260,18 @@ private static boolean shouldTreatAsAdult(RequestConfiguration requestConfigurat
288260 && (tagForChildDirectedTreatment == TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE
289261 || tagForUnderAgeOfConsent == TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE );
290262 }
263+
264+ /**
265+ * Creates a MediationInfo object with Google mediation information.
266+ * This duplicates the logic from UnityAdsWrapper for convenience.
267+ *
268+ * @return MediationInfo object with Google mediation details
269+ */
270+ public static MediationInfo getMediationInfo () {
271+ return new MediationInfo (
272+ ADMOB , // Mediation name
273+ MobileAds .getVersion ().toString (), // Google mediation SDK version
274+ BuildConfig .ADAPTER_VERSION // Adapter version
275+ );
276+ }
291277}
0 commit comments