Commit 05a37bd5 authored by Bernhard Bauer's avatar Bernhard Bauer Committed by Commit Bot

[ChromeHome] Only attach suggestions adapter to the RecyclerView while the sheet is opened.

When the bottom sheet is closed, set its adapter to null, which clears the view
(and recycles all the view holders inside it), to reduce the memory overhead of
the views (which potentially hold large bitmaps) while the sheet is closed.
The adapter itself is still around and holds on to the model, so it can be
reattached when the sheet is opened again. The model caches bitmaps only via
discardable references, which it will drop under memory pressure.

Bug: 718925
Change-Id: I58510bc831a35f15d831915ba9fc47f929253978
Reviewed-on: https://chromium-review.googlesource.com/562136
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Reviewed-by: default avatarNicolas Dossou-Gbété <dgn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485293}
parent 0b49fa16
......@@ -280,11 +280,13 @@ public class NewTabPageView extends FrameLayout implements TileGroup.Observer {
mTileGroup.startObserving(getMaxTileRows(searchProviderHasLogo) * getMaxTileColumns());
mRecyclerView.init(mUiConfig, mContextMenuManager);
// Set up snippets
NewTabPageAdapter newTabPageAdapter = new NewTabPageAdapter(mManager, mNewTabPageLayout,
mUiConfig, offlinePageBridge, mContextMenuManager, /* tileGroupDelegate = */ null);
newTabPageAdapter.refreshSuggestions();
mRecyclerView.init(mUiConfig, mContextMenuManager, newTabPageAdapter);
mRecyclerView.setAdapter(newTabPageAdapter);
mRecyclerView.getLinearLayoutManager().scrollToPosition(scrollPosition);
setupScrollHandling();
......
......@@ -256,7 +256,7 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder> implements
// We are assuming for now that the adapter is used with a single RecyclerView.
// Getting the reference as we are doing here is going to be broken if that changes.
assert mRecyclerView == null;
assert mRecyclerView == null || recyclerView == mRecyclerView;
// FindBugs chokes on the cast below when not checked, raising BC_UNCONFIRMED_CAST
assert recyclerView instanceof SuggestionsRecyclerView;
......
......@@ -80,15 +80,17 @@ public class SuggestionsBottomSheetContent implements BottomSheet.BottomSheetCon
});
UiConfig uiConfig = new UiConfig(mRecyclerView);
mRecyclerView.init(uiConfig, mContextMenuManager);
final NewTabPageAdapter adapter = new NewTabPageAdapter(mSuggestionsUiDelegate,
/* aboveTheFoldView = */ null, uiConfig, OfflinePageBridge.getForProfile(profile),
mContextMenuManager, mTileGroupDelegate);
mRecyclerView.init(uiConfig, mContextMenuManager, adapter);
mBottomSheetObserver = new SuggestionsSheetVisibilityChangeObserver(this, activity) {
@Override
public void onSheetOpened() {
mRecyclerView.setAdapter(adapter);
mRecyclerView.scrollToPosition(0);
adapter.refreshSuggestions();
mSuggestionsUiDelegate.getEventReporter().onSurfaceOpened();
......@@ -121,6 +123,12 @@ public class SuggestionsBottomSheetContent implements BottomSheet.BottomSheetCon
SuggestionsMetrics.recordSurfaceFullyVisible();
}
}
@Override
public void onSheetClosed() {
super.onSheetClosed();
mRecyclerView.setAdapter(null);
}
};
mShadowView = (FadingShadowView) mView.findViewById(R.id.shadow);
......
......@@ -192,11 +192,9 @@ public class SuggestionsRecyclerView extends RecyclerView {
super.onLayout(changed, l, t, r, b);
}
public void init(
UiConfig uiConfig, ContextMenuManager contextMenuManager, NewTabPageAdapter adapter) {
public void init(UiConfig uiConfig, ContextMenuManager contextMenuManager) {
mUiConfig = uiConfig;
mContextMenuManager = contextMenuManager;
setAdapter(adapter);
}
public NewTabPageAdapter getNewTabPageAdapter() {
......
......@@ -203,7 +203,8 @@ public class ArticleSnippetsTest {
ContextMenuManager contextMenuManager =
new ContextMenuManager(mActivityTestRule.getActivity(),
mUiDelegate.getNavigationDelegate(), touchEnabledDelegate);
mRecyclerView.init(mUiConfig, contextMenuManager, mAdapter);
mRecyclerView.init(mUiConfig, contextMenuManager);
mRecyclerView.setAdapter(mAdapter);
mSuggestion = new SnippetArticleViewHolder(
mRecyclerView, contextMenuManager, mUiDelegate, mUiConfig);
......
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