Forked from prasannaboppe/AndroidInterfaceManager.cpp
Last active
June 19, 2017 16:13
-
-
Save SalaSuresh/61a9a11a49f20d5610c5a8ba10182f77 to your computer and use it in GitHub Desktop.
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
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) | |
#include <jni.h> | |
#include "AndroidInterfaceManager.h" | |
using namespace std; | |
AndroidInterfaceManager::AndroidInterfaceManager() { | |
jniEnv = JniHelper::getEnv(); | |
activity = JniHelper::getActivity(); | |
clazz = jniEnv->GetObjectClass(activity); | |
} | |
std::string AndroidInterfaceManager::sample() { | |
jstring jstr = jniEnv->NewStringUTF("This comes from jni."); | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "callFromJNI", | |
"(Ljava/lang/String;)Ljava/lang/String;"); | |
jstring jstrRet = (jstring) jniEnv->CallObjectMethod(activity, methodID, jstr); | |
return JniHelper::jstring2string(jstrRet); | |
} | |
void AndroidInterfaceManager::OpenUrl(std::string url) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "openUrl", "(Ljava/lang/String;)V"); | |
jstring jurl = jniEnv->NewStringUTF(url.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, jurl); | |
} | |
void AndroidInterfaceManager::OpenInStore() { | |
CallVoidMethod((char *) "OpenInStore"); | |
} | |
void AndroidInterfaceManager::ShowNotification(int id, std::string text) { | |
ShowNotification(id, 0, text); | |
} | |
void AndroidInterfaceManager::ShowNotification(int id, long futureTime, std::string text) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showNotification", "(IJLjava/lang/String;)V"); | |
jstring jtext = jniEnv->NewStringUTF(text.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, id, futureTime, jtext); | |
} | |
void AndroidInterfaceManager::PlayServicesLogIn() { | |
CallVoidMethod((char *) "playServicesLogIn"); | |
} | |
void AndroidInterfaceManager::PlayServicesLogOut() { | |
CallVoidMethod((char *) "playServicesLogOut"); | |
} | |
/* | |
* pos - for TOP 0 | |
* pos - for BOTTOM 1 | |
* */ | |
void AndroidInterfaceManager::ShowBanner(int pos) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showBanner", "(I)V"); | |
jniEnv->CallVoidMethod(activity, methodID, pos); | |
} | |
void AndroidInterfaceManager::HideBanner() { | |
CallVoidMethod((char *) "hideBanner"); | |
} | |
/* | |
* type - ICON 1 | |
* type - INFEED 2 | |
* type - CONTENT_STREAM 3 | |
* */ | |
void AndroidInterfaceManager::ShowNativeAds(int type, int width, int height, int x, int y) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showNativeAds", "(IIIII)V"); | |
jniEnv->CallVoidMethod(activity, methodID, type, width, height, x, y); | |
} | |
void AndroidInterfaceManager::HideNativeAds() { | |
CallVoidMethod((char *) "hideNativeAds"); | |
} | |
void AndroidInterfaceManager::ShareScreenShot(int type, std::string path) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "shareScreenShot", "(ILjava/lang/String;)V"); | |
jstring jpath = jniEnv->NewStringUTF(path.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, type, jpath); | |
} | |
void AndroidInterfaceManager::ShowToastMessage(std::string msg) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showToastMessage", "(Ljava/lang/String;)V"); | |
jstring jmsg = jniEnv->NewStringUTF(msg.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, jmsg); | |
} | |
void AndroidInterfaceManager::ShowAchievements() { | |
CallVoidMethod((char *) "showAchievements"); | |
} | |
void AndroidInterfaceManager::ShowLeaderboard(int id) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showLeaderboard", "(I)V"); | |
jniEnv->CallVoidMethod(activity, methodID, id); | |
} | |
void AndroidInterfaceManager::ShowLeaderboard() { | |
ShowLeaderboard(0); | |
} | |
void AndroidInterfaceManager::SubmitScore(int id, int score) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "submitScore", "(II)V"); | |
jniEnv->CallVoidMethod(activity, methodID, id, score); | |
} | |
void AndroidInterfaceManager::UnlockAchievement(int id) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "unlockAchievement", "(I)V"); | |
jniEnv->CallVoidMethod(activity, methodID, id); | |
} | |
void AndroidInterfaceManager::UnlockAchievement(int id, int progress) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "unlockAchievement", "(II)V"); | |
jniEnv->CallVoidMethod(activity, methodID, id, progress); | |
} | |
void AndroidInterfaceManager::ShowInterstitial(int type) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showInterstitial", "(I)V"); | |
jniEnv->CallVoidMethod(activity, methodID, type); | |
} | |
void AndroidInterfaceManager::ShowVideoAds(int type) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "showVideoAds", "(I)V"); | |
jniEnv->CallVoidMethod(activity, methodID, type); | |
} | |
void AndroidInterfaceManager::ExitGame() { | |
CallVoidMethod((char *) "exitGame"); | |
} | |
void AndroidInterfaceManager::OnRemoveAds() { | |
CallVoidMethod((char *) "onRemoveAds"); | |
} | |
void AndroidInterfaceManager::OnInAppMiniRoll() { | |
CallVoidMethod((char *) "onInAppMiniRoll"); | |
} | |
void AndroidInterfaceManager::OnInAppCoins(int coins) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "onInAppCoins", "(I)V"); | |
jniEnv->CallVoidMethod(activity, methodID, coins); | |
} | |
std::string AndroidInterfaceManager::GetPrice(int coins) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "getPrice", "(I)Ljava/lang/String;"); | |
jstring jstrRet = (jstring) jniEnv->CallObjectMethod(activity, methodID, coins); | |
return JniHelper::jstring2string(jstrRet); | |
} | |
std::string AndroidInterfaceManager::GetProfileName() { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "getProfileName", "()Ljava/lang/String;"); | |
jstring jstrRet = (jstring) jniEnv->CallObjectMethod(activity, methodID); | |
return JniHelper::jstring2string(jstrRet); | |
} | |
std::string AndroidInterfaceManager::GetVersion() { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "getVersion", "()Ljava/lang/String;"); | |
jstring jstrRet = (jstring) jniEnv->CallObjectMethod(activity, methodID); | |
return JniHelper::jstring2string(jstrRet); | |
} | |
bool AndroidInterfaceManager::IsPlayServicesLoggedIn() { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "isPlayServicesLoggedIn", "()Z"); | |
return jniEnv->CallBooleanMethod(activity, methodID); | |
} | |
bool AndroidInterfaceManager::IsVideoAdsAvailable() { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "isVideoAdsAvailable", "()Z"); | |
return jniEnv->CallBooleanMethod(activity, methodID); | |
} | |
bool AndroidInterfaceManager::IsNetworkAvailable() { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "isNetworkAvailable", "()Z"); | |
return jniEnv->CallBooleanMethod(activity, methodID); | |
} | |
long AndroidInterfaceManager::GetDurationForAds() { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "getDurationForAds", "()J"); | |
return (long) jniEnv->CallLongMethod(activity, methodID); | |
} | |
void AndroidInterfaceManager::PostAnalyticsEvents(std::string category, std::string action, | |
std::string label, int value) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "logEvent", | |
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V"); | |
jstring jcategory = jniEnv->NewStringUTF(category.c_str()); | |
jstring jaction = jniEnv->NewStringUTF(action.c_str()); | |
jstring jlabel = jniEnv->NewStringUTF(label.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, jcategory, jaction, jlabel, value); | |
} | |
void AndroidInterfaceManager::PostAnalyticsScreen(std::string name) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "logScreen", "(Ljava/lang/String;)V"); | |
jstring jname = jniEnv->NewStringUTF(name.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, jname); | |
} | |
void AndroidInterfaceManager::SetUserProperty(std::string key, std::string value) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, "setUserProperty", | |
"(Ljava/lang/String;Ljava/lang/String;)V"); | |
jstring jkey = jniEnv->NewStringUTF(key.c_str()); | |
jstring jvalue = jniEnv->NewStringUTF(value.c_str()); | |
jniEnv->CallVoidMethod(activity, methodID, jkey, jvalue); | |
} | |
void AndroidInterfaceManager::CallVoidMethod(char *method) { | |
jmethodID methodID = jniEnv->GetMethodID(clazz, method, "()V"); | |
jniEnv->CallVoidMethod(activity, methodID); | |
} | |
AndroidInterfaceManager::~AndroidInterfaceManager() { | |
} | |
extern "C" { | |
void | |
Java_org_cocos2dx_cpp_AppActivity_onInAppPurchaseSuccess(JNIEnv *env, jobject obj, jstring id) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onInAppPurchaseFailed(JNIEnv *env, jobject obj, jstring id) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onPlayServicesLogInSuccess(JNIEnv *env, jobject obj) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onPlayServicesLogInFailed(JNIEnv *env, jobject obj) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onPlayServicesLogOutSuccess(JNIEnv *env, jobject obj) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onPlayServicesLogOutFailed(JNIEnv *env, jobject obj) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onVideoAdsSuccess(JNIEnv *env, jobject obj) { | |
} | |
void Java_org_cocos2dx_cpp_AppActivity_onVideoAdsFailed(JNIEnv *env, jobject obj) { | |
} | |
jstring Java_org_cocos2dx_cpp_AppActivity_getSelectedLanguage(JNIEnv *env, jobject obj) { | |
return env->NewStringUTF("English"); | |
} | |
bool Java_org_cocos2dx_cpp_AppActivity_isNotificationEnabled(JNIEnv *env, jobject obj) { | |
return true; | |
} | |
} | |
#endif |
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
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) | |
#include <iostream> | |
#include "cocos2d.h" | |
using namespace cocos2d; | |
class AndroidInterfaceManager { | |
JNIEnv *jniEnv; | |
jobject activity; | |
jclass clazz; | |
public: | |
AndroidInterfaceManager(); | |
~AndroidInterfaceManager(); | |
std::string sample(); | |
void OpenInStore(); | |
void OpenUrl(std::string url); | |
void ShowNotification(int id, long futureTime, std::string text); | |
void ShowNotification(int id, std::string text); | |
void PlayServicesLogOut(); | |
void PlayServicesLogIn(); | |
void CallVoidMethod(char *method); | |
void ShowBanner(int pos); | |
void HideBanner(); | |
void ShowNativeAds(int type, int width, int height, int x, int y); | |
void HideNativeAds(); | |
void ShareScreenShot(int type, std::string path); | |
void ShowToastMessage(std::string msg); | |
void ShowAchievements(); | |
void ShowLeaderboard(int id); | |
void ShowLeaderboard(); | |
void SubmitScore(int id, int score); | |
void ShowInterstitial(int type); | |
void UnlockAchievement(int id); | |
void UnlockAchievement(int id, int progress); | |
void ExitGame(); | |
void OnRemoveAds(); | |
void OnInAppMiniRoll(); | |
void OnInAppCoins(int coins); | |
std::string GetPrice(int coins); | |
std::string GetProfileName(); | |
std::string GetVersion(); | |
bool IsPlayServicesLoggedIn(); | |
bool IsVideoAdsAvailable(); | |
bool IsNetworkAvailable(); | |
long GetDurationForAds(); | |
void PostAnalyticsEvents(std::string category, std::string action, std::string label, int value); | |
void PostAnalyticsScreen(std::string name); | |
void SetUserProperty(std::string key, std::string value); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment