diff --git a/core/api/current.txt b/core/api/current.txt index 75fcc60080c1e..a2f7a77864537 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -18170,12 +18170,17 @@ package android.hardware.camera2.params { } public final class DeviceStateSensorOrientationMap { - ctor public DeviceStateSensorOrientationMap(@NonNull long[]); method public int getSensorOrientation(long); field public static final long FOLDED = 4L; // 0x4L field public static final long NORMAL = 0L; // 0x0L } + public static final class DeviceStateSensorOrientationMap.Builder { + ctor public DeviceStateSensorOrientationMap.Builder(); + method @NonNull public android.hardware.camera2.params.DeviceStateSensorOrientationMap.Builder addOrientationForState(long, long); + method @NonNull public android.hardware.camera2.params.DeviceStateSensorOrientationMap build(); + } + public final class DynamicRangeProfiles { ctor public DynamicRangeProfiles(@NonNull long[]); method @NonNull public java.util.Set getProfileCaptureRequestConstraints(long); diff --git a/core/java/android/hardware/camera2/params/DeviceStateSensorOrientationMap.java b/core/java/android/hardware/camera2/params/DeviceStateSensorOrientationMap.java index c4c8eb9ef6f0f..b9a327b5b61dd 100644 --- a/core/java/android/hardware/camera2/params/DeviceStateSensorOrientationMap.java +++ b/core/java/android/hardware/camera2/params/DeviceStateSensorOrientationMap.java @@ -18,11 +18,13 @@ package android.hardware.camera2.params; import android.annotation.LongDef; import android.annotation.NonNull; +import android.annotation.SuppressLint; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.utils.HashCodeHelpers; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Objects; @@ -68,24 +70,20 @@ public final class DeviceStateSensorOrientationMap { FOLDED }) public @interface DeviceState {}; - private final HashMap mDeviceStateOrientationMap = new HashMap<>(); + private final HashMap mDeviceStateOrientationMap; /** * Create a new immutable DeviceStateOrientationMap instance. * - *

The array is a list of pairs of elements (angle, deviceState):

+ *

The array is a list of pairs of elements (deviceState, angle):

* - * [angle0, state0, angle1, state1,..., angleN, stateN] + * [state0, angle0, state1, angle1,..., stateN, angleN] * *

Each pair describes the camera sensor orientation when the device is in the * matching deviceState. The angle is in degrees, and must be a multiple of 90.

* *

This constructor takes over the array; do not write to the array afterwards.

* - *

This constructor is public to allow for easier application testing by - * creating custom object instances. It's not necessary to construct these - * objects during normal use of the camera API.

- * * @param elements * An array of elements describing the map * @@ -94,9 +92,12 @@ public final class DeviceStateSensorOrientationMap { * invalid element values * @throws NullPointerException * if {@code elements} is {@code null} + * + * @hide */ public DeviceStateSensorOrientationMap(@NonNull final long[] elements) { mElements = Objects.requireNonNull(elements, "elements must not be null"); + mDeviceStateOrientationMap = new HashMap<>(); if ((elements.length % 2) != 0) { throw new IllegalArgumentException("Device state sensor orientation map length " + elements.length + " is not even!"); @@ -112,6 +113,20 @@ public final class DeviceStateSensorOrientationMap { } } + /** + * Used by the Builder only. + * + * @hide + */ + private DeviceStateSensorOrientationMap(@NonNull final ArrayList elements, + @NonNull final HashMap deviceStateOrientationMap) { + mElements = new long[elements.size()]; + for (int i = 0; i < elements.size(); i++) { + mElements[i] = elements.get(i); + } + mDeviceStateOrientationMap = deviceStateOrientationMap; + } + /** * Return the logical camera sensor orientation given a specific device fold state. * @@ -163,4 +178,58 @@ public final class DeviceStateSensorOrientationMap { } private final long[] mElements; + + /** + * Builds a DeviceStateSensorOrientationMap object. + * + *

This builder is public to allow for easier application testing by + * creating custom object instances. It's not necessary to construct these + * objects during normal use of the camera API.

+ */ + public static final class Builder { + public Builder() { + // Empty + } + + /** + * Add a sensor orientation for a given device state. + * + *

Each pair of deviceState and angle describes the camera sensor orientation when the + * device is in the matching deviceState. The angle is in degrees, and must be a multiple + * of 90.

+ * + * @param deviceState The deviceState. + * @param angle The orientation angle in degrees. + * @return This builder. + * + */ + @SuppressLint("MissingGetterMatchingBuilder") + public @NonNull Builder addOrientationForState(long deviceState, long angle) { + if (angle % 90 != 0) { + throw new IllegalArgumentException("Sensor orientation not divisible by 90: " + + angle); + } + mDeviceStateOrientationMap.put(deviceState, Math.toIntExact(angle)); + mElements.add(deviceState); + mElements.add(angle); + return this; + } + + /** + * Returns an instance of DeviceStateSensorOrientationMap created from the + * fields set on this builder. + * + * @return A DeviceStateSensorOrientationMap. + */ + public @NonNull DeviceStateSensorOrientationMap build() { + if (mElements.size() == 0) { + throw new IllegalStateException("Cannot build a DeviceStateSensorOrientationMap" + + " with zero elements."); + } + return new DeviceStateSensorOrientationMap(mElements, mDeviceStateOrientationMap); + } + + private final ArrayList mElements = new ArrayList<>(); + private final HashMap mDeviceStateOrientationMap = new HashMap<>(); + } }