From bfe6513706a353fcd9fd3e3e673feebdc23f0496 Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 18 Aug 2014 16:41:12 +0400 Subject: [PATCH] setKeepScreenOn method --- cocos/platform/CCDevice.h | 2 ++ cocos/platform/android/CCDevice.cpp | 6 ++++++ .../java/src/org/cocos2dx/lib/Cocos2dxActivity.java | 11 +++++++++++ .../java/src/org/cocos2dx/lib/Cocos2dxHelper.java | 4 ++++ .../jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp | 10 ++++++++++ .../jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h | 1 + cocos/platform/ios/CCDevice.mm | 5 +++++ cocos/platform/linux/CCDevice.cpp | 4 ++++ cocos/platform/mac/CCDevice.mm | 5 +++++ cocos/platform/win32/CCDevice.cpp | 4 ++++ cocos/platform/winrt/CCDevice.cpp | 4 ++++ 11 files changed, 56 insertions(+) diff --git a/cocos/platform/CCDevice.h b/cocos/platform/CCDevice.h index 1affa43e7b..91e6485559 100644 --- a/cocos/platform/CCDevice.h +++ b/cocos/platform/CCDevice.h @@ -65,6 +65,8 @@ public: static void setAccelerometerInterval(float interval); static Data getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha); + + static void setKeepScreenOn(bool value); private: CC_DISALLOW_IMPLICIT_CONSTRUCTORS(Device); diff --git a/cocos/platform/android/CCDevice.cpp b/cocos/platform/android/CCDevice.cpp index fc5a0902f8..3ccb441d19 100644 --- a/cocos/platform/android/CCDevice.cpp +++ b/cocos/platform/android/CCDevice.cpp @@ -202,6 +202,12 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text return ret; } + +void Device::setKeepScreenOn(bool value) +{ + setKeepScreenOnJni(value); +} + NS_CC_END #endif // CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 78929fb300..9ae61cc165 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -34,6 +34,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Message; import android.view.ViewGroup; +import android.view.WindowManager; import android.util.Log; import android.widget.FrameLayout; import android.preference.PreferenceManager.OnActivityResultListener; @@ -58,6 +59,16 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe return sContext; } + public void setKeepScreenOn(boolean value) { + final boolean newValue = value; + runOnUiThread(new Runnable() { + @Override + public void run() { + mGLSurfaceView.setKeepScreenOn(newValue); + } + }); + } + protected void onLoadNativeLibraries() { try { ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); 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 3cabec4822..ea102e7f73 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -167,6 +167,10 @@ public class Cocos2dxHelper { Cocos2dxHelper.sAccelerometerEnabled = false; Cocos2dxHelper.sCocos2dxAccelerometer.disable(); } + + public static void setKeepScreenOn(boolean value) { + ((Cocos2dxActivity)sActivity).setKeepScreenOn(value); + } 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 3449bbbc9d..ef9fac20a4 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -209,6 +209,16 @@ void disableAccelerometerJni() { } } +void setKeepScreenOnJni(bool value) { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setKeepScreenOn", "(Z)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, value); + + t.env->DeleteLocalRef(t.classID); + } +} + // 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 34d521a911..7044a4e5e4 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -39,6 +39,7 @@ extern std::string getFileDirectoryJNI(); extern void enableAccelerometerJni(); extern void disableAccelerometerJni(); extern void setAccelerometerIntervalJni(float interval); +extern void setKeepScreenOnJni(bool value); // 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/CCDevice.mm b/cocos/platform/ios/CCDevice.mm index 607d95340b..28438ea897 100644 --- a/cocos/platform/ios/CCDevice.mm +++ b/cocos/platform/ios/CCDevice.mm @@ -477,6 +477,11 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text return ret; } +void Device::setKeepScreenOn(bool value) +{ + [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; +} + NS_CC_END #endif // CC_PLATFORM_IOS diff --git a/cocos/platform/linux/CCDevice.cpp b/cocos/platform/linux/CCDevice.cpp index 5536c87d81..24b70d05b2 100644 --- a/cocos/platform/linux/CCDevice.cpp +++ b/cocos/platform/linux/CCDevice.cpp @@ -499,6 +499,10 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text return ret; } +void Device::setKeepScreenOn(bool value) +{ +} + NS_CC_END #endif // CC_TARGET_PLATFORM == CC_PLATFORM_LINUX diff --git a/cocos/platform/mac/CCDevice.mm b/cocos/platform/mac/CCDevice.mm index 185dded81d..885a643bc6 100644 --- a/cocos/platform/mac/CCDevice.mm +++ b/cocos/platform/mac/CCDevice.mm @@ -247,6 +247,11 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text return ret; } + +void Device::setKeepScreenOn(bool value) +{ +} + NS_CC_END #endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC diff --git a/cocos/platform/win32/CCDevice.cpp b/cocos/platform/win32/CCDevice.cpp index f6bce3bac5..ed69905ab1 100644 --- a/cocos/platform/win32/CCDevice.cpp +++ b/cocos/platform/win32/CCDevice.cpp @@ -451,6 +451,10 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text return ret; } +void Device::setKeepScreenOn(bool value) +{ +} + NS_CC_END #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 diff --git a/cocos/platform/winrt/CCDevice.cpp b/cocos/platform/winrt/CCDevice.cpp index edf747764a..eedd87cd87 100644 --- a/cocos/platform/winrt/CCDevice.cpp +++ b/cocos/platform/winrt/CCDevice.cpp @@ -163,6 +163,10 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text return ret; } +void Device::setKeepScreenOn(bool value) +{ +} + NS_CC_END #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)