Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.home; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.gfx.BitmapUtils; |
michael@0 | 9 | |
michael@0 | 10 | import org.json.JSONException; |
michael@0 | 11 | import org.json.JSONObject; |
michael@0 | 12 | |
michael@0 | 13 | import android.graphics.Bitmap; |
michael@0 | 14 | import android.util.Log; |
michael@0 | 15 | |
michael@0 | 16 | import java.util.ArrayList; |
michael@0 | 17 | import java.util.List; |
michael@0 | 18 | |
michael@0 | 19 | public class SearchEngine { |
michael@0 | 20 | public static final String LOG_TAG = "GeckoSearchEngine"; |
michael@0 | 21 | |
michael@0 | 22 | public final String name; // Never null. |
michael@0 | 23 | public final String identifier; // Can be null. |
michael@0 | 24 | |
michael@0 | 25 | private final Bitmap icon; |
michael@0 | 26 | private volatile List<String> suggestions = new ArrayList<String>(); // Never null. |
michael@0 | 27 | |
michael@0 | 28 | public SearchEngine(JSONObject engineJSON) throws JSONException { |
michael@0 | 29 | if (engineJSON == null) { |
michael@0 | 30 | throw new IllegalArgumentException("Can't instantiate SearchEngine from null JSON."); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | this.name = getString(engineJSON, "name"); |
michael@0 | 34 | if (this.name == null) { |
michael@0 | 35 | throw new IllegalArgumentException("Cannot have an unnamed search engine."); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | this.identifier = getString(engineJSON, "identifier"); |
michael@0 | 39 | |
michael@0 | 40 | final String iconURI = getString(engineJSON, "iconURI"); |
michael@0 | 41 | if (iconURI == null) { |
michael@0 | 42 | Log.w(LOG_TAG, "iconURI is null for search engine " + this.name); |
michael@0 | 43 | this.icon = null; |
michael@0 | 44 | return; |
michael@0 | 45 | } |
michael@0 | 46 | this.icon = BitmapUtils.getBitmapFromDataURI(iconURI); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | private static String getString(JSONObject data, String key) throws JSONException { |
michael@0 | 50 | if (data.isNull(key)) { |
michael@0 | 51 | return null; |
michael@0 | 52 | } |
michael@0 | 53 | return data.getString(key); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * @return a non-null string suitable for use by FHR. |
michael@0 | 58 | */ |
michael@0 | 59 | public String getEngineIdentifier() { |
michael@0 | 60 | if (this.identifier != null) { |
michael@0 | 61 | return this.identifier; |
michael@0 | 62 | } |
michael@0 | 63 | if (this.name != null) { |
michael@0 | 64 | return "other-" + this.name; |
michael@0 | 65 | } |
michael@0 | 66 | return "other"; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | public boolean hasSuggestions() { |
michael@0 | 70 | return !this.suggestions.isEmpty(); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | public int getSuggestionsCount() { |
michael@0 | 74 | return this.suggestions.size(); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | public Iterable<String> getSuggestions() { |
michael@0 | 78 | return this.suggestions; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | public void setSuggestions(List<String> suggestions) { |
michael@0 | 82 | if (suggestions == null) { |
michael@0 | 83 | this.suggestions = new ArrayList<String>(); |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | this.suggestions = suggestions; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | public Bitmap getIcon() { |
michael@0 | 90 | return this.icon; |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 |