| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.infobar; | 5 package org.chromium.chrome.browser.infobar; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.view.Gravity; | 8 import android.view.Gravity; |
| 9 import android.view.View; | 9 import android.view.View; |
| 10 import android.view.ViewGroup; |
| 10 import android.widget.FrameLayout; | 11 import android.widget.FrameLayout; |
| 11 | 12 |
| 12 import org.chromium.base.ObserverList; | 13 import org.chromium.base.ObserverList; |
| 13 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
| 14 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; |
| 15 import org.chromium.chrome.browser.banners.SwipableOverlayView; | 16 import org.chromium.chrome.browser.banners.SwipableOverlayView; |
| 16 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 17 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
| 17 import org.chromium.chrome.browser.tab.Tab; | 18 import org.chromium.chrome.browser.tab.Tab; |
| 18 import org.chromium.chrome.browser.tab.TabContentViewParent; | |
| 19 import org.chromium.chrome.browser.tab.TabObserver; | 19 import org.chromium.chrome.browser.tab.TabObserver; |
| 20 import org.chromium.content.browser.ContentViewCore; | 20 import org.chromium.content.browser.ContentViewCore; |
| 21 import org.chromium.content_public.browser.WebContents; | 21 import org.chromium.content_public.browser.WebContents; |
| 22 import org.chromium.ui.UiUtils; | 22 import org.chromium.ui.UiUtils; |
| 23 import org.chromium.ui.base.DeviceFormFactor; | 23 import org.chromium.ui.base.DeviceFormFactor; |
| 24 | 24 |
| 25 import java.util.ArrayList; | 25 import java.util.ArrayList; |
| 26 | 26 |
| 27 | 27 |
| 28 /** | 28 /** |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 } | 86 } |
| 87 | 87 |
| 88 /** Resets the state of the InfoBarContainer when the user navigates. */ | 88 /** Resets the state of the InfoBarContainer when the user navigates. */ |
| 89 private final TabObserver mTabObserver = new EmptyTabObserver() { | 89 private final TabObserver mTabObserver = new EmptyTabObserver() { |
| 90 @Override | 90 @Override |
| 91 public void onDidNavigateMainFrame(Tab tab, String url, String baseUrl, | 91 public void onDidNavigateMainFrame(Tab tab, String url, String baseUrl, |
| 92 boolean isNavigationToDifferentPage, boolean isFragmentNavigatio
n, | 92 boolean isNavigationToDifferentPage, boolean isFragmentNavigatio
n, |
| 93 int statusCode) { | 93 int statusCode) { |
| 94 setIsObscuredByOtherView(false); | 94 setIsObscuredByOtherView(false); |
| 95 } | 95 } |
| 96 |
| 97 @Override |
| 98 public void onContentChanged(Tab tab) { |
| 99 if (mTabView == tab.getView()) return; |
| 100 mTabView.removeOnAttachStateChangeListener(mAttachedStateListener); |
| 101 mTabView = tab.getView(); |
| 102 mTabView.addOnAttachStateChangeListener(mAttachedStateListener); |
| 103 } |
| 104 }; |
| 105 |
| 106 /** |
| 107 * Adds/removes the {@link InfoBarContainer} when the tab's view is attached
/detached. This is |
| 108 * mostly to ensure the infobars are not shown in tab switcher overview mode
. |
| 109 */ |
| 110 private final OnAttachStateChangeListener mAttachedStateListener = |
| 111 new OnAttachStateChangeListener() { |
| 112 @Override |
| 113 public void onViewDetachedFromWindow(View v) { |
| 114 removeFromParentView(); |
| 115 } |
| 116 |
| 117 @Override |
| 118 public void onViewAttachedToWindow(View v) { |
| 119 addToParentView(); |
| 120 } |
| 96 }; | 121 }; |
| 97 | 122 |
| 98 private final InfoBarContainerLayout mLayout; | 123 private final InfoBarContainerLayout mLayout; |
| 99 | 124 |
| 100 /** Native InfoBarContainer pointer which will be set by nativeInit(). */ | 125 /** Native InfoBarContainer pointer which will be set by nativeInit(). */ |
| 101 private final long mNativeInfoBarContainer; | 126 private final long mNativeInfoBarContainer; |
| 102 | 127 |
| 103 /** The list of all InfoBars in this container, regardless of whether they'v
e been shown yet. */ | 128 /** The list of all InfoBars in this container, regardless of whether they'v
e been shown yet. */ |
| 104 private final ArrayList<InfoBar> mInfoBars = new ArrayList<InfoBar>(); | 129 private final ArrayList<InfoBar> mInfoBars = new ArrayList<InfoBar>(); |
| 105 | 130 |
| 106 /** True when this container has been emptied and its native counterpart has
been destroyed. */ | 131 /** True when this container has been emptied and its native counterpart has
been destroyed. */ |
| 107 private boolean mDestroyed; | 132 private boolean mDestroyed; |
| 108 | 133 |
| 109 /** The id of the tab associated with us. Set to Tab.INVALID_TAB_ID if no ta
b is associated. */ | 134 /** Parent view that contains the InfoBarContainerLayout. */ |
| 110 private int mTabId; | 135 private ViewGroup mParentView; |
| 111 | 136 |
| 112 /** Parent view that contains the InfoBarContainerLayout. */ | 137 /** The view that {@link Tab#getView()} returns. */ |
| 113 private TabContentViewParent mParentView; | 138 private View mTabView; |
| 114 | 139 |
| 115 /** Whether or not another View is occupying the same space as this one. */ | 140 /** Whether or not another View is occupying the same space as this one. */ |
| 116 private boolean mIsObscured; | 141 private boolean mIsObscured; |
| 117 | 142 |
| 118 private final ObserverList<InfoBarContainerObserver> mObservers = | 143 private final ObserverList<InfoBarContainerObserver> mObservers = |
| 119 new ObserverList<InfoBarContainerObserver>(); | 144 new ObserverList<InfoBarContainerObserver>(); |
| 120 | 145 |
| 121 public InfoBarContainer(Context context, int tabId, TabContentViewParent par
entView, Tab tab) { | 146 public InfoBarContainer(Context context, final ViewGroup parentView, Tab tab
) { |
| 122 super(context, null); | 147 super(context, null); |
| 123 tab.addObserver(mTabObserver); | 148 tab.addObserver(mTabObserver); |
| 149 mTabView = tab.getView(); |
| 124 | 150 |
| 125 // TODO(newt): move this workaround into the infobar views if/when they'
re scrollable. | 151 // TODO(newt): move this workaround into the infobar views if/when they'
re scrollable. |
| 126 // Workaround for http://crbug.com/407149. See explanation in onMeasure(
) below. | 152 // Workaround for http://crbug.com/407149. See explanation in onMeasure(
) below. |
| 127 setVerticalScrollBarEnabled(false); | 153 setVerticalScrollBarEnabled(false); |
| 128 | 154 |
| 129 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams( | 155 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams( |
| 130 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BO
TTOM); | 156 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BO
TTOM); |
| 131 int topMarginDp = DeviceFormFactor.isTablet(context) | 157 int topMarginDp = DeviceFormFactor.isTablet(context) |
| 132 ? TOP_MARGIN_TABLET_DP : TOP_MARGIN_PHONE_DP; | 158 ? TOP_MARGIN_TABLET_DP : TOP_MARGIN_PHONE_DP; |
| 133 lp.topMargin = Math.round(topMarginDp * getResources().getDisplayMetrics
().density); | 159 lp.topMargin = Math.round(topMarginDp * getResources().getDisplayMetrics
().density); |
| 134 setLayoutParams(lp); | 160 setLayoutParams(lp); |
| 135 | 161 |
| 136 mTabId = tabId; | |
| 137 mParentView = parentView; | 162 mParentView = parentView; |
| 138 | 163 |
| 139 mLayout = new InfoBarContainerLayout(context); | 164 mLayout = new InfoBarContainerLayout(context); |
| 140 addView(mLayout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, | 165 addView(mLayout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 141 LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); | 166 LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); |
| 142 | 167 |
| 143 // Chromium's InfoBarContainer may add an InfoBar immediately during thi
s initialization | 168 // Chromium's InfoBarContainer may add an InfoBar immediately during thi
s initialization |
| 144 // call, so make sure everything in the InfoBarContainer is completely r
eady beforehand. | 169 // call, so make sure everything in the InfoBarContainer is completely r
eady beforehand. |
| 145 mNativeInfoBarContainer = nativeInit(); | 170 mNativeInfoBarContainer = nativeInit(); |
| 146 } | 171 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 162 } | 187 } |
| 163 | 188 |
| 164 @Override | 189 @Override |
| 165 public void setContentViewCore(ContentViewCore contentViewCore) { | 190 public void setContentViewCore(ContentViewCore contentViewCore) { |
| 166 super.setContentViewCore(contentViewCore); | 191 super.setContentViewCore(contentViewCore); |
| 167 if (getContentViewCore() != null) { | 192 if (getContentViewCore() != null) { |
| 168 nativeSetWebContents(mNativeInfoBarContainer, contentViewCore.getWeb
Contents()); | 193 nativeSetWebContents(mNativeInfoBarContainer, contentViewCore.getWeb
Contents()); |
| 169 } | 194 } |
| 170 } | 195 } |
| 171 | 196 |
| 197 /** |
| 198 * Sets the parent {@link ViewGroup} that contains the {@link InfoBarContain
er}. |
| 199 */ |
| 200 public void setParentView(ViewGroup parent) { |
| 201 mParentView = parent; |
| 202 removeFromParentView(); |
| 203 addToParentView(); |
| 204 } |
| 205 |
| 172 @VisibleForTesting | 206 @VisibleForTesting |
| 173 public void setAnimationListener(InfoBarAnimationListener listener) { | 207 public void setAnimationListener(InfoBarAnimationListener listener) { |
| 174 mLayout.setAnimationListener(listener); | 208 mLayout.setAnimationListener(listener); |
| 175 } | 209 } |
| 176 | 210 |
| 177 /** | 211 /** |
| 178 * Returns true if any animations are pending or in progress. | 212 * Returns true if any animations are pending or in progress. |
| 179 */ | 213 */ |
| 180 @VisibleForTesting | 214 @VisibleForTesting |
| 181 public boolean isAnimating() { | 215 public boolean isAnimating() { |
| 182 return mLayout.isAnimating(); | 216 return mLayout.isAnimating(); |
| 183 } | 217 } |
| 184 | 218 |
| 185 private void addToParentView() { | 219 private void addToParentView() { |
| 186 super.addToParentView(mParentView); | 220 super.addToParentView(mParentView); |
| 187 } | 221 } |
| 188 | 222 |
| 189 /** | 223 /** |
| 190 * Called when the parent {@link android.view.ViewGroup} has changed for | |
| 191 * this container. | |
| 192 */ | |
| 193 public void onParentViewChanged(int tabId, TabContentViewParent parentView)
{ | |
| 194 mTabId = tabId; | |
| 195 mParentView = parentView; | |
| 196 | |
| 197 removeFromParentView(); | |
| 198 addToParentView(); | |
| 199 } | |
| 200 | |
| 201 /** | |
| 202 * Adds an InfoBar to the view hierarchy. | 224 * Adds an InfoBar to the view hierarchy. |
| 203 * @param infoBar InfoBar to add to the View hierarchy. | 225 * @param infoBar InfoBar to add to the View hierarchy. |
| 204 */ | 226 */ |
| 205 @CalledByNative | 227 @CalledByNative |
| 206 private void addInfoBar(InfoBar infoBar) { | 228 private void addInfoBar(InfoBar infoBar) { |
| 207 assert !mDestroyed; | 229 assert !mDestroyed; |
| 208 if (infoBar == null) { | 230 if (infoBar == null) { |
| 209 return; | 231 return; |
| 210 } | 232 } |
| 211 if (mInfoBars.contains(infoBar)) { | 233 if (mInfoBars.contains(infoBar)) { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 } | 384 } |
| 363 | 385 |
| 364 super.onLayout(changed, l, t, r, b); | 386 super.onLayout(changed, l, t, r, b); |
| 365 } | 387 } |
| 366 | 388 |
| 367 private native long nativeInit(); | 389 private native long nativeInit(); |
| 368 private native void nativeSetWebContents( | 390 private native void nativeSetWebContents( |
| 369 long nativeInfoBarContainerAndroid, WebContents webContents); | 391 long nativeInfoBarContainerAndroid, WebContents webContents); |
| 370 private native void nativeDestroy(long nativeInfoBarContainerAndroid); | 392 private native void nativeDestroy(long nativeInfoBarContainerAndroid); |
| 371 } | 393 } |
| OLD | NEW |