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

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

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