Skip to content

Commit

Permalink
#unity #aderror Adds AdError and LoadAdError APIs
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 344383495
  • Loading branch information
Mobile Ads Developer Relations authored and maddevrelgithubbot committed Nov 26, 2020
1 parent 5e99714 commit 920ca81
Show file tree
Hide file tree
Showing 81 changed files with 1,151 additions and 302 deletions.
Expand Up @@ -31,6 +31,7 @@
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.OnPaidEventListener;
import com.google.android.gms.ads.ResponseInfo;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -171,14 +172,14 @@ public void run() {
}

@Override
public void onAdFailedToLoad(final int errorCode) {
public void onAdFailedToLoad(final LoadAdError error) {
if (mUnityListener != null) {
new Thread(
new Runnable() {
@Override
public void run() {
if (mUnityListener != null) {
mUnityListener.onAdFailedToLoad(PluginUtils.getErrorReason(errorCode));
mUnityListener.onAdFailedToLoad(error);
}
}
})
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.OnPaidEventListener;
import com.google.android.gms.ads.ResponseInfo;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -88,16 +89,14 @@ public void run() {
}

@Override
public void onAdFailedToLoad(final int errorCode) {
public void onAdFailedToLoad(final LoadAdError error) {
if (adListener != null) {
new Thread(
new Runnable() {
@Override
public void run() {
if (adListener != null) {
adListener.onAdFailedToLoad(
PluginUtils.getErrorReason(
errorCode));
adListener.onAdFailedToLoad(error);
}
}
})
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.formats.NativeCustomTemplateAd;

/**
Expand Down Expand Up @@ -105,8 +106,8 @@ public void onCustomTemplateAdLoaded(
}, clickListener)
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
mListener.onAdFailedToLoad(PluginUtils.getErrorReason(errorCode));
public void onAdFailedToLoad(LoadAdError error) {
mListener.onAdFailedToLoad(error);
}
});
}
Expand Down
Expand Up @@ -15,14 +15,16 @@
*/
package com.google.unity.ads;

import com.google.android.gms.ads.LoadAdError;

/**
* An interface form of {@link UnityAdListener} that can be implemented via {@code AndroidJavaProxy}
* in Unity to receive ad events synchronously.
*/
public interface UnityAdListener extends UnityPaidEventListener {

void onAdLoaded();
void onAdFailedToLoad(String errorReason);
void onAdFailedToLoad(LoadAdError error);
void onAdOpened();
void onAdClosed();
void onAdLeftApplication();
Expand Down
Expand Up @@ -15,12 +15,14 @@
*/
package com.google.unity.ads;

import com.google.android.gms.ads.LoadAdError;

/**
* An interface form of {@link UnityAdLoaderListener} that can be implemented via
* {@code AndroidJavaProxy} in Unity to receive ad events synchronously.
*/
public interface UnityAdLoaderListener {
void onAdFailedToLoad(String errorReason);
void onAdFailedToLoad(LoadAdError error);
void onCustomTemplateAdLoaded(CustomNativeAd ad);
void onCustomClick(CustomNativeAd ad, String assetName);
}
Expand Up @@ -3,8 +3,10 @@
import android.app.Activity;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.OnPaidEventListener;
import com.google.android.gms.ads.ResponseInfo;
import com.google.android.gms.ads.rewarded.RewardItem;
Expand Down Expand Up @@ -86,6 +88,7 @@ public void loadAd(final AdRequest request) {
@Override
public void run() {
rewardedAd.loadAd(request, new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
if (callback != null) {
new Thread(new Runnable() {
Expand All @@ -99,15 +102,14 @@ public void run() {
}
}


public void onRewardedAdFailedToLoad(final int errorCode) {
@Override
public void onRewardedAdFailedToLoad(final LoadAdError error) {
if (callback != null) {
new Thread(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.onRewardedAdFailedToLoad(
PluginUtils.getErrorReason(errorCode));
callback.onRewardedAdFailedToLoad(error);
}
}
}).start();
Expand Down Expand Up @@ -154,6 +156,7 @@ public void show() {
public void run() {
if (rewardedAd.isLoaded()) {
rewardedAd.show(activity, new RewardedAdCallback() {
@Override
public void onRewardedAdOpened() {
if (callback != null) {
new Thread(new Runnable() {
Expand All @@ -167,6 +170,7 @@ public void run() {
}
}

@Override
public void onRewardedAdClosed() {
if (callback != null) {
new Thread(new Runnable() {
Expand All @@ -180,6 +184,7 @@ public void run() {
}
}

@Override
public void onUserEarnedReward(@NonNull final RewardItem reward) {
if (callback != null) {
new Thread(new Runnable() {
Expand All @@ -194,14 +199,14 @@ public void run() {
}
}

public void onRewardedAdFailedToShow(final int errorCode) {
@Override
public void onRewardedAdFailedToShow(final AdError error) {
if (callback != null) {
new Thread(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.onRewardedAdFailedToShow(
PluginUtils.getErrorReason(errorCode));
callback.onRewardedAdFailedToShow(error);
}
}
}).start();
Expand Down
@@ -1,5 +1,8 @@
package com.google.unity.ads;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.LoadAdError;

/**
* An interface form of {@link UnityRewardedAdCallback} that can be implemented via {@code
* AndroidJavaProxy} in Unity to receive ad events synchronously.
Expand All @@ -8,9 +11,9 @@ public interface UnityRewardedAdCallback extends UnityPaidEventListener {

void onRewardedAdLoaded();

void onRewardedAdFailedToLoad(String errorReason);
void onRewardedAdFailedToLoad(LoadAdError error);

void onRewardedAdFailedToShow(String errorReason);
void onRewardedAdFailedToShow(AdError error);

void onRewardedAdOpened();

Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.OnPaidEventListener;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.ResponseInfo;
Expand Down Expand Up @@ -95,14 +96,13 @@ public void run() {
rewardedInterstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdFailedToShowFullScreenContent(final AdError adError) {
public void onAdFailedToShowFullScreenContent(final AdError error) {
new Thread(
new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.onAdFailedToShowFullScreenContent(
adError.getMessage());
callback.onAdFailedToShowFullScreenContent(error);
}
}
})
Expand Down Expand Up @@ -151,14 +151,13 @@ public void run() {
}

@Override
public void onRewardedInterstitialAdFailedToLoad(final int errorCode) {
public void onRewardedInterstitialAdFailedToLoad(final LoadAdError error) {
new Thread(
new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.onRewardedInterstitialAdFailedToLoad(
PluginUtils.getErrorReason(errorCode));
callback.onRewardedInterstitialAdFailedToLoad(error);
}
}
})
Expand Down
Expand Up @@ -16,18 +16,21 @@

package com.google.unity.ads;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.LoadAdError;

/**
* Rewarded interstitial ad events that can be implemented via {@code AndroidJavaProxy} in Unity.
*/
public interface UnityRewardedInterstitialAdCallback extends UnityPaidEventListener {

void onRewardedInterstitialAdLoaded();

void onRewardedInterstitialAdFailedToLoad(String errorReason);
void onRewardedInterstitialAdFailedToLoad(LoadAdError error);

void onUserEarnedReward(String type, float amount);

void onAdFailedToShowFullScreenContent(String errorReason);
void onAdFailedToShowFullScreenContent(AdError error);

void onAdShowedFullScreenContent();

Expand Down
76 changes: 76 additions & 0 deletions source/plugin/Assets/GoogleMobileAds/Api/AdError.cs
@@ -0,0 +1,76 @@
// Copyright (C) 2020 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.

using UnityEngine;
using GoogleMobileAds.Common;
namespace GoogleMobileAds.Api
{
/// <summary>
/// Gets error information about why an ad load operation failed.
/// </summary>
/// <returns>Ad Error Object with Code, Message, Domain, and Cause.</returns>
public class AdError
{
private IAdErrorClient client;
public AdError(IAdErrorClient client)
{
this.client = client;

}

/// <summary>
/// Gets the error code.
/// </summary>
/// <returns>Ad Error Code. </returns>
public int GetCode()
{
return client.GetCode();
}

/// <summary>
/// Gets the domain from which the error came.
/// </summary>
/// <returns>Ad Error Domain. </returns>
public string GetDomain()
{
return client.GetDomain();
}

/// <summary>
/// Gets the error message.
/// See https://support.google.com/admob/answer/9905175
/// for explanations of common errors.
/// <summary>
/// <returns>Ad Error Message. </returns>
public string GetMessage()
{
return client.GetMessage();
}

///<summary>
/// Gets the underlying error cause if exists
/// <summary>
/// <returns>Ad Error Cause. </returns>
public AdError GetCause()
{
return new AdError(client.GetCause());
}

public override string ToString()
{
return client.ToString();
}
}
}

11 changes: 11 additions & 0 deletions source/plugin/Assets/GoogleMobileAds/Api/AdError.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -16,8 +16,13 @@

namespace GoogleMobileAds.Api
{
// Event arguments that return when an ad operation fails.
public class AdErrorEventArgs : EventArgs
{
// AdError Object that returns when an ad operation fails.
public AdError AdError { get; set; }

[Obsolete ("use AdError.GetMessage() instead.")]
public string Message { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,4 +1,4 @@
// Copyright (C) 2015 Google, Inc.
// Copyright (C) 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,9 @@ namespace GoogleMobileAds.Api
// Event that occurs when an ad fails to load.
public class AdFailedToLoadEventArgs : EventArgs
{
public LoadAdError LoadAdError { get; set; }

[Obsolete ("Use LoadAdError.GetMessage() instead.")]
public string Message { get; set; }
}
}

0 comments on commit 920ca81

Please sign in to comment.