Commit 70b526a5 authored by Donn Denman's avatar Donn Denman Committed by Commit Bot

[TTS] Fix formatting a definition without a middot

The server returns no pronunciation in some languages. This
formats poorly - with a trailing dot. This is due to the
formatting code relying on the mid-dot to determine if the
card is a definition or not. Now we pass in the card type
which is authoritative.

BUG=1112792, 1117684

Change-Id: If9a4a4ddba9800a8a77212e1cf9d2d1296424dff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2358450
Commit-Queue: Donn Denman <donnd@chromium.org>
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Auto-Submit: Donn Denman <donnd@chromium.org>
Reviewed-by: default avatarJinsuk Kim <jinsukkim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799462}
parent 3e7dcc80
......@@ -16,6 +16,7 @@ import org.chromium.chrome.R;
import org.chromium.chrome.browser.compositor.animation.CompositorAnimator;
import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelAnimation;
import org.chromium.chrome.browser.contextualsearch.QuickActionCategory;
import org.chromium.chrome.browser.contextualsearch.ResolvedSearchTerm.CardTag;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.ui.base.LocalizationUtils;
import org.chromium.ui.resources.dynamics.DynamicResourceLoader;
......@@ -262,10 +263,11 @@ public class ContextualSearchBarControl {
/**
* Updates the Bar to display a dictionary definition card.
* @param searchTerm The string that represents the search term to display.
* @param cardTagEnum Which kind of card is being shown in this update.
*/
public void updateForDictionaryDefinition(String searchTerm) {
void updateForDictionaryDefinition(String searchTerm, @CardTag int cardTagEnum) {
if (!mCardIconControl.didUpdateControlsForDefinition(
mContextControl, mImageControl, searchTerm)) {
mContextControl, mImageControl, searchTerm, cardTagEnum)) {
// Can't style, just update with the text to display.
setSearchTerm(searchTerm);
animateSearchTermResolution();
......
......@@ -11,6 +11,7 @@ import android.widget.ImageView;
import androidx.appcompat.content.res.AppCompatResources;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.contextualsearch.ResolvedSearchTerm.CardTag;
import org.chromium.ui.base.LocalizationUtils;
import org.chromium.ui.resources.dynamics.DynamicResourceLoader;
import org.chromium.ui.resources.dynamics.ViewResourceInflater;
......@@ -37,32 +38,38 @@ public class ContextualSearchCardIconControl extends ViewResourceInflater {
/**
* Tries to update the given controls to display a dictionary definition card, and returns
* whether that was successful. We use the Context, which normally shows the word tapped and its
* surrounding text to show the dictionary word and its pronunciation.
* whether that was successful. We use the Bar's Context, which normally shows the word tapped
* and its surrounding text to show the dictionary word with its pronunciation in grey.
* @param contextControl The {@link ContextualSearchContextControl} that displays a two-part
* main bar text, to set to the dictionary-word/pronunciation.
* @param imageControl The control for the image to show in the bar. If successful we'll show a
* dictionary icon here.
* @param searchTerm The string that represents the search term to display.
* @param cardTagEnum Which kind of card is being shown in this update.
* @return Whether the bar could be updated with the given search term.
*/
boolean didUpdateControlsForDefinition(ContextualSearchContextControl contextControl,
ContextualSearchImageControl imageControl, String searchTerm) {
// This middle-dot character is returned by the server and marks the beginning of the
ContextualSearchImageControl imageControl, String searchTerm,
@CardTag int cardTagEnum) {
assert cardTagEnum == CardTag.CT_DEFINITION
|| cardTagEnum == CardTag.CT_CONTEXTUAL_DEFINITION;
boolean didUpdate = false;
// The middle-dot character is returned by the server and marks the beginning of the
// pronunciation.
int dotSeparatorLocation = searchTerm.indexOf(DEFINITION_MID_DOT);
if (dotSeparatorLocation <= 0 || dotSeparatorLocation >= searchTerm.length() - 1) {
return false;
if (dotSeparatorLocation > 0 && dotSeparatorLocation < searchTerm.length()) {
// Style with the pronunciation in gray in the second half.
String word = searchTerm.substring(0, dotSeparatorLocation);
String pronunciation =
searchTerm.substring(dotSeparatorLocation + 1, searchTerm.length());
pronunciation = LocalizationUtils.isLayoutRtl() ? pronunciation + DEFINITION_MID_DOT
: DEFINITION_MID_DOT + pronunciation;
contextControl.setContextDetails(word, pronunciation);
didUpdate = true;
}
// Style with the pronunciation in gray in the second half.
String word = searchTerm.substring(0, dotSeparatorLocation);
String pronunciation = searchTerm.substring(dotSeparatorLocation + 1, searchTerm.length());
pronunciation = LocalizationUtils.isLayoutRtl() ? pronunciation + DEFINITION_MID_DOT
: DEFINITION_MID_DOT + pronunciation;
contextControl.setContextDetails(word, pronunciation);
setVectorDrawableResourceId(R.drawable.ic_book_round);
imageControl.setCardIconResourceId(getIconResId());
return true;
return didUpdate;
}
/**
......
......@@ -580,7 +580,7 @@ public class ContextualSearchPanel extends OverlayPanel {
mPanelMetrics.onSearchTermResolved();
if (cardTagEnum == CardTag.CT_DEFINITION
|| cardTagEnum == CardTag.CT_CONTEXTUAL_DEFINITION) {
getSearchBarControl().updateForDictionaryDefinition(searchTerm);
getSearchBarControl().updateForDictionaryDefinition(searchTerm, cardTagEnum);
return;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment