Merge remote branch 'origin/android_branch' into android_branch

This commit is contained in:
Eric House 2013-09-22 20:36:38 -07:00
commit 4de9df4c97
4 changed files with 69 additions and 13 deletions

View file

@ -68,8 +68,11 @@
<string name="key_sms_phones">key_sms_phones</string> <string name="key_sms_phones">key_sms_phones</string>
<string name="key_connstat_data">key_connstat_data</string> <string name="key_connstat_data">key_connstat_data</string>
<string name="key_dev_id">key_dev_id</string> <string name="key_dev_id">key_dev_id</string>
<string name="key_gcmvers_regid">key_gcmvers_regid</string> <string name="key_gcmvers_regid">key_gcmvers_regid</string>
<string name="key_relay_regid">key_relay_regid</string> <string name="key_relay_regid">key_relay_regid</string>
<string name="key_relay_regid_ackd">key_relay_regid_ackd</string>
<string name="key_checked_sms">key_checked_sms</string> <string name="key_checked_sms">key_checked_sms</string>
<string name="key_default_group">key_default_group</string> <string name="key_default_group">key_default_group</string>
<string name="key_group_posns">key_group_posns</string> <string name="key_group_posns">key_group_posns</string>

View file

@ -53,6 +53,7 @@ public class GCMIntentService extends GCMBaseIntentService {
{ {
DbgUtils.logf( "GCMIntentService.onUnregistered(%s)", regId ); DbgUtils.logf( "GCMIntentService.onUnregistered(%s)", regId );
XWPrefs.clearGCMDevID( context ); XWPrefs.clearGCMDevID( context );
RelayService.devIDChanged();
notifyRelayService( false ); notifyRelayService( false );
} }

View file

@ -82,6 +82,7 @@ public class RelayService extends XWService
private static HashSet<Integer> s_packetsSent = new HashSet<Integer>(); private static HashSet<Integer> s_packetsSent = new HashSet<Integer>();
private static int s_nextPacketID = 1; private static int s_nextPacketID = 1;
private static boolean s_gcmWorking = false; private static boolean s_gcmWorking = false;
private static boolean s_registered = false;
private Thread m_fetchThread = null; private Thread m_fetchThread = null;
private Thread m_UDPReadThread = null; private Thread m_UDPReadThread = null;
@ -125,8 +126,8 @@ public class RelayService extends XWService
if ( s_gcmWorking != confirmed ) { if ( s_gcmWorking != confirmed ) {
DbgUtils.logf( "RelayService.gcmConfirmed(): changing " DbgUtils.logf( "RelayService.gcmConfirmed(): changing "
+ "s_gcmWorking to %b", confirmed ); + "s_gcmWorking to %b", confirmed );
s_gcmWorking = confirmed;
} }
s_gcmWorking = confirmed;
} }
public static void startService( Context context ) public static void startService( Context context )
@ -164,6 +165,11 @@ public class RelayService extends XWService
return result; return result;
} }
public static void devIDChanged()
{
s_registered = false;
}
// Exists to get incoming data onto the main thread // Exists to get incoming data onto the main thread
private static void postData( Context context, long rowid, byte[] msg ) 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" ); DbgUtils.logf( "not connecting: no network" );
} else if ( startFetchThreadIf() ) { } else if ( startFetchThreadIf() ) {
// do nothing // do nothing
} else { } else if ( registerWithRelayIfNot() ) {
requestMessages(); requestMessages();
} }
break; break;
@ -555,6 +561,7 @@ public class RelayService extends XWService
str = getVLIString( dis ); str = getVLIString( dis );
DbgUtils.logf( "bad relayID \"%s\" reported", str ); DbgUtils.logf( "bad relayID \"%s\" reported", str );
XWPrefs.clearRelayDevID( this ); XWPrefs.clearRelayDevID( this );
s_registered = false;
registerWithRelay(); registerWithRelay();
break; break;
case XWPDEV_REGRSP: case XWPDEV_REGRSP:
@ -564,6 +571,7 @@ public class RelayService extends XWService
maxIntervalSeconds ); maxIntervalSeconds );
setMaxIntervalSeconds( maxIntervalSeconds ); setMaxIntervalSeconds( maxIntervalSeconds );
XWPrefs.setRelayDevID( this, str ); XWPrefs.setRelayDevID( this, str );
s_registered = true;
break; break;
case XWPDEV_HAVEMSGS: case XWPDEV_HAVEMSGS:
requestMessages(); requestMessages();
@ -610,18 +618,50 @@ public class RelayService extends XWService
gotPacket( data, false ); gotPacket( data, false );
} // gotPacket } // 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() private void registerWithRelay()
{ {
DevIDType[] typ = new DevIDType[1]; boolean ackd =
String devid = getDevID( typ ); 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(); ByteArrayOutputStream bas = new ByteArrayOutputStream();
try { try {
DataOutputStream out = new DataOutputStream( bas ); 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, DbgUtils.logf( "registering devID \"%s\" (type=%s)", devid,
typ[0].toString() ); typ.toString() );
out.writeShort( GitVersion.CLIENT_VERS_RELAY ); out.writeShort( GitVersion.CLIENT_VERS_RELAY );
writeVLIString( out, GitVersion.VERS ); 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 ) private void requestMessagesImpl( XWRelayReg reg )
{ {
ByteArrayOutputStream bas = new ByteArrayOutputStream(); ByteArrayOutputStream bas = new ByteArrayOutputStream();
@ -654,10 +702,10 @@ public class RelayService extends XWService
requestMessagesImpl( XWRelayReg.XWPDEV_RQSTMSGS ); requestMessagesImpl( XWRelayReg.XWPDEV_RQSTMSGS );
} }
private void sendKeepAlive() // private void sendKeepAlive()
{ // {
requestMessagesImpl( XWRelayReg.XWPDEV_KEEPALIVE ); // requestMessagesImpl( XWRelayReg.XWPDEV_KEEPALIVE );
} // }
private void sendMessage( long rowid, byte[] msg ) private void sendMessage( long rowid, byte[] msg )
{ {
@ -1063,6 +1111,9 @@ public class RelayService extends XWService
private static void writeVLIString( DataOutputStream os, String str ) private static void writeVLIString( DataOutputStream os, String str )
throws java.io.IOException throws java.io.IOException
{ {
if ( null == str ) {
str = "";
}
int len = str.length(); int len = str.length();
un2vli( len, os ); un2vli( len, os );
os.writeBytes( str ); os.writeBytes( str );

View file

@ -225,7 +225,7 @@ public class XWPrefs {
{ {
int curVers = Utils.getAppVersion( context ); int curVers = Utils.getAppVersion( context );
setPrefsInt( context, R.string.key_gcmvers_regid, curVers ); 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 ) public static String getGCMDevID( Context context )
@ -243,7 +243,7 @@ public class XWPrefs {
public static void clearGCMDevID( Context context ) public static void clearGCMDevID( Context context )
{ {
clearRelayDevID( context ); setPrefsBoolean( context, R.string.key_relay_regid_ackd, false );
} }
public static String getRelayDevID( Context context ) public static String getRelayDevID( Context context )
@ -258,6 +258,7 @@ public class XWPrefs {
public static void setRelayDevID( Context context, String idRelay ) public static void setRelayDevID( Context context, String idRelay )
{ {
setPrefsString( context, R.string.key_relay_regid, idRelay ); setPrefsString( context, R.string.key_relay_regid, idRelay );
setPrefsBoolean( context, R.string.key_relay_regid_ackd, true );
} }
public static void clearRelayDevID( Context context ) public static void clearRelayDevID( Context context )