Browse Source

More clear and understandable suggestions filtering algorithm

master
Anthony Restaino 9 years ago
parent
commit
7331345348
  1. 19
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 91
      app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 A.C.R. Development * Copyright 2015 Anthony Restaino
*/ */
package acr.browser.lightning.activity; package acr.browser.lightning.activity;
@ -1209,7 +1209,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
// don't delete the tab because the browser will close and mess stuff up // don't delete the tab because the browser will close and mess stuff up
} }
@SuppressWarnings("deprecation")
@Override @Override
public void onTrimMemory(int level) { public void onTrimMemory(int level) {
if (level > TRIM_MEMORY_MODERATE && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { if (level > TRIM_MEMORY_MODERATE && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
@ -2108,12 +2107,12 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
mTitleAdapter.notifyDataSetChanged(); mTitleAdapter.notifyDataSetChanged();
} }
@Override
/** /**
* opens a file chooser * opens a file chooser
* param ValueCallback is the message from the WebView indicating a file chooser * param ValueCallback is the message from the WebView indicating a file chooser
* should be opened * should be opened
*/ */
@Override
public void openFileChooser(ValueCallback<Uri> uploadMsg) { public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg; mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT); Intent i = new Intent(Intent.ACTION_GET_CONTENT);
@ -2122,10 +2121,10 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
startActivityForResult(Intent.createChooser(i, getString(R.string.title_file_chooser)), 1); startActivityForResult(Intent.createChooser(i, getString(R.string.title_file_chooser)), 1);
} }
@Override
/** /**
* used to allow uploading into the browser * used to allow uploading into the browser
*/ */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (API < Build.VERSION_CODES.LOLLIPOP) { if (API < Build.VERSION_CODES.LOLLIPOP) {
if (requestCode == 1) { if (requestCode == 1) {
@ -2212,12 +2211,12 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
mActivity.startActivityForResult(chooserIntent, 1); mActivity.startActivityForResult(chooserIntent, 1);
} }
@Override
/** /**
* handles long presses for the browser, tries to get the * handles long presses for the browser, tries to get the
* url of the item that was clicked and sends it (it can be null) * url of the item that was clicked and sends it (it can be null)
* to the click handler that does cool stuff with it * to the click handler that does cool stuff with it
*/ */
@Override
public void onLongPress() { public void onLongPress() {
if (mClickHandler == null) { if (mClickHandler == null) {
mClickHandler = new ClickHandler(mActivity); mClickHandler = new ClickHandler(mActivity);
@ -2350,11 +2349,11 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
} }
@Override
/** /**
* a stupid method that returns the bitmap image to display in place of * a stupid method that returns the bitmap image to display in place of
* a loading video * a loading video
*/ */
@Override
public Bitmap getDefaultVideoPoster() { public Bitmap getDefaultVideoPoster() {
if (mDefaultVideoPoster == null) { if (mDefaultVideoPoster == null) {
mDefaultVideoPoster = BitmapFactory.decodeResource(getResources(), mDefaultVideoPoster = BitmapFactory.decodeResource(getResources(),
@ -2363,10 +2362,10 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
return mDefaultVideoPoster; return mDefaultVideoPoster;
} }
@Override
/** /**
* dumb method that returns the loading progress for a video * dumb method that returns the loading progress for a video
*/ */
@Override
public View getVideoLoadingProgressView() { public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) { if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(this); LayoutInflater inflater = LayoutInflater.from(this);
@ -2375,10 +2374,10 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
return mVideoProgressView; return mVideoProgressView;
} }
@Override
/** /**
* handles javascript requests to create a new window in the browser * handles javascript requests to create a new window in the browser
*/ */
@Override
public void onCreateWindow(Message resultMsg) { public void onCreateWindow(Message resultMsg) {
if (resultMsg == null) { if (resultMsg == null) {
return; return;
@ -2390,11 +2389,11 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
} }
} }
@Override
/** /**
* returns the Activity instance for this activity, * returns the Activity instance for this activity,
* very helpful when creating things in other classes... I think * very helpful when creating things in other classes... I think
*/ */
@Override
public Activity getActivity() { public Activity getActivity() {
return mActivity; return mActivity;
} }
@ -2484,12 +2483,12 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
} }
} }
@Override
/** /**
* handles a long click on the page, parameter String url * handles a long click on the page, parameter String url
* is the url that should have been obtained from the WebView touch node * is the url that should have been obtained from the WebView touch node
* thingy, if it is null, this method tries to deal with it and find a workaround * thingy, if it is null, this method tries to deal with it and find a workaround
*/ */
@Override
public void longClickPage(final String url) { public void longClickPage(final String url) {
HitTestResult result = null; HitTestResult result = null;
String currentUrl = null; String currentUrl = null;

91
app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

@ -3,12 +3,10 @@ package acr.browser.lightning.object;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -32,6 +30,9 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -59,6 +60,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private final BookmarkManager mBookmarkManager; private final BookmarkManager mBookmarkManager;
private static final String ENCODING = "ISO-8859-1"; private static final String ENCODING = "ISO-8859-1";
private static final long INTERVAL_DAY = 86400000; private static final long INTERVAL_DAY = 86400000;
private static final int MAX_SUGGESTIONS = 5;
private final SuggestionsComparator mComparator = new SuggestionsComparator();
private final String mSearchSubtitle; private final String mSearchSubtitle;
private SearchFilter mFilter; private SearchFilter mFilter;
private final Drawable mSearchDrawable; private final Drawable mSearchDrawable;
@ -247,7 +250,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredList.clear(); mFilteredList.clear();
mFilteredList.addAll(getSuggestions()); List<HistoryItem> filtered = getFilteredList();
Collections.sort(filtered, mComparator);
mFilteredList.addAll(filtered);
notifyDataSetChanged(); notifyDataSetChanged();
} }
@ -318,7 +323,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
mSuggestions.clear(); mSuggestions.clear();
mSuggestions.addAll(result); mSuggestions.addAll(result);
mFilteredList.clear(); mFilteredList.clear();
mFilteredList.addAll(getSuggestions()); List<HistoryItem> filtered = getFilteredList();
Collections.sort(filtered, mComparator);
mFilteredList.addAll(filtered);
notifyDataSetChanged(); notifyDataSetChanged();
mIsExecuting = false; mIsExecuting = false;
} }
@ -376,32 +383,64 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
//TODO Write simpler algorithm //TODO Write simpler algorithm
private List<HistoryItem> getSuggestions() { // private List<HistoryItem> getSuggestions() {
List<HistoryItem> filteredList = new ArrayList<>(); // List<HistoryItem> filteredList = new ArrayList<>();
//
int suggestionsSize = mSuggestions.size(); // int suggestionsSize = mSuggestions.size();
int historySize = mHistory.size(); // int historySize = mHistory.size();
int bookmarkSize = mBookmarks.size(); // int bookmarkSize = mBookmarks.size();
//
int maxSuggestions = (bookmarkSize + historySize < 3) ? (5 - bookmarkSize - historySize) // int maxSuggestions = (bookmarkSize + historySize < 3) ? (5 - bookmarkSize - historySize) : (bookmarkSize < 2) ? (4 - bookmarkSize) : (historySize < 1) ? 3 : 2;
: (bookmarkSize < 2) ? (4 - bookmarkSize) : (historySize < 1) ? 3 : 2; // int maxHistory = (suggestionsSize + bookmarkSize < 4) ? (5 - suggestionsSize - bookmarkSize) : 1;
int maxHistory = (suggestionsSize + bookmarkSize < 4) ? (5 - suggestionsSize - bookmarkSize) // int maxBookmarks = (suggestionsSize + historySize < 3) ? (5 - suggestionsSize - historySize) : 2;
: 1; //
int maxBookmarks = (suggestionsSize + historySize < 3) ? (5 - suggestionsSize - historySize) // for (int n = 0; n < bookmarkSize && n < maxBookmarks; n++) {
: 2; // filteredList.add(mBookmarks.get(n));
// }
for (int n = 0; n < bookmarkSize && n < maxBookmarks; n++) { //
filteredList.add(mBookmarks.get(n)); // for (int n = 0; n < historySize && n < maxHistory; n++) {
} // filteredList.add(mHistory.get(n));
// }
//
// for (int n = 0; n < suggestionsSize && n < maxSuggestions; n++) {
// filteredList.add(mSuggestions.get(n));
// }
// return filteredList;
// }
private List<HistoryItem> getFilteredList() {
List<HistoryItem> list = new ArrayList<>();
Iterator<HistoryItem> bookmark = mBookmarks.iterator();
Iterator<HistoryItem> history = mHistory.iterator();
Iterator<HistoryItem> suggestion = mSuggestions.listIterator();
while (list.size() < MAX_SUGGESTIONS) {
if (!bookmark.hasNext() && !suggestion.hasNext() && !history.hasNext()) {
return list;
}
if (bookmark.hasNext()) {
list.add(bookmark.next());
}
if (suggestion.hasNext() && list.size() < MAX_SUGGESTIONS) {
list.add(suggestion.next());
}
if (history.hasNext() && list.size() < MAX_SUGGESTIONS) {
list.add(history.next());
}
for (int n = 0; n < historySize && n < maxHistory; n++) {
filteredList.add(mHistory.get(n));
} }
return list;
}
for (int n = 0; n < suggestionsSize && n < maxSuggestions; n++) { private class SuggestionsComparator implements Comparator<HistoryItem> {
filteredList.add(mSuggestions.get(n));
@Override
public int compare(HistoryItem lhs, HistoryItem rhs) {
if (lhs.getImageId() == rhs.getImageId()) return 0;
if (lhs.getImageId() == R.drawable.ic_bookmark) return -1;
if (rhs.getImageId() == R.drawable.ic_bookmark) return 1;
if (lhs.getImageId() == R.drawable.ic_history) return -1;
return 1;
} }
return filteredList;
} }
} }

Loading…
Cancel
Save