Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateTabLayout.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateTabLayout.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateTabLayout.java |
| index eb684d4eb786c97cae58d79318566777289698d1..f5baa06e2957b3596a9b7670a99095101912b75f 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateTabLayout.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateTabLayout.java |
| @@ -5,20 +5,156 @@ |
| package org.chromium.chrome.browser.infobar.translate; |
| import android.content.Context; |
| +import android.support.annotation.NonNull; |
| import android.support.design.widget.TabLayout; |
| import android.util.AttributeSet; |
| +import android.view.LayoutInflater; |
| +import android.view.MotionEvent; |
| + |
| +import org.chromium.chrome.R; |
| /** |
| - * Lays out an infobar that has all of its controls along a line. |
| - * It's currently used for Translate UI redesign only. |
| + * TabLayout shown in the TranslateCompactInfoBar. |
| */ |
| public class TranslateTabLayout extends TabLayout { |
| + // The tab in which a spinning progress bar is showing. |
| + private Tab mTabShowingProgressBar = null; |
|
gone
2017/03/29 23:53:13
nit: Java references default to null so you don't
Marti Wong
2017/03/30 00:13:52
Done.
|
| + |
| + private final Context mContext; |
|
gone
2017/03/29 23:53:13
nit: Can you check if getContext() can be used ins
Marti Wong
2017/03/30 00:13:52
Done.
|
| + |
| /** |
| * Constructor for inflating from XML. |
| */ |
| public TranslateTabLayout(Context context, AttributeSet attrs) { |
| - super(context, attrs); |
| + this(context, attrs, 0); |
| } |
| - // TODO(martiw): Add special methods on demand. |
| + public TranslateTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| + super(context, attrs, defStyleAttr); |
| + mContext = context; |
| + } |
| + |
| + /** |
| + * Add new Tabs with title strings. |
| + * @param titles Titles of the tabs to be added. |
| + */ |
| + public void addTabs(CharSequence... titles) { |
| + for (CharSequence title : titles) { |
| + addTabWithTitle(title); |
| + } |
| + } |
| + |
| + /** |
| + * Add a new Tab with the title string. |
| + * @param tabTitle Title string of the new tab. |
| + */ |
| + public void addTabWithTitle(CharSequence tabTitle) { |
| + TranslateTabContent tabContent = |
| + (TranslateTabContent) LayoutInflater.from(mContext).inflate( |
| + R.layout.infobar_translate_tab_content, this, false); |
| + // Set text color using tabLayout's ColorStateList. So that the title text will change |
| + // color when selected and unselected. |
| + tabContent.setTextColor(getTabTextColors()); |
| + tabContent.setText(tabTitle); |
| + |
| + Tab tab = newTab(); |
| + tab.setCustomView(tabContent); |
| + tab.setContentDescription(tabTitle); |
| + super.addTab(tab); |
| + } |
| + |
| + /** |
| + * Replace the title string of a tab. |
| + * @param tabPos The position of the tab to modify. |
| + * @param tabTitle The new title string. |
| + */ |
| + public void replaceTabTitle(int tabPos, CharSequence tabTitle) { |
| + if (tabPos < 0 || tabPos >= getTabCount()) { |
| + return; |
| + } |
| + Tab tab = getTabAt(tabPos); |
| + ((TranslateTabContent) tab.getCustomView()).setText(tabTitle); |
| + tab.setContentDescription(tabTitle); |
| + } |
| + |
| + /** |
| + * Select a tab. |
| + * @param tabPos The position of the tab to select. |
| + */ |
| + public void selectTab(int tabPos) { |
| + if (tabPos < 0 || tabPos >= getTabCount() || mTabShowingProgressBar != null) { |
| + return; |
| + } |
| + getTabAt(tabPos).select(); |
| + } |
| + |
| + /** |
| + * Show the spinning progress bar on a specified tab. |
| + * @param tabPos The position of the tab to show the progress bar. |
| + */ |
| + public void showProgressBarOnTab(int tabPos) { |
| + if (tabPos < 0 || tabPos >= getTabCount() || mTabShowingProgressBar != null) { |
| + return; |
| + } |
| + mTabShowingProgressBar = getTabAt(tabPos); |
| + |
| + mTabShowingProgressBar.select(); |
| + |
| + // TODO(martiw) See if we need to setContentDescription as "Translating" here. |
| + |
| + if (tabIsSupported(mTabShowingProgressBar)) { |
| + ((TranslateTabContent) mTabShowingProgressBar.getCustomView()).showProgressBar(); |
| + } |
| + } |
| + |
| + /** Stop the spinning progress bar. */ |
| + public void stopProgressBarAndRevertBack() { |
| + if (mTabShowingProgressBar == null) { |
| + return; |
| + } |
| + |
| + // TODO(martiw) if we have setContentDescription at showProgressBarOnTab(), we need to |
| + // revert it here. |
| + |
| + if (tabIsSupported(mTabShowingProgressBar)) { |
| + ((TranslateTabContent) mTabShowingProgressBar.getCustomView()).hideProgressBar(); |
| + } |
| + // TODO(martiw) See if we need to prevent the following from triggering onTabSelected event. |
| + selectTab(0); // switch the selection back to the first tab. |
| + |
| + mTabShowingProgressBar = null; |
| + } |
| + |
| + // Overrided to block children's touch event when showing progress bar. |
| + @Override |
| + public boolean onInterceptTouchEvent(MotionEvent ev) { |
| + // Allow touches to propagate to children only if the layout can be interacted with. |
| + if (mTabShowingProgressBar != null) { |
| + return true; |
| + } |
| + return super.onInterceptTouchEvent(ev); |
| + } |
| + |
| + /** Check if the tab is supported in TranslateTabLayout. */ |
| + private boolean tabIsSupported(Tab tab) { |
| + return (tab.getCustomView() instanceof TranslateTabContent); |
| + } |
| + |
| + // Overrided to make sure only supported Tabs can be added. |
| + @Override |
| + public void addTab(@NonNull Tab tab, int position, boolean setSelected) { |
| + if (!tabIsSupported(tab)) { |
| + throw new IllegalArgumentException(); |
| + } |
| + super.addTab(tab, position, setSelected); |
| + } |
| + |
| + // Overrided to make sure only supported Tabs can be added. |
| + @Override |
| + public void addTab(@NonNull Tab tab, boolean setSelected) { |
| + if (!tabIsSupported(tab)) { |
| + throw new IllegalArgumentException(); |
| + } |
| + super.addTab(tab, setSelected); |
| + } |
| } |