diff --git a/cocos/platform/CCApplicationProtocol.h b/cocos/platform/CCApplicationProtocol.h index 6d9697fdb6..9993973d0c 100644 --- a/cocos/platform/CCApplicationProtocol.h +++ b/cocos/platform/CCApplicationProtocol.h @@ -133,6 +133,15 @@ public: * @lua NA */ virtual Platform getTargetPlatform() = 0; + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + * @js NA + * @lua NA + */ + virtual bool openURL(const std::string &url) = 0; }; // end of platform group diff --git a/cocos/platform/android/CCApplication-android.cpp b/cocos/platform/android/CCApplication-android.cpp index e542fcf0b5..f91c22cbac 100644 --- a/cocos/platform/android/CCApplication-android.cpp +++ b/cocos/platform/android/CCApplication-android.cpp @@ -176,6 +176,11 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_ANDROID; } +bool Application::openURL(const std::string &url) +{ + return openURLJNI(url.c_str()); +} + void Application::applicationScreenSizeChanged(int newWidth, int newHeight) { } diff --git a/cocos/platform/android/CCApplication-android.h b/cocos/platform/android/CCApplication-android.h index e0a3975824..9b11b87a3e 100644 --- a/cocos/platform/android/CCApplication-android.h +++ b/cocos/platform/android/CCApplication-android.h @@ -83,6 +83,13 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform(); + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + */ + virtual bool openURL(const std::string &url); /** @brief This function will be called when the application screen size is changed. 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 ebbc5bdc75..2c6168ac59 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -32,9 +32,11 @@ import java.lang.Runnable; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.res.AssetManager; +import android.net.Uri; import android.os.Build; import android.preference.PreferenceManager.OnActivityResultListener; import android.util.DisplayMetrics; @@ -170,6 +172,18 @@ public class Cocos2dxHelper { public static void setKeepScreenOn(boolean value) { ((Cocos2dxActivity)sActivity).setKeepScreenOn(value); } + + public static boolean openURL(String url) { + boolean ret = false; + try { + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(url)); + sActivity.startActivity(i); + ret = true; + } catch (Exception e) { + } + return ret; + } public static void preloadBackgroundMusic(final String pPath) { Cocos2dxHelper.sCocos2dMusic.preloadBackgroundMusic(pPath); 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 b7b25a937d..677432216c 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -219,6 +219,20 @@ void setKeepScreenOnJni(bool value) { } } +extern bool openURLJNI(const char* url) { + JniMethodInfo t; + + bool ret = false; + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "openURL", "(Ljava/lang/String;)Z")) { + jstring stringArg = t.env->NewStringUTF(url); + ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + } + return ret; +} + // functions for UserDefault bool getBoolForKeyJNI(const char* key, bool defaultValue) { 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 7044a4e5e4..eda006b94f 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 enableAccelerometerJni(); extern void disableAccelerometerJni(); extern void setAccelerometerIntervalJni(float interval); extern void setKeepScreenOnJni(bool value); +extern bool openURLJNI(const char* url); // functions for UserDefault extern bool getBoolForKeyJNI(const char* key, bool defaultValue); extern int getIntegerForKeyJNI(const char* key, int defaultValue); diff --git a/cocos/platform/ios/CCApplication-ios.h b/cocos/platform/ios/CCApplication-ios.h index 88c6a4f486..e88cc667c1 100644 --- a/cocos/platform/ios/CCApplication-ios.h +++ b/cocos/platform/ios/CCApplication-ios.h @@ -85,6 +85,13 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform(); + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + */ + virtual bool openURL(const std::string &url); /** @brief This function will be called when the application screen size is changed. diff --git a/cocos/platform/ios/CCApplication-ios.mm b/cocos/platform/ios/CCApplication-ios.mm index cbacf107e9..d1c69ff83c 100644 --- a/cocos/platform/ios/CCApplication-ios.mm +++ b/cocos/platform/ios/CCApplication-ios.mm @@ -167,6 +167,13 @@ Application::Platform Application::getTargetPlatform() } } +bool Application::openURL(const std::string &url) +{ + NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding]; + NSURL* nsUrl = [NSURL URLWithString:msg]; + return [[UIApplication sharedApplication] openURL:nsUrl]; +} + void Application::applicationScreenSizeChanged(int newWidth, int newHeight) { } diff --git a/cocos/platform/linux/CCApplication-linux.cpp b/cocos/platform/linux/CCApplication-linux.cpp index 65310a9b0c..d3571aef03 100644 --- a/cocos/platform/linux/CCApplication-linux.cpp +++ b/cocos/platform/linux/CCApplication-linux.cpp @@ -136,6 +136,12 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_LINUX; } +bool Application::openURL(const std::string &url) +{ + std::string op = std::string("open ").append(url); + return system(op.c_str())!=-1; +} + ////////////////////////////////////////////////////////////////////////// // static member function ////////////////////////////////////////////////////////////////////////// diff --git a/cocos/platform/linux/CCApplication-linux.h b/cocos/platform/linux/CCApplication-linux.h index 5c7163fc6e..e7334b50c9 100644 --- a/cocos/platform/linux/CCApplication-linux.h +++ b/cocos/platform/linux/CCApplication-linux.h @@ -77,6 +77,13 @@ public: @return Current language iso 639-1 code */ virtual const char * getCurrentLanguageCode(); + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + */ + virtual bool openURL(const std::string &url); /** diff --git a/cocos/platform/mac/CCApplication-mac.h b/cocos/platform/mac/CCApplication-mac.h index 8ff4c7e166..f75c1d08d9 100644 --- a/cocos/platform/mac/CCApplication-mac.h +++ b/cocos/platform/mac/CCApplication-mac.h @@ -90,6 +90,13 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform(); + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + */ + virtual bool openURL(const std::string &url); /** * Sets the Resource root path. diff --git a/cocos/platform/mac/CCApplication-mac.mm b/cocos/platform/mac/CCApplication-mac.mm index ca72c23883..c767948e09 100644 --- a/cocos/platform/mac/CCApplication-mac.mm +++ b/cocos/platform/mac/CCApplication-mac.mm @@ -213,6 +213,13 @@ LanguageType Application::getCurrentLanguage() return ret; } +bool Application::openURL(const std::string &url) +{ + NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding]; + NSURL* nsUrl = [NSURL URLWithString:msg]; + return [[NSWorkspace sharedWorkspace] openURL:nsUrl]; +} + void Application::setResourceRootPath(const std::string& rootResDir) { _resourceRootPath = rootResDir; diff --git a/cocos/platform/win32/CCApplication-win32.cpp b/cocos/platform/win32/CCApplication-win32.cpp index b419c69e33..ab3fb7f150 100644 --- a/cocos/platform/win32/CCApplication-win32.cpp +++ b/cocos/platform/win32/CCApplication-win32.cpp @@ -30,6 +30,7 @@ THE SOFTWARE. #include "base/CCDirector.h" #include #include "platform/CCFileUtils.h" +#include /** @brief This function change the PVRFrame show/hide setting in register. @param bEnable If true show the PVRFrame window, otherwise hide. @@ -206,6 +207,15 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_WINDOWS; } +bool Application::openURL(const std::string &url) +{ + WCHAR *temp = new WCHAR[url.size() + 1]; + int wchars_num = MultiByteToWideChar(CP_UTF8, 0, url.c_str(), url.size() + 1, temp, url.size() + 1); + HINSTANCE r = ShellExecuteW(NULL, L"open", temp, NULL, NULL, SW_SHOWNORMAL); + delete[] temp; + return (int)r>32; +} + void Application::setResourceRootPath(const std::string& rootResDir) { _resourceRootPath = rootResDir; diff --git a/cocos/platform/win32/CCApplication-win32.h b/cocos/platform/win32/CCApplication-win32.h index bd4afd7a3d..c30a697d74 100644 --- a/cocos/platform/win32/CCApplication-win32.h +++ b/cocos/platform/win32/CCApplication-win32.h @@ -74,6 +74,13 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform(); + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + */ + virtual bool openURL(const std::string &url); /** * Sets the Resource root path. diff --git a/cocos/platform/winrt/CCApplication.cpp b/cocos/platform/winrt/CCApplication.cpp index c039b4a8f7..8fcbf1001d 100644 --- a/cocos/platform/winrt/CCApplication.cpp +++ b/cocos/platform/winrt/CCApplication.cpp @@ -177,6 +177,12 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_WP8; } +bool Application::openURL(const std::string &url) +{ + //TODO release this method + return false; +} + void Application::setResourceRootPath(const std::string& rootResDir) { m_resourceRootPath = rootResDir; diff --git a/cocos/platform/winrt/CCApplication.h b/cocos/platform/winrt/CCApplication.h index ac6435527e..2f561a20c0 100644 --- a/cocos/platform/winrt/CCApplication.h +++ b/cocos/platform/winrt/CCApplication.h @@ -62,6 +62,13 @@ public: @brief Get target platform */ virtual Platform getTargetPlatform() override; + + /** + @brief Open url in default browser + @param String with url to open. + @return true if the resource located by the URL was successfully opened; otherwise false. + */ + virtual bool openURL(const std::string &url); /** * Sets the Resource root path.