Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateCompactInfoBar.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateCompactInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateCompactInfoBar.java |
| index e6a31d0bb25df680313110ffe1b5394733ce0b11..1fbb3fb5f70134ade3a7c7d28d75de1edb19c166 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateCompactInfoBar.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateCompactInfoBar.java |
| @@ -6,36 +6,42 @@ package org.chromium.chrome.browser.infobar; |
| import android.support.design.widget.TabLayout; |
| import android.view.LayoutInflater; |
| -import android.view.View; |
| -import android.view.ViewGroup; |
| import android.widget.LinearLayout; |
| import org.chromium.base.annotations.CalledByNative; |
| import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.infobar.translate.TranslateTabLayout; |
| + |
| +import java.util.ArrayList; |
| /** |
| * Java version of the compcat translate infobar |
| */ |
| class TranslateCompactInfoBar extends InfoBar { |
| private long mNativeTranslateInfoBarPtr; |
| + private TranslateCallback mTranslateCallback; |
| + |
| + private final TranslateOptions mOptions; |
|
gone
2017/04/04 18:44:37
nit: private final above private fields
Leo
2017/04/05 08:37:47
Done.
|
| @CalledByNative |
| - private static InfoBar create() { |
| - return new TranslateCompactInfoBar(); |
| + private static InfoBar create(String sourceLanguageCode, String targetLanguageCode, |
| + String[] languages, String[] codes) { |
| + return new TranslateCompactInfoBar( |
| + sourceLanguageCode, targetLanguageCode, languages, codes); |
| } |
| - TranslateCompactInfoBar() { |
| + TranslateCompactInfoBar(String sourceLanguageCode, String targetLanguageCode, |
| + String[] languages, String[] codes) { |
| super(R.drawable.infobar_translate, null, null); |
| - } |
| - /** |
| - * Provide a custom view as infobar content to replace a standard infobar layout. |
| - */ |
| - protected View createCustomContent(ViewGroup parent) { |
| - LinearLayout content = |
| - (LinearLayout) LayoutInflater.from(getContext()) |
| - .inflate(R.layout.infobar_translate_compact_content, parent, false); |
| - return content; |
| + assert languages.length == codes.length; |
| + ArrayList<TranslateOptions.TranslateLanguagePair> languageList = |
| + new ArrayList<TranslateOptions.TranslateLanguagePair>(); |
| + for (int i = 0; i < languages.length; ++i) { |
| + languageList.add(new TranslateOptions.TranslateLanguagePair(codes[i], languages[i])); |
| + } |
| + mOptions = new TranslateOptions( |
| + sourceLanguageCode, targetLanguageCode, languageList, false, false); |
|
gone
2017/04/04 18:44:38
Move this copy and the original in TranslateInfoBa
Leo
2017/04/05 08:37:47
Some thoughts for the code refactoring.
1, The co
gone
2017/04/05 18:32:36
Yeah, sounds fine.
|
| } |
| @Override |
| @@ -44,30 +50,58 @@ class TranslateCompactInfoBar extends InfoBar { |
| } |
| @Override |
| - protected void createCompactLayoutContent(InfoBarCompactLayout layout) { |
| - // TODO(googleo): Put the real ViewGroup inflation here. |
| - TabLayout tabLayout = new TabLayout(layout.getContext()); |
| - tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); |
| - tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); |
| - tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); |
| - tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); |
| - tabLayout.addTab(tabLayout.newTab().setText("Tab 4")); |
| - tabLayout.addTab(tabLayout.newTab().setText("Tab 5")); |
| - layout.addContent(tabLayout, 1.0f); |
| - } |
| + protected void createCompactLayoutContent(InfoBarCompactLayout parent) { |
| + LinearLayout content = |
| + (LinearLayout) LayoutInflater.from(getContext()) |
| + .inflate(R.layout.infobar_translate_compact_content, parent, false); |
| - @Override |
| - public void createContent(InfoBarLayout layout) { |
| - // TODO(googleo): Draw custom view created by createCustomContent when it's ready. |
| - // Eg. layout.setCustomView(createCustomContent(layout)); |
| - layout.setMessage("Compact Translate Infobar Testing..."); |
| + TranslateTabLayout tabLayout = |
| + (TranslateTabLayout) content.findViewById(R.id.translate_infobar_tabs); |
| + mTranslateCallback = new TranslateCallback(tabLayout); |
| + tabLayout.addOnTabSelectedListener(mTranslateCallback); |
| + tabLayout.addTabs(mOptions.sourceLanguageName(), mOptions.targetLanguageName()); |
| + |
| + parent.addContent(content, 1.0f); |
| } |
| - @Override |
| - public void onButtonClicked(boolean isPrimaryButton) { |
| - if (mNativeTranslateInfoBarPtr == 0) return; |
| + private class TranslateCallback implements TabLayout.OnTabSelectedListener { |
|
gone
2017/04/04 18:44:37
1) Try not to sandwich classes in between function
Leo
2017/04/05 08:37:47
Thanks for the tips. After the code-refactor Trans
|
| + private final TranslateTabLayout mTabs; |
|
gone
2017/04/04 18:44:38
mTabLayout; mTabs sounds like a group of tabs that
Leo
2017/04/05 08:37:47
Done.
|
| + |
| + private TranslateCallback(TranslateTabLayout tabs) { |
| + super(); |
| + mTabs = tabs; |
| + } |
| + |
| + private void hideTabProgressBar(boolean success) { |
| + if (success) { |
| + mTabs.hidePresentProgressBar(); |
| + } else { |
| + mTabs.stopProgressBarAndRevertBack(); |
| + } |
| + } |
| - // TODO(googleo): Handle the button click on this infobar. |
| + @Override |
| + public void onTabSelected(TabLayout.Tab tab) { |
| + if (tab.getPosition() == 0) { |
| + onButtonClicked(ActionType.TRANSLATE_SHOW_ORIGINAL); |
| + } else { |
|
gone
2017/04/04 18:44:37
There was an onInterceptTouchEvent here somewhere
Leo
2017/04/05 08:37:47
Yes, once a tab was selected, all the TouchEvents
|
| + mTabs.showProgressBarOnTab(tab.getPosition()); |
| + onButtonClicked(ActionType.TRANSLATE); |
| + } |
| + } |
| + |
| + @Override |
| + public void onTabUnselected(TabLayout.Tab tab) {} |
| + |
| + @Override |
| + public void onTabReselected(TabLayout.Tab tab) {} |
| + } |
| + |
| + @CalledByNative |
| + private void onPageTranslated(int errorType) { |
| + if (mTranslateCallback != null) { |
| + mTranslateCallback.hideTabProgressBar(errorType == 0); |
| + } |
| } |
| @CalledByNative |