OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.compositor.layouts; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.graphics.Bitmap; |
| 9 import android.text.TextUtils; |
| 10 import android.view.ViewGroup; |
| 11 |
| 12 import com.google.android.apps.chrome.R; |
| 13 |
| 14 import org.chromium.base.ObserverList; |
| 15 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.chrome.browser.Tab; |
| 17 import org.chromium.chrome.browser.compositor.TitleCache; |
| 18 import org.chromium.chrome.browser.compositor.layouts.components.LayoutTab; |
| 19 import org.chromium.chrome.browser.compositor.layouts.components.VirtualView; |
| 20 import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager; |
| 21 import org.chromium.chrome.browser.compositor.layouts.content.TitleBitmapFactory
; |
| 22 import org.chromium.chrome.browser.compositor.layouts.eventfilter.BlackHoleEvent
Filter; |
| 23 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EdgeSwipeEvent
Filter.ScrollDirection; |
| 24 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EdgeSwipeHandl
er; |
| 25 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EventFilter; |
| 26 import org.chromium.chrome.browser.compositor.layouts.eventfilter.GestureEventFi
lter; |
| 27 import org.chromium.chrome.browser.compositor.overlays.SceneOverlay; |
| 28 import org.chromium.chrome.browser.compositor.overlays.strip.StripLayoutHelperMa
nager; |
| 29 import org.chromium.chrome.browser.contextualsearch.ContextualSearchManagementDe
legate; |
| 30 import org.chromium.chrome.browser.device.DeviceClassManager; |
| 31 import org.chromium.chrome.browser.fullscreen.FullscreenManager; |
| 32 import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver; |
| 33 import org.chromium.chrome.browser.tabmodel.EmptyTabModelSelectorObserver; |
| 34 import org.chromium.chrome.browser.tabmodel.TabCreatorManager; |
| 35 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 36 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
| 37 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; |
| 38 import org.chromium.chrome.browser.tabmodel.TabModelObserver; |
| 39 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 40 import org.chromium.chrome.browser.tabmodel.TabModelSelector.CloseAllTabsDelegat
e; |
| 41 import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver; |
| 42 import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabObserver; |
| 43 import org.chromium.chrome.browser.tabmodel.TabModelUtils; |
| 44 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 45 import org.chromium.chrome.browser.widget.OverviewListLayout; |
| 46 import org.chromium.ui.base.LocalizationUtils; |
| 47 import org.chromium.ui.resources.dynamics.DynamicResourceLoader; |
| 48 |
| 49 import java.util.List; |
| 50 |
| 51 /** |
| 52 * A {@link Layout} controller for the more complicated Chrome browser. This is
currently a |
| 53 * superset of {@link LayoutManagerDocument}. |
| 54 */ |
| 55 public class LayoutManagerChrome |
| 56 extends LayoutManagerDocument implements OverviewModeBehavior, CloseAllT
absDelegate { |
| 57 // Layouts |
| 58 /** An {@link Layout} that should be used as the accessibility tab switcher.
*/ |
| 59 protected OverviewListLayout mOverviewListLayout; |
| 60 /** A {@link Layout} that should be used when the user is swiping sideways o
n the toolbar. */ |
| 61 protected ToolbarSwipeLayout mToolbarSwipeLayout; |
| 62 /** A {@link Layout} that should be used when the user is in the tab switche
r. */ |
| 63 protected Layout mOverviewLayout; |
| 64 |
| 65 // Event Filters |
| 66 /** A {@link EventFilter} that consumes all touch events. */ |
| 67 protected EventFilter mBlackHoleEventFilter; |
| 68 private final GestureEventFilter mGestureEventFilter; |
| 69 |
| 70 // Event Filter Handlers |
| 71 private final EdgeSwipeHandler mToolbarSwipeHandler; |
| 72 |
| 73 // Internal State |
| 74 /** A {@link TitleCache} instance that stores all title/favicon bitmaps as C
C resources. */ |
| 75 protected TitleCache mTitleCache; |
| 76 /** Responsible for building non-incognito titles. */ |
| 77 protected TitleBitmapFactory mStandardTitleBitmapFactory; |
| 78 /** Responsible for building all incognito titles. */ |
| 79 protected TitleBitmapFactory mIncognitoTitleBitmapFactory; |
| 80 /** Whether or not animations are enabled. This can disable certain layouts
or effects. */ |
| 81 protected boolean mEnableAnimations = true; |
| 82 private boolean mCreatingNtp; |
| 83 private final ObserverList<OverviewModeObserver> mOverviewModeObservers; |
| 84 private TabModelSelectorObserver mTabModelSelectorObserver; |
| 85 private TabModelObserver mTabModelObserver; |
| 86 private TabModelSelectorTabObserver mTabSelectorTabObserver; |
| 87 |
| 88 /** |
| 89 * Protected class to handle {@link TabModelObserver} related tasks. Extendi
ng classes will |
| 90 * need to override any related calls to add new functionality */ |
| 91 protected class LayoutManagerTabModelObserver extends EmptyTabModelObserver
{ |
| 92 @Override |
| 93 public void didSelectTab(Tab tab, TabSelectionType type, int lastId) { |
| 94 if (tab.getId() != lastId) tabSelected(tab.getId(), lastId, tab.isIn
cognito()); |
| 95 } |
| 96 |
| 97 @Override |
| 98 public void willAddTab(Tab tab, TabLaunchType type) { |
| 99 // Open the new tab |
| 100 if (type == TabLaunchType.FROM_INSTANT || type == TabLaunchType.FROM
_RESTORE) return; |
| 101 |
| 102 tabCreating(getTabModelSelector().getCurrentTabId(), tab.getUrl(), t
ab.isIncognito()); |
| 103 } |
| 104 |
| 105 @Override |
| 106 public void didAddTab(Tab tab, TabLaunchType launchType) { |
| 107 int tabId = tab.getId(); |
| 108 if (launchType != TabLaunchType.FROM_INSTANT |
| 109 && launchType != TabLaunchType.FROM_RESTORE) { |
| 110 boolean incognito = tab.isIncognito(); |
| 111 boolean willBeSelected = launchType != TabLaunchType.FROM_LONGPR
ESS_BACKGROUND |
| 112 || (!getTabModelSelector().isIncognitoSelected() && inco
gnito); |
| 113 float lastTapX = LocalizationUtils.isLayoutRtl() ? mLastContentW
idthDp : 0.f; |
| 114 float lastTapY = 0.f; |
| 115 if (launchType != TabLaunchType.FROM_MENU_OR_OVERVIEW) { |
| 116 float heightDelta = |
| 117 mLastFullscreenViewportDp.height() - mLastVisibleVie
wportDp.height(); |
| 118 lastTapX = mPxToDp * mLastTapX; |
| 119 lastTapY = mPxToDp * mLastTapY - heightDelta; |
| 120 } |
| 121 |
| 122 tabCreated(tabId, getTabModelSelector().getCurrentTabId(), launc
hType, incognito, |
| 123 willBeSelected, lastTapX, lastTapY); |
| 124 } |
| 125 } |
| 126 |
| 127 @Override |
| 128 public void didCloseTab(Tab tab) { |
| 129 tabClosed(tab); |
| 130 } |
| 131 |
| 132 @Override |
| 133 public void tabPendingClosure(Tab tab) { |
| 134 tabClosed(tab); |
| 135 } |
| 136 |
| 137 @Override |
| 138 public void tabClosureUndone(Tab tab) { |
| 139 tabClosureCancelled(tab.getId(), tab.isIncognito()); |
| 140 } |
| 141 |
| 142 @Override |
| 143 public void tabClosureCommitted(Tab tab) { |
| 144 LayoutManagerChrome.this.tabClosureCommitted(tab.getId(), tab.isInco
gnito()); |
| 145 } |
| 146 |
| 147 @Override |
| 148 public void didMoveTab(Tab tab, int newIndex, int curIndex) { |
| 149 tabMoved(tab.getId(), curIndex, newIndex, tab.isIncognito()); |
| 150 } |
| 151 } |
| 152 |
| 153 /** |
| 154 * Delegate of a factory to create an overview layout. |
| 155 */ |
| 156 public interface OverviewLayoutFactoryDelegate { |
| 157 /** |
| 158 * @param context The current Android's context. |
| 159 * @param updateHost The {@link LayoutUpdateHost} view for this layout. |
| 160 * @param renderHost The {@link LayoutRenderHost} view for this layout. |
| 161 * @param eventFilter The {@link EventFilter} that is needed for this vi
ew. |
| 162 */ |
| 163 Layout createOverviewLayout(Context context, LayoutUpdateHost updateHost
, |
| 164 LayoutRenderHost renderHost, EventFilter eventFilter); |
| 165 } |
| 166 |
| 167 /** |
| 168 * Creates the {@link LayoutManagerChrome} instance. |
| 169 * @param host A {@link LayoutManagerHost} instance. |
| 170 * @param overviewLayoutFactoryDelegate A {@link OverviewLayoutFactoryDelega
te} instance. |
| 171 */ |
| 172 public LayoutManagerChrome( |
| 173 LayoutManagerHost host, OverviewLayoutFactoryDelegate overviewLayout
FactoryDelegate) { |
| 174 super(host); |
| 175 Context context = host.getContext(); |
| 176 LayoutRenderHost renderHost = host.getLayoutRenderHost(); |
| 177 |
| 178 // Set up state |
| 179 mStandardTitleBitmapFactory = |
| 180 new TitleBitmapFactory(context, false, R.drawable.default_favico
n); |
| 181 mIncognitoTitleBitmapFactory = |
| 182 new TitleBitmapFactory(context, true, R.drawable.default_favicon
_white); |
| 183 mOverviewModeObservers = new ObserverList<OverviewModeObserver>(); |
| 184 |
| 185 // Build Event Filter Handlers |
| 186 mToolbarSwipeHandler = new ToolbarSwipeHandler(this); |
| 187 |
| 188 // Build Event Filters |
| 189 mBlackHoleEventFilter = new BlackHoleEventFilter(context, this); |
| 190 mGestureEventFilter = new GestureEventFilter(context, this, mGestureHand
ler); |
| 191 |
| 192 // Build Layouts |
| 193 mOverviewListLayout = |
| 194 new OverviewListLayout(context, this, renderHost, mBlackHoleEven
tFilter); |
| 195 mToolbarSwipeLayout = |
| 196 new ToolbarSwipeLayout(context, this, renderHost, mBlackHoleEven
tFilter); |
| 197 if (overviewLayoutFactoryDelegate != null) { |
| 198 mOverviewLayout = overviewLayoutFactoryDelegate.createOverviewLayout
( |
| 199 context, this, renderHost, mGestureEventFilter); |
| 200 } |
| 201 } |
| 202 |
| 203 /** |
| 204 * @return The {@link TabModelObserver} instance this class should be using. |
| 205 */ |
| 206 protected LayoutManagerTabModelObserver createTabModelObserver() { |
| 207 return new LayoutManagerTabModelObserver(); |
| 208 } |
| 209 |
| 210 /** |
| 211 * @return A list of virtual views representing compositor rendered views. |
| 212 */ |
| 213 @Override |
| 214 public void getVirtualViews(List<VirtualView> views) { |
| 215 if (getActiveLayout() != null) { |
| 216 getActiveLayout().getVirtualViews(views); |
| 217 } |
| 218 } |
| 219 |
| 220 /** |
| 221 * @return The {@link EdgeSwipeHandler} responsible for processing swipe eve
nts for the toolbar. |
| 222 */ |
| 223 @Override |
| 224 public EdgeSwipeHandler getTopSwipeHandler() { |
| 225 return mToolbarSwipeHandler; |
| 226 } |
| 227 |
| 228 @Override |
| 229 public void init(TabModelSelector selector, TabCreatorManager creator, |
| 230 TabContentManager content, ViewGroup androidContentContainer, |
| 231 ContextualSearchManagementDelegate contextualSearchDelegate, |
| 232 DynamicResourceLoader dynamicResourceLoader) { |
| 233 // TODO: TitleCache should be a part of the ResourceManager. |
| 234 mTitleCache = mHost.getTitleCache(); |
| 235 |
| 236 // Initialize Layouts |
| 237 mToolbarSwipeLayout.setTabModelSelector(selector, content); |
| 238 mOverviewListLayout.setTabModelSelector(selector, content); |
| 239 if (mOverviewLayout != null) mOverviewLayout.setTabModelSelector(selecto
r, content); |
| 240 |
| 241 super.init(selector, creator, content, androidContentContainer, contextu
alSearchDelegate, |
| 242 dynamicResourceLoader); |
| 243 |
| 244 mTabModelSelectorObserver = new EmptyTabModelSelectorObserver() { |
| 245 @Override |
| 246 public void onTabModelSelected(TabModel newModel, TabModel oldModel)
{ |
| 247 tabModelSwitched(newModel.isIncognito()); |
| 248 } |
| 249 }; |
| 250 selector.addObserver(mTabModelSelectorObserver); |
| 251 selector.setCloseAllTabsDelegate(this); |
| 252 |
| 253 mTabModelObserver = createTabModelObserver(); |
| 254 for (TabModel model : selector.getModels()) model.addObserver(mTabModelO
bserver); |
| 255 |
| 256 mTabSelectorTabObserver = new TabModelSelectorTabObserver(selector) { |
| 257 @Override |
| 258 public void onLoadStarted(Tab tab) { |
| 259 tabLoadStarted(tab.getId(), tab.isIncognito()); |
| 260 } |
| 261 |
| 262 @Override |
| 263 public void onLoadStopped(Tab tab) { |
| 264 tabLoadFinished(tab.getId(), tab.isIncognito()); |
| 265 } |
| 266 |
| 267 @Override |
| 268 public void onPageLoadStarted(Tab tab) { |
| 269 tabPageLoadStarted(tab.getId(), tab.isIncognito()); |
| 270 } |
| 271 |
| 272 @Override |
| 273 public void onPageLoadFinished(Tab tab) { |
| 274 tabPageLoadFinished(tab.getId(), tab.isIncognito()); |
| 275 } |
| 276 |
| 277 @Override |
| 278 public void onPageLoadFailed(Tab tab, int errorCode) { |
| 279 tabPageLoadFinished(tab.getId(), tab.isIncognito()); |
| 280 } |
| 281 |
| 282 @Override |
| 283 public void onCrash(Tab tab, boolean sadTabShown) { |
| 284 tabPageLoadFinished(tab.getId(), tab.isIncognito()); |
| 285 } |
| 286 }; |
| 287 } |
| 288 |
| 289 @Override |
| 290 public void destroy() { |
| 291 super.destroy(); |
| 292 if (mTabModelSelectorObserver != null) { |
| 293 getTabModelSelector().removeObserver(mTabModelSelectorObserver); |
| 294 } |
| 295 if (mTabModelObserver != null) { |
| 296 for (TabModel model : getTabModelSelector().getModels()) { |
| 297 model.removeObserver(mTabModelObserver); |
| 298 } |
| 299 } |
| 300 if (mTabSelectorTabObserver != null) mTabSelectorTabObserver.destroy(); |
| 301 mOverviewModeObservers.clear(); |
| 302 |
| 303 if (mOverviewLayout != null) { |
| 304 mOverviewLayout.destroy(); |
| 305 mOverviewLayout = null; |
| 306 } |
| 307 mOverviewListLayout.destroy(); |
| 308 mToolbarSwipeLayout.destroy(); |
| 309 } |
| 310 |
| 311 @Override |
| 312 protected void addGlobalSceneOverlay(SceneOverlay helper) { |
| 313 super.addGlobalSceneOverlay(helper); |
| 314 mOverviewListLayout.addSceneOverlay(helper); |
| 315 mToolbarSwipeLayout.addSceneOverlay(helper); |
| 316 if (mOverviewLayout != null) mOverviewLayout.addSceneOverlay(helper); |
| 317 } |
| 318 |
| 319 /** |
| 320 * Meant to be overridden by child classes for when they need to extend the
toolbar side swipe |
| 321 * functionality. |
| 322 * @param provider A {@link LayoutProvider} instance. |
| 323 * @return A {@link ToolbarSwipeHandler} instance that will be used
by internal layouts. |
| 324 */ |
| 325 protected ToolbarSwipeHandler createToolbarSwipeHandler(LayoutProvider provi
der) { |
| 326 return new ToolbarSwipeHandler(provider); |
| 327 } |
| 328 |
| 329 /** |
| 330 * Simulates a click on the view at the specified pixel offset |
| 331 * from the top left of the view. |
| 332 * This is used by UI tests. |
| 333 * @param x Coordinate of the click in dp. |
| 334 * @param y Coordinate of the click in dp. |
| 335 */ |
| 336 @VisibleForTesting |
| 337 public void simulateClick(float x, float y) { |
| 338 if (getActiveLayout() != null) getActiveLayout().click(time(), x, y); |
| 339 } |
| 340 |
| 341 /** |
| 342 * Simulates a drag and issues Up-event to commit the drag. |
| 343 * @param x Coordinate to start the Drag from in dp. |
| 344 * @param y Coordinate to start the Drag from in dp. |
| 345 * @param dX Amount of drag in X direction in dp. |
| 346 * @param dY Amount of drag in Y direction in dp. |
| 347 */ |
| 348 @VisibleForTesting |
| 349 public void simulateDrag(float x, float y, float dX, float dY) { |
| 350 if (getActiveLayout() != null) { |
| 351 getActiveLayout().onDown(0, x, y); |
| 352 getActiveLayout().drag(0, x, y, dX, dY); |
| 353 getActiveLayout().onUpOrCancel(time()); |
| 354 } |
| 355 } |
| 356 |
| 357 private boolean isOverviewLayout(Layout layout) { |
| 358 return layout != null && (layout == mOverviewLayout || layout == mOvervi
ewListLayout); |
| 359 } |
| 360 |
| 361 @Override |
| 362 protected void startShowing(Layout layout, boolean animate) { |
| 363 mCreatingNtp = false; |
| 364 super.startShowing(layout, animate); |
| 365 |
| 366 Layout layoutBeingShown = getActiveLayout(); |
| 367 |
| 368 // Check if a layout is showing that should hide the contextual search b
ar. |
| 369 if (mContextualSearchDelegate != null |
| 370 && (isOverviewLayout(layoutBeingShown) |
| 371 || layoutBeingShown == mToolbarSwipeLayout)) { |
| 372 mContextualSearchDelegate.dismissContextualSearchBar(); |
| 373 } |
| 374 |
| 375 // Check if we should notify OverviewModeObservers. |
| 376 if (isOverviewLayout(layoutBeingShown)) { |
| 377 boolean showToolbar = |
| 378 !mEnableAnimations || getTabModelSelector().getCurrentModel(
).getCount() <= 0; |
| 379 for (OverviewModeObserver observer : mOverviewModeObservers) { |
| 380 observer.onOverviewModeStartedShowing(showToolbar); |
| 381 } |
| 382 } |
| 383 } |
| 384 |
| 385 @Override |
| 386 public void startHiding(int nextTabId, boolean hintAtTabSelection) { |
| 387 super.startHiding(nextTabId, hintAtTabSelection); |
| 388 |
| 389 Layout layoutBeingHidden = getActiveLayout(); |
| 390 if (isOverviewLayout(layoutBeingHidden)) { |
| 391 boolean showToolbar = true; |
| 392 if (mEnableAnimations && layoutBeingHidden == mOverviewLayout) { |
| 393 final LayoutTab tab = layoutBeingHidden.getLayoutTab(nextTabId); |
| 394 showToolbar = tab != null ? !tab.showToolbar() : true; |
| 395 } |
| 396 |
| 397 boolean creatingNtp = layoutBeingHidden == mOverviewLayout && mCreat
ingNtp; |
| 398 |
| 399 for (OverviewModeObserver observer : mOverviewModeObservers) { |
| 400 observer.onOverviewModeStartedHiding(showToolbar, creatingNtp); |
| 401 } |
| 402 } |
| 403 } |
| 404 |
| 405 @Override |
| 406 public void doneShowing() { |
| 407 super.doneShowing(); |
| 408 |
| 409 if (isOverviewLayout(getActiveLayout())) { |
| 410 for (OverviewModeObserver observer : mOverviewModeObservers) { |
| 411 observer.onOverviewModeFinishedShowing(); |
| 412 } |
| 413 } |
| 414 } |
| 415 |
| 416 @Override |
| 417 public void doneHiding() { |
| 418 Layout layoutBeingHidden = getActiveLayout(); |
| 419 |
| 420 if (getNextLayout() == getDefaultLayout()) { |
| 421 Tab tab = getTabModelSelector() != null ? getTabModelSelector().getC
urrentTab() : null; |
| 422 emptyCachesExcept(tab != null ? tab.getId() : Tab.INVALID_TAB_ID); |
| 423 } |
| 424 |
| 425 super.doneHiding(); |
| 426 |
| 427 if (isOverviewLayout(layoutBeingHidden)) { |
| 428 for (OverviewModeObserver observer : mOverviewModeObservers) { |
| 429 observer.onOverviewModeFinishedHiding(); |
| 430 } |
| 431 } |
| 432 } |
| 433 |
| 434 @VisibleForTesting |
| 435 public void tabSelected(int tabId, int prevId, boolean incognito) { |
| 436 // Update the model here so we properly set the right selected TabModel. |
| 437 if (getActiveLayout() != null) { |
| 438 getActiveLayout().onTabSelected(time(), tabId, prevId, incognito); |
| 439 } |
| 440 } |
| 441 |
| 442 /** |
| 443 * Should be called when a tab created event is triggered. |
| 444 * @param id The id of the tab that was created. |
| 445 * @param sourceId The id of the creating tab if any. |
| 446 * @param launchType How the tab was launched. |
| 447 * @param incognito Whether or not the created tab is incognito. |
| 448 * @param willBeSelected Whether or not the created tab will be selected. |
| 449 * @param originX The x coordinate of the action that created this ta
b in dp. |
| 450 * @param originY The y coordinate of the action that created this ta
b in dp. |
| 451 */ |
| 452 protected void tabCreated(int id, int sourceId, TabLaunchType launchType, bo
olean incognito, |
| 453 boolean willBeSelected, float originX, float originY) { |
| 454 Tab newTab = TabModelUtils.getTabById(getTabModelSelector().getModel(inc
ognito), id); |
| 455 mCreatingNtp = newTab != null && newTab.isNativePage(); |
| 456 |
| 457 int newIndex = TabModelUtils.getTabIndexById(getTabModelSelector().getMo
del(incognito), id); |
| 458 getActiveLayout().onTabCreated( |
| 459 time(), id, newIndex, sourceId, incognito, !willBeSelected, orig
inX, originY); |
| 460 } |
| 461 |
| 462 /** |
| 463 * Should be called when a tab creating event is triggered (called before th
e tab is done being |
| 464 * created). |
| 465 * @param sourceId The id of the creating tab if any. |
| 466 * @param url The url of the created tab. |
| 467 * @param isIncognito Whether or not created tab will be incognito. |
| 468 */ |
| 469 protected void tabCreating(int sourceId, String url, boolean isIncognito) { |
| 470 if (getActiveLayout() != null) getActiveLayout().onTabCreating(sourceId)
; |
| 471 } |
| 472 |
| 473 /** |
| 474 * Should be called when a tab closed event is triggered. |
| 475 * @param id The id of the closed tab. |
| 476 * @param nextId The id of the next tab that will be visible, if any. |
| 477 * @param incognito Whether or not the closed tab is incognito. |
| 478 */ |
| 479 protected void tabClosed(int id, int nextId, boolean incognito) { |
| 480 if (getActiveLayout() != null) getActiveLayout().onTabClosed(time(), id,
nextId, incognito); |
| 481 } |
| 482 |
| 483 private void tabClosed(Tab tab) { |
| 484 Tab currentTab = |
| 485 getTabModelSelector() != null ? getTabModelSelector().getCurrent
Tab() : null; |
| 486 int nextTabId = currentTab != null ? currentTab.getId() : Tab.INVALID_TA
B_ID; |
| 487 tabClosed(tab.getId(), nextTabId, tab.isIncognito()); |
| 488 } |
| 489 |
| 490 /** |
| 491 * Called when a tab closure has been committed and all tab cleanup should h
appen. |
| 492 * @param id The id of the closed tab. |
| 493 * @param incognito Whether or not the closed tab is incognito. |
| 494 */ |
| 495 protected void tabClosureCommitted(int id, boolean incognito) { |
| 496 if (getActiveLayout() != null) { |
| 497 getActiveLayout().onTabClosureCommitted(time(), id, incognito); |
| 498 } |
| 499 } |
| 500 |
| 501 @Override |
| 502 public boolean closeAllTabsRequest(boolean incognito) { |
| 503 if (!isOverviewLayout(getActiveLayout()) || !getActiveLayout().handlesCl
oseAll()) { |
| 504 return false; |
| 505 } |
| 506 getActiveLayout().onTabsAllClosing(time(), incognito); |
| 507 return true; |
| 508 } |
| 509 |
| 510 /** |
| 511 * Called when the selected tab model has switched. |
| 512 * @param incognito Whether or not the new current tab model is incognito. |
| 513 */ |
| 514 protected void tabModelSwitched(boolean incognito) { |
| 515 if (getActiveLayout() != null) getActiveLayout().onTabModelSwitched(inco
gnito); |
| 516 } |
| 517 |
| 518 private void tabMoved(int id, int oldIndex, int newIndex, boolean incognito)
{ |
| 519 if (getActiveLayout() != null) { |
| 520 getActiveLayout().onTabMoved(time(), id, oldIndex, newIndex, incogni
to); |
| 521 } |
| 522 } |
| 523 |
| 524 private void tabPageLoadStarted(int id, boolean incognito) { |
| 525 if (getActiveLayout() != null) getActiveLayout().onTabPageLoadStarted(id
, incognito); |
| 526 } |
| 527 |
| 528 private void tabPageLoadFinished(int id, boolean incognito) { |
| 529 if (getActiveLayout() != null) getActiveLayout().onTabPageLoadFinished(i
d, incognito); |
| 530 } |
| 531 |
| 532 private void tabLoadStarted(int id, boolean incognito) { |
| 533 if (getActiveLayout() != null) getActiveLayout().onTabLoadStarted(id, in
cognito); |
| 534 } |
| 535 |
| 536 private void tabLoadFinished(int id, boolean incognito) { |
| 537 if (getActiveLayout() != null) getActiveLayout().onTabLoadFinished(id, i
ncognito); |
| 538 } |
| 539 |
| 540 private void tabClosureCancelled(int id, boolean incognito) { |
| 541 if (getActiveLayout() != null) { |
| 542 getActiveLayout().onTabClosureCancelled(time(), id, incognito); |
| 543 } |
| 544 } |
| 545 |
| 546 @Override |
| 547 public void initLayoutTabFromHost(int tabId) { |
| 548 super.initLayoutTabFromHost(tabId); |
| 549 |
| 550 if (getTabModelSelector() == null || getActiveLayout() == null) return; |
| 551 |
| 552 TabModelSelector selector = getTabModelSelector(); |
| 553 Tab tab = selector.getTabById(tabId); |
| 554 if (tab == null) return; |
| 555 |
| 556 LayoutTab layoutTab = getExistingLayoutTab(tabId); |
| 557 if (layoutTab == null) return; |
| 558 |
| 559 if (mTitleCache != null && layoutTab.isTitleNeeded()) { |
| 560 mTitleCache.put(tabId, getTitleBitmap(tab), getFaviconBitmap(tab), t
ab.isIncognito(), |
| 561 tab.isTitleDirectionRtl()); |
| 562 } |
| 563 } |
| 564 |
| 565 /** |
| 566 * Builds a title bitmap for a {@link Tab}. This function does not do anythi
ng in the |
| 567 * general case because only the phone need to bake special resource. |
| 568 * |
| 569 * @param tab The tab to build the title bitmap for. |
| 570 * @return The Title bitmap |
| 571 */ |
| 572 protected Bitmap getTitleBitmap(Tab tab) { |
| 573 TitleBitmapFactory titleBitmapFactory = |
| 574 tab.isIncognito() ? mIncognitoTitleBitmapFactory : mStandardTitl
eBitmapFactory; |
| 575 |
| 576 return titleBitmapFactory.getTitleBitmap(mHost.getContext(), getTitleFor
Tab(tab)); |
| 577 } |
| 578 |
| 579 /** |
| 580 * Comes up with a valid title to return for a tab. |
| 581 * @param tab The {@link Tab} to build a title for. |
| 582 * @return The title to use. |
| 583 */ |
| 584 protected String getTitleForTab(Tab tab) { |
| 585 String title = tab.getTitle(); |
| 586 if (TextUtils.isEmpty(title)) title = tab.getUrl(); |
| 587 return title; |
| 588 } |
| 589 |
| 590 /** |
| 591 * Builds a favicon bitmap for a {@link Tab}. This function does not do anyt
hing in the |
| 592 * general case because only the phone need to bake special texture. |
| 593 * |
| 594 * @param tab The tab to build the title bitmap for. |
| 595 * @return The Favicon bitmap |
| 596 */ |
| 597 protected Bitmap getFaviconBitmap(Tab tab) { |
| 598 TitleBitmapFactory titleBitmapFactory = |
| 599 tab.isIncognito() ? mIncognitoTitleBitmapFactory : mStandardTitl
eBitmapFactory; |
| 600 return titleBitmapFactory.getFaviconBitmap(mHost.getContext(), tab.getFa
vicon()); |
| 601 } |
| 602 |
| 603 /** |
| 604 * @return The {@link OverviewListLayout} managed by this class. |
| 605 */ |
| 606 @VisibleForTesting |
| 607 public Layout getOverviewListLayout() { |
| 608 return mOverviewListLayout; |
| 609 } |
| 610 |
| 611 /** |
| 612 * @return The overview layout {@link Layout} managed by this class. |
| 613 */ |
| 614 @VisibleForTesting |
| 615 public Layout getOverviewLayout() { |
| 616 return mOverviewLayout; |
| 617 } |
| 618 |
| 619 /** |
| 620 * @return The {@link StripLayoutHelperManager} managed by this class. |
| 621 */ |
| 622 @VisibleForTesting |
| 623 public StripLayoutHelperManager getStripLayoutHelperManager() { |
| 624 return null; |
| 625 } |
| 626 |
| 627 /** |
| 628 * @return Whether or not to use the accessibility layout. |
| 629 */ |
| 630 protected boolean useAccessibilityLayout() { |
| 631 return DeviceClassManager.isAccessibilityModeEnabled(mHost.getContext()) |
| 632 || DeviceClassManager.enableAccessibilityLayout(); |
| 633 } |
| 634 |
| 635 /** |
| 636 * Show the overview {@link Layout}. This is generally a {@link Layout} tha
t visibly represents |
| 637 * all of the {@link Tab}s opened by the user. |
| 638 * @param animate Whether or not to animate the transition to overview mode. |
| 639 */ |
| 640 public void showOverview(boolean animate) { |
| 641 boolean useAccessibility = useAccessibilityLayout(); |
| 642 |
| 643 boolean accessibilityIsVisible = |
| 644 useAccessibility && getActiveLayout() == mOverviewListLayout; |
| 645 boolean normalIsVisible = getActiveLayout() == mOverviewLayout && mOverv
iewLayout != null; |
| 646 |
| 647 // We only want to use the AccessibilityOverviewLayout if the following
are all valid: |
| 648 // 1. We're already showing the AccessibilityOverviewLayout OR we're usi
ng accessibility. |
| 649 // 2. We're not already showing the normal OverviewLayout (or we are on
a tablet, in which |
| 650 // case the normal layout is always visible). |
| 651 if ((accessibilityIsVisible || useAccessibility) && !normalIsVisible) { |
| 652 startShowing(mOverviewListLayout, animate); |
| 653 } else if (mOverviewLayout != null) { |
| 654 startShowing(mOverviewLayout, animate); |
| 655 } |
| 656 } |
| 657 |
| 658 /** |
| 659 * Hides the current {@link Layout}, returning to the default {@link Layout}
. |
| 660 * @param animate Whether or not to animate the transition to the default {@
link Layout}. |
| 661 */ |
| 662 public void hideOverview(boolean animate) { |
| 663 Layout activeLayout = getActiveLayout(); |
| 664 if (activeLayout != null && !activeLayout.isHiding()) { |
| 665 if (animate) { |
| 666 activeLayout.onTabSelecting(time(), Tab.INVALID_TAB_ID); |
| 667 } else { |
| 668 startHiding(Tab.INVALID_TAB_ID, false); |
| 669 doneHiding(); |
| 670 } |
| 671 } |
| 672 } |
| 673 |
| 674 /** |
| 675 * @param enabled Whether or not to allow model-reactive animations (tab cre
ation, closing, |
| 676 * etc.). |
| 677 */ |
| 678 public void setEnableAnimations(boolean enabled) { |
| 679 mEnableAnimations = enabled; |
| 680 } |
| 681 |
| 682 @Override |
| 683 public boolean overviewVisible() { |
| 684 Layout activeLayout = getActiveLayout(); |
| 685 return isOverviewLayout(activeLayout) && !activeLayout.isHiding(); |
| 686 } |
| 687 |
| 688 @Override |
| 689 public void addOverviewModeObserver(OverviewModeObserver listener) { |
| 690 mOverviewModeObservers.addObserver(listener); |
| 691 } |
| 692 |
| 693 @Override |
| 694 public void removeOverviewModeObserver(OverviewModeObserver listener) { |
| 695 mOverviewModeObservers.removeObserver(listener); |
| 696 } |
| 697 |
| 698 /** |
| 699 * A {@link EdgeSwipeHandler} meant to respond to edge events for the toolba
r. |
| 700 */ |
| 701 protected class ToolbarSwipeHandler extends EdgeSwipeHandlerLayoutDelegate { |
| 702 /** |
| 703 * Creates an instance of the {@link ToolbarSwipeHandler}. |
| 704 * @param provider A {@link LayoutProvider} instance. |
| 705 */ |
| 706 public ToolbarSwipeHandler(LayoutProvider provider) { |
| 707 super(provider); |
| 708 } |
| 709 |
| 710 @Override |
| 711 public void swipeStarted(ScrollDirection direction, float x, float y) { |
| 712 if (direction == ScrollDirection.DOWN) { |
| 713 startShowing(mOverviewLayout, true); |
| 714 super.swipeStarted(direction, x, y); |
| 715 } else if (direction == ScrollDirection.LEFT || direction == ScrollD
irection.RIGHT) { |
| 716 startShowing(mToolbarSwipeLayout, true); |
| 717 super.swipeStarted(direction, x, y); |
| 718 } |
| 719 } |
| 720 |
| 721 @Override |
| 722 public boolean isSwipeEnabled(ScrollDirection direction) { |
| 723 FullscreenManager manager = mHost.getFullscreenManager(); |
| 724 if (getActiveLayout() != mStaticLayout |
| 725 || !DeviceClassManager.enableToolbarSwipe( |
| 726 FeatureUtilities.isDocumentMode(mHost.getContext(
))) |
| 727 || (manager != null && manager.getPersistentFullscreenMode()
)) { |
| 728 return false; |
| 729 } |
| 730 |
| 731 boolean isAccessibility = |
| 732 DeviceClassManager.isAccessibilityModeEnabled(mHost.getConte
xt()); |
| 733 return direction == ScrollDirection.LEFT || direction == ScrollDirec
tion.RIGHT |
| 734 || (direction == ScrollDirection.DOWN && mOverviewLayout !=
null |
| 735 && !isAccessibility); |
| 736 } |
| 737 } |
| 738 |
| 739 /** |
| 740 * @param id The id of the {@link Tab} to search for. |
| 741 * @return A {@link Tab} instance or {@code null} if it could be found. |
| 742 */ |
| 743 protected Tab getTabById(int id) { |
| 744 TabModelSelector selector = getTabModelSelector(); |
| 745 return selector == null ? null : selector.getTabById(id); |
| 746 } |
| 747 } |
OLD | NEW |