diff options
author | Joey Grover <joeygrover@gmail.com> | 2018-10-04 16:27:56 -0400 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2018-10-04 16:27:56 -0400 |
commit | 97a1fd13cd8da90bce2fcae9056b7f07f389cc43 (patch) | |
tree | d8828a9b98cf5524442d168ef67ff1bb3b23041b | |
parent | 655d17916ff0dd504bf5941efd29fbb0a48fd01d (diff) | |
download | sdl_android-97a1fd13cd8da90bce2fcae9056b7f07f389cc43.tar.gz |
Fix issue with legacy USB connections
Add javadoc to ensure developers are aware of changes.
If no RS is present or at a version that can support USB, the app will use legacy USB transport. If a valid router service is availalbe, multiplexing will be used.
6 files changed, 132 insertions, 13 deletions
diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java index b376d7276..e0674724f 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java @@ -90,6 +90,7 @@ import com.smartdevicelink.trace.enums.InterfaceActivityDirection; import com.smartdevicelink.transport.BaseTransportConfig;
import com.smartdevicelink.transport.MultiplexTransportConfig;
import com.smartdevicelink.transport.SiphonServer;
+import com.smartdevicelink.transport.USBTransportConfig;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.util.CorrelationIdGenerator;
import com.smartdevicelink.util.DebugTool;
@@ -111,6 +112,8 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
@@ -1471,6 +1474,24 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> _systemCapabilityManager = new SystemCapabilityManager(_internalInterface);
// Setup SdlConnection
synchronized(CONNECTION_REFERENCE_LOCK) {
+
+ //Handle legacy USB connections
+ if(_transportConfig != null
+ && TransportType.USB.equals(_transportConfig.getTransportType())){
+ //A USB transport config was provided
+ USBTransportConfig usbTransportConfig = (USBTransportConfig)_transportConfig;
+ if(usbTransportConfig.getUsbAccessory() == null){
+ DebugTool.logInfo("Legacy USB transport config was used, but received null for accessory. Attempting to connect with router service");
+ //The accessory was null which means it came from a router service
+ MultiplexTransportConfig multiplexTransportConfig = new MultiplexTransportConfig(usbTransportConfig.getUSBContext(),_appID);
+ multiplexTransportConfig.setRequiresHighBandwidth(true);
+ multiplexTransportConfig.setSecurityLevel(MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF);
+ multiplexTransportConfig.setPrimaryTransports(Collections.singletonList(TransportType.USB));
+ multiplexTransportConfig.setSecondaryTransports(new ArrayList<TransportType>());
+ _transportConfig = multiplexTransportConfig;
+ }
+ }
+
if(_transportConfig.getTransportType().equals(TransportType.MULTIPLEX)){
this.sdlSession = new SdlSession2(_interfaceBroker,(MultiplexTransportConfig)_transportConfig);
}else{
diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java index d76194db6..b96ac4f65 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java @@ -44,11 +44,14 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.hardware.usb.UsbManager; import android.os.Build; +import android.os.Parcelable; import android.util.Log; import com.smartdevicelink.R; import com.smartdevicelink.transport.RouterServiceValidator.TrustedListCallback; +import com.smartdevicelink.transport.enums.TransportType; import com.smartdevicelink.util.AndroidTools; import com.smartdevicelink.util.SdlAppInfo; import com.smartdevicelink.util.ServiceFinder; @@ -164,6 +167,12 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{ //List obtained. Let's start our service queuedService = componentName; finalIntent.setAction("com.sdl.noaction"); //Replace what's there so we do go into some unintended loop + String transportType = finalIntent.getStringExtra(TransportConstants.START_ROUTER_SERVICE_TRANSPORT_CONNECTED); + if(transportType!= null ){ + if(TransportType.USB.toString().equals(transportType)){ + finalIntent.putExtra(UsbManager.EXTRA_ACCESSORY, (Parcelable)null); + } + } onSdlEnabled(finalContext, finalIntent); } diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java index b566b0a82..4b37dfd24 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java @@ -1593,6 +1593,11 @@ public class SdlRouterService extends Service{ startService.putExtra(TransportConstants.FORCE_TRANSPORT_CONNECTED, true); startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_APP_PACKAGE, getBaseContext().getPackageName()); startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME, new ComponentName(this, this.getClass())); + + if(record!= null && record.getType() != null){ + startService.putExtra(TransportConstants.START_ROUTER_SERVICE_TRANSPORT_CONNECTED, record.getType().toString()); + } + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { startService.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); } diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java b/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java index bd37d05f6..a66651502 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java @@ -27,6 +27,7 @@ public class TransportConstants { public static final String START_ROUTER_SERVICE_SDL_ENABLED_EXTRA = "sdl_enabled"; public static final String START_ROUTER_SERVICE_SDL_ENABLED_APP_PACKAGE = "package_name"; public static final String START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME = "component_name"; + public static final String START_ROUTER_SERVICE_TRANSPORT_CONNECTED = "transport_connected"; //Extra for the transport that just connected public static final String START_ROUTER_SERVICE_SDL_ENABLED_PING = "ping"; @Deprecated public static final String FORCE_TRANSPORT_CONNECTED = "force_connect"; //This is legacy, do not refactor this. diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java b/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java index 148c16d6b..4b878c2aa 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java @@ -40,10 +40,12 @@ import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
+import android.os.Parcelable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import com.smartdevicelink.util.AndroidTools;
+import com.smartdevicelink.util.DebugTool;
import com.smartdevicelink.util.SdlAppInfo;
import com.smartdevicelink.util.ServiceFinder;
@@ -86,9 +88,12 @@ import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA; */
@RequiresApi(12)
public class USBAccessoryAttachmentActivity extends Activity {
- private static final String TAG = USBAccessoryAttachmentActivity.class.getSimpleName();
-
+
+ private static final String TAG = USBAccessoryAttachmentActivity.class.getSimpleName();
+ private static final int USB_SUPPORTED_ROUTER_SERVICE_VERSION = 8;
+
UsbAccessory usbAccessory;
+ Parcelable permissionGranted;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -108,11 +113,8 @@ public class USBAccessoryAttachmentActivity extends Activity { Log.d(TAG, sourceAction + " with action: " + action);
if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
- Intent usbAccessoryAttachedIntent = new Intent(USBTransport.ACTION_USB_ACCESSORY_ATTACHED);
- usbAccessoryAttachedIntent.putExtra(UsbManager.EXTRA_ACCESSORY,intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY));
- usbAccessoryAttachedIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED,intent.getParcelableExtra(UsbManager.EXTRA_PERMISSION_GRANTED));
-
usbAccessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
+ permissionGranted = intent.getParcelableExtra(UsbManager.EXTRA_PERMISSION_GRANTED);
wakeUpRouterService(getApplicationContext());
@@ -131,24 +133,46 @@ public class USBAccessoryAttachmentActivity extends Activity { //We will try to sort the SDL enabled apps and find the one that's been installed the longest
Intent serviceIntent;
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator());
+
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
+ SdlAppInfo optimalRouterService = sdlAppInfoList.get(0);
+
+ if(optimalRouterService.getRouterServiceVersion() < USB_SUPPORTED_ROUTER_SERVICE_VERSION){
+ // The most optimal router service doesn't support the USB connection
+ // At this point to ensure that USB connection is still possible it might be
+ // worth trying to use the legacy USB transport scheme
+ attemptLegacyUsbConnection();
+ return;
+ }
+
serviceIntent = new Intent();
- serviceIntent.setComponent(sdlAppInfoList.get(0).getRouterServiceComponentName());
+ serviceIntent.setComponent(optimalRouterService.getRouterServiceComponentName());
} else{
Log.d(TAG, "No SDL Router Services found");
Log.d(TAG, "WARNING: This application has not specified its SdlRouterService correctly in the manifest. THIS WILL THROW AN EXCEPTION IN FUTURE RELEASES!!");
+ // At this point to ensure that USB connection is still possible it might be
+ // worth trying to use the legacy USB transport scheme
+ attemptLegacyUsbConnection();
return;
}
serviceIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT);
+ ComponentName startedService;
try {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
- context.startService(serviceIntent);
+ startedService = context.startService(serviceIntent);
}else {
serviceIntent.putExtra(FOREGROUND_EXTRA, true);
- context.startForegroundService(serviceIntent);
+ startedService = context.startForegroundService(serviceIntent);
+ }
+ if(startedService == null){
+ // A router service was not started or is not running.
+ DebugTool.logError(TAG + " - Error starting router service. Attempting legacy connection ");
+ attemptLegacyUsbConnection();
+ return;
}
+
//Make sure to send this out for old apps to close down
SdlRouterService.LocalRouterService self = SdlRouterService.getLocalRouterService(serviceIntent, serviceIntent.getComponent());
Intent restart = new Intent(SdlRouterService.REGISTER_NEWER_SERVER_INSTANCE_ACTION);
@@ -183,4 +207,13 @@ public class USBAccessoryAttachmentActivity extends Activity { }
});
}
+
+ private void attemptLegacyUsbConnection(){
+ DebugTool.logInfo("Attempting to send USB connection intent using legacy method");
+ Intent usbAccessoryAttachedIntent = new Intent(USBTransport.ACTION_USB_ACCESSORY_ATTACHED);
+ usbAccessoryAttachedIntent.putExtra(UsbManager.EXTRA_ACCESSORY, usbAccessory);
+ usbAccessoryAttachedIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, permissionGranted);
+ AndroidTools.sendExplicitBroadcast(getApplicationContext(),usbAccessoryAttachedIntent,null);
+ finish();
+ }
}
diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/USBTransportConfig.java b/sdl_android/src/main/java/com/smartdevicelink/transport/USBTransportConfig.java index 0023c153a..b819a6844 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/USBTransportConfig.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/USBTransportConfig.java @@ -5,28 +5,78 @@ import com.smartdevicelink.transport.enums.TransportType; import android.content.Context;
import android.hardware.usb.UsbAccessory;
+/**
+ * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
+ * understand how to implement USB multiplexing. This class and method of USB connection will be
+ * removed in the next major release. If a router service is available to handle multiplexing of the
+ * usb transport it will be used, and this app will connect to whatever router service hosts the USB
+ * connection.
+ * @see MultiplexTransportConfig
+ */
@Deprecated
public class USBTransportConfig extends BaseTransportConfig {
private Context mainActivity = null;
private UsbAccessory usbAccessory = null;
private Boolean queryUsbAcc = true;
-
+
+ /**
+ * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
+ * understand how to implement USB multiplexing. This class and method of USB connection will be
+ * removed in the next major release. If a router service is available to handle multiplexing of the
+ * usb transport it will be used, and this app will connect to whatever router service hosts the USB
+ * connection.
+ * @param mainActivity context used to start USB transport
+ * @see MultiplexTransportConfig
+ */
public USBTransportConfig (Context mainActivity) {
this.mainActivity = mainActivity;
}
-
+
+ /**
+ * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
+ * understand how to implement USB multiplexing. This class and method of USB connection will be
+ * removed in the next major release. If a router service is available to handle multiplexing of the
+ * usb transport it will be used, and this app will connect to whatever router service hosts the USB
+ * connection.
+ * @param mainActivity context used to start USB transport
+ * @param usbAccessory the accessory that was given to this app
+ * @see MultiplexTransportConfig
+ */
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory) {
this.mainActivity = mainActivity;
this.usbAccessory = usbAccessory;
}
-
+
+ /**
+ * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
+ * understand how to implement USB multiplexing. This class and method of USB connection will be
+ * removed in the next major release. If a router service is available to handle multiplexing of the
+ * usb transport it will be used, and this app will connect to whatever router service hosts the USB
+ * connection.
+ * @param mainActivity context used to start USB transport
+ * @param shareConnection enable other sessions on this app to use this USB connection
+ * @param queryUsbAcc attempt to query the USB accessory if none is provided
+ * @see MultiplexTransportConfig
+ */
public USBTransportConfig (Context mainActivity, boolean shareConnection, boolean queryUsbAcc) {
this.mainActivity = mainActivity;
this.queryUsbAcc = queryUsbAcc;
super.shareConnection = shareConnection;
}
-
+
+ /**
+ * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
+ * understand how to implement USB multiplexing. This class and method of USB connection will be
+ * removed in the next major release. If a router service is available to handle multiplexing of the
+ * usb transport it will be used, and this app will connect to whatever router service hosts the USB
+ * connection.
+ * @param mainActivity context used to start USB transport
+ * @param usbAccessory the accessory that was given to this app
+ * @param shareConnection enable other sessions on this app to use this USB connection
+ * @param queryUsbAcc attempt to query the USB accessory if none is provided
+ * @see MultiplexTransportConfig
+ */
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory, boolean shareConnection, boolean queryUsbAcc) {
this.mainActivity = mainActivity;
this.queryUsbAcc = queryUsbAcc;
|