Skip to content

Commit

Permalink
Updating Unity and AdColony adapters (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
bichenwang authored Mar 3, 2017
1 parent 3961087 commit 34bd34a
Show file tree
Hide file tree
Showing 5 changed files with 545 additions and 298 deletions.
222 changes: 135 additions & 87 deletions extras/src/com/mopub/mobileads/AdColonyInterstitial.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@
import android.os.Handler;
import android.util.Log;

import com.jirbo.adcolony.AdColony;
import com.jirbo.adcolony.AdColonyAd;
import com.jirbo.adcolony.AdColonyAdListener;
import com.jirbo.adcolony.AdColonyVideoAd;
import com.adcolony.sdk.AdColony;
import com.adcolony.sdk.AdColonyAppOptions;
import com.adcolony.sdk.AdColonyInterstitialListener;
import com.adcolony.sdk.AdColonyZone;
import com.mopub.common.util.Json;

import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
* Tested with AdColony SDK 2.0.3.
*/
public class AdColonyInterstitial extends CustomEventInterstitial implements AdColonyAdListener {
public class AdColonyInterstitial extends CustomEventInterstitial {
private static final String TAG = "AdColonyInterstitial";
/*
* We recommend passing the AdColony client options, app ID, all zone IDs, and current zone ID
* in the serverExtras Map by specifying Custom Event Data in MoPub's web interface.
*
* Please see AdColony's documentation for more information:
* https://github.com/AdColony/AdColony-Android-SDK/wiki/API-Details#configure-activity-activity-string-client_options-string-app_id-string-zone_ids-
* https://github.com/AdColony/AdColony-Android-SDK-3
*/
private static final String DEFAULT_CLIENT_OPTIONS = "version=YOUR_APP_VERSION_HERE,store:google";
private static final String DEFAULT_APP_ID = "YOUR_AD_COLONY_APP_ID_HERE";
Expand All @@ -39,16 +35,12 @@ public class AdColonyInterstitial extends CustomEventInterstitial implements AdC
public static final String ALL_ZONE_IDS_KEY = "allZoneIds";
public static final String ZONE_ID_KEY = "zoneId";

private static boolean isAdColonyConfigured = false;

private CustomEventInterstitialListener mCustomEventInterstitialListener;
private AdColonyInterstitialListener mAdColonyInterstitialListener;
private final Handler mHandler;
private AdColonyVideoAd mAdColonyVideoAd;
private final ScheduledThreadPoolExecutor mScheduledThreadPoolExecutor;
private boolean mIsLoading;
private com.adcolony.sdk.AdColonyInterstitial mAdColonyInterstitial;

public AdColonyInterstitial() {
mScheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
mHandler = new Handler();
}

Expand Down Expand Up @@ -76,111 +68,167 @@ protected void loadInterstitial(Context context,
zoneId = serverExtras.get(ZONE_ID_KEY);
}

if (!isAdColonyConfigured) {
AdColony.configure((Activity)context, clientOptions, appId, allZoneIds);
isAdColonyConfigured = true;
mAdColonyInterstitialListener = getAdColonyInterstitialListener();
if (!isAdColonyConfigured()) {
AdColony.configure((Activity) context, getAppOptions(clientOptions), appId, allZoneIds);
}

mAdColonyVideoAd = new AdColonyVideoAd(zoneId);
mAdColonyVideoAd.withListener(this);

scheduleOnInterstitialLoaded();
AdColony.requestInterstitial(zoneId, mAdColonyInterstitialListener);
}

@Override
protected void showInterstitial() {
if (mAdColonyVideoAd.isReady()) {
mAdColonyVideoAd.show();
if (mAdColonyInterstitial == null || mAdColonyInterstitial.isExpired()) {
Log.e(TAG, "AdColony interstitial ad is null or has expired");
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.VIDEO_PLAYBACK_ERROR);
}
});
} else {
Log.d("MoPub", "Tried to show a AdColony interstitial ad before it finished loading. Please try again.");
mAdColonyInterstitial.show();
}
}

@Override
protected void onInvalidate() {
if (mAdColonyVideoAd != null) {
mAdColonyVideoAd.withListener(null);
if (mAdColonyInterstitial != null) {
mAdColonyInterstitialListener = null;
mAdColonyInterstitial.setListener(null);
mAdColonyInterstitial.destroy();
mAdColonyInterstitial = null;
}
}

private AdColonyAppOptions getAppOptions(String clientOptions) {
if (clientOptions == null || clientOptions.isEmpty()) {
return null;
}
AdColonyAppOptions adColonyAppOptions = new AdColonyAppOptions();
String[] allOptions = clientOptions.split(",");
for (String option : allOptions) {
String optionNameAndValue[] = option.split(":");
if (optionNameAndValue.length == 2) {
switch (optionNameAndValue[0]) {
case "store":
adColonyAppOptions.setOriginStore(optionNameAndValue[1]);
break;
case "version":
adColonyAppOptions.setAppVersion(optionNameAndValue[1]);
break;
default:
Log.e(TAG, "AdColony client options in wrong format - please check your MoPub dashboard");
return null;
}
} else {
Log.e(TAG, "AdColony client options in wrong format - please check your MoPub dashboard");
return null;
}
}

mScheduledThreadPoolExecutor.shutdownNow();
mIsLoading = false;
return adColonyAppOptions;
}

private boolean extrasAreValid(Map<String, String> extras) {
return extras.containsKey(CLIENT_OPTIONS_KEY)
&& extras.containsKey(APP_ID_KEY)
&& extras.containsKey(ALL_ZONE_IDS_KEY)
&& extras.containsKey(ZONE_ID_KEY);
private boolean isAdColonyConfigured() {
return !AdColony.getSDKVersion().isEmpty();
}

private String[] extractAllZoneIds(Map<String, String> serverExtras) {
String[] result = Json.jsonArrayToStringArray(serverExtras.get(ALL_ZONE_IDS_KEY));
private AdColonyInterstitialListener getAdColonyInterstitialListener() {
if (mAdColonyInterstitialListener != null) {
return mAdColonyInterstitialListener;
} else {
return new AdColonyInterstitialListener() {
@Override
public void onRequestFilled(com.adcolony.sdk.AdColonyInterstitial adColonyInterstitial) {
mAdColonyInterstitial = adColonyInterstitial;
Log.d(TAG, "AdColony interstitial ad has been successfully loaded.");
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialLoaded();
}
});
}

// AdColony requires at least one valid String in the allZoneIds array.
if (result.length == 0) {
result = new String[]{""};
}
@Override
public void onRequestNotFilled(AdColonyZone zone) {
Log.d(TAG, "AdColony interstitial ad has no fill.");
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_NO_FILL);
}
});
}

return result;
}
@Override
public void onClosed(com.adcolony.sdk.AdColonyInterstitial ad) {
Log.d(TAG, "AdColony interstitial ad has been dismissed.");
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialDismissed();
}
});
}

private void scheduleOnInterstitialLoaded() {
Runnable runnable = new Runnable() {
@Override
public void run() {
if (mAdColonyVideoAd.isReady()) {
Log.d("MoPub", "AdColony interstitial ad successfully loaded.");
mIsLoading = false;
mScheduledThreadPoolExecutor.shutdownNow();
@Override
public void onOpened(com.adcolony.sdk.AdColonyInterstitial ad) {
Log.d(TAG, "AdColony interstitial ad shown: " + ad.getZoneID());
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialLoaded();
mCustomEventInterstitialListener.onInterstitialShown();
}
});
}
}
};

if (!mIsLoading) {
mScheduledThreadPoolExecutor.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);
mIsLoading = true;
}
}
@Override
public void onExpiring(com.adcolony.sdk.AdColonyInterstitial ad) {
Log.d(TAG, "AdColony interstitial ad is expiring; requesting new ad");
AdColony.requestInterstitial(ad.getZoneID(), mAdColonyInterstitialListener);
}

/*
* AdColonyAdListener implementation
*/
@Override
public void onLeftApplication(com.adcolony.sdk.AdColonyInterstitial ad) {
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onLeaveApplication();
}
});
}

@Override
public void onAdColonyAdStarted(AdColonyAd adColonyAd) {
Log.d("MoPub", "AdColony interstitial ad shown.");
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialShown();
}
});
@Override
public void onClicked(com.adcolony.sdk.AdColonyInterstitial ad) {
mCustomEventInterstitialListener.onInterstitialClicked();
}
};
}
}

@Override
public void onAdColonyAdAttemptFinished(AdColonyAd adColonyAd) {
Log.d("MoPub", "AdColony interstitial ad dismissed.");
mHandler.post(new Runnable() {
@Override
public void run() {
mCustomEventInterstitialListener.onInterstitialDismissed();
}
});
private boolean extrasAreValid(Map<String, String> extras) {
return extras.containsKey(CLIENT_OPTIONS_KEY)
&& extras.containsKey(APP_ID_KEY)
&& extras.containsKey(ALL_ZONE_IDS_KEY)
&& extras.containsKey(ZONE_ID_KEY);
}

@Deprecated // for testing
ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() {
return mScheduledThreadPoolExecutor;
private String[] extractAllZoneIds(Map<String, String> serverExtras) {
String[] result = Json.jsonArrayToStringArray(serverExtras.get(ALL_ZONE_IDS_KEY));

// AdColony requires at least one valid String in the allZoneIds array.
if (result.length == 0) {
result = new String[]{""};
}

return result;
}

@Deprecated // for testing
void resetAdColonyConfigured() {
isAdColonyConfigured = false;
@Deprecated
// For testing
public static String getAdUnitId(MoPubInterstitial interstitial) {
return interstitial.getMoPubInterstitialView().getAdUnitId();
}
}
Loading

0 comments on commit 34bd34a

Please sign in to comment.