Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions frameworks/cocos2d-x/cocos/base/CCDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,21 @@ void Director::mainLoop()
}
else if (! _invalid)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// version: 1.55.0
// reason: 为实现拍照功能而添加,JAVA层的拍照功能在子线程中完成,而V8不允许跨线程调用Isolate,无法直接执行回调函数,因此添加此缓存,待下一帧执行。
// 已通过Context.runOnUIThread和主线程创建Handler尝试,无效。
if (_cachedFunctionsNextUpdate.size())
{
for (auto iterator : _cachedFunctionsNextUpdate)
{
std::function<void()> method = iterator;
method();
}
_cachedFunctionsNextUpdate.clear();
}
#endif

drawScene();

// release the objects
Expand All @@ -1459,5 +1474,12 @@ void Director::setAnimationInterval(float interval)
}
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
void Director::runInNextUpdate(std::function<void ()> method)
{
_cachedFunctionsNextUpdate.push_back(method);
}
#endif

NS_CC_END

11 changes: 9 additions & 2 deletions frameworks/cocos2d-x/cocos/base/CCDirector.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ class CC_DLL Director : public Ref

void setCullingEnabled (bool enable) { _isCullingEnabled = enable; }
bool isCullingEnabled () const { return _isCullingEnabled; }


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
void runInNextUpdate(std::function<void()> method);
#endif
protected:
void reset();

Expand Down Expand Up @@ -636,7 +639,11 @@ class CC_DLL Director : public Ref
friend class GLView;

bool _isCullingEnabled;


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// 为实现拍照功能而添加,JAVA层的拍照功能在子线程中完成,而V8不允许跨线程调用Isolate,无法直接执行回调函数,因此添加此缓存,待下一帧执行。
std::list<std::function<void()>> _cachedFunctionsNextUpdate;
#endif
private:
/**
Use std::vector to implement a quick matrix stack.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":libcocos2dx" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../../../../../runtime-src/proj.android-studio" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":libcocos2dx" />
<option name="LAST_SUCCESSFUL_SYNC_AGP_VERSION" value="3.0.0" />
<option name="LAST_KNOWN_AGP_VERSION" value="3.0.0" />
</configuration>
</facet>
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<afterSyncTasks>
<task>generateDebugSources</task>
</afterSyncTasks>
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/build/generated/res/rs/debug;file://$MODULE_DIR$/build/generated/res/resValues/debug" />
<option name="TEST_RES_FOLDERS_RELATIVE_PATH" value="" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
<option name="PROJECT_TYPE" value="1" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output />
<content url="file://$MODULE_DIR$/../java/src">
<sourceFolder url="file://$MODULE_DIR$/../java/src" isTestSource="false" />
</content>
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/debug" type="java-resource" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/debug" type="java-resource" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" type="java-test-resource" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/androidTest/debug" type="java-test-resource" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/test/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Gradle: __local_aars__:/Users/zhujialing/Documents/GitHub/smartpi-cocos-full/build/jsb-default.stable/frameworks/cocos2d-x/cocos/platform/android/java/libs/android-async-http-1.4.9.jar:unspecified@jar" level="project" />
<orderEntry type="library" name="Gradle: __local_aars__:/Users/zhujialing/Documents/GitHub/smartpi-cocos-full/build/jsb-default.stable/frameworks/cocos2d-x/cocos/platform/android/java/libs/libcocosanalytics.jar:unspecified@jar" level="project" />
<orderEntry type="library" name="Gradle: __local_aars__:/Users/zhujialing/Documents/GitHub/smartpi-cocos-full/build/jsb-default.stable/frameworks/cocos2d-x/cocos/platform/android/java/libs/com.android.vending.expansion.zipfile.jar:unspecified@jar" level="project" />
<orderEntry type="library" name="Gradle: __local_aars__:/Users/zhujialing/Documents/GitHub/smartpi-cocos-full/build/jsb-default.stable/frameworks/cocos2d-x/cocos/platform/android/java/libs/httpclient-4.4.1.1.jar:unspecified@jar" level="project" />
</component>
</module>
1 change: 1 addition & 0 deletions frameworks/runtime-src/Classes/sdks/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LOCAL_SRC_FILES := SdkManager.cpp \
Bugly/BuglySdk.cpp \
wechat/WechatSdk.mm \
hash/HashSdk.cpp \
utils/photos/PhotoPicker.cpp \

LOCAL_CPP_EXTENSION := .mm .cpp .cc

Expand Down
29 changes: 20 additions & 9 deletions frameworks/runtime-src/Classes/sdks/utils/UtilsSdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

#include "Sdk.h"

class UtilsSdk : public Sdk {
class UtilsSdk : public Sdk
{
private:
std::map<std::string, SdkCallback> _callbacks;
public:
UtilsSdk() : Sdk("UtilsSdk") {}

Expand All @@ -22,6 +25,7 @@ class UtilsSdk : public Sdk {
// memory
unsigned long long getTotalMemorySize();
unsigned long long getAvailableMemorySize();
unsigned long long getIOSProcessMemoryUsage();

// file system storage
unsigned long long getFileSystemTotalSize();
Expand All @@ -30,17 +34,24 @@ class UtilsSdk : public Sdk {
static std::string getUUID();
static int getSignatureCode();

enum NetworkType {
NO_NETWORK = 0,
WIFI,
MOBILE,
enum NetworkType
{
NO_NETWORK = 0,
WIFI,
MOBILE,
};
static NetworkType getNetworkType();

#ifdef SDK_BUGLY
static void setBuglyUserID(const std::string& id);
static void setBuglyUserData(const std::string& params);
#endif
#ifdef SDK_BUGLY
static void setBuglyUserID(const std::string &id);
static void setBuglyUserData(const std::string &params);
#endif

void takeOrPickPhoto(const std::string& method, const std::string& path, const SdkCallback &callback);

// 将结果同步至主线程中进行处理
void callbackToMainThread(const std::string key, const std::string argument);
void invoke(const std::string& key, const std::string& argument);
};

#endif /* UtilsSdk_hpp */
92 changes: 92 additions & 0 deletions frameworks/runtime-src/Classes/sdks/utils/UtilsSdk.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
#import <sys/sysctl.h>
#import <mach/mach.h>
#import "Reachability.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include <sys/time.h>
#define CLASS_NAME "org/cocos2dx/javascript/UtilsSdk"
#endif

#ifdef SDK_BUGLY
#include "Bugly/CocosPlugin/bugly/CrashReport.h"
#endif

#include "utils/photos/PhotoPicker.h"

USING_NS_CC;

static std::string size2string(size_t size){
Expand Down Expand Up @@ -91,6 +97,9 @@
callback("");
}
#endif
else if ("takePhoto" == method || "pickPhoto" == method || "takeOrPickPhoto" == method){
this->takeOrPickPhoto(method, params, callback);
}
else {
callback("");
}
Expand Down Expand Up @@ -364,3 +373,86 @@
}
}
#endif

void UtilsSdk::takeOrPickPhoto(const std::string& method, const std::string& path, const SdkCallback &callback)
{
char buffer[128] = {};
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
sprintf(buffer, "%lld", (long long)([[NSDate date] timeIntervalSince1970] * 1000));
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
time_t current;
time(&current);
sprintf(buffer, "%lld", (long long)current);
#endif
std::string key = std::string(buffer) + ":takePhoto";
auto finder = _callbacks.find(key);
if (_callbacks.end() != finder)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// SE_REPORT_ERROR("key [%s] for callback already exists!", key.c_str());
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#endif
callback("");
return;
}
_callbacks[key] = callback;

FileUtils::getInstance()->createDirectory(path);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
PhotoPicker* picker = [[PhotoPicker alloc] initWithKey:[NSString stringWithUTF8String:key.c_str()] :this];
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
PhotoPicker* picker = new PhotoPicker(key, this);
#endif
if ("takeOrPickPhoto" == method)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
[picker takeOrPickPhoto:[NSString stringWithUTF8String:(path + "/" + buffer + ".png").c_str()]];
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
picker->takeOrPickPhoto(path + "/" + buffer + ".png");
#endif
}
else if ("takePhoto" == method)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
[picker takePhoto:[NSString stringWithUTF8String:(path + "/" + buffer + ".png").c_str()]];
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
picker->takePhoto(path + "/" + buffer + ".png");
#endif
}
else if ("pickPhoto" == method)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
[picker pickPhoto:[NSString stringWithUTF8String:(path + "/" + buffer + ".png").c_str()]];
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
picker->pickPhoto(path + "/" + buffer + ".png");
#endif
}
}

void UtilsSdk::callbackToMainThread(const std::string key, const std::string argument)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
dispatch_async(dispatch_get_main_queue(), ^{
this->invoke(key, argument);
});
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// 安卓系统中似乎没有原生的方式进行任务的线程间同步,资料显示可以通过libuv中uv_async_send实现,因需要添加第三方库,故未尝试。
Director::getInstance()->runInNextUpdate(
[=]
{
this->invoke(key, argument);
});
#endif
}

void UtilsSdk::invoke(const std::string& key, const std::string& argument)
{
auto finder = _callbacks.find(key);
if (_callbacks.end() != finder)
{
SdkCallback& callback = finder->second;
callback(argument);
_callbacks.erase(finder);
}
}
Loading