From a9769efe1cc89ef4e5614b632b605cac7409940b Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 9 Jul 2018 07:11:54 -0700 Subject: [PATCH] implement load/store on android --- .../eehouse/android/xw4/jni/DUtilCtxt.java | 26 +++++++++++++-- xwords4/android/jni/utilwrapper.c | 33 +++++++++++++++---- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java index c98ec18c9..8192636f7 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java @@ -26,6 +26,7 @@ import android.telephony.PhoneNumberUtils; import junit.framework.Assert; import org.eehouse.android.xw4.XWApp; +import org.eehouse.android.xw4.DBUtils; import org.eehouse.android.xw4.DevID; import org.eehouse.android.xw4.R; import org.eehouse.android.xw4.Log; @@ -210,7 +211,28 @@ public class DUtilCtxt { return same; } -// SET_DPROC(getCurSeconds); -// SET_DPROC(md5sum); + public void store( String key, byte[] data ) + { + Log.d( TAG, "store(key=%s)", key ); + if ( null == data ) { + } else { + DBUtils.setBytesFor( m_context, key, data ); + } + } + + public byte[] load( String key ) + { + byte[] result = null; + int resultLen = 0; + Log.d( TAG, "load(key=%s)", key ); + + result = DBUtils.getBytesFor( m_context, key ); + if ( result != null ) { + resultLen = result.length; + } + + Log.d( TAG, "load(%s) returning %d bytes", key, resultLen ); + return result; + } } diff --git a/xwords4/android/jni/utilwrapper.c b/xwords4/android/jni/utilwrapper.c index dec3de003..42df3d767 100644 --- a/xwords4/android/jni/utilwrapper.c +++ b/xwords4/android/jni/utilwrapper.c @@ -459,17 +459,38 @@ and_dutil_getUserQuantityString( XW_DUtilCtxt* duc, XP_U16 stringCode, XP_U16 qu } static void -and_dutil_store( XW_DUtilCtxt* duc, const XP_UCHAR* key, XWStreamCtxt* data ) +and_dutil_store( XW_DUtilCtxt* duc, const XP_UCHAR* key, XWStreamCtxt* stream ) { - XP_LOGF( "%s() NOT IMPLEMENTED", __func__ ); - // XP_ASSERT(0); + DUTIL_CBK_HEADER( "store", "(Ljava/lang/String;[B)V" ); + + JNIEnv* env = ENVFORME( dutil->ti ); + jbyteArray jdata = streamToBArray( env, stream ); + jstring jkey = (*env)->NewStringUTF( env, key ); + + (*env)->CallVoidMethod( env, dutil->jdutil, mid, jkey, jdata ); + + deleteLocalRefs( env, jdata, jkey, DELETE_NO_REF ); + + DUTIL_CBK_TAIL(); } static void -and_dutil_load( XW_DUtilCtxt* duc, const XP_UCHAR* key,XWStreamCtxt* inOut ) +and_dutil_load( XW_DUtilCtxt* duc, const XP_UCHAR* key, XWStreamCtxt* stream ) { - XP_LOGF( "%s() NOT IMPLEMENTED", __func__ ); - // XP_ASSERT(0); + DUTIL_CBK_HEADER("load", "(Ljava/lang/String;)[B"); + + jstring jkey = (*env)->NewStringUTF( env, key ); + jbyteArray jvalue = (*env)->CallObjectMethod( env, dutil->jdutil, + mid, jkey ); + if ( jvalue != NULL ) { + jbyte* jelems = (*env)->GetByteArrayElements( env, jvalue, NULL ); + jsize len = (*env)->GetArrayLength( env, jvalue ); + stream_putBytes( stream, jelems, len ); + (*env)->ReleaseByteArrayElements( env, jvalue, jelems, 0 ); + } + deleteLocalRefs( env, jkey, jvalue, DELETE_NO_REF ); + + DUTIL_CBK_TAIL(); } static void