Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.gecko.home;
8 import org.mozilla.gecko.gfx.BitmapUtils;
10 import org.json.JSONException;
11 import org.json.JSONObject;
13 import android.graphics.Bitmap;
14 import android.util.Log;
16 import java.util.ArrayList;
17 import java.util.List;
19 public class SearchEngine {
20 public static final String LOG_TAG = "GeckoSearchEngine";
22 public final String name; // Never null.
23 public final String identifier; // Can be null.
25 private final Bitmap icon;
26 private volatile List<String> suggestions = new ArrayList<String>(); // Never null.
28 public SearchEngine(JSONObject engineJSON) throws JSONException {
29 if (engineJSON == null) {
30 throw new IllegalArgumentException("Can't instantiate SearchEngine from null JSON.");
31 }
33 this.name = getString(engineJSON, "name");
34 if (this.name == null) {
35 throw new IllegalArgumentException("Cannot have an unnamed search engine.");
36 }
38 this.identifier = getString(engineJSON, "identifier");
40 final String iconURI = getString(engineJSON, "iconURI");
41 if (iconURI == null) {
42 Log.w(LOG_TAG, "iconURI is null for search engine " + this.name);
43 this.icon = null;
44 return;
45 }
46 this.icon = BitmapUtils.getBitmapFromDataURI(iconURI);
47 }
49 private static String getString(JSONObject data, String key) throws JSONException {
50 if (data.isNull(key)) {
51 return null;
52 }
53 return data.getString(key);
54 }
56 /**
57 * @return a non-null string suitable for use by FHR.
58 */
59 public String getEngineIdentifier() {
60 if (this.identifier != null) {
61 return this.identifier;
62 }
63 if (this.name != null) {
64 return "other-" + this.name;
65 }
66 return "other";
67 }
69 public boolean hasSuggestions() {
70 return !this.suggestions.isEmpty();
71 }
73 public int getSuggestionsCount() {
74 return this.suggestions.size();
75 }
77 public Iterable<String> getSuggestions() {
78 return this.suggestions;
79 }
81 public void setSuggestions(List<String> suggestions) {
82 if (suggestions == null) {
83 this.suggestions = new ArrayList<String>();
84 return;
85 }
86 this.suggestions = suggestions;
87 }
89 public Bitmap getIcon() {
90 return this.icon;
91 }
92 }