Skip to content

Commit e5bb2cf

Browse files
committed
Fix initCheck in RS
1 parent 7bd28d7 commit e5bb2cf

6 files changed

Lines changed: 49 additions & 60 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ public void onSdlEnabled(Context context, Intent intent) {
1919
// SdlService needs to be foregrounded in Android O and above
2020
// This will prevent apps in the background from crashing when they try to start SdlService
2121
// Because Android O doesn't allow background apps to start background services
22-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
23-
context.startForegroundService(intent);
24-
} else {
25-
context.startService(intent);
22+
try {
23+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
24+
context.startForegroundService(intent);
25+
} else {
26+
context.startService(intent);
27+
}
28+
} catch (Exception e) {
29+
DebugTool.logError("RHENIGAN", "Failed to start SdlService!");
2630
}
2731
}
2832

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@
4141
import android.content.ComponentName;
4242
import android.content.Context;
4343
import android.content.Intent;
44-
import android.content.pm.PackageInfo;
45-
import android.content.pm.PackageManager;
46-
import android.content.pm.ServiceInfo;
4744
import android.hardware.usb.UsbManager;
4845
import android.os.Build;
4946
import android.os.Looper;
5047
import android.os.Parcelable;
5148
import android.util.AndroidRuntimeException;
5249

5350
import androidx.annotation.CallSuper;
54-
import androidx.core.content.ContextCompat;
5551

5652
import com.smartdevicelink.proxy.rpc.VehicleType;
5753
import com.smartdevicelink.transport.RouterServiceValidator.TrustedListCallback;
@@ -70,8 +66,6 @@
7066
import java.util.Vector;
7167
import java.util.concurrent.ConcurrentLinkedQueue;
7268

73-
import static android.Manifest.permission.BLUETOOTH_CONNECT;
74-
import static android.Manifest.permission.BLUETOOTH_SCAN;
7569
import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA;
7670

7771
public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
@@ -294,14 +288,24 @@ public void onComplete(Vector<ComponentName> routerServices) {
294288
if (runningBluetoothServicePackage.isEmpty()) {
295289
//If there isn't a service running we should try to start one
296290
//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, BluetoothDevice.ACTION_ACL_CONNECTED);
291+
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType);
298292
synchronized (DEVICE_LISTENER_LOCK) {
299293
final boolean sdlDeviceListenerEnabled = SdlDeviceListener.isFeatureSupported(sdlAppInfoList);
300294
if (sdlDeviceListenerEnabled) {
301295
String myPackage = context.getPackageName();
302296
String routerServicePackage = null;
303297
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty() && sdlAppInfoList.get(0).getRouterServiceComponentName() != null) {
304298
routerServicePackage = sdlAppInfoList.get(0).getRouterServiceComponentName().getPackageName();
299+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
300+
if (!AndroidTools.areBtPermissionsGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) {
301+
for (SdlAppInfo appInfo : sdlAppInfoList) {
302+
if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
303+
routerServicePackage = appInfo.getRouterServiceComponentName().getPackageName();
304+
break;
305+
}
306+
}
307+
}
308+
}
305309
}
306310
DebugTool.logInfo(TAG, ": This app's package: " + myPackage);
307311
DebugTool.logInfo(TAG, ": Router service app's package: " + routerServicePackage);
@@ -601,9 +605,21 @@ public boolean onTransportConnected(Context context, BluetoothDevice bluetoothDe
601605
if (store != null) {
602606
vehicleType = new VehicleType(store);
603607
}
604-
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType, BluetoothDevice.ACTION_ACL_CONNECTED);
608+
final List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), vehicleType);
605609
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
606610
ComponentName routerService = sdlAppInfoList.get(0).getRouterServiceComponentName();
611+
//If we are on android 12 check the app has BT permissions
612+
//If it does not try to find another app in the list that does;
613+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
614+
if (!AndroidTools.areBtPermissionsGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) {
615+
for (SdlAppInfo appInfo : sdlAppInfoList) {
616+
if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
617+
routerService = appInfo.getRouterServiceComponentName();
618+
break;
619+
}
620+
}
621+
}
622+
}
607623
startRouterService(context, routerService, false, bluetoothDevice, true, vehicleType);
608624
}
609625
}

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

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

217217
private boolean startSequenceComplete = false;
218218
private VehicleType receivedVehicleType;
219-
private String connectionType;
220219

221220
private ExecutorService packetExecutor = null;
222221
ConcurrentHashMap<TransportType, PacketWriteTaskMaster> packetWriteTaskMasterMap = null;
@@ -1097,6 +1096,13 @@ private boolean initCheck() {
10971096
DebugTool.logError(TAG, "Bluetooth Permission is not granted. Shutting down");
10981097
return false;
10991098
}
1099+
1100+
//If Android 12 or newer make sure we have BT Runtime permissions
1101+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.areBtPermissionsGranted(this, this.getPackageName())) {
1102+
DebugTool.logError(TAG, "Bluetooth Runtime Permissions are not granted. Shutting down");
1103+
return false;
1104+
}
1105+
11001106
if (!AndroidTools.isServiceExported(this, new ComponentName(this, this.getClass()))) { //We want to check to see if our service is actually exported
11011107
DebugTool.logError(TAG, "Service isn't exported. Shutting down");
11021108
return false;
@@ -1105,7 +1111,7 @@ private boolean initCheck() {
11051111
ComponentName name = new ComponentName(this, this.getClass());
11061112
SdlAppInfo currentAppInfo = null;
11071113

1108-
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null, connectionType);
1114+
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null);
11091115
for (SdlAppInfo appInfo : sdlAppInfoList) {
11101116
if (appInfo.getRouterServiceComponentName().equals(name)) {
11111117
currentAppInfo = appInfo;
@@ -1151,7 +1157,7 @@ public void onCreate() {
11511157
* The method will attempt to start up the next router service in line based on the sorting criteria of best router service.
11521158
*/
11531159
protected void deployNextRouterService() {
1154-
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null, connectionType);
1160+
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(getApplicationContext(), new SdlAppInfo.BestRouterComparator(), null);
11551161
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
11561162
ComponentName name = new ComponentName(this, this.getClass());
11571163
SdlAppInfo info;
@@ -1258,9 +1264,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
12581264
(HashMap<String, Object>) intent.getSerializableExtra(TransportConstants.VEHICLE_INFO_EXTRA)
12591265
);
12601266
}
1261-
if (intent != null && intent.hasExtra(TransportConstants.CONNECTION_TYPE_EXTRA)) {
1262-
connectionType = intent.getStringExtra(TransportConstants.CONNECTION_TYPE_EXTRA);
1263-
}
12641267
// 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.
12651268
if (firstStart) {
12661269
firstStart = false;

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

Lines changed: 1 addition & 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, TransportConstants.ACTION_USB_ACCESSORY_ATTACHED);
146+
List<SdlAppInfo> sdlAppInfoList = AndroidTools.querySdlAppInfo(context, new SdlAppInfo.BestRouterComparator(), null);
147147

148148
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
149149
SdlAppInfo optimalRouterService = sdlAppInfoList.get(0);

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

Lines changed: 5 additions & 21 deletions
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, String connectionType) {
165+
public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAppInfo> comparator, VehicleType type) {
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,34 +171,18 @@ 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
175174
for (ResolveInfo info : resolveInfoList) {
176175
PackageInfo packageInfo;
177176
try {
178-
packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, PackageManager.GET_PERMISSIONS);
179-
boolean btPermissionsAllowed = areBtPermissionsGranted(context, info.serviceInfo.packageName);
180-
if (btPermissionsAllowed) {
181-
SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context);
182-
sdlAppInfoList.add(appInformation);
183-
}
177+
packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, 0);
178+
SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context);
179+
sdlAppInfoList.add(appInformation);
180+
184181
} catch (NameNotFoundException e) {
185182
//Package was not found, likely a sign the resolve info can't be trusted.
186183
}
187184

188185
}
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-
}
202186
}
203187

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

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -200,33 +200,15 @@ private static ValidationResult checkRouterServiceIntent(Context context, Class
200200

201201
boolean serviceFilterHasAction = false;
202202
String className = localRouterClass.getName();
203-
204-
boolean areBtPermissionsEnabled = true;
205-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
206-
areBtPermissionsEnabled = false;
207-
try {
208-
ComponentName cn = new ComponentName(context.getPackageName(), className);
209-
PackageManager pm = context.getPackageManager();
210-
ServiceInfo serviceInfo = pm.getServiceInfo(cn, 0);
211-
areBtPermissionsEnabled = AndroidTools.areBtPermissionsGranted(context, serviceInfo.packageName);
212-
} catch (PackageManager.NameNotFoundException e) {
213-
e.printStackTrace();
214-
}
215-
}
216-
217-
List<SdlAppInfo> services = AndroidTools.querySdlAppInfo(context, null, null, null);
203+
List<SdlAppInfo> services = AndroidTools.querySdlAppInfo(context, null, null);
218204
for (SdlAppInfo sdlAppInfo : services) {
219205
if (sdlAppInfo != null && sdlAppInfo.getRouterServiceComponentName() != null
220206
&& className.equals((sdlAppInfo.getRouterServiceComponentName().getClassName()))) {
221207
serviceFilterHasAction = true;
222208
break;
223209
}
224210
}
225-
226-
if (!serviceFilterHasAction && !areBtPermissionsEnabled) {
227-
retVal.successful = true;
228-
retVal.resultText = "intent-filter not found for SdlRouterService because BT Permissions are disabled";
229-
} else if (!serviceFilterHasAction && areBtPermissionsEnabled) {
211+
if (!serviceFilterHasAction) {
230212
retVal.successful = false;
231213
retVal.resultText = "This application has not specified its intent-filter for the SdlRouterService.";
232214
}

0 commit comments

Comments
 (0)