Simplified and improved filter algorithm

This commit is contained in:
Anthony Restaino 2015-01-29 20:31:33 -05:00
parent 8314676918
commit 7e23135824

View File

@ -12,14 +12,11 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.*; import android.widget.*;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -174,8 +171,6 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
new RetrieveSearchSuggestions().execute(query); new RetrieveSearchSuggestions().execute(query);
} }
List<HistoryItem> filter = new ArrayList<HistoryItem>();
int counter = 0; int counter = 0;
mBookmarks = new ArrayList<HistoryItem>(); mBookmarks = new ArrayList<HistoryItem>();
for (int n = 0; n < mAllBookmarks.size(); n++) { for (int n = 0; n < mAllBookmarks.size(); n++) {
@ -184,11 +179,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
if (mAllBookmarks.get(n).getTitle().toLowerCase(Locale.getDefault()) if (mAllBookmarks.get(n).getTitle().toLowerCase(Locale.getDefault())
.startsWith(query)) { .startsWith(query)) {
filter.add(mAllBookmarks.get(n));
mBookmarks.add(mAllBookmarks.get(n)); mBookmarks.add(mAllBookmarks.get(n));
counter++; counter++;
} else if (mAllBookmarks.get(n).getUrl().contains(query)) { } else if (mAllBookmarks.get(n).getUrl().contains(query)) {
filter.add(mAllBookmarks.get(n));
mBookmarks.add(mAllBookmarks.get(n)); mBookmarks.add(mAllBookmarks.get(n));
counter++; counter++;
} }
@ -199,22 +192,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
mHistory = mDatabaseHandler.findItemsContaining(constraint.toString()); mHistory = mDatabaseHandler.findItemsContaining(constraint.toString());
for (int n = 0; n < mHistory.size(); n++) { mFilteredList = getSuggestions();
if (n >= 5) { results.count = 1;
break;
}
filter.add(mHistory.get(n));
}
for (int n = 0; n < mSuggestions.size(); n++) {
if (filter.size() >= 5) {
break;
}
filter.add(mSuggestions.get(n));
}
results.count = filter.size();
results.values = filter;
return results; return results;
} }
@ -225,10 +204,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
synchronized (mFilteredList) { notifyDataSetChanged();
mFilteredList = getSuggestions();
notifyDataSetChanged();
}
} }
} }
@ -272,29 +248,25 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
int eventType = mXpp.getEventType(); int eventType = mXpp.getEventType();
int counter = 0; int counter = 0;
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG && "suggestion".equals(mXpp.getName())) {
if ("suggestion".equals(mXpp.getName())) { String suggestion = mXpp.getAttributeValue(null, "data");
String suggestion = mXpp.getAttributeValue(null, "data"); filter.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"',
filter.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"', suggestion, R.drawable.ic_search));
suggestion, R.drawable.ic_search)); counter++;
counter++; if (counter >= 5) {
if (counter >= 5) { break;
break;
}
} }
} }
eventType = mXpp.next(); eventType = mXpp.next();
} }
} catch (Exception e){
} catch (FileNotFoundException e) { return filter;
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (XmlPullParserException e) {
} finally { } finally {
if (download != null) { if (download != null) {
try { try {
download.close(); download.close();
} catch (IOException e) { } catch (IOException e) {
return filter;
} }
} }
} }
@ -331,66 +303,34 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
public List<HistoryItem> getSuggestions() { public List<HistoryItem> getSuggestions() {
List<HistoryItem> filteredList = new ArrayList<HistoryItem>(); List<HistoryItem> filteredList = new ArrayList<HistoryItem>();
int suggestionsSize = 0; int suggestionsSize = (mSuggestions == null) ? 0 : mSuggestions.size();
int historySize = 0; int historySize = (mHistory == null) ? 0 : mHistory.size();
int bookmarkSize = 0; int bookmarkSize = (mBookmarks == null) ? 0 : mBookmarks.size();
if (mSuggestions != null) { int maxSuggestions = (bookmarkSize + historySize < 3) ? (5 - bookmarkSize - historySize)
suggestionsSize = mSuggestions.size(); : (bookmarkSize < 2) ? (4 - bookmarkSize) : (historySize < 1) ? 3 : 2;
} int maxHistory = (suggestionsSize + bookmarkSize < 4) ? (5 - suggestionsSize - bookmarkSize)
if (mHistory != null) { : 1;
historySize = mHistory.size(); int maxBookmarks = (suggestionsSize + historySize < 3) ? (5 - suggestionsSize - historySize)
} : 2;
if (mBookmarks != null) {
bookmarkSize = mBookmarks.size();
}
int maxSuggestions = 2;
int maxHistory = 1;
int maxBookmarks = 2;
if (!mUseGoogle || mIncognito) { if (!mUseGoogle || mIncognito) {
maxHistory++; maxHistory++;
maxBookmarks++; maxBookmarks++;
} }
if (bookmarkSize + historySize < 3) { for (int n = 0; n < bookmarkSize && n < maxBookmarks; n++) {
maxSuggestions = 5 - (bookmarkSize + historySize);
} else if (bookmarkSize < 2) {
maxSuggestions += 2 - bookmarkSize;
} else if (historySize < 1) {
maxSuggestions += 1;
}
if (suggestionsSize + bookmarkSize < 4) {
maxHistory = 5 - (suggestionsSize + bookmarkSize);
}
if (suggestionsSize + historySize < 3) {
maxBookmarks = 5 - (suggestionsSize + historySize);
}
for (int n = 0; n < bookmarkSize; n++) {
if (n >= maxBookmarks) {
break;
}
filteredList.add(mBookmarks.get(n)); filteredList.add(mBookmarks.get(n));
} }
for (int n = 0; n < historySize; n++) { for (int n = 0; n < historySize && n < maxHistory; n++) {
if (n >= maxHistory) {
break;
}
filteredList.add(mHistory.get(n)); filteredList.add(mHistory.get(n));
} }
for (int n = 0; n < suggestionsSize; n++) { for (int n = 0; n < suggestionsSize && n < maxSuggestions; n++) {
if (n >= maxSuggestions) {
break;
}
filteredList.add(mSuggestions.get(n)); filteredList.add(mSuggestions.get(n));
} }
// Log.i("MAX", "Max: "+maxSuggestions+" "+maxBookmarks+" "+maxHistory);
// Log.i("SIZE",
// "size: "+suggestionsSize+" "+bookmarkSize+" "+historySize);
return filteredList; return filteredList;
} }
} }