Trim search string before querying for apps.

Fixes issue #452. It turns out that recent versions of android do
this automatically, but my gingerbread emulator didn't.

In the process, I also refactored the getQuery method. It was previously
a void method that modified the state of the search view, which is a
bit counter-intuitive given it's name. Instead, I made it just a getter,
which calculates the query and returns it. That way, I was able to remove
mQuery from the fields in the search view. Generally, the less state we
need to worry about (e.g. fields in an object), the less assumptions we
need to make about whether that field has been set or not.
This commit is contained in:
Peter Serwylo 2014-01-30 20:22:35 +11:00
parent 772004756e
commit f098b9c6d8

View File

@ -48,23 +48,22 @@ public class SearchResults extends ListActivity {
private AppListAdapter applist; private AppListAdapter applist;
private String mQuery; protected String getQuery() {
Intent intent = getIntent();
protected void getQuery(Intent intent) { String query;
if (Intent.ACTION_SEARCH.equals(intent.getAction())) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
mQuery = intent.getStringExtra(SearchManager.QUERY); query = intent.getStringExtra(SearchManager.QUERY);
} else { } else {
Uri data = intent.getData(); Uri data = intent.getData();
if (data.isHierarchical()) { if (data != null && data.isHierarchical()) {
mQuery = data.getQueryParameter("q"); query = data.getQueryParameter("q");
if (mQuery.startsWith("pname:")) if (query != null && query.startsWith("pname:"))
mQuery = mQuery.substring(6); query = query.substring(6);
} else { } else {
mQuery = data.getEncodedSchemeSpecificPart(); query = data.getEncodedSchemeSpecificPart();
} }
} }
if (mQuery == null || mQuery.length() == 0) return query;
finish();
} }
@Override @Override
@ -79,25 +78,34 @@ public class SearchResults extends ListActivity {
// Start a search by just typing // Start a search by just typing
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
}
getQuery(getIntent()); @Override
protected void onResume() {
super.onResume();
updateView(); updateView();
} }
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
getQuery(intent);
super.onNewIntent(intent); super.onNewIntent(intent);
updateView(); setIntent(intent);
} }
private void updateView() { private void updateView() {
String query = getQuery();
if (query != null)
query = query.trim();
if (query == null || query.length() == 0)
finish();
List<String> matchingids = new ArrayList<String>(); List<String> matchingids = new ArrayList<String>();
try { try {
DB db = DB.getDB(); DB db = DB.getDB();
matchingids = db.doSearch(mQuery); matchingids = db.doSearch(query.trim());
} catch (Exception ex) { } catch (Exception ex) {
Log.d("FDroid", "Search failed - " + ex.getMessage()); Log.d("FDroid", "Search failed - " + ex.getMessage());
} finally { } finally {
@ -107,7 +115,6 @@ public class SearchResults extends ListActivity {
List<DB.App> apps = new ArrayList<DB.App>(); List<DB.App> apps = new ArrayList<DB.App>();
List<DB.App> allApps = ((FDroidApp) getApplication()).getApps(); List<DB.App> allApps = ((FDroidApp) getApplication()).getApps();
for (DB.App app : allApps) { for (DB.App app : allApps) {
boolean include = false;
for (String id : matchingids) { for (String id : matchingids) {
if (id.equals(app.id)) { if (id.equals(app.id)) {
apps.add(app); apps.add(app);
@ -119,14 +126,14 @@ public class SearchResults extends ListActivity {
TextView tv = (TextView) findViewById(R.id.description); TextView tv = (TextView) findViewById(R.id.description);
String headertext; String headertext;
if (apps.size() == 0) { if (apps.size() == 0) {
headertext = getString(R.string.searchres_noapps, mQuery); headertext = getString(R.string.searchres_noapps, query);
} else if (apps.size() == 1) { } else if (apps.size() == 1) {
headertext = getString(R.string.searchres_oneapp, mQuery); headertext = getString(R.string.searchres_oneapp, query);
} else { } else {
headertext = getString(R.string.searchres_napps, apps.size(), mQuery); headertext = getString(R.string.searchres_napps, apps.size(), query);
} }
tv.setText(headertext); tv.setText(headertext);
Log.d("FDroid", "Search for '" + mQuery + "' returned " + apps.size() Log.d("FDroid", "Search for '" + query + "' returned " + apps.size()
+ " results"); + " results");
applist.clear(); applist.clear();
for (DB.App app : apps) { for (DB.App app : apps) {