From a7147e726512f2e86037d9aa9a921667c6b8b40a Mon Sep 17 00:00:00 2001 From: Jiao Jianhua <16467515@qq.com> Date: Wed, 31 May 2017 21:14:29 +0800 Subject: [PATCH 1/2] Add Baidu searc suggestion --- .../fragment/GeneralSettingsFragment.java | 14 ++++- .../preference/PreferenceManager.java | 1 + .../search/BaiduSuggestionsModel.java | 57 +++++++++++++++++++ .../lightning/search/SuggestionsAdapter.java | 2 + .../lightning/search/SuggestionsManager.java | 15 +++++ app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/arrays.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java diff --git a/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java index e6a0398..368e3fa 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java @@ -117,6 +117,9 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme case SUGGESTION_DUCK: searchsSuggestions.setSummary(R.string.powered_by_duck); break; + case SUGGESTION_BAIDU: + searchsSuggestions.setSummary(R.string.powered_by_baidu); + break; case SUGGESTION_NONE: searchsSuggestions.setSummary(R.string.search_suggestions_off); break; @@ -360,7 +363,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme AlertDialog.Builder picker = new AlertDialog.Builder(mActivity); picker.setTitle(getResources().getString(R.string.search_suggestions)); - int currentChoice = 2; + int currentChoice = 3; switch (mPreferenceManager.getSearchSuggestionChoice()) { case SUGGESTION_GOOGLE: @@ -369,9 +372,12 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme case SUGGESTION_DUCK: currentChoice = 1; break; - case SUGGESTION_NONE: + case SUGGESTION_BAIDU: currentChoice = 2; break; + case SUGGESTION_NONE: + currentChoice = 3; + break; } picker.setSingleChoiceItems(R.array.suggestions, currentChoice, @@ -388,6 +394,10 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme searchsSuggestions.setSummary(R.string.powered_by_duck); break; case 2: + mPreferenceManager.setSearchSuggestionChoice(Suggestion.SUGGESTION_BAIDU); + searchsSuggestions.setSummary(R.string.powered_by_baidu); + break; + case 3: mPreferenceManager.setSearchSuggestionChoice(Suggestion.SUGGESTION_NONE); searchsSuggestions.setSummary(R.string.search_suggestions_off); break; diff --git a/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java b/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java index 42d4d24..8be0ac5 100644 --- a/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java +++ b/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java @@ -70,6 +70,7 @@ public class PreferenceManager { public enum Suggestion { SUGGESTION_GOOGLE, SUGGESTION_DUCK, + SUGGESTION_BAIDU, SUGGESTION_NONE } diff --git a/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java b/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java new file mode 100644 index 0000000..6800183 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java @@ -0,0 +1,57 @@ +package acr.browser.lightning.search; + +import android.app.Application; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; + +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.util.List; + +import acr.browser.lightning.R; +import acr.browser.lightning.database.HistoryItem; +import acr.browser.lightning.utils.FileUtils; + +// http://unionsug.baidu.com/su?wd=encodeURIComponent(U) +// http://suggestion.baidu.com/s?wd=encodeURIComponent(U)&action=opensearch + + +class BaiduSuggestionsModel extends BaseSuggestionsModel { + + @NonNull private static final String ENCODING = "UTF-8"; + @Nullable private static XmlPullParser sXpp; + @NonNull private final String mSearchSubtitle; + + BaiduSuggestionsModel(@NonNull Application application) { + super(application, ENCODING); + mSearchSubtitle = application.getString(R.string.suggestion); + } + + @NonNull + protected String createQueryUrl(@NonNull String query, @NonNull String language) { + return "http://suggestion.baidu.com/s?wd=" + query + "&action=opensearch"; + } + + @Override + protected void parseResults(@NonNull InputStream inputStream, @NonNull List results) throws Exception { + String content = FileUtils.readStringFromStream(inputStream, "GBK"); + JSONArray respArray = new JSONArray(content); + JSONArray jsonArray = respArray.getJSONArray(1); + int counter = 0; + for (int n = 0, size = jsonArray.length(); n < size; n++) { + String suggestion = jsonArray.getString(n); + results.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"', + suggestion, R.drawable.ic_search)); + counter++; + if (counter >= MAX_RESULTS) { + break; + } + } + } +} diff --git a/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java b/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java index 2937f70..dae2a33 100644 --- a/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java @@ -304,6 +304,8 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable { return SuggestionsManager.createGoogleQueryObservable(query, mApplication); } else if (mSuggestionChoice == PreferenceManager.Suggestion.SUGGESTION_DUCK) { return SuggestionsManager.createDuckQueryObservable(query, mApplication); + } else if (mSuggestionChoice == PreferenceManager.Suggestion.SUGGESTION_BAIDU) { + return SuggestionsManager.createBaiduQueryObservable(query, mApplication); } else { return Single.empty(); } diff --git a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java index 2855f40..13edb8c 100644 --- a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java @@ -34,6 +34,21 @@ class SuggestionsManager { }); } + @NonNull + static Single> createBaiduQueryObservable(@NonNull final String query, + @NonNull final Application application) { + return Single.create(new SingleAction>() { + @Override + public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { + sIsTaskExecuting = true; + List results = new BaiduSuggestionsModel(application).getResults(query); + subscriber.onItem(results); + subscriber.onComplete(); + sIsTaskExecuting = false; + } + }); + } + @NonNull static Single> createDuckQueryObservable(@NonNull final String query, @NonNull final Application application) { diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index af1b7e2..a947e3f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -186,6 +186,7 @@ 不受信任的证书 由DuckDuckGo提供技术支持 + 由百度提供技术支持 关闭搜索建议 HTTP代理 diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index d3d6716..0577edd 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -22,6 +22,7 @@ @string/powered_by_google @string/powered_by_duck + @string/powered_by_baidu @string/search_suggestions_off diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ee47f61..906d4f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -133,6 +133,7 @@ Search suggestions Powered by Google Powered by DuckDuckGo + Powered by Baidu No search suggestions HTTP proxy From af58c93ed5eccdcffafabf9ce1980cddc395a0cd Mon Sep 17 00:00:00 2001 From: Jiao Jianhua <16467515@qq.com> Date: Wed, 31 May 2017 21:35:14 +0800 Subject: [PATCH 2/2] New: Baidu search suggestion --- .../acr/browser/lightning/search/BaiduSuggestionsModel.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java b/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java index 6800183..412bfd8 100644 --- a/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java +++ b/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java @@ -5,12 +5,8 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import org.json.JSONArray; -import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; -import org.xmlpull.v1.XmlPullParserFactory; -import java.io.BufferedInputStream; import java.io.InputStream; import java.util.List;