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

Commit 3946e958 authored by Hui Yu's avatar Hui Yu
Browse files

Add app compat flag to enable FGS BG-launch restriction.

Add a app compat flag FGS_BG_START_RESTRICTION_CHANGE_ID to
enable FGS BG start restriction for any app. Default is disabled. To turn it on for
an app, go to Settings --> Developer options --> App Compatibility
Changes, find the app and turn on this change.

Bug: 170668199
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java#testFgsStartFromBG2
Change-Id: Ia54de45f9a417e287dcba19b9f09e0f63da953f1
parent d25fb136
Loading
Loading
Loading
Loading
+29 −2
Original line number Diff line number Diff line
@@ -56,6 +56,8 @@ import android.app.Service;
import android.app.ServiceStartArgs;
import android.app.admin.DevicePolicyEventLogger;
import android.appwidget.AppWidgetManagerInternal;
import android.compat.annotation.ChangeId;
import android.compat.annotation.Disabled;
import android.content.ComponentName;
import android.content.ComponentName.WithComponentName;
import android.content.Context;
@@ -79,6 +81,7 @@ import android.os.Message;
import android.os.Process;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.TransactionTooLargeException;
@@ -100,6 +103,7 @@ import android.webkit.WebViewZygote;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.app.procstats.ServiceState;
import com.android.internal.compat.IPlatformCompat;
import com.android.internal.messages.nano.SystemMessageProto;
import com.android.internal.notification.SystemNotificationChannels;
import com.android.internal.os.BatteryStatsImpl;
@@ -234,6 +238,16 @@ public final class ActiveServices {
    private static final SimpleDateFormat DATE_FORMATTER =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final IPlatformCompat mPlatformCompat;

    /**
     * The BG-launch FGS restriction feature is going to be allowed only for apps targetSdkVersion
     * is higher than R.
     */
    @ChangeId
    @Disabled
    static final long FGS_BG_START_RESTRICTION_CHANGE_ID = 170668199L;

    final Runnable mLastAnrDumpClearer = new Runnable() {
        @Override public void run() {
            synchronized (mAm) {
@@ -430,6 +444,9 @@ public final class ActiveServices {
        }
        mMaxStartingBackground = maxBg > 0
                ? maxBg : ActivityManager.isLowRamDeviceStatic() ? 1 : 8;

        final IBinder b = ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE);
        mPlatformCompat = IPlatformCompat.Stub.asInterface(b);
    }

    void systemServicesReady() {
@@ -557,7 +574,8 @@ public final class ActiveServices {
                    r.mLoggedInfoAllowStartForeground = true;
                }
                if (r.mAllowStartForeground == FGS_FEATURE_DENIED
                        && mAm.mConstants.mFlagFgsStartRestrictionEnabled) {
                        && (mAm.mConstants.mFlagFgsStartRestrictionEnabled
                        || isChangeEnabled(FGS_BG_START_RESTRICTION_CHANGE_ID, r))) {
                    if (mAm.mConstants.mFlagFgsStartTempAllowListEnabled
                            && mAm.isOnDeviceIdleWhitelistLocked(r.appInfo.uid, false)) {
                        // uid is on DeviceIdleController's allowlist.
@@ -1468,7 +1486,8 @@ public final class ActiveServices {
                            r.mLoggedInfoAllowStartForeground = true;
                        }
                        if (r.mAllowStartForeground == FGS_FEATURE_DENIED
                                && mAm.mConstants.mFlagFgsStartRestrictionEnabled) {
                                && (mAm.mConstants.mFlagFgsStartRestrictionEnabled
                                || isChangeEnabled(FGS_BG_START_RESTRICTION_CHANGE_ID, r))) {
                            if (mAm.mConstants.mFlagFgsStartTempAllowListEnabled
                                    && mAm.isOnDeviceIdleWhitelistLocked(r.appInfo.uid, false)) {
                                // uid is on DeviceIdleController's allowlist.
@@ -5101,4 +5120,12 @@ public final class ActiveServices {
        context.getSystemService(NotificationManager.class).notifyAsUser(Long.toString(now),
                NOTE_FOREGROUND_SERVICE_BG_LAUNCH, n.build(), UserHandle.ALL);
    }

    private boolean isChangeEnabled(long changeId, ServiceRecord r) {
        boolean enabled = false;
        try {
            enabled = mPlatformCompat.isChangeEnabled(changeId, r.appInfo);
        } catch (RemoteException e) { }
        return enabled;
    }
}