Commit 8bc03e93 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Android: InfoBarContainer on Tab UserData

InfoBarContainer is now managed by Tab's UserDataHost,
and accessed through new static methods |from()| and |get()| defined
in the container class. Tab still a reference to a container.
It will be removed when all the callsites of |Tab.getInfoBatContainer|
are updated in a follow-up CL.

Bug: 877878
Change-Id: If59bf7e750bcaf7f0988925ab06f94eb8974cddc
Reviewed-on: https://chromium-review.googlesource.com/1212378
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590264}
parent 1eba14c5
...@@ -642,7 +642,7 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat ...@@ -642,7 +642,7 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
if (!hasValidTab() || mTab.getWebContents() == null) return false; if (!hasValidTab() || mTab.getWebContents() == null) return false;
InstantAppsHandler handler = InstantAppsHandler.getInstance(); InstantAppsHandler handler = InstantAppsHandler.getInstance();
TabRedirectHandler redirect = TabRedirectHandler.getOrNull(mTab); TabRedirectHandler redirect = TabRedirectHandler.get(mTab);
Intent intent = redirect != null ? redirect.getInitialIntent() : null; Intent intent = redirect != null ? redirect.getInitialIntent() : null;
// TODO(mariakhomenko): consider also handling NDEF_DISCOVER action redirects. // TODO(mariakhomenko): consider also handling NDEF_DISCOVER action redirects.
if (isIncomingRedirect && intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) { if (isIncomingRedirect && intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
......
...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.infobar; ...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.infobar;
import android.animation.Animator; import android.animation.Animator;
import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.Gravity; import android.view.Gravity;
...@@ -14,6 +15,7 @@ import android.view.ViewGroup; ...@@ -14,6 +15,7 @@ import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import org.chromium.base.ObserverList; import org.chromium.base.ObserverList;
import org.chromium.base.UserData;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.R; import org.chromium.chrome.R;
...@@ -30,6 +32,7 @@ import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetObserver; ...@@ -30,6 +32,7 @@ import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetObserver;
import org.chromium.chrome.browser.widget.bottomsheet.EmptyBottomSheetObserver; import org.chromium.chrome.browser.widget.bottomsheet.EmptyBottomSheetObserver;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.UiUtils; import org.chromium.ui.UiUtils;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.display.DisplayAndroid; import org.chromium.ui.display.DisplayAndroid;
import org.chromium.ui.display.DisplayUtil; import org.chromium.ui.display.DisplayUtil;
...@@ -41,9 +44,11 @@ import java.util.ArrayList; ...@@ -41,9 +44,11 @@ import java.util.ArrayList;
* When initiated from native code, special code is needed to keep the Java and native infobar in * When initiated from native code, special code is needed to keep the Java and native infobar in
* sync, see NativeInfoBar. * sync, see NativeInfoBar.
*/ */
public class InfoBarContainer extends SwipableOverlayView { public class InfoBarContainer extends SwipableOverlayView implements UserData {
private static final String TAG = "InfoBarContainer"; private static final String TAG = "InfoBarContainer";
private static final Class<InfoBarContainer> USER_DATA_KEY = InfoBarContainer.class;
/** Top margin, including the toolbar and tabstrip height and 48dp of web contents. */ /** Top margin, including the toolbar and tabstrip height and 48dp of web contents. */
private static final int TOP_MARGIN_PHONE_DP = 104; private static final int TOP_MARGIN_PHONE_DP = 104;
private static final int TOP_MARGIN_TABLET_DP = 144; private static final int TOP_MARGIN_TABLET_DP = 144;
...@@ -195,8 +200,26 @@ public class InfoBarContainer extends SwipableOverlayView { ...@@ -195,8 +200,26 @@ public class InfoBarContainer extends SwipableOverlayView {
/** The tab that hosts this infobar container. */ /** The tab that hosts this infobar container. */
private Tab mTab; private Tab mTab;
public InfoBarContainer(Context context, final ViewGroup parentView, Tab tab) { public static InfoBarContainer from(Tab tab) {
super(context, null); InfoBarContainer container = get(tab);
if (container == null) {
container = tab.getUserDataHost().setUserData(USER_DATA_KEY, new InfoBarContainer(tab));
}
return container;
}
/**
* Returns {@link InfoBarContainer} object for a given {@link Tab}, or {@code null}
* if there is no object available.
*/
@Nullable
public static InfoBarContainer get(Tab tab) {
return tab.getUserDataHost().getUserData(USER_DATA_KEY);
}
private InfoBarContainer(Tab tab) {
super(tab.getThemedApplicationContext(), null);
tab.addObserver(mTabObserver); tab.addObserver(mTabObserver);
mTabView = tab.getView(); mTabView = tab.getView();
mTab = tab; mTab = tab;
...@@ -207,9 +230,10 @@ public class InfoBarContainer extends SwipableOverlayView { ...@@ -207,9 +230,10 @@ public class InfoBarContainer extends SwipableOverlayView {
updateLayoutParams(tab.getActivity()); updateLayoutParams(tab.getActivity());
mParentView = parentView; mParentView = getBottomContainer(tab);
Runnable makeContainerVisibleRunnable = () -> runUpEventAnimation(true); Runnable makeContainerVisibleRunnable = () -> runUpEventAnimation(true);
Context context = tab.getThemedApplicationContext();
mLayout = new InfoBarContainerLayout(context, makeContainerVisibleRunnable); mLayout = new InfoBarContainerLayout(context, makeContainerVisibleRunnable);
addView(mLayout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, addView(mLayout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
...@@ -224,6 +248,12 @@ public class InfoBarContainer extends SwipableOverlayView { ...@@ -224,6 +248,12 @@ public class InfoBarContainer extends SwipableOverlayView {
mNativeInfoBarContainer = nativeInit(); mNativeInfoBarContainer = nativeInit();
} }
private static ViewGroup getBottomContainer(Tab tab) {
WindowAndroid windowAndroid = tab.getWindowAndroid();
Activity activity = windowAndroid == null ? null : windowAndroid.getActivity().get();
return activity == null ? null : (ViewGroup) activity.findViewById(R.id.bottom_container);
}
private void updateLayoutParams(@Nullable ChromeActivity activity) { private void updateLayoutParams(@Nullable ChromeActivity activity) {
if (activity == null) { if (activity == null) {
return; return;
...@@ -385,6 +415,7 @@ public class InfoBarContainer extends SwipableOverlayView { ...@@ -385,6 +415,7 @@ public class InfoBarContainer extends SwipableOverlayView {
return mDestroyed; return mDestroyed;
} }
@Override
public void destroy() { public void destroy() {
ChromeActivity activity = mTab.getActivity(); ChromeActivity activity = mTab.getActivity();
if (activity != null && mBottomSheetObserver != null && activity.getBottomSheet() != null) { if (activity != null && mBottomSheetObserver != null && activity.getBottomSheet() != null) {
......
...@@ -562,7 +562,7 @@ public class Tab ...@@ -562,7 +562,7 @@ public class Tab
return mUserDataHost; return mUserDataHost;
} }
Context getContext() { public Context getThemedApplicationContext() {
return mThemedApplicationContext; return mThemedApplicationContext;
} }
...@@ -1822,19 +1822,11 @@ public class Tab ...@@ -1822,19 +1822,11 @@ public class Tab
initNativeWebContents(mWebContents, parentId); initNativeWebContents(mWebContents, parentId);
// The InfoBarContainer needs to be created after the ContentView has been natively
// initialized.
// In the case where restoring a Tab or showing a prerendered one we already have a // In the case where restoring a Tab or showing a prerendered one we already have a
// valid infobar container, no need to recreate one. // valid infobar container, no need to recreate one.
if (mInfoBarContainer == null) { mInfoBarContainer = InfoBarContainer.from(this);
WindowAndroid windowAndroid = getWindowAndroid();
Activity activity =
windowAndroid == null ? null : windowAndroid.getActivity().get();
ViewGroup bottomContainer = activity == null ? null
: (ViewGroup) activity.findViewById(R.id.bottom_container);
// The InfoBarContainer needs to be created after the ContentView has been natively
// initialized.
mInfoBarContainer = new InfoBarContainer(mThemedApplicationContext, bottomContainer,
this);
}
mInfoBarContainer.setWebContents(getWebContents()); mInfoBarContainer.setWebContents(getWebContents());
mSwipeRefreshHandler = new SwipeRefreshHandler(mThemedApplicationContext, this); mSwipeRefreshHandler = new SwipeRefreshHandler(mThemedApplicationContext, this);
...@@ -2055,10 +2047,6 @@ public class Tab ...@@ -2055,10 +2047,6 @@ public class Tab
nativeDestroy(mNativeTabAndroid); nativeDestroy(mNativeTabAndroid);
assert mNativeTabAndroid == 0; assert mNativeTabAndroid == 0;
if (mInfoBarContainer != null) {
mInfoBarContainer.destroy();
mInfoBarContainer = null;
}
mUserDataHost.destroy(); mUserDataHost.destroy();
} }
......
...@@ -10,6 +10,7 @@ import android.content.Intent; ...@@ -10,6 +10,7 @@ import android.content.Intent;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.os.SystemClock; import android.os.SystemClock;
import android.provider.Browser; import android.provider.Browser;
import android.support.annotation.Nullable;
import android.text.TextUtils; import android.text.TextUtils;
import org.chromium.base.UserData; import org.chromium.base.UserData;
...@@ -68,7 +69,7 @@ public class TabRedirectHandler extends EmptyTabObserver implements UserData { ...@@ -68,7 +69,7 @@ public class TabRedirectHandler extends EmptyTabObserver implements UserData {
UserDataHost host = tab.getUserDataHost(); UserDataHost host = tab.getUserDataHost();
TabRedirectHandler handler = host.getUserData(USER_DATA_KEY); TabRedirectHandler handler = host.getUserData(USER_DATA_KEY);
if (handler == null) { if (handler == null) {
handler = new TabRedirectHandler(tab.getContext()); handler = new TabRedirectHandler(tab.getThemedApplicationContext());
host.setUserData(USER_DATA_KEY, handler); host.setUserData(USER_DATA_KEY, handler);
tab.addObserver(handler); tab.addObserver(handler);
} }
...@@ -79,7 +80,8 @@ public class TabRedirectHandler extends EmptyTabObserver implements UserData { ...@@ -79,7 +80,8 @@ public class TabRedirectHandler extends EmptyTabObserver implements UserData {
* @return {@link TabRedirectHandler} hanging to the given {@link Tab}, * @return {@link TabRedirectHandler} hanging to the given {@link Tab},
* or {@code null} if there is no instance available. * or {@code null} if there is no instance available.
*/ */
public static TabRedirectHandler getOrNull(Tab tab) { @Nullable
public static TabRedirectHandler get(Tab tab) {
return tab.getUserDataHost().getUserData(USER_DATA_KEY); return tab.getUserDataHost().getUserData(USER_DATA_KEY);
} }
......
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