Created
July 22, 2014 03:01
-
-
Save midworld/30ea9f2c1c4f62b2cd61 to your computer and use it in GitHub Desktop.
Cocos2d-x Plugin-x for Google Play Ad (not AdMob SDK)
This file contains 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
package org.cocos2dx.cpp; | |
import java.util.HashSet; | |
import java.util.Hashtable; | |
import java.util.Iterator; | |
import java.util.Set; | |
import org.cocos2dx.plugin.AdsWrapper; | |
import org.cocos2dx.plugin.InterfaceAds; | |
import org.cocos2dx.plugin.PluginWrapper; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.AdSize; | |
import com.google.android.gms.ads.AdView; | |
import com.google.android.gms.ads.AdListener; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.util.Log; | |
import android.view.WindowManager; | |
public class GooglePlayAds implements InterfaceAds { | |
private static final String LOG_TAG = "GooglePlayAds"; | |
private static Activity mContext = null; | |
private static boolean bDebug = false; | |
private static GooglePlayAds mAdapter = null; | |
private AdView adView = null; | |
private String mPublishID = ""; | |
private Set<String> mTestDevices = null; | |
private WindowManager mWm = null; | |
private static final int ADMOB_SIZE_BANNER = 1; | |
private static final int ADMOB_SIZE_IABMRect = 2; | |
private static final int ADMOB_SIZE_IABBanner = 3; | |
private static final int ADMOB_SIZE_IABLeaderboard = 4; | |
private static final int ADMOB_SIZE_Skyscraper = 5; | |
private static final int ADMOB_TYPE_BANNER = 1; | |
private static final int ADMOB_TYPE_FULLSCREEN = 2; | |
protected static void LogE(String msg, Exception e) { | |
Log.e(LOG_TAG, msg, e); | |
e.printStackTrace(); | |
} | |
protected static void LogD(String msg) { | |
if (bDebug) { | |
Log.d(LOG_TAG, msg); | |
} | |
} | |
public GooglePlayAds(Context context) { | |
mContext = (Activity) context; | |
mAdapter = this; | |
} | |
@Override | |
public void setDebugMode(boolean debug) { | |
bDebug = debug; | |
} | |
@Override | |
public String getSDKVersion() { | |
return "5.0.77"; | |
} | |
@Override | |
public void configDeveloperInfo(Hashtable<String, String> devInfo) { | |
try { | |
mPublishID = devInfo.get("AdmobID"); | |
LogD("init AppInfo : " + mPublishID); | |
} catch (Exception e) { | |
LogE("initAppInfo, The format of appInfo is wrong", e); | |
} | |
} | |
@Override | |
public void showAds(Hashtable<String, String> info, int pos) { | |
try | |
{ | |
String strType = info.get("AdmobType"); | |
int adsType = Integer.parseInt(strType); | |
switch (adsType) { | |
case ADMOB_TYPE_BANNER: | |
{ | |
String strSize = info.get("AdmobSizeEnum"); | |
int sizeEnum = Integer.parseInt(strSize); | |
showBannerAd(sizeEnum, pos); | |
break; | |
} | |
case ADMOB_TYPE_FULLSCREEN: | |
LogD("Now not support full screen view in Admob"); | |
break; | |
default: | |
break; | |
} | |
} catch (Exception e) { | |
LogE("Error when show Ads ( " + info.toString() + " )", e); | |
} | |
} | |
@Override | |
public void spendPoints(int points) { | |
LogD("Admob not support spend points!"); | |
} | |
@Override | |
public void hideAds(Hashtable<String, String> info) { | |
try | |
{ | |
String strType = info.get("AdmobType"); | |
int adsType = Integer.parseInt(strType); | |
switch (adsType) { | |
case ADMOB_TYPE_BANNER: | |
hideBannerAd(); | |
break; | |
case ADMOB_TYPE_FULLSCREEN: | |
LogD("Now not support full screen view in Admob"); | |
break; | |
default: | |
break; | |
} | |
} catch (Exception e) { | |
LogE("Error when hide Ads ( " + info.toString() + " )", e); | |
} | |
} | |
private void showBannerAd(int sizeEnum, int pos) { | |
final int curPos = pos; | |
final int curSize = sizeEnum; | |
PluginWrapper.runOnMainThread(new Runnable() { | |
@Override | |
public void run() { | |
// destory the ad view before | |
if (null != adView) { | |
if (null != mWm) { | |
mWm.removeView(adView); | |
} | |
adView.destroy(); | |
adView = null; | |
} | |
AdSize size = AdSize.BANNER; | |
switch (curSize) { | |
case GooglePlayAds.ADMOB_SIZE_BANNER: | |
size = AdSize.BANNER; | |
break; | |
case GooglePlayAds.ADMOB_SIZE_IABMRect: | |
size = AdSize.FULL_BANNER; | |
break; | |
case GooglePlayAds.ADMOB_SIZE_IABBanner: | |
size = AdSize.LARGE_BANNER; | |
break; | |
case GooglePlayAds.ADMOB_SIZE_IABLeaderboard: | |
size = AdSize.LEADERBOARD; | |
break; | |
case GooglePlayAds.ADMOB_SIZE_Skyscraper: | |
size = AdSize.WIDE_SKYSCRAPER; | |
break; | |
default: | |
break; | |
} | |
adView = new AdView(mContext); | |
adView.setAdSize(size); | |
adView.setAdUnitId(mPublishID); | |
AdRequest.Builder reqBuiler = new AdRequest.Builder(); | |
try { | |
if (mTestDevices != null) { | |
Iterator<String> ir = mTestDevices.iterator(); | |
while(ir.hasNext()) | |
{ | |
reqBuiler.addTestDevice(ir.next()); | |
} | |
} | |
} catch (Exception e) { | |
LogE("Error during add test device", e); | |
} | |
AdRequest req = reqBuiler.build(); | |
adView.loadAd(req); | |
adView.setAdListener(new AdmobAdsListener()); | |
if (null == mWm) { | |
mWm = (WindowManager) mContext.getSystemService("window"); | |
} | |
AdsWrapper.addAdView(mWm, adView, curPos); | |
} | |
}); | |
} | |
private void hideBannerAd() { | |
PluginWrapper.runOnMainThread(new Runnable() { | |
@Override | |
public void run() { | |
if (null != adView) { | |
if (null != mWm) { | |
mWm.removeView(adView); | |
} | |
adView.destroy(); | |
adView = null; | |
} | |
} | |
}); | |
} | |
public void addTestDevice(String deviceID) { | |
LogD("addTestDevice invoked : " + deviceID); | |
if (null == mTestDevices) { | |
mTestDevices = new HashSet<String>(); | |
} | |
mTestDevices.add(deviceID); | |
} | |
private class AdmobAdsListener extends AdListener { | |
@Override | |
public void onAdClosed() { | |
LogD("onDismissScreen invoked"); | |
AdsWrapper.onAdsResult(mAdapter, AdsWrapper.RESULT_CODE_AdsDismissed, "Ads view dismissed!"); | |
} | |
@Override | |
public void onAdFailedToLoad(int errorCode) { | |
int errorNo = AdsWrapper.RESULT_CODE_UnknownError; | |
String errorMsg = "Unknow error"; | |
switch (errorCode) { | |
case 2: // ERROR_CODE_NETWORK_ERROR | |
errorNo = AdsWrapper.RESULT_CODE_NetworkError; | |
errorMsg = "Network error"; | |
break; | |
case 1: // ERROR_CODE_INVALID_REQUEST | |
errorNo = AdsWrapper.RESULT_CODE_NetworkError; | |
errorMsg = "The ad request is invalid"; | |
break; | |
case 3: // ERROR_CODE_NO_FILL | |
errorMsg = "The ad request is successful, but no ad was returned due to lack of ad inventory."; | |
break; | |
default: // ERROR_CODE_INTERNAL_ERROR = 0 | |
break; | |
} | |
LogD("failed to receive ad : " + errorNo + " , " + errorMsg); | |
AdsWrapper.onAdsResult(mAdapter, errorNo, errorMsg); | |
} | |
@Override | |
public void onAdLeftApplication() { | |
LogD("onAdLeftApplication invoked"); | |
} | |
@Override | |
public void onAdOpened() { | |
LogD("onAdOpened invoked"); | |
AdsWrapper.onAdsResult(mAdapter, AdsWrapper.RESULT_CODE_AdsShown, "Ads view shown!"); | |
} | |
@Override | |
public void onAdLoaded() { | |
LogD("onAdLoaded invoked"); | |
AdsWrapper.onAdsResult(mAdapter, AdsWrapper.RESULT_CODE_AdsReceived, "Ads request received success!"); | |
} | |
} | |
@Override | |
public String getPluginVersion() { | |
return "0.1.0"; | |
} | |
@Override | |
public void queryPoints() { | |
LogD("Admob not support query points!"); | |
} | |
} | |
/** usage | |
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID | |
const char* pluginName = "org/cocos2dx/cpp/GooglePlayAds"; | |
#else | |
const char* pluginName = "AdsAdmob"; | |
#endif | |
_admob = dynamic_cast<plugin::ProtocolAds*>( | |
plugin::PluginManager::getInstance()->loadPlugin(pluginName)); | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment