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

Commit 41cb66fa authored by Marin Shalamanov's avatar Marin Shalamanov
Browse files

Change the type of display port from byte to int

Using byte for display port is error prone since ports
are in the range [0, 255] and bytes have the range [-128, 127].
This way we need to downcast from int to byte in order to write a
value to display port and also we need to call Byte.toUnsignedInt
every time we want to consume it.

Test: m services
Bug: 153334857
Change-Id: I4dce87c0a411c5d447f62cc5564eb4b8a8fb75f0
parent 793137ab
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -24,6 +24,7 @@ import android.graphics.Rect;
import android.text.TextUtils;
import android.text.TextUtils;


import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.util.Objects;


/**
/**
 * Describes how the pixels of physical display device reflects the content of
 * Describes how the pixels of physical display device reflects the content of
@@ -73,7 +74,7 @@ public final class DisplayViewport {
    public String uniqueId;
    public String uniqueId;


    // The physical port that the associated display device is connected to.
    // The physical port that the associated display device is connected to.
    public @Nullable Byte physicalPort;
    public @Nullable Integer physicalPort;


    public @ViewportType int type;
    public @ViewportType int type;


@@ -118,7 +119,7 @@ public final class DisplayViewport {
              && deviceWidth == other.deviceWidth
              && deviceWidth == other.deviceWidth
              && deviceHeight == other.deviceHeight
              && deviceHeight == other.deviceHeight
              && TextUtils.equals(uniqueId, other.uniqueId)
              && TextUtils.equals(uniqueId, other.uniqueId)
              && physicalPort == other.physicalPort
              && Objects.equals(physicalPort, other.physicalPort)
              && type == other.type;
              && type == other.type;
    }
    }


@@ -144,12 +145,11 @@ public final class DisplayViewport {
    // For debugging purposes.
    // For debugging purposes.
    @Override
    @Override
    public String toString() {
    public String toString() {
        final Integer port = physicalPort == null ? null : Byte.toUnsignedInt(physicalPort);
        return "DisplayViewport{type=" + typeToString(type)
        return "DisplayViewport{type=" + typeToString(type)
                + ", valid=" + valid
                + ", valid=" + valid
                + ", displayId=" + displayId
                + ", displayId=" + displayId
                + ", uniqueId='" + uniqueId + "'"
                + ", uniqueId='" + uniqueId + "'"
                + ", physicalPort=" + port
                + ", physicalPort=" + physicalPort
                + ", orientation=" + orientation
                + ", orientation=" + orientation
                + ", logicalFrame=" + logicalFrame
                + ", logicalFrame=" + logicalFrame
                + ", physicalFrame=" + physicalFrame
                + ", physicalFrame=" + physicalFrame
+11 −8
Original line number Original line Diff line number Diff line
@@ -43,12 +43,12 @@ public abstract class DisplayAddress implements Parcelable {
    /**
    /**
     * Creates an address for a physical display given its port and model.
     * Creates an address for a physical display given its port and model.
     *
     *
     * @param port A port in the range [0, 255] interpreted as signed.
     * @param port A port in the range [0, 255].
     * @param model A positive integer, or {@code null} if the model cannot be identified.
     * @param model A positive integer, or {@code null} if the model cannot be identified.
     * @return The {@link Physical} address.
     * @return The {@link Physical} address.
     */
     */
    @NonNull
    @NonNull
    public static Physical fromPortAndModel(byte port, Long model) {
    public static Physical fromPortAndModel(int port, Long model) {
        return new Physical(port, model);
        return new Physical(port, model);
    }
    }


@@ -92,10 +92,10 @@ public abstract class DisplayAddress implements Parcelable {
        /**
        /**
         * Physical port to which the display is connected.
         * Physical port to which the display is connected.
         *
         *
         * @return A port in the range [0, 255] interpreted as signed.
         * @return A port in the range [0, 255].
         */
         */
        public byte getPort() {
        public int getPort() {
            return (byte) mPhysicalDisplayId;
            return (int) (mPhysicalDisplayId & 0xFF);
        }
        }


        /**
        /**
@@ -118,7 +118,7 @@ public abstract class DisplayAddress implements Parcelable {
        @Override
        @Override
        public String toString() {
        public String toString() {
            final StringBuilder builder = new StringBuilder("{")
            final StringBuilder builder = new StringBuilder("{")
                    .append("port=").append(Byte.toUnsignedInt(getPort()));
                    .append("port=").append(getPort());


            final Long model = getModel();
            final Long model = getModel();
            if (model != null) {
            if (model != null) {
@@ -142,8 +142,11 @@ public abstract class DisplayAddress implements Parcelable {
            mPhysicalDisplayId = physicalDisplayId;
            mPhysicalDisplayId = physicalDisplayId;
        }
        }


        private Physical(byte port, Long model) {
        private Physical(int port, Long model) {
            mPhysicalDisplayId = Byte.toUnsignedLong(port)
            if (port < 0 || port > 255) {
                throw new IllegalArgumentException("The port should be in the interval [0, 255]");
            }
            mPhysicalDisplayId = Integer.toUnsignedLong(port)
                    | (model == null ? UNKNOWN_MODEL : (model << MODEL_SHIFT));
                    | (model == null ? UNKNOWN_MODEL : (model << MODEL_SHIFT));
        }
        }


+4 −4
Original line number Original line Diff line number Diff line
@@ -55,8 +55,8 @@ static struct {


status_t android_hardware_display_DisplayViewport_toNative(JNIEnv* env, jobject viewportObj,
status_t android_hardware_display_DisplayViewport_toNative(JNIEnv* env, jobject viewportObj,
        DisplayViewport* viewport) {
        DisplayViewport* viewport) {
    static const jclass byteClass = FindClassOrDie(env, "java/lang/Byte");
    static const jclass intClass = FindClassOrDie(env, "java/lang/Integer");
    static const jmethodID byteValue = env->GetMethodID(byteClass, "byteValue", "()B");
    static const jmethodID byteValue = env->GetMethodID(intClass, "byteValue", "()B");


    viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId);
    viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId);
    viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
    viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
@@ -122,8 +122,8 @@ int register_android_hardware_display_DisplayViewport(JNIEnv* env) {
    gDisplayViewportClassInfo.uniqueId = GetFieldIDOrDie(env,
    gDisplayViewportClassInfo.uniqueId = GetFieldIDOrDie(env,
            gDisplayViewportClassInfo.clazz, "uniqueId", "Ljava/lang/String;");
            gDisplayViewportClassInfo.clazz, "uniqueId", "Ljava/lang/String;");


    gDisplayViewportClassInfo.physicalPort = GetFieldIDOrDie(env,
    gDisplayViewportClassInfo.physicalPort = GetFieldIDOrDie(env, gDisplayViewportClassInfo.clazz,
            gDisplayViewportClassInfo.clazz, "physicalPort", "Ljava/lang/Byte;");
                                                             "physicalPort", "Ljava/lang/Integer;");


    gDisplayViewportClassInfo.type = GetFieldIDOrDie(env,
    gDisplayViewportClassInfo.type = GetFieldIDOrDie(env,
            gDisplayViewportClassInfo.clazz, "type", "I");
            gDisplayViewportClassInfo.clazz, "type", "I");
+1 −1
Original line number Original line Diff line number Diff line
@@ -987,7 +987,7 @@ final class LocalDisplayAdapter extends DisplayAdapter {
            int[] ports = res.getIntArray(
            int[] ports = res.getIntArray(
                    com.android.internal.R.array.config_localPrivateDisplayPorts);
                    com.android.internal.R.array.config_localPrivateDisplayPorts);
            if (ports != null) {
            if (ports != null) {
                int port = Byte.toUnsignedInt(physicalAddress.getPort());
                int port = physicalAddress.getPort();
                for (int p : ports) {
                for (int p : ports) {
                    if (p == port) {
                    if (p == port) {
                        return true;
                        return true;
+1 −2
Original line number Original line Diff line number Diff line
@@ -642,8 +642,7 @@ class DisplayWindowSettings {
        if (mIdentifier == IDENTIFIER_PORT && displayInfo.address != null) {
        if (mIdentifier == IDENTIFIER_PORT && displayInfo.address != null) {
            // Config suggests using port as identifier for physical displays.
            // Config suggests using port as identifier for physical displays.
            if (displayInfo.address instanceof DisplayAddress.Physical) {
            if (displayInfo.address instanceof DisplayAddress.Physical) {
                byte port = ((DisplayAddress.Physical) displayInfo.address).getPort();
                return "port:" + ((DisplayAddress.Physical) displayInfo.address).getPort();
                return "port:" + Byte.toUnsignedInt(port);
            }
            }
        }
        }
        return displayInfo.uniqueId;
        return displayInfo.uniqueId;
Loading