From 58dac82e7a1b149da95ed4e812285a3453dc73ac Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Fri, 23 Jun 2017 23:31:22 -0400 Subject: [PATCH] Moving search suggestions into their own package --- .../lightning/search/SuggestionsManager.java | 9 +++-- .../BaiduSuggestionsModel.java | 13 +++--- .../BaseSuggestionsModel.java | 40 ++++++++++++------- .../DuckSuggestionsModel.java | 6 +-- .../GoogleSuggestionsModel.java | 6 +-- 5 files changed, 43 insertions(+), 31 deletions(-) rename app/src/main/java/acr/browser/lightning/search/{ => suggestions}/BaiduSuggestionsModel.java (81%) rename app/src/main/java/acr/browser/lightning/search/{ => suggestions}/BaseSuggestionsModel.java (89%) rename app/src/main/java/acr/browser/lightning/search/{ => suggestions}/DuckSuggestionsModel.java (88%) rename app/src/main/java/acr/browser/lightning/search/{ => suggestions}/GoogleSuggestionsModel.java (92%) 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 13edb8c..90bbf6b 100644 --- a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java @@ -10,6 +10,9 @@ import com.anthonycr.bonsai.SingleSubscriber; import java.util.List; import acr.browser.lightning.database.HistoryItem; +import acr.browser.lightning.search.suggestions.BaiduSuggestionsModel; +import acr.browser.lightning.search.suggestions.DuckSuggestionsModel; +import acr.browser.lightning.search.suggestions.GoogleSuggestionsModel; class SuggestionsManager { @@ -26,7 +29,7 @@ class SuggestionsManager { @Override public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { sIsTaskExecuting = true; - List results = new GoogleSuggestionsModel(application).getResults(query); + List results = new GoogleSuggestionsModel(application).fetchResults(query); subscriber.onItem(results); subscriber.onComplete(); sIsTaskExecuting = false; @@ -41,7 +44,7 @@ class SuggestionsManager { @Override public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { sIsTaskExecuting = true; - List results = new BaiduSuggestionsModel(application).getResults(query); + List results = new BaiduSuggestionsModel(application).fetchResults(query); subscriber.onItem(results); subscriber.onComplete(); sIsTaskExecuting = false; @@ -56,7 +59,7 @@ class SuggestionsManager { @Override public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { sIsTaskExecuting = true; - List results = new DuckSuggestionsModel(application).getResults(query); + List results = new DuckSuggestionsModel(application).fetchResults(query); subscriber.onItem(results); subscriber.onComplete(); sIsTaskExecuting = false; diff --git a/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java b/app/src/main/java/acr/browser/lightning/search/suggestions/BaiduSuggestionsModel.java similarity index 81% rename from app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java rename to app/src/main/java/acr/browser/lightning/search/suggestions/BaiduSuggestionsModel.java index 412bfd8..15ff881 100644 --- a/app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java +++ b/app/src/main/java/acr/browser/lightning/search/suggestions/BaiduSuggestionsModel.java @@ -1,11 +1,9 @@ -package acr.browser.lightning.search; +package acr.browser.lightning.search.suggestions; 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; @@ -18,13 +16,12 @@ import acr.browser.lightning.utils.FileUtils; // http://suggestion.baidu.com/s?wd=encodeURIComponent(U)&action=opensearch -class BaiduSuggestionsModel extends BaseSuggestionsModel { +public 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) { + public BaiduSuggestionsModel(@NonNull Application application) { super(application, ENCODING); mSearchSubtitle = application.getString(R.string.suggestion); } @@ -39,12 +36,14 @@ class BaiduSuggestionsModel extends BaseSuggestionsModel { 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)); + suggestion, R.drawable.ic_search)); counter++; + if (counter >= MAX_RESULTS) { break; } diff --git a/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsModel.java b/app/src/main/java/acr/browser/lightning/search/suggestions/BaseSuggestionsModel.java similarity index 89% rename from app/src/main/java/acr/browser/lightning/search/BaseSuggestionsModel.java rename to app/src/main/java/acr/browser/lightning/search/suggestions/BaseSuggestionsModel.java index 7dfcf47..8a86196 100644 --- a/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsModel.java +++ b/app/src/main/java/acr/browser/lightning/search/suggestions/BaseSuggestionsModel.java @@ -1,4 +1,4 @@ -package acr.browser.lightning.search; +package acr.browser.lightning.search.suggestions; import android.app.Application; import android.support.annotation.NonNull; @@ -28,7 +28,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; -abstract class BaseSuggestionsModel { +public abstract class BaseSuggestionsModel { private static final String TAG = "BaseSuggestionsModel"; @@ -57,23 +57,25 @@ abstract class BaseSuggestionsModel { mCacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build(); } + /** + * Retrieves the results for a query. + * + * @param rawQuery the raw query to retrieve the results for. + * @return a list of history items for the query. + */ @NonNull - private static String getLanguage() { - String language = Locale.getDefault().getLanguage(); - if (TextUtils.isEmpty(language)) { - language = DEFAULT_LANGUAGE; - } - return language; - } - - @NonNull - List getResults(@NonNull String query) { + public final List fetchResults(@NonNull final String rawQuery) { List filter = new ArrayList<>(5); + + String query; try { - query = URLEncoder.encode(query, mEncoding); + query = URLEncoder.encode(rawQuery, mEncoding); } catch (UnsupportedEncodingException e) { Log.e(TAG, "Unable to encode the URL", e); + + return filter; } + InputStream inputStream = downloadSuggestionsForQuery(query, mLanguage); if (inputStream == null) { // There are no suggestions for this query, return an empty list. @@ -83,7 +85,6 @@ abstract class BaseSuggestionsModel { parseResults(inputStream, filter); } catch (Exception e) { Log.e(TAG, "Unable to parse results", e); - return filter; } finally { Utils.close(inputStream); } @@ -93,7 +94,7 @@ abstract class BaseSuggestionsModel { /** * This method downloads the search suggestions for the specific query. - * NOTE: This is a blocking operation, do not getResults on the UI thread. + * NOTE: This is a blocking operation, do not fetchResults on the UI thread. * * @param query the query to get suggestions for * @return the cache file containing the suggestions @@ -122,6 +123,15 @@ abstract class BaseSuggestionsModel { return null; } + @NonNull + private static String getLanguage() { + String language = Locale.getDefault().getLanguage(); + if (TextUtils.isEmpty(language)) { + language = DEFAULT_LANGUAGE; + } + return language; + } + private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(@NonNull Chain chain) throws IOException { diff --git a/app/src/main/java/acr/browser/lightning/search/DuckSuggestionsModel.java b/app/src/main/java/acr/browser/lightning/search/suggestions/DuckSuggestionsModel.java similarity index 88% rename from app/src/main/java/acr/browser/lightning/search/DuckSuggestionsModel.java rename to app/src/main/java/acr/browser/lightning/search/suggestions/DuckSuggestionsModel.java index b07b303..89b55dd 100644 --- a/app/src/main/java/acr/browser/lightning/search/DuckSuggestionsModel.java +++ b/app/src/main/java/acr/browser/lightning/search/suggestions/DuckSuggestionsModel.java @@ -1,4 +1,4 @@ -package acr.browser.lightning.search; +package acr.browser.lightning.search.suggestions; import android.app.Application; import android.support.annotation.NonNull; @@ -13,12 +13,12 @@ import acr.browser.lightning.R; import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.utils.FileUtils; -final class DuckSuggestionsModel extends BaseSuggestionsModel { +public final class DuckSuggestionsModel extends BaseSuggestionsModel { @NonNull private static final String ENCODING = "UTF-8"; @NonNull private final String mSearchSubtitle; - DuckSuggestionsModel(@NonNull Application application) { + public DuckSuggestionsModel(@NonNull Application application) { super(application, ENCODING); mSearchSubtitle = application.getString(R.string.suggestion); } diff --git a/app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsModel.java b/app/src/main/java/acr/browser/lightning/search/suggestions/GoogleSuggestionsModel.java similarity index 92% rename from app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsModel.java rename to app/src/main/java/acr/browser/lightning/search/suggestions/GoogleSuggestionsModel.java index ed892a0..40321e9 100644 --- a/app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsModel.java +++ b/app/src/main/java/acr/browser/lightning/search/suggestions/GoogleSuggestionsModel.java @@ -1,4 +1,4 @@ -package acr.browser.lightning.search; +package acr.browser.lightning.search.suggestions; import android.app.Application; import android.support.annotation.NonNull; @@ -15,13 +15,13 @@ import java.util.List; import acr.browser.lightning.R; import acr.browser.lightning.database.HistoryItem; -class GoogleSuggestionsModel extends BaseSuggestionsModel { +public class GoogleSuggestionsModel extends BaseSuggestionsModel { @NonNull private static final String ENCODING = "ISO-8859-1"; @Nullable private static XmlPullParser sXpp; @NonNull private final String mSearchSubtitle; - GoogleSuggestionsModel(@NonNull Application application) { + public GoogleSuggestionsModel(@NonNull Application application) { super(application, ENCODING); mSearchSubtitle = application.getString(R.string.suggestion); }