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