LocalStorageAndroid

/*

 Local Storage support for the JS Bindings for iOS.

 Works on cocos2d-iphone and cocos2d-x.

 */


#include "cocos2d.h"


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)


#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#include <string>

#include "jni.h"

#include "jni/JniHelper.h"


USING_NS_CC;

static int _initialized = 0;


static void splitFilename (std::string& str)

{

size_t found = 0;

found=str.find_last_of("/\\");

if (found != std::string::npos)

{

str = str.substr(found+1);

}

}


void localStorageInit( const char *fullpath)

{

if (fullpath == NULL || strlen(fullpath) == 0) return;


if( ! _initialized ) {

JniMethodInfo t;


        if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "init", "(Ljava/lang/String;Ljava/lang/String;)Z")) {

       std::string strDBFilename = fullpath;

       splitFilename(strDBFilename);

       jstring jdbName = t.env->NewStringUTF(strDBFilename.c_str());

       jstring jtableName = t.env->NewStringUTF("data");

            jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, jdbName, jtableName);

            t.env->DeleteLocalRef(jdbName);

            t.env->DeleteLocalRef(jtableName);

            t.env->DeleteLocalRef(t.classID);

                if (ret) {

                    _initialized = 1;

                }

        }

}

}


void localStorageFree()

{

if( _initialized ) {

JniMethodInfo t;

        

        if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "destory", "()V"))

        {

            t.env->CallStaticVoidMethod(t.classID, t.methodID);

       t.env->DeleteLocalRef(t.classID); 

        }

        

_initialized = 0;

}

}


/** sets an item in the LS */

void localStorageSetItem( const char *key, const char *value)

{

assert( _initialized );

    JniMethodInfo t;


    if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "setItem", "(Ljava/lang/String;Ljava/lang/String;)V")) {

        jstring jkey = t.env->NewStringUTF(key);

        jstring jvalue = t.env->NewStringUTF(value);

        t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey, jvalue);

        t.env->DeleteLocalRef(jkey);

        t.env->DeleteLocalRef(jvalue);

        t.env->DeleteLocalRef(t.classID);

    }

}


/** gets an item from the LS */

const char* localStorageGetItem( const char *key )

{

assert( _initialized );

    JniMethodInfo t;

    CCString* pStr = NULL;

    if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;")) {

        jstring jkey = t.env->NewStringUTF(key);

        jstring ret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey);

        pStr = CCString::create(JniHelper::jstring2string(ret));

        t.env->DeleteLocalRef(ret);

        t.env->DeleteLocalRef(jkey);

        t.env->DeleteLocalRef(t.classID);

    }

    return pStr ? pStr->getCString() : NULL;

}


/** removes an item from the LS */

void localStorageRemoveItem( const char *key )

{

assert( _initialized );

    JniMethodInfo t;


    if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "removeItem", "(Ljava/lang/String;)V")) {

        jstring jkey = t.env->NewStringUTF(key);

        t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey);

        t.env->DeleteLocalRef(jkey);

        t.env->DeleteLocalRef(t.classID);

    }


}


#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值