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..f09f409334ce1b551629113425b60960defc83b3 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,169 @@ |
| 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; |
| + |
| + private Context mContext; |
|
gone
2017/03/29 17:04:10
Can these be final?
Marti Wong
2017/03/29 23:38:07
Done. Yes, it can be final :)
|
| + private AttributeSet mAttrs; |
| + private int mDefStyleAttr; |
|
Marti Wong
2017/03/29 23:38:07
mAttrs and mDefStyleAttr no longer needed after se
|
| + |
| /** |
| * Constructor for inflating from XML. |
| */ |
| public TranslateTabLayout(Context context, AttributeSet attrs) { |
| - super(context, attrs); |
| + this(context, attrs, 0); |
| + } |
| + |
| + public TranslateTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| + super(context, attrs, defStyleAttr); |
| + |
| + mAttrs = attrs; |
| + mDefStyleAttr = 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); |
| + tabContent.setupTextAppearance(mContext, getTabTextColors(), mAttrs, mDefStyleAttr); |
| + 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()) { |
| + return; |
| + } |
| + if (mTabShowingProgressBar != null) { |
|
gone
2017/03/29 17:04:10
Might as well combine this with the above conditio
Marti Wong
2017/03/29 23:38:07
Done.
|
| + 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() != null && tab.getCustomView() instanceof TranslateTabContent); |
|
gone
2017/03/29 17:04:10
you don't need to null check; instanceof automatic
Marti Wong
2017/03/29 23:38:07
Done.
|
| } |
| - // TODO(martiw): Add special methods on demand. |
| + // Overrided to make sure only supported Tabs can be added. |
| + @Override |
| + public void addTab(@NonNull Tab tab, int position, boolean setSelected) { |
| + if (!tabIsSupported(tab)) { |
| + throwsIllegalArgumentException(); |
| + return; |
| + } |
| + 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)) { |
| + throwsIllegalArgumentException(); |
| + return; |
| + } |
| + super.addTab(tab, setSelected); |
| + } |
| + |
| + private void throwsIllegalArgumentException() { |
| + throw new IllegalArgumentException( |
|
gone
2017/03/29 17:04:10
Ah, I meant that you could probably just inline th
Marti Wong
2017/03/29 23:38:07
Done.
Agree, it can make the code cleaner.
I remov
|
| + "Only Tab with TranslateTabContent as its CustomView is allowed to add."); |
|
gone
2017/03/29 17:04:11
is allowed to add -> can be added
Marti Wong
2017/03/29 23:38:07
Message removed.
|
| + } |
| } |