Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/ChromeHomeNewTabPage.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/ChromeHomeNewTabPage.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/ChromeHomeNewTabPage.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..97e85a600d205f74fa1c7e6899bd83b0851461dd |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/ChromeHomeNewTabPage.java |
| @@ -0,0 +1,187 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.ntp; |
| + |
| +import android.content.Context; |
| +import android.content.res.Resources; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.view.View.OnClickListener; |
| + |
| +import org.chromium.base.ApiCompatibilityUtils; |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.ChromeActivity; |
| +import org.chromium.chrome.browser.NativePage; |
| +import org.chromium.chrome.browser.UrlConstants; |
| +import org.chromium.chrome.browser.ntp.LogoBridge.Logo; |
| +import org.chromium.chrome.browser.ntp.LogoBridge.LogoObserver; |
| +import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
| +import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrlServiceObserver; |
| +import org.chromium.chrome.browser.tab.Tab; |
| +import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver; |
| +import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; |
| +import org.chromium.chrome.browser.tabmodel.TabModelObserver; |
| +import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| +import org.chromium.chrome.browser.widget.BottomSheet; |
| + |
| +/** |
| + * The new tab page to display when Chrome Home is enabled. |
| + */ |
| +public class ChromeHomeNewTabPage implements NativePage, TemplateUrlServiceObserver { |
|
dgn
2017/02/28 14:09:56
is "Chrome" needed in the class name?
Theresa
2017/02/28 17:02:23
Yes, I think it is. "Chrome Home" is the name of t
|
| + private final Tab mTab; |
| + private final TabModelObserver mTabModelObserver; |
| + private final TabModelSelector mTabModelSelector; |
| + private final LogoManagerImpl mLogoManager; |
|
Michael van Ouwerkerk
2017/02/28 13:36:15
I wanted to say: just use the interface type LogoM
Theresa
2017/02/28 17:02:23
Thank you for the feedback. I made destroy() an in
|
| + |
| + private final View mView; |
| + private final LogoView mLogoView; |
| + |
| + private final String mTitle; |
| + private final int mBackgroundColor; |
| + private final int mThemeColor; |
| + |
| + /** |
| + * Constructs a ChromeHomeNewTabPage. |
| + * @param context The context used to inflate the view. |
| + * @param tab The Tab that is showing this new tab page. |
| + * @param tabModelSelector The TabModelSelector used to open tabs. |
| + */ |
| + public ChromeHomeNewTabPage( |
| + final Context context, final Tab tab, final TabModelSelector tabModelSelector) { |
| + mTab = tab; |
| + mTabModelSelector = tabModelSelector; |
| + mView = LayoutInflater.from(context).inflate(R.layout.chrome_home_new_tab_page, null); |
| + |
| + Resources res = context.getResources(); |
| + mTitle = res.getString(R.string.button_new_tab); |
| + mBackgroundColor = ApiCompatibilityUtils.getColor(res, R.color.ntp_bg); |
| + mThemeColor = ApiCompatibilityUtils.getColor(res, R.color.default_primary_color); |
| + |
| + TemplateUrlService.getInstance().addObserver(this); |
| + |
| + mLogoView = (LogoView) mView.findViewById(R.id.search_provider_logo); |
| + mLogoManager = new LogoManagerImpl(tab, mLogoView); |
| + mLogoManager.getSearchProviderLogo(new LogoObserver() { |
| + @Override |
| + public void onLogoAvailable(Logo logo, boolean fromCache) { |
| + if (logo == null && fromCache) return; |
| + mLogoView.setMananger(mLogoManager); |
| + mLogoView.updateLogo(logo); |
| + } |
| + }); |
| + updateSearchProviderLogoVisibility(); |
| + |
| + final ChromeActivity activity = tab.getActivity(); |
| + |
| + // TODO(twellington): remove this after only one NTP may be open at a time and NTP is |
| + // destroyed after user navigates to a different tab. |
| + mTabModelObserver = new EmptyTabModelObserver() { |
| + @Override |
| + public void didSelectTab(Tab tab, TabSelectionType type, int lastId) { |
| + boolean isNewTabPage = isTabChromeHomeNewTabPage(tab); |
| + activity.getFadingBackgroundView().setEnabled(!isNewTabPage); |
| + if (isNewTabPage) onNewTabPageShown(); |
| + } |
| + }; |
| + mTabModelSelector.getModel(false).addObserver(mTabModelObserver); |
| + |
| + View closeButton = mView.findViewById(R.id.close_button); |
| + closeButton.setOnClickListener(new OnClickListener() { |
| + @Override |
| + public void onClick(View v) { |
| + activity.getBottomSheet().setSheetState(BottomSheet.SHEET_STATE_PEEK, true); |
| + mTabModelSelector.closeTab(tab); |
| + // TODO(twellington): show overview mode. |
| + } |
| + }); |
| + |
| + onNewTabPageShown(); |
| + |
| + // TODO(twellington): disallow moving the NTP to the other window in Android N+ |
| + // multi-window mode. |
| + } |
| + |
| + @Override |
| + public View getView() { |
| + return mView; |
| + } |
| + |
| + @Override |
| + public String getTitle() { |
| + return mTitle; |
| + } |
| + |
| + @Override |
| + public String getUrl() { |
| + return UrlConstants.NTP_URL; |
| + } |
| + |
| + @Override |
| + public String getHost() { |
| + return UrlConstants.NTP_HOST; |
| + } |
| + |
| + @Override |
| + public int getBackgroundColor() { |
| + return mBackgroundColor; |
| + } |
| + |
| + @Override |
| + public int getThemeColor() { |
| + return mThemeColor; |
| + } |
| + |
| + @Override |
| + public boolean needsToolbarShadow() { |
| + return false; |
| + } |
| + |
| + @Override |
| + public void updateForUrl(String url) {} |
| + |
| + @Override |
| + public void destroy() { |
| + mLogoManager.destroy(); |
| + |
| + // The next tab will be selected before this one is destroyed. If the currently selected |
| + // tab is a Chrome Home new tab page, the FadingBackgroundView should not be enabled. |
| + boolean newTabPageSelected = mTabModelSelector.getCurrentTab().getUrl().equals(getUrl()) |
|
Michael van Ouwerkerk
2017/02/28 13:36:15
This newTabPageSelected variable is unused... what
dgn
2017/02/28 14:09:56
Looks like it got replaced by the isTabChromeHomeN
Theresa
2017/02/28 17:02:23
That's correct -- it's no longer needed since it w
|
| + && !mTabModelSelector.getCurrentTab().isIncognito(); |
| + mTab.getActivity().getFadingBackgroundView().setEnabled(!isTabChromeHomeNewTabPage( |
| + mTabModelSelector.getCurrentTab())); |
| + |
| + mTabModelSelector.getModel(false).removeObserver(mTabModelObserver); |
| + } |
| + |
| + private void updateSearchProviderLogoVisibility() { |
| + boolean hasLogo = TemplateUrlService.getInstance().isDefaultSearchEngineGoogle(); |
| + mLogoView.setVisibility(hasLogo ? View.VISIBLE : View.GONE); |
| + } |
| + |
| + private void onNewTabPageShown() { |
| + mTab.getActivity().getFadingBackgroundView().setViewAlpha(0.f); |
|
dgn
2017/02/28 14:09:56
nit: 0f? I see a mix of 0f, 0.0f, 0.f, .0f is used
Theresa
2017/02/28 17:02:23
Done (in FadingBackgroundView).
|
| + mTab.getActivity().getFadingBackgroundView().setEnabled(false); |
| + |
| + // This method may be called when an NTP is selected due to the user switching tab models. |
| + // In this case, we do not want the bottom sheet to open. Unfortunately, without observing |
| + // OverviewModeBehavior, we have no good signal to show the BottomSheet when an NTP is |
| + // selected in the tab switcher. Eventually this won't matter because we will not allow |
| + // NTPs to remain open after the user leaves them. |
| + if (mTab.getActivity().isInOverviewMode()) return; |
| + |
| + mTab.getActivity().getBottomSheet().setSheetState(BottomSheet.SHEET_STATE_HALF, true); |
| + } |
| + |
| + private boolean isTabChromeHomeNewTabPage(Tab tab) { |
| + return tab.getUrl().equals(getUrl()) && !tab.isIncognito(); |
|
Michael van Ouwerkerk
2017/02/28 13:36:15
nit: use only one space after return
Theresa
2017/02/28 17:02:23
Done.
|
| + } |
| + |
| + // TemplateUrlServiceObserver overrides |
| + |
| + @Override |
| + public void onTemplateURLServiceChanged() { |
| + updateSearchProviderLogoVisibility(); |
| + } |
| +} |