diff options
Diffstat (limited to 'main/src/cgeo/geocaching/SearchActivity.java')
| -rw-r--r-- | main/src/cgeo/geocaching/SearchActivity.java | 200 |
1 files changed, 66 insertions, 134 deletions
diff --git a/main/src/cgeo/geocaching/SearchActivity.java b/main/src/cgeo/geocaching/SearchActivity.java index 57b391f..fe48fda 100644 --- a/main/src/cgeo/geocaching/SearchActivity.java +++ b/main/src/cgeo/geocaching/SearchActivity.java @@ -13,8 +13,6 @@ import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.utils.EditUtils; -import cgeo.geocaching.utils.GeoDirHandler; -import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; @@ -38,26 +36,28 @@ public class SearchActivity extends AbstractActivity { @InjectView(R.id.buttonLatitude) protected Button buttonLatitude; @InjectView(R.id.buttonLongitude) protected Button buttonLongitude; - @InjectView(R.id.search_coordinates) protected Button findByCoords; - @InjectView(R.id.search_address) protected Button findByAddress; - @InjectView(R.id.geocode) protected AutoCompleteTextView geocodeEdit; - @InjectView(R.id.display_geocode) protected Button displayByGeocode; - @InjectView(R.id.search_keyword) protected Button findByKeyword; - @InjectView(R.id.search_username) protected Button findByUserName; - @InjectView(R.id.search_owner) protected Button findByOwner; - @InjectView(R.id.trackable) protected AutoCompleteTextView trackable; - @InjectView(R.id.display_trackable) protected Button displayTrackable; - @InjectView(R.id.latitude) protected EditText latEdit; - @InjectView(R.id.longitude) protected EditText lonEdit; - @InjectView(R.id.keyword) protected EditText keywordEditText; + @InjectView(R.id.search_coordinates) protected Button buttonSearchCoords; + @InjectView(R.id.address) protected EditText 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.search_keyword) protected Button buttonSearchKeyword; + @InjectView(R.id.username) protected EditText userNameEditText; + @InjectView(R.id.search_username) protected Button buttonSearchUserName; + @InjectView(R.id.owner) protected EditText ownerNameEditText; - @InjectView(R.id.geocode) protected EditText geocodeEditText; - @InjectView(R.id.trackable) protected EditText trackableEditText; + @InjectView(R.id.search_owner) protected Button buttonSearchOwner; + + @InjectView(R.id.trackable) protected AutoCompleteTextView trackableEditText; + @InjectView(R.id.display_trackable) protected Button buttonSearchTrackable; @Override - public void onCreate(Bundle savedInstanceState) { + public final void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); // search query @@ -89,25 +89,18 @@ public class SearchActivity extends AbstractActivity { } @Override - public void onConfigurationChanged(Configuration newConfig) { + public final void onConfigurationChanged(final Configuration newConfig) { super.onConfigurationChanged(newConfig); init(); } @Override - public void onResume() { + public final void onResume() { super.onResume(); - geoDirHandler.startGeo(); init(); } - @Override - public void onPause() { - geoDirHandler.stopGeo(); - super.onPause(); - } - /** * Performs a search for query either as geocode, trackable code or keyword * @@ -142,7 +135,7 @@ public class SearchActivity extends AbstractActivity { } if (keywordSearch) { // keyword fallback, if desired by caller - cgeocaches.startActivityKeyword(this, query.trim()); + CacheListActivity.startActivityKeyword(this, query.trim()); return true; } @@ -150,73 +143,77 @@ public class SearchActivity extends AbstractActivity { } private void init() { - buttonLatitude.setOnClickListener(new FindByCoordsAction()); buttonLongitude.setOnClickListener(new FindByCoordsAction()); - findByCoords.setOnClickListener(new FindByCoordsListener()); - EditUtils.setActionListener((EditText) findViewById(R.id.address), new Runnable() { + buttonSearchCoords.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(final View arg0) { + findByCoordsFn(); + } + }); + + setSearchAction(addressEditText, buttonSearchAddress, new Runnable() { @Override public void run() { findByAddressFn(); } }); - findByAddress.setOnClickListener(new FindByAddressListener()); - EditUtils.setActionListener(geocodeEdit, new Runnable() { + setSearchAction(geocodeEditText, buttonSearchGeocode, new Runnable() { @Override public void run() { findByGeocodeFn(); } }); - addHistoryEntries(geocodeEdit, cgData.getRecentGeocodesForSearch()); - displayByGeocode.setOnClickListener(new FindByGeocodeListener()); + addHistoryEntries(geocodeEditText, DataStore.getRecentGeocodesForSearch()); - EditUtils.setActionListener((EditText) findViewById(R.id.keyword), new Runnable() { + setSearchAction(keywordEditText, buttonSearchKeyword, new Runnable() { @Override public void run() { findByKeywordFn(); } }); - findByKeyword.setOnClickListener(new FindByKeywordListener()); - EditUtils.setActionListener((EditText) findViewById(R.id.username), new Runnable() { + setSearchAction(userNameEditText, buttonSearchUserName, new Runnable() { @Override public void run() { findByUsernameFn(); } }); - findByUserName.setOnClickListener(new FindByUsernameListener()); - EditUtils.setActionListener((EditText) findViewById(R.id.owner), new Runnable() { + setSearchAction(ownerNameEditText, buttonSearchOwner, new Runnable() { @Override public void run() { findByOwnerFn(); } }); - findByOwner.setOnClickListener(new OnClickListener() { + + setSearchAction(trackableEditText, buttonSearchTrackable, new Runnable() { @Override - public void onClick(View arg0) { - findByOwnerFn(); + public void run() { + findTrackableFn(); } }); + addHistoryEntries(trackableEditText, DataStore.getTrackableCodes()); + disableSuggestions(trackableEditText); + } - EditUtils.setActionListener(trackable, new Runnable() { - + private static void setSearchAction(final EditText editText, final Button button, final Runnable runnable) { + EditUtils.setActionListener(editText, runnable); + button.setOnClickListener(new View.OnClickListener() { @Override - public void run() { - findTrackableFn(); + public void onClick(final View arg0) { + runnable.run(); } }); - addHistoryEntries(trackable, cgData.getTrackableCodes()); - disableSuggestions(trackable); - displayTrackable.setOnClickListener(new FindTrackableListener()); } private void addHistoryEntries(final AutoCompleteTextView textView, final String[] entries) { @@ -226,33 +223,15 @@ public class SearchActivity extends AbstractActivity { } } - private final GeoDirHandler geoDirHandler = new GeoDirHandler() { - @Override - public void updateGeoData(final IGeoData geo) { - try { - if (geo.getCoords() != null) { - if (latEdit != null) { - latEdit.setHint(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE_RAW)); - } - if (lonEdit != null) { - lonEdit.setHint(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE_RAW)); - } - } - } catch (final Exception e) { - Log.w("Failed to update location."); - } - } - }; - private class FindByCoordsAction implements OnClickListener { @Override - public void onClick(View arg0) { + public void onClick(final View arg0) { final CoordinatesInputDialog coordsDialog = new CoordinatesInputDialog(SearchActivity.this, null, null, app.currentGeo()); coordsDialog.setCancelable(true); coordsDialog.setOnCoordinateUpdate(new CoordinatesInputDialog.CoordinateUpdate() { @Override - public void update(Geopoint gp) { + public void update(final Geopoint gp) { buttonLatitude.setText(gp.format(GeopointFormatter.Format.LAT_DECMINUTE)); buttonLongitude.setText(gp.format(GeopointFormatter.Format.LON_DECMINUTE)); } @@ -261,17 +240,9 @@ public class SearchActivity extends AbstractActivity { } } - private class FindByCoordsListener implements View.OnClickListener { - - @Override - public void onClick(View arg0) { - findByCoordsFn(); - } - } - private void findByCoordsFn() { - final String latText = buttonLatitude.getText().toString(); - final String lonText = buttonLongitude.getText().toString(); + final String latText = StringUtils.trim(buttonLatitude.getText().toString()); + final String lonText = StringUtils.trim(buttonLongitude.getText().toString()); if (StringUtils.isEmpty(latText) || StringUtils.isEmpty(lonText)) { final IGeoData geo = app.currentGeo(); @@ -281,42 +252,27 @@ public class SearchActivity extends AbstractActivity { } } else { try { - cgeocaches.startActivityCoordinates(this, new Geopoint(StringUtils.trim(latText), StringUtils.trim(lonText))); + CacheListActivity.startActivityCoordinates(this, new Geopoint(latText, lonText)); } catch (final Geopoint.ParseException e) { showToast(res.getString(e.resource)); } } } - private class FindByKeywordListener implements View.OnClickListener { - - @Override - public void onClick(View arg0) { - findByKeywordFn(); - } - } - private void findByKeywordFn() { // find caches by coordinates - final String keyText = keywordEditText.getText().toString(); + final String keyText = StringUtils.trim(keywordEditText.getText().toString()); if (StringUtils.isBlank(keyText)) { helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_keyword)); return; } - cgeocaches.startActivityKeyword(this, StringUtils.trim(keyText)); - } - - private class FindByAddressListener implements View.OnClickListener { - @Override - public void onClick(View arg0) { - findByAddressFn(); - } + CacheListActivity.startActivityKeyword(this, keyText); } private void findByAddressFn() { - final String addText = addressEditText.getText().toString(); + final String addText = StringUtils.trim(addressEditText.getText().toString()); if (StringUtils.isBlank(addText)) { helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_address)); @@ -324,34 +280,26 @@ public class SearchActivity extends AbstractActivity { } final Intent addressesIntent = new Intent(this, AddressListActivity.class); - addressesIntent.putExtra(Intents.EXTRA_KEYWORD, StringUtils.trim(addText)); + addressesIntent.putExtra(Intents.EXTRA_KEYWORD, addText); startActivity(addressesIntent); } - private class FindByUsernameListener implements View.OnClickListener { - - @Override - public void onClick(View arg0) { - findByUsernameFn(); - } - } - - public void findByUsernameFn() { - final String usernameText = userNameEditText.getText().toString(); + public final void findByUsernameFn() { + final String usernameText = StringUtils.trim(userNameEditText.getText().toString()); if (StringUtils.isBlank(usernameText)) { helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_user)); return; } - cgeocaches.startActivityUserName(this, StringUtils.trim(usernameText)); + CacheListActivity.startActivityUserName(this, usernameText); } private void findByOwnerFn() { findByOwnerFn(ownerNameEditText.getText().toString()); } - private void findByOwnerFn(String userName) { + private void findByOwnerFn(final String userName) { final String usernameText = StringUtils.trimToEmpty(userName); if (StringUtils.isBlank(usernameText)) { @@ -359,38 +307,22 @@ public class SearchActivity extends AbstractActivity { return; } - cgeocaches.startActivityOwner(this, StringUtils.trim(usernameText)); - } - - private class FindByGeocodeListener implements View.OnClickListener { - - @Override - public void onClick(View arg0) { - findByGeocodeFn(); - } + CacheListActivity.startActivityOwner(this, usernameText); } private void findByGeocodeFn() { - final String geocodeText = geocodeEditText.getText().toString(); + final String geocodeText = StringUtils.trim(geocodeEditText.getText().toString()); if (StringUtils.isBlank(geocodeText) || geocodeText.equalsIgnoreCase("GC")) { helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_gccode)); return; } - CacheDetailActivity.startActivity(this, StringUtils.trim(geocodeText)); - } - - private class FindTrackableListener implements View.OnClickListener { - - @Override - public void onClick(View arg0) { - findTrackableFn(); - } + CacheDetailActivity.startActivity(this, geocodeText); } private void findTrackableFn() { - final String trackableText = trackableEditText.getText().toString(); + final String trackableText = StringUtils.trim(trackableEditText.getText().toString()); if (StringUtils.isBlank(trackableText) || trackableText.equalsIgnoreCase("TB")) { helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_tb)); @@ -398,18 +330,18 @@ public class SearchActivity extends AbstractActivity { } final Intent trackablesIntent = new Intent(this, TrackableActivity.class); - trackablesIntent.putExtra(Intents.EXTRA_GEOCODE, StringUtils.trim(trackableText).toUpperCase(Locale.US)); + trackablesIntent.putExtra(Intents.EXTRA_GEOCODE, trackableText.toUpperCase(Locale.US)); startActivity(trackablesIntent); } @Override - public boolean onCreateOptionsMenu(Menu menu) { + public final boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.search_activity_options, menu); return true; } @Override - public boolean onOptionsItemSelected(MenuItem item) { + public final boolean onOptionsItemSelected(final MenuItem item) { if (item.getItemId() == R.id.menu_search_own_caches) { findByOwnerFn(Settings.getUsername()); return true; |
