sdl_java_suite icon indicating copy to clipboard operation
sdl_java_suite copied to clipboard

Android 12 Catch ForegroundServiceStartNotAllowedException

Open JulianKast opened this issue 1 year ago • 1 comments

Fixes #1815

This PR is [ready] for review.

Risk

This PR makes [minor] API changes.

Testing Plan

  • [x] I have verified that I have not introduced new warnings in this PR (or explain why below)
  • [x] I have run the unit tests with this PR
  • [x] I have tested this PR against Core and verified behavior (if applicable, if not applicable, explain why below).
  • [x] I have tested Android

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.

                final Handler handler = new Handler(Looper.getMainLooper());
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            serviceIntent.putExtra(FOREGROUND_EXTRA, true);
                            DebugTool.logInfo(TAG, "Attempting to startForegroundService - " + System.currentTimeMillis());
                            setForegroundExceptionHandler(); //Prevent ANR in case the OS takes too long to start the service
                            context.startForegroundService(serviceIntent);
                        } catch (ForegroundServiceStartNotAllowedException e) {
                            Log.e(TAG, "ForegroundServiceStartNotAllowedException herer");
                        }

                    }
                }, 25000);

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.

throw new ForegroundServiceStartNotAllowedException("msg");

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 the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permission. In testing, I saw it happen between 20 to 25 seconds.

Changelog

Bug Fixes
  • Catch ForegroundServiceStartNotAllowedException in two places in SdlBroadcastReceiver.

CLA

JulianKast avatar Jul 26 '22 20:07 JulianKast

Codecov Report

Merging #1823 (4f8e483) into develop (ccf0af9) will increase coverage by 0.25%. The diff coverage is 0.00%.

Impacted file tree graph

@@              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     
Impacted Files Coverage Δ
...martdevicelink/transport/SdlBroadcastReceiver.java 3.02% <0.00%> (-0.12%) :arrow_down:
...rtdevicelink/streaming/video/SdlRemoteDisplay.java 52.43% <0.00%> (+1.21%) :arrow_up:
...rc/main/java/com/android/grafika/gles/EglCore.java 44.82% <0.00%> (+2.58%) :arrow_up:
.../java/com/android/grafika/gles/EglSurfaceBase.java 32.14% <0.00%> (+7.14%) :arrow_up:
...smartdevicelink/encoder/VirtualDisplayEncoder.java 45.69% <0.00%> (+9.60%) :arrow_up:
...main/java/com/android/grafika/gles/Drawable2d.java 67.92% <0.00%> (+11.32%) :arrow_up:
...ava/com/android/grafika/gles/Texture2dProgram.java 59.18% <0.00%> (+24.48%) :arrow_up:
...n/java/com/android/grafika/gles/FullFrameRect.java 55.00% <0.00%> (+30.00%) :arrow_up:

codecov[bot] avatar Jul 26 '22 20:07 codecov[bot]