Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a7250e02 authored by Varun Shah's avatar Varun Shah
Browse files

Gate the new FGS time limit logic with a targetSdk check.

Bug: 30399444
Test: atest CtsFgsTimeoutTestCases
Change-Id: I85b485cb5ec2596a5695c695a96594e11994c5eb
parent 385e06a7
Loading
Loading
Loading
Loading
+23 −7
Original line number Original line Diff line number Diff line
@@ -507,6 +507,18 @@ public final class ActiveServices {
     */
     */
    final SparseArray<SparseArray<TimeLimitedFgsInfo>> mTimeLimitedFgsInfo = new SparseArray<>();
    final SparseArray<SparseArray<TimeLimitedFgsInfo>> mTimeLimitedFgsInfo = new SparseArray<>();


    /**
     * Foreground services of certain types will now have a time limit. If the foreground service
     * of the offending type is not stopped within the allocated time limit, it will receive a
     * callback via {@link Service#onTimeout(int, int)} and it must then be stopped within a few
     * seconds. If an app fails to do so, it will be declared an ANR.
     *
     * @see Service#onTimeout(int, int) onTimeout callback for additional details
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = VERSION_CODES.VANILLA_ICE_CREAM)
    static final long FGS_INTRODUCE_TIME_LIMITS = 317799821L;

    // allowlisted packageName.
    // allowlisted packageName.
    ArraySet<String> mAllowListWhileInUsePermissionInFgs = new ArraySet<>();
    ArraySet<String> mAllowListWhileInUsePermissionInFgs = new ArraySet<>();


@@ -2396,7 +2408,10 @@ public final class ActiveServices {
                                // "if (r.mAllowStartForeground == REASON_DENIED...)" block below.
                                // "if (r.mAllowStartForeground == REASON_DENIED...)" block below.
                            }
                            }
                        }
                        }
                    } else if (getTimeLimitedFgsType(foregroundServiceType)
                    } else if (CompatChanges.isChangeEnabled(
                                    FGS_INTRODUCE_TIME_LIMITS, r.appInfo.uid)
                                && android.app.Flags.introduceNewServiceOntimeoutCallback()
                                && getTimeLimitedFgsType(foregroundServiceType)
                                        != ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE) {
                                        != ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE) {
                        // Calling startForeground on a FGS type which has a time limit will only be
                        // Calling startForeground on a FGS type which has a time limit will only be
                        // allowed if the app is in a state where it can normally start another FGS
                        // allowed if the app is in a state where it can normally start another FGS
@@ -2686,7 +2701,10 @@ public final class ActiveServices {
                    mAm.notifyPackageUse(r.serviceInfo.packageName,
                    mAm.notifyPackageUse(r.serviceInfo.packageName,
                            PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE);
                            PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE);


                    if (CompatChanges.isChangeEnabled(FGS_INTRODUCE_TIME_LIMITS, r.appInfo.uid)
                            && android.app.Flags.introduceNewServiceOntimeoutCallback()) {
                        maybeUpdateFgsTrackingLocked(r, previousFgsType);
                        maybeUpdateFgsTrackingLocked(r, previousFgsType);
                    }
                } else {
                } else {
                    if (DEBUG_FOREGROUND_SERVICE) {
                    if (DEBUG_FOREGROUND_SERVICE) {
                        Slog.d(TAG, "Suppressing startForeground() for FAS " + r);
                        Slog.d(TAG, "Suppressing startForeground() for FAS " + r);
@@ -3907,12 +3925,10 @@ public final class ActiveServices {
                Slog.w(TAG_SERVICE, "Exception from scheduleTimeoutServiceForType: " + e);
                Slog.w(TAG_SERVICE, "Exception from scheduleTimeoutServiceForType: " + e);
            }
            }


            if (android.app.Flags.introduceNewServiceOntimeoutCallback()) {
            // ANR the service after giving the service some time to clean up.
            // ANR the service after giving the service some time to clean up.
            mFGSAnrTimer.start(sr, mAm.mConstants.mFgsAnrExtraWaitDuration);
            mFGSAnrTimer.start(sr, mAm.mConstants.mFgsAnrExtraWaitDuration);
        }
        }
    }
    }
    }


    void onFgsAnrTimeout(ServiceRecord sr) {
    void onFgsAnrTimeout(ServiceRecord sr) {
        final int fgsType = getTimeLimitedFgsType(sr.foregroundServiceType);
        final int fgsType = getTimeLimitedFgsType(sr.foregroundServiceType);