Browse Source

refactoring search suggestions adapter

master
Anthony Restaino 8 years ago
parent
commit
284f2c7ff9
  1. 6
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 4
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  3. 3
      app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java
  4. 4
      app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java
  5. 4
      app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java
  6. 34
      app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java
  7. 6
      app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java

6
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -113,7 +113,7 @@ import acr.browser.lightning.fragment.BookmarksFragment;
import acr.browser.lightning.fragment.TabsFragment; import acr.browser.lightning.fragment.TabsFragment;
import acr.browser.lightning.interpolator.BezierDecelerateInterpolator; import acr.browser.lightning.interpolator.BezierDecelerateInterpolator;
import acr.browser.lightning.receiver.NetworkReceiver; import acr.browser.lightning.receiver.NetworkReceiver;
import acr.browser.lightning.search.Suggestions; import acr.browser.lightning.search.SuggestionsAdapter;
import acr.browser.lightning.utils.DrawableUtils; import acr.browser.lightning.utils.DrawableUtils;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
@ -161,7 +161,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private View mCustomView; private View mCustomView;
// Adapter // Adapter
private Suggestions mSuggestionsAdapter; private SuggestionsAdapter mSuggestionsAdapter;
// Callback // Callback
private CustomViewCallback mCustomViewCallback; private CustomViewCallback mCustomViewCallback;
@ -1503,7 +1503,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) { private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) {
mSuggestionsAdapter = new Suggestions(this, mDarkTheme, isIncognito()); mSuggestionsAdapter = new SuggestionsAdapter(this, mDarkTheme, isIncognito());
getUrl.setThreshold(1); getUrl.setThreshold(1);
getUrl.setDropDownWidth(-1); getUrl.setDropDownWidth(-1);

4
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -17,7 +17,7 @@ import acr.browser.lightning.fragment.DebugSettingsFragment;
import acr.browser.lightning.fragment.LightningPreferenceFragment; import acr.browser.lightning.fragment.LightningPreferenceFragment;
import acr.browser.lightning.fragment.PrivacySettingsFragment; import acr.browser.lightning.fragment.PrivacySettingsFragment;
import acr.browser.lightning.fragment.TabsFragment; import acr.browser.lightning.fragment.TabsFragment;
import acr.browser.lightning.search.Suggestions; import acr.browser.lightning.search.SuggestionsAdapter;
import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.AdBlock;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningView;
@ -68,6 +68,6 @@ public interface AppComponent {
void inject(DebugSettingsFragment fragment); void inject(DebugSettingsFragment fragment);
void inject(Suggestions suggestions); void inject(SuggestionsAdapter suggestionsAdapter);
} }

3
app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java

@ -32,6 +32,7 @@ abstract class BaseSuggestionsTask {
private static final String TAG = BaseSuggestionsTask.class.getSimpleName(); private static final String TAG = BaseSuggestionsTask.class.getSimpleName();
static final int MAX_RESULTS = 5;
private static final long INTERVAL_DAY = TimeUnit.DAYS.toMillis(1); private static final long INTERVAL_DAY = TimeUnit.DAYS.toMillis(1);
private static final String DEFAULT_LANGUAGE = "en"; private static final String DEFAULT_LANGUAGE = "en";
@Nullable private static String sLanguage; @Nullable private static String sLanguage;
@ -104,7 +105,7 @@ abstract class BaseSuggestionsTask {
@NonNull @NonNull
private File downloadSuggestionsForQuery(@NonNull String query, String language, @NonNull Application app) { private File downloadSuggestionsForQuery(@NonNull String query, String language, @NonNull Application app) {
String queryUrl = getQueryUrl(query, language); String queryUrl = getQueryUrl(query, language);
File cacheFile = new File(app.getCacheDir(), queryUrl.hashCode() + Suggestions.CACHE_FILE_TYPE); File cacheFile = new File(app.getCacheDir(), queryUrl.hashCode() + SuggestionsAdapter.CACHE_FILE_TYPE);
if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) { if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) {
return cacheFile; return cacheFile;
} }

4
app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java

@ -13,7 +13,7 @@ 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;
public final class DuckSuggestionsTask extends BaseSuggestionsTask { final class DuckSuggestionsTask extends BaseSuggestionsTask {
private static final String ENCODING = "UTF-8"; private static final String ENCODING = "UTF-8";
@NonNull private final String mSearchSubtitle; @NonNull private final String mSearchSubtitle;
@ -41,7 +41,7 @@ public final class DuckSuggestionsTask extends BaseSuggestionsTask {
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 >= 5) { if (counter >= MAX_RESULTS) {
break; break;
} }
} }

4
app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java

@ -15,7 +15,7 @@ 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;
public class GoogleSuggestionsTask extends BaseSuggestionsTask { class GoogleSuggestionsTask extends BaseSuggestionsTask {
private static final String ENCODING = "ISO-8859-1"; private static final String ENCODING = "ISO-8859-1";
@Nullable private static XmlPullParser sXpp; @Nullable private static XmlPullParser sXpp;
@ -47,7 +47,7 @@ public class GoogleSuggestionsTask extends BaseSuggestionsTask {
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 >= 5) { if (counter >= MAX_RESULTS) {
break; break;
} }
} }

34
app/src/main/java/acr/browser/lightning/search/Suggestions.java → app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java

@ -43,11 +43,11 @@ import com.anthonycr.bonsai.Subscriber;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
public class Suggestions extends BaseAdapter implements Filterable { public class SuggestionsAdapter extends BaseAdapter implements Filterable {
private static final Scheduler FILTER_SCHEDULER = Schedulers.newSingleThreadedScheduler(); private static final Scheduler FILTER_SCHEDULER = Schedulers.newSingleThreadedScheduler();
public static final String CACHE_FILE_TYPE = ".sgg"; static final String CACHE_FILE_TYPE = ".sgg";
private final List<HistoryItem> mFilteredList = new ArrayList<>(5); private final List<HistoryItem> mFilteredList = new ArrayList<>(5);
@ -74,7 +74,7 @@ public class Suggestions extends BaseAdapter implements Filterable {
@NonNull private final Context mContext; @NonNull private final Context mContext;
private PreferenceManager.Suggestion mSuggestionChoice; private PreferenceManager.Suggestion mSuggestionChoice;
public Suggestions(@NonNull Context context, boolean dark, boolean incognito) { public SuggestionsAdapter(@NonNull Context context, boolean dark, boolean incognito) {
super(); super();
BrowserApp.getAppComponent().inject(this); BrowserApp.getAppComponent().inject(this);
mContext = context; mContext = context;
@ -123,7 +123,7 @@ public class Suggestions extends BaseAdapter implements Filterable {
private static class SuggestionHolder { private static class SuggestionHolder {
public SuggestionHolder(@NonNull View view) { SuggestionHolder(@NonNull View view) {
mTitle = (TextView) view.findViewById(R.id.title); mTitle = (TextView) view.findViewById(R.id.title);
mUrl = (TextView) view.findViewById(R.id.url); mUrl = (TextView) view.findViewById(R.id.url);
mImage = (ImageView) view.findViewById(R.id.suggestionIcon); mImage = (ImageView) view.findViewById(R.id.suggestionIcon);
@ -319,50 +319,50 @@ public class Suggestions extends BaseAdapter implements Filterable {
private static class SearchFilter extends Filter { private static class SearchFilter extends Filter {
@NonNull private final Suggestions mSuggestions; @NonNull private final SuggestionsAdapter mSuggestionsAdapter;
public SearchFilter(@NonNull Suggestions suggestions) { SearchFilter(@NonNull SuggestionsAdapter suggestionsAdapter) {
mSuggestions = suggestions; mSuggestionsAdapter = suggestionsAdapter;
} }
@Override @Override
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) { if (constraint == null || constraint.length() == 0) {
mSuggestions.clearSuggestions(); mSuggestionsAdapter.clearSuggestions();
return results; return results;
} }
String query = constraint.toString().toLowerCase(Locale.getDefault()).trim(); String query = constraint.toString().toLowerCase(Locale.getDefault()).trim();
if (mSuggestions.shouldRequestNetwork() && !SuggestionsManager.isRequestInProgress()) { if (mSuggestionsAdapter.shouldRequestNetwork() && !SuggestionsManager.isRequestInProgress()) {
mSuggestions.getSuggestionsForQuery(query) mSuggestionsAdapter.getSuggestionsForQuery(query)
.subscribeOn(Schedulers.worker()) .subscribeOn(Schedulers.worker())
.observeOn(Schedulers.main()) .observeOn(Schedulers.main())
.subscribe(new OnSubscribe<List<HistoryItem>>() { .subscribe(new OnSubscribe<List<HistoryItem>>() {
@Override @Override
public void onNext(@Nullable List<HistoryItem> item) { public void onNext(@Nullable List<HistoryItem> item) {
mSuggestions.combineResults(null, null, item); mSuggestionsAdapter.combineResults(null, null, item);
} }
}); });
} }
mSuggestions.getBookmarksForQuery(query) mSuggestionsAdapter.getBookmarksForQuery(query)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(Schedulers.main()) .observeOn(Schedulers.main())
.subscribe(new OnSubscribe<List<HistoryItem>>() { .subscribe(new OnSubscribe<List<HistoryItem>>() {
@Override @Override
public void onNext(@Nullable List<HistoryItem> item) { public void onNext(@Nullable List<HistoryItem> item) {
mSuggestions.combineResults(item, null, null); mSuggestionsAdapter.combineResults(item, null, null);
} }
}); });
mSuggestions.getHistoryForQuery(query) mSuggestionsAdapter.getHistoryForQuery(query)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(Schedulers.main()) .observeOn(Schedulers.main())
.subscribe(new OnSubscribe<List<HistoryItem>>() { .subscribe(new OnSubscribe<List<HistoryItem>>() {
@Override @Override
public void onNext(@Nullable List<HistoryItem> item) { public void onNext(@Nullable List<HistoryItem> item) {
mSuggestions.combineResults(null, item, null); mSuggestionsAdapter.combineResults(null, item, null);
} }
}); });
results.count = 1; results.count = 1;
@ -376,7 +376,7 @@ public class Suggestions extends BaseAdapter implements Filterable {
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
mSuggestions.combineResults(null, null, null); mSuggestionsAdapter.combineResults(null, null, null);
} }
} }
@ -385,7 +385,7 @@ public class Suggestions extends BaseAdapter implements Filterable {
@NonNull @NonNull
private final Application app; private final Application app;
public ClearCacheRunnable(@NonNull Application app) { ClearCacheRunnable(@NonNull Application app) {
this.app = app; this.app = app;
} }

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

@ -12,7 +12,7 @@ import java.util.List;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
public class SuggestionsManager { class SuggestionsManager {
public enum Source { public enum Source {
GOOGLE, GOOGLE,
@ -21,11 +21,11 @@ public class SuggestionsManager {
private static volatile boolean sIsTaskExecuting; private static volatile boolean sIsTaskExecuting;
public static boolean isRequestInProgress() { static boolean isRequestInProgress() {
return sIsTaskExecuting; return sIsTaskExecuting;
} }
public static Observable<List<HistoryItem>> getObservable(@NonNull final String query, @NonNull final Context context, @NonNull final Source source) { static Observable<List<HistoryItem>> getObservable(@NonNull final String query, @NonNull final Context context, @NonNull final Source source) {
return Observable.create(new Action<List<HistoryItem>>() { return Observable.create(new Action<List<HistoryItem>>() {
@Override @Override
public void onSubscribe(@NonNull final Subscriber<List<HistoryItem>> subscriber) { public void onSubscribe(@NonNull final Subscriber<List<HistoryItem>> subscriber) {

Loading…
Cancel
Save