diff options
Diffstat (limited to 'android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java')
-rw-r--r-- | android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java | 32 |
1 files changed, 27 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 7bed1b482..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 @@ -34,6 +34,8 @@ package com.smartdevicelink.transport; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; +import android.app.ForegroundServiceStartNotAllowedException; +import android.app.ServiceStartNotAllowedException; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; @@ -282,8 +284,8 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { restart.putExtra(LOCAL_ROUTER_SERVICE_DID_START_OWN, true); context.sendBroadcast(restart); - } catch (SecurityException e) { - DebugTool.logError(TAG, "Security exception, process is bad"); + } catch (SecurityException | IllegalStateException e) { + handleStartServiceException(e); } } @@ -478,9 +480,8 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { } else { context.startService(intent); } - } catch (SecurityException e) { - DebugTool.logError(TAG, "Security exception, process is bad"); - // This service could not be started + } catch (SecurityException | IllegalStateException e) { + handleStartServiceException(e); } } @@ -599,6 +600,27 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { return false; } + /** + * 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; + } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) { + if (e instanceof ForegroundServiceStartNotAllowedException) { + DebugTool.logError(TAG, "Not allowed to start service in foreground"); + return; + } else if (e instanceof ServiceStartNotAllowedException) { + DebugTool.logError(TAG, "Not allowed to start service in current state"); + return; + } + } + DebugTool.logError(TAG, "Unable to start service for unknown reason"); + } + private static SdlDeviceListener getSdlDeviceListener(Context context, BluetoothDevice bluetoothDevice) { |