Commit e58dd02e authored by Theresa Wellington's avatar Theresa Wellington Committed by Commit Bot

[Home] Handle activity destruction in BottomSheet*

Notify BottomSheet, BottomSheetContentController, and
BottomSheetNewTabController about activity destruction and do
appropriate clean up (including ending animations).

BUG=741041

Change-Id: Iee2d185d87e520e99a865cb569cc14a006aba55f
Reviewed-on: https://chromium-review.googlesource.com/570719
Commit-Queue: Theresa <twellington@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486779}
parent ee94c3fe
......@@ -1143,6 +1143,16 @@ public abstract class ChromeActivity extends AsyncInitializationActivity
mToolbarManager = null;
}
if (mBottomSheet != null) {
mBottomSheet.destroy();
mBottomSheet = null;
}
if (mBottomSheetContentController != null) {
mBottomSheetContentController.destroy();
mBottomSheetContentController = null;
}
if (mTabModelsInitialized) {
TabModelSelector selector = getTabModelSelector();
if (selector != null) selector.destroy();
......
......@@ -245,6 +245,9 @@ public class BottomSheet
/** Whether or not the back button was used to enter the tab switcher. */
private boolean mBackButtonDismissesChrome;
/** Whether {@link #destroy()} has been called. **/
private boolean mIsDestroyed;
/**
* An interface defining content that can be displayed inside of the bottom sheet for Chrome
* Home.
......@@ -487,6 +490,16 @@ public class BottomSheet
});
}
/**
* Called when the activity containing the {@link BottomSheet} is destroyed.
*/
public void destroy() {
mIsDestroyed = true;
mIsTouchEnabled = false;
mObservers.clear();
endAnimations();
}
/**
* Handle a back press event.
* - If the navigation stack is empty, the sheet will be opened to the half state.
......@@ -530,6 +543,13 @@ public class BottomSheet
setSheetState(BottomSheet.SHEET_STATE_HALF, true);
}
/** Immediately end all animations and null the animators. */
public void endAnimations() {
if (mSettleAnimator != null) mSettleAnimator.end();
mSettleAnimator = null;
endTransitionAnimations();
}
/**
* Immediately end the bottom sheet content transition animations and null the animator.
*/
......@@ -895,6 +915,8 @@ public class BottomSheet
mContentSwapAnimatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (mIsDestroyed) return;
mSheetContent = content;
for (BottomSheetObserver o : mObservers) {
o.onSheetContentChanged(content);
......@@ -1094,6 +1116,8 @@ public class BottomSheet
mSettleAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animator) {
if (mIsDestroyed) return;
mSettleAnimator = null;
setInternalCurrentState(targetState);
mTargetState = SHEET_STATE_NONE;
......@@ -1491,12 +1515,4 @@ public class BottomSheet
mHasShownTextBubble = true;
preferences.edit().putBoolean(BOTTOM_SHEET_HELP_BUBBLE_SHOWN, true).apply();
}
/** Ends all animations. */
@VisibleForTesting
public void endAnimationsForTests() {
if (mSettleAnimator != null) mSettleAnimator.end();
mSettleAnimator = null;
endTransitionAnimations();
}
}
......@@ -34,6 +34,7 @@ import org.chromium.chrome.browser.suggestions.SuggestionsBottomSheetContent;
import org.chromium.chrome.browser.tabmodel.EmptyTabModelSelectorObserver;
import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.chrome.browser.util.MathUtils;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetContent;
......@@ -103,7 +104,7 @@ public class BottomSheetContentController extends BottomNavigationView
if (mSelectedItemId != 0 && mSelectedItemId != R.id.action_home) {
showBottomSheetContent(R.id.action_home);
} else {
clearBottomSheetContents();
clearBottomSheetContents(false);
}
// TODO(twellington): determine a policy for destroying the
// SuggestionsBottomSheetContent.
......@@ -122,7 +123,7 @@ public class BottomSheetContentController extends BottomNavigationView
}
if (mBottomSheet.getSheetState() == BottomSheet.SHEET_STATE_PEEK) {
clearBottomSheetContents();
clearBottomSheetContents(false);
}
}
......@@ -142,6 +143,7 @@ public class BottomSheetContentController extends BottomNavigationView
private boolean mShouldOpenSheetOnNextContentChange;
private PlaceholderSheetContent mPlaceholderContent;
private boolean mOmniboxHasFocus;
private TabModelSelectorObserver mTabModelSelectorObserver;
public BottomSheetContentController(Context context, AttributeSet atts) {
super(context, atts);
......@@ -149,6 +151,19 @@ public class BottomSheetContentController extends BottomNavigationView
mPlaceholderContent = new PlaceholderSheetContent(context);
}
/** Called when the activity containing the bottom sheet is destroyed. */
public void destroy() {
clearBottomSheetContents(true);
if (mPlaceholderContent != null) {
mPlaceholderContent.destroy();
mPlaceholderContent = null;
}
if (mTabModelSelector != null) {
mTabModelSelector.removeObserver(mTabModelSelectorObserver);
mTabModelSelector = null;
}
}
/**
* Initializes the {@link BottomSheetContentController}.
* @param bottomSheet The {@link BottomSheet} associated with this bottom nav.
......@@ -162,7 +177,7 @@ public class BottomSheetContentController extends BottomNavigationView
mBottomSheet.addObserver(mBottomSheetObserver);
mActivity = activity;
mTabModelSelector = tabModelSelector;
mTabModelSelector.addObserver(new EmptyTabModelSelectorObserver() {
mTabModelSelectorObserver = new EmptyTabModelSelectorObserver() {
@Override
public void onTabModelSelected(TabModel newModel, TabModel oldModel) {
updateVisuals(newModel.isIncognito());
......@@ -176,7 +191,8 @@ public class BottomSheetContentController extends BottomNavigationView
mBottomSheetContents.remove(INCOGNITO_HOME_ID);
}
}
});
};
mTabModelSelector.addObserver(mTabModelSelectorObserver);
Resources res = getContext().getResources();
mDistanceBelowToolbarPx = controlContainerHeight
......@@ -349,12 +365,14 @@ public class BottomSheetContentController extends BottomNavigationView
return mSelectedItemId;
}
public void clearBottomSheetContents() {
private void clearBottomSheetContents(boolean destroyHomeContent) {
Iterator<Entry<Integer, BottomSheetContent>> contentIterator =
mBottomSheetContents.entrySet().iterator();
while (contentIterator.hasNext()) {
Entry<Integer, BottomSheetContent> entry = contentIterator.next();
if (entry.getKey() == R.id.action_home || entry.getKey() == INCOGNITO_HOME_ID) {
if (!destroyHomeContent
&& (entry.getKey() == R.id.action_home
|| entry.getKey() == INCOGNITO_HOME_ID)) {
continue;
}
......
......@@ -275,7 +275,7 @@ public class BottomSheetBackBehaviorTest {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
mBottomSheet.endAnimationsForTests();
mBottomSheet.endAnimations();
}
});
}
......
......@@ -520,7 +520,7 @@ public class BottomSheetNewTabControllerTest {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
mBottomSheet.endAnimationsForTests();
mBottomSheet.endAnimations();
}
});
......
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