Last active
January 3, 2016 03:28
-
-
Save amr/8402104 to your computer and use it in GitHub Desktop.
MoPub adapter to serve AdMob's interstitials with the new Google Play Services API.
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 a modified version based on code by MoPub Inc. | |
* The author's modifications are released into the public domain. MoPub .inc code | |
* is originally released under the license below. | |
* | |
* Copyright (c) 2010-2013, MoPub Inc. | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are | |
* met: | |
* | |
* Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* | |
* Neither the name of 'MoPub Inc.' nor the names of its contributors | |
* may be used to endorse or promote products derived from this software | |
* without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
package org.amrmostafa.mopub.adapters; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.util.Log; | |
import com.google.android.gms.ads.AdListener; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.InterstitialAd; | |
import com.mopub.mobileads.CustomEventInterstitial; | |
import java.util.Map; | |
import static com.mopub.mobileads.MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR; | |
import static com.mopub.mobileads.MoPubErrorCode.NETWORK_INVALID_STATE; | |
import static com.mopub.mobileads.MoPubErrorCode.NETWORK_NO_FILL; | |
/** | |
* MoPub adapter to serve AdMob's interstitials with the new Google Play Services API | |
* | |
* To use this, add a custom native event to MoPub, with the following as the class name and data: | |
* | |
* Class name: org.amrmostafa.mopub.adapters.GooglePlayServicesInterstitial | |
* Data: {"adUnitID": "<your-admob-interstitial-unit-id>"} | |
* | |
* Based on the vanilla MoPub adapter for the deprecated AdMob API whic can be found at: https://github.com/mopub/mopub-android-sdk/blob/master/extras/src/com/mopub/mobileads/GoogleAdMobInterstitial.java | |
* | |
* However, it doesn't make use of the "Location" bit. | |
*/ | |
public class GooglePlayServicesInterstitial extends CustomEventInterstitial { | |
public static final String AD_UNIT_ID_KEY = "adUnitID"; | |
private InterstitialAd mInterstitialAd; | |
private CustomEventInterstitialListener mInterstitialListener; | |
@Override | |
protected void loadInterstitial(Context context, | |
CustomEventInterstitialListener customEventInterstitialListener, | |
Map<String, Object> localExtras, | |
Map<String, String> serverExtras) { | |
mInterstitialListener = customEventInterstitialListener; | |
if (!(context instanceof Activity)) { | |
mInterstitialListener.onInterstitialFailed(ADAPTER_CONFIGURATION_ERROR); | |
return; | |
} | |
String pubId; | |
if (extrasAreValid(serverExtras)) { | |
pubId = serverExtras.get(AD_UNIT_ID_KEY); | |
} else { | |
mInterstitialListener.onInterstitialFailed(ADAPTER_CONFIGURATION_ERROR); | |
return; | |
} | |
mInterstitialAd = new InterstitialAd((Activity) context); | |
mInterstitialAd.setAdUnitId(pubId); | |
mInterstitialAd.setAdListener(new GooglePlayServicesAdListener(customEventInterstitialListener)); | |
AdRequest adRequest = new AdRequest.Builder().addTestDevice("9AE74AC8FBFB5EDB59797116525FEAA8").build(); | |
mInterstitialAd.loadAd(adRequest); | |
} | |
@Override | |
protected void showInterstitial() { | |
if (mInterstitialAd.isLoaded()) { | |
mInterstitialAd.show(); | |
} else { | |
Log.d("MoPub", "Tried to show a Google AdMob interstitial ad before it finished loading. Please try again."); | |
} | |
} | |
@Override | |
protected void onInvalidate() { | |
if (mInterstitialAd != null) { | |
mInterstitialAd.setAdListener(null); | |
} | |
} | |
private boolean extrasAreValid(Map<String, String> serverExtras) { | |
return serverExtras.containsKey(AD_UNIT_ID_KEY); | |
} | |
static public class GooglePlayServicesAdListener extends AdListener { | |
private CustomEventInterstitialListener mInterstitialListener; | |
private boolean mHasAlreadyRegisteredClick; | |
public GooglePlayServicesAdListener(CustomEventInterstitialListener mInterstitialListener) { | |
this.mInterstitialListener = mInterstitialListener; | |
} | |
@Override | |
public void onAdClosed() { | |
Log.d("MoPub", "Google AdMob interstitial ad dismissed."); | |
mInterstitialListener.onInterstitialDismissed(); | |
} | |
@Override | |
public void onAdFailedToLoad(int errorCode) { | |
Log.d("MoPub", "Google AdMob interstitial ad failed to load."); | |
if (errorCode == AdRequest.ERROR_CODE_NO_FILL) | |
mInterstitialListener.onInterstitialFailed(NETWORK_NO_FILL); | |
else if (errorCode == AdRequest.ERROR_CODE_INVALID_REQUEST) | |
mInterstitialListener.onInterstitialFailed(ADAPTER_CONFIGURATION_ERROR); | |
else | |
mInterstitialListener.onInterstitialFailed(NETWORK_INVALID_STATE); | |
} | |
@Override | |
public void onAdLeftApplication() { | |
if (!mHasAlreadyRegisteredClick) { | |
Log.d("MoPub", "Google AdMob interstitial ad clicked."); | |
mHasAlreadyRegisteredClick = true; | |
mInterstitialListener.onInterstitialClicked(); | |
} | |
} | |
@Override | |
public void onAdOpened() { | |
Log.d("MoPub", "Showing Google AdMob interstitial ad."); | |
mInterstitialListener.onInterstitialShown(); | |
} | |
@Override | |
public void onAdLoaded() { | |
Log.d("MoPub", "Google AdMob interstitial ad loaded successfully."); | |
mInterstitialListener.onInterstitialLoaded(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment