|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import org.mozilla.gecko.gfx.BitmapUtils; |
|
9 |
|
10 import org.json.JSONException; |
|
11 import org.json.JSONObject; |
|
12 |
|
13 import android.graphics.Bitmap; |
|
14 import android.util.Log; |
|
15 |
|
16 import java.util.ArrayList; |
|
17 import java.util.List; |
|
18 |
|
19 public class SearchEngine { |
|
20 public static final String LOG_TAG = "GeckoSearchEngine"; |
|
21 |
|
22 public final String name; // Never null. |
|
23 public final String identifier; // Can be null. |
|
24 |
|
25 private final Bitmap icon; |
|
26 private volatile List<String> suggestions = new ArrayList<String>(); // Never null. |
|
27 |
|
28 public SearchEngine(JSONObject engineJSON) throws JSONException { |
|
29 if (engineJSON == null) { |
|
30 throw new IllegalArgumentException("Can't instantiate SearchEngine from null JSON."); |
|
31 } |
|
32 |
|
33 this.name = getString(engineJSON, "name"); |
|
34 if (this.name == null) { |
|
35 throw new IllegalArgumentException("Cannot have an unnamed search engine."); |
|
36 } |
|
37 |
|
38 this.identifier = getString(engineJSON, "identifier"); |
|
39 |
|
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 } |
|
48 |
|
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 } |
|
55 |
|
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 } |
|
68 |
|
69 public boolean hasSuggestions() { |
|
70 return !this.suggestions.isEmpty(); |
|
71 } |
|
72 |
|
73 public int getSuggestionsCount() { |
|
74 return this.suggestions.size(); |
|
75 } |
|
76 |
|
77 public Iterable<String> getSuggestions() { |
|
78 return this.suggestions; |
|
79 } |
|
80 |
|
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 } |
|
88 |
|
89 public Bitmap getIcon() { |
|
90 return this.icon; |
|
91 } |
|
92 } |
|
93 |