Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.omnibox; | |
| 6 | |
| 7 import android.util.Log; | |
| 8 | |
| 9 import org.json.JSONArray; | |
| 10 import org.json.JSONException; | |
| 11 import org.json.JSONObject; | |
| 12 | |
| 13 import java.util.ArrayList; | |
| 14 import java.util.List; | |
| 15 | |
| 16 /** | |
| 17 * Structured representation of the JSON payload of a suggestion with an Answer. | |
| 18 */ | |
| 19 public class SuggestionAnswer { | |
| 20 private static final String TAG = "SuggestionAnswer"; | |
| 21 | |
| 22 private ImageLine mFirstLine; | |
| 23 private ImageLine mSecondLine; | |
| 24 | |
| 25 private static final String ANSWERS_JSON_LINE = "l"; | |
| 26 private static final String ANSWERS_JSON_IMAGE_LINE = "il"; | |
| 27 private static final String ANSWERS_JSON_TEXT = "t"; | |
| 28 private static final String ANSWERS_JSON_ADDITIONAL_TEXT = "at"; | |
| 29 private static final String ANSWERS_JSON_STATUS_TEXT = "st"; | |
| 30 private static final String ANSWERS_JSON_TEXT_TYPE = "tt"; | |
| 31 | |
| 32 private SuggestionAnswer() { | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Parses the JSON representation of an answer and constructs a SuggestionAn swer from the | |
| 37 * contents. | |
| 38 * | |
| 39 * @param answerContents The JSON representation of an answer. | |
| 40 * @return A SuggestionAnswer with the answer contents or null if the conten ts are malformed or | |
| 41 * missing required elements. | |
| 42 */ | |
| 43 public static SuggestionAnswer parseAnswerContents(String answerContents) { | |
| 44 SuggestionAnswer answer = new SuggestionAnswer(); | |
| 45 | |
| 46 try { | |
| 47 JSONObject jsonAnswer = new JSONObject(answerContents); | |
| 48 JSONArray jsonLines = jsonAnswer.getJSONArray(ANSWERS_JSON_LINE); | |
| 49 | |
| 50 if (jsonLines.length() < 2) { | |
| 51 Log.e(TAG, "Answer JSON missing expected data: " + jsonAnswer); | |
| 52 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.
| |
| 53 } | |
| 54 | |
| 55 answer.mFirstLine = new SuggestionAnswer.ImageLine( | |
| 56 jsonLines.getJSONObject(0).getJSONObject(ANSWERS_JSON_IMAGE_ LINE)); | |
| 57 answer.mSecondLine = new SuggestionAnswer.ImageLine( | |
| 58 jsonLines.getJSONObject(1).getJSONObject(ANSWERS_JSON_IMAGE_ LINE)); | |
| 59 } catch (JSONException e) { | |
| 60 Log.e(TAG, "Problem parsing answer JSON: " + e.getMessage()); | |
| 61 return null; | |
| 62 } | |
| 63 | |
| 64 return answer; | |
| 65 } | |
| 66 | |
| 67 public ImageLine getFirstLine() { | |
| 68 return mFirstLine; | |
| 69 } | |
| 70 | |
| 71 public ImageLine getSecondLine() { | |
| 72 return mSecondLine; | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Represents a single line of an answer, contianing any number of typed tex t fields and an | |
|
groby-ooo-7-16
2014/06/12 23:10:49
"containing"
Justin Donnelly
2014/06/17 21:34:03
Done.
| |
| 77 * optional image. | |
| 78 */ | |
| 79 public static class ImageLine { | |
| 80 private final List<TextField> mTextFields; | |
| 81 private final TextField mAdditionalText; | |
| 82 private final TextField mStatusText; | |
| 83 | |
| 84 ImageLine(JSONObject jsonLine) throws JSONException { | |
| 85 mTextFields = new ArrayList<TextField>(); | |
| 86 | |
| 87 JSONArray textValues = jsonLine.getJSONArray(ANSWERS_JSON_TEXT); | |
| 88 for (int i = 0; i < textValues.length(); i++) { | |
| 89 mTextFields.add(new TextField(textValues.getJSONObject(i))); | |
| 90 } | |
| 91 | |
| 92 mAdditionalText = jsonLine.has(ANSWERS_JSON_ADDITIONAL_TEXT) ? | |
| 93 new TextField(jsonLine.getJSONObject(ANSWERS_JSON_ADDITIONAL _TEXT)) : | |
| 94 null; | |
| 95 | |
| 96 mStatusText = jsonLine.has(ANSWERS_JSON_STATUS_TEXT) ? | |
| 97 new TextField(jsonLine.getJSONObject(ANSWERS_JSON_STATUS_TEX T)) : | |
| 98 null; | |
| 99 } | |
| 100 | |
| 101 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.
| |
| 102 return mTextFields; | |
| 103 } | |
| 104 | |
| 105 public boolean hasAdditionalText() { | |
| 106 return mAdditionalText != null; | |
| 107 } | |
| 108 | |
| 109 public TextField getAdditionalText() { | |
| 110 return mAdditionalText; | |
| 111 } | |
| 112 | |
| 113 public boolean hasStatusText() { | |
| 114 return mStatusText != null; | |
| 115 } | |
| 116 | |
| 117 public TextField getStatusText() { | |
| 118 return mStatusText; | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Represents a single typed text field of an answer. | |
| 124 */ | |
| 125 public static class TextField { | |
| 126 private final int mType; | |
| 127 private final String mText; | |
| 128 | |
| 129 TextField(JSONObject jsonTextField) throws JSONException { | |
| 130 mType = jsonTextField.getInt(ANSWERS_JSON_TEXT_TYPE); | |
| 131 mText = jsonTextField.getString(ANSWERS_JSON_TEXT); | |
| 132 } | |
| 133 | |
| 134 public int getType() { | |
| 135 return mType; | |
| 136 } | |
| 137 | |
| 138 public String getText() { | |
| 139 return mText; | |
| 140 } | |
| 141 } | |
| 142 } | |
| OLD | NEW |