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

Commit cab5432e authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Merge cherrypicks of ['googleplex-android-review.googlesource.com/27070879',...

Merge cherrypicks of ['googleplex-android-review.googlesource.com/27070879', 'googleplex-android-review.googlesource.com/27232419'] into sparse-11808821-L25700030003621284.
SPARSE_CHANGE: I85b485cb5ec2596a5695c695a96594e11994c5eb
SPARSE_CHANGE: I80687713591cf146b70f196a4ffaeb0f3c26a1e5

Change-Id: I8e5de61b600b07936605f1a287bf3135af202069
parents 235cd4a5 888daa96
Loading
Loading
Loading
Loading
+30 −10
Original line number Diff line number Diff line
@@ -507,6 +507,18 @@ public final class ActiveServices {
     */
    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.
    ArraySet<String> mAllowListWhileInUsePermissionInFgs = new ArraySet<>();

@@ -2396,11 +2408,16 @@ public final class ActiveServices {
                                // "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) {
                        // 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
                        // and it hasn't hit the time limit for that type in the past 24hrs.
                        // and it hasn't hit its time limit in the past 24hrs, or it has been in the
                        // foreground after it hit its time limit, or it is currently in the
                        // TOP (or better) proc state.

                        // See if the app could start an FGS or not.
                        r.clearFgsAllowStart();
@@ -2426,11 +2443,13 @@ public final class ActiveServices {
                                            SystemClock.elapsedRealtime() - (24 * 60 * 60 * 1000));
                                final long lastTimeOutAt = fgsTypeInfo.getTimeLimitExceededAt();
                                if (fgsTypeInfo.getFirstFgsStartRealtime() < before24Hr
                                        || r.app.mState.getCurProcState() <= PROCESS_STATE_TOP
                                        || (lastTimeOutAt != Long.MIN_VALUE
                                            && r.app.mState.getLastTopTime() > lastTimeOutAt)) {
                                    // Reset the time limit info for this fgs type if it has been
                                    // more than 24hrs since the first fgs start or if the app was
                                    // in the TOP state after time limit was exhausted.
                                    // more than 24hrs since the first fgs start or if the app is
                                    // currently in the TOP state or was in the TOP state after
                                    // the time limit was exhausted previously.
                                    fgsTypeInfo.reset();
                                } else if (lastTimeOutAt > 0) {
                                    // Time limit was exhausted within the past 24 hours and the app
@@ -2686,7 +2705,10 @@ public final class ActiveServices {
                    mAm.notifyPackageUse(r.serviceInfo.packageName,
                            PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE);

                    if (CompatChanges.isChangeEnabled(FGS_INTRODUCE_TIME_LIMITS, r.appInfo.uid)
                            && android.app.Flags.introduceNewServiceOntimeoutCallback()) {
                        maybeUpdateFgsTrackingLocked(r, previousFgsType);
                    }
                } else {
                    if (DEBUG_FOREGROUND_SERVICE) {
                        Slog.d(TAG, "Suppressing startForeground() for FAS " + r);
@@ -3907,12 +3929,10 @@ public final class ActiveServices {
                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.
            mFGSAnrTimer.start(sr, mAm.mConstants.mFgsAnrExtraWaitDuration);
        }
    }
    }

    void onFgsAnrTimeout(ServiceRecord sr) {
        final int fgsType = getTimeLimitedFgsType(sr.foregroundServiceType);
+5 −2
Original line number Diff line number Diff line
@@ -4342,6 +4342,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            mServices.bringDownDisabledPackageServicesLocked(
                    packageName, null, userId, false, true, true);
            mServices.onUidRemovedLocked(uid);
            if (mBooted) {
                mAtmInternal.resumeTopActivities(true);
@@ -4372,9 +4373,10 @@ public class ActivityManagerService extends IActivityManager.Stub
            Slog.w(TAG, "Can't force stop all processes of all users, that is insane!");
        }
        final int uid = getPackageManagerInternal().getPackageUid(packageName,
                            MATCH_DEBUG_TRIAGED_MISSING | MATCH_ANY_USER, UserHandle.USER_SYSTEM);
        if (appId < 0 && packageName != null) {
            appId = UserHandle.getAppId(getPackageManagerInternal().getPackageUid(packageName,
                    MATCH_DEBUG_TRIAGED_MISSING | MATCH_ANY_USER, UserHandle.USER_SYSTEM));
            appId = UserHandle.getAppId(uid);
        }
        boolean didSomething;
@@ -4418,6 +4420,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
            didSomething = true;
        }
        mServices.onUidRemovedLocked(uid);
        if (packageName == null) {
            // Remove all sticky broadcasts from this user.