Browse Source

Merge pull request #577 from jiaojianhua/upstream

Baidu search suggestion
master
Anthony Restaino 8 years ago committed by GitHub
parent
commit
d7e1cd7c72
  1. 14
      app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java
  2. 1
      app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java
  3. 53
      app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java
  4. 2
      app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java
  5. 15
      app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java
  6. 1
      app/src/main/res/values-zh-rCN/strings.xml
  7. 1
      app/src/main/res/values/arrays.xml
  8. 1
      app/src/main/res/values/strings.xml

14
app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java

@ -117,6 +117,9 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme @@ -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 @@ -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 @@ -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 @@ -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;

1
app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java

@ -70,6 +70,7 @@ public class PreferenceManager { @@ -70,6 +70,7 @@ public class PreferenceManager {
public enum Suggestion {
SUGGESTION_GOOGLE,
SUGGESTION_DUCK,
SUGGESTION_BAIDU,
SUGGESTION_NONE
}

53
app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
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.xmlpull.v1.XmlPullParser;
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<HistoryItem> 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;
}
}
}
}

2
app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java

@ -304,6 +304,8 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable { @@ -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();
}

15
app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java

@ -34,6 +34,21 @@ class SuggestionsManager { @@ -34,6 +34,21 @@ class SuggestionsManager {
});
}
@NonNull
static Single<List<HistoryItem>> createBaiduQueryObservable(@NonNull final String query,
@NonNull final Application application) {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) {
sIsTaskExecuting = true;
List<HistoryItem> results = new BaiduSuggestionsModel(application).getResults(query);
subscriber.onItem(results);
subscriber.onComplete();
sIsTaskExecuting = false;
}
});
}
@NonNull
static Single<List<HistoryItem>> createDuckQueryObservable(@NonNull final String query,
@NonNull final Application application) {

1
app/src/main/res/values-zh-rCN/strings.xml

@ -186,6 +186,7 @@ @@ -186,6 +186,7 @@
<string name="message_certificate_untrusted">不受信任的证书</string>
<string name="powered_by_duck">由DuckDuckGo提供技术支持</string>
<string name="powered_by_baidu">由百度提供技术支持</string>
<string name="search_suggestions_off">关闭搜索建议</string>
<string name="http_proxy">HTTP代理</string>
<string-array name="proxy_choices_array">

1
app/src/main/res/values/arrays.xml

@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
<string-array name="suggestions">
<item>@string/powered_by_google</item>
<item>@string/powered_by_duck</item>
<item>@string/powered_by_baidu</item>
<item>@string/search_suggestions_off</item>
</string-array>

1
app/src/main/res/values/strings.xml

@ -134,6 +134,7 @@ @@ -134,6 +134,7 @@
<string name="search_suggestions">Search suggestions</string>
<string name="powered_by_google">Powered by Google</string>
<string name="powered_by_duck">Powered by DuckDuckGo</string>
<string name="powered_by_baidu">Powered by Baidu</string>
<string name="search_suggestions_off">No search suggestions</string>
<string name="http_proxy">HTTP proxy</string>
<string-array name="proxy_choices_array">

Loading…
Cancel
Save