19
19
import com .smartdevicelink .exception .SdlExceptionCause ;
20
20
import com .smartdevicelink .protocol .SdlPacket ;
21
21
import com .smartdevicelink .trace .SdlTrace ;
22
- import com .smartdevicelink .trace .enums .InterfaceActivityDirection ;
23
- import com .smartdevicelink .transport .ITransportListener ;
24
- import com .smartdevicelink .transport .SdlTransport ;
25
- import com .smartdevicelink .transport .SiphonServer ;
22
+ import com .smartdevicelink .trace .enums .InterfaceActivityDirection ;
26
23
import com .smartdevicelink .transport .enums .TransportType ;
27
24
import com .smartdevicelink .util .DebugTool ;
28
25
37
34
*/
38
35
@ SuppressLint ("NewApi" )
39
36
public class USBTransport extends SdlTransport {
40
- /**
37
+
38
+ // Boolean to monitor if the transport is in a disconnecting state
39
+ private boolean _disconnecting = false ;
40
+ /**
41
41
* Broadcast action: sent when a USB accessory is attached.
42
42
*
43
43
* UsbManager.EXTRA_ACCESSORY extra contains UsbAccessory object that has
@@ -278,7 +278,6 @@ public void openConnection() throws SdlException {
278
278
filter .addAction (UsbManager .ACTION_USB_ACCESSORY_DETACHED );
279
279
filter .addAction (ACTION_USB_PERMISSION );
280
280
getContext ().registerReceiver (mUSBReceiver , filter );
281
-
282
281
initializeAccessory ();
283
282
} catch (Exception e ) {
284
283
String msg = "Couldn't start opening connection" ;
@@ -353,6 +352,14 @@ private void stopReaderThread() {
353
352
* @param ex Disconnect exception, if any
354
353
*/
355
354
private void disconnect (String msg , Exception ex ) {
355
+
356
+ // If already disconnecting, return
357
+ if (_disconnecting ) {
358
+ // No need to recursively call
359
+ return ;
360
+ }
361
+ _disconnecting = true ;
362
+
356
363
final State state = getState ();
357
364
switch (state ) {
358
365
case LISTENING :
@@ -372,6 +379,7 @@ private void disconnect(String msg, Exception ex) {
372
379
if (mOutputStream != null ) {
373
380
try {
374
381
mOutputStream .close ();
382
+ mOutputStream = null ;
375
383
} catch (IOException e ) {
376
384
logW ("Can't close output stream" , e );
377
385
mOutputStream = null ;
@@ -380,6 +388,7 @@ private void disconnect(String msg, Exception ex) {
380
388
if (mInputStream != null ) {
381
389
try {
382
390
mInputStream .close ();
391
+ mInputStream = null ;
383
392
} catch (IOException e ) {
384
393
logW ("Can't close input stream" , e );
385
394
mInputStream = null ;
@@ -388,6 +397,7 @@ private void disconnect(String msg, Exception ex) {
388
397
if (mParcelFD != null ) {
389
398
try {
390
399
mParcelFD .close ();
400
+ mParcelFD = null ;
391
401
} catch (IOException e ) {
392
402
logW ("Can't close file descriptor" , e );
393
403
mParcelFD = null ;
@@ -428,6 +438,7 @@ private void disconnect(String msg, Exception ex) {
428
438
"; doing nothing" );
429
439
break ;
430
440
}
441
+ _disconnecting = false ;
431
442
}
432
443
433
444
/**
@@ -440,31 +451,34 @@ private void disconnect(String msg, Exception ex) {
440
451
public TransportType getTransportType () {
441
452
return TransportType .USB ;
442
453
}
443
-
454
+
444
455
/**
445
456
* Looks for an already connected compatible accessory and connect to it.
446
457
*/
447
458
private void initializeAccessory () {
448
- logI ("Looking for connected accessories" );
459
+ if (!mConfig .getQueryUsbAcc ()){
460
+ logI ("Query for accessory is disabled." );
461
+ return ;
462
+ }
463
+ logI ("Looking for connected accessories" );
449
464
UsbAccessory acc = mConfig .getUsbAccessory ();
450
- if (acc == null || !isAccessorySupported (acc )){ //Check to see if our config included an accessory and that it is supported. If not, see if there are any other accessories connected.
451
- UsbManager usbManager = getUsbManager ();
452
- UsbAccessory [] accessories = usbManager .getAccessoryList ();
453
- if (accessories != null ) {
454
- logD ("Found total " + accessories .length + " accessories" );
455
- for (UsbAccessory accessory : accessories ) {
456
- if (isAccessorySupported (accessory )) {
457
- acc = accessory ;
458
- break ;
459
- }
460
- }
461
- } else {
462
- logI ("No connected accessories found" );
463
- return ;
464
- }
465
- }
466
-
467
- connectToAccessory (acc );
465
+ if ( acc == null || !isAccessorySupported (acc )){ //Check to see if our config included an accessory and that it is supported. If not, see if there are any other accessories connected.
466
+ UsbManager usbManager = getUsbManager ();
467
+ UsbAccessory [] accessories = usbManager .getAccessoryList ();
468
+ if (accessories != null ) {
469
+ logD ("Found total " + accessories .length + " accessories" );
470
+ for (UsbAccessory accessory : accessories ) {
471
+ if (isAccessorySupported (accessory )) {
472
+ acc = accessory ;
473
+ break ;
474
+ }
475
+ }
476
+ } else {
477
+ logI ("No connected accessories found" );
478
+ return ;
479
+ }
480
+ }
481
+ connectToAccessory (acc );
468
482
}
469
483
470
484
/**
@@ -647,7 +661,7 @@ private enum State {
647
661
* Internal task that connects to and reads data from a USB accessory.
648
662
*
649
663
* Since the class has to have access to the parent class' variables,
650
- * sdlhronization must be taken in consideration! For now, all access
664
+ * synchronization must be taken in consideration! For now, all access
651
665
* to variables of USBTransport must be surrounded with
652
666
* synchronized (USBTransport.this) { … }
653
667
*/
@@ -728,8 +742,8 @@ private boolean connect() {
728
742
}
729
743
730
744
logI ("Accessory opened!" );
731
-
732
- synchronized (USBTransport .this ) {
745
+
746
+ synchronized (USBTransport .this ) {
733
747
setState (State .CONNECTED );
734
748
handleTransportConnected ();
735
749
}
@@ -758,7 +772,10 @@ private void readFromTransport() {
758
772
// read loop
759
773
while (!isInterrupted ()) {
760
774
try {
761
- bytesRead = mInputStream .read (buffer );
775
+ if (mInputStream == null )
776
+ continue ;
777
+
778
+ bytesRead = mInputStream .read (buffer );
762
779
if (bytesRead == -1 ) {
763
780
if (isInterrupted ()) {
764
781
logI ("EOF reached, and thread is interrupted" );
0 commit comments