diff options
author | “JKAST” <julian.kast@live.com> | 2022-08-15 13:49:28 -0400 |
---|---|---|
committer | “JKAST” <julian.kast@live.com> | 2022-08-15 13:49:28 -0400 |
commit | 4f8e48376303cad3509061a4516a0367a969d735 (patch) | |
tree | 069ff24f886ca3b0d6cad0f64ecc2d675c842989 /android/sdl_android/src | |
parent | 649885899a032116b0555dc99ac9927fe732ced5 (diff) | |
download | sdl_android-4f8e48376303cad3509061a4516a0367a969d735.tar.gz |
Add method handleStartServiceException to handle exceptions when trying to start routerService in foreground
Diffstat (limited to 'android/sdl_android/src')
-rw-r--r-- | android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java | 31 |
1 files changed, 21 insertions, 10 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 07e155bac..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 @@ -32,10 +32,10 @@ package com.smartdevicelink.transport; -import android.annotation.SuppressLint; 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; @@ -284,10 +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 (@SuppressLint({"NewApi", "LocalSuppress"}) ForegroundServiceStartNotAllowedException e) { - DebugTool.logError(TAG, "Not allowed to start service in Foreground"); + } catch (SecurityException | IllegalStateException e) { + handleStartServiceException(e); } } @@ -482,11 +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 (@SuppressLint({"NewApi", "LocalSuppress"}) ForegroundServiceStartNotAllowedException e) { - DebugTool.logError(TAG, "Not allowed to start service in Foreground"); + } catch (SecurityException | IllegalStateException e) { + handleStartServiceException(e); } } @@ -605,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) { |