Commit f729ab15 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Android: Revise TabObserver.onActivityAttachmentChanged

TabObserver.onActivityAttachmentChanged is passed WindowAndroid object
(or null) instead of boolean |isAttached|.

Used DisplayCutoutController for an example.

Bug: 952703
Change-Id: If827a6a1df9338added95f0ffcff5400e49ddefe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2134171
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#758127}
parent 6de7d4c5
......@@ -27,6 +27,7 @@ import org.chromium.chrome.browser.ui.messages.snackbar.SnackbarManager.Snackbar
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController;
import org.chromium.content_public.browser.UiThreadTaskTraits;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
import java.util.ArrayList;
import java.util.HashSet;
......@@ -150,10 +151,11 @@ class AutofillAssistantUiController {
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
public void onActivityAttachmentChanged(
Tab tab, @Nullable WindowAndroid window) {
if (mWebContents == null) return;
if (!isAttached && tab.getWebContents() == mWebContents) {
if (window == null && tab.getWebContents() == mWebContents) {
if (!allowTabSwitching) {
safeNativeStop(DropOutReason.TAB_DETACHED);
return;
......
......@@ -25,6 +25,7 @@ import org.chromium.content_public.browser.WebContents;
import org.chromium.third_party.android.swiperefresh.SwipeRefreshLayout;
import org.chromium.ui.OverscrollAction;
import org.chromium.ui.OverscrollRefreshHandler;
import org.chromium.ui.base.WindowAndroid;
/**
* An overscroll handler implemented in terms a modified version of the Android
......@@ -97,8 +98,8 @@ public class SwipeRefreshHandler
mTab = tab;
mTabObserver = new EmptyTabObserver() {
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (!isAttached && mSwipeRefreshLayout != null) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window == null && mSwipeRefreshLayout != null) {
cancelStopRefreshingRunnable();
detachSwipeRefreshLayoutIfNecessary();
mSwipeRefreshLayout.setOnRefreshListener(null);
......
......@@ -8,6 +8,8 @@ import android.app.Activity;
import android.content.Context;
import android.view.ContextMenu;
import androidx.annotation.Nullable;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.ChromeActivity;
......@@ -25,6 +27,7 @@ import org.chromium.content_public.browser.GestureStateListener;
import org.chromium.content_public.browser.SelectionPopupController;
import org.chromium.content_public.browser.WebContents;
import org.chromium.net.NetworkChangeNotifier;
import org.chromium.ui.base.WindowAndroid;
/** Manages the activation and gesture listeners for ContextualSearch on a given tab. */
public class ContextualSearchTabHelper
......@@ -151,8 +154,8 @@ public class ContextualSearchTabHelper
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) {
updateHooksForTab(tab);
} else {
removeContextualSearchHooks(mWebContents);
......
......@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.display_cutout;
import android.app.Activity;
import android.graphics.Rect;
import android.view.WindowManager.LayoutParams;
......@@ -14,14 +15,17 @@ import org.chromium.base.UserData;
import org.chromium.base.UserDataHost;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.blink.mojom.ViewportFit;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabImpl;
import org.chromium.chrome.browser.tab.TabObserver;
import org.chromium.chrome.browser.tab.TabSelectionType;
import org.chromium.chrome.browser.tab.TabUtils;
import org.chromium.components.browser_ui.widget.InsetObserverView;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver;
import org.chromium.ui.base.WindowAndroid;
/**
* Controls the display cutout state for the tab.
......@@ -65,11 +69,11 @@ public class DisplayCutoutController implements InsetObserverView.WindowInsetObs
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
assert tab == mTab;
if (isAttached) {
maybeAddInsetObserver();
if (window != null) {
maybeAddInsetObserver(window.getActivity().get());
} else {
maybeRemoveInsetObserver();
}
......@@ -93,17 +97,17 @@ public class DisplayCutoutController implements InsetObserverView.WindowInsetObs
mTab = tab;
tab.addObserver(mTabObserver);
maybeAddInsetObserver();
maybeAddInsetObserver(TabUtils.getActivity(tab));
}
/**
* Add an observer to {@link InsetObserverView} if we have not already added
* one.
*/
private void maybeAddInsetObserver() {
if (mInsetObserverView != null || ((TabImpl) mTab).getActivity() == null) return;
private void maybeAddInsetObserver(Activity activity) {
if (mInsetObserverView != null || activity == null) return;
mInsetObserverView = ((TabImpl) mTab).getActivity().getInsetObserverView();
mInsetObserverView = ((ChromeActivity) activity).getInsetObserverView();
if (mInsetObserverView == null) return;
mInsetObserverView.addObserver(this);
......
......@@ -13,6 +13,7 @@ import org.chromium.chrome.browser.tab.Tab;
import org.chromium.components.dom_distiller.content.DistillablePageUtils;
import org.chromium.components.dom_distiller.content.DistillablePageUtils.PageDistillableDelegate;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
* A mechanism for clients interested in the distillability of a page to receive updates.
......@@ -145,8 +146,8 @@ public class TabDistillabilityProvider
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) return;
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) return;
resetState();
}
......
......@@ -28,6 +28,7 @@ import org.chromium.chrome.browser.widget.bottomsheet.EmptyBottomSheetObserver;
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.KeyboardVisibilityDelegate.KeyboardVisibilityListener;
import org.chromium.ui.base.WindowAndroid;
import java.util.ArrayList;
......@@ -118,8 +119,8 @@ public class InfoBarContainer implements UserData, KeyboardVisibilityListener, I
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) {
initializeContainerView();
updateWebContents();
} else {
......
......@@ -35,6 +35,7 @@ import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver;
import org.chromium.ui.base.WindowAndroid;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
......@@ -331,10 +332,8 @@ public class PictureInPictureController {
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) {
dismissActivity(mActivity, METRICS_END_REASON_REPARENT);
}
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) dismissActivity(mActivity, METRICS_END_REASON_REPARENT);
}
@Override
......
......@@ -7,12 +7,15 @@ package org.chromium.chrome.browser.tab;
import android.graphics.Bitmap;
import android.view.ContextMenu;
import androidx.annotation.Nullable;
import org.chromium.components.find_in_page.FindMatchRectsDetails;
import org.chromium.components.find_in_page.FindNotificationDetails;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.WebContents;
import org.chromium.net.NetError;
import org.chromium.ui.base.WindowAndroid;
/**
* An implementation of the {@link TabObserver} which has empty implementations of all methods.
......@@ -126,7 +129,7 @@ public class EmptyTabObserver implements TabObserver {
String targetUrl, WebContents newWebContents) {}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {}
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {}
@Override
public void onInteractabilityChanged(Tab tab, boolean isInteractable) {}
......
......@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.tab;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
......@@ -26,6 +27,7 @@ import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.UiThreadTaskTraits;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.common.ConsoleMessageLevel;
import org.chromium.ui.base.WindowAndroid;
/**
* Class that controls navigations and allows to intercept them. It is used on Android to 'convert'
......@@ -76,8 +78,8 @@ public class InterceptNavigationDelegateImpl implements InterceptNavigationDeleg
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean attached) {
if (attached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) {
setExternalNavigationHandler(
mTab.getDelegateFactory().createExternalNavigationHandler(tab));
}
......
......@@ -4,6 +4,8 @@
package org.chromium.chrome.browser.tab;
import androidx.annotation.Nullable;
import org.chromium.base.Callback;
import org.chromium.base.UserData;
import org.chromium.base.annotations.NativeMethods;
......@@ -11,6 +13,7 @@ import org.chromium.components.browser_ui.util.BrowserControlsVisibilityDelegate
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.common.BrowserControlsState;
import org.chromium.ui.base.WindowAndroid;
/**
* Manages the state of tab browser controls.
......@@ -82,8 +85,8 @@ public class TabBrowserControlsConstraintsHelper implements UserData {
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) updateVisibilityDelegate();
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) updateVisibilityDelegate();
}
@Override
......
......@@ -308,8 +308,7 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
// Non-null delegate factory while being detached is not valid.
assert !(window == null && tabDelegateFactory != null);
boolean attached = window != null;
if (attached) {
if (window != null) {
updateWindowAndroid(window);
if (tabDelegateFactory != null) setDelegateFactory(tabDelegateFactory);
......@@ -324,7 +323,7 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
|| (window == null && tabDelegateFactory == null);
if (notify) {
for (TabObserver observer : mObservers) {
observer.onActivityAttachmentChanged(this, attached);
observer.onActivityAttachmentChanged(this, window);
}
}
}
......
......@@ -7,12 +7,15 @@ package org.chromium.chrome.browser.tab;
import android.graphics.Bitmap;
import android.view.ContextMenu;
import androidx.annotation.Nullable;
import org.chromium.chrome.browser.TabLoadStatus;
import org.chromium.components.find_in_page.FindMatchRectsDetails;
import org.chromium.components.find_in_page.FindNotificationDetails;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
* An observer that is notified of changes to a {@link Tab} object.
......@@ -289,9 +292,10 @@ public interface TabObserver {
/**
* Called when the Tab is attached or detached from an {@code Activity}.
* @param tab The notifying {@link Tab}.
* @param isAttached Whether the Tab is being attached or detached.
* @param window {@link WindowAndroid} which the Tab is being associated with. {@code null} if
* the tab is being detached.
*/
void onActivityAttachmentChanged(Tab tab, boolean isAttached);
void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window);
/**
* A notification when tab changes whether or not it is interactable and is accepting input.
......
......@@ -8,6 +8,8 @@ import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import org.chromium.chrome.browser.device.DeviceClassManager;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.util.AccessibilityUtil;
......@@ -21,6 +23,7 @@ import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.SelectionPopupController;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.common.BrowserControlsState;
import org.chromium.ui.base.WindowAndroid;
/**
* Determines the desired visibility of the browser controls based on the current state of a given
......@@ -149,8 +152,8 @@ public class TabStateBrowserControlsVisibilityDelegate
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) updateVisibilityConstraints();
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) updateVisibilityConstraints();
}
@Override
......
......@@ -17,6 +17,7 @@ import org.chromium.components.security_state.SecurityStateModel;
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.RenderWidgetHostView;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.util.ColorUtils;
/**
......@@ -253,7 +254,7 @@ public class TabThemeColorHelper extends EmptyTabObserver implements UserData {
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
updateDefaultColor();
updateDefaultBackgroundColor();
}
......
......@@ -6,10 +6,13 @@ package org.chromium.chrome.browser.tab;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import org.chromium.base.Callback;
import org.chromium.base.supplier.ObservableSupplier;
import org.chromium.content_public.browser.RenderWidgetHostView;
import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid;
/**
* Implementation of the abstract class {@link ViewAndroidDelegate} for Chrome.
......@@ -36,8 +39,8 @@ public class TabViewAndroidDelegate extends ViewAndroidDelegate {
mTab.addObserver(new EmptyTabObserver() {
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window != null) {
mCurrentInsetSupplier =
tab.getWindowAndroid().getApplicationBottomInsetProvider();
mCurrentInsetSupplier.addObserver(insetObserver);
......
......@@ -43,6 +43,7 @@ import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelUtils;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.modaldialog.DialogDismissalCause;
import org.chromium.ui.modaldialog.ModalDialogManager;
import org.chromium.ui.modaldialog.ModalDialogProperties;
......@@ -68,8 +69,8 @@ public class ActivityTabWebContentsDelegateAndroid extends TabWebContentsDelegat
mActivity = activity;
tab.addObserver(new EmptyTabObserver() {
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (!isAttached) mActivity = null;
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window == null) mActivity = null;
}
@Override
......
......@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.tabmodel;
import android.app.Activity;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior;
......@@ -18,6 +19,7 @@ import org.chromium.chrome.browser.tab.TabHidingType;
import org.chromium.chrome.browser.tab.TabLaunchType;
import org.chromium.chrome.browser.tab.TabSelectionType;
import org.chromium.chrome.browser.tabmodel.TabPersistentStore.TabPersistentStoreObserver;
import org.chromium.ui.base.WindowAndroid;
import java.util.concurrent.atomic.AtomicBoolean;
......@@ -192,8 +194,8 @@ public class TabModelSelectorImpl extends TabModelSelectorBase implements TabMod
}
@Override
public void onActivityAttachmentChanged(Tab tab, boolean attached) {
if (!attached && !isReparentingInProgress()) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window == null && !isReparentingInProgress()) {
getModel(tab.isIncognito()).removeTab(tab);
}
}
......
......@@ -15,6 +15,7 @@ import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
......@@ -29,6 +30,7 @@ import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabImpl;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsAccessibility;
import org.chromium.ui.base.WindowAndroid;
/**
* Represents the suspension page presented when a user tries to visit a site whose fully-qualified
......@@ -207,8 +209,8 @@ public class SuspendedTab extends EmptyTabObserver implements UserData {
// TabObserver implementation.
@Override
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (!isAttached) {
public void onActivityAttachmentChanged(Tab tab, @Nullable WindowAndroid window) {
if (window == null) {
removeViewIfPresent();
} else {
attachView();
......
......@@ -107,11 +107,12 @@ public class TabBrowserControlsConstraintsHelperTest {
initHelper();
Mockito.verify(mDelegateFactory, Mockito.never())
.createBrowserControlsVisibilityDelegate(mTab);
mRegisteredTabObserver.onActivityAttachmentChanged(mTab, false);
mRegisteredTabObserver.onActivityAttachmentChanged(mTab, null);
Mockito.verify(mDelegateFactory, Mockito.never())
.createBrowserControlsVisibilityDelegate(mTab);
mRegisteredTabObserver.onActivityAttachmentChanged(mTab, true);
WindowAndroid window = Mockito.mock(WindowAndroid.class);
mRegisteredTabObserver.onActivityAttachmentChanged(mTab, window);
Mockito.verify(mDelegateFactory, Mockito.times(1))
.createBrowserControlsVisibilityDelegate(mTab);
verifyUpdateState(BrowserControlsState.BOTH);
......@@ -139,7 +140,8 @@ public class TabBrowserControlsConstraintsHelperTest {
Mockito.when(newDelegateFactory.createBrowserControlsVisibilityDelegate(Mockito.any()))
.thenReturn(newVisibilityDelegate);
mRegisteredTabObserver.onActivityAttachmentChanged(mTab, true);
WindowAndroid window = Mockito.mock(WindowAndroid.class);
mRegisteredTabObserver.onActivityAttachmentChanged(mTab, window);
Mockito.verify(newDelegateFactory).createBrowserControlsVisibilityDelegate(mTab);
verifyUpdateState(BrowserControlsState.BOTH);
......
......@@ -15,6 +15,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.chromium.base.supplier.ObservableSupplierImpl;
......@@ -88,13 +89,13 @@ public class TabViewAndroidDelegateTest {
assertEquals("The bottom inset for the tab should be non-zero.", 10,
mViewAndroidDelegate.getViewportInsetBottom());
mTabObserverCaptor.getValue().onActivityAttachmentChanged(mTab, false);
mTabObserverCaptor.getValue().onActivityAttachmentChanged(mTab, null);
assertEquals("The bottom inset for the tab should be zero.", 0,
mViewAndroidDelegate.getViewportInsetBottom());
mTabObserverCaptor.getValue().onActivityAttachmentChanged(mTab, true);
WindowAndroid window = Mockito.mock(WindowAndroid.class);
mTabObserverCaptor.getValue().onActivityAttachmentChanged(mTab, window);
assertEquals("The bottom inset for the tab should be non-zero.", 10,
mViewAndroidDelegate.getViewportInsetBottom());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment