diff options
author | RHenigan <heniganr1@gmail.com> | 2022-02-21 13:47:23 -0500 |
---|---|---|
committer | RHenigan <heniganr1@gmail.com> | 2022-02-21 13:47:23 -0500 |
commit | 48f7cb0a330062d2be3cf694bb5a984492a78e32 (patch) | |
tree | 8774065e98ff422f8ee0c7dfcb8bb63ed1cbb8cb | |
parent | 3b2f02bcc381fb6079623e41951bbd75958c1072 (diff) | |
download | sdl_android-48f7cb0a330062d2be3cf694bb5a984492a78e32.tar.gz |
Dev Review Fixes
6 files changed, 54 insertions, 66 deletions
diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java index 704239246..4761c15c4 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java @@ -29,7 +29,7 @@ public class MainActivity extends AppCompatActivity { if (!checkPermission()) { requestPermission(); } - } else { + } else if (BuildConfig.TRANSPORT.equals("TCP")){ //If we are connected to a module we want to start our SdlService SdlReceiver.queryForConnectedService(this); } @@ -40,9 +40,7 @@ public class MainActivity extends AppCompatActivity { } private boolean checkPermission() { - int btConnectPermission = ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT); - - return btConnectPermission == PackageManager.PERMISSION_GRANTED; + return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT); } private void requestPermission() { @@ -55,9 +53,9 @@ public class MainActivity extends AppCompatActivity { case REQUEST_CODE: if (grantResults.length > 0) { - boolean connectAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + boolean btConnectGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED; - if (connectAccepted) { + if (btConnectGranted) { SdlReceiver.queryForConnectedService(this); } } 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 2690d153f..e130d3d78 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 @@ -22,8 +22,8 @@ public class SdlReceiver extends SdlBroadcastReceiver { // We will check the intent for a pendingIntent parcelable extra // This pendingIntent allows us to start the SdlService from the context of the active router service which is in the foreground if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - if (intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA) != null) { - PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA); + PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA); + if (pendingIntent != null) { try { pendingIntent.send(context, 0, intent); } catch (PendingIntent.CanceledException e) { @@ -54,6 +54,6 @@ public class SdlReceiver extends SdlBroadcastReceiver { @Override public String getSdlServiceName() { - return "SdlService"; + return SdlService.class.getSimpleName(); } }
\ No newline at end of file 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 4bea676b8..7bed1b482 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 @@ -66,6 +66,7 @@ import java.util.Locale; import java.util.Vector; import java.util.concurrent.ConcurrentLinkedQueue; +import static android.Manifest.permission.BLUETOOTH_CONNECT; import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA; public abstract class SdlBroadcastReceiver extends BroadcastReceiver { @@ -91,7 +92,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { private static Thread.UncaughtExceptionHandler foregroundExceptionHandler = null; private static final Object DEVICE_LISTENER_LOCK = new Object(); private static SdlDeviceListener sdlDeviceListener; - private static String serviceName; + private static String serviceName = null; public int getRouterServiceVersion() { return SdlRouterService.ROUTER_SERVICE_VERSION_NUMBER; @@ -100,7 +101,6 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { @Override @CallSuper public void onReceive(Context context, Intent intent) { - serviceName = getSdlServiceName(); //Log.i(TAG, "Sdl Receiver Activated"); final String action = intent.getAction(); @@ -136,6 +136,10 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); } + if (serviceName == null) { + serviceName = getSdlServiceName(); + } + boolean didStart = false; if (localRouterClass == null) { localRouterClass = defineLocalSdlRouterClass(); @@ -301,18 +305,10 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { 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) { - boolean preAndroid12RouterServiceOnDevice = false; - for (SdlAppInfo appInfo : sdlAppInfoList) { - //If an installed app RS version is older than Android 12 update version (16) - if (appInfo.getRouterServiceVersion() < ANDROID_12_ROUTER_SERVICE_VERSION) { - preAndroid12RouterServiceOnDevice = true; - break; - } - } // 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 - if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) { + if (!isPreAndroid12RSOnDevice(sdlAppInfoList) && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, routerServicePackage) && sdlAppInfoList.size() > 1) { for (SdlAppInfo appInfo : sdlAppInfoList) { - if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { + if (AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, appInfo.getRouterServiceComponentName().getPackageName())) { //If this app in the list has BT Connect permissions, we want to use that apps RS routerServicePackage = appInfo.getRouterServiceComponentName().getPackageName(); break; @@ -625,18 +621,10 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty()) { ComponentName routerService = sdlAppInfoList.get(0).getRouterServiceComponentName(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - boolean preAndroid12RouterServiceOnDevice = false; - for (SdlAppInfo appInfo : sdlAppInfoList) { - //If an installed app RS version is older than Android 12 update version (16) - if (appInfo.getRouterServiceVersion() < ANDROID_12_ROUTER_SERVICE_VERSION) { - preAndroid12RouterServiceOnDevice = true; - break; - } - } // 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 - if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) { + if (!isPreAndroid12RSOnDevice(sdlAppInfoList) && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) { for (SdlAppInfo appInfo : sdlAppInfoList) { - if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { + if (AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, context, appInfo.getRouterServiceComponentName().getPackageName())) { routerService = appInfo.getRouterServiceComponentName(); //If this app in the list has BT Connect permissions, we want to use that apps RS break; @@ -680,6 +668,16 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { } } + private static boolean isPreAndroid12RSOnDevice(List<SdlAppInfo> sdlAppInfoList) { + for (SdlAppInfo appInfo : sdlAppInfoList) { + //If an installed app RS version is older than Android 12 update version (16) + if (appInfo.getRouterServiceVersion() < ANDROID_12_ROUTER_SERVICE_VERSION) { + return true; + } + } + return false; + } + /** * We need to define this for local copy of the Sdl Router Service class. * It will be the main point of connection for Sdl enabled apps @@ -712,7 +710,6 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { public String getSdlServiceName() { return "SdlService"; } - //public abstract void onSdlDisabled(Context context); //Removing for now until we're able to abstract from developer 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 970e818ee..f140ccef2 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 @@ -120,6 +120,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import static android.Manifest.permission.BLUETOOTH_CONNECT; +import static android.Manifest.permission.BLUETOOTH_SCAN; import static com.smartdevicelink.transport.TransportConstants.CONNECTED_DEVICE_STRING_EXTRA_NAME; import static com.smartdevicelink.transport.TransportConstants.FOREGROUND_EXTRA; import static com.smartdevicelink.transport.TransportConstants.FORMED_PACKET_EXTRA_NAME; @@ -217,7 +219,6 @@ public class SdlRouterService extends Service { private boolean startSequenceComplete = false; private VehicleType receivedVehicleType; - private boolean isConnectedOverUSB; private boolean waitingForBTRuntimePermissions = false; private Handler btPermissionsHandler; private Runnable btPermissionsRunnable; @@ -373,7 +374,8 @@ public class SdlRouterService extends Service { switch (msg.what) { case TransportConstants.ROUTER_REQUEST_BT_CLIENT_CONNECT: //Starting with Android 12 this use case will require the BLUETOOTH_SCAN PERMISSION - if (!AndroidTools.isBtScanPermissionGranted(service.getApplicationContext(), service.getPackageName())) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isPermissionGranted(BLUETOOTH_SCAN, service.getApplicationContext(), service.getPackageName())) { + DebugTool.logError(TAG, "BLUETOOTH_SCAN Permissions not granted for this app"); break; } if (receivedBundle.getBoolean(TransportConstants.CONNECT_AS_CLIENT_BOOLEAN_EXTRA, false) @@ -1096,7 +1098,7 @@ public class SdlRouterService extends Service { * * @return true if this service is set up correctly */ - private boolean initCheck() { + private boolean initCheck(boolean isConnectedOverUSB) { if (!processCheck()) { DebugTool.logError(TAG, "Not using correct process. Shutting down"); wrongProcess = true; @@ -1108,7 +1110,7 @@ public class SdlRouterService extends Service { } // If Android 12 or newer make sure we have BLUETOOTH_CONNECT Runtime permission - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isBtConnectPermissionGranted(this, this.getPackageName())) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, this, this.getPackageName())) { if (!isConnectedOverUSB) { //If BLUETOOTH_CONNECT permission is not granted We want to make sure we are connected over USB return false; } @@ -1275,13 +1277,14 @@ public class SdlRouterService extends Service { (HashMap<String, Object>) intent.getSerializableExtra(TransportConstants.VEHICLE_INFO_EXTRA) ); } + boolean isConnectedOverUSB = false; if (intent != null && intent.hasExtra(TransportConstants.CONNECTION_TYPE_EXTRA)) { isConnectedOverUSB = TransportConstants.ACTION_USB_ACCESSORY_ATTACHED.equalsIgnoreCase(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; - if (!initCheck()) { // Run checks on process and permissions + if (!initCheck(isConnectedOverUSB)) { // Run checks on process and permissions deployNextRouterService(); closeSelf(); return START_REDELIVER_INTENT; @@ -1306,7 +1309,6 @@ public class SdlRouterService extends Service { } if (intent != null) { if (intent.getBooleanExtra(FOREGROUND_EXTRA, false)) { - hasConnectedBefore = false; hasCalledStartForeground = false; 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 class SdlRouterService extends Service { altTransportTimerHandler = null; } + if (btPermissionsHandler != null && btPermissionsRunnable != null) { + btPermissionsHandler.removeCallbacks(btPermissionsRunnable); + } + DebugTool.logWarning(TAG, "Sdl Router Service Destroyed"); closing = true; //No need for this Broadcast Receiver anymore @@ -1723,6 +1729,9 @@ public class SdlRouterService extends Service { */ @SuppressWarnings("MissingPermission") private boolean bluetoothAvailable() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, SdlRouterService.this, SdlRouterService.this.getPackageName())) { + return false; + } try { return (!(BluetoothAdapter.getDefaultAdapter() == null) && BluetoothAdapter.getDefaultAdapter().isEnabled()); } catch (NullPointerException e) { // only for BluetoothAdapter.getDefaultAdapter().isEnabled() call @@ -1785,7 +1794,7 @@ public class SdlRouterService extends Service { private synchronized void initBluetoothSerialService() { if (waitingForBTRuntimePermissions) { - //The app has not be granted the BLUETOOTH_CONNECT runtime permission + DebugTool.logWarning(TAG, "This app has not been granted the BLUETOOTH_CONNECT runtime permission"); return; } @@ -1864,7 +1873,7 @@ public class SdlRouterService extends Service { notifyClients(createHardwareConnectedMessage(record)); } - if (isConnectedOverUSB && !AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && TransportType.USB.equals(record.getType()) && !AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, SdlRouterService.this, SdlRouterService.this.getPackageName())) { //Delay starting bluetoothTransport when we are connected over USB and the app does not have the BLUETOOTH_CONNECT permissions waitingForBTRuntimePermissions = true; btPermissionsHandler = new Handler(Looper.myLooper()); @@ -1872,7 +1881,7 @@ public class SdlRouterService extends Service { btPermissionsRunnable = new Runnable() { @Override public void run() { - if (!AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) { + if (!AndroidTools.isPermissionGranted(BLUETOOTH_CONNECT, SdlRouterService.this, SdlRouterService.this.getPackageName())) { btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY); } else { waitingForBTRuntimePermissions = false; 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 e8f88c259..3d08f7858 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 @@ -203,38 +203,22 @@ public class AndroidTools { return sdlAppInfoList; } - public static boolean isBtConnectPermissionGranted(Context context, String servicePackageName) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { - //Permissions are only for SDK 31 and above - return true; - } + public static boolean isPermissionGranted(String permissionName, Context context, String servicePackageName) { PackageManager packageManager = context.getPackageManager(); - PackageInfo packageInfo; - try { - packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS); - int btConnectPermission = packageManager.checkPermission(BLUETOOTH_CONNECT, packageInfo.packageName); - return btConnectPermission == PackageManager.PERMISSION_GRANTED; - } catch (NameNotFoundException e) { - e.printStackTrace(); - DebugTool.logError(TAG, "servicePackageName not found while checking BT Connect permissions"); + if (packageManager == null) { return false; } - } - - public static boolean isBtScanPermissionGranted(Context context, String servicePackageName) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { - //Permissions are only for SDK 31 and above - return true; - } - PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo; try { packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS); - int btScanPermission = packageManager.checkPermission(BLUETOOTH_SCAN, packageInfo.packageName); - return btScanPermission == PackageManager.PERMISSION_GRANTED; + if (packageInfo == null) { + return false; + } + int permissionResult = packageManager.checkPermission(permissionName, packageInfo.packageName); + return permissionResult == PackageManager.PERMISSION_GRANTED; } catch (NameNotFoundException e) { e.printStackTrace(); - DebugTool.logError(TAG, "servicePackageName not found while checking BT SCAN permissions"); + DebugTool.logError(TAG, "servicePackageName not found while checking " + permissionName + " permission", e); return false; } } diff --git a/android/sdl_android/src/main/res/values/strings.xml b/android/sdl_android/src/main/res/values/strings.xml index e9858a1c9..955b86239 100644 --- a/android/sdl_android/src/main/res/values/strings.xml +++ b/android/sdl_android/src/main/res/values/strings.xml @@ -5,7 +5,7 @@ <string name="lockscreen_device_image_description">Device Icon</string> <string name="default_lockscreen_warning_message">Swipe down to dismiss, acknowledging that you are not the driver.</string> <string name="spp_out_of_resource">Too many apps are using Bluetooth</string> - <string name="allow_bluetooth_permissions">Please click here and allow the app to use bluetooth permissions</string> + <string name="allow_bluetooth_permissions">Please grant this app bluetooth permissions</string> <string name="notification_title">SmartDeviceLink</string> <string name="sdl_error_notification_channel_name">SDL Error</string> </resources>
\ No newline at end of file |