From d8f3b0c25b46a20ea26ef6778410e2557734da60 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 May 2011 18:32:13 +0800 Subject: [PATCH] [android] CCUserDefault works ok on android2.2 --- .../platform/android/CCFileUtils_android.cpp | 6 +-- cocos2dx/platform/android/Cocos2dJni.cpp | 47 +++++++++++++++++++ cocos2dx/platform/android/Cocos2dJni.h | 1 + .../org/cocos2dx/lib/Cocos2dxActivity.java | 7 +++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/cocos2dx/platform/android/CCFileUtils_android.cpp b/cocos2dx/platform/android/CCFileUtils_android.cpp index 8637e74b66..9a94995b89 100644 --- a/cocos2dx/platform/android/CCFileUtils_android.cpp +++ b/cocos2dx/platform/android/CCFileUtils_android.cpp @@ -25,6 +25,7 @@ THE SOFTWARE. NS_CC_BEGIN; #include "CCCommon.h" +#include "Cocos2dJni.h" #define MAX_PATH 256 @@ -107,9 +108,8 @@ int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out) string CCFileUtils::getWriteablePath() { // the path is: /data/data/ + package name - string dir("/data/data"); - size_t length = s_strResourcePath.rfind(".") - s_strResourcePath.rfind("/"); - return dir + s_strResourcePath.substr(s_strResourcePath.rfind("/"), length) + "/" ; + string dir("/data/data/"); + return dir + getPackageNameJNI() + "/" ; } NS_CC_END; diff --git a/cocos2dx/platform/android/Cocos2dJni.cpp b/cocos2dx/platform/android/Cocos2dJni.cpp index f3a60676e8..340defa818 100644 --- a/cocos2dx/platform/android/Cocos2dJni.cpp +++ b/cocos2dx/platform/android/Cocos2dJni.cpp @@ -375,4 +375,51 @@ extern "C" { cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); } + + ////////////////////////////////////////////////////////////////////////// + // get package name + ////////////////////////////////////////////////////////////////////////// + + static char* jstringTostring(JNIEnv* env, jstring jstr) + { + char* rtn = NULL; + + // convert jstring to byte array + jclass clsstring = env->FindClass("java/lang/String"); + jstring strencode = env->NewStringUTF("utf-8"); + jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); + jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode); + jsize alen = env->GetArrayLength(barr); + jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE); + + // copy byte array into char[] + if (alen > 0) + { + rtn = (char*)malloc(alen + 1); + memcpy(rtn, ba, alen); + rtn[alen] = 0; + } + env->ReleaseByteArrayElements(barr, ba, 0); + + return rtn; + } + + const char* getPackageNameJNI() + { + TMethodJNI t; + const char* ret = NULL; + + if (getMethodID(t + , "org/cocos2dx/lib/Cocos2dxActivity" + , "getCocos2dxPackageName" + , "()Ljava/lang/String;")) + { + jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); + ret = jstringTostring(t.env, str); + + LOGD("package name %s", ret); + } + + return ret; + } } diff --git a/cocos2dx/platform/android/Cocos2dJni.h b/cocos2dx/platform/android/Cocos2dJni.h index 7cf0d83c98..004f870897 100644 --- a/cocos2dx/platform/android/Cocos2dJni.h +++ b/cocos2dx/platform/android/Cocos2dJni.h @@ -34,6 +34,7 @@ extern "C" void disableAccelerometerJNI(); void showMessageBoxJNI(const char * pszMsg, const char * pszTitle); void setKeyboardStateJNI(int bOpen); + const char* getPackageNameJNI(); } #endif // __ANDROID_COCOS2D_JNI_H__ diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java index 5a5cd4a55c..40a02e92e7 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -64,6 +64,7 @@ public class Cocos2dxActivity extends Activity{ private static boolean accelerometerEnabled = false; private static Handler handler; private final static int HANDLER_SHOW_DIALOG = 1; + private static String packageName; private static native void nativeSetPaths(String apkPath); @@ -171,6 +172,10 @@ public class Cocos2dxActivity extends Activity{ backgroundMusicPlayer.end(); soundPlayer.end(); } + + public static String getCocos2dxPackageName(){ + return packageName; + } @Override protected void onResume() { @@ -195,6 +200,8 @@ public class Cocos2dxActivity extends Activity{ } protected void setPackgeName(String packageName) { + Cocos2dxActivity.packageName = packageName; + String apkFilePath = ""; ApplicationInfo appInfo = null; PackageManager packMgmr = getApplication().getPackageManager();