Merge "Adds getRearDisplayMetrics method" into udc-dev

This commit is contained in:
Anvesh Renikindi
2023-05-12 16:36:54 +00:00
committed by Android (Google) Code Review

View File

@@ -31,12 +31,14 @@ import android.view.DisplayAddress;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.window.extensions.WindowExtensions;
import androidx.window.extensions.core.util.function.Consumer; import androidx.window.extensions.core.util.function.Consumer;
import com.android.internal.R; import com.android.internal.R;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
/** /**
@@ -51,6 +53,7 @@ import java.util.concurrent.Executor;
public class WindowAreaComponentImpl implements WindowAreaComponent, public class WindowAreaComponentImpl implements WindowAreaComponent,
DeviceStateManager.DeviceStateCallback { DeviceStateManager.DeviceStateCallback {
private static final int INVALID_DISPLAY_ADDRESS = -1;
private final Object mLock = new Object(); private final Object mLock = new Object();
@NonNull @NonNull
@@ -69,8 +72,7 @@ public class WindowAreaComponentImpl implements WindowAreaComponent,
private final int mConcurrentDisplayState; private final int mConcurrentDisplayState;
@NonNull @NonNull
private final int[] mFoldedDeviceStates; private final int[] mFoldedDeviceStates;
@NonNull private long mRearDisplayAddress = INVALID_DISPLAY_ADDRESS;
private long mRearDisplayAddress = 0;
@WindowAreaSessionState @WindowAreaSessionState
private int mRearDisplaySessionStatus = WindowAreaComponent.SESSION_STATE_INACTIVE; private int mRearDisplaySessionStatus = WindowAreaComponent.SESSION_STATE_INACTIVE;
@@ -109,10 +111,7 @@ public class WindowAreaComponentImpl implements WindowAreaComponent,
R.integer.config_deviceStateConcurrentRearDisplay); R.integer.config_deviceStateConcurrentRearDisplay);
mDeviceStateManager.registerCallback(mExecutor, this); mDeviceStateManager.registerCallback(mExecutor, this);
if (mConcurrentDisplayState != INVALID_DEVICE_STATE) { mRearDisplayAddress = getRearDisplayAddress(context);
mRearDisplayAddress = Long.parseLong(context.getResources().getString(
R.string.config_rearDisplayPhysicalAddress));
}
} }
/** /**
@@ -219,6 +218,44 @@ public class WindowAreaComponentImpl implements WindowAreaComponent,
} }
} }
/**
* Returns the{@link DisplayMetrics} associated with the rear facing display. If the rear facing
* display was not found in the display list, but we have already computed the
* {@link DisplayMetrics} for that display, we return the cached value. If no display has been
* found, then we return an empty {@link DisplayMetrics} value.
*
* TODO(b/267563768): Update with guidance from Display team for missing displays.
*
* @since {@link WindowExtensions#VENDOR_API_LEVEL_3}
*/
@Override
public DisplayMetrics getRearDisplayMetrics() {
DisplayMetrics metrics = null;
// DISPLAY_CATEGORY_REAR displays are only available when you are in the concurrent
// display state, so we have to look through all displays to match the address
Display[] displays = mDisplayManager.getDisplays(
DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED);
for (int i = 0; i < displays.length; i++) {
DisplayAddress.Physical address =
(DisplayAddress.Physical) displays[i].getAddress();
if (mRearDisplayAddress == address.getPhysicalDisplayId()) {
metrics = new DisplayMetrics();
displays[i].getRealMetrics(metrics);
break;
}
}
synchronized (mLock) {
// Update the rear display metrics with our latest value if one was received
if (metrics != null) {
mRearDisplayMetrics = metrics;
}
return Objects.requireNonNullElseGet(mRearDisplayMetrics, DisplayMetrics::new);
}
}
/** /**
* Adds a listener interested in receiving updates on the RearDisplayPresentationStatus * Adds a listener interested in receiving updates on the RearDisplayPresentationStatus
* of the device. Because this is being called from the OEM provided * of the device. Because this is being called from the OEM provided
@@ -260,8 +297,8 @@ public class WindowAreaComponentImpl implements WindowAreaComponent,
return; return;
} }
@WindowAreaStatus int currentStatus = getCurrentRearDisplayPresentationModeStatus(); @WindowAreaStatus int currentStatus = getCurrentRearDisplayPresentationModeStatus();
DisplayMetrics metrics = DisplayMetrics metrics = currentStatus == STATUS_UNSUPPORTED ? new DisplayMetrics()
currentStatus == STATUS_UNSUPPORTED ? null : getRearDisplayMetrics(); : getRearDisplayMetrics();
consumer.accept( consumer.accept(
new RearDisplayPresentationStatus(currentStatus, metrics)); new RearDisplayPresentationStatus(currentStatus, metrics));
} }
@@ -491,37 +528,10 @@ public class WindowAreaComponentImpl implements WindowAreaComponent,
} }
} }
/** private long getRearDisplayAddress(Context context) {
* Returns the{@link DisplayMetrics} associated with the rear facing display. If the rear facing String address = context.getResources().getString(
* display was not found in the display list, but we have already computed the R.string.config_rearDisplayPhysicalAddress);
* {@link DisplayMetrics} for that display, we return the cached value. return address.isEmpty() ? INVALID_DISPLAY_ADDRESS : Long.parseLong(address);
*
* TODO(b/267563768): Update with guidance from Display team for missing displays.
*
* @throws IllegalArgumentException if the display is not found and there is no cached
* {@link DisplayMetrics} for this display.
*/
@GuardedBy("mLock")
private DisplayMetrics getRearDisplayMetrics() {
Display[] displays = mDisplayManager.getDisplays(
DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED);
for (int i = 0; i < displays.length; i++) {
DisplayAddress.Physical address =
(DisplayAddress.Physical) displays[i].getAddress();
if (mRearDisplayAddress == address.getPhysicalDisplayId()) {
if (mRearDisplayMetrics == null) {
mRearDisplayMetrics = new DisplayMetrics();
}
displays[i].getRealMetrics(mRearDisplayMetrics);
return mRearDisplayMetrics;
}
}
if (mRearDisplayMetrics != null) {
return mRearDisplayMetrics;
} else {
throw new IllegalArgumentException(
"No display found with the provided display address");
}
} }
@GuardedBy("mLock") @GuardedBy("mLock")