Skip to content

Commit 48f7cb0

Browse files
committed
Dev Review Fixes
1 parent 3b2f02b commit 48f7cb0

6 files changed

Lines changed: 54 additions & 66 deletions

File tree

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected void onCreate(Bundle savedInstanceState) {
2929
if (!checkPermission()) {
3030
requestPermission();
3131
}
32-
} else {
32+
} else if (BuildConfig.TRANSPORT.equals("TCP")){
3333
//If we are connected to a module we want to start our SdlService
3434
SdlReceiver.queryForConnectedService(this);
3535
}
@@ -40,9 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
4040
}
4141

4242
private boolean checkPermission() {
43-
int btConnectPermission = ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT);
44-
45-
return btConnectPermission == PackageManager.PERMISSION_GRANTED;
43+
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT);
4644
}
4745

4846
private void requestPermission() {
@@ -55,9 +53,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
5553
case REQUEST_CODE:
5654
if (grantResults.length > 0) {
5755

58-
boolean connectAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
56+
boolean btConnectGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
5957

60-
if (connectAccepted) {
58+
if (btConnectGranted) {
6159
SdlReceiver.queryForConnectedService(this);
6260
}
6361
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public void onSdlEnabled(Context context, Intent intent) {
2222
// We will check the intent for a pendingIntent parcelable extra
2323
// This pendingIntent allows us to start the SdlService from the context of the active router service which is in the foreground
2424
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
25-
if (intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA) != null) {
26-
PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA);
25+
PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA);
26+
if (pendingIntent != null) {
2727
try {
2828
pendingIntent.send(context, 0, intent);
2929
} catch (PendingIntent.CanceledException e) {
@@ -54,6 +54,6 @@ public void onReceive(Context context, Intent intent) {
5454

5555
@Override
5656
public String getSdlServiceName() {
57-
return "SdlService";
57+
return SdlService.class.getSimpleName();
5858
}
5959
}

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import java.util.Vector;
6767
import java.util.concurrent.ConcurrentLinkedQueue;
6868

69+
import static android.Manifest.permission.BLUETOOTH_CONNECT;
6970
import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA;
7071

7172
public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
@@ -91,7 +92,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
9192
private static Thread.UncaughtExceptionHandler foregroundExceptionHandler = null;
9293
private static final Object DEVICE_LISTENER_LOCK = new Object();
9394
private static SdlDeviceListener sdlDeviceListener;
94-
private static String serviceName;
95+
private static String serviceName = null;
9596

9697
public int getRouterServiceVersion() {
9798
return SdlRouterService.ROUTER_SERVICE_VERSION_NUMBER;
@@ -100,7 +101,6 @@ public int getRouterServiceVersion() {
100101
@Override
101102
@CallSuper
102103
public void onReceive(Context context, Intent intent) {
103-
serviceName = getSdlServiceName();
104104

105105
//Log.i(TAG, "Sdl Receiver Activated");
106106
final String action = intent.getAction();
@@ -136,6 +136,10 @@ public void onReceive(Context context, Intent intent) {
136136
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
137137
}
138138

139+
if (serviceName == null) {
140+
serviceName = getSdlServiceName();
141+
}
142+
139143
boolean didStart = false;
140144
if (localRouterClass == null) {
141145
localRouterClass = defineLocalSdlRouterClass();
@@ -301,18 +305,10 @@ public void onComplete(Vector<ComponentName> routerServices) {
301305
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty() && sdlAppInfoList.get(0).getRouterServiceComponentName() != null) {
302306
routerServicePackage = sdlAppInfoList.get(0).getRouterServiceComponentName().getPackageName();
303307
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
304-
boolean preAndroid12RouterServiceOnDevice = false;
305-
for (SdlAppInfo appInfo : sdlAppInfoList) {
306-
//If an installed app RS version is older than Android 12 update version (16)
307-
if (appInfo.getRouterServiceVersion() < ANDROID_12_ROUTER_SERVICE_VERSION) {
308-
preAndroid12RouterServiceOnDevice = true;
309-
break;
310-
}
311-
}
312308
// 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
313-
if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) {
309+
if (!isPreAndroid12RSOnDevice(sdlAppInfoList) && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, routerServicePackage) && sdlAppInfoList.size() > 1) {
314310
for (SdlAppInfo appInfo : sdlAppInfoList) {
315-
if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
311+
if (AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, appInfo.getRouterServiceComponentName().getPackageName())) {
316312
//If this app in the list has BT Connect permissions, we want to use that apps RS
317313
routerServicePackage = appInfo.getRouterServiceComponentName().getPackageName();
318314
break;
@@ -625,18 +621,10 @@ public boolean onTransportConnected(Context context, BluetoothDevice bluetoothDe
625621
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) {
626622
ComponentName routerService = sdlAppInfoList.get(0).getRouterServiceComponentName();
627623
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
628-
boolean preAndroid12RouterServiceOnDevice = false;
629-
for (SdlAppInfo appInfo : sdlAppInfoList) {
630-
//If an installed app RS version is older than Android 12 update version (16)
631-
if (appInfo.getRouterServiceVersion() < ANDROID_12_ROUTER_SERVICE_VERSION) {
632-
preAndroid12RouterServiceOnDevice = true;
633-
break;
634-
}
635-
}
636624
// 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
637-
if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) {
625+
if (!isPreAndroid12RSOnDevice(sdlAppInfoList) && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) {
638626
for (SdlAppInfo appInfo : sdlAppInfoList) {
639-
if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
627+
if (AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, appInfo.getRouterServiceComponentName().getPackageName())) {
640628
routerService = appInfo.getRouterServiceComponentName();
641629
//If this app in the list has BT Connect permissions, we want to use that apps RS
642630
break;
@@ -680,6 +668,16 @@ public static ComponentName consumeQueuedRouterService() {
680668
}
681669
}
682670

671+
private static boolean isPreAndroid12RSOnDevice(List<SdlAppInfo> sdlAppInfoList) {
672+
for (SdlAppInfo appInfo : sdlAppInfoList) {
673+
//If an installed app RS version is older than Android 12 update version (16)
674+
if (appInfo.getRouterServiceVersion() < ANDROID_12_ROUTER_SERVICE_VERSION) {
675+
return true;
676+
}
677+
}
678+
return false;
679+
}
680+
683681
/**
684682
* We need to define this for local copy of the Sdl Router Service class.
685683
* It will be the main point of connection for Sdl enabled apps
@@ -712,7 +710,6 @@ public static ComponentName consumeQueuedRouterService() {
712710
public String getSdlServiceName() {
713711
return "SdlService";
714712
}
715-
716713
//public abstract void onSdlDisabled(Context context); //Removing for now until we're able to abstract from developer
717714

718715

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
import java.util.concurrent.ScheduledExecutorService;
121121
import java.util.concurrent.TimeUnit;
122122

123+
import static android.Manifest.permission.BLUETOOTH_CONNECT;
124+
import static android.Manifest.permission.BLUETOOTH_SCAN;
123125
import static com.smartdevicelink.transport.TransportConstants.CONNECTED_DEVICE_STRING_EXTRA_NAME;
124126
import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA;
125127
import static com.smartdevicelink.transport.TransportConstants.FORMED_PACKET_EXTRA_NAME;
@@ -217,7 +219,6 @@ public class SdlRouterService extends Service {
217219

218220
private boolean startSequenceComplete = false;
219221
private VehicleType receivedVehicleType;
220-
private boolean isConnectedOverUSB;
221222
private boolean waitingForBTRuntimePermissions = false;
222223
private Handler btPermissionsHandler;
223224
private Runnable btPermissionsRunnable;
@@ -373,7 +374,8 @@ public void handleMessage(Message msg) {
373374
switch (msg.what) {
374375
case TransportConstants.ROUTER_REQUEST_BT_CLIENT_CONNECT:
375376
//Starting with Android 12 this use case will require the BLUETOOTH_SCAN PERMISSION
376-
if (!AndroidTools.isBtScanPermissionGranted(service.getApplicationContext(), service.getPackageName())) {
377+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isPermissionGranted(BLUETOOTH_SCAN, service.getApplicationContext(), service.getPackageName())) {
378+
DebugTool.logError(TAG, "BLUETOOTH_SCAN Permissions not granted for this app");
377379
break;
378380
}
379381
if (receivedBundle.getBoolean(TransportConstants.CONNECT_AS_CLIENT_BOOLEAN_EXTRA, false)
@@ -1096,7 +1098,7 @@ private boolean permissionCheck(String permissionToCheck) {
10961098
*
10971099
* @return true if this service is set up correctly
10981100
*/
1099-
private boolean initCheck() {
1101+
private boolean initCheck(boolean isConnectedOverUSB) {
11001102
if (!processCheck()) {
11011103
DebugTool.logError(TAG, "Not using correct process. Shutting down");
11021104
wrongProcess = true;
@@ -1108,7 +1110,7 @@ private boolean initCheck() {
11081110
}
11091111

11101112
// If Android 12 or newer make sure we have BLUETOOTH_CONNECT Runtime permission
1111-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isBtConnectPermissionGranted(this, this.getPackageName())) {
1113+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, this, this.getPackageName())) {
11121114
if (!isConnectedOverUSB) { //If BLUETOOTH_CONNECT permission is not granted We want to make sure we are connected over USB
11131115
return false;
11141116
}
@@ -1275,13 +1277,14 @@ public int onStartCommand(Intent intent, int flags, int startId) {
12751277
(HashMap<String, Object>) intent.getSerializableExtra(TransportConstants.VEHICLE_INFO_EXTRA)
12761278
);
12771279
}
1280+
boolean isConnectedOverUSB = false;
12781281
if (intent != null && intent.hasExtra(TransportConstants.CONNECTION_TYPE_EXTRA)) {
12791282
isConnectedOverUSB = TransportConstants.ACTION_USB_ACCESSORY_ATTACHED.equalsIgnoreCase(intent.getStringExtra(TransportConstants.CONNECTION_TYPE_EXTRA));
12801283
}
12811284
// 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.
12821285
if (firstStart) {
12831286
firstStart = false;
1284-
if (!initCheck()) { // Run checks on process and permissions
1287+
if (!initCheck(isConnectedOverUSB)) { // Run checks on process and permissions
12851288
deployNextRouterService();
12861289
closeSelf();
12871290
return START_REDELIVER_INTENT;
@@ -1306,7 +1309,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
13061309
}
13071310
if (intent != null) {
13081311
if (intent.getBooleanExtra(FOREGROUND_EXTRA, false)) {
1309-
hasConnectedBefore = false;
13101312
hasCalledStartForeground = false;
13111313

13121314
if (!this.isPrimaryTransportConnected()) { //If there is no transport connected we need to ensure the service is moved to the foreground
@@ -1361,6 +1363,10 @@ public void onDestroy() {
13611363
altTransportTimerHandler = null;
13621364
}
13631365

1366+
if (btPermissionsHandler != null && btPermissionsRunnable != null) {
1367+
btPermissionsHandler.removeCallbacks(btPermissionsRunnable);
1368+
}
1369+
13641370
DebugTool.logWarning(TAG, "Sdl Router Service Destroyed");
13651371
closing = true;
13661372
//No need for this Broadcast Receiver anymore
@@ -1723,6 +1729,9 @@ private boolean isTransportConnected(TransportType transportType) {
17231729
*/
17241730
@SuppressWarnings("MissingPermission")
17251731
private boolean bluetoothAvailable() {
1732+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, SdlRouterService.this, SdlRouterService.this.getPackageName())) {
1733+
return false;
1734+
}
17261735
try {
17271736
return (!(BluetoothAdapter.getDefaultAdapter() == null) && BluetoothAdapter.getDefaultAdapter().isEnabled());
17281737
} catch (NullPointerException e) { // only for BluetoothAdapter.getDefaultAdapter().isEnabled() call
@@ -1785,7 +1794,7 @@ public void closeSelf() {
17851794

17861795
private synchronized void initBluetoothSerialService() {
17871796
if (waitingForBTRuntimePermissions) {
1788-
//The app has not be granted the BLUETOOTH_CONNECT runtime permission
1797+
DebugTool.logWarning(TAG, "This app has not been granted the BLUETOOTH_CONNECT runtime permission");
17891798
return;
17901799
}
17911800

@@ -1864,15 +1873,15 @@ public void onTransportConnected(final TransportRecord record) {
18641873
notifyClients(createHardwareConnectedMessage(record));
18651874
}
18661875

1867-
if (isConnectedOverUSB && !AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) {
1876+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && TransportType.USB.equals(record.getType()) && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, SdlRouterService.this, SdlRouterService.this.getPackageName())) {
18681877
//Delay starting bluetoothTransport when we are connected over USB and the app does not have the BLUETOOTH_CONNECT permissions
18691878
waitingForBTRuntimePermissions = true;
18701879
btPermissionsHandler = new Handler(Looper.myLooper());
18711880
//Continuously Check for the BLUETOOTH_CONNECT Permission
18721881
btPermissionsRunnable = new Runnable() {
18731882
@Override
18741883
public void run() {
1875-
if (!AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) {
1884+
if (!AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, SdlRouterService.this, SdlRouterService.this.getPackageName())) {
18761885
btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY);
18771886
} else {
18781887
waitingForBTRuntimePermissions = false;

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -203,38 +203,22 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
203203
return sdlAppInfoList;
204204
}
205205

206-
public static boolean isBtConnectPermissionGranted(Context context, String servicePackageName) {
207-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
208-
//Permissions are only for SDK 31 and above
209-
return true;
210-
}
206+
public static boolean isPermissionGranted(String permissionName, Context context, String servicePackageName) {
211207
PackageManager packageManager = context.getPackageManager();
212-
PackageInfo packageInfo;
213-
try {
214-
packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS);
215-
int btConnectPermission = packageManager.checkPermission(BLUETOOTH_CONNECT, packageInfo.packageName);
216-
return btConnectPermission == PackageManager.PERMISSION_GRANTED;
217-
} catch (NameNotFoundException e) {
218-
e.printStackTrace();
219-
DebugTool.logError(TAG, "servicePackageName not found while checking BT Connect permissions");
208+
if (packageManager == null) {
220209
return false;
221210
}
222-
}
223-
224-
public static boolean isBtScanPermissionGranted(Context context, String servicePackageName) {
225-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
226-
//Permissions are only for SDK 31 and above
227-
return true;
228-
}
229-
PackageManager packageManager = context.getPackageManager();
230211
PackageInfo packageInfo;
231212
try {
232213
packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS);
233-
int btScanPermission = packageManager.checkPermission(BLUETOOTH_SCAN, packageInfo.packageName);
234-
return btScanPermission == PackageManager.PERMISSION_GRANTED;
214+
if (packageInfo == null) {
215+
return false;
216+
}
217+
int permissionResult = packageManager.checkPermission(permissionName, packageInfo.packageName);
218+
return permissionResult == PackageManager.PERMISSION_GRANTED;
235219
} catch (NameNotFoundException e) {
236220
e.printStackTrace();
237-
DebugTool.logError(TAG, "servicePackageName not found while checking BT SCAN permissions");
221+
DebugTool.logError(TAG, "servicePackageName not found while checking " + permissionName + " permission", e);
238222
return false;
239223
}
240224
}

android/sdl_android/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<string name="lockscreen_device_image_description">Device Icon</string>
66
<string name="default_lockscreen_warning_message">Swipe down to dismiss, acknowledging that you are not the driver.</string>
77
<string name="spp_out_of_resource">Too many apps are using Bluetooth</string>
8-
<string name="allow_bluetooth_permissions">Please click here and allow the app to use bluetooth permissions</string>
8+
<string name="allow_bluetooth_permissions">Please grant this app bluetooth permissions</string>
99
<string name="notification_title">SmartDeviceLink</string>
1010
<string name="sdl_error_notification_channel_name">SDL Error</string>
1111
</resources>

0 commit comments

Comments
 (0)