|
| 1 | +package com.google.ads.mediation.sample.customevent; |
| 2 | + |
| 3 | +import com.google.ads.mediation.sample.sdk.SampleAdRequest; |
| 4 | +import com.google.ads.mediation.sample.sdk.SampleAdSize; |
| 5 | +import com.google.ads.mediation.sample.sdk.SampleAdView; |
| 6 | +import com.google.ads.mediation.sample.sdk.SampleInterstitial; |
| 7 | +import com.google.android.gms.ads.AdSize; |
| 8 | +import com.google.android.gms.ads.mediation.MediationAdRequest; |
| 9 | +import com.google.android.gms.ads.mediation.customevent.CustomEventBanner; |
| 10 | +import com.google.android.gms.ads.mediation.customevent.CustomEventBannerListener; |
| 11 | +import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitial; |
| 12 | +import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitialListener; |
| 13 | + |
| 14 | +import android.content.Context; |
| 15 | +import android.os.Bundle; |
| 16 | + |
| 17 | +/** |
| 18 | + * A custom event for the Sample ad network. Custom events allow publishers to write their own |
| 19 | + * mediation adapter. |
| 20 | + */ |
| 21 | +public class SampleCustomEvent implements CustomEventBanner, CustomEventInterstitial { |
| 22 | + |
| 23 | + /** The {@link SampleAdView} representing a banner ad. */ |
| 24 | + private SampleAdView sampleAdView; |
| 25 | + |
| 26 | + /** Represents a {@link SampleInterstitial}. */ |
| 27 | + private SampleInterstitial sampleInterstitial; |
| 28 | + |
| 29 | + /** The event is being destroyed. Perform any necessary cleanup here. */ |
| 30 | + @Override |
| 31 | + public void onDestroy() { |
| 32 | + if (sampleAdView != null) { |
| 33 | + sampleAdView.destroy(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * The app is being paused. This call will only be forwarded to the adapter if the developer |
| 39 | + * notifies mediation that the app is being paused. |
| 40 | + */ |
| 41 | + @Override |
| 42 | + public void onPause() { |
| 43 | + // The sample ad network doesn't have an onPause method, so it does nothing. |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * The app is being resumed. This call will only be forwarded to the adapter if the developer |
| 48 | + * notifies mediation that the app is being resumed. |
| 49 | + */ |
| 50 | + @Override |
| 51 | + public void onResume() { |
| 52 | + // The sample ad network doesn't have an onResume method, so it does nothing. |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + public void requestBannerAd(Context context, |
| 58 | + CustomEventBannerListener listener, |
| 59 | + String serverParameter, |
| 60 | + AdSize size, |
| 61 | + MediationAdRequest mediationAdRequest, |
| 62 | + Bundle customEventExtras) { |
| 63 | + /* |
| 64 | + * In this method, you should: |
| 65 | + * |
| 66 | + * 1. Create your banner view. |
| 67 | + * 2. Set your ad network's listener. |
| 68 | + * 3. Make an ad request. |
| 69 | + * |
| 70 | + * When setting your ad network's listener, don't forget to send the following callbacks: |
| 71 | + * |
| 72 | + * listener.onAdLoaded(this); |
| 73 | + * listener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_*); |
| 74 | + * listener.onAdClicked(this); |
| 75 | + * listener.onAdOpened(this); |
| 76 | + * listener.onAdLeftApplication(this); |
| 77 | + * listener.onAdClosed(this); |
| 78 | + */ |
| 79 | + |
| 80 | + sampleAdView = new SampleAdView(context); |
| 81 | + |
| 82 | + // Assumes that the serverParameter is the AdUnit for the Sample Network. |
| 83 | + sampleAdView.setAdUnit(serverParameter); |
| 84 | + |
| 85 | + sampleAdView.setSize(new SampleAdSize(size.getWidth(), size.getHeight())); |
| 86 | + |
| 87 | + // Implement a SampleAdListener and forward callbacks to mediation. The callback forwarding is |
| 88 | + // handled by SampleBannerEventFowarder. |
| 89 | + sampleAdView.setAdListener(new SampleCustomBannerEventForwarder(listener, sampleAdView)); |
| 90 | + |
| 91 | + // Make an ad request. |
| 92 | + sampleAdView.fetchAd(createSampleRequest(mediationAdRequest)); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Helper method to create a {@link SampleAdRequest}. |
| 98 | + * @param mediationAdRequest The mediation request with targeting information. |
| 99 | + * @return The created {@link SampleAdRequest}. |
| 100 | + */ |
| 101 | + private SampleAdRequest createSampleRequest(MediationAdRequest mediationAdRequest) { |
| 102 | + SampleAdRequest request = new SampleAdRequest(); |
| 103 | + request.setTestMode(mediationAdRequest.isTesting()); |
| 104 | + request.setKeywords(mediationAdRequest.getKeywords()); |
| 105 | + return request; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void requestInterstitialAd(Context context, |
| 110 | + CustomEventInterstitialListener listener, |
| 111 | + String serverParameter, |
| 112 | + MediationAdRequest mediationAdRequest, |
| 113 | + Bundle customEventExtras) { |
| 114 | + /* |
| 115 | + * In this method, you should: |
| 116 | + * |
| 117 | + * 1. Create your interstitial ad. |
| 118 | + * 2. Set your ad network's listener. |
| 119 | + * 3. Make an ad request. |
| 120 | + * |
| 121 | + * When setting your ad network's listener, don't forget to send the following callbacks: |
| 122 | + * |
| 123 | + * listener.onAdLoaded(this); |
| 124 | + * listener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_*); |
| 125 | + * listener.onAdOpened(this); |
| 126 | + * listener.onAdLeftApplication(this); |
| 127 | + * listener.onAdClosed(this); |
| 128 | + */ |
| 129 | + |
| 130 | + sampleInterstitial = new SampleInterstitial(context); |
| 131 | + |
| 132 | + // Here we're assuming the serverParameter is the ad unit for the Sample Ad Network. |
| 133 | + sampleInterstitial.setAdUnit(serverParameter); |
| 134 | + |
| 135 | + // Implement a SampleAdListener and forward callbacks to mediation. |
| 136 | + sampleInterstitial.setAdListener(new SampleCustomInterstitialEventForwarder(listener)); |
| 137 | + |
| 138 | + // Make an ad request. |
| 139 | + sampleInterstitial.fetchAd(createSampleRequest(mediationAdRequest)); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void showInterstitial() { |
| 145 | + // Show your interstitial ad. |
| 146 | + sampleInterstitial.show(); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments