Skip to content

Commit 4f8e483

Browse files
committed
Add method handleStartServiceException to handle exceptions when trying to start routerService in foreground
1 parent 6498858 commit 4f8e483

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
package com.smartdevicelink.transport;
3434

35-
import android.annotation.SuppressLint;
3635
import android.app.ActivityManager;
3736
import android.app.ActivityManager.RunningServiceInfo;
3837
import android.app.ForegroundServiceStartNotAllowedException;
38+
import android.app.ServiceStartNotAllowedException;
3939
import android.bluetooth.BluetoothAdapter;
4040
import android.bluetooth.BluetoothDevice;
4141
import android.bluetooth.BluetoothProfile;
@@ -284,10 +284,8 @@ private static void startRouterService(Context context, ComponentName componentN
284284
restart.putExtra(LOCAL_ROUTER_SERVICE_DID_START_OWN, true);
285285
context.sendBroadcast(restart);
286286

287-
} catch (SecurityException e) {
288-
DebugTool.logError(TAG, "Security exception, process is bad");
289-
} catch (@SuppressLint({"NewApi", "LocalSuppress"}) ForegroundServiceStartNotAllowedException e) {
290-
DebugTool.logError(TAG, "Not allowed to start service in Foreground");
287+
} catch (SecurityException | IllegalStateException e) {
288+
handleStartServiceException(e);
291289
}
292290
}
293291

@@ -482,11 +480,8 @@ protected static void pingRouterService(Context context, String packageName, Str
482480
} else {
483481
context.startService(intent);
484482
}
485-
} catch (SecurityException e) {
486-
DebugTool.logError(TAG, "Security exception, process is bad");
487-
// This service could not be started
488-
} catch (@SuppressLint({"NewApi", "LocalSuppress"}) ForegroundServiceStartNotAllowedException e) {
489-
DebugTool.logError(TAG, "Not allowed to start service in Foreground");
483+
} catch (SecurityException | IllegalStateException e) {
484+
handleStartServiceException(e);
490485
}
491486
}
492487

@@ -605,6 +600,22 @@ private static boolean isBluetoothConnected() {
605600
return false;
606601
}
607602

603+
private static void handleStartServiceException(Exception e) {
604+
if (e instanceof SecurityException) {
605+
DebugTool.logError(TAG, "Security exception, process is bad");
606+
return;
607+
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
608+
if (e instanceof ForegroundServiceStartNotAllowedException) {
609+
DebugTool.logError(TAG, "Not allowed to start service in foreground");
610+
return;
611+
} else if (e instanceof ServiceStartNotAllowedException) {
612+
DebugTool.logError(TAG, "Not allowed to start service in current state");
613+
return;
614+
}
615+
}
616+
DebugTool.logError(TAG, "Unable to start service for unknown reason");
617+
}
618+
608619

609620
private static SdlDeviceListener getSdlDeviceListener(Context context, BluetoothDevice bluetoothDevice) {
610621

0 commit comments

Comments
 (0)