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
This commit is contained in:
Marin Shalamanov
2020-04-24 17:33:59 +02:00
parent 793137aba1
commit 41cb66fa00
7 changed files with 27 additions and 25 deletions

View File

@@ -24,6 +24,7 @@ import android.graphics.Rect;
import android.text.TextUtils;
import java.lang.annotation.Retention;
import java.util.Objects;
/**
* Describes how the pixels of physical display device reflects the content of
@@ -73,7 +74,7 @@ public final class DisplayViewport {
public String uniqueId;
// The physical port that the associated display device is connected to.
public @Nullable Byte physicalPort;
public @Nullable Integer physicalPort;
public @ViewportType int type;
@@ -118,7 +119,7 @@ public final class DisplayViewport {
&& deviceWidth == other.deviceWidth
&& deviceHeight == other.deviceHeight
&& TextUtils.equals(uniqueId, other.uniqueId)
&& physicalPort == other.physicalPort
&& Objects.equals(physicalPort, other.physicalPort)
&& type == other.type;
}
@@ -144,12 +145,11 @@ public final class DisplayViewport {
// For debugging purposes.
@Override
public String toString() {
final Integer port = physicalPort == null ? null : Byte.toUnsignedInt(physicalPort);
return "DisplayViewport{type=" + typeToString(type)
+ ", valid=" + valid
+ ", displayId=" + displayId
+ ", uniqueId='" + uniqueId + "'"
+ ", physicalPort=" + port
+ ", physicalPort=" + physicalPort
+ ", orientation=" + orientation
+ ", logicalFrame=" + logicalFrame
+ ", physicalFrame=" + physicalFrame

View File

@@ -43,12 +43,12 @@ public abstract class DisplayAddress implements Parcelable {
/**
* 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.
* @return The {@link Physical} address.
*/
@NonNull
public static Physical fromPortAndModel(byte port, Long model) {
public static Physical fromPortAndModel(int port, Long model) {
return new Physical(port, model);
}
@@ -92,10 +92,10 @@ public abstract class DisplayAddress implements Parcelable {
/**
* 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() {
return (byte) mPhysicalDisplayId;
public int getPort() {
return (int) (mPhysicalDisplayId & 0xFF);
}
/**
@@ -118,7 +118,7 @@ public abstract class DisplayAddress implements Parcelable {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder("{")
.append("port=").append(Byte.toUnsignedInt(getPort()));
.append("port=").append(getPort());
final Long model = getModel();
if (model != null) {
@@ -142,8 +142,11 @@ public abstract class DisplayAddress implements Parcelable {
mPhysicalDisplayId = physicalDisplayId;
}
private Physical(byte port, Long model) {
mPhysicalDisplayId = Byte.toUnsignedLong(port)
private Physical(int port, Long model) {
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));
}

View File

@@ -55,8 +55,8 @@ static struct {
status_t android_hardware_display_DisplayViewport_toNative(JNIEnv* env, jobject viewportObj,
DisplayViewport* viewport) {
static const jclass byteClass = FindClassOrDie(env, "java/lang/Byte");
static const jmethodID byteValue = env->GetMethodID(byteClass, "byteValue", "()B");
static const jclass intClass = FindClassOrDie(env, "java/lang/Integer");
static const jmethodID byteValue = env->GetMethodID(intClass, "byteValue", "()B");
viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId);
viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
@@ -122,8 +122,8 @@ int register_android_hardware_display_DisplayViewport(JNIEnv* env) {
gDisplayViewportClassInfo.uniqueId = GetFieldIDOrDie(env,
gDisplayViewportClassInfo.clazz, "uniqueId", "Ljava/lang/String;");
gDisplayViewportClassInfo.physicalPort = GetFieldIDOrDie(env,
gDisplayViewportClassInfo.clazz, "physicalPort", "Ljava/lang/Byte;");
gDisplayViewportClassInfo.physicalPort = GetFieldIDOrDie(env, gDisplayViewportClassInfo.clazz,
"physicalPort", "Ljava/lang/Integer;");
gDisplayViewportClassInfo.type = GetFieldIDOrDie(env,
gDisplayViewportClassInfo.clazz, "type", "I");

View File

@@ -987,7 +987,7 @@ final class LocalDisplayAdapter extends DisplayAdapter {
int[] ports = res.getIntArray(
com.android.internal.R.array.config_localPrivateDisplayPorts);
if (ports != null) {
int port = Byte.toUnsignedInt(physicalAddress.getPort());
int port = physicalAddress.getPort();
for (int p : ports) {
if (p == port) {
return true;

View File

@@ -642,8 +642,7 @@ class DisplayWindowSettings {
if (mIdentifier == IDENTIFIER_PORT && displayInfo.address != null) {
// Config suggests using port as identifier for physical displays.
if (displayInfo.address instanceof DisplayAddress.Physical) {
byte port = ((DisplayAddress.Physical) displayInfo.address).getPort();
return "port:" + Byte.toUnsignedInt(port);
return "port:" + ((DisplayAddress.Physical) displayInfo.address).getPort();
}
}
return displayInfo.uniqueId;

View File

@@ -161,7 +161,7 @@ public class LocalDisplayAdapterTest {
DisplayDeviceInfo info, int expectedPort, boolean shouldBePrivate) {
final DisplayAddress.Physical address = (DisplayAddress.Physical) info.address;
assertNotNull(address);
assertEquals((byte) expectedPort, address.getPort());
assertEquals(expectedPort, address.getPort());
assertEquals(DISPLAY_MODEL, address.getModel());
assertEquals(shouldBePrivate, (info.flags & DisplayDeviceInfo.FLAG_PRIVATE) != 0);
}
@@ -254,7 +254,7 @@ public class LocalDisplayAdapterTest {
int expectedDensityDpi) {
final DisplayAddress.Physical physical = (DisplayAddress.Physical) info.address;
assertNotNull(physical);
assertEquals((byte) expectedPort, physical.getPort());
assertEquals(expectedPort, physical.getPort());
assertEquals(expectedXdpi, info.xDpi, 0.01);
assertEquals(expectedYDpi, info.yDpi, 0.01);
assertEquals(expectedDensityDpi, info.densityDpi);
@@ -323,7 +323,7 @@ public class LocalDisplayAdapterTest {
}
private static DisplayAddress.Physical createDisplayAddress(int port) {
return DisplayAddress.fromPortAndModel((byte) port, DISPLAY_MODEL);
return DisplayAddress.fromPortAndModel(port, DISPLAY_MODEL);
}
private static SurfaceControl.DisplayInfo createFakeDisplayInfo() {

View File

@@ -80,7 +80,7 @@ import java.nio.charset.StandardCharsets;
@RunWith(WindowTestRunner.class)
public class DisplayWindowSettingsTests extends WindowTestsBase {
private static final byte DISPLAY_PORT = (byte) 0xFF;
private static final int DISPLAY_PORT = 0xFF;
private static final long DISPLAY_MODEL = 0xEEEEEEEEL;
private static final File TEST_FOLDER = getInstrumentation().getTargetContext().getCacheDir();
@@ -486,7 +486,7 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {
DisplayAddress.fromPortAndModel(DISPLAY_PORT, DISPLAY_MODEL);
mPrimaryDisplay.getDisplayInfo().address = displayAddress;
final String displayIdentifier = "port:" + Byte.toUnsignedInt(DISPLAY_PORT);
final String displayIdentifier = "port:" + DISPLAY_PORT;
prepareDisplaySettings(displayIdentifier, true /* usePortAsId */);
readAndAssertDisplaySettings(mPrimaryDisplay);
@@ -537,7 +537,7 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {
assertTrue(mStorage.wasWriteSuccessful());
// Verify that settings were stored correctly.
assertEquals("Attribute value must be stored", "port:" + Byte.toUnsignedInt(DISPLAY_PORT),
assertEquals("Attribute value must be stored", "port:" + DISPLAY_PORT,
getStoredDisplayAttributeValue("name"));
assertEquals("Attribute value must be stored", "true",
getStoredDisplayAttributeValue("shouldShowSystemDecors"));