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

Commit 3d51486f authored by Kenneth Ford's avatar Kenneth Ford Committed by Android (Google) Code Review
Browse files

Merge changes I8edf60fc,I4e33a32f,Iec1dbd59,Icd8af7b9 into main

* changes:
  Rename DeviceStateManager#INVALID_DEVICE_STATE_IDENTIFIER
  Pass DeviceState objects from DeviceStateManagerService to the client
  Updates FoldableDeviceStateProvider to use properties instead of flags
  Update device_state_configuration.xml schema to support new properties
parents f8a624a7 16a1cf4b
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -1624,8 +1624,6 @@ package android.hardware.camera2.params {
package android.hardware.devicestate {

  @FlaggedApi("android.hardware.devicestate.feature.flags.device_state_property_api") public final class DeviceState {
    ctor @Deprecated public DeviceState(@IntRange(from=android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE_IDENTIFIER, to=android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE_IDENTIFIER) int, @NonNull String, int);
    ctor public DeviceState(@IntRange(from=android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE_IDENTIFIER, to=android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE_IDENTIFIER) int, @NonNull String, @NonNull java.util.Set<java.lang.Integer>);
    field public static final int PROPERTY_POLICY_AVAILABLE_FOR_APP_REQUEST = 8; // 0x8
  }

+248 −45
Original line number Diff line number Diff line
@@ -25,8 +25,9 @@ import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.TestApi;

import com.android.internal.util.Preconditions;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArraySet;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -37,8 +38,7 @@ import java.util.Objects;
import java.util.Set;

/**
 * A state of the device defined by the {@link DeviceStateProvider} and managed by the
 * {@link DeviceStateManagerService}.
 * A state of the device managed by {@link DeviceStateManager}.
 * <p>
 * Device state is an abstract concept that allows mapping the current state of the device to the
 * state of the system. This is useful for variable-state devices, like foldable or rollable
@@ -268,68 +268,88 @@ public final class DeviceState {
    @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
    public @interface DeviceStateProperties {}

    /** Unique identifier for the device state. */
    @IntRange(from = MINIMUM_DEVICE_STATE_IDENTIFIER, to = MAXIMUM_DEVICE_STATE_IDENTIFIER)
    private final int mIdentifier;
    /** @hide */
    @IntDef(prefix = {"PROPERTY_"}, flag = true, value = {
            PROPERTY_FOLDABLE_HARDWARE_CONFIGURATION_FOLD_IN_CLOSED,
            PROPERTY_FOLDABLE_HARDWARE_CONFIGURATION_FOLD_IN_HALF_OPEN,
            PROPERTY_FOLDABLE_HARDWARE_CONFIGURATION_FOLD_IN_OPEN
    })
    @Retention(RetentionPolicy.SOURCE)
    @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
    public @interface PhysicalDeviceStateProperties {}

    /** @hide */
    @IntDef(prefix = {"PROPERTY_"}, flag = true, value = {
            PROPERTY_POLICY_CANCEL_OVERRIDE_REQUESTS,
            PROPERTY_POLICY_CANCEL_WHEN_REQUESTER_NOT_ON_TOP,
            PROPERTY_POLICY_UNSUPPORTED_WHEN_THERMAL_STATUS_CRITICAL,
            PROPERTY_POLICY_UNSUPPORTED_WHEN_POWER_SAVE_MODE,
            PROPERTY_POLICY_AVAILABLE_FOR_APP_REQUEST,
            PROPERTY_APP_INACCESSIBLE,
            PROPERTY_EMULATED_ONLY,
            PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_OUTER_PRIMARY,
            PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_INNER_PRIMARY,
            PROPERTY_POWER_CONFIGURATION_TRIGGER_SLEEP,
            PROPERTY_POWER_CONFIGURATION_TRIGGER_WAKE,
            PROPERTY_EXTENDED_DEVICE_STATE_EXTERNAL_DISPLAY,
            PROPERTY_FEATURE_REAR_DISPLAY,
            PROPERTY_FEATURE_DUAL_DISPLAY_INTERNAL_DEFAULT
    })
    @Retention(RetentionPolicy.SOURCE)
    @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
    public @interface SystemDeviceStateProperties {}

    /** String description of the device state. */
    @NonNull
    private final String mName;
    private final DeviceState.Configuration mDeviceStateConfiguration;

    @DeviceStateFlags
    private final int mFlags;

    private final Set<@DeviceStateProperties Integer> mProperties;
    /** @hide */
    public DeviceState(@NonNull DeviceState.Configuration deviceStateConfiguration) {
        Objects.requireNonNull(deviceStateConfiguration, "Device StateConfiguration is null");
        mDeviceStateConfiguration = deviceStateConfiguration;
        mFlags = 0;
    }

    /** @hide */
    public DeviceState(
            @IntRange(from = MINIMUM_DEVICE_STATE_IDENTIFIER, to =
                    MAXIMUM_DEVICE_STATE_IDENTIFIER) int identifier,
            @NonNull String name,
            @NonNull Set<@DeviceStateProperties Integer> properties) {
        mDeviceStateConfiguration = new DeviceState.Configuration(identifier, name, properties,
                Collections.emptySet());
        mFlags = 0;
    }

    /**
     * @deprecated Deprecated in favor of {@link #DeviceState(int, String, Set)}
     * @hide
     */
    // TODO(b/325124054): Make non-default and remove deprecated callback methods.
    @TestApi
    @Deprecated
    public DeviceState(
            @IntRange(from = MINIMUM_DEVICE_STATE_IDENTIFIER, to =
                    MAXIMUM_DEVICE_STATE_IDENTIFIER) int identifier,
            @NonNull String name,
            @DeviceStateFlags int flags) {
        Preconditions.checkArgumentInRange(identifier, MINIMUM_DEVICE_STATE_IDENTIFIER,
                MAXIMUM_DEVICE_STATE_IDENTIFIER,
                "identifier");

        mIdentifier = identifier;
        mName = name;
        mDeviceStateConfiguration = new DeviceState.Configuration(identifier, name,
                Collections.emptySet(), Collections.emptySet());
        mFlags = flags;
        mProperties = Collections.emptySet();
    }

    /** @hide */
    @TestApi
    public DeviceState(
            @IntRange(from = MINIMUM_DEVICE_STATE_IDENTIFIER, to =
                    MAXIMUM_DEVICE_STATE_IDENTIFIER) int identifier,
            @NonNull String name,
            @NonNull Set<@DeviceStateProperties Integer> properties) {
        Preconditions.checkArgumentInRange(identifier, MINIMUM_DEVICE_STATE_IDENTIFIER,
                MAXIMUM_DEVICE_STATE_IDENTIFIER,
                "identifier");

        mIdentifier = identifier;
        mName = name;
        mProperties = Set.copyOf(properties);
        mFlags = 0;
    }

    /** Returns the unique identifier for the device state. */
    @IntRange(from = MINIMUM_DEVICE_STATE_IDENTIFIER)
    public int getIdentifier() {
        return mIdentifier;
        return mDeviceStateConfiguration.getIdentifier();
    }

    /** Returns a string description of the device state. */
    @NonNull
    public String getName() {
        return mName;
        return mDeviceStateConfiguration.getName();
    }

    /**
@@ -345,10 +365,13 @@ public final class DeviceState {

    @Override
    public String toString() {
        return "DeviceState{" + "identifier=" + mIdentifier + ", name='" + mName + '\''
                + ", app_accessible=" + !hasProperty(PROPERTY_APP_INACCESSIBLE)
        return "DeviceState{" + "identifier=" + mDeviceStateConfiguration.getIdentifier()
                + ", name='" + mDeviceStateConfiguration.getName() + '\''
                + ", app_accessible=" + !mDeviceStateConfiguration.getSystemProperties().contains(
                PROPERTY_APP_INACCESSIBLE)
                + ", cancel_when_requester_not_on_top="
                + hasProperty(PROPERTY_POLICY_CANCEL_WHEN_REQUESTER_NOT_ON_TOP)
                + mDeviceStateConfiguration.getSystemProperties().contains(
                PROPERTY_POLICY_CANCEL_WHEN_REQUESTER_NOT_ON_TOP)
                + "}";
    }

@@ -357,14 +380,12 @@ public final class DeviceState {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DeviceState that = (DeviceState) o;
        return mIdentifier == that.mIdentifier
                && Objects.equals(mName, that.mName)
                && Objects.equals(mProperties, that.mProperties);
        return Objects.equals(mDeviceStateConfiguration, that.mDeviceStateConfiguration);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mIdentifier, mName, mProperties);
        return Objects.hash(mDeviceStateConfiguration);
    }

    /** Checks if a specific flag is set
@@ -381,7 +402,8 @@ public final class DeviceState {
     * Checks if a specific property is set on this state
     */
    public boolean hasProperty(@DeviceStateProperties int propertyToCheckFor) {
        return mProperties.contains(propertyToCheckFor);
        return mDeviceStateConfiguration.mSystemProperties.contains(propertyToCheckFor)
                || mDeviceStateConfiguration.mPhysicalProperties.contains(propertyToCheckFor);
    }

    /**
@@ -389,10 +411,191 @@ public final class DeviceState {
     */
    public boolean hasProperties(@NonNull @DeviceStateProperties int... properties) {
        for (int i = 0; i < properties.length; i++) {
            if (mProperties.contains(properties[i])) {
            if (!hasProperty(properties[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns the underlying {@link DeviceState.Configuration} object used to model the
     * device state.
     * @hide
     */
    public Configuration getConfiguration() {
        return mDeviceStateConfiguration;
    }

    /**
     * Detailed description of a {@link DeviceState} that includes separated sets of
     * {@link DeviceStateProperties} for properties that correspond to the state of the system when
     * the device is in this state, as well as physical properties that describe this state.
     *
     * Instantiation of this class should only be done by the system server, and clients of
     * {@link DeviceStateManager} will receive {@link DeviceState} objects.
     *
     * @see DeviceStateManager
     * @hide
     */
    public static final class Configuration implements Parcelable {
        /** Unique identifier for the device state. */
        @IntRange(from = MINIMUM_DEVICE_STATE_IDENTIFIER, to = MAXIMUM_DEVICE_STATE_IDENTIFIER)
        private final int mIdentifier;

        /** String description of the device state. */
        @NonNull
        private final String mName;

        /** {@link ArraySet} of system properties that apply to this state. */
        @NonNull
        private final ArraySet<@SystemDeviceStateProperties Integer> mSystemProperties;

        /** {@link ArraySet} of physical device properties that apply to this state. */
        @NonNull
        private final ArraySet<@PhysicalDeviceStateProperties Integer> mPhysicalProperties;

        private Configuration(int identifier, @NonNull String name,
                @NonNull Set<@SystemDeviceStateProperties Integer> systemProperties,
                @NonNull Set<@PhysicalDeviceStateProperties Integer> physicalProperties) {
            mIdentifier = identifier;
            mName = name;
            mSystemProperties = new ArraySet<@SystemDeviceStateProperties Integer>(
                    systemProperties);
            mPhysicalProperties = new ArraySet<@PhysicalDeviceStateProperties Integer>(
                    physicalProperties);
        }

        /** Returns the unique identifier for the device state. */
        public int getIdentifier() {
            return mIdentifier;
        }

        /** Returns a string description of the device state. */
        @NonNull
        public String getName() {
            return mName;
        }

        /** Returns the {@link Set} of system properties that apply to this state. */
        @NonNull
        public Set<@SystemDeviceStateProperties Integer> getSystemProperties() {
            return mSystemProperties;
        }

        /** Returns the {@link Set} of physical device properties that apply to this state. */
        @NonNull
        public Set<@DeviceStateProperties Integer> getPhysicalProperties() {
            return mPhysicalProperties;
        }

        @Override
        public String toString() {
            return "DeviceState{" + "identifier=" + mIdentifier
                    + ", name='" + mName + '\''
                    + ", app_accessible=" + mSystemProperties.contains(PROPERTY_APP_INACCESSIBLE)
                    + ", cancel_when_requester_not_on_top="
                    + mSystemProperties.contains(PROPERTY_POLICY_CANCEL_WHEN_REQUESTER_NOT_ON_TOP)
                    + "}";
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DeviceState.Configuration that = (DeviceState.Configuration) o;
            return mIdentifier == that.mIdentifier
                    && Objects.equals(mName, that.mName)
                    && Objects.equals(mSystemProperties, that.mSystemProperties)
                    && Objects.equals(mPhysicalProperties, that.mPhysicalProperties);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mIdentifier, mName, mSystemProperties, mPhysicalProperties);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeInt(mIdentifier);
            dest.writeString8(mName);

            dest.writeInt(mSystemProperties.size());
            for (int i = 0; i < mSystemProperties.size(); i++) {
                dest.writeInt(mSystemProperties.valueAt(i));
            }

            dest.writeInt(mPhysicalProperties.size());
            for (int i = 0; i < mPhysicalProperties.size(); i++) {
                dest.writeInt(mPhysicalProperties.valueAt(i));
            }
        }

        @NonNull
        public static final Creator<DeviceState.Configuration> CREATOR = new Creator<>() {
            @Override
            public DeviceState.Configuration createFromParcel(Parcel source) {
                int identifier = source.readInt();
                String name = source.readString8();
                ArraySet<@DeviceStateProperties Integer> systemProperties = new ArraySet<>();
                for (int i = 0; i < source.readInt(); i++) {
                    systemProperties.add(source.readInt());
                }
                ArraySet<@DeviceStateProperties Integer> physicalProperties = new ArraySet<>();
                for (int j = 0; j < source.readInt(); j++) {
                    physicalProperties.add(source.readInt());
                }
                return new DeviceState.Configuration(identifier, name, systemProperties,
                        physicalProperties);
            }

            @Override
            public DeviceState.Configuration[] newArray(int size) {
                return new DeviceState.Configuration[size];
            }
        };

        /** @hide */
        public static class Builder {
            private final int mIdentifier;
            private final String mName;
            private Set<@SystemDeviceStateProperties Integer> mSystemProperties =
                    Collections.emptySet();
            private Set<@PhysicalDeviceStateProperties Integer> mPhysicalProperties =
                    Collections.emptySet();

            public Builder(int identifier, String name) {
                mIdentifier = identifier;
                mName = name;
            }

            /** Sets the system properties for this {@link DeviceState.Configuration.Builder} */
            public Builder setSystemProperties(
                    Set<@SystemDeviceStateProperties Integer> systemProperties) {
                mSystemProperties = systemProperties;
                return this;
            }

            /** Sets the system properties for this {@link DeviceState.Configuration.Builder} */
            public Builder setPhysicalProperties(
                    Set<@PhysicalDeviceStateProperties Integer> physicalProperties) {
                mPhysicalProperties = physicalProperties;
                return this;
            }

            /**
             * Returns a new {@link DeviceState.Configuration} whose values match the values set on
             * the builder.
             */
            public DeviceState.Configuration build() {
                return new DeviceState.Configuration(mIdentifier, mName, mSystemProperties,
                        mPhysicalProperties);
            }
        }
    }
}
+31 −25
Original line number Diff line number Diff line
@@ -24,7 +24,8 @@ import android.os.Parcelable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;

@@ -56,19 +57,19 @@ public final class DeviceStateInfo implements Parcelable {
     * The list of states supported by the device.
     */
    @NonNull
    public final int[] supportedStates;
    public final ArrayList<DeviceState> supportedStates;

    /**
     * The base (non-override) state of the device. The base state is the state of the device
     * ignoring any override requests made through a call to {@link DeviceStateManager#requestState(
     * DeviceStateRequest, Executor, DeviceStateRequest.Callback)}.
     */
    public final int baseState;
    public final DeviceState baseState;

    /**
     * The state of the device.
     */
    public final int currentState;
    public final DeviceState currentState;

    /**
     * Creates a new instance of {@link DeviceStateInfo}.
@@ -76,8 +77,9 @@ public final class DeviceStateInfo implements Parcelable {
     * NOTE: Unlike {@link #DeviceStateInfo(DeviceStateInfo)}, this constructor does not copy the
     * supplied parameters.
     */
    public DeviceStateInfo(@NonNull int[] supportedStates, int baseState, int state) {
        this.supportedStates = supportedStates;
    public DeviceStateInfo(@NonNull List<DeviceState> supportedStates, DeviceState baseState,
            DeviceState state) {
        this.supportedStates = new ArrayList<>(supportedStates);
        this.baseState = baseState;
        this.currentState = state;
    }
@@ -87,8 +89,7 @@ public final class DeviceStateInfo implements Parcelable {
     * the fields of the returned instance.
     */
    public DeviceStateInfo(@NonNull DeviceStateInfo info) {
        this(Arrays.copyOf(info.supportedStates, info.supportedStates.length),
                info.baseState, info.currentState);
        this(List.copyOf(info.supportedStates), info.baseState, info.currentState);
    }

    @Override
@@ -96,15 +97,15 @@ public final class DeviceStateInfo implements Parcelable {
        if (this == other) return true;
        if (other == null || getClass() != other.getClass()) return false;
        DeviceStateInfo that = (DeviceStateInfo) other;
        return baseState == that.baseState
                &&  currentState == that.currentState
                && Arrays.equals(supportedStates, that.supportedStates);
        return baseState.equals(that.baseState)
                &&  currentState.equals(that.currentState)
                && Objects.equals(supportedStates, that.supportedStates);
    }

    @Override
    public int hashCode() {
        int result = Objects.hash(baseState, currentState);
        result = 31 * result + Arrays.hashCode(supportedStates);
        result = 31 * result + supportedStates.hashCode();
        return result;
    }

@@ -112,13 +113,13 @@ public final class DeviceStateInfo implements Parcelable {
    @ChangeFlags
    public int diff(@NonNull DeviceStateInfo other) {
        int diff = 0;
        if (!Arrays.equals(supportedStates, other.supportedStates)) {
        if (!supportedStates.equals(other.supportedStates)) {
            diff |= CHANGED_SUPPORTED_STATES;
        }
        if (baseState != other.baseState) {
        if (!baseState.equals(other.baseState)) {
            diff |= CHANGED_BASE_STATE;
        }
        if (currentState != other.currentState) {
        if (!currentState.equals(other.currentState)) {
            diff |= CHANGED_CURRENT_STATE;
        }
        return diff;
@@ -126,13 +127,13 @@ public final class DeviceStateInfo implements Parcelable {

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(supportedStates.length);
        for (int i = 0; i < supportedStates.length; i++) {
            dest.writeInt(supportedStates[i]);
        dest.writeInt(supportedStates.size());
        for (int i = 0; i < supportedStates.size(); i++) {
            dest.writeTypedObject(supportedStates.get(i).getConfiguration(), flags);
        }

        dest.writeInt(baseState);
        dest.writeInt(currentState);
        dest.writeTypedObject(baseState.getConfiguration(), flags);
        dest.writeTypedObject(currentState.getConfiguration(), flags);
    }

    @Override
@@ -140,16 +141,21 @@ public final class DeviceStateInfo implements Parcelable {
        return 0;
    }

    public static final @NonNull Creator<DeviceStateInfo> CREATOR = new Creator<DeviceStateInfo>() {
    public static final @NonNull Creator<DeviceStateInfo> CREATOR = new Creator<>() {
        @Override
        public DeviceStateInfo createFromParcel(Parcel source) {
            final int numberOfSupportedStates = source.readInt();
            final int[] supportedStates = new int[numberOfSupportedStates];
            final ArrayList<DeviceState> supportedStates = new ArrayList<>(numberOfSupportedStates);
            for (int i = 0; i < numberOfSupportedStates; i++) {
                supportedStates[i] = source.readInt();
                DeviceState.Configuration configuration = source.readTypedObject(
                        DeviceState.Configuration.CREATOR);
                supportedStates.add(i, new DeviceState(configuration));
            }
            final int baseState = source.readInt();
            final int currentState = source.readInt();

            final DeviceState baseState = new DeviceState(
                    source.readTypedObject(DeviceState.Configuration.CREATOR));
            final DeviceState currentState = new DeviceState(
                    source.readTypedObject(DeviceState.Configuration.CREATOR));

            return new DeviceStateInfo(supportedStates, baseState, currentState);
        }
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ public final class DeviceStateManager {
     *
     * @hide
     */
    public static final int INVALID_DEVICE_STATE = -1;
    public static final int INVALID_DEVICE_STATE_IDENTIFIER = -1;

    /**
     * The minimum allowed device state identifier.
+27 −68

File changed.

Preview size limit exceeded, changes collapsed.

Loading