Browse Source

Moving search suggestions into their own package

master
anthony restaino 7 years ago
parent
commit
58dac82e7a
  1. 9
      app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java
  2. 11
      app/src/main/java/acr/browser/lightning/search/suggestions/BaiduSuggestionsModel.java
  3. 40
      app/src/main/java/acr/browser/lightning/search/suggestions/BaseSuggestionsModel.java
  4. 6
      app/src/main/java/acr/browser/lightning/search/suggestions/DuckSuggestionsModel.java
  5. 6
      app/src/main/java/acr/browser/lightning/search/suggestions/GoogleSuggestionsModel.java

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

@ -10,6 +10,9 @@ import com.anthonycr.bonsai.SingleSubscriber;
import java.util.List; import java.util.List;
import acr.browser.lightning.database.HistoryItem; 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 { class SuggestionsManager {
@ -26,7 +29,7 @@ class SuggestionsManager {
@Override @Override
public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) { public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) {
sIsTaskExecuting = true; sIsTaskExecuting = true;
List<HistoryItem> results = new GoogleSuggestionsModel(application).getResults(query); List<HistoryItem> results = new GoogleSuggestionsModel(application).fetchResults(query);
subscriber.onItem(results); subscriber.onItem(results);
subscriber.onComplete(); subscriber.onComplete();
sIsTaskExecuting = false; sIsTaskExecuting = false;
@ -41,7 +44,7 @@ class SuggestionsManager {
@Override @Override
public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) { public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) {
sIsTaskExecuting = true; sIsTaskExecuting = true;
List<HistoryItem> results = new BaiduSuggestionsModel(application).getResults(query); List<HistoryItem> results = new BaiduSuggestionsModel(application).fetchResults(query);
subscriber.onItem(results); subscriber.onItem(results);
subscriber.onComplete(); subscriber.onComplete();
sIsTaskExecuting = false; sIsTaskExecuting = false;
@ -56,7 +59,7 @@ class SuggestionsManager {
@Override @Override
public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) { public void onSubscribe(@NonNull final SingleSubscriber<List<HistoryItem>> subscriber) {
sIsTaskExecuting = true; sIsTaskExecuting = true;
List<HistoryItem> results = new DuckSuggestionsModel(application).getResults(query); List<HistoryItem> results = new DuckSuggestionsModel(application).fetchResults(query);
subscriber.onItem(results); subscriber.onItem(results);
subscriber.onComplete(); subscriber.onComplete();
sIsTaskExecuting = false; sIsTaskExecuting = false;

11
app/src/main/java/acr/browser/lightning/search/BaiduSuggestionsModel.java → 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.app.Application;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.json.JSONArray; import org.json.JSONArray;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
@ -18,13 +16,12 @@ import acr.browser.lightning.utils.FileUtils;
// http://suggestion.baidu.com/s?wd=encodeURIComponent(U)&action=opensearch // 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"; @NonNull private static final String ENCODING = "UTF-8";
@Nullable private static XmlPullParser sXpp;
@NonNull private final String mSearchSubtitle; @NonNull private final String mSearchSubtitle;
BaiduSuggestionsModel(@NonNull Application application) { public BaiduSuggestionsModel(@NonNull Application application) {
super(application, ENCODING); super(application, ENCODING);
mSearchSubtitle = application.getString(R.string.suggestion); mSearchSubtitle = application.getString(R.string.suggestion);
} }
@ -39,12 +36,14 @@ class BaiduSuggestionsModel extends BaseSuggestionsModel {
String content = FileUtils.readStringFromStream(inputStream, "GBK"); String content = FileUtils.readStringFromStream(inputStream, "GBK");
JSONArray respArray = new JSONArray(content); JSONArray respArray = new JSONArray(content);
JSONArray jsonArray = respArray.getJSONArray(1); JSONArray jsonArray = respArray.getJSONArray(1);
int counter = 0; int counter = 0;
for (int n = 0, size = jsonArray.length(); n < size; n++) { for (int n = 0, size = jsonArray.length(); n < size; n++) {
String suggestion = jsonArray.getString(n); String suggestion = jsonArray.getString(n);
results.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"', results.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"',
suggestion, R.drawable.ic_search)); suggestion, R.drawable.ic_search));
counter++; counter++;
if (counter >= MAX_RESULTS) { if (counter >= MAX_RESULTS) {
break; break;
} }

40
app/src/main/java/acr/browser/lightning/search/BaseSuggestionsModel.java → 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.app.Application;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -28,7 +28,7 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
abstract class BaseSuggestionsModel { public abstract class BaseSuggestionsModel {
private static final String TAG = "BaseSuggestionsModel"; private static final String TAG = "BaseSuggestionsModel";
@ -57,23 +57,25 @@ abstract class BaseSuggestionsModel {
mCacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build(); 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 @NonNull
private static String getLanguage() { public final List<HistoryItem> fetchResults(@NonNull final String rawQuery) {
String language = Locale.getDefault().getLanguage();
if (TextUtils.isEmpty(language)) {
language = DEFAULT_LANGUAGE;
}
return language;
}
@NonNull
List<HistoryItem> getResults(@NonNull String query) {
List<HistoryItem> filter = new ArrayList<>(5); List<HistoryItem> filter = new ArrayList<>(5);
String query;
try { try {
query = URLEncoder.encode(query, mEncoding); query = URLEncoder.encode(rawQuery, 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);
return filter;
} }
InputStream inputStream = downloadSuggestionsForQuery(query, mLanguage); InputStream inputStream = downloadSuggestionsForQuery(query, mLanguage);
if (inputStream == null) { if (inputStream == null) {
// There are no suggestions for this query, return an empty list. // There are no suggestions for this query, return an empty list.
@ -83,7 +85,6 @@ abstract class BaseSuggestionsModel {
parseResults(inputStream, filter); parseResults(inputStream, filter);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Unable to parse results", e); Log.e(TAG, "Unable to parse results", e);
return filter;
} finally { } finally {
Utils.close(inputStream); Utils.close(inputStream);
} }
@ -93,7 +94,7 @@ abstract class BaseSuggestionsModel {
/** /**
* This method downloads the search suggestions for the specific query. * 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 * @param query the query to get suggestions for
* @return the cache file containing the suggestions * @return the cache file containing the suggestions
@ -122,6 +123,15 @@ abstract class BaseSuggestionsModel {
return null; 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() { private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override @Override
public Response intercept(@NonNull Chain chain) throws IOException { public Response intercept(@NonNull Chain chain) throws IOException {

6
app/src/main/java/acr/browser/lightning/search/DuckSuggestionsModel.java → 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.app.Application;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -13,12 +13,12 @@ import acr.browser.lightning.R;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.FileUtils; 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 static final String ENCODING = "UTF-8";
@NonNull private final String mSearchSubtitle; @NonNull private final String mSearchSubtitle;
DuckSuggestionsModel(@NonNull Application application) { public DuckSuggestionsModel(@NonNull Application application) {
super(application, ENCODING); super(application, ENCODING);
mSearchSubtitle = application.getString(R.string.suggestion); mSearchSubtitle = application.getString(R.string.suggestion);
} }

6
app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsModel.java → 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.app.Application;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -15,13 +15,13 @@ import java.util.List;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
class GoogleSuggestionsModel extends BaseSuggestionsModel { public class GoogleSuggestionsModel extends BaseSuggestionsModel {
@NonNull private static final String ENCODING = "ISO-8859-1"; @NonNull private static final String ENCODING = "ISO-8859-1";
@Nullable private static XmlPullParser sXpp; @Nullable private static XmlPullParser sXpp;
@NonNull private final String mSearchSubtitle; @NonNull private final String mSearchSubtitle;
GoogleSuggestionsModel(@NonNull Application application) { public GoogleSuggestionsModel(@NonNull Application application) {
super(application, ENCODING); super(application, ENCODING);
mSearchSubtitle = application.getString(R.string.suggestion); mSearchSubtitle = application.getString(R.string.suggestion);
} }
Loading…
Cancel
Save