diff --git a/xwords4/android/XWords4/jni/xwjni.c b/xwords4/android/XWords4/jni/xwjni.c index 63e2c8d80..c64f64921 100644 --- a/xwords4/android/XWords4/jni/xwjni.c +++ b/xwords4/android/XWords4/jni/xwjni.c @@ -1019,7 +1019,6 @@ Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getAddr (JNIEnv* env, jclass C, jint gamePtr, jobject jaddr ) { XWJNI_START(); - LOG_FUNC(); XP_ASSERT( state->game.comms ); CommsAddrRec addr; comms_getAddr( state->game.comms, &addr ); @@ -1027,6 +1026,34 @@ Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getAddr XWJNI_END(); } +JNIEXPORT jobjectArray JNICALL +Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getAddrs +( JNIEnv* env, jclass C, jint gamePtr ) +{ + jobjectArray result = NULL; + XP_U16 ii; + XWJNI_START(); + XP_ASSERT( state->game.comms ); + CommsAddrRec addrs[MAX_NUM_PLAYERS]; + XP_U16 count = VSIZE(addrs); + comms_getAddrs( state->game.comms, addrs, &count ); + + jclass clas = (*env)->FindClass( env, PKG_PATH("jni/CommsAddrRec") ); + result = (*env)->NewObjectArray( env, count, clas, NULL ); + + jmethodID initId = (*env)->GetMethodID( env, clas, "", "()V" ); + for ( ii = 0; ii < count; ++ii ) { + jobject jaddr = (*env)->NewObject( env, clas, initId ); + setJAddrRec( env, jaddr, &addrs[ii] ); + (*env)->SetObjectArrayElement( env, result, ii, jaddr ); + (*env)->DeleteLocalRef( env, jaddr ); + } + (*env)->DeleteLocalRef( env, clas ); + + XWJNI_END(); + return result; +} + JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_comms_1setAddr ( JNIEnv* env, jclass C, jint gamePtr, jobject jaddr ) @@ -1111,21 +1138,23 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1summarize setString( env, jsummary, "relayID", buf ); } setString( env, jsummary, "roomName", addr.u.ip_relay.invite ); -#ifdef XWFEATURE_BLUETOOTH - } else if ( COMMS_CONN_BT == addr.conType ) { - XP_BtAddrStr addrs[MAX_NUM_PLAYERS]; +#if defined XWFEATURE_BLUETOOTH || defined XWFEATURE_SMS + } else if ( COMMS_CONN_BT == addr.conType + || COMMS_CONN_SMS == addr.conType ) { + XP_Bool isBT = COMMS_CONN_BT == addr.conType; + CommsAddrRec addrs[MAX_NUM_PLAYERS]; XP_U16 count = VSIZE(addrs); - comms_getBTAddrs( comms, addrs, &count ); + comms_getAddrs( comms, addrs, &count ); int ii; - XP_ASSERT( count < VSIZE(addrs) ); const XP_UCHAR* addrps[count]; for ( ii = 0; ii < count; ++ii ) { - addrps[ii] = (XP_UCHAR*)&addrs[ii]; - XP_LOGF( "%s: adding btaddr %s", __func__, addrps[ii] ); + addrps[ii] = isBT ? (XP_UCHAR*)&addrs[ii].u.bt.btAddr : + (XP_UCHAR*)&addrs[ii].u.sms.phone; + XP_LOGF( "%s: adding btaddr/phone %s", __func__, addrps[ii] ); } jobjectArray jaddrs = makeStringArray( env, count, addrps ); - setObject( env, jsummary, "remoteBTAddrs", "[Ljava/lang/String;", + setObject( env, jsummary, "remoteDevs", "[Ljava/lang/String;", jaddrs ); (*env)->DeleteLocalRef( env, jaddrs ); #endif diff --git a/xwords4/android/XWords4/res/layout/game_config.xml b/xwords4/android/XWords4/res/layout/game_config.xml index d873df0e1..97da13ee7 100644 --- a/xwords4/android/XWords4/res/layout/game_config.xml +++ b/xwords4/android/XWords4/res/layout/game_config.xml @@ -69,8 +69,7 @@ android:orientation="vertical" android:layout_marginTop="5sp" android:layout_marginBottom="5sp" - > - + /> - + + + + + + + Invitation accepted. - + Connection (via SMS/text) + Connected number[s]: + Game in play with %s diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index 86b0931f6..765f7a590 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -192,9 +192,10 @@ public class DBUtils { } break; case COMMS_CONN_BT: + case COMMS_CONN_SMS: col = cursor.getColumnIndex( DBHelper.REMOTEDEVS ); if ( col >= 0 ) { - summary.setRemoteBTAddrs( cursor.getString( col ) ); + summary.setRemoteDevs( cursor.getString( col ) ); } break; } @@ -274,8 +275,9 @@ public class DBUtils { values.put( DBHelper.RELAYID, summary.relayID ); break; case COMMS_CONN_BT: - values.put( DBHelper.REMOTEDEVS, - summary.summarizeBTDevs() ); + case COMMS_CONN_SMS: + values.put( DBHelper.REMOTEDEVS, + summary.summarizeDevs() ); break; } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfig.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfig.java index edd855fa0..5c35954ec 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfig.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfig.java @@ -84,6 +84,7 @@ public class GameConfig extends XWActivity private Button m_playButton; private ImageButton m_refreshRoomsButton; private View m_connectSetRelay; + private View m_connectSetSMS; private Spinner m_roomChoose; // private Button m_configureButton; private long m_rowid; @@ -100,6 +101,7 @@ public class GameConfig extends XWActivity private String m_browseText; private LinearLayout m_playerLayout; private CommsAddrRec m_carOrig; + private CommsAddrRec[] m_remoteAddrs; private CommsAddrRec m_car; private CommonPrefs m_cp; private boolean m_canDoSMS = false; @@ -399,6 +401,10 @@ public class GameConfig extends XWActivity setContentView(R.layout.game_config); m_connectSetRelay = findViewById(R.id.connect_set_relay); + m_connectSetSMS = findViewById(R.id.connect_set_sms); + if ( !XWApp.SMSSUPPORTED ) { + m_connectSetSMS.setVisibility( View.GONE ); + } m_addPlayerButton = (Button)findViewById(R.id.add_player); m_addPlayerButton.setOnClickListener( this ); @@ -478,6 +484,7 @@ public class GameConfig extends XWActivity m_carOrig = new CommsAddrRec(); if ( XwJNI.game_hasComms( gamePtr ) ) { XwJNI.comms_getAddr( gamePtr, m_carOrig ); + m_remoteAddrs = XwJNI.comms_getAddrs( gamePtr ); } else if (DeviceRole.SERVER_STANDALONE != m_giOrig.serverRole){ String relayName = CommonPrefs.getDefaultRelayHost( this ); int relayPort = CommonPrefs.getDefaultRelayPort( this ); @@ -516,6 +523,8 @@ public class GameConfig extends XWActivity loadPlayersList(); configLangSpinner(); + loadPhones(); + m_phoniesSpinner.setSelection( m_gi.phoniesAction.ordinal() ); setSmartnessSpinner(); @@ -692,6 +701,11 @@ public class GameConfig extends XWActivity m_connectSetRelay. setVisibility( m_conType == CommsConnType.COMMS_CONN_RELAY ? View.VISIBLE : View.GONE ); + if ( XWApp.SMSSUPPORTED ) { + m_connectSetSMS. + setVisibility( m_conType == CommsConnType.COMMS_CONN_SMS ? + View.VISIBLE : View.GONE ); + } if ( ! localOnlyGame() && ((0 == m_gi.remoteCount() ) @@ -788,6 +802,21 @@ public class GameConfig extends XWActivity } } + private void loadPhones() + { + if ( XWApp.SMSSUPPORTED ) { + LinearLayout phoneList = + (LinearLayout)findViewById(R.id.sms_phones); + for ( CommsAddrRec addr : m_remoteAddrs ) { + XWListItem item = + (XWListItem)Utils.inflate( this, R.layout.list_item ); + item.setText( addr.sms_phone ); + item.setEnabled( false ); + phoneList.addView( item ); + } + } + } + private void setSmartnessSpinner() { int setting = -1; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/GameSummary.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/GameSummary.java index 7a02bffcd..ae57ca4c2 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/GameSummary.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/GameSummary.java @@ -56,7 +56,7 @@ public class GameSummary { public int pendingMsgLevel; public long modtime; public int gameID; - public String[] remoteBTAddrs; + public String[] remoteDevs; // BTAddr or phone number public int dictLang; public DeviceRole serverRole; @@ -103,20 +103,19 @@ public class GameSummary { return result; } - public String summarizeBTDevs() + public String summarizeDevs() { String result = null; - if ( null != remoteBTAddrs ) { - result = TextUtils.join( "\n", remoteBTAddrs ); + if ( null != remoteDevs ) { + result = TextUtils.join( "\n", remoteDevs ); } return result; } - public void setRemoteBTAddrs( String asString ) + public void setRemoteDevs( String asString ) { - DbgUtils.logf( "setRemoteBTAddrs(%s)", asString ); if ( null != asString ) { - remoteBTAddrs = TextUtils.split( asString, "\n" ); + remoteDevs = TextUtils.split( asString, "\n" ); } } @@ -192,10 +191,16 @@ public class GameSummary { } } else if ( gameOver ) { fmtID = R.string.summary_gameover; + } else if ( null != remoteDevs + && CommsConnType.COMMS_CONN_SMS == conType ) { + result = Utils.format( m_context, R.string.summary_conn_sms, + TextUtils.join(", ", remoteDevs) ); } else { fmtID = R.string.summary_conn; } - result = m_context.getString( fmtID ); + if ( null == result ) { + result = m_context.getString( fmtID ); + } break; } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java index 0101aacde..74281c718 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java @@ -228,6 +228,7 @@ public class XwJNI { public static native void comms_start( int gamePtr ); public static native void comms_resetSame( int gamePtr ); public static native void comms_getAddr( int gamePtr, CommsAddrRec addr ); + public static native CommsAddrRec[] comms_getAddrs( int gamePtr ); public static native void comms_setAddr( int gamePtr, CommsAddrRec addr ); public static native void comms_resendAll( int gamePtr ); public static native void comms_ackAny( int gamePtr ); diff --git a/xwords4/common/comms.c b/xwords4/common/comms.c index 8ccd36450..413799d70 100644 --- a/xwords4/common/comms.c +++ b/xwords4/common/comms.c @@ -785,6 +785,19 @@ comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr ) } /* comms_setAddr */ +void +comms_getAddrs( const CommsCtxt* comms, CommsAddrRec addr[], XP_U16* nRecs ) +{ + AddressRecord* recs; + XP_U16 count; + for ( count = 0, recs = comms->recs; + count < *nRecs && !!recs; + ++count, recs = recs->next ) { + XP_MEMCPY( &addr[count], &recs->addr, sizeof(addr[count]) ); + } + *nRecs = count; +} + #ifdef XWFEATURE_RELAY static XP_Bool haveRelayID( const CommsCtxt* comms ) @@ -1778,24 +1791,6 @@ comms_isConnected( const CommsCtxt* const comms ) return result; } -#ifdef XWFEATURE_BLUETOOTH -void -comms_getBTAddrs( const CommsCtxt* const comms, - XP_BtAddrStr* addrs, XP_U16* count ) -{ - XP_ASSERT( COMMS_CONN_BT == comms->addr.conType ); - - const AddressRecord* rec; - XP_U16 ii = 0; - for ( rec = comms->recs; !!rec; rec = rec->next ) { - XP_MEMCPY( &addrs[ii], &rec->addr.u.bt.btAddr, sizeof( addrs[ii] ) ); - ++ii; - } - XP_ASSERT( ii <= *count ); - *count = ii; -} -#endif - #if defined COMMS_HEARTBEAT || defined XWFEATURE_COMMSACK static void sendEmptyMsg( CommsCtxt* comms, AddressRecord* rec ) diff --git a/xwords4/common/comms.h b/xwords4/common/comms.h index 96d00dd3f..78aa361a2 100644 --- a/xwords4/common/comms.h +++ b/xwords4/common/comms.h @@ -184,6 +184,8 @@ XP_Bool comms_checkAddr( DeviceRole role, const CommsAddrRec* addr, void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr ); void comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr ); +void comms_getAddrs( const CommsCtxt* comms, CommsAddrRec addr[], + XP_U16* nRecs ); #ifdef XWFEATURE_RELAY XP_Bool comms_getRelayID( const CommsCtxt* comms, XP_UCHAR* buf, XP_U16* len ); @@ -214,11 +216,6 @@ XP_Bool comms_checkComplete( const CommsAddrRec* const addr ); XP_Bool comms_canChat( const CommsCtxt* comms ); XP_Bool comms_isConnected( const CommsCtxt* const comms ); -#ifdef XWFEATURE_BLUETOOTH -void comms_getBTAddrs( const CommsCtxt* const comms, - XP_BtAddrStr* addrs, XP_U16* count ); -#endif - # ifdef DEBUG void comms_getStats( CommsCtxt* comms, XWStreamCtxt* stream ); const char* ConnType2Str( CommsConnType typ );