call back into java for util_getCurSeconds implementation so java and

C world are sure to be using the same clock.
This commit is contained in:
Eric House 2012-09-29 19:27:02 -07:00
parent ebaa22594d
commit 8281bd7f10
6 changed files with 32 additions and 5 deletions

View file

@ -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 ) {

View file

@ -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 */

View file

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

View file

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

View file

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

View file

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