Browse Source

Making encoding a constructor parameter

master
anthony restaino 8 years ago
parent
commit
103eea9fcb
  1. 11
      app/src/main/java/acr/browser/lightning/search/BaseSuggestionsModel.java
  2. 8
      app/src/main/java/acr/browser/lightning/search/DuckSuggestionsModel.java
  3. 8
      app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsModel.java

11
app/src/main/java/acr/browser/lightning/search/BaseSuggestionsModel.java

@ -37,16 +37,15 @@ abstract class BaseSuggestionsModel {
@Nullable private static String sLanguage; @Nullable private static String sLanguage;
@NonNull private final OkHttpClient mHttpClient; @NonNull private final OkHttpClient mHttpClient;
@NonNull private final CacheControl mCacheControl; @NonNull private final CacheControl mCacheControl;
@NonNull private final String mEncoding;
@NonNull @NonNull
protected abstract String createQueryUrl(@NonNull String query, @NonNull String language); protected abstract String createQueryUrl(@NonNull String query, @NonNull String language);
protected abstract void parseResults(@NonNull InputStream inputStream, @NonNull List<HistoryItem> results) throws Exception; protected abstract void parseResults(@NonNull InputStream inputStream, @NonNull List<HistoryItem> results) throws Exception;
@NonNull BaseSuggestionsModel(@NonNull Application application, @NonNull String encoding) {
protected abstract String getEncoding(); mEncoding = encoding;
BaseSuggestionsModel(@NonNull Application application) {
File suggestionsCache = new File(application.getCacheDir(), "suggestion_responses"); File suggestionsCache = new File(application.getCacheDir(), "suggestion_responses");
mHttpClient = new OkHttpClient.Builder() mHttpClient = new OkHttpClient.Builder()
.cache(new Cache(suggestionsCache, FileUtils.megabytesToBytes(1))) .cache(new Cache(suggestionsCache, FileUtils.megabytesToBytes(1)))
@ -70,7 +69,7 @@ abstract class BaseSuggestionsModel {
List<HistoryItem> getResults(@NonNull String query) { List<HistoryItem> getResults(@NonNull String query) {
List<HistoryItem> filter = new ArrayList<>(5); List<HistoryItem> filter = new ArrayList<>(5);
try { try {
query = URLEncoder.encode(query, getEncoding()); query = URLEncoder.encode(query, mEncoding);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unable to encode the URL", e); Log.e(TAG, "Unable to encode the URL", e);
} }
@ -107,7 +106,7 @@ abstract class BaseSuggestionsModel {
// OkHttp automatically gzips requests // OkHttp automatically gzips requests
Request suggestionsRequest = new Request.Builder().url(url) Request suggestionsRequest = new Request.Builder().url(url)
.addHeader("Accept-Charset", getEncoding()) .addHeader("Accept-Charset", mEncoding)
.cacheControl(mCacheControl) .cacheControl(mCacheControl)
.build(); .build();

8
app/src/main/java/acr/browser/lightning/search/DuckSuggestionsModel.java

@ -20,7 +20,7 @@ final class DuckSuggestionsModel extends BaseSuggestionsModel {
@NonNull private final String mSearchSubtitle; @NonNull private final String mSearchSubtitle;
DuckSuggestionsModel(@NonNull Application application) { DuckSuggestionsModel(@NonNull Application application) {
super(application); super(application, ENCODING);
mSearchSubtitle = application.getString(R.string.suggestion); mSearchSubtitle = application.getString(R.string.suggestion);
} }
@ -47,10 +47,4 @@ final class DuckSuggestionsModel extends BaseSuggestionsModel {
} }
} }
@NonNull
@Override
protected String getEncoding() {
return ENCODING;
}
} }

8
app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsModel.java

@ -23,7 +23,7 @@ class GoogleSuggestionsModel extends BaseSuggestionsModel {
@NonNull private final String mSearchSubtitle; @NonNull private final String mSearchSubtitle;
GoogleSuggestionsModel(@NonNull Application application) { GoogleSuggestionsModel(@NonNull Application application) {
super(application); super(application, ENCODING);
mSearchSubtitle = application.getString(R.string.suggestion); mSearchSubtitle = application.getString(R.string.suggestion);
} }
@ -54,12 +54,6 @@ class GoogleSuggestionsModel extends BaseSuggestionsModel {
} }
} }
@NonNull
@Override
protected String getEncoding() {
return ENCODING;
}
@NonNull @NonNull
private static synchronized XmlPullParser getParser() throws XmlPullParserException { private static synchronized XmlPullParser getParser() throws XmlPullParserException {
if (sXpp == null) { if (sXpp == null) {

Loading…
Cancel
Save