Bug 769145 - Remove mSuggestEngine and use mSearchEngines for all engines. r=mfinkle

--HG--
extra : rebase_source : 8a4568993cdf11afa917ab19db8c214c0798db8e
This commit is contained in:
Brian Nicholson 2012-07-06 10:49:14 -07:00
parent 6404c76edd
commit ab9868d9e6

View File

@ -60,7 +60,6 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
private static final int SUGGESTION_MAX = 3;
private String mSearchTerm;
private SearchEngine mSuggestEngine;
private ArrayList<SearchEngine> mSearchEngines;
private SuggestClient mSuggestClient;
private AsyncTask<String, Void, ArrayList<String>> mSuggestTask;
@ -236,7 +235,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
}
private int getSuggestEngineCount() {
return (mSearchTerm.length() == 0 || mSuggestEngine == null) ? 0 : 1;
return (mSearchTerm.length() == 0 || mSuggestClient == null) ? 0 : 1;
}
// Add the search engines to the number of reported results.
@ -248,7 +247,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
if (mSearchTerm.length() == 0)
return resultCount;
return resultCount + mSearchEngines.size() + getSuggestEngineCount();
return resultCount + mSearchEngines.size();
}
// If an item is part of the cursor result set, return that entry.
@ -264,14 +263,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
}
// return search engine
return new AwesomeBarSearchEngineItem(getEngine(engineIndex).name);
}
private SearchEngine getEngine(int index) {
final int suggestEngineCount = getSuggestEngineCount();
if (index < suggestEngineCount)
return mSuggestEngine;
return mSearchEngines.get(index - suggestEngineCount);
return new AwesomeBarSearchEngineItem(mSearchEngines.get(engineIndex).name);
}
private int getEngineIndex(int position) {
@ -280,7 +272,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
// return suggest engine index
if (position < suggestEngineCount)
return 0;
return position;
// not an engine
if (position - suggestEngineCount < resultCount)
@ -308,9 +300,8 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
// has the same effect as clicking the single suggestion. If the
// row contains multiple items, clicking the row will do nothing.
int index = getEngineIndex(position);
if (index != -1) {
return getEngine(index).suggestions.isEmpty();
}
if (index != -1)
return mSearchEngines.get(index).suggestions.isEmpty();
return true;
}
@ -333,7 +324,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
viewHolder = (SearchEntryViewHolder) convertView.getTag();
}
bindSearchEngineView(getEngine(getEngineIndex(position)), viewHolder);
bindSearchEngineView(mSearchEngines.get(getEngineIndex(position)), viewHolder);
} else {
AwesomeEntryViewHolder viewHolder = null;
@ -444,30 +435,13 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
}
};
/**
* Sets the suggest engine, which will show suggestions for user-entered queries.
* If the suggest engine has already been set, it will be replaced, and its
* suggestions will be copied to the new suggest engine.
*/
public void setSuggestEngine(String name, Drawable icon) {
// We currently save the suggest engine in shared preferences, so this
// method is called immediately when the AwesomeBar is created. It's
// called again in setSuggestions(), when the list of search engines is
// received from Gecko (in case the suggestion engine has changed).
final SearchEngine suggestEngine = new SearchEngine(name, icon);
if (mSuggestEngine != null)
suggestEngine.suggestions = mSuggestEngine.suggestions;
mSuggestEngine = suggestEngine;
}
/**
* Sets suggestions associated with the current suggest engine.
* If there is no suggest engine, this does nothing.
*/
public void setSuggestions(final ArrayList<String> suggestions) {
if (mSuggestEngine != null) {
mSuggestEngine.suggestions = suggestions;
if (mSuggestClient != null) {
mSearchEngines.get(0).suggestions = suggestions;
getCursorAdapter().notifyDataSetChanged();
}
}
@ -475,27 +449,31 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
/**
* Sets search engines to be shown for user-entered queries.
*/
public void setSearchEngines(String suggestEngine, JSONArray engines) {
mSearchEngines = new ArrayList<SearchEngine>();
for (int i = 0; i < engines.length(); i++) {
try {
public void setSearchEngines(JSONObject data) {
try {
String suggestEngine = data.isNull("suggestEngine") ? null : data.getString("suggestEngine");
String suggestTemplate = data.isNull("suggestTemplate") ? null : data.getString("suggestTemplate");
JSONArray engines = data.getJSONArray("searchEngines");
mSearchEngines = new ArrayList<SearchEngine>();
for (int i = 0; i < engines.length(); i++) {
JSONObject engineJSON = engines.getJSONObject(i);
String name = engineJSON.getString("name");
String iconURI = engineJSON.getString("iconURI");
Drawable icon = getDrawableFromDataURI(iconURI);
if (name.equals(suggestEngine)) {
setSuggestEngine(name, icon);
if (name.equals(suggestEngine) && suggestTemplate != null) {
// suggest engine should be at the front of the list
mSearchEngines.add(0, new SearchEngine(name, icon));
mSuggestClient = new SuggestClient(GeckoApp.mAppContext, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX);
} else {
mSearchEngines.add(new SearchEngine(name, icon));
}
} catch (JSONException e) {
Log.e(LOGTAG, "Error getting search engine JSON", e);
return;
}
} catch (JSONException e) {
Log.e(LOGTAG, "Error getting search engine JSON", e);
}
AwesomeBarCursorAdapter adapter = getCursorAdapter();
if (adapter != null)
adapter.notifyDataSetChanged();
filter(mSearchTerm);
}
private Drawable getDrawableFromDataURI(String dataURI) {
@ -512,23 +490,13 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
return drawable;
}
public void handleMessage(String event, JSONObject message) {
try {
if (event.equals("SearchEngines:Data")) {
final String suggestEngine = message.isNull("suggestEngine") ? null : message.getString("suggestEngine");
final String suggestTemplate = message.isNull("suggestTemplate") ? null : message.getString("suggestTemplate");
final JSONArray engines = message.getJSONArray("searchEngines");
if (suggestTemplate != null)
mSuggestClient = new SuggestClient(GeckoApp.mAppContext, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX);
GeckoAppShell.getMainHandler().post(new Runnable() {
public void run() {
setSearchEngines(suggestEngine, engines);
}
});
}
} catch (Exception e) {
// do nothing
Log.i(LOGTAG, "handleMessage throws " + e + " for message: " + event);
public void handleMessage(String event, final JSONObject message) {
if (event.equals("SearchEngines:Data")) {
GeckoAppShell.getMainHandler().post(new Runnable() {
public void run() {
setSearchEngines(message);
}
});
}
}