diff --git a/xwords4/android/XWords4/res/values/common_rsrc.xml b/xwords4/android/XWords4/res/values/common_rsrc.xml
index 3a7c304ec..081188b81 100644
--- a/xwords4/android/XWords4/res/values/common_rsrc.xml
+++ b/xwords4/android/XWords4/res/values/common_rsrc.xml
@@ -68,8 +68,11 @@
key_sms_phones
key_connstat_data
key_dev_id
+
key_gcmvers_regid
key_relay_regid
+ key_relay_regid_ackd
+
key_checked_sms
key_default_group
key_group_posns
diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java
index 53f10ed3d..6d18e323d 100644
--- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java
+++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java
@@ -53,6 +53,7 @@ public class GCMIntentService extends GCMBaseIntentService {
{
DbgUtils.logf( "GCMIntentService.onUnregistered(%s)", regId );
XWPrefs.clearGCMDevID( context );
+ RelayService.devIDChanged();
notifyRelayService( false );
}
diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java
index 814a4b410..3187dcf24 100644
--- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java
+++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java
@@ -82,6 +82,7 @@ public class RelayService extends XWService
private static HashSet s_packetsSent = new HashSet();
private static int s_nextPacketID = 1;
private static boolean s_gcmWorking = false;
+ private static boolean s_registered = false;
private Thread m_fetchThread = null;
private Thread m_UDPReadThread = null;
@@ -125,8 +126,8 @@ public class RelayService extends XWService
if ( s_gcmWorking != confirmed ) {
DbgUtils.logf( "RelayService.gcmConfirmed(): changing "
+ "s_gcmWorking to %b", confirmed );
+ s_gcmWorking = confirmed;
}
- s_gcmWorking = confirmed;
}
public static void startService( Context context )
@@ -164,6 +165,11 @@ public class RelayService extends XWService
return result;
}
+ public static void devIDChanged()
+ {
+ s_registered = false;
+ }
+
// Exists to get incoming data onto the main thread
private static void postData( Context context, long rowid, byte[] msg )
{
@@ -290,7 +296,7 @@ public class RelayService extends XWService
DbgUtils.logf( "not connecting: no network" );
} else if ( startFetchThreadIf() ) {
// do nothing
- } else {
+ } else if ( registerWithRelayIfNot() ) {
requestMessages();
}
break;
@@ -555,6 +561,7 @@ public class RelayService extends XWService
str = getVLIString( dis );
DbgUtils.logf( "bad relayID \"%s\" reported", str );
XWPrefs.clearRelayDevID( this );
+ s_registered = false;
registerWithRelay();
break;
case XWPDEV_REGRSP:
@@ -564,6 +571,7 @@ public class RelayService extends XWService
maxIntervalSeconds );
setMaxIntervalSeconds( maxIntervalSeconds );
XWPrefs.setRelayDevID( this, str );
+ s_registered = true;
break;
case XWPDEV_HAVEMSGS:
requestMessages();
@@ -610,18 +618,50 @@ public class RelayService extends XWService
gotPacket( data, false );
} // gotPacket
+ private boolean shouldRegister()
+ {
+ String relayID = XWPrefs.getRelayDevID( this );
+ boolean registered = null != relayID;
+ if ( registered ) {
+ registered = XWPrefs
+ .getPrefsBoolean( this, R.string.key_relay_regid_ackd, false );
+ }
+ DbgUtils.logf( "shouldRegister()=>%b", !registered );
+ return !registered;
+ }
+
+ // Register: pass both the relay-assigned relayID (or empty string
+ // if none has been assigned yet) and the deviceID IFF it's
+ // changed since we last registered (Otherwise just ID_TYPE_NONE
+ // and no string)
+ //
+ // How do we know if we need to register? We keep a timestamp
+ // indicating when we last got a reg-response. When the GCM id
+ // changes, that timestamp is cleared.
private void registerWithRelay()
{
- DevIDType[] typ = new DevIDType[1];
- String devid = getDevID( typ );
+ boolean ackd =
+ XWPrefs.getPrefsBoolean( this, R.string.key_relay_regid_ackd,
+ false );
+ String relayID = XWPrefs.getRelayDevID( this );
+ DevIDType[] typa = new DevIDType[1];
+ String devid = getDevID( typa );
+ DevIDType typ = typa[0];
ByteArrayOutputStream bas = new ByteArrayOutputStream();
try {
DataOutputStream out = new DataOutputStream( bas );
- out.writeByte( typ[0].ordinal() );
- writeVLIString( out, devid );
+
+ writeVLIString( out, relayID ); // may be empty
+ if ( ackd && DevIDType.ID_TYPE_RELAY == typ ) { // all's well
+ out.writeByte( DevIDType.ID_TYPE_NONE.ordinal() );
+ } else {
+ out.writeByte( typ.ordinal() );
+ writeVLIString( out, devid );
+ }
+
DbgUtils.logf( "registering devID \"%s\" (type=%s)", devid,
- typ[0].toString() );
+ typ.toString() );
out.writeShort( GitVersion.CLIENT_VERS_RELAY );
writeVLIString( out, GitVersion.VERS );
@@ -634,6 +674,14 @@ public class RelayService extends XWService
}
}
+ private boolean registerWithRelayIfNot()
+ {
+ if ( !s_registered && shouldRegister() ) {
+ registerWithRelay();
+ }
+ return s_registered;
+ }
+
private void requestMessagesImpl( XWRelayReg reg )
{
ByteArrayOutputStream bas = new ByteArrayOutputStream();
@@ -654,10 +702,10 @@ public class RelayService extends XWService
requestMessagesImpl( XWRelayReg.XWPDEV_RQSTMSGS );
}
- private void sendKeepAlive()
- {
- requestMessagesImpl( XWRelayReg.XWPDEV_KEEPALIVE );
- }
+ // private void sendKeepAlive()
+ // {
+ // requestMessagesImpl( XWRelayReg.XWPDEV_KEEPALIVE );
+ // }
private void sendMessage( long rowid, byte[] msg )
{
@@ -1063,6 +1111,9 @@ public class RelayService extends XWService
private static void writeVLIString( DataOutputStream os, String str )
throws java.io.IOException
{
+ if ( null == str ) {
+ str = "";
+ }
int len = str.length();
un2vli( len, os );
os.writeBytes( str );
diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java
index 1491c52ff..1cfecaaea 100644
--- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java
+++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java
@@ -225,7 +225,7 @@ public class XWPrefs {
{
int curVers = Utils.getAppVersion( context );
setPrefsInt( context, R.string.key_gcmvers_regid, curVers );
- clearPrefsKey( context, R.string.key_relay_regid );
+ setPrefsBoolean( context, R.string.key_relay_regid_ackd, false );
}
public static String getGCMDevID( Context context )
@@ -243,7 +243,7 @@ public class XWPrefs {
public static void clearGCMDevID( Context context )
{
- clearRelayDevID( context );
+ setPrefsBoolean( context, R.string.key_relay_regid_ackd, false );
}
public static String getRelayDevID( Context context )
@@ -258,6 +258,7 @@ public class XWPrefs {
public static void setRelayDevID( Context context, String idRelay )
{
setPrefsString( context, R.string.key_relay_regid, idRelay );
+ setPrefsBoolean( context, R.string.key_relay_regid_ackd, true );
}
public static void clearRelayDevID( Context context )