Skip to content

Commit 3b2f02b

Browse files
committed
Clean up and comments
1 parent 6fd76ae commit 3b2f02b

5 files changed

Lines changed: 41 additions & 22 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public void onSdlEnabled(Context context, Intent intent) {
1818
DebugTool.logInfo(TAG, "SDL Enabled");
1919
intent.setClass(context, SdlService.class);
2020

21+
// Starting with Android S SdlService needs to be started from a foreground context.
22+
// We will check the intent for a pendingIntent parcelable extra
23+
// This pendingIntent allows us to start the SdlService from the context of the active router service which is in the foreground
2124
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
2225
if (intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA) != null) {
2326
PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA);

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ public void handleMessage(Message msg) {
372372

373373
switch (msg.what) {
374374
case TransportConstants.ROUTER_REQUEST_BT_CLIENT_CONNECT:
375+
//Starting with Android 12 this use case will require the BLUETOOTH_SCAN PERMISSION
375376
if (!AndroidTools.isBtScanPermissionGranted(service.getApplicationContext(), service.getPackageName())) {
376377
break;
377378
}
@@ -1106,9 +1107,9 @@ private boolean initCheck() {
11061107
return false;
11071108
}
11081109

1109-
// If Android 12 or newer make sure we have BT Runtime permissions
1110+
// If Android 12 or newer make sure we have BLUETOOTH_CONNECT Runtime permission
11101111
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isBtConnectPermissionGranted(this, this.getPackageName())) {
1111-
if (!isConnectedOverUSB) {
1112+
if (!isConnectedOverUSB) { //If BLUETOOTH_CONNECT permission is not granted We want to make sure we are connected over USB
11121113
return false;
11131114
}
11141115
}
@@ -1142,6 +1143,7 @@ private boolean initCheck() {
11421143
return true;
11431144
}
11441145

1146+
11451147
@Override
11461148
public void onCreate() {
11471149
super.onCreate();
@@ -1176,7 +1178,6 @@ protected void deployNextRouterService() {
11761178
if (info.getRouterServiceComponentName().equals(name) && listSize > i + 1) {
11771179
SdlAppInfo nextUp = sdlAppInfoList.get(i + 1);
11781180
Intent serviceIntent = new Intent();
1179-
//Add check if it is second pass also add extra
11801181
serviceIntent.setComponent(nextUp.getRouterServiceComponentName());
11811182
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
11821183
startService(serviceIntent);
@@ -1195,7 +1196,6 @@ protected void deployNextRouterService() {
11951196
DebugTool.logInfo(TAG, "No sdl apps found");
11961197
return;
11971198
}
1198-
11991199
closing = true;
12001200
closeBluetoothSerialServer();
12011201
notifyAltTransportOfClose(TransportConstants.ROUTER_SHUTTING_DOWN_REASON_NEWER_SERVICE);
@@ -1785,6 +1785,7 @@ public void closeSelf() {
17851785

17861786
private synchronized void initBluetoothSerialService() {
17871787
if (waitingForBTRuntimePermissions) {
1788+
//The app has not be granted the BLUETOOTH_CONNECT runtime permission
17881789
return;
17891790
}
17901791

@@ -1845,6 +1846,11 @@ public void onTransportConnected(final TransportRecord record) {
18451846
startService.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
18461847

18471848
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
1849+
//Starting in Android 12 we need to start services from a foreground context
1850+
//To enable developers to be able to start their SdlService from the "background"
1851+
//we will attach a pendingIntent as an extra to the intent
1852+
//the developer can use this pendingIntent to start their SdlService from the context of
1853+
//the active RouterService
18481854
Intent pending = new Intent();
18491855
PendingIntent pendingIntent = PendingIntent.getForegroundService(this, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT);
18501856
startService.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent);
@@ -1859,10 +1865,10 @@ public void onTransportConnected(final TransportRecord record) {
18591865
}
18601866

18611867
if (isConnectedOverUSB && !AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) {
1862-
//Delay starting bluetoothTransport
1868+
//Delay starting bluetoothTransport when we are connected over USB and the app does not have the BLUETOOTH_CONNECT permissions
18631869
waitingForBTRuntimePermissions = true;
18641870
btPermissionsHandler = new Handler(Looper.myLooper());
1865-
//Continuously Check for the Bluetooth Permissions
1871+
//Continuously Check for the BLUETOOTH_CONNECT Permission
18661872
btPermissionsRunnable = new Runnable() {
18671873
@Override
18681874
public void run() {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
package com.smartdevicelink.util;
3434

35-
import android.app.PendingIntent;
3635
import android.content.ComponentName;
3736
import android.content.Context;
3837
import android.content.Intent;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
package com.smartdevicelink.util;
3434

3535
import android.Manifest;
36-
import android.content.ComponentName;
3736
import android.content.Context;
3837
import android.content.Intent;
3938
import android.content.pm.ActivityInfo;
4039
import android.content.pm.PackageInfo;
4140
import android.content.pm.PackageManager;
4241
import android.content.pm.ResolveInfo;
43-
import android.content.pm.ServiceInfo;
4442
import android.os.Build;
4543

4644
import com.smartdevicelink.R;

0 commit comments

Comments
 (0)