reintroduce BroadcastReceiver listening for connectivity going up and

down.  When network's down, return -1 from send proc so that comms
timing can retry.  This works on both cellphone on carrier network
(with airplane mode as the test for loss/gain of network) and on
emulator (with death/restart of relay as the test since I don't know
how to kill the iface itself).  A quick test on phone using wifi this
morning suggested it didn't work, that into/out of airplane mode did
not result in notifications getting set.  Need to test that, and back
this out if can't be fixed since I can't break wifi.
This commit is contained in:
Andy2 2010-12-15 18:39:25 -08:00
parent d1605c4493
commit 647443c03e

View file

@ -31,9 +31,13 @@ import java.util.Vector;
import java.util.Iterator;
import junit.framework.Assert;
import android.telephony.SmsManager;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.PendingIntent;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
@ -80,6 +84,8 @@ public class CommsTransport implements TransportProcs {
private ByteBuffer m_bytesIn;
private Context m_context;
private BroadcastReceiver m_receiver;
private boolean m_netAvail = true;
// assembling inbound packet
private byte[] m_packetIn;
@ -93,6 +99,8 @@ public class CommsTransport implements TransportProcs {
m_handler = handler;
m_buffersOut = new Vector<ByteBuffer>();
m_bytesIn = ByteBuffer.allocate( 2048 );
buildNetAvailReceiver();
}
public class CommsThread extends Thread {
@ -100,6 +108,7 @@ public class CommsTransport implements TransportProcs {
@Override
public void run()
{
m_done = false;
boolean failed = true;
try {
if ( Build.PRODUCT.contains("sdk") ) {
@ -228,17 +237,10 @@ public class CommsTransport implements TransportProcs {
public void waitToStop()
{
m_done = true;
if ( null != m_selector ) {
m_selector.wakeup();
}
if ( null != m_thread ) { // synchronized this? Or use Thread method
try {
m_thread.join(100); // wait up to 1/10 second
} catch ( java.lang.InterruptedException ie ) {
Utils.logf( "got InterruptedException: " + ie.toString() );
}
m_thread = null;
waitToStopImpl();
if ( null != m_receiver ) {
m_context.unregisterReceiver( m_receiver );
m_receiver = null;
}
}
@ -330,6 +332,55 @@ public class CommsTransport implements TransportProcs {
}
}
private class CommsBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive( Context context, Intent intent )
{
if ( intent.getAction().
equals( ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo ni = (NetworkInfo)intent.
getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
boolean netAvail = NetworkInfo.State.CONNECTED == ni.getState();
boolean netAvailDet = NetworkInfo.DetailedState.CONNECTED ==
ni.getDetailedState();
Utils.logf( "CommsTransport::onReceive: netAvail=%s;netAvailDet=%s",
netAvail?"true":"false", netAvailDet?"true":"false" );
m_netAvail = netAvail;
if ( !netAvail ) {
waitToStopImpl();
m_jniThread.handle( JNICmd.CMD_TRANSFAIL );
}
}
}
}
private void buildNetAvailReceiver()
{
m_receiver = new CommsBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction( ConnectivityManager.CONNECTIVITY_ACTION );
Intent intent = m_context.registerReceiver( m_receiver, filter );
}
private void waitToStopImpl()
{
m_done = true; // this is in a race!
if ( null != m_selector ) {
m_selector.wakeup();
}
if ( null != m_thread ) { // synchronized this? Or use Thread method
try {
m_thread.join(100); // wait up to 1/10 second
} catch ( java.lang.InterruptedException ie ) {
Utils.logf( "got InterruptedException: " + ie.toString() );
}
m_thread = null;
}
}
// TransportProcs interface
public int transportSend( byte[] buf, final CommsAddrRec faddr )
{
@ -347,12 +398,14 @@ public class CommsTransport implements TransportProcs {
switch ( m_addr.conType ) {
case COMMS_CONN_RELAY:
putOut( buf ); // add to queue
if ( null == m_thread ) {
m_thread = new CommsThread();
m_thread.start();
if ( m_netAvail ) {
putOut( buf ); // add to queue
if ( null == m_thread ) {
m_thread = new CommsThread();
m_thread.start();
}
nSent = buf.length;
}
nSent = buf.length;
break;
case COMMS_CONN_SMS:
Assert.fail();