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

Commit 836dc0da authored by Darryl L Johnson's avatar Darryl L Johnson
Browse files

Add MAX/MIN_DEVICE_STATE constants to restrict possible states to range.

This adds a MAXIMUM_DEVICE_STATE and MINIMUM_DEVICE_STATE constant to
DeviceStateManager that contains the max allowed device state indentifier
which reserves any larger values for future defined constants in the
platform. The constant will be used by CTS to validate that the set
of supported states is in the range
[MINIMUM_DEVICE_STATE, MAXIMUM_DEVICE_STATE].

The values match the max allowed value returned by the default provider
impl which is defined in the XSD schema.

Bug: 159401801
Bug: 177235528
Test: atest DeviceStateTest
Change-Id: Ia5a8e2841bac19d36e9d6cd00950d09628a7f719
parent 8dcb37cc
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -913,6 +913,8 @@ package android.hardware.devicestate {
    method @NonNull public int[] getSupportedStates();
    method public void removeDeviceStateListener(@NonNull android.hardware.devicestate.DeviceStateManager.DeviceStateListener);
    method @RequiresPermission(android.Manifest.permission.CONTROL_DEVICE_STATE) public void requestState(@NonNull android.hardware.devicestate.DeviceStateRequest, @Nullable java.util.concurrent.Executor, @Nullable android.hardware.devicestate.DeviceStateRequest.Callback);
    field public static final int MAXIMUM_DEVICE_STATE = 255; // 0xff
    field public static final int MINIMUM_DEVICE_STATE = 0; // 0x0
  }

  public static interface DeviceStateManager.DeviceStateListener {
+6 −0
Original line number Diff line number Diff line
@@ -42,6 +42,12 @@ public final class DeviceStateManager {
     */
    public static final int INVALID_DEVICE_STATE = -1;

    /** The minimum allowed device state identifier. */
    public static final int MINIMUM_DEVICE_STATE = 0;

    /** The maximum allowed device state identifier. */
    public static final int MAXIMUM_DEVICE_STATE = 255;

    private final DeviceStateManagerGlobal mGlobal;

    /** @hide */
+12 −6
Original line number Diff line number Diff line
@@ -16,9 +16,14 @@

package com.android.server.devicestate;

import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE;
import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE;

import android.annotation.IntRange;
import android.annotation.NonNull;

import com.android.internal.util.Preconditions;

import java.util.Objects;

/**
@@ -35,24 +40,25 @@ import java.util.Objects;
 */
public final class DeviceState {
    /** Unique identifier for the device state. */
    @IntRange(from = 0)
    @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE)
    private final int mIdentifier;

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

    public DeviceState(@IntRange(from = 0) int identifier,
    public DeviceState(
            @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE) int identifier,
            @NonNull String name) {
        if (identifier < 0) {
            throw new IllegalArgumentException("Identifier must be greater than or equal to zero.");
        }
        Preconditions.checkArgumentInRange(identifier, MINIMUM_DEVICE_STATE, MAXIMUM_DEVICE_STATE,
                "identifier");

        mIdentifier = identifier;
        mName = name;
    }

    /** Returns the unique identifier for the device state. */
    @IntRange(from = 0)
    @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE)
    public int getIdentifier() {
        return mIdentifier;
    }
+6 −3
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.server.devicestate;

import static android.Manifest.permission.CONTROL_DEVICE_STATE;
import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE;
import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE;
import static android.hardware.devicestate.DeviceStateRequest.FLAG_CANCEL_WHEN_BASE_CHANGES;

import android.annotation.IntRange;
@@ -89,7 +91,7 @@ public final class DeviceStateManagerService extends SystemService {
    // the current state after the initial callback from the DeviceStateProvider.
    @GuardedBy("mLock")
    @NonNull
    private DeviceState mCommittedState = new DeviceState(0, "UNSET");
    private DeviceState mCommittedState = new DeviceState(MINIMUM_DEVICE_STATE, "UNSET");
    // The device state that is currently awaiting callback from the policy to be committed.
    @GuardedBy("mLock")
    @NonNull
@@ -598,8 +600,9 @@ public final class DeviceStateManagerService extends SystemService {
        }

        @Override
        public void onStateChanged(@IntRange(from = 0) int identifier) {
            if (identifier < 0) {
        public void onStateChanged(
                @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE) int identifier) {
            if (identifier < MINIMUM_DEVICE_STATE || identifier > MAXIMUM_DEVICE_STATE) {
                throw new IllegalArgumentException("Invalid identifier: " + identifier);
            }

+7 −2
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.server.devicestate;

import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE;
import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE;

import android.annotation.IntRange;

/**
@@ -65,8 +68,10 @@ public interface DeviceStateProvider {
         *
         * @param identifier the identifier of the new device state.
         *
         * @throws IllegalArgumentException if the state is less than 0.
         * @throws IllegalArgumentException if the state is less than {@link MINIMUM_DEVICE_STATE}
         * or greater than {@link MAXIMUM_DEVICE_STATE}.
         */
        void onStateChanged(@IntRange(from = 0) int identifier);
        void onStateChanged(
                @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE) int identifier);
    }
}
Loading