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

Commit 8f5f7f89 authored by Achim Thesmann's avatar Achim Thesmann
Browse files

Introduce new enum values for BAL mode

Two new flag values that in the long term should replace MODE_BACKGROUND_ACTIVITY_START_ALLOWED.

MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS - allow activity starts at any time - either based on permission or based on visibility. This includes BAL permissions granted by the now deprecated setPendingIntentBackgroundActivityLaunchAllowed method.

MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS - allows activity starts only based on visibility. This is a much safer option for apps sending PendingIntents, since this typically means the user is interacting with that app to trigger the start.

Test: atest BackgroundActivityLaunchTest BackgroundActivityStartContriller*Test
Flag: com.android.window.flags.bal_additional_start_modes
Bug: 352182359
Change-Id: I84500ccf585bc50a29e96e7a0232474102382987
parent 3afcb7c0
Loading
Loading
Loading
Loading
+17 −1
Original line number Diff line number Diff line
@@ -104,7 +104,9 @@ public class ActivityOptions extends ComponentOptions {
            MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED,
            MODE_BACKGROUND_ACTIVITY_START_ALLOWED,
            MODE_BACKGROUND_ACTIVITY_START_DENIED,
            MODE_BACKGROUND_ACTIVITY_START_COMPAT})
            MODE_BACKGROUND_ACTIVITY_START_COMPAT,
            MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS,
            MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE})
    public @interface BackgroundActivityStartMode {}
    /**
     * No explicit value chosen. The system will decide whether to grant privileges.
@@ -118,6 +120,20 @@ public class ActivityOptions extends ComponentOptions {
     * Deny the {@link PendingIntent} to use the background activity start privileges.
     */
    public static final int MODE_BACKGROUND_ACTIVITY_START_DENIED = 2;
    /**
     * Allow the {@link PendingIntent} to use ALL background activity start privileges, including
     * special permissions that will allow starts at any time.
     *
     * @hide
     */
    public static final int MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS = 3;
    /**
     * Allow the {@link PendingIntent} to use background activity start privileges based on
     * visibility of the app.
     *
     * @hide
     */
    public static final int MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE = 4;
    /**
     * Special behavior for compatibility.
     * Similar to {@link #MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED}
+31 −23
Original line number Diff line number Diff line
@@ -18,9 +18,11 @@ package android.app;

import static android.app.ActivityOptions.BackgroundActivityStartMode;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_COMPAT;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE;

import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -48,15 +50,7 @@ public class ComponentOptions {
    public static final String KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED =
            "android.pendingIntent.backgroundActivityAllowed";

    /**
     * PendingIntent caller allows activity to be started if caller has BAL permission.
     * @hide
     */
    public static final String KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION =
            "android.pendingIntent.backgroundActivityAllowedByPermission";

    private Integer mPendingIntentBalAllowed = MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED;
    private boolean mPendingIntentBalAllowedByPermission = false;

    ComponentOptions() {
    }
@@ -69,9 +63,6 @@ public class ComponentOptions {
        mPendingIntentBalAllowed =
                opts.getInt(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED,
                        MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED);
        setPendingIntentBackgroundActivityLaunchAllowedByPermission(
                opts.getBoolean(
                        KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION, false));
    }

    /**
@@ -114,10 +105,19 @@ public class ComponentOptions {
    public @NonNull ComponentOptions setPendingIntentBackgroundActivityStartMode(
            @BackgroundActivityStartMode int state) {
        switch (state) {
            case MODE_BACKGROUND_ACTIVITY_START_ALLOWED:
                if (mPendingIntentBalAllowed != MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS) {
                    // do not overwrite ALWAYS with ALLOWED for backwards compatibility,
                    // if setPendingIntentBackgroundActivityLaunchAllowedByPermission is used
                    // before this method.
                    mPendingIntentBalAllowed = state;
                }
                break;
            case MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED:
            case MODE_BACKGROUND_ACTIVITY_START_DENIED:
            case MODE_BACKGROUND_ACTIVITY_START_COMPAT:
            case MODE_BACKGROUND_ACTIVITY_START_ALLOWED:
            case MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS:
            case MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE:
                mPendingIntentBalAllowed = state;
                break;
            default:
@@ -140,20 +140,32 @@ public class ComponentOptions {
    }

    /**
     * Set PendingIntent activity can be launched from background if caller has BAL permission.
     * Get PendingIntent activity is allowed to be started in the background if the caller
     * has BAL permission.
     * @hide
     * @deprecated check for #MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
     */
    public void setPendingIntentBackgroundActivityLaunchAllowedByPermission(boolean allowed) {
        mPendingIntentBalAllowedByPermission = allowed;
    @Deprecated
    public boolean isPendingIntentBackgroundActivityLaunchAllowedByPermission() {
        return mPendingIntentBalAllowed == MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS;
    }

    /**
     * Get PendingIntent activity is allowed to be started in the background if the caller
     * has BAL permission.
     * Set PendingIntent activity can be launched from background if caller has BAL permission.
     * @hide
     * @deprecated use #MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
     */
    public boolean isPendingIntentBackgroundActivityLaunchAllowedByPermission() {
        return mPendingIntentBalAllowedByPermission;
    @Deprecated
    public void setPendingIntentBackgroundActivityLaunchAllowedByPermission(boolean allowed) {
        if (allowed) {
            setPendingIntentBackgroundActivityStartMode(
                    MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS);
        } else {
            if (getPendingIntentBackgroundActivityStartMode()
                    == MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS) {
                setPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
            }
        }
    }

    /** @hide */
@@ -162,10 +174,6 @@ public class ComponentOptions {
        if (mPendingIntentBalAllowed != MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED) {
            b.putInt(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED, mPendingIntentBalAllowed);
        }
        if (mPendingIntentBalAllowedByPermission) {
            b.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION,
                    mPendingIntentBalAllowedByPermission);
        }
        return b;
    }