| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.support.design.widget.TabLayout; | 7 import android.support.design.widget.TabLayout; |
| 8 import android.view.LayoutInflater; | 8 import android.view.LayoutInflater; |
| 9 import android.view.View; | 9 import android.view.View; |
| 10 import android.view.View.OnClickListener; | 10 import android.view.View.OnClickListener; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 private static final String INFOBAR_HISTOGRAM = "Translate.CompactInfobar.Ev
ent"; | 64 private static final String INFOBAR_HISTOGRAM = "Translate.CompactInfobar.Ev
ent"; |
| 65 private static final String INFOBAR_HISTOGRAM_TRANSLATION_COUNT = | 65 private static final String INFOBAR_HISTOGRAM_TRANSLATION_COUNT = |
| 66 "Translate.CompactInfobar.TranslationsPerPage"; | 66 "Translate.CompactInfobar.TranslationsPerPage"; |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * This is used to back a UMA histogram, so it should be treated as | 69 * This is used to back a UMA histogram, so it should be treated as |
| 70 * append-only. The values should not be changed or reused, and | 70 * append-only. The values should not be changed or reused, and |
| 71 * INFOBAR_HISTOGRAM_BOUNDARY should be the last. | 71 * INFOBAR_HISTOGRAM_BOUNDARY should be the last. |
| 72 */ | 72 */ |
| 73 private static final int INFOBAR_IMPRESSION = 0; | 73 private static final int INFOBAR_IMPRESSION = 0; |
| 74 private static final int INFOBAR_TRANSLATE = 1; | 74 private static final int INFOBAR_TARGET_TAB_TRANSLATE = 1; |
| 75 private static final int INFOBAR_DECLINE = 2; | 75 private static final int INFOBAR_DECLINE = 2; |
| 76 private static final int INFOBAR_OPTIONS = 3; | 76 private static final int INFOBAR_OPTIONS = 3; |
| 77 private static final int INFOBAR_MORE_LANGUAGES = 4; | 77 private static final int INFOBAR_MORE_LANGUAGES = 4; |
| 78 private static final int INFOBAR_MORE_LANGUAGES_TRANSLATE = 5; | 78 private static final int INFOBAR_MORE_LANGUAGES_TRANSLATE = 5; |
| 79 private static final int INFOBAR_PAGE_NOT_IN = 6; | 79 private static final int INFOBAR_PAGE_NOT_IN = 6; |
| 80 private static final int INFOBAR_ALWAYS_TRANSLATE = 7; | 80 private static final int INFOBAR_ALWAYS_TRANSLATE = 7; |
| 81 private static final int INFOBAR_NEVER_TRANSLATE = 8; | 81 private static final int INFOBAR_NEVER_TRANSLATE = 8; |
| 82 private static final int INFOBAR_NEVER_TRANSLATE_SITE = 9; | 82 private static final int INFOBAR_NEVER_TRANSLATE_SITE = 9; |
| 83 private static final int INFOBAR_SCROLL_HIDE = 10; | 83 private static final int INFOBAR_SCROLL_HIDE = 10; |
| 84 private static final int INFOBAR_SCROLL_SHOW = 11; | 84 private static final int INFOBAR_SCROLL_SHOW = 11; |
| 85 private static final int INFOBAR_REVERT = 12; | 85 private static final int INFOBAR_REVERT = 12; |
| 86 private static final int INFOBAR_SNACKBAR_ALWAYS_TRANSLATE_IMPRESSION = 13; | 86 private static final int INFOBAR_SNACKBAR_ALWAYS_TRANSLATE_IMPRESSION = 13; |
| 87 private static final int INFOBAR_SNACKBAR_NEVER_TRANSLATE_IMPRESSION = 14; | 87 private static final int INFOBAR_SNACKBAR_NEVER_TRANSLATE_IMPRESSION = 14; |
| 88 private static final int INFOBAR_SNACKBAR_NEVER_TRANSLATE_SITE_IMPRESSION =
15; | 88 private static final int INFOBAR_SNACKBAR_NEVER_TRANSLATE_SITE_IMPRESSION =
15; |
| 89 private static final int INFOBAR_SNACKBAR_CANCEL_ALWAYS = 16; | 89 private static final int INFOBAR_SNACKBAR_CANCEL_ALWAYS = 16; |
| 90 private static final int INFOBAR_SNACKBAR_CANCEL_NEVER_SITE = 17; | 90 private static final int INFOBAR_SNACKBAR_CANCEL_NEVER_SITE = 17; |
| 91 private static final int INFOBAR_SNACKBAR_CANCEL_NEVER = 18; | 91 private static final int INFOBAR_SNACKBAR_CANCEL_NEVER = 18; |
| 92 private static final int INFOBAR_ALWAYS_TRANSLATE_UNDO = 19; | 92 private static final int INFOBAR_ALWAYS_TRANSLATE_UNDO = 19; |
| 93 private static final int INFOBAR_CLOSE = 20; | 93 private static final int INFOBAR_CLOSE_DEPRECATED = 20; |
| 94 private static final int INFOBAR_SNACKBAR_AUTO_ALWAYS_IMPRESSION = 21; | 94 private static final int INFOBAR_SNACKBAR_AUTO_ALWAYS_IMPRESSION = 21; |
| 95 private static final int INFOBAR_SNACKBAR_AUTO_NEVER_IMPRESSION = 22; | 95 private static final int INFOBAR_SNACKBAR_AUTO_NEVER_IMPRESSION = 22; |
| 96 private static final int INFOBAR_SNACKBAR_CANCEL_AUTO_ALWAYS = 23; | 96 private static final int INFOBAR_SNACKBAR_CANCEL_AUTO_ALWAYS = 23; |
| 97 private static final int INFOBAR_SNACKBAR_CANCEL_AUTO_NEVER = 24; | 97 private static final int INFOBAR_SNACKBAR_CANCEL_AUTO_NEVER = 24; |
| 98 private static final int INFOBAR_HISTOGRAM_BOUNDARY = 25; | 98 private static final int INFOBAR_HISTOGRAM_BOUNDARY = 25; |
| 99 | 99 |
| 100 // Need 2 instances of TranslateMenuHelper to prevent a race condition bug w
hich happens when | 100 // Need 2 instances of TranslateMenuHelper to prevent a race condition bug w
hich happens when |
| 101 // showing language menu after dismissing overflow menu. | 101 // showing language menu after dismissing overflow menu. |
| 102 private TranslateMenuHelper mOverflowMenuHelper; | 102 private TranslateMenuHelper mOverflowMenuHelper; |
| 103 private TranslateMenuHelper mLanguageMenuHelper; | 103 private TranslateMenuHelper mLanguageMenuHelper; |
| 104 private TintedImageButton mMenuButton; | 104 private TintedImageButton mMenuButton; |
| 105 | 105 |
| 106 private boolean mMenuExpanded; | 106 private boolean mMenuExpanded; |
| 107 private boolean mIsFirstLayout = true; | 107 private boolean mIsFirstLayout = true; |
| 108 private boolean mUserInteracted; |
| 108 | 109 |
| 109 /** The controller for translate UI snackbars. */ | 110 /** The controller for translate UI snackbars. */ |
| 110 class TranslateSnackbarController implements SnackbarController { | 111 class TranslateSnackbarController implements SnackbarController { |
| 111 private final int mActionId; | 112 private final int mActionId; |
| 112 | 113 |
| 113 public TranslateSnackbarController(int actionId) { | 114 public TranslateSnackbarController(int actionId) { |
| 114 mActionId = actionId; | 115 mActionId = actionId; |
| 115 } | 116 } |
| 116 | 117 |
| 117 @Override | 118 @Override |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 (LinearLayout) LayoutInflater.from(getContext()) | 178 (LinearLayout) LayoutInflater.from(getContext()) |
| 178 .inflate(R.layout.infobar_translate_compact_content, par
ent, false); | 179 .inflate(R.layout.infobar_translate_compact_content, par
ent, false); |
| 179 | 180 |
| 180 mTabLayout = (TranslateTabLayout) content.findViewById(R.id.translate_in
fobar_tabs); | 181 mTabLayout = (TranslateTabLayout) content.findViewById(R.id.translate_in
fobar_tabs); |
| 181 mTabLayout.addTabs(mOptions.sourceLanguageName(), mOptions.targetLanguag
eName()); | 182 mTabLayout.addTabs(mOptions.sourceLanguageName(), mOptions.targetLanguag
eName()); |
| 182 | 183 |
| 183 // Set translating status in the beginning for pages translated automati
cally. | 184 // Set translating status in the beginning for pages translated automati
cally. |
| 184 if (mInitialStep == TRANSLATING_INFOBAR) { | 185 if (mInitialStep == TRANSLATING_INFOBAR) { |
| 185 mTabLayout.getTabAt(TARGET_TAB_INDEX).select(); | 186 mTabLayout.getTabAt(TARGET_TAB_INDEX).select(); |
| 186 mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX); | 187 mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX); |
| 188 mUserInteracted = true; |
| 187 } | 189 } |
| 188 | 190 |
| 189 mTabLayout.addOnTabSelectedListener(this); | 191 mTabLayout.addOnTabSelectedListener(this); |
| 190 | 192 |
| 191 // Dismiss all menus and end peeking animation when there is layout chan
ged. | 193 // Dismiss all menus and end peeking animation when there is layout chan
ged. |
| 192 mTabLayout.addOnLayoutChangeListener(new OnLayoutChangeListener() { | 194 mTabLayout.addOnLayoutChangeListener(new OnLayoutChangeListener() { |
| 193 @Override | 195 @Override |
| 194 public void onLayoutChange(View v, int left, int top, int right, int
bottom, | 196 public void onLayoutChange(View v, int left, int top, int right, int
bottom, |
| 195 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 197 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 196 if (left != oldLeft || top != oldTop || right != oldRight || bot
tom != oldBottom) { | 198 if (left != oldLeft || top != oldTop || right != oldRight || bot
tom != oldBottom) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 default: | 247 default: |
| 246 assert false : "Unsupported Menu Item Id"; | 248 assert false : "Unsupported Menu Item Id"; |
| 247 } | 249 } |
| 248 } | 250 } |
| 249 | 251 |
| 250 private void startTranslating(int tabPosition) { | 252 private void startTranslating(int tabPosition) { |
| 251 if (TARGET_TAB_INDEX == tabPosition) { | 253 if (TARGET_TAB_INDEX == tabPosition) { |
| 252 // Already on the target tab. | 254 // Already on the target tab. |
| 253 mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX); | 255 mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX); |
| 254 onButtonClicked(ActionType.TRANSLATE); | 256 onButtonClicked(ActionType.TRANSLATE); |
| 257 mUserInteracted = true; |
| 255 } else { | 258 } else { |
| 256 mTabLayout.getTabAt(TARGET_TAB_INDEX).select(); | 259 mTabLayout.getTabAt(TARGET_TAB_INDEX).select(); |
| 257 } | 260 } |
| 258 } | 261 } |
| 259 | 262 |
| 260 @CalledByNative | 263 @CalledByNative |
| 261 private void onPageTranslated(int errorType) { | 264 private void onPageTranslated(int errorType) { |
| 262 incrementAndRecordTranslationsPerPageCount(); | 265 incrementAndRecordTranslationsPerPageCount(); |
| 263 if (mTabLayout != null) { | 266 if (mTabLayout != null) { |
| 264 mTabLayout.hideProgressBar(); | 267 mTabLayout.hideProgressBar(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 286 Snackbar.UMA_TRANSLATE_ALWAYS, ACTION_AUTO_ALWAYS_TRANSLATE); | 289 Snackbar.UMA_TRANSLATE_ALWAYS, ACTION_AUTO_ALWAYS_TRANSLATE); |
| 287 } | 290 } |
| 288 | 291 |
| 289 @Override | 292 @Override |
| 290 protected void onNativeDestroyed() { | 293 protected void onNativeDestroyed() { |
| 291 mNativeTranslateInfoBarPtr = 0; | 294 mNativeTranslateInfoBarPtr = 0; |
| 292 super.onNativeDestroyed(); | 295 super.onNativeDestroyed(); |
| 293 } | 296 } |
| 294 | 297 |
| 295 private void closeInfobar(boolean explicitly) { | 298 private void closeInfobar(boolean explicitly) { |
| 299 if (!mUserInteracted) { |
| 300 recordInfobarAction(INFOBAR_DECLINE); |
| 301 } |
| 302 |
| 296 // Check if we should trigger the auto "never translate" if infobar is c
losed explicitly. | 303 // Check if we should trigger the auto "never translate" if infobar is c
losed explicitly. |
| 297 if (explicitly && mNativeTranslateInfoBarPtr != 0 | 304 if (explicitly && mNativeTranslateInfoBarPtr != 0 |
| 298 && nativeShouldAutoNeverTranslate(mNativeTranslateInfoBarPtr, mM
enuExpanded)) { | 305 && nativeShouldAutoNeverTranslate(mNativeTranslateInfoBarPtr, mM
enuExpanded)) { |
| 299 createAndShowSnackbar(getContext().getString(R.string.translate_snac
kbar_language_never, | 306 createAndShowSnackbar(getContext().getString(R.string.translate_snac
kbar_language_never, |
| 300 mOptions.sourceLanguageName()), | 307 mOptions.sourceLanguageName()), |
| 301 Snackbar.UMA_TRANSLATE_NEVER, ACTION_AUTO_NEVER_LANGUAGE); | 308 Snackbar.UMA_TRANSLATE_NEVER, ACTION_AUTO_NEVER_LANGUAGE); |
| 302 // Postpone the infobar dismiss until the snackbar finished showing.
Otherwise, the | 309 // Postpone the infobar dismiss until the snackbar finished showing.
Otherwise, the |
| 303 // reference to the native infobar is killed and there is no way for
the snackbar to | 310 // reference to the native infobar is killed and there is no way for
the snackbar to |
| 304 // perform the action. | 311 // perform the action. |
| 305 return; | 312 return; |
| 306 } | 313 } |
| 307 recordInfobarAction(INFOBAR_CLOSE); | |
| 308 // This line will dismiss this infobar. | 314 // This line will dismiss this infobar. |
| 309 super.onCloseButtonClicked(); | 315 super.onCloseButtonClicked(); |
| 310 } | 316 } |
| 311 | 317 |
| 312 @Override | 318 @Override |
| 313 public void onCloseButtonClicked() { | 319 public void onCloseButtonClicked() { |
| 314 mTabLayout.endPeekingAnimationIfPlaying(); | 320 mTabLayout.endPeekingAnimationIfPlaying(); |
| 315 closeInfobar(true); | 321 closeInfobar(true); |
| 316 } | 322 } |
| 317 | 323 |
| 318 @Override | 324 @Override |
| 319 public void onTabSelected(TabLayout.Tab tab) { | 325 public void onTabSelected(TabLayout.Tab tab) { |
| 320 switch (tab.getPosition()) { | 326 switch (tab.getPosition()) { |
| 321 case SOURCE_TAB_INDEX: | 327 case SOURCE_TAB_INDEX: |
| 322 incrementAndRecordTranslationsPerPageCount(); | 328 incrementAndRecordTranslationsPerPageCount(); |
| 323 recordInfobarAction(INFOBAR_REVERT); | 329 recordInfobarAction(INFOBAR_REVERT); |
| 324 onButtonClicked(ActionType.TRANSLATE_SHOW_ORIGINAL); | 330 onButtonClicked(ActionType.TRANSLATE_SHOW_ORIGINAL); |
| 325 return; | 331 return; |
| 326 case TARGET_TAB_INDEX: | 332 case TARGET_TAB_INDEX: |
| 327 recordInfobarAction(INFOBAR_TRANSLATE); | 333 recordInfobarAction(INFOBAR_TARGET_TAB_TRANSLATE); |
| 328 recordInfobarLanguageData( | 334 recordInfobarLanguageData( |
| 329 INFOBAR_HISTOGRAM_TRANSLATE_LANGUAGE, mOptions.targetLan
guageCode()); | 335 INFOBAR_HISTOGRAM_TRANSLATE_LANGUAGE, mOptions.targetLan
guageCode()); |
| 330 startTranslating(TARGET_TAB_INDEX); | 336 startTranslating(TARGET_TAB_INDEX); |
| 331 return; | 337 return; |
| 332 default: | 338 default: |
| 333 assert false : "Unexpected Tab Index"; | 339 assert false : "Unexpected Tab Index"; |
| 334 } | 340 } |
| 335 } | 341 } |
| 336 | 342 |
| 337 @Override | 343 @Override |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 return; | 391 return; |
| 386 default: | 392 default: |
| 387 assert false : "Unexpected overflow menu code"; | 393 assert false : "Unexpected overflow menu code"; |
| 388 } | 394 } |
| 389 } | 395 } |
| 390 | 396 |
| 391 @Override | 397 @Override |
| 392 public void onTargetMenuItemClicked(String code) { | 398 public void onTargetMenuItemClicked(String code) { |
| 393 // Reset target code in both UI and native. | 399 // Reset target code in both UI and native. |
| 394 if (mNativeTranslateInfoBarPtr != 0 && mOptions.setTargetLanguage(code))
{ | 400 if (mNativeTranslateInfoBarPtr != 0 && mOptions.setTargetLanguage(code))
{ |
| 401 recordInfobarAction(INFOBAR_MORE_LANGUAGES_TRANSLATE); |
| 395 recordInfobarLanguageData( | 402 recordInfobarLanguageData( |
| 396 INFOBAR_HISTOGRAM_MORE_LANGUAGES_LANGUAGE, mOptions.targetLa
nguageCode()); | 403 INFOBAR_HISTOGRAM_MORE_LANGUAGES_LANGUAGE, mOptions.targetLa
nguageCode()); |
| 397 nativeApplyStringTranslateOption( | 404 nativeApplyStringTranslateOption( |
| 398 mNativeTranslateInfoBarPtr, TranslateOption.TARGET_CODE, cod
e); | 405 mNativeTranslateInfoBarPtr, TranslateOption.TARGET_CODE, cod
e); |
| 399 // Adjust UI. | 406 // Adjust UI. |
| 400 mTabLayout.replaceTabTitle(TARGET_TAB_INDEX, mOptions.getRepresentat
ionFromCode(code)); | 407 mTabLayout.replaceTabTitle(TARGET_TAB_INDEX, mOptions.getRepresentat
ionFromCode(code)); |
| 401 startTranslating(mTabLayout.getSelectedTabPosition()); | 408 startTranslating(mTabLayout.getSelectedTabPosition()); |
| 402 } | 409 } |
| 403 } | 410 } |
| 404 | 411 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 if (mOptions.alwaysTranslateLanguageState() | 499 if (mOptions.alwaysTranslateLanguageState() |
| 493 && mTabLayout.getSelectedTabPosition() == SOURCE_TAB_IND
EX) { | 500 && mTabLayout.getSelectedTabPosition() == SOURCE_TAB_IND
EX) { |
| 494 startTranslating(mTabLayout.getSelectedTabPosition()); | 501 startTranslating(mTabLayout.getSelectedTabPosition()); |
| 495 } | 502 } |
| 496 return; | 503 return; |
| 497 case ACTION_AUTO_ALWAYS_TRANSLATE: | 504 case ACTION_AUTO_ALWAYS_TRANSLATE: |
| 498 toggleAlwaysTranslate(); | 505 toggleAlwaysTranslate(); |
| 499 return; | 506 return; |
| 500 case ACTION_OVERFLOW_NEVER_LANGUAGE: | 507 case ACTION_OVERFLOW_NEVER_LANGUAGE: |
| 501 case ACTION_AUTO_NEVER_LANGUAGE: | 508 case ACTION_AUTO_NEVER_LANGUAGE: |
| 509 mUserInteracted = true; |
| 502 // After applying this option, the infobar will dismiss. | 510 // After applying this option, the infobar will dismiss. |
| 503 nativeApplyBoolTranslateOption( | 511 nativeApplyBoolTranslateOption( |
| 504 mNativeTranslateInfoBarPtr, TranslateOption.NEVER_TRANSL
ATE, true); | 512 mNativeTranslateInfoBarPtr, TranslateOption.NEVER_TRANSL
ATE, true); |
| 505 return; | 513 return; |
| 506 case ACTION_OVERFLOW_NEVER_SITE: | 514 case ACTION_OVERFLOW_NEVER_SITE: |
| 515 mUserInteracted = true; |
| 507 // After applying this option, the infobar will dismiss. | 516 // After applying this option, the infobar will dismiss. |
| 508 nativeApplyBoolTranslateOption( | 517 nativeApplyBoolTranslateOption( |
| 509 mNativeTranslateInfoBarPtr, TranslateOption.NEVER_TRANSL
ATE_SITE, true); | 518 mNativeTranslateInfoBarPtr, TranslateOption.NEVER_TRANSL
ATE_SITE, true); |
| 510 return; | 519 return; |
| 511 default: | 520 default: |
| 512 assert false : "Unsupported Menu Item Id, in handle post snackba
r"; | 521 assert false : "Unsupported Menu Item Id, in handle post snackba
r"; |
| 513 } | 522 } |
| 514 } | 523 } |
| 515 | 524 |
| 516 private void toggleAlwaysTranslate() { | 525 private void toggleAlwaysTranslate() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 536 INFOBAR_HISTOGRAM_TRANSLATION_COUNT, ++mTotalTranslationCount); | 545 INFOBAR_HISTOGRAM_TRANSLATION_COUNT, ++mTotalTranslationCount); |
| 537 } | 546 } |
| 538 | 547 |
| 539 private native void nativeApplyStringTranslateOption( | 548 private native void nativeApplyStringTranslateOption( |
| 540 long nativeTranslateCompactInfoBar, int option, String value); | 549 long nativeTranslateCompactInfoBar, int option, String value); |
| 541 private native void nativeApplyBoolTranslateOption( | 550 private native void nativeApplyBoolTranslateOption( |
| 542 long nativeTranslateCompactInfoBar, int option, boolean value); | 551 long nativeTranslateCompactInfoBar, int option, boolean value); |
| 543 private native boolean nativeShouldAutoNeverTranslate( | 552 private native boolean nativeShouldAutoNeverTranslate( |
| 544 long nativeTranslateCompactInfoBar, boolean menuExpanded); | 553 long nativeTranslateCompactInfoBar, boolean menuExpanded); |
| 545 } | 554 } |
| OLD | NEW |