1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/SearchEngine.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.mozilla.gecko.gfx.BitmapUtils; 1.12 + 1.13 +import org.json.JSONException; 1.14 +import org.json.JSONObject; 1.15 + 1.16 +import android.graphics.Bitmap; 1.17 +import android.util.Log; 1.18 + 1.19 +import java.util.ArrayList; 1.20 +import java.util.List; 1.21 + 1.22 +public class SearchEngine { 1.23 + public static final String LOG_TAG = "GeckoSearchEngine"; 1.24 + 1.25 + public final String name; // Never null. 1.26 + public final String identifier; // Can be null. 1.27 + 1.28 + private final Bitmap icon; 1.29 + private volatile List<String> suggestions = new ArrayList<String>(); // Never null. 1.30 + 1.31 + public SearchEngine(JSONObject engineJSON) throws JSONException { 1.32 + if (engineJSON == null) { 1.33 + throw new IllegalArgumentException("Can't instantiate SearchEngine from null JSON."); 1.34 + } 1.35 + 1.36 + this.name = getString(engineJSON, "name"); 1.37 + if (this.name == null) { 1.38 + throw new IllegalArgumentException("Cannot have an unnamed search engine."); 1.39 + } 1.40 + 1.41 + this.identifier = getString(engineJSON, "identifier"); 1.42 + 1.43 + final String iconURI = getString(engineJSON, "iconURI"); 1.44 + if (iconURI == null) { 1.45 + Log.w(LOG_TAG, "iconURI is null for search engine " + this.name); 1.46 + this.icon = null; 1.47 + return; 1.48 + } 1.49 + this.icon = BitmapUtils.getBitmapFromDataURI(iconURI); 1.50 + } 1.51 + 1.52 + private static String getString(JSONObject data, String key) throws JSONException { 1.53 + if (data.isNull(key)) { 1.54 + return null; 1.55 + } 1.56 + return data.getString(key); 1.57 + } 1.58 + 1.59 + /** 1.60 + * @return a non-null string suitable for use by FHR. 1.61 + */ 1.62 + public String getEngineIdentifier() { 1.63 + if (this.identifier != null) { 1.64 + return this.identifier; 1.65 + } 1.66 + if (this.name != null) { 1.67 + return "other-" + this.name; 1.68 + } 1.69 + return "other"; 1.70 + } 1.71 + 1.72 + public boolean hasSuggestions() { 1.73 + return !this.suggestions.isEmpty(); 1.74 + } 1.75 + 1.76 + public int getSuggestionsCount() { 1.77 + return this.suggestions.size(); 1.78 + } 1.79 + 1.80 + public Iterable<String> getSuggestions() { 1.81 + return this.suggestions; 1.82 + } 1.83 + 1.84 + public void setSuggestions(List<String> suggestions) { 1.85 + if (suggestions == null) { 1.86 + this.suggestions = new ArrayList<String>(); 1.87 + return; 1.88 + } 1.89 + this.suggestions = suggestions; 1.90 + } 1.91 + 1.92 + public Bitmap getIcon() { 1.93 + return this.icon; 1.94 + } 1.95 +} 1.96 +