Loading libs/WindowManager/Jetpack/src/androidx/window/extensions/layout/WindowLayoutComponentImpl.java +21 −15 Original line number Diff line number Diff line Loading @@ -169,21 +169,7 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { || containsConsumer(consumer)) { return; } final IllegalArgumentException exception = new IllegalArgumentException( "Context must be a UI Context with display association, which should be" + " an Activity, WindowContext or InputMethodService"); if (!context.isUiContext()) { throw exception; } if (context.getAssociatedDisplayId() == INVALID_DISPLAY) { // This is to identify if #isUiContext of a non-UI Context is overridden. // #isUiContext is more likely to be overridden than #getAssociatedDisplayId // since #isUiContext is a public API. StrictMode.onIncorrectContextUsed("The registered Context is a UI Context " + "but not associated with any display. " + "This Context may not receive any WindowLayoutInfo update. " + dumpAllBaseContextToString(context), exception); } assertUiContext(context); Log.d(TAG, "Register WindowLayoutInfoListener on " + dumpAllBaseContextToString(context)); mFoldingFeatureProducer.getData((features) -> { Loading Loading @@ -363,6 +349,7 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { @Override @NonNull public WindowLayoutInfo getCurrentWindowLayoutInfo(@NonNull @UiContext Context context) { assertUiContext(context); synchronized (mLock) { return getWindowLayoutInfo(context, mLastReportedFoldingFeatures); } Loading @@ -377,6 +364,25 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { return mSupportedWindowFeatures; } private void assertUiContext(@NonNull Context context) { final IllegalArgumentException exception = new IllegalArgumentException( "Context must be a UI Context with display association, which should be " + "an Activity, WindowContext or InputMethodService"); if (!context.isUiContext()) { throw exception; } if (context.getAssociatedDisplayId() == INVALID_DISPLAY) { // This is to identify if #isUiContext of a non-UI Context is overridden. // #isUiContext is more likely to be overridden than #getAssociatedDisplayId // since #isUiContext is a public API. StrictMode.onIncorrectContextUsed("The given context is a UI context, " + "but it is not associated with any display. " + "This context may not receive WindowLayoutInfo updates and " + "may get an empty WindowLayoutInfo return value. " + dumpAllBaseContextToString(context), exception); } } /** @see #getWindowLayoutInfo(Context, List) */ private WindowLayoutInfo getWindowLayoutInfo(int displayId, @NonNull WindowConfiguration windowConfiguration, Loading libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/extensions/layout/WindowLayoutComponentImplTest.java +8 −6 Original line number Diff line number Diff line Loading @@ -74,6 +74,11 @@ public class WindowLayoutComponentImplTest { mWindowLayoutComponent.onDisplayFeaturesChanged(Collections.emptyList()); } @Test(expected = IllegalArgumentException.class) public void testAddWindowLayoutListener_nonUiContext_throwsError() { mWindowLayoutComponent.addWindowLayoutInfoListener(mAppContext, info -> {}); } @Test public void testGetCurrentWindowLayoutInfo_noFoldingFeature_returnsEmptyList() { final Context testUiContext = new TestUiContext(mAppContext); Loading Loading @@ -127,12 +132,9 @@ public class WindowLayoutComponentImplTest { featureRect, FoldingFeature.TYPE_HINGE, FoldingFeature.STATE_FLAT)); } @Test public void testGetCurrentWindowLayoutInfo_nonUiContext_returnsEmptyList() { final WindowLayoutInfo layoutInfo = @Test(expected = IllegalArgumentException.class) public void testGetCurrentWindowLayoutInfo_nonUiContext_throwsError() { mWindowLayoutComponent.getCurrentWindowLayoutInfo(mAppContext); assertThat(layoutInfo.getDisplayFeatures()).isEmpty(); } /** Loading Loading
libs/WindowManager/Jetpack/src/androidx/window/extensions/layout/WindowLayoutComponentImpl.java +21 −15 Original line number Diff line number Diff line Loading @@ -169,21 +169,7 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { || containsConsumer(consumer)) { return; } final IllegalArgumentException exception = new IllegalArgumentException( "Context must be a UI Context with display association, which should be" + " an Activity, WindowContext or InputMethodService"); if (!context.isUiContext()) { throw exception; } if (context.getAssociatedDisplayId() == INVALID_DISPLAY) { // This is to identify if #isUiContext of a non-UI Context is overridden. // #isUiContext is more likely to be overridden than #getAssociatedDisplayId // since #isUiContext is a public API. StrictMode.onIncorrectContextUsed("The registered Context is a UI Context " + "but not associated with any display. " + "This Context may not receive any WindowLayoutInfo update. " + dumpAllBaseContextToString(context), exception); } assertUiContext(context); Log.d(TAG, "Register WindowLayoutInfoListener on " + dumpAllBaseContextToString(context)); mFoldingFeatureProducer.getData((features) -> { Loading Loading @@ -363,6 +349,7 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { @Override @NonNull public WindowLayoutInfo getCurrentWindowLayoutInfo(@NonNull @UiContext Context context) { assertUiContext(context); synchronized (mLock) { return getWindowLayoutInfo(context, mLastReportedFoldingFeatures); } Loading @@ -377,6 +364,25 @@ public class WindowLayoutComponentImpl implements WindowLayoutComponent { return mSupportedWindowFeatures; } private void assertUiContext(@NonNull Context context) { final IllegalArgumentException exception = new IllegalArgumentException( "Context must be a UI Context with display association, which should be " + "an Activity, WindowContext or InputMethodService"); if (!context.isUiContext()) { throw exception; } if (context.getAssociatedDisplayId() == INVALID_DISPLAY) { // This is to identify if #isUiContext of a non-UI Context is overridden. // #isUiContext is more likely to be overridden than #getAssociatedDisplayId // since #isUiContext is a public API. StrictMode.onIncorrectContextUsed("The given context is a UI context, " + "but it is not associated with any display. " + "This context may not receive WindowLayoutInfo updates and " + "may get an empty WindowLayoutInfo return value. " + dumpAllBaseContextToString(context), exception); } } /** @see #getWindowLayoutInfo(Context, List) */ private WindowLayoutInfo getWindowLayoutInfo(int displayId, @NonNull WindowConfiguration windowConfiguration, Loading
libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/extensions/layout/WindowLayoutComponentImplTest.java +8 −6 Original line number Diff line number Diff line Loading @@ -74,6 +74,11 @@ public class WindowLayoutComponentImplTest { mWindowLayoutComponent.onDisplayFeaturesChanged(Collections.emptyList()); } @Test(expected = IllegalArgumentException.class) public void testAddWindowLayoutListener_nonUiContext_throwsError() { mWindowLayoutComponent.addWindowLayoutInfoListener(mAppContext, info -> {}); } @Test public void testGetCurrentWindowLayoutInfo_noFoldingFeature_returnsEmptyList() { final Context testUiContext = new TestUiContext(mAppContext); Loading Loading @@ -127,12 +132,9 @@ public class WindowLayoutComponentImplTest { featureRect, FoldingFeature.TYPE_HINGE, FoldingFeature.STATE_FLAT)); } @Test public void testGetCurrentWindowLayoutInfo_nonUiContext_returnsEmptyList() { final WindowLayoutInfo layoutInfo = @Test(expected = IllegalArgumentException.class) public void testGetCurrentWindowLayoutInfo_nonUiContext_throwsError() { mWindowLayoutComponent.getCurrentWindowLayoutInfo(mAppContext); assertThat(layoutInfo.getDisplayFeatures()).isEmpty(); } /** Loading