From 2a4bce4ad1990b0d9f72e45af61ab2191590683a Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 22 Feb 2017 22:29:02 -0800 Subject: [PATCH] remove Context ivar from CurGameInfo It claimed to be Serializable but wasn't: Crash. Now it is. --- .../android/xw4/GameConfigDelegate.java | 7 +-- .../org/eehouse/android/xw4/GameUtils.java | 14 +++--- .../eehouse/android/xw4/jni/CurGameInfo.java | 47 +++++++++---------- .../eehouse/android/xw4/jni/GameSummary.java | 2 +- .../eehouse/android/xw4/jni/JNIThread.java | 14 ++++-- 5 files changed, 43 insertions(+), 41 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameConfigDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameConfigDelegate.java index d6aba77b3..09b2592fc 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameConfigDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameConfigDelegate.java @@ -569,7 +569,7 @@ public class GameConfigDelegate extends DelegateBase } if ( null == m_gi ) { - m_gi = new CurGameInfo( m_activity, m_giOrig ); + m_gi = new CurGameInfo( m_giOrig ); } m_carOrig = new CommsAddrRec(); @@ -848,7 +848,7 @@ public class GameConfigDelegate extends DelegateBase if ( !isFinishing() ) { m_playerLayout.removeAllViews(); - String[] names = m_gi.visibleNames( false ); + String[] names = m_gi.visibleNames( m_activity, false ); // only enable delete if one will remain (or two if networked) boolean canDelete = names.length > 2 || (localOnlyGame() && names.length > 1); @@ -973,7 +973,8 @@ public class GameConfigDelegate extends DelegateBase private void selLangChanged( String chosen ) { - m_gi.setLang( DictLangCache.getLangLangCode( m_activity, chosen ) ); + m_gi.setLang( m_activity, DictLangCache + .getLangLangCode( m_activity, chosen ) ); loadPlayersList(); configDictSpinner( m_dictSpinner, m_gi.dictLang, m_gi.dictName ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java index 72e459c6a..25fc3cfe7 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java @@ -124,7 +124,7 @@ public class GameUtils { gamePtr.release(); gamePtr = XwJNI.initNew( gi, dictNames, pairs.m_bytes, pairs.m_paths, - gi.langName(), (UtilCtxt)null, + gi.langName( context ), (UtilCtxt)null, JNIUtilsImpl.get( context ), (DrawCtx)null, CommonPrefs.get( context ), (TransportProcs)null ); @@ -363,7 +363,7 @@ public class GameUtils { DbgUtils.logw( TAG, "loadMakeGame() failing: dicts %s unavailable", TextUtils.join( ",", dictNames ) ); } else { - String langName = gi.langName(); + String langName = gi.langName( context ); gamePtr = XwJNI.initFromStream( rowid, stream, gi, dictNames, pairs.m_bytes, pairs.m_paths, langName, util, @@ -585,7 +585,7 @@ public class GameUtils { Assert.assertNotNull( inviteID ); CurGameInfo gi = new CurGameInfo( context, inviteID ); gi.setFrom( jsonData ); - gi.setLang( lang[0], dict[0] ); + gi.setLang( context, lang[0], dict[0] ); gi.forceChannel = forceChannel; lang[0] = gi.dictLang; dict[0] = gi.dictName; @@ -977,7 +977,7 @@ public class GameUtils { XwJNI.gi_from_stream( gi, stream ); // first time required so dictNames() will work - gi.replaceDicts( newDict ); + gi.replaceDicts( context, newDict ); String[] dictNames = gi.dictNames(); DictUtils.DictPairs pairs = DictUtils.openDicts( context, @@ -986,11 +986,11 @@ public class GameUtils { GamePtr gamePtr = XwJNI.initFromStream( rowid, stream, gi, dictNames, pairs.m_bytes, pairs.m_paths, - gi.langName(), null, + gi.langName( context ), null, JNIUtilsImpl.get(context), null, CommonPrefs.get( context ), null ); // second time required as game_makeFromStream can overwrite - gi.replaceDicts( newDict ); + gi.replaceDicts( context, newDict ); saveGame( context, gamePtr, gi, lock, false ); @@ -1022,7 +1022,7 @@ public class GameUtils { // games? String[] dictNames = gi.dictNames(); DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames ); - String langName = gi.langName(); + String langName = gi.langName( context ); GamePtr gamePtr = null; boolean madeGame = false; CommonPrefs cp = CommonPrefs.get( context ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/CurGameInfo.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/CurGameInfo.java index 0738a19a8..ce3554634 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/CurGameInfo.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/CurGameInfo.java @@ -71,7 +71,6 @@ public class CurGameInfo implements Serializable { // private int m_nVisiblePlayers; private int m_smartness; private String m_name; // not shared across the jni boundary - private Context m_context; public CurGameInfo( Context context ) { @@ -81,7 +80,6 @@ public class CurGameInfo implements Serializable { public CurGameInfo( Context context, String inviteID ) { boolean isNetworked = null != inviteID; - m_context = context; nPlayers = 2; gameSeconds = 60 * nPlayers * CommonPrefs.getDefaultPlayerMinutes( context ); @@ -127,12 +125,11 @@ public class CurGameInfo implements Serializable { juggle(); } - setLang( 0 ); + setLang( context, 0 ); } - public CurGameInfo( Context context, CurGameInfo src ) + public CurGameInfo( CurGameInfo src ) { - m_context = context; m_name = src.m_name; gameID = src.gameID; nPlayers = src.nPlayers; @@ -200,23 +197,23 @@ public class CurGameInfo implements Serializable { } } - public void setLang( int lang, String dict ) + public void setLang( Context context, int lang, String dict ) { if ( null != dict ) { dictName = dict; } - setLang( lang ); + setLang( context, lang ); } - public void setLang( int lang ) + public void setLang( Context context, int lang ) { if ( 0 == lang ) { - String dictName = CommonPrefs.getDefaultHumanDict( m_context ); - lang = DictLangCache.getDictLangCode( m_context, dictName ); + String dictName = CommonPrefs.getDefaultHumanDict( context ); + lang = DictLangCache.getDictLangCode( context, dictName ); } if ( dictLang != lang ) { dictLang = lang; - assignDicts(); + assignDicts( context ); } } @@ -317,10 +314,10 @@ public class CurGameInfo implements Serializable { return locs; } - public String[] visibleNames( boolean withDicts ) + public String[] visibleNames( Context context, boolean withDicts ) { String nameFmt = withDicts? - LocUtils.getString( m_context, R.string.name_dict_fmt ) + LocUtils.getString( context, R.string.name_dict_fmt ) : "%s"; String[] names = new String[nPlayers]; for ( int ii = 0; ii < nPlayers; ++ii ) { @@ -328,14 +325,14 @@ public class CurGameInfo implements Serializable { if ( lp.isLocal || serverRole == DeviceRole.SERVER_STANDALONE ) { String name; if ( lp.isRobot() ) { - String format = LocUtils.getString( m_context, R.string.robot_name_fmt ); + String format = LocUtils.getString( context, R.string.robot_name_fmt ); name = String.format( format, lp.name ); } else { name = lp.name; } names[ii] = String.format( nameFmt, name, dictName(lp) ); } else { - names[ii] = LocUtils.getString( m_context, R.string.guest_name ); + names[ii] = LocUtils.getString( context, R.string.guest_name ); } } return names; @@ -352,10 +349,10 @@ public class CurGameInfo implements Serializable { } // Replace any dict that doesn't exist with newDict - public void replaceDicts( String newDict ) + public void replaceDicts( Context context, String newDict ) { String[] dicts = - DictLangCache.getHaveLang( m_context, dictLang ); + DictLangCache.getHaveLang( context, dictLang ); HashSet installed = new HashSet( Arrays.asList(dicts) ); if ( !installed.contains( dictName ) ) { @@ -372,9 +369,9 @@ public class CurGameInfo implements Serializable { } } - public String langName() + public String langName( Context context ) { - return DictLangCache.getLangName( m_context, dictLang ); + return DictLangCache.getLangName( context, dictLang ); } public String dictName( final LocalPlayer lp ) @@ -484,7 +481,7 @@ public class CurGameInfo implements Serializable { return canJuggle; } - private void assignDicts() + private void assignDicts( Context context ) { // For each player's dict, if non-null and language matches // leave it alone. Otherwise replace with default if that @@ -492,13 +489,13 @@ public class CurGameInfo implements Serializable { // right language. String humanDict = - DictLangCache.getBestDefault( m_context, dictLang, true ); + DictLangCache.getBestDefault( context, dictLang, true ); String robotDict = - DictLangCache.getBestDefault( m_context, dictLang, false ); + DictLangCache.getBestDefault( context, dictLang, false ); if ( null == dictName - || ! DictUtils.dictExists( m_context, dictName ) - || dictLang != DictLangCache.getDictLangCode( m_context, + || ! DictUtils.dictExists( context, dictName ) + || dictLang != DictLangCache.getDictLangCode( context, dictName ) ) { dictName = humanDict; } @@ -507,7 +504,7 @@ public class CurGameInfo implements Serializable { LocalPlayer lp = players[ii]; if ( null != lp.dictName && - dictLang != DictLangCache.getDictLangCode( m_context, + dictLang != DictLangCache.getDictLangCode( context, lp.dictName ) ) { lp.dictName = null; } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java index 231a48915..68ce0140e 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java @@ -358,7 +358,7 @@ public class GameSummary implements Serializable { { String[] names = null; if ( null != m_gi ) { - names = m_gi.visibleNames( false ); + names = m_gi.visibleNames( m_context, false ); } else if ( null != m_playersSummary ) { names = TextUtils.split( m_playersSummary, "\n" ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java index 14d644498..c2741cbb7 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java @@ -218,15 +218,19 @@ public class JNIThread extends Thread { } m_jniGamePtr = null; if ( null != stream ) { - m_jniGamePtr = XwJNI.initFromStream( m_rowid, stream, m_gi, dictNames, - pairs.m_bytes, pairs.m_paths, - m_gi.langName(), utils, jniUtils, + m_jniGamePtr = XwJNI.initFromStream( m_rowid, stream, m_gi, + dictNames, pairs.m_bytes, + pairs.m_paths, + m_gi.langName( m_context ), + utils, jniUtils, null, cp, m_xport ); } if ( null == m_jniGamePtr ) { m_jniGamePtr = XwJNI.initNew( m_gi, dictNames, pairs.m_bytes, - pairs.m_paths, m_gi.langName(), utils, - jniUtils, null, cp, m_xport ); + pairs.m_paths, + m_gi.langName(m_context), + utils, jniUtils, null, cp, + m_xport ); } Assert.assertNotNull( m_jniGamePtr ); m_lastSavedState = Arrays.hashCode( stream );