|
32 | 32 |
|
33 | 33 | package com.smartdevicelink.transport; |
34 | 34 |
|
35 | | -import android.annotation.SuppressLint; |
36 | 35 | import android.app.ActivityManager; |
37 | 36 | import android.app.ActivityManager.RunningServiceInfo; |
38 | 37 | import android.app.ForegroundServiceStartNotAllowedException; |
| 38 | +import android.app.ServiceStartNotAllowedException; |
39 | 39 | import android.bluetooth.BluetoothAdapter; |
40 | 40 | import android.bluetooth.BluetoothDevice; |
41 | 41 | import android.bluetooth.BluetoothProfile; |
@@ -284,10 +284,8 @@ private static void startRouterService(Context context, ComponentName componentN |
284 | 284 | restart.putExtra(LOCAL_ROUTER_SERVICE_DID_START_OWN, true); |
285 | 285 | context.sendBroadcast(restart); |
286 | 286 |
|
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); |
291 | 289 | } |
292 | 290 | } |
293 | 291 |
|
@@ -482,11 +480,8 @@ protected static void pingRouterService(Context context, String packageName, Str |
482 | 480 | } else { |
483 | 481 | context.startService(intent); |
484 | 482 | } |
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); |
490 | 485 | } |
491 | 486 | } |
492 | 487 |
|
@@ -605,6 +600,22 @@ private static boolean isBluetoothConnected() { |
605 | 600 | return false; |
606 | 601 | } |
607 | 602 |
|
| 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 | + |
608 | 619 |
|
609 | 620 | private static SdlDeviceListener getSdlDeviceListener(Context context, BluetoothDevice bluetoothDevice) { |
610 | 621 |
|
|
0 commit comments