Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3604)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java

Issue 332943002: [AiS] Add a structured representation of Answer JSON. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java b/chrome/android/java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java
new file mode 100644
index 0000000000000000000000000000000000000000..7721d0ed36966450d13157007b81ea712e778147
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java
@@ -0,0 +1,142 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.omnibox;
+
+import android.util.Log;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Structured representation of the JSON payload of a suggestion with an Answer.
+ */
+public class SuggestionAnswer {
+ private static final String TAG = "SuggestionAnswer";
+
+ private ImageLine mFirstLine;
+ private ImageLine mSecondLine;
+
+ private static final String ANSWERS_JSON_LINE = "l";
+ private static final String ANSWERS_JSON_IMAGE_LINE = "il";
+ private static final String ANSWERS_JSON_TEXT = "t";
+ private static final String ANSWERS_JSON_ADDITIONAL_TEXT = "at";
+ private static final String ANSWERS_JSON_STATUS_TEXT = "st";
+ private static final String ANSWERS_JSON_TEXT_TYPE = "tt";
+
+ private SuggestionAnswer() {
+ }
+
+ /**
+ * Parses the JSON representation of an answer and constructs a SuggestionAnswer from the
+ * contents.
+ *
+ * @param answerContents The JSON representation of an answer.
+ * @return A SuggestionAnswer with the answer contents or null if the contents are malformed or
+ * missing required elements.
+ */
+ public static SuggestionAnswer parseAnswerContents(String answerContents) {
+ SuggestionAnswer answer = new SuggestionAnswer();
+
+ try {
+ JSONObject jsonAnswer = new JSONObject(answerContents);
+ JSONArray jsonLines = jsonAnswer.getJSONArray(ANSWERS_JSON_LINE);
+
+ if (jsonLines.length() < 2) {
+ Log.e(TAG, "Answer JSON missing expected data: " + jsonAnswer);
+ return null;
groby-ooo-7-16 2014/06/12 23:10:49 Might want to handle the "more than two lines" cas
Justin Donnelly 2014/06/17 21:34:03 Done.
+ }
+
+ answer.mFirstLine = new SuggestionAnswer.ImageLine(
+ jsonLines.getJSONObject(0).getJSONObject(ANSWERS_JSON_IMAGE_LINE));
+ answer.mSecondLine = new SuggestionAnswer.ImageLine(
+ jsonLines.getJSONObject(1).getJSONObject(ANSWERS_JSON_IMAGE_LINE));
+ } catch (JSONException e) {
+ Log.e(TAG, "Problem parsing answer JSON: " + e.getMessage());
+ return null;
+ }
+
+ return answer;
+ }
+
+ public ImageLine getFirstLine() {
+ return mFirstLine;
+ }
+
+ public ImageLine getSecondLine() {
+ return mSecondLine;
+ }
+
+ /**
+ * Represents a single line of an answer, contianing any number of typed text fields and an
groby-ooo-7-16 2014/06/12 23:10:49 "containing"
Justin Donnelly 2014/06/17 21:34:03 Done.
+ * optional image.
+ */
+ public static class ImageLine {
+ private final List<TextField> mTextFields;
+ private final TextField mAdditionalText;
+ private final TextField mStatusText;
+
+ ImageLine(JSONObject jsonLine) throws JSONException {
+ mTextFields = new ArrayList<TextField>();
+
+ JSONArray textValues = jsonLine.getJSONArray(ANSWERS_JSON_TEXT);
+ for (int i = 0; i < textValues.length(); i++) {
+ mTextFields.add(new TextField(textValues.getJSONObject(i)));
+ }
+
+ mAdditionalText = jsonLine.has(ANSWERS_JSON_ADDITIONAL_TEXT) ?
+ new TextField(jsonLine.getJSONObject(ANSWERS_JSON_ADDITIONAL_TEXT)) :
+ null;
+
+ mStatusText = jsonLine.has(ANSWERS_JSON_STATUS_TEXT) ?
+ new TextField(jsonLine.getJSONObject(ANSWERS_JSON_STATUS_TEXT)) :
+ null;
+ }
+
+ public List<TextField> getTextFields() {
Ted C 2014/06/13 23:54:16 I would javadoc these methods to give an idea of h
Justin Donnelly 2014/06/17 21:34:03 Done.
+ return mTextFields;
+ }
+
+ public boolean hasAdditionalText() {
+ return mAdditionalText != null;
+ }
+
+ public TextField getAdditionalText() {
+ return mAdditionalText;
+ }
+
+ public boolean hasStatusText() {
+ return mStatusText != null;
+ }
+
+ public TextField getStatusText() {
+ return mStatusText;
+ }
+ }
+
+ /**
+ * Represents a single typed text field of an answer.
+ */
+ public static class TextField {
+ private final int mType;
+ private final String mText;
+
+ TextField(JSONObject jsonTextField) throws JSONException {
+ mType = jsonTextField.getInt(ANSWERS_JSON_TEXT_TYPE);
+ mText = jsonTextField.getString(ANSWERS_JSON_TEXT);
+ }
+
+ public int getType() {
+ return mType;
+ }
+
+ public String getText() {
+ return mText;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698