save net status state on a timer (when handler is available) waiting

five seconds.
This commit is contained in:
Eric House 2012-07-10 06:21:49 -07:00
parent bd70a1dd7d
commit c2956f4064
4 changed files with 67 additions and 31 deletions

View file

@ -882,7 +882,8 @@ public class BoardActivity extends XWActivity
// m_jniThread.handle( JNICmd.CMD_DRAW_BT_STATUS, accepted );
// }
ConnStatusHandler.
updateStatusIn(this, CommsAddrRec.CommsConnType.COMMS_CONN_BT,
updateStatusIn(this, m_handler,
CommsAddrRec.CommsConnType.COMMS_CONN_BT,
MultiService.MultiEvent.MESSAGE_ACCEPTED == event);
break;
case MESSAGE_NOGAME:
@ -1611,7 +1612,7 @@ public class BoardActivity extends XWActivity
m_view.startHandling( this, m_jniThread, m_jniGamePtr, m_gi,
m_connType );
if ( null != m_xport ) {
m_xport.setReceiver( m_jniThread );
m_xport.setReceiver( m_jniThread, m_handler );
}
m_jniThread.handle( JNICmd.CMD_START );

View file

@ -48,7 +48,8 @@ public class CommsTransport implements TransportProcs,
private CommsAddrRec m_relayAddr;
private JNIThread m_jniThread;
private CommsThread m_thread;
private TransportProcs.TPMsgHandler m_handler;
private TransportProcs.TPMsgHandler m_tpHandler;
private Handler m_handler;
private boolean m_done = false;
private Vector<ByteBuffer> m_buffersOut;
@ -67,7 +68,7 @@ public class CommsTransport implements TransportProcs,
{
m_jniGamePtr = jniGamePtr;
m_context = context;
m_handler = handler;
m_tpHandler = handler;
m_buffersOut = new Vector<ByteBuffer>();
m_bytesIn = ByteBuffer.allocate( 2048 );
@ -202,9 +203,10 @@ public class CommsTransport implements TransportProcs,
} // loop
}
public void setReceiver( JNIThread jnit )
public void setReceiver( JNIThread jnit, Handler handler )
{
m_jniThread = jnit;
m_handler = handler;
}
public void waitToStop()
@ -407,21 +409,21 @@ public class CommsTransport implements TransportProcs,
case COMMS_RELAYSTATE_UNCONNECTED:
case COMMS_RELAYSTATE_DENIED:
case COMMS_RELAYSTATE_CONNECT_PENDING:
ConnStatusHandler.updateStatusOut( m_context,
ConnStatusHandler.updateStatusOut( m_context, m_handler,
CommsConnType.COMMS_CONN_RELAY,
false );
ConnStatusHandler.updateStatusIn( m_context,
ConnStatusHandler.updateStatusIn( m_context, m_handler,
CommsConnType.COMMS_CONN_RELAY,
false );
break;
case COMMS_RELAYSTATE_CONNECTED:
case COMMS_RELAYSTATE_RECONNECTED:
ConnStatusHandler.updateStatusOut( m_context,
ConnStatusHandler.updateStatusOut( m_context, m_handler,
CommsConnType.COMMS_CONN_RELAY,
true );
break;
case COMMS_RELAYSTATE_ALLCONNECTED:
ConnStatusHandler.updateStatusIn( m_context,
ConnStatusHandler.updateStatusIn( m_context, m_handler,
CommsConnType.COMMS_CONN_RELAY,
true );
break;
@ -431,12 +433,12 @@ public class CommsTransport implements TransportProcs,
public void relayConnd( String room, int devOrder, boolean allHere,
int nMissing )
{
m_handler.tpmRelayConnd( room, devOrder, allHere, nMissing );
m_tpHandler.tpmRelayConnd( room, devOrder, allHere, nMissing );
}
public void relayErrorProc( XWRELAY_ERROR relayErr )
{
m_handler.tpmRelayErrorProc( relayErr );
m_tpHandler.tpmRelayErrorProc( relayErr );
}
public boolean relayNoConnProc( byte[] buf, String relayID )

View file

@ -123,6 +123,7 @@ public class ConnStatusHandler {
private static HashMap<CommsConnType,SuccessRecord[]> s_records =
new HashMap<CommsConnType,SuccessRecord[]>();
private static Object s_lockObj = new Object();
private static boolean s_needsSave = false;
public static void setRect( int left, int top, int right, int bottom )
{
@ -210,7 +211,7 @@ public class ConnStatusHandler {
}
}
public static void updateStatusIn( Context context,
public static void updateStatusIn( Context context, Handler handler,
CommsConnType connType, boolean success )
{
synchronized( s_lockObj ) {
@ -218,10 +219,10 @@ public class ConnStatusHandler {
record.update( success );
}
invalidateParent();
saveState( context );
saveState( context, handler );
}
public static void updateStatusOut( Context context,
public static void updateStatusOut( Context context, Handler handler,
CommsConnType connType, boolean success )
{
synchronized( s_lockObj ) {
@ -229,7 +230,7 @@ public class ConnStatusHandler {
record.update( success );
}
invalidateParent();
saveState( context );
saveState( context, handler );
}
public static void draw( Canvas canvas, Resources res,
@ -319,20 +320,26 @@ public class ConnStatusHandler {
}
}
private static void saveState( Context context )
private static void saveState( final Context context, Handler handler )
{
DbgUtils.logf( "saveState called; need to coalesce these!!!" );
synchronized( s_lockObj ) {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream( bas );
out.writeObject(s_records);
out.flush();
String as64 = XwJNI.base64Encode( bas.toByteArray() );
CommonPrefs.setPrefsString( context,
R.string.key_connstat_data, as64 );
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "loadState: %s", ioe.toString() );
if ( null == handler ) {
doSave( context );
} else {
boolean savePending;
synchronized( s_lockObj ) {
savePending = s_needsSave;
if ( !savePending ) {
s_needsSave = true;
}
}
if ( !savePending ) {
Runnable proc = new Runnable() {
public void run() {
doSave( context );
}
};
handler.postDelayed( proc, 5000 );
}
}
}
@ -376,4 +383,29 @@ public class ConnStatusHandler {
return records[isIn?0:1];
}
private static void doSave( Context context )
{
synchronized( s_lockObj ) {
DbgUtils.logf( "ConnStatusHandler:doSave() doing save" );
ByteArrayOutputStream bas
= new ByteArrayOutputStream();
try {
ObjectOutputStream out
= new ObjectOutputStream( bas );
out.writeObject(s_records);
out.flush();
String as64 =
XwJNI.base64Encode( bas.toByteArray() );
CommonPrefs.
setPrefsString( context,
R.string.key_connstat_data,
as64 );
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "loadState: %s",
ioe.toString() );
}
s_needsSave = false;
}
}
}

View file

@ -240,8 +240,8 @@ public class SMSService extends Service {
case HANDLE:
++m_nReceived;
ConnStatusHandler.
updateStatusIn( this, CommsConnType.COMMS_CONN_SMS,
true );
updateStatusIn( this, null,
CommsConnType.COMMS_CONN_SMS, true );
if ( s_showToasts ) {
DbgUtils.showf( this, "got %dth msg", m_nReceived );
}
@ -544,7 +544,8 @@ public class SMSService extends Service {
ee.toString() );
}
ConnStatusHandler.updateStatusOut( this, CommsConnType.COMMS_CONN_SMS,
ConnStatusHandler.updateStatusOut( this, null,
CommsConnType.COMMS_CONN_SMS,
success );
return success;
}