From 0dfdb8711cca99b85a9ab471477f29ffff5cbb02 Mon Sep 17 00:00:00 2001 From: RHenigan Date: Fri, 11 Feb 2022 14:43:18 -0500 Subject: Proposal Revision Changes --- .../hello_sdl_android/src/main/AndroidManifest.xml | 3 -- .../java/com/sdl/hellosdlandroid/MainActivity.java | 6 +-- .../java/com/sdl/hellosdlandroid/SdlReceiver.java | 5 +++ .../transport/SdlBroadcastReceiver.java | 36 ++++++++++++--- .../transport/SdlRouterService.java | 51 ++++++++++++---------- .../com/smartdevicelink/util/AndroidTools.java | 30 ++++++++----- 6 files changed, 85 insertions(+), 46 deletions(-) (limited to 'android') diff --git a/android/hello_sdl_android/src/main/AndroidManifest.xml b/android/hello_sdl_android/src/main/AndroidManifest.xml index 636d236ad..415aa66c2 100755 --- a/android/hello_sdl_android/src/main/AndroidManifest.xml +++ b/android/hello_sdl_android/src/main/AndroidManifest.xml @@ -6,9 +6,6 @@ - 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 ac0f2bf5e..b5e4b5e76 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 @@ -13,7 +13,6 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import static android.Manifest.permission.BLUETOOTH_CONNECT; -import static android.Manifest.permission.BLUETOOTH_SCAN; public class MainActivity extends AppCompatActivity { @@ -42,13 +41,12 @@ public class MainActivity extends AppCompatActivity { private boolean checkPermission() { int btConnectPermission = ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT); - int btScanPermission = ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_SCAN); - return btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED; + return btConnectPermission == PackageManager.PERMISSION_GRANTED; } private void requestPermission() { - ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT, BLUETOOTH_SCAN}, REQUEST_CODE); + ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT}, REQUEST_CODE); } @Override 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 770513e94..758906190 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 @@ -48,4 +48,9 @@ public class SdlReceiver extends SdlBroadcastReceiver { public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); // Required if overriding this method } + + @Override + public String getSdlServiceName() { + return "SdlService"; + } } \ 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 c5ce98395..51e59675e 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 @@ -90,6 +90,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; public int getRouterServiceVersion() { return SdlRouterService.ROUTER_SERVICE_VERSION_NUMBER; @@ -98,6 +99,8 @@ 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(); if (action == null) { @@ -297,9 +300,17 @@ 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) { - if (!AndroidTools.areBtPermissionsGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) { + boolean preAndroid12RouterServiceOnDevice = false; + for (SdlAppInfo appInfo : sdlAppInfoList) { + //If the RS version is older than Android 12 update version + if (appInfo.getRouterServiceVersion() < 16) { + preAndroid12RouterServiceOnDevice = true; + break; + } + } + if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) { for (SdlAppInfo appInfo : sdlAppInfoList) { - if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { + if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { routerServicePackage = appInfo.getRouterServiceComponentName().getPackageName(); break; } @@ -388,10 +399,9 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { public void uncaughtException(Thread t, Throwable e) { if (e != null && e instanceof AndroidRuntimeException - && "android.app.RemoteServiceException".equals(e.getClass().getName()) //android.app.RemoteServiceException is a private class + && ("android.app.RemoteServiceException".equals(e.getClass().getName()) || "android.app.ForegroundServiceDidNotStartInTimeException".equals(e.getClass().getName())) //android.app.RemoteServiceException is a private class && e.getMessage() != null - && e.getMessage().contains("SdlRouterService")) { - + && (e.getMessage().contains("SdlRouterService")) || e.getMessage().contains(serviceName)) { DebugTool.logInfo(TAG, "Handling failed startForegroundService call"); Looper.loop(); } else if (defaultUncaughtExceptionHandler != null) { //No other exception should be handled @@ -611,9 +621,17 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { //If we are on android 12 check the app has BT permissions //If it does not try to find another app in the list that does; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - if (!AndroidTools.areBtPermissionsGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) { + boolean preAndroid12RouterServiceOnDevice = false; + for (SdlAppInfo appInfo : sdlAppInfoList) { + //If the RS version is older than Android 12 update version + if (appInfo.getRouterServiceVersion() < 16) { + preAndroid12RouterServiceOnDevice = true; + break; + } + } + if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) { for (SdlAppInfo appInfo : sdlAppInfoList) { - if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { + if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) { routerService = appInfo.getRouterServiceComponentName(); break; } @@ -678,6 +696,10 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { */ public abstract void onSdlEnabled(Context context, Intent intent); + 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 3404f4dd4..302e791b9 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 @@ -372,6 +372,9 @@ public class SdlRouterService extends Service { switch (msg.what) { case TransportConstants.ROUTER_REQUEST_BT_CLIENT_CONNECT: + if (!AndroidTools.isBtScanPermissionGranted(service.getApplicationContext(), service.getPackageName())) { + break; + } if (receivedBundle.getBoolean(TransportConstants.CONNECT_AS_CLIENT_BOOLEAN_EXTRA, false) && !connectAsClient) { //We check this flag to make sure we don't try to connect over and over again. On D/C we should set to false //Log.d(TAG,"Attempting to connect as bt client"); @@ -1104,24 +1107,8 @@ public class SdlRouterService extends Service { } // If Android 12 or newer make sure we have BT Runtime permissions - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.areBtPermissionsGranted(this, this.getPackageName())) { - if (isConnectedOverUSB) { - waitingForBTRuntimePermissions = true; - btPermissionsHandler = new Handler(Looper.myLooper()); - btPermissionsRunnable = new Runnable() { - @Override - public void run() { - if (!AndroidTools.areBtPermissionsGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) { - btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY); - } else { - waitingForBTRuntimePermissions = false; - initBluetoothSerialService(); - } - } - }; - btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY); - showBTPermissionsNotification(); - } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isBtConnectPermissionGranted(this, this.getPackageName())) { + if (!isConnectedOverUSB) { return false; } } @@ -1858,7 +1845,9 @@ public class SdlRouterService extends Service { startService.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - startService.putExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, true); + Intent pending = new Intent(); + PendingIntent pendingIntent = PendingIntent.getForegroundService(this, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT); + startService.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent); } AndroidTools.sendExplicitBroadcast(getApplicationContext(), startService, null); @@ -1868,6 +1857,27 @@ public class SdlRouterService extends Service { //If we have clients notifyClients(createHardwareConnectedMessage(record)); } + + if (isConnectedOverUSB) { + //Delay starting bluetoothTransport + waitingForBTRuntimePermissions = true; + btPermissionsHandler = new Handler(Looper.myLooper()); + //Continuously Check for the Bluetooth Permissions + btPermissionsRunnable = new Runnable() { + @Override + public void run() { + if (!AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) { + btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY); + } else { + waitingForBTRuntimePermissions = false; + initBluetoothSerialService(); + } + } + }; + btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY); + //Present Notification to take user to permissions page for the app + showBTPermissionsNotification(); + } } private Message createHardwareConnectedMessage(final TransportRecord record) { @@ -2937,9 +2947,6 @@ public class SdlRouterService extends Service { if (receivedVehicleType != null) { pingIntent.putExtra(TransportConstants.VEHICLE_INFO_EXTRA, receivedVehicleType.getStore()); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - pingIntent.putExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, true); - } } private void startClientPings() { 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 b99958fae..3e5665556 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 @@ -204,7 +204,7 @@ public class AndroidTools { return sdlAppInfoList; } - public static boolean areBtPermissionsGranted(Context context, String servicePackageName) { + 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; @@ -214,11 +214,28 @@ public class AndroidTools { 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"); + 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 btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED; + return btScanPermission == PackageManager.PERMISSION_GRANTED; } catch (NameNotFoundException e) { e.printStackTrace(); - DebugTool.logError(TAG, "servicePackageName not found while checking BT permissions"); + DebugTool.logError(TAG, "servicePackageName not found while checking BT SCAN permissions"); return false; } } @@ -250,13 +267,6 @@ public class AndroidTools { for (ResolveInfo app : apps) { try { intent.setClassName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && intent.getBooleanExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, false)) { - Intent pending = new Intent(); - PendingIntent pendingIntent = PendingIntent.getForegroundService(context, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT); - intent.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent); - } - context.sendBroadcast(intent); } catch (Exception e) { //In case there is missing info in the app reference we want to keep moving -- cgit v1.2.1