Cache some objects to speed up the query process.
This class needs a lot of attention to speed up the filtering process and make it easier to understand.
This commit is contained in:
parent
73b367053c
commit
388e614dd7
@ -38,6 +38,11 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
|
|||||||
private Context mContext;
|
private Context mContext;
|
||||||
private boolean mIncognito;
|
private boolean mIncognito;
|
||||||
private BookmarkManager mBookmarkManager;
|
private BookmarkManager mBookmarkManager;
|
||||||
|
private static final String ENCODING = "ISO-8859-1";
|
||||||
|
private static final String URL_ENCODING = "UTF-8";
|
||||||
|
private XmlPullParserFactory mFactory;
|
||||||
|
private XmlPullParser mXpp;
|
||||||
|
private String mSearchSubtitle;
|
||||||
|
|
||||||
public SearchAdapter(Context context, boolean incognito) {
|
public SearchAdapter(Context context, boolean incognito) {
|
||||||
mDatabaseHandler = new HistoryDatabaseHandler(context);
|
mDatabaseHandler = new HistoryDatabaseHandler(context);
|
||||||
@ -50,6 +55,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
|
|||||||
mPreferences = context.getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
|
mPreferences = context.getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
|
||||||
mUseGoogle = mPreferences.getBoolean(PreferenceConstants.GOOGLE_SEARCH_SUGGESTIONS, true);
|
mUseGoogle = mPreferences.getBoolean(PreferenceConstants.GOOGLE_SEARCH_SUGGESTIONS, true);
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
mSearchSubtitle = mContext.getString(R.string.suggestion);
|
||||||
mIncognito = incognito;
|
mIncognito = incognito;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,6 +199,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
|
|||||||
mDatabaseHandler = new HistoryDatabaseHandler(mContext);
|
mDatabaseHandler = new HistoryDatabaseHandler(mContext);
|
||||||
}
|
}
|
||||||
mHistory = mDatabaseHandler.findItemsContaining(constraint.toString());
|
mHistory = mDatabaseHandler.findItemsContaining(constraint.toString());
|
||||||
|
|
||||||
for (int n = 0; n < mHistory.size(); n++) {
|
for (int n = 0; n < mHistory.size(); n++) {
|
||||||
if (n >= 5) {
|
if (n >= 5) {
|
||||||
break;
|
break;
|
||||||
@ -247,46 +254,52 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
|
|||||||
String query = arg0[0];
|
String query = arg0[0];
|
||||||
try {
|
try {
|
||||||
query = query.replace(" ", "+");
|
query = query.replace(" ", "+");
|
||||||
URLEncoder.encode(query, "UTF-8");
|
URLEncoder.encode(query, ENCODING);
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
InputStream download = null;
|
InputStream download = null;
|
||||||
try {
|
try {
|
||||||
try {
|
download = new java.net.URL("http://google.com/complete/search?q=" + query
|
||||||
download = new java.net.URL("http://google.com/complete/search?q=" + query
|
+ "&output=toolbar&hl=en").openStream();
|
||||||
+ "&output=toolbar&hl=en").openStream();
|
if (mFactory == null) {
|
||||||
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
|
mFactory = XmlPullParserFactory.newInstance();
|
||||||
factory.setNamespaceAware(true);
|
mFactory.setNamespaceAware(true);
|
||||||
XmlPullParser xpp = factory.newPullParser();
|
}
|
||||||
xpp.setInput(download, "iso-8859-1");
|
if (mXpp == null) {
|
||||||
int eventType = xpp.getEventType();
|
mXpp = mFactory.newPullParser();
|
||||||
int counter = 0;
|
}
|
||||||
while (eventType != XmlPullParser.END_DOCUMENT) {
|
mXpp.setInput(download, URL_ENCODING);
|
||||||
if (eventType == XmlPullParser.START_TAG) {
|
int eventType = mXpp.getEventType();
|
||||||
if ("suggestion".equals(xpp.getName())) {
|
int counter = 0;
|
||||||
String suggestion = xpp.getAttributeValue(null, "data");
|
while (eventType != XmlPullParser.END_DOCUMENT) {
|
||||||
filter.add(new HistoryItem(mContext.getString(R.string.suggestion)
|
if (eventType == XmlPullParser.START_TAG) {
|
||||||
+ " \"" + suggestion + '"', suggestion,
|
if ("suggestion".equals(mXpp.getName())) {
|
||||||
R.drawable.ic_search));
|
String suggestion = mXpp.getAttributeValue(null, "data");
|
||||||
counter++;
|
filter.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"',
|
||||||
if (counter >= 5) {
|
suggestion, R.drawable.ic_search));
|
||||||
break;
|
counter++;
|
||||||
}
|
if (counter >= 5) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eventType = xpp.next();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (download != null) {
|
|
||||||
download.close();
|
|
||||||
}
|
}
|
||||||
|
eventType = mXpp.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
} catch (XmlPullParserException e) {
|
} catch (XmlPullParserException e) {
|
||||||
|
} finally {
|
||||||
|
if (download != null) {
|
||||||
|
try {
|
||||||
|
download.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -376,8 +389,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
|
|||||||
}
|
}
|
||||||
filteredList.add(mSuggestions.get(n));
|
filteredList.add(mSuggestions.get(n));
|
||||||
}
|
}
|
||||||
//Log.i("MAX", "Max: "+maxSuggestions+" "+maxBookmarks+" "+maxHistory);
|
// Log.i("MAX", "Max: "+maxSuggestions+" "+maxBookmarks+" "+maxHistory);
|
||||||
//Log.i("SIZE", "size: "+suggestionsSize+" "+bookmarkSize+" "+historySize);
|
// Log.i("SIZE",
|
||||||
|
// "size: "+suggestionsSize+" "+bookmarkSize+" "+historySize);
|
||||||
return filteredList;
|
return filteredList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user