mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-05 05:30:29 +00:00
Bug 1048525 - Use SuggestClient from MC. r=margaret
--HG-- rename : mobile/android/base/home/SuggestClient.java => mobile/android/base/SuggestClient.java
This commit is contained in:
parent
d64b0cbea4
commit
1bdffeaf7d
@ -2,10 +2,12 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.home;
|
||||
package org.mozilla.gecko;
|
||||
|
||||
import org.mozilla.gecko.AppConstants;
|
||||
import org.mozilla.gecko.GeckoAppShell;
|
||||
import org.mozilla.gecko.mozglue.RobocopTarget;
|
||||
import org.mozilla.gecko.util.HardwareUtils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
|
||||
@ -28,7 +30,11 @@ import java.util.ArrayList;
|
||||
*/
|
||||
public class SuggestClient {
|
||||
private static final String LOGTAG = "GeckoSuggestClient";
|
||||
private static final String USER_AGENT = GeckoAppShell.getGeckoInterface().getDefaultUAString();
|
||||
|
||||
// This should go through GeckoInterface to get the UA, but the search activity
|
||||
// doesn't use a GeckoView yet. Until it does, get the UA directly.
|
||||
private static final String USER_AGENT = HardwareUtils.isTablet() ?
|
||||
AppConstants.USER_AGENT_FENNEC_TABLET : AppConstants.USER_AGENT_FENNEC_MOBILE;
|
||||
|
||||
private final Context mContext;
|
||||
private final int mTimeout;
|
||||
@ -112,7 +118,7 @@ public class SuggestClient {
|
||||
*/
|
||||
JSONArray results = new JSONArray(json);
|
||||
JSONArray jsonSuggestions = results.getJSONArray(1);
|
||||
|
||||
|
||||
int added = 0;
|
||||
for (int i = 0; (i < jsonSuggestions.length()) && (added < mMaxResults); i++) {
|
||||
String suggestion = jsonSuggestions.getString(i);
|
@ -19,6 +19,7 @@ import org.mozilla.gecko.GeckoAppShell;
|
||||
import org.mozilla.gecko.GeckoEvent;
|
||||
import org.mozilla.gecko.PrefsHelper;
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.SuggestClient;
|
||||
import org.mozilla.gecko.Tab;
|
||||
import org.mozilla.gecko.Tabs;
|
||||
import org.mozilla.gecko.Telemetry;
|
||||
|
@ -302,7 +302,6 @@ gbjar.sources += [
|
||||
'home/SearchEngineRow.java',
|
||||
'home/SearchLoader.java',
|
||||
'home/SimpleCursorLoader.java',
|
||||
'home/SuggestClient.java',
|
||||
'home/TabMenuStrip.java',
|
||||
'home/TabMenuStripLayout.java',
|
||||
'home/TopSitesGridItemView.java',
|
||||
@ -379,6 +378,7 @@ gbjar.sources += [
|
||||
'sqlite/MatrixBlobCursor.java',
|
||||
'sqlite/SQLiteBridge.java',
|
||||
'sqlite/SQLiteBridgeException.java',
|
||||
'SuggestClient.java',
|
||||
'SurfaceBits.java',
|
||||
'Tab.java',
|
||||
'Tabs.java',
|
||||
|
@ -5,8 +5,8 @@ import java.util.HashMap;
|
||||
|
||||
import org.mozilla.gecko.Actions;
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.SuggestClient;
|
||||
import org.mozilla.gecko.home.BrowserSearch;
|
||||
import org.mozilla.gecko.home.SuggestClient;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
@ -1,145 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.search.autocomplete;
|
||||
|
||||
import org.json.JSONArray;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Use network-based search suggestions.
|
||||
*/
|
||||
public class SuggestClient {
|
||||
private static final String LOGTAG = "GeckoSuggestClient";
|
||||
private static final String USER_AGENT = "";
|
||||
|
||||
private final Context mContext;
|
||||
private final int mTimeout;
|
||||
|
||||
// should contain the string "__searchTerms__", which is replaced with the query
|
||||
private final String mSuggestTemplate;
|
||||
|
||||
// the maximum number of suggestions to return
|
||||
private final int mMaxResults;
|
||||
|
||||
// used by robocop for testing
|
||||
private boolean mCheckNetwork;
|
||||
|
||||
// used to make suggestions appear instantly after opt-in
|
||||
private String mPrevQuery;
|
||||
private ArrayList<String> mPrevResults;
|
||||
|
||||
public SuggestClient(Context context, String suggestTemplate, int timeout, int maxResults) {
|
||||
mContext = context;
|
||||
mMaxResults = maxResults;
|
||||
mSuggestTemplate = suggestTemplate;
|
||||
mTimeout = timeout;
|
||||
mCheckNetwork = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries for a given search term and returns an ArrayList of suggestions.
|
||||
*/
|
||||
public ArrayList<String> query(String query) {
|
||||
if (query.equals(mPrevQuery))
|
||||
return mPrevResults;
|
||||
|
||||
ArrayList<String> suggestions = new ArrayList<String>();
|
||||
if (TextUtils.isEmpty(mSuggestTemplate) || TextUtils.isEmpty(query)) {
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
if (!isNetworkConnected() && mCheckNetwork) {
|
||||
Log.i(LOGTAG, "Not connected to network");
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
try {
|
||||
String encoded = URLEncoder.encode(query, "UTF-8");
|
||||
String suggestUri = mSuggestTemplate.replace("__searchTerms__", encoded);
|
||||
|
||||
URL url = new URL(suggestUri);
|
||||
String json = null;
|
||||
HttpURLConnection urlConnection = null;
|
||||
InputStream in = null;
|
||||
try {
|
||||
urlConnection = (HttpURLConnection) url.openConnection();
|
||||
urlConnection.setConnectTimeout(mTimeout);
|
||||
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
|
||||
in = new BufferedInputStream(urlConnection.getInputStream());
|
||||
json = convertStreamToString(in);
|
||||
} finally {
|
||||
if (urlConnection != null)
|
||||
urlConnection.disconnect();
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(LOGTAG, "error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (json != null) {
|
||||
/*
|
||||
* Sample result:
|
||||
* ["foo",["food network","foothill college","foot locker",...]]
|
||||
*/
|
||||
JSONArray results = new JSONArray(json);
|
||||
JSONArray jsonSuggestions = results.getJSONArray(1);
|
||||
|
||||
int added = 0;
|
||||
for (int i = 0; (i < jsonSuggestions.length()) && (added < mMaxResults); i++) {
|
||||
String suggestion = jsonSuggestions.getString(i);
|
||||
if (!suggestion.equalsIgnoreCase(query)) {
|
||||
suggestions.add(suggestion);
|
||||
added++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.e(LOGTAG, "Suggestion query failed");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(LOGTAG, "Error", e);
|
||||
}
|
||||
|
||||
mPrevQuery = query;
|
||||
mPrevResults = suggestions;
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
private boolean isNetworkConnected() {
|
||||
NetworkInfo networkInfo = getActiveNetworkInfo();
|
||||
return networkInfo != null && networkInfo.isConnected();
|
||||
}
|
||||
|
||||
private NetworkInfo getActiveNetworkInfo() {
|
||||
ConnectivityManager connectivity = (ConnectivityManager) mContext
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (connectivity == null)
|
||||
return null;
|
||||
return connectivity.getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
private String convertStreamToString(java.io.InputStream is) {
|
||||
try {
|
||||
return new java.util.Scanner(is).useDelimiter("\\A").next();
|
||||
} catch (java.util.NoSuchElementException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import org.mozilla.gecko.SuggestClient;
|
||||
import org.mozilla.gecko.Telemetry;
|
||||
import org.mozilla.gecko.TelemetryContract;
|
||||
import org.mozilla.search.AcceptsSearchQuery;
|
||||
|
@ -8,7 +8,6 @@ search_activity_sources = [
|
||||
'java/org/mozilla/search/AcceptsSearchQuery.java',
|
||||
'java/org/mozilla/search/autocomplete/AutoCompleteAdapter.java',
|
||||
'java/org/mozilla/search/autocomplete/ClearableEditText.java',
|
||||
'java/org/mozilla/search/autocomplete/SuggestClient.java',
|
||||
'java/org/mozilla/search/autocomplete/SuggestionsFragment.java',
|
||||
'java/org/mozilla/search/Constants.java',
|
||||
'java/org/mozilla/search/MainActivity.java',
|
||||
|
Loading…
x
Reference in New Issue
Block a user