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

Commit 56d454f2 authored by Robert Horvath's avatar Robert Horvath Committed by Android (Google) Code Review
Browse files

Merge "Add standby ports APIs to LowPowerStandbyManager"

parents 4c2b1952 e996ea5e
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -326,6 +326,7 @@ package android {
    field public static final String SET_CLIP_SOURCE = "android.permission.SET_CLIP_SOURCE";
    field public static final String SET_DEFAULT_ACCOUNT_FOR_CONTACTS = "android.permission.SET_DEFAULT_ACCOUNT_FOR_CONTACTS";
    field public static final String SET_HARMFUL_APP_WARNINGS = "android.permission.SET_HARMFUL_APP_WARNINGS";
    field public static final String SET_LOW_POWER_STANDBY_PORTS = "android.permission.SET_LOW_POWER_STANDBY_PORTS";
    field public static final String SET_MEDIA_KEY_LISTENER = "android.permission.SET_MEDIA_KEY_LISTENER";
    field public static final String SET_ORIENTATION = "android.permission.SET_ORIENTATION";
    field public static final String SET_POINTER_SPEED = "android.permission.SET_POINTER_SPEED";
@@ -10662,6 +10663,7 @@ package android.os {
  public final class PowerManager {
    method @RequiresPermission(allOf={android.Manifest.permission.READ_DREAM_STATE, android.Manifest.permission.WRITE_DREAM_STATE}) public void dream(long);
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public boolean forceSuspend();
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_LOW_POWER_STANDBY, android.Manifest.permission.DEVICE_POWER}) public java.util.List<android.os.PowerManager.LowPowerStandbyPortDescription> getActiveLowPowerStandbyPorts();
    method @NonNull public android.os.BatterySaverPolicyConfig getFullPowerSavePolicy();
    method @Nullable @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_LOW_POWER_STANDBY, android.Manifest.permission.DEVICE_POWER}) public android.os.PowerManager.LowPowerStandbyPolicy getLowPowerStandbyPolicy();
    method public int getPowerSaveModeTrigger();
@@ -10669,6 +10671,7 @@ package android.os {
    method @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) public boolean isAmbientDisplaySuppressed();
    method @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) public boolean isAmbientDisplaySuppressedForToken(@NonNull String);
    method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_LOW_POWER_STANDBY, android.Manifest.permission.DEVICE_POWER}) public boolean isLowPowerStandbySupported();
    method @NonNull @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS) public android.os.PowerManager.LowPowerStandbyPortsLock newLowPowerStandbyPortsLock(@NonNull java.util.List<android.os.PowerManager.LowPowerStandbyPortDescription>);
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setAdaptivePowerSaveEnabled(boolean);
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setAdaptivePowerSavePolicy(@NonNull android.os.BatterySaverPolicyConfig);
    method @RequiresPermission(anyOf={android.Manifest.permission.BATTERY_PREDICTION, android.Manifest.permission.DEVICE_POWER}) public void setBatteryDischargePrediction(@NonNull java.time.Duration, boolean);
@@ -10680,6 +10683,7 @@ package android.os {
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setPowerSaveModeEnabled(boolean);
    method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void suppressAmbientDisplay(@NonNull String, boolean);
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.USER_ACTIVITY}) public void userActivity(long, int, int);
    field @RequiresPermission(android.Manifest.permission.MANAGE_LOW_POWER_STANDBY) public static final String ACTION_LOW_POWER_STANDBY_PORTS_CHANGED = "android.os.action.LOW_POWER_STANDBY_PORTS_CHANGED";
    field public static final int POWER_SAVE_MODE_TRIGGER_DYNAMIC = 1; // 0x1
    field public static final int POWER_SAVE_MODE_TRIGGER_PERCENTAGE = 0; // 0x0
    field public static final String REBOOT_USERSPACE = "userspace";
@@ -10702,6 +10706,25 @@ package android.os {
    method @NonNull public String getIdentifier();
  }
  public static final class PowerManager.LowPowerStandbyPortDescription {
    ctor public PowerManager.LowPowerStandbyPortDescription(int, int, int);
    ctor public PowerManager.LowPowerStandbyPortDescription(int, int, int, @Nullable android.net.LinkAddress);
    method @Nullable public android.net.LinkAddress getBindAddress();
    method public int getPortMatcher();
    method public int getPortNumber();
    method public int getProtocol();
    field public static final int MATCH_PORT_LOCAL = 1; // 0x1
    field public static final int MATCH_PORT_REMOTE = 2; // 0x2
    field public static final int PROTOCOL_TCP = 1; // 0x1
    field public static final int PROTOCOL_UDP = 2; // 0x2
  }
  public final class PowerManager.LowPowerStandbyPortsLock {
    method @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS) public void acquire();
    method protected void finalize();
    method @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS) public void release();
  }
  @Deprecated public class PowerWhitelistManager {
    method @Deprecated @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void addToWhitelist(@NonNull String);
    method @Deprecated @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void addToWhitelist(@NonNull java.util.List<java.lang.String>);
+13 −0
Original line number Diff line number Diff line
@@ -86,6 +86,12 @@ interface IPowerManager
    boolean isExemptFromLowPowerStandby();
    boolean isReasonAllowedInLowPowerStandby(int reason);
    boolean isFeatureAllowedInLowPowerStandby(String feature);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS)")
    void acquireLowPowerStandbyPorts(in IBinder token, in List<LowPowerStandbyPortDescription> ports);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS)")
    void releaseLowPowerStandbyPorts(in IBinder token);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(anyOf = { android.Manifest.permission.MANAGE_LOW_POWER_STANDBY, android.Manifest.permission.DEVICE_POWER })")
    List<LowPowerStandbyPortDescription> getActiveLowPowerStandbyPorts();

    parcelable LowPowerStandbyPolicy {
        String identifier;
@@ -94,6 +100,13 @@ interface IPowerManager
        List<String> allowedFeatures;
    }

    parcelable LowPowerStandbyPortDescription {
        int protocol;
        int portMatcher;
        int portNumber;
        @nullable String bindAddress;
    }

    @UnsupportedAppUsage
    void reboot(boolean confirm, String reason, boolean wait);
    void rebootSafeMode(boolean confirm, boolean wait);
+374 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.annotation.TestApi;
import android.app.PropertyInvalidatedCache;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.net.LinkAddress;
import android.service.dreams.Sandman;
import android.sysprop.InitProperties;
import android.util.ArrayMap;
@@ -47,6 +48,7 @@ import java.lang.annotation.RetentionPolicy;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Executor;
@@ -2471,6 +2473,49 @@ public final class PowerManager {
        }
    }

    /**
     * Creates a new Low Power Standby ports lock.
     *
     * <p>A Low Power Standby ports lock requests that the given ports remain open during
     * Low Power Standby.
     * Call {@link LowPowerStandbyPortsLock#acquire} to acquire the lock.
     * This request is only respected if the calling package is exempt
     * (see {@link #isExemptFromLowPowerStandby()}), and until the returned
     * {@code LowPowerStandbyPorts} object is destroyed or has
     * {@link LowPowerStandbyPortsLock#release} called on it.
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS)
    @NonNull
    public LowPowerStandbyPortsLock newLowPowerStandbyPortsLock(
            @NonNull List<LowPowerStandbyPortDescription> ports) {
        LowPowerStandbyPortsLock standbyPorts = new LowPowerStandbyPortsLock(ports);
        return standbyPorts;
    }

    /**
     * Gets all ports that should remain open in standby.
     * Only includes ports requested by exempt packages (see {@link #getLowPowerStandbyPolicy()}).
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(anyOf = {
            android.Manifest.permission.MANAGE_LOW_POWER_STANDBY,
            android.Manifest.permission.DEVICE_POWER
    })
    @NonNull
    public List<LowPowerStandbyPortDescription> getActiveLowPowerStandbyPorts() {
        try {
            return LowPowerStandbyPortDescription.fromParcelable(
                    mService.getActiveLowPowerStandbyPorts());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Return whether the given application package name is on the device's power allowlist.
     * Apps can be placed on the allowlist through the settings UI invoked by
@@ -2979,6 +3024,19 @@ public final class PowerManager {
    public static final String ACTION_LOW_POWER_STANDBY_POLICY_CHANGED =
            "android.os.action.LOW_POWER_STANDBY_POLICY_CHANGED";

    /**
     * Intent that is broadcast when Low Power Standby exempt ports change.
     * This broadcast is only sent to registered receivers.
     *
     * @see #getActiveLowPowerStandbyPorts
     * @hide
     */
    @SystemApi
    @RequiresPermission(android.Manifest.permission.MANAGE_LOW_POWER_STANDBY)
    @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_LOW_POWER_STANDBY_PORTS_CHANGED =
            "android.os.action.LOW_POWER_STANDBY_PORTS_CHANGED";

    /**
     * Signals that wake-on-lan/wake-on-wlan is allowed in Low Power Standby.
     *
@@ -3153,6 +3211,322 @@ public final class PowerManager {
        }
    }

    /**
     * Describes ports that may be requested to remain open during Low Power Standby.
     *
     * @hide
     */
    @SystemApi
    public static final class LowPowerStandbyPortDescription {
        /** @hide */
        @IntDef(prefix = { "PROTOCOL_" }, value = {
                PROTOCOL_TCP,
                PROTOCOL_UDP,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface Protocol {
        }

        /**
         * Constant to indicate the {@link LowPowerStandbyPortDescription} refers to a TCP port.
         */
        public static final int PROTOCOL_TCP = 1;
        /**
         * Constant to indicate the {@link LowPowerStandbyPortDescription} refers to a UDP port.
         */
        public static final int PROTOCOL_UDP = 2;

        /** @hide */
        @IntDef(prefix = { "MATCH_PORT_" }, value = {
                MATCH_PORT_LOCAL,
                MATCH_PORT_REMOTE,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface PortMatcher {
        }
        /**
         * Constant to indicate the {@link LowPowerStandbyPortDescription}'s port number is to be
         * matched against the socket's local port number (the destination port number of an
         * incoming packet).
         */
        public static final int MATCH_PORT_LOCAL = 1;
        /**
         * Constant to indicate the {@link LowPowerStandbyPortDescription}'s port number is to be
         * matched against the socket's remote port number (the source port number of an
         * incoming packet).
         */
        public static final int MATCH_PORT_REMOTE = 2;

        @Protocol
        private final int mProtocol;
        @PortMatcher
        private final int mPortMatcher;
        private final int mPortNumber;
        @Nullable
        private final LinkAddress mBindAddress;

        /**
         * Describes a port.
         *
         * @param protocol The protocol of the port to match, {@link #PROTOCOL_TCP} or
         *                 {@link #PROTOCOL_UDP}.
         * @param portMatcher Whether to match the source port number of an incoming packet
         *                    ({@link #MATCH_PORT_REMOTE}), or the destination port
         *                    ({@link #MATCH_PORT_LOCAL}).
         * @param portNumber The port number to match.
         *
         * @see #newLowPowerStandbyPortsLock(List)
         */
        public LowPowerStandbyPortDescription(@Protocol int protocol, @PortMatcher int portMatcher,
                int portNumber) {
            this.mProtocol = protocol;
            this.mPortMatcher = portMatcher;
            this.mPortNumber = portNumber;
            this.mBindAddress = null;
        }

        /**
         * Describes a port.
         *
         * @param protocol The protocol of the port to match, {@link #PROTOCOL_TCP} or
         *                 {@link #PROTOCOL_UDP}.
         * @param portMatcher Whether to match the source port number of an incoming packet
         *                    ({@link #MATCH_PORT_REMOTE}), or the destination port
         *                    ({@link #MATCH_PORT_LOCAL}).
         * @param portNumber The port number to match.
         * @param bindAddress The bind address to match.
         *
         * @see #newLowPowerStandbyPortsLock(List)
         */
        public LowPowerStandbyPortDescription(@Protocol int protocol, @PortMatcher int portMatcher,
                int portNumber, @Nullable LinkAddress bindAddress) {
            this.mProtocol = protocol;
            this.mPortMatcher = portMatcher;
            this.mPortNumber = portNumber;
            this.mBindAddress = bindAddress;
        }

        private String protocolToString(int protocol) {
            switch (protocol) {
                case PROTOCOL_TCP: return "TCP";
                case PROTOCOL_UDP: return "UDP";
            }
            return String.valueOf(protocol);
        }

        private String portMatcherToString(int portMatcher) {
            switch (portMatcher) {
                case MATCH_PORT_LOCAL: return "MATCH_PORT_LOCAL";
                case MATCH_PORT_REMOTE: return "MATCH_PORT_REMOTE";
            }
            return String.valueOf(portMatcher);
        }

        /**
         * Returns the described port's protocol,
         * either {@link #PROTOCOL_TCP} or {@link #PROTOCOL_UDP}.
         *
         * @see #PROTOCOL_TCP
         * @see #PROTOCOL_UDP
         * @see #getPortNumber()
         * @see #getPortMatcher()
         */
        @Protocol
        public int getProtocol() {
            return mProtocol;
        }

        /**
         * Returns how the port number ({@link #getPortNumber()}) should be matched against
         * incoming packets.
         * Either {@link #PROTOCOL_TCP} or {@link #PROTOCOL_UDP}.
         *
         * @see #PROTOCOL_TCP
         * @see #PROTOCOL_UDP
         * @see #getPortNumber()
         * @see #getProtocol()
         */
        @PortMatcher
        public int getPortMatcher() {
            return mPortMatcher;
        }

        /**
         * Returns how the port number that incoming packets should be matched against.
         *
         * @see #getPortMatcher()
         * @see #getProtocol()
         */
        public int getPortNumber() {
            return mPortNumber;
        }

        /**
         * Returns the bind address to match against, or {@code null} if matching against any
         * bind address.
         *
         * @see #getPortMatcher()
         * @see #getProtocol()
         */
        @Nullable
        public LinkAddress getBindAddress() {
            return mBindAddress;
        }

        @Override
        public String toString() {
            return "PortDescription{"
                    + "mProtocol=" + protocolToString(mProtocol)
                    + ", mPortMatcher=" + portMatcherToString(mPortMatcher)
                    + ", mPortNumber=" + mPortNumber
                    + ", mBindAddress=" + mBindAddress
                    + '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof LowPowerStandbyPortDescription)) return false;
            LowPowerStandbyPortDescription that = (LowPowerStandbyPortDescription) o;
            return mProtocol == that.mProtocol && mPortMatcher == that.mPortMatcher
                    && mPortNumber == that.mPortNumber && Objects.equals(mBindAddress,
                    that.mBindAddress);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mProtocol, mPortMatcher, mPortNumber, mBindAddress);
        }

        /** @hide */
        public static IPowerManager.LowPowerStandbyPortDescription toParcelable(
                LowPowerStandbyPortDescription portDescription) {
            if (portDescription == null) {
                return null;
            }

            IPowerManager.LowPowerStandbyPortDescription parcelablePortDescription =
                    new IPowerManager.LowPowerStandbyPortDescription();
            parcelablePortDescription.protocol = portDescription.mProtocol;
            parcelablePortDescription.portMatcher = portDescription.mPortMatcher;
            parcelablePortDescription.portNumber = portDescription.mPortNumber;
            if (portDescription.mBindAddress != null) {
                parcelablePortDescription.bindAddress = portDescription.mBindAddress.toString();
            }
            return parcelablePortDescription;
        }

        /** @hide */
        public static List<IPowerManager.LowPowerStandbyPortDescription> toParcelable(
                List<LowPowerStandbyPortDescription> portDescriptions) {
            if (portDescriptions == null) {
                return null;
            }

            ArrayList<IPowerManager.LowPowerStandbyPortDescription> result = new ArrayList<>();
            for (LowPowerStandbyPortDescription port : portDescriptions) {
                result.add(toParcelable(port));
            }
            return result;
        }

        /** @hide */
        public static LowPowerStandbyPortDescription fromParcelable(
                IPowerManager.LowPowerStandbyPortDescription parcelablePortDescription) {
            if (parcelablePortDescription == null) {
                return null;
            }

            LinkAddress bindAddress = null;
            if (parcelablePortDescription.bindAddress != null) {
                bindAddress = new LinkAddress(parcelablePortDescription.bindAddress);
            }
            return new LowPowerStandbyPortDescription(
                    parcelablePortDescription.protocol,
                    parcelablePortDescription.portMatcher,
                    parcelablePortDescription.portNumber,
                    bindAddress);
        }

        /** @hide */
        public static List<LowPowerStandbyPortDescription> fromParcelable(
                List<IPowerManager.LowPowerStandbyPortDescription> portDescriptions) {
            if (portDescriptions == null) {
                return null;
            }

            ArrayList<LowPowerStandbyPortDescription> result = new ArrayList<>();
            for (IPowerManager.LowPowerStandbyPortDescription port : portDescriptions) {
                result.add(fromParcelable(port));
            }
            return result;
        }
    }

    /**
     * An object that can be used to request network ports to remain open during Low Power Standby.
     *
     * <p>Use {@link #newLowPowerStandbyPortsLock} to create a ports lock, and {@link #acquire()}
     * to request the ports to remain open. The request is only respected if the app requesting the
     * lock is exempt from Low Power Standby ({@link #isExemptFromLowPowerStandby()}).
     *
     * @hide
     */
    @SystemApi
    @SuppressLint("NotCloseable")
    public final class LowPowerStandbyPortsLock {
        private final IBinder mToken;
        private final List<LowPowerStandbyPortDescription> mPorts;
        private boolean mHeld;

        LowPowerStandbyPortsLock(List<LowPowerStandbyPortDescription> ports) {
            mPorts = ports;
            mToken = new Binder();
        }

        /** Request the ports to remain open during standby. */
        @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS)
        public void acquire() {
            synchronized (mToken) {
                try {
                    mService.acquireLowPowerStandbyPorts(mToken,
                            LowPowerStandbyPortDescription.toParcelable(mPorts));
                    mHeld = true;
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }
        }

        /**
         * Release the request, allowing these ports to be blocked during standby.
         *
         * <p>Note: This lock is not reference counted, so calling this method will release the lock
         * regardless of how many times {@link #acquire()} has been called before.
         */
        @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS)
        public void release() {
            synchronized (mToken) {
                try {
                    mService.releaseLowPowerStandbyPorts(mToken);
                    mHeld = false;
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }
        }

        @Override
        protected void finalize() {
            synchronized (mToken) {
                if (mHeld) {
                    Log.wtf(TAG, "LowPowerStandbyPorts finalized while still held");
                    release();
                }
            }
        }
    }

    /**
     * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) .
     * @hide
+7 −0
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@
    <protected-broadcast android:name="android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL" />
    <protected-broadcast android:name="android.os.action.LOW_POWER_STANDBY_ENABLED_CHANGED" />
    <protected-broadcast android:name="android.os.action.LOW_POWER_STANDBY_POLICY_CHANGED" />
    <protected-broadcast android:name="android.os.action.LOW_POWER_STANDBY_PORTS_CHANGED" />
    <protected-broadcast android:name="android.os.action.ENHANCED_DISCHARGE_PREDICTION_CHANGED" />

    <!-- @deprecated This is rarely used and will be phased out soon. -->
@@ -5958,6 +5959,12 @@
    <permission android:name="android.permission.MANAGE_LOW_POWER_STANDBY"
                android:protectionLevel="signature|privileged" />

    <!-- @hide @SystemApi Allows an application to request ports to remain open during
         Low Power Standby.
         <p>Not for use by third-party applications. -->
    <permission android:name="android.permission.SET_LOW_POWER_STANDBY_PORTS"
        android:protectionLevel="signature|privileged" />

   <!-- @hide Allows low-level access to tun tap driver -->
    <permission android:name="android.permission.NET_TUNNELING"
        android:protectionLevel="signature|role" />
+1 −0
Original line number Diff line number Diff line
@@ -281,6 +281,7 @@ applications that come with the platform
        <permission name="android.permission.MANAGE_GAME_MODE"/>
        <permission name="android.permission.MANAGE_GAME_ACTIVITY" />
        <permission name="android.permission.MANAGE_LOW_POWER_STANDBY" />
        <permission name="android.permission.SET_LOW_POWER_STANDBY_PORTS" />
        <permission name="android.permission.MANAGE_ROLLBACKS"/>
        <permission name="android.permission.MANAGE_USB"/>
        <!-- Needed for tests only -->
Loading