Skip to content

Commit 7bd28d7

Browse files
committed
Allow USB to start Router Service if no BT perm
1 parent 9eb0069 commit 7bd28d7

7 files changed

Lines changed: 37 additions & 20 deletions

File tree

android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,17 @@ protected void onCreate(Bundle savedInstanceState) {
2424
super.onCreate(savedInstanceState);
2525
setContentView(R.layout.activity_main);
2626

27-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
28-
if (!checkPermission()) {
29-
requestPermission();
30-
}
31-
} else {
32-
//If we are connected to a module we want to start our SdlService
33-
startSDLService();
34-
}
35-
}
3627

37-
private void startSDLService() {
3828
if (BuildConfig.TRANSPORT.equals("MULTI") || BuildConfig.TRANSPORT.equals("MULTI_HB")) {
39-
SdlReceiver.queryForConnectedService(this);
40-
} else if (BuildConfig.TRANSPORT.equals("TCP")) {
29+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
30+
if (!checkPermission()) {
31+
requestPermission();
32+
}
33+
} else {
34+
//If we are connected to a module we want to start our SdlService
35+
SdlReceiver.queryForConnectedService(this);
36+
}
37+
} else {
4138
Intent proxyIntent = new Intent(this, SdlService.class);
4239
startService(proxyIntent);
4340
}
@@ -64,7 +61,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
6461
boolean scanAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
6562

6663
if (connectAccepted && scanAccepted) {
67-
startSDLService();
64+
SdlReceiver.queryForConnectedService(this);
6865
}
6966
}
7067
break;

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public void onComplete(Vector<ComponentName> routerServices) {
294294
if (runningBluetoothServicePackage.isEmpty()) {
295295
//If there isn't a service running we should try to start one
296296
//We will try to sort the SDL enabled apps and find the one that's been installed the longest
297-
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType);
297+
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType, BluetoothDevice.ACTION_ACL_CONNECTED);
298298
synchronized (DEVICE_LISTENER_LOCK) {
299299
final boolean sdlDeviceListenerEnabled = SdlDeviceListener.isFeatureSupported(sdlAppInfoList);
300300
if (sdlDeviceListenerEnabled) {
@@ -601,7 +601,7 @@ public boolean onTransportConnected(Context context, BluetoothDevice bluetoothDe
601601
if (store != null) {
602602
vehicleType = new VehicleType(store);
603603
}
604-
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType);
604+
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType, BluetoothDevice.ACTION_ACL_CONNECTED);
605605
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
606606
ComponentName routerService = sdlAppInfoList.get(0).getRouterServiceComponentName();
607607
startRouterService(context, routerService, false, bluetoothDevice, true, vehicleType);

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public class SdlRouterService extends Service {
216216

217217
private boolean startSequenceComplete = false;
218218
private VehicleType receivedVehicleType;
219+
private String connectionType;
219220

220221
private ExecutorService packetExecutor = null;
221222
ConcurrentHashMap<TransportType, PacketWriteTaskMaster> packetWriteTaskMasterMap = null;
@@ -1104,7 +1105,7 @@ private boolean initCheck() {
11041105
ComponentName name = new ComponentName(this, this.getClass());
11051106
SdlAppInfo currentAppInfo = null;
11061107

1107-
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null);
1108+
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null, connectionType);
11081109
for (SdlAppInfo appInfo : sdlAppInfoList) {
11091110
if (appInfo.getRouterServiceComponentName().equals(name)) {
11101111
currentAppInfo = appInfo;
@@ -1150,7 +1151,7 @@ public void onCreate() {
11501151
* The method will attempt to start up the next router service in line based on the sorting criteria of best router service.
11511152
*/
11521153
protected void deployNextRouterService() {
1153-
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null);
1154+
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null, connectionType);
11541155
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
11551156
ComponentName name = new ComponentName(this, this.getClass());
11561157
SdlAppInfo info;
@@ -1257,6 +1258,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
12571258
(HashMap<String, Object>) intent.getSerializableExtra(TransportConstants.VEHICLE_INFO_EXTRA)
12581259
);
12591260
}
1261+
if (intent != null && intent.hasExtra(TransportConstants.CONNECTION_TYPE_EXTRA)) {
1262+
connectionType = intent.getStringExtra(TransportConstants.CONNECTION_TYPE_EXTRA);
1263+
}
12601264
// Only trusting the first intent received to start the RouterService and run initial checks to avoid a case where an app could send incorrect data after the spp connection has started.
12611265
if (firstStart) {
12621266
firstStart = false;

android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void onComplete(Vector<ComponentName> routerServices) {
143143
//If there isn't a service running we should try to start one
144144
//We will try to sort the SDL enabled apps and find the one that's been installed the longest
145145
Intent serviceIntent;
146-
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), null);
146+
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), null, TransportConstants.ACTION_USB_ACCESSORY_ATTACHED);
147147

148148
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
149149
SdlAppInfo optimalRouterService = sdlAppInfoList.get(0);
@@ -167,6 +167,7 @@ public void onComplete(Vector<ComponentName> routerServices) {
167167
return;
168168
}
169169
serviceIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT);
170+
serviceIntent.putExtra(TransportConstants.CONNECTION_TYPE_EXTRA, TransportConstants.ACTION_USB_ACCESSORY_ATTACHED);
170171

171172
ComponentName startedService;
172173
try {

android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
162162
* @param comparator the Comparator to sort the resulting list. If null is supplied, they will be returned as they are from the system
163163
* @return the sorted list of SdlAppInfo objects that represent SDL apps
164164
*/
165-
public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAppInfo> comparator, VehicleType type) {
165+
public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAppInfo> comparator, VehicleType type, String connectionType) {
166166
List<SdlAppInfo> sdlAppInfoList = new ArrayList<>();
167167
Intent intent = new Intent(TransportConstants.ROUTER_SERVICE_ACTION);
168168
List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentServices(intent, PackageManager.GET_META_DATA);
@@ -171,6 +171,7 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
171171
PackageManager packageManager = context.getPackageManager();
172172
if (packageManager != null) {
173173

174+
//try to add based on BT permissions
174175
for (ResolveInfo info : resolveInfoList) {
175176
PackageInfo packageInfo;
176177
try {
@@ -185,6 +186,19 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
185186
}
186187

187188
}
189+
//If there are no apps with BT permissions and the device is connected over USB, then find the best RS to start over USB
190+
if (sdlAppInfoList.isEmpty() && TransportConstants.ACTION_USB_ACCESSORY_ATTACHED.equalsIgnoreCase(connectionType)) {
191+
for (ResolveInfo info : resolveInfoList) {
192+
PackageInfo packageInfo;
193+
try {
194+
packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, PackageManager.GET_PERMISSIONS);
195+
SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context);
196+
sdlAppInfoList.add(appInformation);
197+
} catch (NameNotFoundException e) {
198+
//Package was not found, likely a sign the resolve info can't be trusted.
199+
}
200+
}
201+
}
188202
}
189203

190204
List<SdlAppInfo> sdlAppInfoListVehicleType = new ArrayList<>();

android/sdl_android/src/main/java/com/smartdevicelink/util/IntegrationValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private static ValidationResult checkRouterServiceIntent(Context context, Class
214214
}
215215
}
216216

217-
List<SdlAppInfo> services = AndroidTools.querySdlAppInfo(context, null, null);
217+
List<SdlAppInfo> services = AndroidTools.querySdlAppInfo(context, null, null, null);
218218
for (SdlAppInfo sdlAppInfo : services) {
219219
if (sdlAppInfo != null && sdlAppInfo.getRouterServiceComponentName() != null
220220
&& className.equals((sdlAppInfo.getRouterServiceComponentName().getClassName()))) {

base/src/main/java/com/smartdevicelink/transport/TransportConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class TransportConstants {
4545
public static final String FOREGROUND_EXTRA = "foreground";
4646
public static final String CONFIRMED_SDL_DEVICE = "confirmed_sdl_device";
4747
public static final String VEHICLE_INFO_EXTRA = "vehicle_info";
48+
public static final String CONNECTION_TYPE_EXTRA = "connection_type";
4849

4950
public static final String BIND_LOCATION_PACKAGE_NAME_EXTRA = "BIND_LOCATION_PACKAGE_NAME_EXTRA";
5051
public static final String BIND_LOCATION_CLASS_NAME_EXTRA = "BIND_LOCATION_CLASS_NAME_EXTRA";

0 commit comments

Comments
 (0)