|
34 | 34 |
|
35 | 35 | import android.app.ActivityManager; |
36 | 36 | import android.app.ActivityManager.RunningServiceInfo; |
| 37 | +import android.app.ForegroundServiceStartNotAllowedException; |
| 38 | +import android.app.ServiceStartNotAllowedException; |
37 | 39 | import android.bluetooth.BluetoothAdapter; |
38 | 40 | import android.bluetooth.BluetoothDevice; |
39 | 41 | import android.bluetooth.BluetoothProfile; |
@@ -282,8 +284,8 @@ private static void startRouterService(Context context, ComponentName componentN |
282 | 284 | restart.putExtra(LOCAL_ROUTER_SERVICE_DID_START_OWN, true); |
283 | 285 | context.sendBroadcast(restart); |
284 | 286 |
|
285 | | - } catch (SecurityException e) { |
286 | | - DebugTool.logError(TAG, "Security exception, process is bad"); |
| 287 | + } catch (SecurityException | IllegalStateException e) { |
| 288 | + handleStartServiceException(e); |
287 | 289 | } |
288 | 290 | } |
289 | 291 |
|
@@ -478,9 +480,8 @@ protected static void pingRouterService(Context context, String packageName, Str |
478 | 480 | } else { |
479 | 481 | context.startService(intent); |
480 | 482 | } |
481 | | - } catch (SecurityException e) { |
482 | | - DebugTool.logError(TAG, "Security exception, process is bad"); |
483 | | - // This service could not be started |
| 483 | + } catch (SecurityException | IllegalStateException e) { |
| 484 | + handleStartServiceException(e); |
484 | 485 | } |
485 | 486 | } |
486 | 487 |
|
@@ -599,6 +600,22 @@ private static boolean isBluetoothConnected() { |
599 | 600 | return false; |
600 | 601 | } |
601 | 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 | + |
602 | 619 |
|
603 | 620 | private static SdlDeviceListener getSdlDeviceListener(Context context, BluetoothDevice bluetoothDevice) { |
604 | 621 |
|
|
0 commit comments