Skip to content

Commit

Permalink
Add inset consumption and animation listeners to InsetObserverView
Browse files Browse the repository at this point in the history
This lets us hang DeferredIMEWindowInsetApplicationCallback's listeners off a
single, already-established location instead of using the decor view
directly. It also enables other components to consume insets out of band
and listen for inset animation progress.

Change-Id: I905657fbbcb8f43ecfb655b089ad11812166fb00
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4317263
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com>
Commit-Queue: Patrick Noland <pnoland@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1115945}
  • Loading branch information
Patrick Noland authored and Chromium LUCI CQ committed Mar 10, 2023
1 parent 3427f2f commit 6da61cd
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.base.supplier.ObservableSupplierImpl;
import org.chromium.base.supplier.OneshotSupplierImpl;
import org.chromium.base.supplier.UnownedUserDataSupplier;
import org.chromium.blink.mojom.DisplayMode;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.IntentHandler;
Expand Down Expand Up @@ -67,6 +68,8 @@
import org.chromium.components.browser_ui.modaldialog.AppModalPresenter;
import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.components.browser_ui.util.BrowserControlsVisibilityDelegate;
import org.chromium.components.browser_ui.widget.InsetObserverView;
import org.chromium.components.browser_ui.widget.InsetObserverViewSupplier;
import org.chromium.components.external_intents.ExternalNavigationHandler;
import org.chromium.components.url_formatter.UrlFormatter;
import org.chromium.content_public.browser.LoadUrlParams;
Expand Down Expand Up @@ -147,6 +150,8 @@ boolean isActivityDisabledForTests() {
private SearchBoxDataProvider mSearchBoxDataProvider;
private Tab mTab;
private ObservableSupplierImpl<Profile> mProfileSupplier = new ObservableSupplierImpl<>();
protected final UnownedUserDataSupplier<InsetObserverView> mInsetObserverViewSupplier =
new InsetObserverViewSupplier();

@Override
protected boolean isStartedUpCorrectly(Intent intent) {
Expand Down Expand Up @@ -184,6 +189,17 @@ protected void triggerLayoutInflation() {
mSearchBoxDataProvider = new SearchBoxDataProvider(this);
mSearchBoxDataProvider.setIsFromQuickActionSearchWidget(isFromQuickActionSearchWidget());

ViewGroup rootView = (ViewGroup) getWindow().getDecorView().getRootView();
// Setting fitsSystemWindows to false ensures that the root view doesn't consume the
// insets.
rootView.setFitsSystemWindows(false);
// Add a custom view right after the root view that stores the insets to access later.
// WebContents needs the insets to determine the portion of the screen obscured by
// non-content displaying things such as the OSK.
mInsetObserverViewSupplier.attach(getWindowAndroid().getUnownedUserDataHost());
mInsetObserverViewSupplier.set(InsetObserverView.create(this));
rootView.addView(mInsetObserverViewSupplier.get(), 0);

mContentView = createContentView();
setContentView(mContentView);

Expand Down Expand Up @@ -246,6 +262,7 @@ mSearchBoxDataProvider, null, new WindowDelegate(getWindow()), getWindowAndroid(
if (!getActivityDelegate().shouldDelayNativeInitialization()) {
mHandler.post(this::startDelayedNativeInitialization);
}

onInitialLayoutInflationComplete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
package org.chromium.chrome.browser.omnibox;

import android.view.View;
import android.view.WindowInsets;

import androidx.annotation.NonNull;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsAnimationCompat;
import androidx.core.view.WindowInsetsAnimationCompat.BoundsCompat;
import androidx.core.view.WindowInsetsCompat;

import org.chromium.components.browser_ui.widget.InsetObserverView;
import org.chromium.components.browser_ui.widget.InsetObserverView.WindowInsetsAnimationListener;
import org.chromium.components.browser_ui.widget.InsetObserverView.WindowInsetsConsumer;
import org.chromium.components.browser_ui.widget.InsetObserverViewSupplier;
import org.chromium.ui.base.WindowAndroid;

import java.util.List;
Expand All @@ -26,21 +28,20 @@
* an animation is known to be running.
*/
class DeferredIMEWindowInsetApplicationCallback
extends WindowInsetsAnimationCompat.Callback implements OnApplyWindowInsetsListener {
implements WindowInsetsConsumer, WindowInsetsAnimationListener {
private static final int NO_DEFERRED_KEYBOARD_HEIGHT = -1;
private int mDeferredKeyboardHeight = NO_DEFERRED_KEYBOARD_HEIGHT;
private int mKeyboardHeight;
private boolean mAnimationInProgress;
private WindowInsetsAnimationCompat mCurrentAnimation;
private View mView;
private InsetObserverView mInsetObserverView;
private final Runnable mOnUpdateCallback;

/**
* Constructs a new DeferredIMEWindowInsetApplicationCallback.
* @param onUpdateCallback Callback to be invoked when the keyboard height changes.
*/
public DeferredIMEWindowInsetApplicationCallback(@NonNull Runnable onUpdateCallback) {
super(WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE);
mOnUpdateCallback = onUpdateCallback;
}

Expand All @@ -49,19 +50,25 @@ public DeferredIMEWindowInsetApplicationCallback(@NonNull Runnable onUpdateCallb
* IME window insets and listening for IME animation updates.
*/
public void attach(WindowAndroid windowAndroid) {
mView = windowAndroid.getActivity().get().getWindow().getDecorView();
ViewCompat.setWindowInsetsAnimationCallback(mView, this);
ViewCompat.setOnApplyWindowInsetsListener(mView, this);
InsetObserverView insetObserverView =
InsetObserverViewSupplier.getValueOrNullFrom(windowAndroid);
assert insetObserverView
!= null
: "DeferredIMEWindowInsetApplicationCallback can only be used in activities with an"
+ " InsetObserverView";
mInsetObserverView = insetObserverView;
insetObserverView.addInsetsConsumer(this);
insetObserverView.addWindowInsetsAnimationListener(this);
}

/** Detaches this callback from the root of the given window. */
public void detach() {
ViewCompat.setWindowInsetsAnimationCallback(mView, null);
ViewCompat.setOnApplyWindowInsetsListener(mView, null);
mView = null;
mInsetObserverView.removeInsetsConsumer(this);
mInsetObserverView.removeWindowInsetsAnimationListener(this);
mAnimationInProgress = false;
mDeferredKeyboardHeight = NO_DEFERRED_KEYBOARD_HEIGHT;
mKeyboardHeight = 0;
mInsetObserverView = null;
}

public int getCurrentKeyboardHeight() {
Expand All @@ -78,10 +85,13 @@ public void onPrepare(@NonNull WindowInsetsAnimationCompat animation) {

@NonNull
@Override
public WindowInsetsCompat onProgress(@NonNull WindowInsetsCompat windowInsetsCompat,
@NonNull List<WindowInsetsAnimationCompat> list) {
return windowInsetsCompat;
}
public void onStart(
@NonNull WindowInsetsAnimationCompat animation, @NonNull BoundsCompat bounds) {}

@NonNull
@Override
public void onProgress(@NonNull WindowInsetsCompat windowInsetsCompat,
@NonNull List<WindowInsetsAnimationCompat> list) {}

@Override
public void onEnd(@NonNull WindowInsetsAnimationCompat animation) {
Expand Down Expand Up @@ -117,11 +127,9 @@ public WindowInsetsCompat onApplyWindowInsets(

// Zero out (consume) the ime insets; we're applying them ourselves so no one else needs
// to consume them.
WindowInsets windowInsets = new WindowInsetsCompat.Builder(windowInsetsCompat)
.setInsets(WindowInsetsCompat.Type.ime(), Insets.NONE)
.build()
.toWindowInsets();
return WindowInsetsCompat.toWindowInsetsCompat(view.onApplyWindowInsets(windowInsets));
return new WindowInsetsCompat.Builder(windowInsetsCompat)
.setInsets(WindowInsetsCompat.Type.ime(), Insets.NONE)
.build();
}

private void commitKeyboardHeight(int newKeyboardHeight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@

import static junit.framework.Assert.assertEquals;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.app.Activity;
import android.view.View;
import android.view.Window;

import androidx.core.graphics.Insets;
import androidx.core.view.WindowInsetsAnimationCompat;
Expand All @@ -30,10 +25,10 @@
import org.mockito.junit.MockitoRule;

import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.components.browser_ui.widget.InsetObserverView;
import org.chromium.components.browser_ui.widget.InsetObserverViewSupplier;
import org.chromium.ui.base.WindowAndroid;

import java.lang.ref.WeakReference;

/**
* Unit tests for {@link DeferredIMEWindowInsetApplicationCallback}.
*/
Expand All @@ -45,7 +40,6 @@ public class DeferredIMEWindowInsetApplicationCallbackTest {
public @Rule MockitoRule mMockitoRule = MockitoJUnit.rule();

private DeferredIMEWindowInsetApplicationCallback mCallback;
private WeakReference<Activity> mActivityRef;
private WindowInsetsCompat.Builder mBaseWindowInsets;
private WindowInsetsAnimationCompat mAnimation;
private WindowInsetsAnimationCompat mAnimation2;
Expand All @@ -55,28 +49,20 @@ public class DeferredIMEWindowInsetApplicationCallbackTest {
@Mock
private WindowAndroid mWindowAndroid;
@Mock
private Activity mActivity;
@Mock
private Window mWindow;
private View mView;
@Mock
private View mDecorView;
InsetObserverView mInsetObserverView;

@Before
public void setUp() {
mActivityRef = new WeakReference<>(mActivity);
doReturn(mActivityRef).when(mWindowAndroid).getActivity();
doReturn(mWindow).when(mActivity).getWindow();
doReturn(mDecorView).when(mWindow).getDecorView();
InsetObserverViewSupplier.setInstanceForTesting(mInsetObserverView);
mAnimation = new WindowInsetsAnimationCompat(WindowInsetsCompat.Type.ime(), null, 160);
mAnimation2 = new WindowInsetsAnimationCompat(WindowInsetsCompat.Type.ime(), null, 160);
mCallback = new DeferredIMEWindowInsetApplicationCallback(mUpdateRunnable);
mBaseWindowInsets =
new WindowInsetsCompat.Builder()
.setInsets(WindowInsetsCompat.Type.statusBars(), STATUS_BAR_INSETS)
.setInsets(WindowInsetsCompat.Type.navigationBars(), NAV_BAR_INSETS);
doAnswer(invocation -> invocation.getArgument(0))
.when(mDecorView)
.onApplyWindowInsets(any());
}

@Test
Expand All @@ -87,9 +73,9 @@ public void testAnimatedkeyboardShowHide() {
WindowInsetsCompat windowInsets =
mBaseWindowInsets.setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, 384))
.build();
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mDecorView, windowInsets);
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mView, windowInsets);

assertEquals(NAV_BAR_INSETS, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
assertEquals(Insets.NONE, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
verify(mUpdateRunnable, never()).run();

mCallback.onEnd(mAnimation);
Expand All @@ -99,7 +85,7 @@ public void testAnimatedkeyboardShowHide() {
mCallback.onPrepare(mAnimation);
windowInsets =
mBaseWindowInsets.setInsets(WindowInsetsCompat.Type.ime(), Insets.NONE).build();
mCallback.onApplyWindowInsets(mDecorView, windowInsets);
mCallback.onApplyWindowInsets(mView, windowInsets);
verify(mUpdateRunnable, times(2)).run();
assertEquals(0, mCallback.getCurrentKeyboardHeight());

Expand All @@ -116,7 +102,7 @@ public void testAnimatedkeyboardShow_noNavBar() {
mBaseWindowInsets.setInsets(WindowInsetsCompat.Type.navigationBars(), Insets.NONE)
.setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, 384))
.build();
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mDecorView, windowInsets);
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mView, windowInsets);

assertEquals(Insets.NONE, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
verify(mUpdateRunnable, never()).run();
Expand All @@ -127,18 +113,22 @@ public void testUnanimatedChange_appliedImmediately() {
WindowInsetsCompat windowInsets =
mBaseWindowInsets.setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, 384))
.build();
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mDecorView, windowInsets);
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mView, windowInsets);

assertEquals(NAV_BAR_INSETS, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
assertEquals(Insets.NONE, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
verify(mUpdateRunnable).run();
assertEquals(300, mCallback.getCurrentKeyboardHeight());
}

@Test
public void testAttachDetach() {
mCallback.attach(mWindowAndroid);
verify(mInsetObserverView).addWindowInsetsAnimationListener(mCallback);
verify(mInsetObserverView).addInsetsConsumer(mCallback);

mCallback.detach();
verify(mDecorView, times(2)).setOnApplyWindowInsetsListener(null);
verify(mInsetObserverView).removeWindowInsetsAnimationListener(mCallback);
verify(mInsetObserverView).removeInsetsConsumer(mCallback);
}

@Test
Expand All @@ -149,15 +139,15 @@ public void testConcurrentAnimations_respectFinalOnly() {
WindowInsetsCompat windowInsets =
mBaseWindowInsets.setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, 384))
.build();
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mDecorView, windowInsets);
WindowInsetsCompat modifiedInsets = mCallback.onApplyWindowInsets(mView, windowInsets);

assertEquals(NAV_BAR_INSETS, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
assertEquals(Insets.NONE, modifiedInsets.getInsets(WindowInsetsCompat.Type.ime()));
verify(mUpdateRunnable, never()).run();

mCallback.onPrepare(mAnimation2);
windowInsets =
mBaseWindowInsets.setInsets(WindowInsetsCompat.Type.ime(), Insets.NONE).build();
mCallback.onApplyWindowInsets(mDecorView, windowInsets);
mCallback.onApplyWindowInsets(mView, windowInsets);
// The hide cancelled out the show, so there's no effective update.
verify(mUpdateRunnable, never()).run();
assertEquals(0, mCallback.getCurrentKeyboardHeight());
Expand Down
1 change: 1 addition & 0 deletions components/browser_ui/widget/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ robolectric_library("junit") {
"//base/test:test_support_java",
"//components/browser_ui/test/android:test_support_java",
"//third_party/androidx:androidx_annotation_annotation_java",
"//third_party/androidx:androidx_core_core_java",
"//third_party/androidx:androidx_test_core_java",
"//third_party/androidx:androidx_test_runner_java",
"//third_party/junit",
Expand Down

0 comments on commit 6da61cd

Please sign in to comment.