From 8281bd7f105a550f6c89107b71e28734db2add4c Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 29 Sep 2012 19:27:02 -0700 Subject: [PATCH] call back into java for util_getCurSeconds implementation so java and C world are sure to be using the same clock. --- xwords4/android/XWords4/jni/andutils.c | 13 +++++++++++++ xwords4/android/XWords4/jni/andutils.h | 2 ++ xwords4/android/XWords4/jni/utilwrapper.c | 9 ++++++--- xwords4/android/XWords4/jni/xwjni.c | 4 ++-- .../XWords4/src/org/eehouse/android/xw4/Utils.java | 8 ++++++++ xwords4/android/scripts/getsigs.sh | 1 + 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/xwords4/android/XWords4/jni/andutils.c b/xwords4/android/XWords4/jni/andutils.c index f0f3283d5..9e579985d 100644 --- a/xwords4/android/XWords4/jni/andutils.c +++ b/xwords4/android/XWords4/jni/andutils.c @@ -484,6 +484,19 @@ and_empty_stream( MPFORMAL AndGlobals* globals ) return stream; } +XP_U32 +getCurSeconds( JNIEnv* env ) +{ + jclass clazz = (*env)->FindClass( env, PKG_PATH("Utils") ); + XP_ASSERT( !!clazz ); + jmethodID mid = (*env)->GetStaticMethodID( env, clazz, + "getCurSeconds", "()J" ); + jlong result = (*env)->CallStaticLongMethod( env, clazz, mid ); + + deleteLocalRef( env, clazz ); + return result; +} + void deleteLocalRef( JNIEnv* env, jobject jobj ) { if ( NULL != jobj ) { diff --git a/xwords4/android/XWords4/jni/andutils.h b/xwords4/android/XWords4/jni/andutils.h index bda6aa314..4d4e62584 100644 --- a/xwords4/android/XWords4/jni/andutils.h +++ b/xwords4/android/XWords4/jni/andutils.h @@ -72,6 +72,8 @@ void intToJenumField( JNIEnv* env, jobject jobj, int val, const char* field, jobject intToJEnum( JNIEnv* env, int val, const char* enumSig ); jint jEnumToInt( JNIEnv* env, jobject jenum ); +XP_U32 getCurSeconds( JNIEnv* env ); + void deleteLocalRef( JNIEnv* env, jobject jobj ); void deleteLocalRefs( JNIEnv* env, jobject jobj, ... ); # define DELETE_NO_REF ((jobject)-1) /* terminates above varargs list */ diff --git a/xwords4/android/XWords4/jni/utilwrapper.c b/xwords4/android/XWords4/jni/utilwrapper.c index 081d67c19..a5f18f295 100644 --- a/xwords4/android/XWords4/jni/utilwrapper.c +++ b/xwords4/android/XWords4/jni/utilwrapper.c @@ -369,9 +369,12 @@ and_util_altKeyDown( XW_UtilCtxt* uc ) XP_U32 and_util_getCurSeconds( XW_UtilCtxt* uc ) { - struct timeval tv; - gettimeofday( &tv, NULL ); - return tv.tv_sec; + AndUtil* andutil = (AndUtil*)uc; + XP_U32 curSeconds = getCurSeconds( *andutil->env ); + /* struct timeval tv; */ + /* gettimeofday( &tv, NULL ); */ + /* XP_LOGF( "%s: %d vs %d", __func__, (int)tv.tv_sec, (int)curSeconds ); */ + return curSeconds; } diff --git a/xwords4/android/XWords4/jni/xwjni.c b/xwords4/android/XWords4/jni/xwjni.c index 630489b1b..8a70e4839 100644 --- a/xwords4/android/XWords4/jni/xwjni.c +++ b/xwords4/android/XWords4/jni/xwjni.c @@ -55,7 +55,7 @@ makeGI( MPFORMAL JNIEnv* env, jobject j_gi ) gi->gameID = getInt( env, j_gi, "gameID" ); if ( 0 == gi->gameID ) { while ( 0 == gi->gameID ) { - gi->gameID = and_util_getCurSeconds( NULL ); + gi->gameID = getCurSeconds( env ); } setInt( env, j_gi, "gameID", gi->gameID ); } @@ -395,7 +395,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_initJNI MPASSIGN( state->mpool, mpool ); globals->vtMgr = make_vtablemgr(MPPARM_NOCOMMA(mpool)); - XP_U32 secs = and_util_getCurSeconds( NULL ); + XP_U32 secs = getCurSeconds( env ); XP_LOGF( "initing srand with %ld", secs ); srandom( secs ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java index 8715901de..a3589d77d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -43,6 +43,7 @@ import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; +import java.util.Date; import java.util.HashMap; import java.util.Random; import junit.framework.Assert; @@ -362,6 +363,13 @@ public class Utils { return result; } + public static long getCurSeconds() + { + long millis = new Date().getTime(); + int result = (int)(millis / 1000); + return result; + } + private static void setFirstBootStatics( Context context ) { int thisVersion = 0; diff --git a/xwords4/android/scripts/getsigs.sh b/xwords4/android/scripts/getsigs.sh index 49de53e93..74261650e 100755 --- a/xwords4/android/scripts/getsigs.sh +++ b/xwords4/android/scripts/getsigs.sh @@ -53,5 +53,6 @@ javap -s org.eehouse.android.${NODE}.jni.UtilCtxt javap -s org.eehouse.android.${NODE}.jni.CommsAddrRec javap -s org.eehouse.android.${NODE}.jni.TransportProcs javap -s org.eehouse.android.${NODE}.jni.JNIUtils +javap -s org.eehouse.android.${NODE}.Utils cat /tmp/javah$$.txt rm /tmp/javah$$.txt