From 04fa7a8902ef039a357c07cf90581aa5da898154 Mon Sep 17 00:00:00 2001 From: Filipe Lemos Date: Wed, 28 Oct 2015 17:57:57 +0000 Subject: [PATCH] Added Application::getVersion() to get the app version. --- cocos/platform/CCApplicationProtocol.h | 7 +++++++ .../android/CCApplication-android.cpp | 5 +++++ .../platform/android/CCApplication-android.h | 5 +++++ .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 9 +++++++++ .../Java_org_cocos2dx_lib_Cocos2dxHelper.cpp | 13 ++++++++++++ .../Java_org_cocos2dx_lib_Cocos2dxHelper.h | 1 + cocos/platform/ios/CCApplication-ios.h | 5 +++++ cocos/platform/ios/CCApplication-ios.mm | 8 ++++++++ cocos/platform/linux/CCApplication-linux.cpp | 5 +++++ cocos/platform/linux/CCApplication-linux.h | 5 +++++ cocos/platform/mac/CCApplication-mac.h | 5 +++++ cocos/platform/mac/CCApplication-mac.mm | 8 ++++++++ cocos/platform/win32/CCApplication-win32.cpp | 5 +++++ cocos/platform/win32/CCApplication-win32.h | 5 +++++ cocos/platform/winrt/CCApplication.cpp | 20 +++++++++++++++++++ cocos/platform/winrt/CCApplication.h | 5 +++++ 16 files changed, 111 insertions(+) diff --git a/cocos/platform/CCApplicationProtocol.h b/cocos/platform/CCApplicationProtocol.h index 2b78c0b92f..9ac60baa2b 100644 --- a/cocos/platform/CCApplicationProtocol.h +++ b/cocos/platform/CCApplicationProtocol.h @@ -136,6 +136,13 @@ public: */ virtual Platform getTargetPlatform() = 0; + /** + @brief Get application version. + * @js NA + * @lua NA + */ + virtual std::string getVersion() = 0; + /** @brief Open url in default browser. @param String with url to open. diff --git a/cocos/platform/android/CCApplication-android.cpp b/cocos/platform/android/CCApplication-android.cpp index 8d802a1814..b3f0d18bef 100644 --- a/cocos/platform/android/CCApplication-android.cpp +++ b/cocos/platform/android/CCApplication-android.cpp @@ -200,6 +200,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_ANDROID; } +std::string Application::getVersion() +{ + return getVersionJNI(); +} + bool Application::openURL(const std::string &url) { return openURLJNI(url.c_str()); diff --git a/cocos/platform/android/CCApplication-android.h b/cocos/platform/android/CCApplication-android.h index d1f8c47c48..3783868235 100644 --- a/cocos/platform/android/CCApplication-android.h +++ b/cocos/platform/android/CCApplication-android.h @@ -84,6 +84,11 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief Get application version. + */ + virtual std::string getVersion(); + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 8bd35f4980..bee8144410 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -212,6 +212,15 @@ public class Cocos2dxHelper { sVibrateService.vibrate((long)(duration * 1000)); } + public static String getVersion() { + try { + String version = Cocos2dxActivity.getContext().getPackageManager().getPackageInfo(Cocos2dxActivity.getContext().getPackageName(), 0).versionName; + return version; + } catch(Exception e) { + return "-"; + } + } + public static boolean openURL(String url) { boolean ret = false; try { diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp index cbd1285e05..4a6b0a39e4 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -205,6 +205,19 @@ void vibrateJni(float duration) { } } +std::string getVersionJNI() { + JniMethodInfo t; + std::string ret(""); + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getVersion", "()Ljava/lang/String;")) { + jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); + t.env->DeleteLocalRef(t.classID); + ret = JniHelper::jstring2string(str); + t.env->DeleteLocalRef(str); + } + return ret; +} + extern bool openURLJNI(const char* url) { JniMethodInfo t; diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h index 534a35669c..c9f50994cc 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -40,6 +40,7 @@ extern void disableAccelerometerJni(); extern void setAccelerometerIntervalJni(float interval); extern void setKeepScreenOnJni(bool value); extern void vibrateJni(float duration); +extern std::string getVersionJNI(); extern bool openURLJNI(const char* url); // functions for UserDefault extern bool getBoolForKeyJNI(const char* key, bool defaultValue); diff --git a/cocos/platform/ios/CCApplication-ios.h b/cocos/platform/ios/CCApplication-ios.h index ae93c83d4d..63b3884369 100644 --- a/cocos/platform/ios/CCApplication-ios.h +++ b/cocos/platform/ios/CCApplication-ios.h @@ -86,6 +86,11 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief Get application version. + */ + virtual std::string getVersion(); + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/ios/CCApplication-ios.mm b/cocos/platform/ios/CCApplication-ios.mm index 7411478060..69bc93c895 100644 --- a/cocos/platform/ios/CCApplication-ios.mm +++ b/cocos/platform/ios/CCApplication-ios.mm @@ -139,6 +139,14 @@ Application::Platform Application::getTargetPlatform() } } +std::string Application::getVersion() { + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; + if (version) { + return [version UTF8String]; + } + return ""; +} + bool Application::openURL(const std::string &url) { NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding]; diff --git a/cocos/platform/linux/CCApplication-linux.cpp b/cocos/platform/linux/CCApplication-linux.cpp index 12f1e89228..8f19a553fb 100644 --- a/cocos/platform/linux/CCApplication-linux.cpp +++ b/cocos/platform/linux/CCApplication-linux.cpp @@ -136,6 +136,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_LINUX; } +std::string Application::getVersion() +{ + return ""; +} + bool Application::openURL(const std::string &url) { std::string op = std::string("open ").append(url); diff --git a/cocos/platform/linux/CCApplication-linux.h b/cocos/platform/linux/CCApplication-linux.h index c827023af1..7342269028 100644 --- a/cocos/platform/linux/CCApplication-linux.h +++ b/cocos/platform/linux/CCApplication-linux.h @@ -78,6 +78,11 @@ public: */ virtual const char * getCurrentLanguageCode(); + /** + @brief Get application version + */ + virtual std::string getVersion(); + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/mac/CCApplication-mac.h b/cocos/platform/mac/CCApplication-mac.h index 2a243a7624..bf20f5c1e6 100644 --- a/cocos/platform/mac/CCApplication-mac.h +++ b/cocos/platform/mac/CCApplication-mac.h @@ -91,6 +91,11 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief Get application version. + */ + virtual std::string getVersion(); + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/mac/CCApplication-mac.mm b/cocos/platform/mac/CCApplication-mac.mm index 1666776a5c..47c55a8b07 100644 --- a/cocos/platform/mac/CCApplication-mac.mm +++ b/cocos/platform/mac/CCApplication-mac.mm @@ -118,6 +118,14 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_MAC; } +std::string Application::getVersion() { + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; + if (version) { + return [version UTF8String]; + } + return ""; +} + ///////////////////////////////////////////////////////////////////////////////////////////////// // static member function ////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/cocos/platform/win32/CCApplication-win32.cpp b/cocos/platform/win32/CCApplication-win32.cpp index 9ee4b56f3f..1c752094ef 100644 --- a/cocos/platform/win32/CCApplication-win32.cpp +++ b/cocos/platform/win32/CCApplication-win32.cpp @@ -217,6 +217,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_WINDOWS; } +std::string Application::getVersion() +{ + return ""; +} + bool Application::openURL(const std::string &url) { WCHAR *temp = new WCHAR[url.size() + 1]; diff --git a/cocos/platform/win32/CCApplication-win32.h b/cocos/platform/win32/CCApplication-win32.h index 71013eca9e..455e6d5b89 100644 --- a/cocos/platform/win32/CCApplication-win32.h +++ b/cocos/platform/win32/CCApplication-win32.h @@ -75,6 +75,11 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief Get application version + */ + virtual std::string getVersion(); + /** @brief Open url in default browser @param String with url to open. diff --git a/cocos/platform/winrt/CCApplication.cpp b/cocos/platform/winrt/CCApplication.cpp index 374445a554..65a583994a 100644 --- a/cocos/platform/winrt/CCApplication.cpp +++ b/cocos/platform/winrt/CCApplication.cpp @@ -36,6 +36,7 @@ using namespace Windows::Foundation; #include "platform/CCFileUtils.h" #include "CCWinRTUtils.h" #include "platform/CCApplication.h" +#include "tinyxml2/tinyxml2.h" /** @brief This function change the PVRFrame show/hide setting in register. @@ -225,6 +226,25 @@ Application::Platform Application::getTargetPlatform() } } +std::string Application::getVersion() +{ + std::string r(""); + std::string s = FileUtils::getInstance()->getStringFromFile("WMAppManifest.xml"); + if (!s.empty()) { + tinyxml2::XMLDocument doc; + if (!doc.Parse(s.c_str())) { + tinyxml2::XMLElement *app = doc.RootElement()->FirstChildElement("App"); + if (app) { + const char* version = app->Attribute("Version"); + if (version) { + r = version; + } + } + } + } + return r; +} + bool Application::openURL(const std::string &url) { #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT diff --git a/cocos/platform/winrt/CCApplication.h b/cocos/platform/winrt/CCApplication.h index 453aa196fa..f2e9856ecb 100644 --- a/cocos/platform/winrt/CCApplication.h +++ b/cocos/platform/winrt/CCApplication.h @@ -63,6 +63,11 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform() override; + + /** + @brief Get application version + */ + virtual std::string getVersion() override; /** @brief Open url in default browser