@@ -86,8 +86,7 @@ public class TransportBroker {
8686 private Context currentContext = null ;
8787
8888 private final Object INIT_LOCK = new Object ();
89-
90- private TransportType queuedOnTransportConnect = null ;
89+ private final Object MESSAGE_SEND_LOCK = new Object ();
9190
9291 Messenger routerServiceMessenger = null ;
9392 final Messenger clientMessenger ;
@@ -126,59 +125,55 @@ public void onServiceDisconnected(ComponentName className) {
126125 };
127126 }
128127
129- protected synchronized boolean sendMessageToRouterService (Message message ) {
128+ protected boolean sendMessageToRouterService (Message message ) {
130129 return sendMessageToRouterService (message , 0 );
131130 }
132131
133- protected synchronized boolean sendMessageToRouterService (Message message , int retryCount ) {
134- if (message == null ) {
135- Log .w (TAG , "Attempted to send null message" );
136- return false ;
137- }
138- //Log.i(TAG, "Attempting to send message type - " + message.what);
139- if (isBound && routerServiceMessenger != null ) {
140- if (registeredWithRouterService
141- || message .what == TransportConstants .ROUTER_REGISTER_CLIENT ) { //We can send a message if we are registered or are attempting to register
142- try {
143- routerServiceMessenger .send (message );
144- return true ;
145- } catch (RemoteException e ) {
146- e .printStackTrace ();
147- //Let's check to see if we should retry
148- if ((Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH_MR1 && e instanceof TransactionTooLargeException )
149- || (retryCount < 5 && routerServiceMessenger .getBinder ().isBinderAlive () && routerServiceMessenger .getBinder ().pingBinder ())) { //We probably just failed on a small transaction =\
150- try {
151- Thread .sleep (100 );
152- } catch (InterruptedException e1 ) {
153- e1 .printStackTrace ();
132+ protected boolean sendMessageToRouterService (Message message , int retryCount ) {
133+ synchronized (MESSAGE_SEND_LOCK ) {
134+ if (message == null ) {
135+ Log .w (TAG , "Attempted to send null message" );
136+ return false ;
137+ }
138+ //Log.i(TAG, "Attempting to send message type - " + message.what);
139+ if (isBound && routerServiceMessenger != null && routerServiceMessenger .getBinder () != null && routerServiceMessenger .getBinder ().isBinderAlive ()) {
140+ if (registeredWithRouterService
141+ || message .what == TransportConstants .ROUTER_REGISTER_CLIENT ) { //We can send a message if we are registered or are attempting to register
142+ try {
143+ routerServiceMessenger .send (message );
144+ return true ;
145+ } catch (RemoteException e ) {
146+ e .printStackTrace ();
147+ //Let's check to see if we should retry
148+ if ((Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH_MR1 && e instanceof TransactionTooLargeException )
149+ || (retryCount < 5 && routerServiceMessenger .getBinder ().isBinderAlive () && routerServiceMessenger .getBinder ().pingBinder ())) { //We probably just failed on a small transaction =\
150+ try {
151+ Thread .sleep (100 );
152+ } catch (InterruptedException e1 ) {
153+ e1 .printStackTrace ();
154+ }
155+ return sendMessageToRouterService (message , retryCount ++);
156+ } else {
157+ //DeadObject, time to kill our connection
158+ Log .d (TAG , "Dead object while attempting to send packet" );
159+ stop ();
160+ onHardwareDisconnected (null , null );
161+ return false ;
154162 }
155- return sendMessageToRouterService (message , retryCount ++);
156- } else {
157- //DeadObject, time to kill our connection
158- Log .d (TAG , "Dead object while attempting to send packet" );
159- routerServiceMessenger = null ;
160- registeredWithRouterService = false ;
161- unBindFromRouterService ();
162- isBound = false ;
163+ } catch (NullPointerException e ) {
164+ Log .d (TAG , "Null messenger while attempting to send packet" ); // NPE, routerServiceMessenger is null
165+ stop ();
163166 onHardwareDisconnected (null , null );
164167 return false ;
165168 }
166- } catch (NullPointerException e ) {
167- Log .d (TAG , "Null messenger while attempting to send packet" ); // NPE, routerServiceMessenger is null
168- routerServiceMessenger = null ;
169- registeredWithRouterService = false ;
170- unBindFromRouterService ();
171- isBound = false ;
172- onHardwareDisconnected (null , null );
169+ } else {
170+ Log .e (TAG , "Unable to send message to router service. Not registered." );
173171 return false ;
174172 }
175173 } else {
176- Log .e (TAG , "Unable to send message to router service. Not registered ." );
174+ Log .e (TAG , "Unable to send message to router service. Not bound ." );
177175 return false ;
178176 }
179- } else {
180- Log .e (TAG , "Unable to send message to router service. Not bound." );
181- return false ;
182177 }
183178 }
184179
@@ -416,7 +411,6 @@ public TransportBroker(Context context, String appId, ComponentName service) {
416411 }
417412 //this.appId = appId.concat(timeStamp);
418413 this .appId = appId ;
419- queuedOnTransportConnect = null ;
420414 currentContext = context ;
421415 //Log.d(TAG, "Registering our reply receiver: " + whereToReply);
422416 this .routerService = service ;
@@ -448,7 +442,6 @@ public void resetSession() {
448442 synchronized (INIT_LOCK ) {
449443 unregisterWithRouterService ();
450444 routerServiceMessenger = null ;
451- queuedOnTransportConnect = null ;
452445 unBindFromRouterService ();
453446 isBound = false ;
454447 }
@@ -463,13 +456,12 @@ public void stop() {
463456 unregisterWithRouterService ();
464457 unBindFromRouterService ();
465458 routerServiceMessenger = null ;
466- queuedOnTransportConnect = null ;
467459 currentContext = null ;
468460
469461 }
470462 }
471463
472- private synchronized void unBindFromRouterService () {
464+ private void unBindFromRouterService () {
473465 try {
474466 getContext ().unbindService (routerConnection );
475467
@@ -487,27 +479,17 @@ private synchronized void unBindFromRouterService() {
487479
488480
489481 public void onServiceUnregsiteredFromRouterService (int unregisterCode ) {
490- queuedOnTransportConnect = null ;
491482 }
492483
493484 @ Deprecated
494485 public void onHardwareDisconnected (TransportType type ) {
495- routerServiceDisconnect ();
486+ stop ();
496487 }
497488
498489 public void onHardwareDisconnected (TransportRecord record , List <TransportRecord > connectedTransports ) {
499490
500491 }
501492
502- private void routerServiceDisconnect () {
503- synchronized (INIT_LOCK ) {
504- unBindFromRouterService ();
505- routerServiceMessenger = null ;
506- routerConnection = null ;
507- queuedOnTransportConnect = null ;
508- }
509- }
510-
511493 /**
512494 * WILL NO LONGER BE CALLED
513495 *
@@ -518,7 +500,6 @@ private void routerServiceDisconnect() {
518500 public boolean onHardwareConnected (TransportType type ) {
519501 synchronized (INIT_LOCK ) {
520502 if (routerServiceMessenger == null ) {
521- queuedOnTransportConnect = type ;
522503 return false ;
523504 }
524505 return true ;
@@ -528,7 +509,6 @@ public boolean onHardwareConnected(TransportType type) {
528509 public boolean onHardwareConnected (List <TransportRecord > transports ) {
529510 synchronized (INIT_LOCK ) {
530511 if (routerServiceMessenger == null && transports != null && transports .size () > 0 ) {
531- queuedOnTransportConnect = transports .get (transports .size () - 1 ).getType ();
532512 return false ;
533513 }
534514 return true ;
@@ -616,7 +596,7 @@ public boolean sendPacketToRouterService(SdlPacket packet) { //We use ints becau
616596 ByteArrayMessageSpliter splitter = new ByteArrayMessageSpliter (appId , TransportConstants .ROUTER_SEND_PACKET , bytes , packet .getPrioirtyCoefficient ());
617597 splitter .setRouterServiceVersion (routerServiceVersion );
618598 splitter .setTransportRecord (packet .getTransportRecord ());
619- while (splitter .isActive ()) {
599+ while (splitter .isActive () && routerServiceMessenger != null ) {
620600 sendMessageToRouterService (splitter .nextMessage ());
621601 }
622602 return splitter .close ();
0 commit comments