Skip to content

Commit

Permalink
Post-process Currency conversion answer text.
Browse files Browse the repository at this point in the history
This change ensures that currency conversion answers are shortened to
offer just final result, eg. instead of:

   1,000 United State Dollar = 1,330.75 Canadian Dollar
   1000 usd to cad

this will show:

   1,330.75 Canadian Dollar
   1000 usd to cad

Bug: 936730
Change-Id: I1222c5fba17e6cca07947e37bc2494f89abec98e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1500915
Reviewed-by: Theresa <twellington@chromium.org>
Commit-Queue: Ender <ender@google.com>
Cr-Commit-Position: refs/heads/master@{#637855}
  • Loading branch information
tomasz-wiszkowski authored and Commit Bot committed Mar 5, 2019
1 parent 4ac0c19 commit 6bcf394
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ protected void build(SuggestionAnswer.ImageLine line) {
*/
@SuppressWarnings("deprecation") // Update usage of Html.fromHtml when API min is 24
protected void appendAndStyleText(String text, MetricAffectingSpan[] styles) {
// Unescape HTML entities (e.g. "&quot;", "&gt;").
text = Html.fromHtml(text).toString();
text = processAnswerText(text);

// Determine the maximum height of the TextAppearanceSpans that are applied for this field.
for (MetricAffectingSpan style : styles) {
if (!(style instanceof TextAppearanceSpan)) continue;
Expand All @@ -101,12 +105,9 @@ protected void appendAndStyleText(String text, MetricAffectingSpan[] styles) {
if (mHeightSp < textHeightSp) mHeightSp = textHeightSp;
}

// Unescape HTML entities (e.g. "&quot;", "&gt;").
text = Html.fromHtml(text).toString();

// Append as HTML (answer responses contain simple markup).
int start = mText.length();
mText.append(Html.fromHtml(text));
mText.append(text);
int end = mText.length();

for (MetricAffectingSpan style : styles) {
Expand All @@ -121,4 +122,14 @@ protected void appendAndStyleText(String text, MetricAffectingSpan[] styles) {
* @return TextAppearanceSpan array specifying styles to be used to present text field.
*/
protected abstract MetricAffectingSpan[] getAppearanceForText(@AnswerTextType int type);

/**
* Process (if desired) content of the answer text.
*
* @param text Source text.
* @return Either original or modified text.
*/
protected String processAnswerText(String text) {
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ static AnswerText[] from(Context context, OmniboxSuggestion suggestion, String q
appendAndStyleText(text, getAppearanceForText(AnswerTextType.SUGGESTION));
}

/**
* Process (if desired) content of the answer text.
*
* @param text Source text.
* @return Either original or modified text.
*/
@Override
protected String processAnswerText(String text) {
if (mIsAnswer && mAnswerType == AnswerType.CURRENCY) {
// Modify the content of answer to present only the value after conversion, that is:
// 1,000 United State Dollar = 1,330.75 Canadian Dollar
// becomes
// 1,330.75 Canadian Dollar
int offset = text.indexOf(" = ");
if (offset > 0) {
text = text.substring(offset + 3);
}
}
return text;
}

/**
* Return the TextAppearanceSpan array specifying text decorations for a given field type.
*
Expand Down

0 comments on commit 6bcf394

Please sign in to comment.