| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.infobar; | 5 package org.chromium.chrome.browser.infobar; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.text.Spannable; | 8 import android.text.Spannable; |
| 9 import android.text.SpannableString; | 9 import android.text.SpannableString; |
| 10 import android.text.SpannableStringBuilder; | 10 import android.text.SpannableStringBuilder; |
| 11 import android.text.TextUtils; | 11 import android.text.TextUtils; |
| 12 import android.text.style.ClickableSpan; | 12 import android.text.style.ClickableSpan; |
| 13 import android.view.View; | 13 import android.view.View; |
| 14 import android.widget.CheckBox; | 14 import android.widget.CheckBox; |
| 15 | 15 |
| 16 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
| 17 import org.chromium.ui.base.DeviceFormFactor; | 17 import org.chromium.ui.base.DeviceFormFactor; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Java version of the translate infobar | 20 * Java version of the translate infobar |
| 21 */ | 21 */ |
| 22 public class TranslateInfoBar extends TwoButtonInfoBar implements SubPanelListen
er { | 22 public class TranslateInfoBar extends InfoBar implements SubPanelListener { |
| 23 // Needs to be kept in sync with the Type enum in translate_infobar_delegate
.h. | 23 // Needs to be kept in sync with the Type enum in translate_infobar_delegate
.h. |
| 24 public static final int BEFORE_TRANSLATE_INFOBAR = 0; | 24 public static final int BEFORE_TRANSLATE_INFOBAR = 0; |
| 25 public static final int TRANSLATING_INFOBAR = 1; | 25 public static final int TRANSLATING_INFOBAR = 1; |
| 26 public static final int AFTER_TRANSLATE_INFOBAR = 2; | 26 public static final int AFTER_TRANSLATE_INFOBAR = 2; |
| 27 public static final int TRANSLATE_ERROR_INFOBAR = 3; | 27 public static final int TRANSLATE_ERROR_INFOBAR = 3; |
| 28 public static final int MAX_INFOBAR_INDEX = 4; | 28 public static final int MAX_INFOBAR_INDEX = 4; |
| 29 | 29 |
| 30 // Defines what subpanel needs to be shown, if any | 30 // Defines what subpanel needs to be shown, if any |
| 31 public static final int NO_PANEL = 0; | 31 public static final int NO_PANEL = 0; |
| 32 public static final int LANGUAGE_PANEL = 1; | 32 public static final int LANGUAGE_PANEL = 1; |
| 33 public static final int NEVER_PANEL = 2; | 33 public static final int NEVER_PANEL = 2; |
| 34 public static final int ALWAYS_PANEL = 3; | 34 public static final int ALWAYS_PANEL = 3; |
| 35 public static final int MAX_PANEL_INDEX = 4; | 35 public static final int MAX_PANEL_INDEX = 4; |
| 36 | 36 |
| 37 private int mInfoBarType; | 37 private int mInfoBarType; |
| 38 private final TranslateOptions mOptions; | 38 private final TranslateOptions mOptions; |
| 39 private int mOptionsPanelViewType; | 39 private int mOptionsPanelViewType; |
| 40 private TranslateSubPanel mSubPanel; | 40 private TranslateSubPanel mSubPanel; |
| 41 private final boolean mShouldShowNeverBar; | 41 private final boolean mShouldShowNeverBar; |
| 42 private final TranslateInfoBarDelegate mTranslateDelegate; | 42 private final TranslateInfoBarDelegate mTranslateDelegate; |
| 43 | 43 |
| 44 public TranslateInfoBar(long nativeInfoBarPtr, TranslateInfoBarDelegate dele
gate, | 44 public TranslateInfoBar(long nativeInfoBarPtr, TranslateInfoBarDelegate dele
gate, |
| 45 int infoBarType, int sourceLanguageIndex, int targetLanguageIndex, | 45 int infoBarType, int sourceLanguageIndex, int targetLanguageIndex, |
| 46 boolean autoTranslatePair, boolean shouldShowNeverBar, | 46 boolean autoTranslatePair, boolean shouldShowNeverBar, |
| 47 boolean triggeredFromMenu, String[] languages) { | 47 boolean triggeredFromMenu, String[] languages) { |
| 48 super(null, R.drawable.infobar_translate); | 48 super(null, R.drawable.infobar_translate, null); |
| 49 | |
| 50 mTranslateDelegate = delegate; | 49 mTranslateDelegate = delegate; |
| 51 mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex
, languages, | 50 mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex
, languages, |
| 52 autoTranslatePair, triggeredFromMenu); | 51 autoTranslatePair, triggeredFromMenu); |
| 53 mInfoBarType = infoBarType; | 52 mInfoBarType = infoBarType; |
| 54 mShouldShowNeverBar = shouldShowNeverBar; | 53 mShouldShowNeverBar = shouldShowNeverBar; |
| 55 mOptionsPanelViewType = NO_PANEL; | 54 mOptionsPanelViewType = NO_PANEL; |
| 56 setNativeInfoBar(nativeInfoBarPtr); | 55 setNativeInfoBar(nativeInfoBarPtr); |
| 57 } | 56 } |
| 58 | 57 |
| 59 @Override | 58 @Override |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 case TranslateInfoBar.TRANSLATE_ERROR_INFOBAR: | 103 case TranslateInfoBar.TRANSLATE_ERROR_INFOBAR: |
| 105 // retry | 104 // retry |
| 106 action = InfoBar.ACTION_TYPE_TRANSLATE; | 105 action = InfoBar.ACTION_TYPE_TRANSLATE; |
| 107 break; | 106 break; |
| 108 default: | 107 default: |
| 109 break; | 108 break; |
| 110 } | 109 } |
| 111 return action; | 110 return action; |
| 112 } | 111 } |
| 113 | 112 |
| 114 @Override | 113 private CharSequence getMessageText(Context context) { |
| 115 public CharSequence getMessageText(Context context) { | |
| 116 switch (getInfoBarType()) { | 114 switch (getInfoBarType()) { |
| 117 case BEFORE_TRANSLATE_INFOBAR: | 115 case BEFORE_TRANSLATE_INFOBAR: |
| 118 String template = context.getString(R.string.translate_infobar_t
ext); | 116 String template = context.getString(R.string.translate_infobar_t
ext); |
| 119 return formatBeforeInfoBarMessage(template, mOptions.sourceLangu
age(), | 117 return formatBeforeInfoBarMessage(template, mOptions.sourceLangu
age(), |
| 120 mOptions.targetLanguage(), LANGUAGE_PANEL); | 118 mOptions.targetLanguage(), LANGUAGE_PANEL); |
| 121 case AFTER_TRANSLATE_INFOBAR: | 119 case AFTER_TRANSLATE_INFOBAR: |
| 122 String translated = context.getString( | 120 String translated = context.getString( |
| 123 R.string.translate_infobar_translation_done, mOptions.ta
rgetLanguage()); | 121 R.string.translate_infobar_translation_done, mOptions.ta
rgetLanguage()); |
| 124 if (needsAlwaysPanel()) { | 122 if (needsAlwaysPanel()) { |
| 125 String moreOptions = context.getString( | 123 String moreOptions = context.getString( |
| 126 R.string.translate_infobar_translation_more_options)
; | 124 R.string.translate_infobar_translation_more_options)
; |
| 127 return formatAfterTranslateInfoBarMessage(translated, moreOp
tions, | 125 return formatAfterTranslateInfoBarMessage(translated, moreOp
tions, |
| 128 ALWAYS_PANEL); | 126 ALWAYS_PANEL); |
| 129 } else { | 127 } else { |
| 130 return translated; | 128 return translated; |
| 131 } | 129 } |
| 132 case TRANSLATING_INFOBAR: | 130 case TRANSLATING_INFOBAR: |
| 133 return context.getString(R.string.translate_infobar_translating, | 131 return context.getString(R.string.translate_infobar_translating, |
| 134 mOptions.targetLanguage()); | 132 mOptions.targetLanguage()); |
| 135 default: | 133 default: |
| 136 return context.getString(R.string.translate_infobar_error); | 134 return context.getString(R.string.translate_infobar_error); |
| 137 } | 135 } |
| 138 } | 136 } |
| 139 | 137 |
| 140 @Override | 138 private String getPrimaryButtonText(Context context) { |
| 141 public String getPrimaryButtonText(Context context) { | |
| 142 switch (getInfoBarType()) { | 139 switch (getInfoBarType()) { |
| 143 case BEFORE_TRANSLATE_INFOBAR: | 140 case BEFORE_TRANSLATE_INFOBAR: |
| 144 return context.getString(R.string.translate_button); | 141 return context.getString(R.string.translate_button); |
| 145 case AFTER_TRANSLATE_INFOBAR: | 142 case AFTER_TRANSLATE_INFOBAR: |
| 146 if (!needsAlwaysPanel()) { | 143 if (!needsAlwaysPanel()) { |
| 147 return context.getString(R.string.translate_button_done); | 144 return context.getString(R.string.translate_button_done); |
| 148 } | 145 } |
| 149 return null; | 146 return null; |
| 150 case TRANSLATE_ERROR_INFOBAR: | 147 case TRANSLATE_ERROR_INFOBAR: |
| 151 return context.getString(R.string.translate_retry); | 148 return context.getString(R.string.translate_retry); |
| 152 default: | 149 default: |
| 153 return null; // no inner buttons on the remaining infobars | 150 return null; // no inner buttons on the remaining infobars |
| 154 } | 151 } |
| 155 } | 152 } |
| 156 | 153 |
| 157 @Override | 154 private String getSecondaryButtonText(Context context) { |
| 158 public String getSecondaryButtonText(Context context) { | |
| 159 switch (getInfoBarType()) { | 155 switch (getInfoBarType()) { |
| 160 case BEFORE_TRANSLATE_INFOBAR: | 156 case BEFORE_TRANSLATE_INFOBAR: |
| 161 return context.getString(R.string.translate_nope); | 157 return context.getString(R.string.translate_nope); |
| 162 case AFTER_TRANSLATE_INFOBAR: | 158 case AFTER_TRANSLATE_INFOBAR: |
| 163 if (!needsAlwaysPanel()) { | 159 if (!needsAlwaysPanel()) { |
| 164 return context.getString(R.string.translate_show_original); | 160 return context.getString(R.string.translate_show_original); |
| 165 } | 161 } |
| 166 return null; | 162 return null; |
| 167 default: | 163 default: |
| 168 return null; | 164 return null; |
| 169 } | 165 } |
| 170 } | 166 } |
| 171 | 167 |
| 172 @Override | 168 @Override |
| 173 public void createContent(InfoBarLayout layout) { | 169 public void createContent(InfoBarLayout layout) { |
| 174 if (mOptionsPanelViewType == NO_PANEL) { | 170 if (mOptionsPanelViewType == NO_PANEL) { |
| 175 mSubPanel = null; | 171 mSubPanel = null; |
| 176 } else { | 172 } else { |
| 177 mSubPanel = panelFor(mOptionsPanelViewType); | 173 mSubPanel = panelFor(mOptionsPanelViewType); |
| 178 if (mSubPanel != null) { | 174 if (mSubPanel != null) { |
| 179 mSubPanel.createContent(getContext(), layout); | 175 mSubPanel.createContent(getContext(), layout); |
| 180 } | 176 } |
| 181 return; | 177 return; |
| 182 } | 178 } |
| 183 | 179 |
| 180 Context context = layout.getContext(); |
| 181 layout.setMessage(getMessageText(context)); |
| 182 layout.setButtons(getPrimaryButtonText(context), getSecondaryButtonText(
context)); |
| 183 |
| 184 if (getInfoBarType() == AFTER_TRANSLATE_INFOBAR && | 184 if (getInfoBarType() == AFTER_TRANSLATE_INFOBAR && |
| 185 !needsAlwaysPanel() && | 185 !needsAlwaysPanel() && |
| 186 !mOptions.triggeredFromMenu()) { | 186 !mOptions.triggeredFromMenu()) { |
| 187 // Long always translate version | 187 // Long always translate version |
| 188 TranslateCheckBox checkBox = new TranslateCheckBox(mOptions, this); | 188 TranslateCheckBox checkBox = new TranslateCheckBox(context, mOptions
, this); |
| 189 checkBox.createContent(getContext(), layout); | 189 layout.setCustomContent(checkBox); |
| 190 } | 190 } |
| 191 | |
| 192 super.createContent(layout); | |
| 193 } | 191 } |
| 194 | 192 |
| 195 // SubPanelListener implementation | 193 // SubPanelListener implementation |
| 196 @Override | 194 @Override |
| 197 public void onPanelClosed(int action) { | 195 public void onPanelClosed(int action) { |
| 198 setControlsEnabled(false); | 196 setControlsEnabled(false); |
| 199 if (mOptionsPanelViewType == LANGUAGE_PANEL) { | 197 if (mOptionsPanelViewType == LANGUAGE_PANEL) { |
| 200 // Close the sub panel and show the infobar again. | 198 // Close the sub panel and show the infobar again. |
| 201 mOptionsPanelViewType = NO_PANEL; | 199 mOptionsPanelViewType = NO_PANEL; |
| 202 updateViewForCurrentState(createView()); | 200 updateViewForCurrentState(createView()); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 void changeInfoBarTypeAndNativePointer(int infoBarType, long newNativePointe
r) { | 335 void changeInfoBarTypeAndNativePointer(int infoBarType, long newNativePointe
r) { |
| 338 if (infoBarType >= 0 && infoBarType < MAX_INFOBAR_INDEX) { | 336 if (infoBarType >= 0 && infoBarType < MAX_INFOBAR_INDEX) { |
| 339 mInfoBarType = infoBarType; | 337 mInfoBarType = infoBarType; |
| 340 replaceNativePointer(newNativePointer); | 338 replaceNativePointer(newNativePointer); |
| 341 updateViewForCurrentState(createView()); | 339 updateViewForCurrentState(createView()); |
| 342 } else { | 340 } else { |
| 343 assert false : "Trying to change the InfoBar to a type that is inval
id."; | 341 assert false : "Trying to change the InfoBar to a type that is inval
id."; |
| 344 } | 342 } |
| 345 } | 343 } |
| 346 } | 344 } |
| OLD | NEW |