Skip to content

Commit 6e6040d

Browse files
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>
1 parent bf62616 commit 6e6040d

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,12 @@ private static boolean isBluetoothConnected() {
600600
return false;
601601
}
602602

603-
private static void handleStartServiceException(Exception e) {
603+
/**
604+
* Convenience method to log details on the specific exception that occurred while attempting to
605+
* start a foreground service.
606+
* @param e the exception that occurred
607+
*/
608+
protected static void handleStartServiceException(Exception e) {
604609
if (e instanceof SecurityException) {
605610
DebugTool.logError(TAG, "Security exception, process is bad");
606611
return;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,21 @@ private boolean bindToService() {
136136
} else {
137137
bindingIntent.putExtra(FOREGROUND_EXTRA, true);
138138
SdlBroadcastReceiver.setForegroundExceptionHandler(); //Prevent ANR in case the OS takes too long to start the service
139-
context.startForegroundService(bindingIntent);
139+
try {
140+
context.startForegroundService(bindingIntent);
141+
} catch (SecurityException | IllegalStateException e) {
142+
SdlBroadcastReceiver.handleStartServiceException(e);
143+
}
140144

141145
}
142146
bindingIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_STATUS);
143-
return context.bindService(bindingIntent, routerConnection, Context.BIND_AUTO_CREATE);
147+
boolean didBind = false;
148+
try {
149+
didBind = context.bindService(bindingIntent, routerConnection, Context.BIND_AUTO_CREATE);
150+
} catch (SecurityException | IllegalStateException e) {
151+
SdlBroadcastReceiver.handleStartServiceException(e);
152+
}
153+
return didBind;
144154
}
145155

146156
private void unBindFromService() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ public void onUsbTransferUpdate(boolean success) {
202202

203203
}
204204

205-
} catch (SecurityException e) {
206-
DebugTool.logError(TAG, "Security exception, process is bad");
205+
} catch (SecurityException | IllegalStateException e) {
206+
SdlBroadcastReceiver.handleStartServiceException(e);
207207
}
208208
} else {
209209
if (usbAccessory != null) {

0 commit comments

Comments
 (0)