From e5bb2cf6648ced76dff582a87ac56bcb89d2e2e6 Mon Sep 17 00:00:00 2001 From: RHeniz Date: Thu, 18 Nov 2021 10:13:11 -0500 Subject: Fix initCheck in RS --- .../java/com/sdl/hellosdlandroid/SdlReceiver.java | 12 +++++--- .../transport/SdlBroadcastReceiver.java | 32 ++++++++++++++++------ .../transport/SdlRouterService.java | 15 ++++++---- .../transport/USBAccessoryAttachmentActivity.java | 2 +- .../com/smartdevicelink/util/AndroidTools.java | 26 ++++-------------- .../smartdevicelink/util/IntegrationValidator.java | 22 ++------------- 6 files changed, 49 insertions(+), 60 deletions(-) (limited to 'android') diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java index 32c0ab1d5..ec2ebd31c 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java @@ -19,10 +19,14 @@ public class SdlReceiver extends SdlBroadcastReceiver { // SdlService needs to be foregrounded in Android O and above // This will prevent apps in the background from crashing when they try to start SdlService // Because Android O doesn't allow background apps to start background services - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent); - } else { - context.startService(intent); + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(intent); + } else { + context.startService(intent); + } + } catch (Exception e) { + DebugTool.logError("RHENIGAN", "Failed to start SdlService!"); } } diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java index 4893545b2..15fee7227 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java @@ -41,9 +41,6 @@ import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.ServiceInfo; import android.hardware.usb.UsbManager; import android.os.Build; import android.os.Looper; @@ -51,7 +48,6 @@ import android.os.Parcelable; import android.util.AndroidRuntimeException; import androidx.annotation.CallSuper; -import androidx.core.content.ContextCompat; import com.smartdevicelink.proxy.rpc.VehicleType; import com.smartdevicelink.transport.RouterServiceValidator.TrustedListCallback; @@ -70,8 +66,6 @@ import java.util.Locale; import java.util.Vector; import java.util.concurrent.ConcurrentLinkedQueue; -import static android.Manifest.permission.BLUETOOTH_CONNECT; -import static android.Manifest.permission.BLUETOOTH_SCAN; import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA; public abstract class SdlBroadcastReceiver extends BroadcastReceiver { @@ -294,7 +288,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { if (runningBluetoothServicePackage.isEmpty()) { //If there isn't a service running we should try to start one //We will try to sort the SDL enabled apps and find the one that's been installed the longest - final List sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType, BluetoothDevice.ACTION_ACL_CONNECTED); + final List sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType); synchronized (DEVICE_LISTENER_LOCK) { final boolean sdlDeviceListenerEnabled = SdlDeviceListener.isFeatureSupported(sdlAppInfoList); if (sdlDeviceListenerEnabled) { @@ -302,6 +296,16 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { String routerServicePackage = null; if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty() && sdlAppInfoList.get(0).getRouterServiceComponentName() != null) { routerServicePackage = sdlAppInfoList.get(0).getRouterServiceComponentName().getPackageName(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (!AndroidTools.areBtPermissionsGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) { + for (SdlAppInfo appInfo : sdlAppInfoList) { + if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { + routerServicePackage = appInfo.getRouterServiceComponentName().getPackageName(); + break; + } + } + } + } } DebugTool.logInfo(TAG, ": This app's package: " + myPackage); DebugTool.logInfo(TAG, ": Router service app's package: " + routerServicePackage); @@ -601,9 +605,21 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { if (store != null) { vehicleType = new VehicleType(store); } - final List sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType, BluetoothDevice.ACTION_ACL_CONNECTED); + final List sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType); if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) { ComponentName routerService = sdlAppInfoList.get(0).getRouterServiceComponentName(); + //If we are on android 12 check the app has BT permissions + //If it does not try to find another app in the list that does; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (!AndroidTools.areBtPermissionsGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) { + for (SdlAppInfo appInfo : sdlAppInfoList) { + if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { + routerService = appInfo.getRouterServiceComponentName(); + break; + } + } + } + } startRouterService(context, routerService, false, bluetoothDevice, true, vehicleType); } } diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java index 41f2b007c..fa4ba4054 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java @@ -216,7 +216,6 @@ public class SdlRouterService extends Service { private boolean startSequenceComplete = false; private VehicleType receivedVehicleType; - private String connectionType; private ExecutorService packetExecutor = null; ConcurrentHashMap packetWriteTaskMasterMap = null; @@ -1097,6 +1096,13 @@ public class SdlRouterService extends Service { DebugTool.logError(TAG, "Bluetooth Permission is not granted. Shutting down"); return false; } + + //If Android 12 or newer make sure we have BT Runtime permissions + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.areBtPermissionsGranted(this, this.getPackageName())) { + DebugTool.logError(TAG, "Bluetooth Runtime Permissions are not granted. Shutting down"); + return false; + } + if (!AndroidTools.isServiceExported(this, new ComponentName(this, this.getClass()))) { //We want to check to see if our service is actually exported DebugTool.logError(TAG, "Service isn't exported. Shutting down"); return false; @@ -1105,7 +1111,7 @@ public class SdlRouterService extends Service { ComponentName name = new ComponentName(this, this.getClass()); SdlAppInfo currentAppInfo = null; - List sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null, connectionType); + List sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null); for (SdlAppInfo appInfo : sdlAppInfoList) { if (appInfo.getRouterServiceComponentName().equals(name)) { currentAppInfo = appInfo; @@ -1151,7 +1157,7 @@ public class SdlRouterService extends Service { * The method will attempt to start up the next router service in line based on the sorting criteria of best router service. */ protected void deployNextRouterService() { - List sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null, connectionType); + List sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null); if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) { ComponentName name = new ComponentName(this, this.getClass()); SdlAppInfo info; @@ -1258,9 +1264,6 @@ public class SdlRouterService extends Service { (HashMap) intent.getSerializableExtra(TransportConstants.VEHICLE_INFO_EXTRA) ); } - if (intent != null && intent.hasExtra(TransportConstants.CONNECTION_TYPE_EXTRA)) { - connectionType = intent.getStringExtra(TransportConstants.CONNECTION_TYPE_EXTRA); - } // 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. if (firstStart) { firstStart = false; diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java index c91ef68e8..82341afe2 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java @@ -143,7 +143,7 @@ public class USBAccessoryAttachmentActivity extends Activity { //If there isn't a service running we should try to start one //We will try to sort the SDL enabled apps and find the one that's been installed the longest Intent serviceIntent; - List sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), null, TransportConstants.ACTION_USB_ACCESSORY_ATTACHED); + List sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), null); if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) { SdlAppInfo optimalRouterService = sdlAppInfoList.get(0); diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java b/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java index 3aaea30dc..02ef5169d 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java @@ -162,7 +162,7 @@ public class AndroidTools { * @param comparator the Comparator to sort the resulting list. If null is supplied, they will be returned as they are from the system * @return the sorted list of SdlAppInfo objects that represent SDL apps */ - public static List querySdlAppInfo(Context context, Comparator comparator, VehicleType type, String connectionType) { + public static List querySdlAppInfo(Context context, Comparator comparator, VehicleType type) { List sdlAppInfoList = new ArrayList<>(); Intent intent = new Intent(TransportConstants.ROUTER_SERVICE_ACTION); List resolveInfoList = context.getPackageManager().queryIntentServices(intent, PackageManager.GET_META_DATA); @@ -171,34 +171,18 @@ public class AndroidTools { PackageManager packageManager = context.getPackageManager(); if (packageManager != null) { - //try to add based on BT permissions for (ResolveInfo info : resolveInfoList) { PackageInfo packageInfo; try { - packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, PackageManager.GET_PERMISSIONS); - boolean btPermissionsAllowed = areBtPermissionsGranted(context, info.serviceInfo.packageName); - if (btPermissionsAllowed) { - SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context); - sdlAppInfoList.add(appInformation); - } + packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, 0); + SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context); + sdlAppInfoList.add(appInformation); + } catch (NameNotFoundException e) { //Package was not found, likely a sign the resolve info can't be trusted. } } - //If there are no apps with BT permissions and the device is connected over USB, then find the best RS to start over USB - if (sdlAppInfoList.isEmpty() && TransportConstants.ACTION_USB_ACCESSORY_ATTACHED.equalsIgnoreCase(connectionType)) { - for (ResolveInfo info : resolveInfoList) { - PackageInfo packageInfo; - try { - packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, PackageManager.GET_PERMISSIONS); - SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context); - sdlAppInfoList.add(appInformation); - } catch (NameNotFoundException e) { - //Package was not found, likely a sign the resolve info can't be trusted. - } - } - } } List sdlAppInfoListVehicleType = new ArrayList<>(); diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/util/IntegrationValidator.java b/android/sdl_android/src/main/java/com/smartdevicelink/util/IntegrationValidator.java index cf72f445b..930436729 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/util/IntegrationValidator.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/util/IntegrationValidator.java @@ -200,21 +200,7 @@ public class IntegrationValidator { boolean serviceFilterHasAction = false; String className = localRouterClass.getName(); - - boolean areBtPermissionsEnabled = true; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - areBtPermissionsEnabled = false; - try { - ComponentName cn = new ComponentName(context.getPackageName(), className); - PackageManager pm = context.getPackageManager(); - ServiceInfo serviceInfo = pm.getServiceInfo(cn, 0); - areBtPermissionsEnabled = AndroidTools.areBtPermissionsGranted(context, serviceInfo.packageName); - } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); - } - } - - List services = AndroidTools.querySdlAppInfo(context, null, null, null); + List services = AndroidTools.querySdlAppInfo(context, null, null); for (SdlAppInfo sdlAppInfo : services) { if (sdlAppInfo != null && sdlAppInfo.getRouterServiceComponentName() != null && className.equals((sdlAppInfo.getRouterServiceComponentName().getClassName()))) { @@ -222,11 +208,7 @@ public class IntegrationValidator { break; } } - - if (!serviceFilterHasAction && !areBtPermissionsEnabled) { - retVal.successful = true; - retVal.resultText = "intent-filter not found for SdlRouterService because BT Permissions are disabled"; - } else if (!serviceFilterHasAction && areBtPermissionsEnabled) { + if (!serviceFilterHasAction) { retVal.successful = false; retVal.resultText = "This application has not specified its intent-filter for the SdlRouterService."; } -- cgit v1.2.1