michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.gfx.BitmapUtils; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.graphics.Bitmap; michael@0: import android.util.Log; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.List; michael@0: michael@0: public class SearchEngine { michael@0: public static final String LOG_TAG = "GeckoSearchEngine"; michael@0: michael@0: public final String name; // Never null. michael@0: public final String identifier; // Can be null. michael@0: michael@0: private final Bitmap icon; michael@0: private volatile List suggestions = new ArrayList(); // Never null. michael@0: michael@0: public SearchEngine(JSONObject engineJSON) throws JSONException { michael@0: if (engineJSON == null) { michael@0: throw new IllegalArgumentException("Can't instantiate SearchEngine from null JSON."); michael@0: } michael@0: michael@0: this.name = getString(engineJSON, "name"); michael@0: if (this.name == null) { michael@0: throw new IllegalArgumentException("Cannot have an unnamed search engine."); michael@0: } michael@0: michael@0: this.identifier = getString(engineJSON, "identifier"); michael@0: michael@0: final String iconURI = getString(engineJSON, "iconURI"); michael@0: if (iconURI == null) { michael@0: Log.w(LOG_TAG, "iconURI is null for search engine " + this.name); michael@0: this.icon = null; michael@0: return; michael@0: } michael@0: this.icon = BitmapUtils.getBitmapFromDataURI(iconURI); michael@0: } michael@0: michael@0: private static String getString(JSONObject data, String key) throws JSONException { michael@0: if (data.isNull(key)) { michael@0: return null; michael@0: } michael@0: return data.getString(key); michael@0: } michael@0: michael@0: /** michael@0: * @return a non-null string suitable for use by FHR. michael@0: */ michael@0: public String getEngineIdentifier() { michael@0: if (this.identifier != null) { michael@0: return this.identifier; michael@0: } michael@0: if (this.name != null) { michael@0: return "other-" + this.name; michael@0: } michael@0: return "other"; michael@0: } michael@0: michael@0: public boolean hasSuggestions() { michael@0: return !this.suggestions.isEmpty(); michael@0: } michael@0: michael@0: public int getSuggestionsCount() { michael@0: return this.suggestions.size(); michael@0: } michael@0: michael@0: public Iterable getSuggestions() { michael@0: return this.suggestions; michael@0: } michael@0: michael@0: public void setSuggestions(List suggestions) { michael@0: if (suggestions == null) { michael@0: this.suggestions = new ArrayList(); michael@0: return; michael@0: } michael@0: this.suggestions = suggestions; michael@0: } michael@0: michael@0: public Bitmap getIcon() { michael@0: return this.icon; michael@0: } michael@0: } michael@0: