Commit eb808654 authored by Matthew Jones's avatar Matthew Jones Committed by Commit Bot

Convert BottomSheetController to an ActivityTabProvider user

This patch replaces the need for several larger objects in the
BottomSheetController (LayoutManager and TabModelSelector) in favor
of instead using the ActivityTabProvider. This simplifies the logic
in this class.

Change-Id: I001593cfccaadbf1c0e60ceaa7231e203c56a338
Reviewed-on: https://chromium-review.googlesource.com/1211447Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589530}
parent feaba58c
......@@ -1403,8 +1403,8 @@ public abstract class ChromeActivity extends AsyncInitializationActivity
((BottomContainer) findViewById(R.id.bottom_container)).setBottomSheet(mBottomSheet);
mBottomSheetController = new BottomSheetController(this, getTabModelSelector(),
getCompositorViewHolder().getLayoutManager(), mScrimView, mBottomSheet,
mBottomSheetController = new BottomSheetController(this, mActivityTabProvider,
mScrimView, mBottomSheet,
getCompositorViewHolder().getLayoutManager().getOverlayPanelManager(),
!ChromeFeatureList.isEnabled(ChromeFeatureList.CONTEXTUAL_SUGGESTIONS_BUTTON));
......
......@@ -10,22 +10,15 @@ import android.view.View;
import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationStatus;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.ActivityTabProvider.HintlessActivityTabObserver;
import org.chromium.chrome.browser.compositor.bottombar.OverlayPanel;
import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManager;
import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManager.OverlayPanelManagerObserver;
import org.chromium.chrome.browser.compositor.layouts.Layout;
import org.chromium.chrome.browser.compositor.layouts.LayoutManager;
import org.chromium.chrome.browser.compositor.layouts.SceneChangeObserver;
import org.chromium.chrome.browser.compositor.layouts.StaticLayout;
import org.chromium.chrome.browser.compositor.layouts.phone.SimpleAnimationLayout;
import org.chromium.chrome.browser.snackbar.SnackbarManager;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver;
import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.tabmodel.TabModelObserver;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabObserver;
import org.chromium.chrome.browser.tab.TabObserver;
import org.chromium.chrome.browser.widget.ScrimView;
import org.chromium.chrome.browser.widget.ScrimView.ScrimObserver;
import org.chromium.chrome.browser.widget.ScrimView.ScrimParams;
......@@ -49,9 +42,6 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
/** The parameters that controll how the scrim behaves while the sheet is open. */
private final ScrimParams mScrimParams;
/** A handle to the {@link LayoutManager} to determine what state the browser is in. */
private final LayoutManager mLayoutManager;
/** A handle to the {@link BottomSheet} that this class controls. */
private final BottomSheet mBottomSheet;
......@@ -79,21 +69,28 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
/** Whether the bottom sheet should be suppressed when Contextual Search is showing. */
private boolean mSuppressSheetForContextualSearch;
/** A means for getting the activity's current tab and observing change events. */
private ActivityTabProvider mTabProvider;
/** The last known activity tab, if available. */
private Tab mLastActivityTab;
/**
* Build a new controller of the bottom sheet.
* @param tabModelSelector A tab model selector to track events on tabs open in the browser.
* @param layoutManager A layout manager for detecting changes in the active layout.
* @param activity An activity for context.
* @param activityTabProvider The provider of the activity's current tab.
* @param scrim The scrim that shows when the bottom sheet is opened.
* @param bottomSheet The bottom sheet that this class will be controlling.
* @param overlayManager The manager for overlay panels to attach listeners to.
* @param suppressSheetForContextualSearch Whether the bottom sheet should be suppressed when
* Contextual Search is showing.
*/
public BottomSheetController(final Activity activity, final TabModelSelector tabModelSelector,
final LayoutManager layoutManager, final ScrimView scrim, BottomSheet bottomSheet,
OverlayPanelManager overlayManager, boolean suppressSheetForContextualSearch) {
public BottomSheetController(final Activity activity,
final ActivityTabProvider activityTabProvider, final ScrimView scrim,
BottomSheet bottomSheet, OverlayPanelManager overlayManager,
boolean suppressSheetForContextualSearch) {
mBottomSheet = bottomSheet;
mLayoutManager = layoutManager;
mTabProvider = activityTabProvider;
mOverlayPanelManager = overlayManager;
mSuppressSheetForContextualSearch = suppressSheetForContextualSearch;
mSnackbarManager = new SnackbarManager(
......@@ -102,66 +99,43 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
ApplicationStatus.registerStateListenerForActivity(this, activity);
mFullShowRequestedSet = new HashSet<>();
// Watch for navigation and tab switching that close the sheet.
new TabModelSelectorTabObserver(tabModelSelector) {
// Initialize the queue with a comparator that checks content priority.
mContentQueue = new PriorityQueue<>(INITIAL_QUEUE_CAPACITY,
(content1, content2) -> content2.getPriority() - content1.getPriority());
final TabObserver tabObserver = new EmptyTabObserver() {
@Override
public void onPageLoadStarted(Tab tab, String url) {
if (tab != tabModelSelector.getCurrentTab()) return;
clearRequestsAndHide();
}
@Override
public void onCrash(Tab tab, boolean sadTabShown) {
if (tab != tabModelSelector.getCurrentTab()) return;
clearRequestsAndHide();
}
};
final TabModelObserver tabSelectionObserver = new EmptyTabModelObserver() {
/** The currently active tab. */
private Tab mCurrentTab = tabModelSelector.getCurrentTab();
@Override
public void didSelectTab(Tab tab, @TabModel.TabSelectionType int type, int lastId) {
if (tab == mCurrentTab) return;
mCurrentTab = tab;
clearRequestsAndHide();
}
};
tabModelSelector.getCurrentModel().addObserver(tabSelectionObserver);
tabModelSelector.addObserver(new TabModelSelectorObserver() {
@Override
public void onChange() {}
@Override
public void onNewTabCreated(Tab tab) {}
@Override
public void onTabModelSelected(TabModel newModel, TabModel oldModel) {
if (oldModel != null) oldModel.removeObserver(tabSelectionObserver);
newModel.addObserver(tabSelectionObserver);
clearRequestsAndHide();
}
@Override
public void onTabStateInitialized() {}
});
// If the layout changes (to tab switcher, toolbar swipe, etc.) hide the sheet.
mLayoutManager.addSceneChangeObserver(new SceneChangeObserver() {
mTabProvider.addObserverAndTrigger(new HintlessActivityTabObserver() {
@Override
public void onTabSelectionHinted(int tabId) {}
public void onActivityTabChanged(Tab tab) {
// Temporarily suppress the sheet if entering a state where there is no activity
// tab.
if (tab == null) {
suppressSheet(StateChangeReason.COMPOSITED_UI);
return;
}
@Override
public void onSceneChange(Layout layout) {
// If the tab did not change, reshow the existing content. Once the tab actually
// changes, existing content and requests will be cleared.
if (canShowInLayout(layout)) {
// If refocusing the same tab, simply unsupress the sheet.
if (mLastActivityTab == tab) {
unsuppressSheet();
} else if (!canShowInLayout(layout)) {
suppressSheet(StateChangeReason.COMPOSITED_UI);
return;
}
// Move the observer to the new activity tab and clear the sheet.
if (mLastActivityTab != null) mLastActivityTab.removeObserver(tabObserver);
mLastActivityTab = tab;
mLastActivityTab.addObserver(tabObserver);
clearRequestsAndHide();
}
});
......@@ -220,10 +194,6 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
}
});
}
// Initialize the queue with a comparator that checks content priority.
mContentQueue = new PriorityQueue<>(INITIAL_QUEUE_CAPACITY,
(content1, content2) -> content2.getPriority() - content1.getPriority());
}
/**
......@@ -241,8 +211,8 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
* the browser (i.e. the tab switcher may be showing).
*/
private void unsuppressSheet() {
if (!mIsSuppressed || !canShowInLayout(mLayoutManager.getActiveLayout())
|| !mWasShownForCurrentTab || isOtherUIObscuring()) {
if (!mIsSuppressed || mTabProvider.getActivityTab() == null || !mWasShownForCurrentTab
|| isOtherUIObscuring()) {
return;
}
mIsSuppressed = false;
......@@ -295,7 +265,7 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
*/
private boolean loadInternal(BottomSheetContent content) {
if (content == mBottomSheet.getCurrentSheetContent()) return true;
if (!canShowInLayout(mLayoutManager.getActiveLayout())) return false;
if (mTabProvider.getActivityTab() == null) return false;
BottomSheetContent shownContent = mBottomSheet.getCurrentSheetContent();
boolean shouldSuppressExistingContent = shownContent != null
......@@ -414,14 +384,6 @@ public class BottomSheetController implements ApplicationStatus.ActivityStateLis
mIsSuppressed = false;
}
/**
* @param layout A {@link Layout} to check if the sheet can be shown in.
* @return Whether the bottom sheet can show in the specified layout.
*/
protected boolean canShowInLayout(Layout layout) {
return layout instanceof StaticLayout || layout instanceof SimpleAnimationLayout;
}
/**
* @return Whether some other UI is preventing the sheet from showing.
*/
......
......@@ -70,8 +70,8 @@ public class ScrimTest {
mScrim = activity.getScrim();
mSheetController = new BottomSheetController(activity, activity.getTabModelSelector(),
activity.getCompositorViewHolder().getLayoutManager(), mScrim, mBottomSheet,
mSheetController = new BottomSheetController(activity,
activity.getActivityTabProvider(), mScrim, mBottomSheet,
activity.getCompositorViewHolder().getLayoutManager().getOverlayPanelManager(),
true);
});
......
......@@ -71,8 +71,8 @@ public class BottomSheetControllerTest {
ScrimView scrim = new ScrimView(mActivityTestRule.getActivity(), null, coordinator);
mSheetController = new BottomSheetController(activity, activity.getTabModelSelector(),
activity.getCompositorViewHolder().getLayoutManager(), scrim, mBottomSheet,
mSheetController = new BottomSheetController(activity,
activity.getActivityTabProvider(), scrim, mBottomSheet,
activity.getCompositorViewHolder().getLayoutManager().getOverlayPanelManager(),
true);
......
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