diff options
author | Joey Grover <joeygrover@gmail.com> | 2022-08-15 14:40:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-15 14:40:43 -0400 |
commit | 3fd2499167106b2a5925efc249e563e8793eb302 (patch) | |
tree | 069ff24f886ca3b0d6cad0f64ecc2d675c842989 | |
parent | ccf0af9faa46da92a3ec10b05b3d397fa8f16a45 (diff) | |
parent | 4f8e48376303cad3509061a4516a0367a969d735 (diff) | |
download | sdl_android-3fd2499167106b2a5925efc249e563e8793eb302.tar.gz |
Merge pull request #1823 from smartdevicelink/bugfix/issue_1815_ForegroundServiceStartNotAllowedException
Android 12 Catch ForegroundServiceStartNotAllowedException
-rw-r--r-- | android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java | 27 |
1 files changed, 22 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..73d1fe2dc 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,22 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver { return false; } + private 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) { |