@@ -73,6 +73,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
7373 private static final String TAG = "Sdl Broadcast Receiver" ;
7474
7575 protected static final String SDL_ROUTER_SERVICE_CLASS_NAME = "sdlrouterservice" ;
76+ protected static final int ANDROID_12_ROUTER_SERVICE_VERSION = 16 ;
7677
7778 public static final String LOCAL_ROUTER_SERVICE_EXTRA = "router_service" ;
7879 public static final String LOCAL_ROUTER_SERVICE_DID_START_OWN = "did_start" ;
@@ -302,15 +303,17 @@ public void onComplete(Vector<ComponentName> routerServices) {
302303 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
303304 boolean preAndroid12RouterServiceOnDevice = false ;
304305 for (SdlAppInfo appInfo : sdlAppInfoList ) {
305- //If the RS version is older than Android 12 update version
306- if (appInfo .getRouterServiceVersion () < 16 ) {
306+ //If an installed app RS version is older than Android 12 update version (16)
307+ if (appInfo .getRouterServiceVersion () < ANDROID_12_ROUTER_SERVICE_VERSION ) {
307308 preAndroid12RouterServiceOnDevice = true ;
308309 break ;
309310 }
310311 }
312+ // If all apps have a RS newer than the Android 12 update, chosen app does not have BT Connect permissions, and more than 1 sdl app is installed
311313 if (!preAndroid12RouterServiceOnDevice && !AndroidTools .isBtConnectPermissionGranted (context , routerServicePackage ) && sdlAppInfoList .size () > 1 ) {
312314 for (SdlAppInfo appInfo : sdlAppInfoList ) {
313315 if (AndroidTools .isBtConnectPermissionGranted (context , appInfo .getRouterServiceComponentName ().getPackageName ())) {
316+ //If this app in the list has BT Connect permissions, we want to use that apps RS
314317 routerServicePackage = appInfo .getRouterServiceComponentName ().getPackageName ();
315318 break ;
316319 }
@@ -383,13 +386,16 @@ private void wakeRouterServiceAltTransport(Context context) {
383386 }
384387
385388 /**
386- * This method will set a new UncaughtExceptionHandler for the current thread. The only
387- * purpose of the custom UncaughtExceptionHandler is to catch the rare occurrence that the
388- * SdlRouterService can't be started fast enough by the system after calling
389- * startForegroundService so the onCreate method doesn't get called before the foreground promise
390- * timer expires. The new UncaughtExceptionHandler will catch that specific exception and tell the
391- * main looper to continue forward. This still leaves the SdlRouterService killed, but prevents
392- * an ANR to the app that makes the startForegroundService call.
389+ * This method will set a new UncaughtExceptionHandler for the current thread.
390+ * There are two exceptions we want to catch here. The first exception is the rare
391+ * occurrence that the SdlRouterService can't be started fast enough by the system after calling
392+ * startForegroundService so the onCreate method doesn't get called before the foreground
393+ * promise timer expires. The second is for the instance where the developers "SdlService" class
394+ * can't be started fast enough by the system after calling startForegroundService OR the app
395+ * is unable to start the "SdlService" class because the developer did not export the service
396+ * in the manifest. The new UncaughtExceptionHandler will catch these specific exception and
397+ * tell the main looper to continue forward. This still leaves the respective Service killed,
398+ * but prevents an ANR to the app that makes the startForegroundService call.
393399 */
394400 static protected void setForegroundExceptionHandler () {
395401 final Thread .UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread .getDefaultUncaughtExceptionHandler ();
@@ -618,21 +624,21 @@ public boolean onTransportConnected(Context context, BluetoothDevice bluetoothDe
618624 final List <SdlAppInfo > sdlAppInfoList = AndroidTools .querySdlAppInfo (context , new SdlAppInfo .BestRouterComparator (), vehicleType );
619625 if (sdlAppInfoList != null && !sdlAppInfoList .isEmpty ()) {
620626 ComponentName routerService = sdlAppInfoList .get (0 ).getRouterServiceComponentName ();
621- //If we are on android 12 check the app has BT permissions
622- //If it does not try to find another app in the list that does;
623627 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
624628 boolean preAndroid12RouterServiceOnDevice = false ;
625629 for (SdlAppInfo appInfo : sdlAppInfoList ) {
626- //If the RS version is older than Android 12 update version
627- if (appInfo .getRouterServiceVersion () < 16 ) {
630+ //If an installed app RS version is older than Android 12 update version (16)
631+ if (appInfo .getRouterServiceVersion () < ANDROID_12_ROUTER_SERVICE_VERSION ) {
628632 preAndroid12RouterServiceOnDevice = true ;
629633 break ;
630634 }
631635 }
636+ // If all apps have a RS newer than the Android 12 update, chosen app does not have BT Connect permissions, and more than 1 sdl app is installed
632637 if (!preAndroid12RouterServiceOnDevice && !AndroidTools .isBtConnectPermissionGranted (context , routerService .getPackageName ()) && sdlAppInfoList .size () > 1 ) {
633638 for (SdlAppInfo appInfo : sdlAppInfoList ) {
634639 if (AndroidTools .isBtConnectPermissionGranted (context , appInfo .getRouterServiceComponentName ().getPackageName ())) {
635640 routerService = appInfo .getRouterServiceComponentName ();
641+ //If this app in the list has BT Connect permissions, we want to use that apps RS
636642 break ;
637643 }
638644 }
@@ -696,6 +702,13 @@ public static ComponentName consumeQueuedRouterService() {
696702 */
697703 public abstract void onSdlEnabled (Context context , Intent intent );
698704
705+
706+ /**
707+ * The developer can override this method to return the name of the class that manages their
708+ * SdlService. This method is used to ensure the SdlBroadcastReceivers exception catcher catches
709+ * the correct exception that may be thrown by the app trying to start their SdlService. If this
710+ * exception is not caught the user may experience an ANR for that app.
711+ */
699712 public String getSdlServiceName () {
700713 return "SdlService" ;
701714 }
0 commit comments