diff options
Diffstat (limited to 'main/src/cgeo/geocaching/SearchActivity.java')
| -rw-r--r-- | main/src/cgeo/geocaching/SearchActivity.java | 81 |
1 files changed, 61 insertions, 20 deletions
diff --git a/main/src/cgeo/geocaching/SearchActivity.java b/main/src/cgeo/geocaching/SearchActivity.java index 4e1777d..2a37e27 100644 --- a/main/src/cgeo/geocaching/SearchActivity.java +++ b/main/src/cgeo/geocaching/SearchActivity.java @@ -10,12 +10,18 @@ import cgeo.geocaching.connector.capability.ISearchByGeocode; import cgeo.geocaching.connector.trackable.TrackableConnector; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; +import cgeo.geocaching.search.AutoCompleteAdapter; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.EditUtils; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +import rx.functions.Func1; import android.app.Activity; import android.app.SearchManager; @@ -26,10 +32,8 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; -import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; -import android.widget.EditText; import java.util.Locale; @@ -39,19 +43,19 @@ public class SearchActivity extends AbstractActivity { @InjectView(R.id.buttonLongitude) protected Button buttonLongitude; @InjectView(R.id.search_coordinates) protected Button buttonSearchCoords; - @InjectView(R.id.address) protected EditText addressEditText; + @InjectView(R.id.address) protected AutoCompleteTextView addressEditText; @InjectView(R.id.search_address) protected Button buttonSearchAddress; @InjectView(R.id.geocode) protected AutoCompleteTextView geocodeEditText; @InjectView(R.id.display_geocode) protected Button buttonSearchGeocode; - @InjectView(R.id.keyword) protected EditText keywordEditText; + @InjectView(R.id.keyword) protected AutoCompleteTextView keywordEditText; @InjectView(R.id.search_keyword) protected Button buttonSearchKeyword; - @InjectView(R.id.finder) protected EditText finderNameEditText; + @InjectView(R.id.finder) protected AutoCompleteTextView finderNameEditText; @InjectView(R.id.search_finder) protected Button buttonSearchFinder; - @InjectView(R.id.owner) protected EditText ownerNameEditText; + @InjectView(R.id.owner) protected AutoCompleteTextView ownerNameEditText; @InjectView(R.id.search_owner) protected Button buttonSearchOwner; @InjectView(R.id.trackable) protected AutoCompleteTextView trackableEditText; @@ -60,9 +64,23 @@ public class SearchActivity extends AbstractActivity { @Override public final void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + final Intent intent = getIntent(); + + // search suggestion for a cache + if (Intents.ACTION_GEOCACHE.equals(intent.getAction())) { + CacheDetailActivity.startActivity(this, intent.getStringExtra(SearchManager.QUERY)); + finish(); + return; + } + + // search suggestion for a trackable + if (Intents.ACTION_TRACKABLE.equals(intent.getAction())) { + TrackableActivity.startActivity(this, null, intent.getStringExtra(SearchManager.QUERY), null); + finish(); + return; + } // search query - final Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { hideKeyboard(); final String query = intent.getStringExtra(SearchManager.QUERY); @@ -173,7 +191,7 @@ public class SearchActivity extends AbstractActivity { public void run() { findByAddressFn(); } - }); + }, null); setSearchAction(geocodeEditText, buttonSearchGeocode, new Runnable() { @@ -181,8 +199,13 @@ public class SearchActivity extends AbstractActivity { public void run() { findByGeocodeFn(); } + }, new Func1<String, String[]>() { + + @Override + public String[] call(final String input) { + return DataStore.getSuggestionsGeocode(input); + } }); - addHistoryEntries(geocodeEditText, DataStore.getRecentGeocodesForSearch()); setSearchAction(keywordEditText, buttonSearchKeyword, new Runnable() { @@ -190,6 +213,12 @@ public class SearchActivity extends AbstractActivity { public void run() { findByKeywordFn(); } + }, new Func1<String, String[]>() { + + @Override + public String[] call(final String input) { + return DataStore.getSuggestionsKeyword(input); + } }); setSearchAction(finderNameEditText, buttonSearchFinder, new Runnable() { @@ -198,6 +227,12 @@ public class SearchActivity extends AbstractActivity { public void run() { findByFinderFn(); } + }, new Func1<String, String[]>() { + + @Override + public String[] call(final String input) { + return DataStore.getSuggestionsFinderName(input); + } }); setSearchAction(ownerNameEditText, buttonSearchOwner, new Runnable() { @@ -206,6 +241,12 @@ public class SearchActivity extends AbstractActivity { public void run() { findByOwnerFn(); } + }, new Func1<String, String[]>() { + + @Override + public String[] call(final String input) { + return DataStore.getSuggestionsOwnerName(input); + } }); setSearchAction(trackableEditText, buttonSearchTrackable, new Runnable() { @@ -214,12 +255,16 @@ public class SearchActivity extends AbstractActivity { public void run() { findTrackableFn(); } + }, new Func1<String, String[]>() { + + @Override + public String[] call(final String input) { + return DataStore.getSuggestionsTrackableCode(input); + } }); - addHistoryEntries(trackableEditText, DataStore.getTrackableCodes()); - disableSuggestions(trackableEditText); } - private static void setSearchAction(final EditText editText, final Button button, final Runnable runnable) { + private static void setSearchAction(final AutoCompleteTextView editText, final Button button, final @NonNull Runnable runnable, final @Nullable Func1<String, String[]> suggestionFunction) { EditUtils.setActionListener(editText, runnable); button.setOnClickListener(new View.OnClickListener() { @Override @@ -227,12 +272,8 @@ public class SearchActivity extends AbstractActivity { runnable.run(); } }); - } - - private void addHistoryEntries(final AutoCompleteTextView textView, final String[] entries) { - if (entries != null) { - final ArrayAdapter<String> historyAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, entries); - textView.setAdapter(historyAdapter); + if (suggestionFunction != null) { + editText.setAdapter(new AutoCompleteAdapter(editText.getContext(), android.R.layout.simple_dropdown_item_1line, suggestionFunction)); } } @@ -305,7 +346,7 @@ public class SearchActivity extends AbstractActivity { return; } - CacheListActivity.startActivityUserName(this, usernameText); + CacheListActivity.startActivityFinder(this, usernameText); } private void findByOwnerFn() { @@ -331,7 +372,7 @@ public class SearchActivity extends AbstractActivity { return; } - CacheDetailActivity.startActivity(this, geocodeText.toUpperCase()); + CacheDetailActivity.startActivity(this, geocodeText.toUpperCase(Locale.US)); } private void findTrackableFn() { |
