Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created June 26, 2012 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loretoparisi/2998309 to your computer and use it in GitHub Desktop.
Save loretoparisi/2998309 to your computer and use it in GitHub Desktop.
MoPub Android Interstitial
package com.designfuture.music.util;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.widget.FrameLayout;
import com.beintoo.main.Beintoo;
import com.beintoo.main.Beintoo.BGetVgoodListener;
import com.beintoo.main.Beintoo.BPlayerLoginListener;
import com.beintoo.wrappers.Player;
import com.beintoo.wrappers.VgoodChooseOne;
import com.brightroll.androidsdk.Ad;
import com.brightroll.androidsdk.AdDelegate;
import com.designfuture.framework.util.LogHelper;
import com.designfuture.music.global.Global;
import com.inmobi.androidsdk.IMAdInterstitial;
import com.inmobi.androidsdk.IMAdInterstitialListener;
import com.inmobi.androidsdk.IMAdRequest;
import com.inmobi.androidsdk.IMAdRequest.ErrorCode;
import com.inmobi.androidsdk.IMSDKUtil;
import com.inneractive.api.ads.InneractiveAd;
import com.mopub.mobileads.MoPubInterstitial;
import com.mopub.mobileads.MoPubInterstitial.MoPubInterstitialListener;
import com.mopub.mobileads.MoPubInterstitial.MoPubInterstitialView;
public class MoPubInterstitialHelper {
/**
* Apertura appliacazione Name: Android_Interstitial_Startup AdUnit:
* agltb3B1Yi1pbmNyDQsSBFNpdGUYr9CHFAw
*
* Successo del music id Name: Android_Interstitial_MusicID AdUnit:
* agltb3B1Yi1pbmNyDQsSBFNpdGUY-feMFAw
*
* Click sul footer NowPlaying Name: Android_Interstitial_NowPlaying AdUnit:
* agltb3B1Yi1pbmNyDQsSBFNpdGUY4f-MFAw
*
* Click su un elemento della vista Album Name: Android_Interstitial_Album
* AdUnit: agltb3B1Yi1pbmNyDQsSBFNpdGUYw-CMFAw
*
* Search Name: Android_Interstitial_Search AdUnit:
* agltb3B1Yi1pbmNyDQsSBFNpdGUYl9WIFAw
*/
public enum InterstitialPostion {
DEFAULT(Global.MOPUP_FULLSCREEN_PUBLISHER_ID, "Default"), APP_OPEN("agltb3B1Yi1pbmNyDQsSBFNpdGUYr9CHFAw", "Android_Interstitial_Startup"), MUSIC_ID_RESULT(
"agltb3B1Yi1pbmNyDQsSBFNpdGUY-feMFAw", "Android_Interstitial_MusicID"), NOW_PLAYING_FOOTER_CLICK("agltb3B1Yi1pbmNyDQsSBFNpdGUY4f-MFAw", "Android_Interstitial_NowPlaying"), ALBUM_DETAIL_ITEM_CLICK(
"agltb3B1Yi1pbmNyDQsSBFNpdGUYw-CMFAw", "Android_Interstitial_Album"), SEARCH("agltb3B1Yi1pbmNyDQsSBFNpdGUYl9WIFAw", "Android_Interstitial_Search");
String key;
String name;
InterstitialPostion(String key, String name) {
this.key = key;
this.name = name;
}
public String toString() {
return "AdUnit [ - name: " + name + " - id: " + key + " ]";
}
}
private static String TAG = "MoPubInterstitialHelper";
private static final int MAX_CONSECUTIVE_FAIL = 20;
private static final int MAX_SECONDS_FAIL = 30;
private static MoPubInterstitialHelper instance;
private boolean stopRequests = false;
public Activity mCallerActivity = null;
public static MoPubInterstitialHelper getInstance() {
if (instance == null)
instance = new MoPubInterstitialHelper();
return instance;
}
public MoPubInterstitial createMoPubView(Activity activity, boolean connected) {
return createMoPubView(activity, InterstitialPostion.DEFAULT, connected);
}
/**
*
* standard method used by the activities to create an Interstitial
*
* @param activity
* @param connected
* @return
*/
public MoPubInterstitial createMoPubView(Activity activity, InterstitialPostion position, boolean connected) {
if ( activity == null || !connected || position == null )
return null;
stopRequests = false;
mCallerActivity = activity;
MoPubInterstitial adView = new MoPubInterstitial(activity, position.key);
adView.setListener(new AdListener(adView));
adView.load();
return adView;
}
public void stopRequests() {
stopRequests = true;
mCallerActivity = null;
}
protected void adLoaded(MoPubInterstitialView adView) {
// notify to our MoPubInterstitialView that the custom event
// successfully loaded an interstitial
adView.customEventDidLoadAd();
}
protected void adFailedToRecive(MoPubInterstitialView adView) {
// notify to our MoPubInterstitialView that the custom event fail
adView.customEventDidFailToLoadAd();
}
/**
* mopub custom event method for Beingintoo
*/
public void loadBeintooAd(final MoPubInterstitialView adView) {
LogHelper.i(TAG, "loadBeeintoAd");
Beintoo.setApiKey(Global.BEINTOO_API_KEY);
// DebugUtility.isDebugEnable = true;
// BeintooSdkParams.useSandbox = true;
final Activity activity = adView.getActivity();
// Registrazione dell'utente/app
Beintoo.playerLogin(activity, new BPlayerLoginListener() {
@Override
public void onError() {
LogHelper.i(TAG, "loadBeintooAd - playerLogin onError");
}
@Override
public void onComplete(Player p) {
adView.customEventActionWillBegin();
Beintoo.GetVgood(adView.getContext(), null, false, null, Beintoo.VGOOD_NOTIFICATION_ALERT, new BGetVgoodListener() {
@Override
public void onComplete(VgoodChooseOne v) {
LogHelper.i(TAG, "loadBeintooAd - VgoodChooseOne");
adLoaded(adView);
}
@Override
public void onError() {
LogHelper.i(TAG, "loadBeintooAd - onError");
adFailedToRecive(adView);
}
@Override
public void nothingToDispatch() {
LogHelper.i(TAG, "loadBeintooAd - nothingToDispatch");
adFailedToRecive(adView);
}
@Override
public void isOverQuota() {
LogHelper.i(TAG, "loadBeintooAd - isOverQuota");
adFailedToRecive(adView);
}
});
}
});
}
/**
* mopub custom event method for inMobi
*
* @param adView
*/
public void loadInMobiAd(final MoPubInterstitialView adView) {
LogHelper.i(TAG, "loadInMobiAd");
// initialize an interstitial ad
final IMAdInterstitial mIMAdInterstitial = new IMAdInterstitial((Activity) adView.getContext(), Global.INMOBI_PUBLISHER_ID);
// set the listener if the app has to know ad status notifications
mIMAdInterstitial.setImAdInterstitialListener(new IMAdInterstitialListener() {
@Override
public void onShowAdScreen(IMAdInterstitial view) {
LogHelper.i(TAG, "loadInMobiAd -> onShowAdScreen");
adView.customEventActionWillBegin();
}
@Override
public void onLeaveApplication(IMAdInterstitial view) {
LogHelper.i(TAG, "loadInMobiAd -> onLeaveApplication");
}
@Override
public void onDismissAdScreen(IMAdInterstitial view) {
LogHelper.i(TAG, "loadInMobiAd -> onDismissAdScreen");
}
@Override
public void onAdRequestLoaded(IMAdInterstitial view) {
// notify load to mopub
adLoaded(adView);
// show inmobi interstitial
mIMAdInterstitial.show();
LogHelper.i(TAG, "loadInMobiAd -> onAdRequestLoaded");
}
@Override
public void onAdRequestFailed(IMAdInterstitial view, ErrorCode errcode) {
// notify fail to mopub
adFailedToRecive(adView);
LogHelper.i(TAG, "loadInMobiAd -> onAdRequestFailed");
}
});
// next line was missing
if (Global.isDebuggable())
IMSDKUtil.setLogLevel(1);
IMAdRequest mAdRequest = new IMAdRequest();
// set imobi request parameters
Map<String, String> map = new HashMap<String, String>();
map.put("tp", "c_mopub");
mAdRequest.setRequestParams(map);
mAdRequest.setTestMode(false);
// trigger the request
mIMAdInterstitial.loadNewAd(mAdRequest);
}
public void loadBrightRoll(final MoPubInterstitialView AdView) {
LogHelper.i(TAG, "loadBrightRoll");
// Ad listener
AdDelegate delegate = new AdDelegate() {
@Override
public Activity getAdActivity() {
LogHelper.i(TAG, "loadBrightRoll - getAdActivity");
return AdView.getActivity();
}
@Override
public void adFetched(Ad ad) {
LogHelper.i(TAG, "loadBrightRoll - adFetched");
adLoaded(AdView);
ad.show();
}
@Override
public void adFetchFailed(Ad ad) {
LogHelper.i(TAG, "loadBrightRoll - adFetchFailed");
adFailedToRecive(AdView);
}
@Override
public void adDismissed(Ad ad) {
LogHelper.i(TAG, "loadBrightRoll - adDismissed");
}
};
// create new Ads
Ad ad = new Ad();
ad.setDelegate(delegate);
// public static final String BRIGHTROLL_PUBLISHER_ID = "3846604";
ad.setSitePlacementId(Integer.parseInt(Global.BRIGHTROLL_PUBLISHER_ID));
// start fetching the event
// AdView.customEventActionWillBegin();
ad.fetch();
}
/**
* mopub custom event method for Inneractive
*
* @param adView
*/
public void loadInneractiveAd(MoPubInterstitialView view) {
LogHelper.i(TAG, "loadInneractiveAd");
view.customEventActionWillBegin();
try {
Activity activity = view.getActivity();
android.webkit.WebViewDatabase webViewDB = android.webkit.WebViewDatabase.getInstance(activity);
if (webViewDB == null) {
adFailedToRecive(view);
return;
}
FrameLayout layout = new FrameLayout(activity);
layout.setTag("Inneractive");
layout.setBackgroundResource(android.R.color.transparent);
if (!InneractiveAd.displayAd(activity, layout, Global.INNERACTIVE_PUBLISHER_ID, InneractiveAd.IaAdType.Interstitial, 0)) {
LogHelper.i(TAG, "loadInneractiveAd -> adFailedToRecive");
adFailedToRecive(view);
} else {
LogHelper.i(TAG, "loadInneractiveAd -> adLoaded");
adLoaded(view);
}
} catch (Exception e) {
LogHelper.e(TAG, "loadInneractiveAd -> Exception", e);
//e.printStackTrace();
adFailedToRecive(view);
}
}
private class AdListener implements MoPubInterstitialListener {
int count = MAX_CONSECUTIVE_FAIL;
long first_request_time = 0;
MoPubInterstitial adView;
public AdListener(MoPubInterstitial ad) {
if (ad == null)
throw new IllegalArgumentException();
this.adView = ad;
first_request_time = System.currentTimeMillis();
}
@Override
public void OnInterstitialLoaded() {
LogHelper.i(TAG, "OnInterstitialLoaded");
if (adView.isReady() && (mCallerActivity == null || !mCallerActivity.equals(adView.getActivity())))
LogHelper.i(TAG, "interstitial riferito alla richiesta su un altra activity");
if (adView.isReady() && mCallerActivity != null && mCallerActivity.equals(adView.getActivity())) {
count = MAX_CONSECUTIVE_FAIL;
adView.show();
}
}
@Override
public void OnInterstitialFailed() {
LogHelper.i(TAG, "OnInterstitialFailed");
if (adView.isReady() && (mCallerActivity == null || !mCallerActivity.equals(adView.getActivity())))
LogHelper.i(TAG, "interstitial riferito alla richiesta su un altra activity");
if (adView.isReady() && mCallerActivity != null && mCallerActivity.equals(adView.getActivity())) {
adView.show();
count = MAX_CONSECUTIVE_FAIL;
} else {
if (count > 0 && (System.currentTimeMillis() - first_request_time) < (MAX_SECONDS_FAIL * 1000) && !stopRequests) {
count--;
adView.load();
return;
}
}
}
}
public void loadMadvertiseAd(MoPubInterstitialView adView) {
LogHelper.i(TAG, "loadMadvertiseAd");
LogHelper.i(TAG, "loadMadvertiseAd -> adFailedToRecive");
adFailedToRecive(adView);
// new MadvertiseHelper(adView);
}
/*
* private class MadvertiseHelper implements MadvertiseViewCallbackListener
* {
*
* private MoPubInterstitialView adView;
*
* public MadvertiseHelper(MoPubInterstitialView view) { if (view == null)
* return;
*
* this.adView = view;
*
* LayoutInflater inflater = (LayoutInflater)
* view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
*
* MoPubInterstitialView madvertiseView = (MoPubInterstitialView)
* inflater.inflate(R.layout.madvertise_interstitial_ads, null);
* madvertiseView.setMadvertiseViewCallbackListener(this); }
*
* @Override public void onLoaded(boolean succeed, MadvertiseView madView) {
* LogHelper.i(TAG, "MAdvertiseHelper.onLoaded: succeed: " + succeed);
*
* // code to make transparent webView background work after Android // 3.0>
* if (madView != null && madView.getChildAt(0) != null &&
* !UIUtils.isBeforeHoneycomb()) ((WebView)
* madView.getChildAt(0)).setLayerType(View.LAYER_TYPE_SOFTWARE, null);
*
* if (succeed) adLoaded(adView); else adFailedToRecive(adView); }
*
* @Override public void onError(Exception exception) { LogHelper.i(TAG,
* "MAdvertiseHelper.onError"); adFailedToRecive(adView); }
*
* @Override public void onIllegalHttpStatusCode(int statusCode, String
* message) { LogHelper.i(TAG,
* "MAdvertiseHelper.onIllegalHttpStatusCode: status: " + statusCode +
* " - message: " + message); // after this callback onError is called so we
* don't need to call // adFailedToRecive }
*
* @Override public void onAdClicked() { }
*
* @Override public void onApplicationPause() { }
*
* @Override public void onApplicationResume() { } }
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment