Skip to content

Commit 5a5ad93

Browse files
authored
Merge pull request #374 from smartdevicelink/develop
Pull in PR #367 as hotfix into master
2 parents fa3031b + a2264a5 commit 5a5ad93

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

sdl_android_lib/src/com/smartdevicelink/transport/USBTransport.java

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import com.smartdevicelink.exception.SdlExceptionCause;
2020
import com.smartdevicelink.protocol.SdlPacket;
2121
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;
2623
import com.smartdevicelink.transport.enums.TransportType;
2724
import com.smartdevicelink.util.DebugTool;
2825

@@ -37,7 +34,10 @@
3734
*/
3835
@SuppressLint("NewApi")
3936
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+
/**
4141
* Broadcast action: sent when a USB accessory is attached.
4242
*
4343
* UsbManager.EXTRA_ACCESSORY extra contains UsbAccessory object that has
@@ -278,7 +278,6 @@ public void openConnection() throws SdlException {
278278
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
279279
filter.addAction(ACTION_USB_PERMISSION);
280280
getContext().registerReceiver(mUSBReceiver, filter);
281-
282281
initializeAccessory();
283282
} catch (Exception e) {
284283
String msg = "Couldn't start opening connection";
@@ -353,6 +352,14 @@ private void stopReaderThread() {
353352
* @param ex Disconnect exception, if any
354353
*/
355354
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+
356363
final State state = getState();
357364
switch (state) {
358365
case LISTENING:
@@ -372,6 +379,7 @@ private void disconnect(String msg, Exception ex) {
372379
if (mOutputStream != null) {
373380
try {
374381
mOutputStream.close();
382+
mOutputStream = null;
375383
} catch (IOException e) {
376384
logW("Can't close output stream", e);
377385
mOutputStream = null;
@@ -380,6 +388,7 @@ private void disconnect(String msg, Exception ex) {
380388
if (mInputStream != null) {
381389
try {
382390
mInputStream.close();
391+
mInputStream = null;
383392
} catch (IOException e) {
384393
logW("Can't close input stream", e);
385394
mInputStream = null;
@@ -388,6 +397,7 @@ private void disconnect(String msg, Exception ex) {
388397
if (mParcelFD != null) {
389398
try {
390399
mParcelFD.close();
400+
mParcelFD = null;
391401
} catch (IOException e) {
392402
logW("Can't close file descriptor", e);
393403
mParcelFD = null;
@@ -428,6 +438,7 @@ private void disconnect(String msg, Exception ex) {
428438
"; doing nothing");
429439
break;
430440
}
441+
_disconnecting = false;
431442
}
432443

433444
/**
@@ -440,31 +451,34 @@ private void disconnect(String msg, Exception ex) {
440451
public TransportType getTransportType() {
441452
return TransportType.USB;
442453
}
443-
454+
444455
/**
445456
* Looks for an already connected compatible accessory and connect to it.
446457
*/
447458
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");
449464
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);
468482
}
469483

470484
/**
@@ -647,7 +661,7 @@ private enum State {
647661
* Internal task that connects to and reads data from a USB accessory.
648662
*
649663
* 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
651665
* to variables of USBTransport must be surrounded with
652666
* synchronized (USBTransport.this) { … }
653667
*/
@@ -728,8 +742,8 @@ private boolean connect() {
728742
}
729743

730744
logI("Accessory opened!");
731-
732-
synchronized (USBTransport.this) {
745+
746+
synchronized (USBTransport.this) {
733747
setState(State.CONNECTED);
734748
handleTransportConnected();
735749
}
@@ -758,7 +772,10 @@ private void readFromTransport() {
758772
// read loop
759773
while (!isInterrupted()) {
760774
try {
761-
bytesRead = mInputStream.read(buffer);
775+
if (mInputStream == null)
776+
continue;
777+
778+
bytesRead = mInputStream.read(buffer);
762779
if (bytesRead == -1) {
763780
if (isInterrupted()) {
764781
logI("EOF reached, and thread is interrupted");

sdl_android_lib/src/com/smartdevicelink/transport/USBTransportConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class USBTransportConfig extends BaseTransportConfig {
99

1010
private Context mainActivity = null;
1111
private UsbAccessory usbAccessory = null;
12+
private Boolean queryUsbAcc = true;
1213

1314
public USBTransportConfig (Context mainActivity) {
1415
this.mainActivity = mainActivity;
@@ -19,6 +20,23 @@ public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory) {
1920
this.usbAccessory = usbAccessory;
2021
}
2122

23+
public USBTransportConfig (Context mainActivity, boolean shareConnection, boolean queryUsbAcc) {
24+
this.mainActivity = mainActivity;
25+
this.queryUsbAcc = queryUsbAcc;
26+
super.shareConnection = shareConnection;
27+
}
28+
29+
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory, boolean shareConnection, boolean queryUsbAcc) {
30+
this.mainActivity = mainActivity;
31+
this.queryUsbAcc = queryUsbAcc;
32+
this.usbAccessory = usbAccessory;
33+
super.shareConnection = shareConnection;
34+
}
35+
36+
public Boolean getQueryUsbAcc () {
37+
return queryUsbAcc;
38+
}
39+
2240
public Context getUSBContext () {
2341
return mainActivity;
2442
}

0 commit comments

Comments
 (0)