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

Commit f131f0bf authored by Darryl Johnson's avatar Darryl Johnson Committed by Android (Google) Code Review
Browse files

Merge "Add MAX/MIN_DEVICE_STATE constants to restrict possible states to range." into sc-dev

parents da11ca67 836dc0da
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -914,6 +914,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