[android] CCUserDefault works ok on android2.2

This commit is contained in:
minggo 2011-05-12 18:32:13 +08:00
parent a50fe1b95b
commit d8f3b0c25b
4 changed files with 58 additions and 3 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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__

View File

@ -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);
@ -172,6 +173,10 @@ public class Cocos2dxActivity extends Activity{
soundPlayer.end();
}
public static String getCocos2dxPackageName(){
return packageName;
}
@Override
protected void onResume() {
super.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();