diff options
author | Joey Grover <joeygrover@gmail.com> | 2022-09-14 09:42:05 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-14 09:42:05 -0400 |
commit | 6e6040d1cc5c285a11b037555715466a699bae7f (patch) | |
tree | fc34f4b30134e6f48d9cd4752d4dd949bfb23c26 | |
parent | bf62616ec1928fba468515466f9be32c46bf4443 (diff) | |
download | sdl_android-6e6040d1cc5c285a11b037555715466a699bae7f.tar.gz |
Prevent exceptions being thrown for remaining startForegroundService calls (#1830)
* Change SdlBR handleStartServiceEx access modifier
Update to protected to allow other transport classes to use it
* Add try/catch to RS status class startFrgndService
* Add try/catch to USB Activity startFrgndService
* Add missing space in SDLRSSProvider try block
Co-authored-by: Julian Kast <julian.kast@live.com>
Co-authored-by: Julian Kast <julian.kast@live.com>
3 files changed, 20 insertions, 5 deletions
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 73d1fe2dc..936989f5c 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 @@ -600,7 +600,12 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { return false; } - private static void handleStartServiceException(Exception e) { + /** + * Convenience method to log details on the specific exception that occurred while attempting to + * start a foreground service. + * @param e the exception that occurred + */ + protected static void handleStartServiceException(Exception e) { if (e instanceof SecurityException) { DebugTool.logError(TAG, "Security exception, process is bad"); return; diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java index b90a55684..078fe104c 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java @@ -136,11 +136,21 @@ public class SdlRouterStatusProvider { } else { bindingIntent.putExtra(FOREGROUND_EXTRA, true); SdlBroadcastReceiver.setForegroundExceptionHandler(); //Prevent ANR in case the OS takes too long to start the service - context.startForegroundService(bindingIntent); + try { + context.startForegroundService(bindingIntent); + } catch (SecurityException | IllegalStateException e) { + SdlBroadcastReceiver.handleStartServiceException(e); + } } bindingIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_STATUS); - return context.bindService(bindingIntent, routerConnection, Context.BIND_AUTO_CREATE); + boolean didBind = false; + try { + didBind = context.bindService(bindingIntent, routerConnection, Context.BIND_AUTO_CREATE); + } catch (SecurityException | IllegalStateException e) { + SdlBroadcastReceiver.handleStartServiceException(e); + } + return didBind; } private void unBindFromService() { diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java index 22ee5d1db..7bb2f753b 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java @@ -202,8 +202,8 @@ public class USBAccessoryAttachmentActivity extends Activity { }
- } catch (SecurityException e) {
- DebugTool.logError(TAG, "Security exception, process is bad");
+ } catch (SecurityException | IllegalStateException e) {
+ SdlBroadcastReceiver.handleStartServiceException(e);
}
} else {
if (usbAccessory != null) {
|