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

Commit 84282d80 authored by Valentin Iftime's avatar Valentin Iftime
Browse files

Add feature flags for Polite Notifications

 Add sysprop flags to enable and configure the polite notifications feature.

Test: atest FrameworksUiServicesTests
Bug: 270456865
Bug: 291897570
Bug: 291899544
Bug: 291907312
Change-Id: Id8b9dce9b9622626c481a240c644becbed389a7a
parent 1f09f711
Loading
Loading
Loading
Loading
+90 −0
Original line number Diff line number Diff line
@@ -86,6 +86,28 @@ public class SystemUiSystemPropertiesFlags {
        public static final Flag ENABLE_ATTENTION_HELPER_REFACTOR = devFlag(
                "persist.debug.sysui.notification.enable_attention_helper_refactor");

        // TODO b/291899544: for released flags, use resource config values
        /** Value used by polite notif. feature */
        public static final Flag NOTIF_COOLDOWN_T1 = devFlag(
                "persist.debug.sysui.notification.notif_cooldown_t1", 5000);
        /** Value used by polite notif. feature */
        public static final Flag NOTIF_COOLDOWN_T2 = devFlag(
                "persist.debug.sysui.notification.notif_cooldown_t2", 3000);
        /** Value used by polite notif. feature */
        public static final Flag NOTIF_VOLUME1 = devFlag(
                "persist.debug.sysui.notification.notif_volume1", 30);
        public static final Flag NOTIF_VOLUME2 = devFlag(
                "persist.debug.sysui.notification.notif_volume2", 0);
        /** Value used by polite notif. feature. -1 to ignore the counter */
        public static final Flag NOTIF_COOLDOWN_COUNTER_RESET = devFlag(
                "persist.debug.sysui.notification.notif_cooldown_counter_reset", 10);
        /**
         * Value used by polite notif. feature: cooldown behavior/strategy. Valid values: rule1,
         * rule2
         */
        public static final Flag NOTIF_COOLDOWN_RULE = devFlag(
                "persist.debug.sysui.notification.notif_cooldown_rule", "rule1");

        /** b/301242692: Visit extra URIs used in notifications to prevent security issues. */
        public static final Flag VISIT_RISKY_URIS = devFlag(
                "persist.sysui.notification.visit_risky_uris");
@@ -97,6 +119,10 @@ public class SystemUiSystemPropertiesFlags {
    public interface FlagResolver {
        /** Is the flag enabled? */
        boolean isEnabled(Flag flag);
        /** Get the flag value (integer) */
        int getIntValue(Flag flag);
        /** Get the flag value (string) */
        String getStringValue(Flag flag);
    }

    /** The primary, immutable resolver returned by getResolver() */
@@ -133,6 +159,22 @@ public class SystemUiSystemPropertiesFlags {
        return new Flag(name, false, null);
    }

    /**
     * Creates a flag that with a default integer value in debuggable builds.
     */
    @VisibleForTesting
    public static Flag devFlag(String name, int defaultValue) {
        return new Flag(name, defaultValue, null);
    }

    /**
     * Creates a flag that with a default string value in debuggable builds.
     */
    @VisibleForTesting
    public static Flag devFlag(String name, String defaultValue) {
        return new Flag(name, defaultValue, null);
    }

    /**
     * Creates a flag that is disabled by default in debuggable builds.
     * It can be enabled or force-disabled by setting this flag's SystemProperty to 1 or 0.
@@ -161,6 +203,8 @@ public class SystemUiSystemPropertiesFlags {
    public static final class Flag {
        public final String mSysPropKey;
        public final boolean mDefaultValue;
        public final int mDefaultIntValue;
        public final String mDefaultStringValue;
        @Nullable
        public final Flag mDebugDefault;

@@ -170,6 +214,24 @@ public class SystemUiSystemPropertiesFlags {
            mSysPropKey = sysPropKey;
            mDefaultValue = defaultValue;
            mDebugDefault = debugDefault;
            mDefaultIntValue = 0;
            mDefaultStringValue = null;
        }

        public Flag(@NonNull String sysPropKey, int defaultValue, @Nullable Flag debugDefault) {
            mSysPropKey = sysPropKey;
            mDefaultIntValue = defaultValue;
            mDebugDefault = debugDefault;
            mDefaultValue = false;
            mDefaultStringValue = null;
        }

        public Flag(@NonNull String sysPropKey, String defaultValue, @Nullable Flag debugDefault) {
            mSysPropKey = sysPropKey;
            mDefaultStringValue = defaultValue;
            mDebugDefault = debugDefault;
            mDefaultValue = false;
            mDefaultIntValue = 0;
        }
    }

@@ -181,6 +243,16 @@ public class SystemUiSystemPropertiesFlags {
        public boolean isEnabled(Flag flag) {
            return flag.mDefaultValue;
        }

        @Override
        public int getIntValue(Flag flag) {
            return flag.mDefaultIntValue;
        }

        @Override
        public String getStringValue(Flag flag) {
            return flag.mDefaultStringValue;
        }
    }

    /** Implementation of the interface used in debuggable builds. */
@@ -199,5 +271,23 @@ public class SystemUiSystemPropertiesFlags {
        public boolean getBoolean(String key, boolean defaultValue) {
            return SystemProperties.getBoolean(key, defaultValue);
        }

        /** Look up the value; overridable for tests to avoid needing to set SystemProperties */
        @VisibleForTesting
        public int getIntValue(Flag flag) {
            if (flag.mDebugDefault == null) {
                return SystemProperties.getInt(flag.mSysPropKey, flag.mDefaultIntValue);
            }
            return SystemProperties.getInt(flag.mSysPropKey, getIntValue(flag.mDebugDefault));
        }

        /** Look up the value; overridable for tests to avoid needing to set SystemProperties */
        @VisibleForTesting
        public String getStringValue(Flag flag) {
            if (flag.mDebugDefault == null) {
                return SystemProperties.get(flag.mSysPropKey, flag.mDefaultStringValue);
            }
            return SystemProperties.get(flag.mSysPropKey, getStringValue(flag.mDebugDefault));
        }
    }
}
+19 −4
Original line number Diff line number Diff line
@@ -48,6 +48,8 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;

import com.android.internal.config.sysui.SystemUiSystemPropertiesFlags;
import com.android.internal.config.sysui.SystemUiSystemPropertiesFlags.Flag;
import com.android.internal.config.sysui.SystemUiSystemPropertiesFlags.FlagResolver;

import org.junit.After;
import org.junit.Assert;
@@ -422,11 +424,24 @@ public class NotificationRankingUpdateTest {
        mNotificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "test channel",
                NotificationManager.IMPORTANCE_DEFAULT);

        SystemUiSystemPropertiesFlags.TEST_RESOLVER = flag -> {
        SystemUiSystemPropertiesFlags.TEST_RESOLVER = new FlagResolver() {
            @Override
            public boolean isEnabled(Flag flag) {
                if (flag.mSysPropKey.equals(RANKING_UPDATE_ASHMEM.mSysPropKey)) {
                    return mRankingUpdateAshmem;
                }
                return new SystemUiSystemPropertiesFlags.DebugResolver().isEnabled(flag);
            }

            @Override
            public int getIntValue(Flag flag) {
                return 0;
            }

            @Override
            public String getStringValue(Flag flag) {
                return null;
            }
        };
    }

+24 −0
Original line number Diff line number Diff line
@@ -21,15 +21,39 @@ import java.util.Map;

public class TestableFlagResolver implements SystemUiSystemPropertiesFlags.FlagResolver {
    private Map<String, Boolean> mOverrides = new HashMap<>();
    private Map<String, Integer> mOverridesInt = new HashMap<>();
    private Map<String, String> mOverridesString = new HashMap<>();

    @Override
    public boolean isEnabled(SystemUiSystemPropertiesFlags.Flag flag) {
        return mOverrides.getOrDefault(flag.mSysPropKey, flag.mDefaultValue);
    }

    @Override
    public int getIntValue(SystemUiSystemPropertiesFlags.Flag flag) {
        return mOverridesInt.getOrDefault(flag.mSysPropKey, flag.mDefaultIntValue);
    }

    @Override
    public String getStringValue(SystemUiSystemPropertiesFlags.Flag flag) {
        return mOverridesString.getOrDefault(flag.mSysPropKey, flag.mDefaultStringValue);
    }

    public TestableFlagResolver setFlagOverride(SystemUiSystemPropertiesFlags.Flag flag,
            boolean isEnabled) {
        mOverrides.put(flag.mSysPropKey, isEnabled);
        return this;
    }

    public TestableFlagResolver setFlagOverride(SystemUiSystemPropertiesFlags.Flag flag,
        int value) {
        mOverridesInt.put(flag.mSysPropKey, value);
        return this;
    }

    public TestableFlagResolver setFlagOverride(SystemUiSystemPropertiesFlags.Flag flag,
        String value) {
        mOverridesString.put(flag.mSysPropKey, value);
        return this;
    }
}