diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java index a1a2c3016..ce1db43fd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java @@ -45,6 +45,7 @@ import java.util.Set; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import org.eehouse.android.xw4.MultiService.DictFetchOwner; import org.eehouse.android.xw4.MultiService.MultiEvent; import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType; import org.eehouse.android.xw4.jni.CommsAddrRec; @@ -1056,8 +1057,9 @@ public class BTService extends XWService { if ( DictLangCache.haveDict( context, nli.lang, nli.dict ) ) { result = makeGame( context, nli, btName, btAddr ); } else { - Intent intent = MultiService.makeMissingDictIntent( context, nli ); - Assert.fail(); + Intent intent = MultiService + .makeMissingDictIntent( context, nli, + DictFetchOwner.OWNER_BT ); // NetLaunchInfo.putExtras( intent, gameID, btName, btAddr ); MultiService.postMissingDictNotification( context, intent, nli.gameID ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java index 075007329..480d9ce70 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java @@ -43,10 +43,15 @@ public class MultiService { public static final String OWNER = "OWNER"; public static final String BT_NAME = "BT_NAME"; public static final String BT_ADDRESS = "BT_ADDRESS"; + public static final String NLI_DATA = "nli"; - public static final int OWNER_SMS = 1; - public static final int OWNER_RELAY = 2; - public static final int OWNER_BT = 3; + public enum DictFetchOwner { _NONE, + OWNER_SMS, + OWNER_RELAY, + OWNER_BT, + }; + + private static final String ACTION_FETCH_DICT = "_afd"; private MultiEventListener m_li; @@ -102,42 +107,23 @@ public class MultiService { } } - public static void fillInviteIntent( Intent intent, String gameName, - int lang, String dict, - int nPlayersT, int nPlayersH ) - { - intent.putExtra( GAMENAME, gameName ); - intent.putExtra( LANG, lang ); - intent.putExtra( DICT, dict ); - intent.putExtra( NPLAYERST, nPlayersT ); // both of these used - intent.putExtra( NPLAYERSH, nPlayersH ); - } - - public static Intent makeMissingDictIntent( Context context, String gameName, - int lang, String dict, - int nPlayersT, int nPlayersH ) + public static Intent makeMissingDictIntent( Context context, NetLaunchInfo nli, + DictFetchOwner owner ) { Intent intent = new Intent( context, DictsActivity.class ); - fillInviteIntent( intent, gameName, lang, dict, nPlayersT, nPlayersH ); - return intent; - } - - public static Intent makeMissingDictIntent( Context context, NetLaunchInfo nli ) - { - Intent intent = makeMissingDictIntent( context, null, nli.lang, nli.dict, - nli.nPlayersT, 1 ); - intent.putExtra( ROOM, nli.room ); + intent.setAction( ACTION_FETCH_DICT ); + intent.putExtra( LANG, nli.lang ); + intent.putExtra( DICT, nli.dict ); + intent.putExtra( OWNER, owner.ordinal() ); + intent.putExtra( NLI_DATA, nli.toString() ); return intent; } public static boolean isMissingDictIntent( Intent intent ) { - boolean result = intent.hasExtra( LANG ) - // && intent.hasExtra( DICT ) - && (intent.hasExtra( GAMEID ) || intent.hasExtra( ROOM )) - && intent.hasExtra( GAMENAME ) - && intent.hasExtra( NPLAYERST ) - && intent.hasExtra( NPLAYERSH ); + String action = intent.getAction(); + boolean result = null != action && action.equals( ACTION_FETCH_DICT ); + // DbgUtils.logf( "isMissingDictIntent() => %b", result ); return result; } @@ -179,16 +165,20 @@ public class MultiService { String dict = intent.getStringExtra( DICT ); downloaded = DictLangCache.haveDict( context, lang, dict ); if ( downloaded ) { - switch ( intent.getIntExtra( OWNER, -1 ) ) { - case OWNER_SMS: - SMSService.onGameDictDownload( context, intent ); - break; - case OWNER_RELAY: - case OWNER_BT: - GamesListDelegate.onGameDictDownload( context, intent ); - break; - default: + int ordinal = intent.getIntExtra( OWNER, -1 ); + if ( -1 == ordinal ) { DbgUtils.logf( "unexpected OWNER" ); + } else { + DictFetchOwner owner = DictFetchOwner.values()[ordinal]; + switch ( owner ) { + case OWNER_SMS: + SMSService.onGameDictDownload( context, intent ); + break; + case OWNER_RELAY: + case OWNER_BT: + GamesListDelegate.onGameDictDownload( context, intent ); + break; + } } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java index 59f268c32..181e932e9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java @@ -82,45 +82,14 @@ public class NetLaunchInfo { public NetLaunchInfo( String data ) { - try { - JSONObject json = new JSONObject( data ); + init( data ); + } - int flags = json.getInt(ADDRS_KEY); - m_addrs = DBUtils.intToConnTypeSet( flags ); - - lang = json.optInt( MultiService.LANG, -1 ); - forceChannel = json.optInt( MultiService.FORCECHANNEL, 0 ); - dict = json.optString( MultiService.DICT ); - gameName = json.optString( MultiService.GAMENAME ); - nPlayersT = json.optInt( MultiService.NPLAYERST, -1 ); - nPlayersH = json.optInt( MultiService.NPLAYERSH, -1 ); - gameID = json.optInt( MultiService.GAMEID, -1 ); - - for ( CommsConnType typ : m_addrs.getTypes() ) { - switch ( typ ) { - case COMMS_CONN_BT: - btAddress = json.getString( MultiService.BT_ADDRESS ); - btName = json.getString( MultiService.BT_NAME ); - break; - case COMMS_CONN_RELAY: - room = json.getString( MultiService.ROOM ); - m_inviteID = json.optString( MultiService.INVITEID ); - break; - case COMMS_CONN_SMS: - phone = json.getString( PHONE_KEY ); - isGSM = json.getBoolean( GSM_KEY ); - osVers = json.getInt( OSVERS_KEY ); - break; - default: - DbgUtils.logf( "Unexpected typ %s", typ.toString() ); - break; - } - } - - calcValid(); - } catch ( JSONException jse ) { - DbgUtils.loge( jse ); - } + public NetLaunchInfo( Intent intent ) + { + String data = intent.getStringExtra( MultiService.NLI_DATA ); + Assert.assertNotNull( data ); + init( data ); } public NetLaunchInfo( Bundle bundle ) @@ -195,24 +164,6 @@ public class NetLaunchInfo { calcValid(); } - public NetLaunchInfo( Intent intent ) - { - room = intent.getStringExtra( MultiService.ROOM ); - m_inviteID = intent.getStringExtra( MultiService.INVITEID ); - lang = intent.getIntExtra( MultiService.LANG, -1 ); - forceChannel = intent.getIntExtra( MultiService.FORCECHANNEL, -1 ); - dict = intent.getStringExtra( MultiService.DICT ); - gameName = intent.getStringExtra( MultiService.GAMENAME ); - nPlayersT = intent.getIntExtra( MultiService.NPLAYERST, -1 ); - nPlayersH = intent.getIntExtra( MultiService.NPLAYERSH, -1 ); - gameID = intent.getIntExtra( MultiService.GAMEID, -1 ); - btName = intent.getStringExtra( MultiService.BT_NAME ); - btAddress = intent.getStringExtra( MultiService.BT_ADDRESS ); - m_addrs = DBUtils.intToConnTypeSet( intent.getIntExtra( ADDRS_KEY, -1 ) ); - - calcValid(); - } - public NetLaunchInfo( int gamID, int dictLang, String dictName, int nPlayers ) { this(); @@ -349,6 +300,51 @@ public class NetLaunchInfo { return result; } + private void init( String data ) + { + try { + JSONObject json = new JSONObject( data ); + + int flags = json.getInt(ADDRS_KEY); + m_addrs = DBUtils.intToConnTypeSet( flags ); + + lang = json.optInt( MultiService.LANG, -1 ); + forceChannel = json.optInt( MultiService.FORCECHANNEL, 0 ); + dict = json.optString( MultiService.DICT ); + gameName = json.optString( MultiService.GAMENAME ); + nPlayersT = json.optInt( MultiService.NPLAYERST, -1 ); + nPlayersH = json.optInt( MultiService.NPLAYERSH, -1 ); + gameID = json.optInt( MultiService.GAMEID, -1 ); + + for ( CommsConnType typ : m_addrs.getTypes() ) { + switch ( typ ) { + case COMMS_CONN_BT: + btAddress = json.getString( MultiService.BT_ADDRESS ); + btName = json.getString( MultiService.BT_NAME ); + break; + case COMMS_CONN_RELAY: + room = json.getString( MultiService.ROOM ); + m_inviteID = json.optString( MultiService.INVITEID ); + break; + case COMMS_CONN_SMS: + phone = json.getString( PHONE_KEY ); + isGSM = json.getBoolean( GSM_KEY ); + osVers = json.getInt( OSVERS_KEY ); + break; + default: + DbgUtils.logf( "Unexpected typ %s", typ.toString() ); + break; + } + } + + calcValid(); + } catch ( JSONException jse ) { + DbgUtils.loge( jse ); + } + + calcValid(); + } + private void appendInt( Uri.Builder ub, String key, int value ) { ub.appendQueryParameter( key, String.format("%d", value ) );