-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android 12 Catch ForegroundServiceStartNotAllowedException #1823
Android 12 Catch ForegroundServiceStartNotAllowedException #1823
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #1823 +/- ##
=============================================
+ Coverage 53.78% 54.04% +0.25%
- Complexity 5519 5532 +13
=============================================
Files 562 562
Lines 25809 25821 +12
Branches 3395 3398 +3
=============================================
+ Hits 13881 13954 +73
+ Misses 10660 10596 -64
- Partials 1268 1271 +3
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was still getting errors in the IDE even with the suppress lint calls. I think we can get around those still. Try adding a new method like the one I have included to handle the exceptions and use version checks within in it. Then in the original catch statement add catching for IllegalStateException
since that is a parent exception that is included in lower versions of Android. In case there are other extensions of IllegalStateException
that can be thrown we will simply catch them and print out that the reason was unknown. This should probably help us into the future prevent crashes when Android decides to add more state requirements.
The exception handler
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");
}
Refactored exception catching
} catch (SecurityException | IllegalStateException e) {
handleStartServiceException(e);
// This service could not be started
}
…ng to start routerService in foreground
Fixes #1815
This PR is [ready] for review.
Risk
This PR makes [minor] API changes.
Testing Plan
Core Tests
Tested against Sync 3.0 with Android 12 device and Android 9 device.
Android 12 device:
I was able to replicate the issue by using a delayed handler. I had to put the try-catch inside of the handler to catch it.
To catch it where it is now we can do that by throwing the exception where we call
context.startForegroundService(serviceIntent);
You will also need to add@RequiresApi(api = Build.VERSION_CODES.S)
when testing it this way.Android 9 device:
Connected Hello sdl without the modifications above to ensure that the code ran as intended.
Summary
Android 12 may throw a
ForegroundServiceStartNotAllowedException
if we try to start the router service after a certain amount of time from receiving a broadcast that requires theBLUETOOTH_CONNECT
orBLUETOOTH_SCAN
permission. In testing, I saw it happen between 20 to 25 seconds.Changelog
Bug Fixes
ForegroundServiceStartNotAllowedException
in two places inSdlBroadcastReceiver
.CLA