diff options
| author | 2021-11-05 01:42:40 +0000 | |
|---|---|---|
| committer | 2021-11-05 01:42:40 +0000 | |
| commit | 7e37ab845dd11f6d95b8506d7f168e18de287ce6 (patch) | |
| tree | 5700bd1dcae1296fef29482744973d07313460a6 | |
| parent | 7656c0f3a4622f05bb853ebac1c8bb93db8d3e55 (diff) | |
| parent | dda327c243fd8864ec30626b6f19c2baf6f86458 (diff) | |
Merge "Add support for parsing states." into sc-v2-dev
4 files changed, 90 insertions, 8 deletions
diff --git a/libs/WindowManager/Jetpack/src/androidx/window/common/CommonDisplayFeature.java b/libs/WindowManager/Jetpack/src/androidx/window/common/CommonDisplayFeature.java index e6ad011e617e..eb9429747b66 100644 --- a/libs/WindowManager/Jetpack/src/androidx/window/common/CommonDisplayFeature.java +++ b/libs/WindowManager/Jetpack/src/androidx/window/common/CommonDisplayFeature.java @@ -30,18 +30,21 @@ import java.util.regex.Pattern; /** Wrapper for both Extension and Sidecar versions of DisplayFeature. */ final class CommonDisplayFeature implements DisplayFeature { private static final Pattern FEATURE_PATTERN = - Pattern.compile("([a-z]+)-\\[(\\d+),(\\d+),(\\d+),(\\d+)]"); + Pattern.compile("([a-z]+)-\\[(\\d+),(\\d+),(\\d+),(\\d+)]-?(flat|half-opened)?"); private static final String FEATURE_TYPE_FOLD = "fold"; private static final String FEATURE_TYPE_HINGE = "hinge"; + private static final String PATTERN_STATE_FLAT = "flat"; + private static final String PATTERN_STATE_HALF_OPENED = "half-opened"; + // TODO(b/183049815): Support feature strings that include the state of the feature. + /** * Parses a display feature from a string. * * @throws IllegalArgumentException if the provided string is improperly formatted or could not - * otherwise be parsed. - * + * otherwise be parsed. * @see #FEATURE_PATTERN */ @NonNull @@ -52,6 +55,7 @@ final class CommonDisplayFeature implements DisplayFeature { } try { String featureType = featureMatcher.group(1); + featureType = featureType == null ? "" : featureType; int type; switch (featureType) { case FEATURE_TYPE_FOLD: @@ -73,8 +77,21 @@ final class CommonDisplayFeature implements DisplayFeature { if (isZero(featureRect)) { throw new IllegalArgumentException("Feature has empty bounds: " + string); } - - return new CommonDisplayFeature(type, null, featureRect); + String stateString = featureMatcher.group(6); + stateString = stateString == null ? "" : stateString; + Integer state; + switch (stateString) { + case PATTERN_STATE_FLAT: + state = COMMON_STATE_FLAT; + break; + case PATTERN_STATE_HALF_OPENED: + state = COMMON_STATE_HALF_OPENED; + break; + default: + state = null; + break; + } + return new CommonDisplayFeature(type, state, featureRect); } catch (NumberFormatException e) { throw new IllegalArgumentException("Malformed feature description: " + string, e); } @@ -87,6 +104,7 @@ final class CommonDisplayFeature implements DisplayFeature { private final Rect mRect; CommonDisplayFeature(int type, @Nullable Integer state, @NonNull Rect rect) { + assertValidState(state); this.mType = type; this.mState = state; if (rect.width() == 0 && rect.height() == 0) { @@ -125,4 +143,11 @@ final class CommonDisplayFeature implements DisplayFeature { public int hashCode() { return Objects.hash(mType, mState, mRect); } + + private static void assertValidState(@Nullable Integer state) { + if (state != null && state != COMMON_STATE_FLAT && state != COMMON_STATE_HALF_OPENED) { + throw new IllegalArgumentException("Invalid state: " + state + + "must be either COMMON_STATE_FLAT or COMMON_STATE_HALF_OPENED"); + } + } } diff --git a/libs/WindowManager/Jetpack/src/androidx/window/common/DisplayFeature.java b/libs/WindowManager/Jetpack/src/androidx/window/common/DisplayFeature.java index b6c4c436d0b1..573641857b99 100644 --- a/libs/WindowManager/Jetpack/src/androidx/window/common/DisplayFeature.java +++ b/libs/WindowManager/Jetpack/src/androidx/window/common/DisplayFeature.java @@ -16,11 +16,15 @@ package androidx.window.common; +import android.annotation.IntDef; import android.annotation.Nullable; import android.graphics.Rect; import androidx.annotation.NonNull; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + /** Wrapper for both Extension and Sidecar versions of DisplayFeature. */ public interface DisplayFeature { /** Returns the type of the feature. */ @@ -28,9 +32,29 @@ public interface DisplayFeature { /** Returns the state of the feature, or {@code null} if the feature has no state. */ @Nullable + @State Integer getState(); /** Returns the bounds of the feature. */ @NonNull Rect getRect(); + + /** + * A common state to represent a FLAT hinge. This is needed because the definitions in Sidecar + * and Extensions do not match exactly. + */ + int COMMON_STATE_FLAT = 3; + /** + * A common state to represent a HALF_OPENED hinge. This is needed because the definitions in + * Sidecar and Extensions do not match exactly. + */ + int COMMON_STATE_HALF_OPENED = 2; + + /** + * The possible states for a folding hinge. + */ + @IntDef({COMMON_STATE_FLAT, COMMON_STATE_HALF_OPENED}) + @Retention(RetentionPolicy.SOURCE) + @interface State {} + } diff --git a/libs/WindowManager/Jetpack/src/androidx/window/extensions/layout/WindowLayoutComponentImpl.java b/libs/WindowManager/Jetpack/src/androidx/window/extensions/layout/WindowLayoutComponentImpl.java index 383d91da6af8..32d447ef1586 100644 --- a/libs/WindowManager/Jetpack/src/androidx/window/extensions/layout/WindowLayoutComponentImpl.java +++ b/libs/WindowManager/Jetpack/src/androidx/window/extensions/layout/WindowLayoutComponentImpl.java @@ -122,8 +122,19 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { private int getFeatureState(DisplayFeature feature) { Integer featureState = feature.getState(); Optional<Integer> posture = mDevicePostureProducer.getData(); - int fallbackPosture = posture.orElse(FoldingFeature.STATE_FLAT); - return featureState == null ? fallbackPosture : featureState; + int fallbackPosture = posture.orElse(DisplayFeature.COMMON_STATE_FLAT); + int displayFeatureState = featureState == null ? fallbackPosture : featureState; + return convertToExtensionState(displayFeatureState); + } + + private int convertToExtensionState(int state) { + switch (state) { + case DisplayFeature.COMMON_STATE_FLAT: + return FoldingFeature.STATE_FLAT; + case DisplayFeature.COMMON_STATE_HALF_OPENED: + return FoldingFeature.STATE_HALF_OPENED; + } + return FoldingFeature.STATE_FLAT; } private void onDisplayFeaturesChanged() { diff --git a/libs/WindowManager/Jetpack/src/androidx/window/sidecar/SampleSidecarImpl.java b/libs/WindowManager/Jetpack/src/androidx/window/sidecar/SampleSidecarImpl.java index ece198cad818..aa949f126154 100644 --- a/libs/WindowManager/Jetpack/src/androidx/window/sidecar/SampleSidecarImpl.java +++ b/libs/WindowManager/Jetpack/src/androidx/window/sidecar/SampleSidecarImpl.java @@ -38,6 +38,7 @@ import androidx.window.util.DataProducer; import androidx.window.util.PriorityDataProducer; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -47,6 +48,7 @@ import java.util.Optional; */ class SampleSidecarImpl extends StubSidecar { private static final String TAG = "SampleSidecar"; + private static final boolean DEBUG = false; private final SettingsDevicePostureProducer mSettingsDevicePostureProducer; private final DataProducer<Integer> mDevicePostureProducer; @@ -88,10 +90,30 @@ class SampleSidecarImpl extends StubSidecar { Optional<Integer> posture = mDevicePostureProducer.getData(); SidecarDeviceState deviceState = new SidecarDeviceState(); - deviceState.posture = posture.orElse(SidecarDeviceState.POSTURE_UNKNOWN); + deviceState.posture = posture.orElse(deviceStateFromFeature()); return deviceState; } + private int deviceStateFromFeature() { + List<DisplayFeature> storedFeatures = mDisplayFeatureProducer.getData() + .orElse(Collections.emptyList()); + for (int i = 0; i < storedFeatures.size(); i++) { + DisplayFeature feature = storedFeatures.get(i); + final int state = feature.getState() == null ? -1 : feature.getState(); + if (DEBUG && feature.getState() == null) { + Log.d(TAG, "feature#getState was null for DisplayFeature: " + feature); + } + + switch (state) { + case DisplayFeature.COMMON_STATE_FLAT: + return SidecarDeviceState.POSTURE_OPENED; + case DisplayFeature.COMMON_STATE_HALF_OPENED: + return SidecarDeviceState.POSTURE_HALF_OPENED; + } + } + return SidecarDeviceState.POSTURE_UNKNOWN; + } + @NonNull @Override public SidecarWindowLayoutInfo getWindowLayoutInfo(@NonNull IBinder windowToken) { |