issue #2224 : Modify the implementation of plugin admob.

This commit is contained in:
zhangbin 2013-05-27 14:40:08 +08:00
parent 35bc7a51d8
commit c5965989b0
11 changed files with 23 additions and 326 deletions

View File

@ -1,96 +0,0 @@
/****************************************************************************
Copyright (c) 2012-2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCX_ADS_ADMOB_H__
#define __CCX_ADS_ADMOB_H__
#include "ProtocolAds.h"
#include <map>
#include <string>
namespace cocos2d { namespace plugin {
class AdsAdmob : public ProtocolAds
{
PLUGIN_REGISTER_DECL(AdsAdmob)
public:
typedef enum {
kSizeBanner = 0,
kSizeIABMRect,
kSizeIABBanner,
kSizeIABLeaderboard,
} AdmobBannerSize;
/**
@brief plugin initialization
*/
virtual bool init();
/**
@brief config the application info
@param devInfo This parameter is the info of application, must contains:
AdmobID The publisher ID of admob.
@warning Must invoke this interface before other interfaces.
And invoked only once.
*/
virtual void configDeveloperInfo(TAdsDeveloperInfo devInfo);
/**
@brief show adview
@param type The adview type need to show.
@param sizeEnum The size of the banner view.
(only used when type is kBannerAd)
Use the enum number in AdmobBannerSize.
@param pos The position where the adview be shown.
(only used when type is kBannerAd)
*/
virtual void showAds(AdsType type, int sizeEnum = 0, AdsPos pos = kPosCenter);
/**
@brief Hide the adview
@param type The adview type need to hide.
*/
virtual void hideAds(AdsType type);
/**
@brief Set whether needs to output logs to console.
@param debug if true debug mode enabled, or debug mode disabled.
*/
virtual void setDebugMode(bool debug);
/**
@brief Add the test device ID
@param deviceID The device ID
*/
void addTestDevice(const char* deviceID);
virtual const char* getPluginVersion() { return "v0.1.01"; };
virtual const char* getSDKVersion();
virtual ~AdsAdmob();
};
}} // namespace cocos2d { namespace plugin {
#endif /* __CCX_ADS_ADMOB_H__ */

View File

@ -1,60 +0,0 @@
[admob]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = pluginx_admob
# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = plugin
android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include/linux -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.6/include -I%(androidndkdir)s/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/lib/gcc/arm-linux-androideabi/4.6/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.1/include
clang_flags = -nostdinc -x c++
cocos_headers = -I%(pluginxdir)s/protocols/include -I%(pluginxdir)s/plugins/admob/include
cocos_flags = -DANDROID
cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common
# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s
# what headers to parse
headers = %(pluginxdir)s/plugins/admob/include/AdsAdmob.h
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^CCMenu*$".
classes = AdsAdmob
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just
# add a single "*" as functions. See bellow for several examples. A special class name is "*", which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.
skip = *::[createPlugin]
rename_functions =
rename_classes =
# for all class names, should we remove something when registering in the target VM?
remove_prefix =
# classes for which there will be no "parent" lookup
classes_have_no_parents =
# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip =
# classes that create no constructor
# CCSet is special and we will use a hand-written constructor
abstract_classes = AdsAdmob
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = yes

View File

@ -1,81 +0,0 @@
/****************************************************************************
Copyright (c) 2012-2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "AdsAdmob.h"
#include "PluginUtils.h"
namespace cocos2d { namespace plugin {
PLUGIN_REGISTER_IMPL(AdsAdmob)
AdsAdmob::~AdsAdmob()
{
}
bool AdsAdmob::init()
{
return PluginUtils::initJavaPlugin(this, "org.cocos2dx.plugin.AdsAdmob");
}
void AdsAdmob::configDeveloperInfo(TAdsDeveloperInfo appInfo)
{
ProtocolAds::configDeveloperInfo(appInfo);
}
void AdsAdmob::showAds(AdsType type, int sizeEnum, AdsPos pos)
{
ProtocolAds::showAds(type, sizeEnum, pos);
}
void AdsAdmob::hideAds(AdsType type)
{
ProtocolAds::hideAds(type);
}
const char* AdsAdmob::getSDKVersion()
{
return ProtocolAds::getSDKVersion();
}
void AdsAdmob::setDebugMode(bool debug)
{
ProtocolAds::setDebugMode(debug);
}
void AdsAdmob::addTestDevice(const char* deviceID)
{
PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
PluginJniMethodInfo t;
if (PluginJniHelper::getMethodInfo(t
, pData->jclassName.c_str()
, "addTestDevice"
, "(Ljava/lang/String;)V"))
{
jstring strDeviceID = t.env->NewStringUTF(deviceID);
t.env->CallVoidMethod(pData->jobj, t.methodID, strDeviceID);
t.env->DeleteLocalRef(strDeviceID);
t.env->DeleteLocalRef(t.classID);
}
}
}} // namespace cocos2d { namespace plugin {

View File

@ -30,16 +30,4 @@
<nature>com.android.ide.eclipse.adt.AndroidNature</nature> <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
<linkedResources>
<link>
<name>android</name>
<type>2</type>
<locationURI>PARENT-1-PROJECT_LOC/platform/android</locationURI>
</link>
<link>
<name>include</name>
<type>2</type>
<locationURI>PARENT-1-PROJECT_LOC/include</locationURI>
</link>
</linkedResources>
</projectDescription> </projectDescription>

View File

@ -1,20 +0,0 @@
# set params
PLUGIN_ANDROID_ROOT=$(cd "$(dirname "$0")"; pwd)
if [ ! "${PLUGIN_ROOT}" ]; then
PLUGIN_ROOT="$PLUGIN_ANDROID_ROOT"/../..
fi
# build
"$ANDROID_NDK_ROOT"/ndk-build -C "$PLUGIN_ANDROID_ROOT" \
NDK_MODULE_PATH="$PLUGIN_ROOT"
echo
if [ "0" != "$?" ]; then
echo "Build error occoured!!!"
exit 1
fi
echo
echo "Native build action success."
exit 0

View File

@ -1,29 +0,0 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := PluginAdmobStatic
LOCAL_MODULE_FILENAME := libPluginAdmobStatic
LOCAL_SRC_FILES := \
$(addprefix ../../platform/android/, \
AdsAdmob.cpp \
) \
LOCAL_CFLAGS :=
LOCAL_EXPORT_CFLAGS :=
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../include
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include
LOCAL_WHOLE_STATIC_LIBRARIES := PluginProtocolStatic
LOCAL_LDLIBS := -landroid
LOCAL_LDLIBS += -llog
include $(BUILD_STATIC_LIBRARY)
$(call import-module, protocols/proj.android/jni)

View File

@ -1,7 +0,0 @@
# it is needed for ndk-r5
APP_STL := gnustl_static
APP_CPPFLAGS += -frtti
APP_MODULES := PluginAdmobStatic
APP_ABI :=armeabi
#APP_ABI :=x86
#APP_ABI :=mips mips-r2 mips-r2-sf armeabi

View File

@ -28,8 +28,6 @@ import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import org.cocos2dx.plugin.InterfaceAds.AdsAdapter;
import com.google.ads.*; import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode; import com.google.ads.AdRequest.ErrorCode;
@ -38,7 +36,7 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import android.view.WindowManager; import android.view.WindowManager;
public class AdsAdmob implements AdsAdapter { public class AdsAdmob implements InterfaceAds {
private static final String LOG_TAG = "AdsAdmob"; private static final String LOG_TAG = "AdsAdmob";
private static Activity mContext = null; private static Activity mContext = null;
@ -94,10 +92,10 @@ public class AdsAdmob implements AdsAdapter {
@Override @Override
public void showAds(int adsType, int sizeEnum, int pos) { public void showAds(int adsType, int sizeEnum, int pos) {
switch (adsType) { switch (adsType) {
case InterfaceAds.ADS_TYPE_BANNER: case AdsWrapper.ADS_TYPE_BANNER:
showBannerAd(sizeEnum, pos); showBannerAd(sizeEnum, pos);
break; break;
case InterfaceAds.ADS_TYPE_FULL_SCREEN: case AdsWrapper.ADS_TYPE_FULL_SCREEN:
LogD("Now not support full screen view in Admob"); LogD("Now not support full screen view in Admob");
break; break;
default: default:
@ -113,10 +111,10 @@ public class AdsAdmob implements AdsAdapter {
@Override @Override
public void hideAds(int adsType) { public void hideAds(int adsType) {
switch (adsType) { switch (adsType) {
case InterfaceAds.ADS_TYPE_BANNER: case AdsWrapper.ADS_TYPE_BANNER:
hideBannerAd(); hideBannerAd();
break; break;
case InterfaceAds.ADS_TYPE_FULL_SCREEN: case AdsWrapper.ADS_TYPE_FULL_SCREEN:
break; break;
default: default:
break; break;
@ -178,7 +176,7 @@ public class AdsAdmob implements AdsAdapter {
if (null == mWm) { if (null == mWm) {
mWm = (WindowManager) mContext.getSystemService("window"); mWm = (WindowManager) mContext.getSystemService("window");
} }
InterfaceAds.addAdView(mWm, adView, curPos); AdsWrapper.addAdView(mWm, adView, curPos);
} }
}); });
} }
@ -211,20 +209,20 @@ public class AdsAdmob implements AdsAdapter {
@Override @Override
public void onDismissScreen(Ad arg0) { public void onDismissScreen(Ad arg0) {
LogD("onDismissScreen invoked"); LogD("onDismissScreen invoked");
InterfaceAds.onAdsResult(mAdapter, InterfaceAds.RESULT_CODE_FullScreenViewDismissed, "Full screen ads view dismissed!"); AdsWrapper.onAdsResult(mAdapter, AdsWrapper.RESULT_CODE_FullScreenViewDismissed, "Full screen ads view dismissed!");
} }
@Override @Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) { public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
int errorNo = InterfaceAds.RESULT_CODE_UnknownError; int errorNo = AdsWrapper.RESULT_CODE_UnknownError;
String errorMsg = "Unknow error"; String errorMsg = "Unknow error";
switch (arg1) { switch (arg1) {
case NETWORK_ERROR: case NETWORK_ERROR:
errorNo = InterfaceAds.RESULT_CODE_NetworkError; errorNo = AdsWrapper.RESULT_CODE_NetworkError;
errorMsg = "Network error"; errorMsg = "Network error";
break; break;
case INVALID_REQUEST: case INVALID_REQUEST:
errorNo = InterfaceAds.RESULT_CODE_NetworkError; errorNo = AdsWrapper.RESULT_CODE_NetworkError;
errorMsg = "The ad request is invalid"; errorMsg = "The ad request is invalid";
break; break;
case NO_FILL: case NO_FILL:
@ -234,7 +232,7 @@ public class AdsAdmob implements AdsAdapter {
break; break;
} }
LogD("failed to receive ad : " + errorNo + " , " + errorMsg); LogD("failed to receive ad : " + errorNo + " , " + errorMsg);
InterfaceAds.onAdsResult(mAdapter, errorNo, errorMsg); AdsWrapper.onAdsResult(mAdapter, errorNo, errorMsg);
} }
@Override @Override
@ -245,13 +243,18 @@ public class AdsAdmob implements AdsAdapter {
@Override @Override
public void onPresentScreen(Ad arg0) { public void onPresentScreen(Ad arg0) {
LogD("onPresentScreen invoked"); LogD("onPresentScreen invoked");
InterfaceAds.onAdsResult(mAdapter, InterfaceAds.RESULT_CODE_FullScreenViewShown, "Full screen ads view shown!"); AdsWrapper.onAdsResult(mAdapter, AdsWrapper.RESULT_CODE_FullScreenViewShown, "Full screen ads view shown!");
} }
@Override @Override
public void onReceiveAd(Ad arg0) { public void onReceiveAd(Ad arg0) {
LogD("onReceiveAd invoked"); LogD("onReceiveAd invoked");
InterfaceAds.onAdsResult(mAdapter, InterfaceAds.RESULT_CODE_AdsReceived, "Ads request received success!"); AdsWrapper.onAdsResult(mAdapter, AdsWrapper.RESULT_CODE_AdsReceived, "Ads request received success!");
} }
} }
@Override
public String getPluginVersion() {
return "0.2.0";
}
} }

View File

@ -71,7 +71,7 @@ bool HelloWorld::init()
return false; return false;
} }
m_pAdmob = dynamic_cast<AdsAdmob*>(PluginManager::getInstance()->loadPlugin("AdsAdmob")); m_pAdmob = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdsAdmob"));
TAdsDeveloperInfo devInfo; TAdsDeveloperInfo devInfo;
devInfo["AdmobID"] = "a1516fb6b16b12f"; devInfo["AdmobID"] = "a1516fb6b16b12f";
m_pAdmob->configDeveloperInfo(devInfo); m_pAdmob->configDeveloperInfo(devInfo);
@ -168,7 +168,7 @@ void HelloWorld::testShow(CCObject* pSender)
int nSize = 0; int nSize = 0;
if (m_pAds == m_pAdmob) if (m_pAds == m_pAdmob)
{ {
nSize = AdsAdmob::kSizeBanner; nSize = 0;
} }
if (m_pAds) if (m_pAds)

View File

@ -25,7 +25,7 @@ THE SOFTWARE.
#define __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__
#include "cocos2d.h" #include "cocos2d.h"
#include "AdsAdmob.h" #include "ProtocolAds.h"
class MyAdsListener : public cocos2d::plugin::AdsListener class MyAdsListener : public cocos2d::plugin::AdsListener
{ {
@ -57,7 +57,7 @@ public:
CREATE_FUNC(HelloWorld); CREATE_FUNC(HelloWorld);
private: private:
cocos2d::plugin::AdsAdmob* m_pAdmob; cocos2d::plugin::ProtocolAds* m_pAdmob;
MyAdsListener* m_pListener; MyAdsListener* m_pListener;
cocos2d::CCMenuItemToggle* m_pCaseItem; cocos2d::CCMenuItemToggle* m_pCaseItem;

View File

@ -13,10 +13,9 @@ LOCAL_SRC_FILES := hellocpp/main.cpp \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static \ LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static \
PluginProtocolStatic PluginAdmobStatic PluginProtocolStatic
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)
$(call import-module,cocos2dx) $(call import-module,cocos2dx)
$(call import-module,protocols/android) $(call import-module,protocols/android)
$(call import-module,plugins/admob/android)