use of org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView in project AndroidChromium by JackyAndroid.
the class NewTabPageView method initialize.
/**
* Initializes the NTP. This must be called immediately after inflation, before this object is
* used in any other way.
*
* @param manager NewTabPageManager used to perform various actions when the user interacts
* with the page.
* @param searchProviderHasLogo Whether the search provider has a logo.
* @param scrollPosition The adapter scroll position to initialize to.
*/
public void initialize(NewTabPageManager manager, boolean searchProviderHasLogo, int scrollPosition) {
mManager = manager;
mUiConfig = new UiConfig(this);
ViewStub stub = (ViewStub) findViewById(R.id.new_tab_page_layout_stub);
mUseCardsUi = manager.getSuggestionsSource() != null;
if (mUseCardsUi) {
stub.setLayoutResource(R.layout.new_tab_page_recycler_view);
mRecyclerView = (NewTabPageRecyclerView) stub.inflate();
// Don't attach now, the recyclerView itself will determine when to do it.
mNewTabPageLayout = (NewTabPageLayout) LayoutInflater.from(getContext()).inflate(R.layout.new_tab_page_layout, mRecyclerView, false);
mNewTabPageLayout.setUseCardsUiEnabled(mUseCardsUi);
mRecyclerView.setAboveTheFoldView(mNewTabPageLayout);
// Tailor the LayoutParams for the snippets UI, as the configuration in the XML is
// made for the ScrollView UI.
ViewGroup.LayoutParams params = mNewTabPageLayout.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mRecyclerView.setItemAnimator(new DefaultItemAnimator() {
@Override
public void onAnimationFinished(ViewHolder viewHolder) {
super.onAnimationFinished(viewHolder);
// When removing sections, because the animations are all translations, the
// scroll events don't fire and we can get in the situation where the toolbar
// buttons disappear.
updateSearchBoxOnScroll();
}
});
} else {
stub.setLayoutResource(R.layout.new_tab_page_scroll_view);
mScrollView = (NewTabPageScrollView) stub.inflate();
mScrollView.setBackgroundColor(NtpStyleUtils.getBackgroundColorResource(getResources(), false));
mScrollView.enableBottomShadow(SHADOW_COLOR);
mNewTabPageLayout = (NewTabPageLayout) findViewById(R.id.ntp_content);
}
mMostVisitedDesign = new MostVisitedDesign(getContext());
mMostVisitedLayout = (MostVisitedLayout) mNewTabPageLayout.findViewById(R.id.most_visited_layout);
mMostVisitedDesign.initMostVisitedLayout(searchProviderHasLogo);
mSearchProviderLogoView = (LogoView) mNewTabPageLayout.findViewById(R.id.search_provider_logo);
mSearchBoxView = (ViewGroup) mNewTabPageLayout.findViewById(R.id.search_box);
mNoSearchLogoSpacer = mNewTabPageLayout.findViewById(R.id.no_search_logo_spacer);
initializeSearchBoxTextView();
initializeVoiceSearchButton();
initializeBottomToolbar();
mNewTabPageLayout.addOnLayoutChangeListener(this);
setSearchProviderHasLogo(searchProviderHasLogo);
mPendingLoadTasks++;
mManager.setMostVisitedURLsObserver(this, mMostVisitedDesign.getNumberOfTiles(searchProviderHasLogo));
// Set up snippets
if (mUseCardsUi) {
mNewTabPageAdapter = NewTabPageAdapter.create(mManager, mNewTabPageLayout, mUiConfig);
mRecyclerView.setAdapter(mNewTabPageAdapter);
mRecyclerView.scrollToPosition(scrollPosition);
if (CardsVariationParameters.isScrollBelowTheFoldEnabled()) {
int searchBoxHeight = NtpStyleUtils.getSearchBoxHeight(getResources());
mRecyclerView.getLinearLayoutManager().scrollToPositionWithOffset(mNewTabPageAdapter.getFirstHeaderPosition(), searchBoxHeight);
}
// Set up swipe-to-dismiss
ItemTouchHelper helper = new ItemTouchHelper(mNewTabPageAdapter.getItemTouchCallbacks());
helper.attachToRecyclerView(mRecyclerView);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
private boolean mScrolledOnce = false;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState != RecyclerView.SCROLL_STATE_DRAGGING)
return;
RecordUserAction.record("MobileNTP.Snippets.Scrolled");
if (mScrolledOnce)
return;
mScrolledOnce = true;
NewTabPageUma.recordSnippetAction(NewTabPageUma.SNIPPETS_ACTION_SCROLLED);
}
});
initializeSearchBoxRecyclerViewScrollHandling();
} else {
initializeSearchBoxScrollHandling();
}
}
use of org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView in project AndroidChromium by JackyAndroid.
the class SnippetArticleViewHolder method createContextMenu.
@Override
protected void createContextMenu(ContextMenu menu) {
RecordHistogram.recordSparseSlowlyHistogram("NewTabPage.Snippets.CardLongPressed", mArticle.mPosition);
mArticle.recordAgeAndScore("NewTabPage.Snippets.CardLongPressed");
OnMenuItemClickListener listener = new ContextMenuItemClickListener(mArticle, mNewTabPageManager, getRecyclerView());
// Create a context menu akin to the one shown for MostVisitedItems.
if (mNewTabPageManager.isOpenInNewWindowEnabled()) {
addContextMenuItem(menu, ID_OPEN_IN_NEW_WINDOW, R.string.contextmenu_open_in_other_window, listener);
}
addContextMenuItem(menu, ID_OPEN_IN_NEW_TAB, R.string.contextmenu_open_in_new_tab, listener);
if (mNewTabPageManager.isOpenInIncognitoEnabled()) {
addContextMenuItem(menu, ID_OPEN_IN_INCOGNITO_TAB, R.string.contextmenu_open_in_incognito_tab, listener);
}
// TODO(peconn): Only show 'Save for Offline' for appropriate snippet types.
if (SnippetsConfig.isSaveToOfflineEnabled() && OfflinePageBridge.canSavePage(mArticle.mUrl)) {
addContextMenuItem(menu, ID_SAVE_FOR_OFFLINE, R.string.contextmenu_save_link, listener);
}
addContextMenuItem(menu, ID_REMOVE, R.string.remove, listener);
// Disable touch events on the RecyclerView while the context menu is open. This is to
// prevent the user long pressing to get the context menu then on the same press scrolling
// or swiping to dismiss an item (eg. https://crbug.com/638854, 638555, 636296)
final NewTabPageRecyclerView recyclerView = (NewTabPageRecyclerView) itemView.getParent();
recyclerView.setTouchEnabled(false);
mNewTabPageManager.addContextMenuCloseCallback(new Callback<Menu>() {
@Override
public void onResult(Menu result) {
recyclerView.setTouchEnabled(true);
mNewTabPageManager.removeContextMenuCloseCallback(this);
}
});
}
use of org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView in project AndroidChromium by JackyAndroid.
the class NewTabPageLayout method measureWithCardsUiEnabled.
/**
* Performs layout measurements for when the cards ui is enabled.
*/
private void measureWithCardsUiEnabled(int widthMeasureSpec, int heightMeasureSpec) {
assert mCardsUiEnabled;
mLogoSpacer.setVisibility(View.GONE);
mSearchBoxSpacer.setVisibility(View.GONE);
// Remove the extra spacing before measuring because it might not be needed anymore.
mMostVisitedLayout.setExtraVerticalSpacing(0);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
boolean hasSpaceForPeekingCard = false;
int spaceToFill = mParentViewportHeight - mPeekingCardHeight - mTabStripHeight;
@NTPLayoutResult int layoutResult;
// We need to make sure we have just enough space to show the peeking card.
if (getMeasuredHeight() > spaceToFill) {
layoutResult = NewTabPageUma.NTP_LAYOUT_DOES_NOT_FIT;
// and let MostVisited get cut to make it clear that the page is scrollable.
if (mMostVisitedLayout.getChildCount() > 0) {
// Add some extra space if needed (the 'bleed' is the amount of the layout that
// will be cut off by the bottom of the screen).
int currentBleed = getMeasuredHeight() - mParentViewportHeight - mTabStripHeight;
int minimumBleed = (int) (mMostVisitedLayout.getChildAt(0).getMeasuredHeight() * 0.44);
if (currentBleed < minimumBleed) {
int extraBleed = minimumBleed - currentBleed;
mLogoSpacer.getLayoutParams().height = (int) (extraBleed * 0.25);
mLogoSpacer.setVisibility(View.INVISIBLE);
mSearchBoxSpacer.getLayoutParams().height = (int) (extraBleed * 0.25);
mSearchBoxSpacer.setVisibility(View.INVISIBLE);
mMostVisitedLayout.setExtraVerticalSpacing((int) (extraBleed * 0.5));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
layoutResult = NewTabPageUma.NTP_LAYOUT_DOES_NOT_FIT_PUSH_MOST_LIKELY;
}
}
} else {
hasSpaceForPeekingCard = true;
// If there is enough space, reduce the space we are going to fill.
if (mFieldTrialLayoutAdjustment != 0f) {
if (getMeasuredHeight() < spaceToFill - mFieldTrialLayoutAdjustment) {
spaceToFill -= mFieldTrialLayoutAdjustment;
layoutResult = NewTabPageUma.NTP_LAYOUT_FITS_WITH_FIELD_TRIAL;
} else {
layoutResult = NewTabPageUma.NTP_LAYOUT_FITS_WITHOUT_FIELD_TRIAL;
}
} else {
layoutResult = NewTabPageUma.NTP_LAYOUT_FITS_NO_FIELD_TRIAL;
}
// Call super.onMeasure with mode EXACTLY and the target height to allow the top
// spacer (which has a weight of 1) to grow and take up the remaining space.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(spaceToFill, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
distributeExtraSpace(mTopSpacer.getMeasuredHeight());
}
assert getParent() instanceof NewTabPageRecyclerView;
NewTabPageRecyclerView recyclerView = (NewTabPageRecyclerView) getParent();
recyclerView.setHasSpaceForPeekingCard(hasSpaceForPeekingCard);
// contents. We want to record what the user sees when the layout has stabilized.
if (mMostVisitedLayout.getChildCount() > 0 && !mLayoutResultRecorded) {
mLayoutResultRecorded = true;
NewTabPageUma.recordNTPLayoutResult(layoutResult);
}
}
Aggregations