aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/SearchActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/SearchActivity.java')
-rw-r--r--main/src/cgeo/geocaching/SearchActivity.java116
1 files changed, 54 insertions, 62 deletions
diff --git a/main/src/cgeo/geocaching/SearchActivity.java b/main/src/cgeo/geocaching/SearchActivity.java
index 6fdff5a..b1a58a4 100644
--- a/main/src/cgeo/geocaching/SearchActivity.java
+++ b/main/src/cgeo/geocaching/SearchActivity.java
@@ -1,14 +1,17 @@
package cgeo.geocaching;
+import butterknife.InjectView;
+import butterknife.Views;
+
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.connector.capability.ISearchByGeocode;
-import cgeo.geocaching.connector.gc.GCConstants;
+import cgeo.geocaching.connector.trackable.TrackableConnector;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
+import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.dialog.CoordinatesInputDialog;
-import cgeo.geocaching.utils.BaseUtils;
import cgeo.geocaching.utils.EditUtils;
import cgeo.geocaching.utils.GeoDirHandler;
import cgeo.geocaching.utils.Log;
@@ -33,20 +36,32 @@ import java.util.Locale;
public class SearchActivity extends AbstractActivity {
- private static final int MENU_SEARCH_OWN_CACHES = 1;
- private EditText latEdit = null;
- private EditText lonEdit = null;
-
- public SearchActivity() {
- super("c:geo-search");
- }
+ @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.address) protected EditText addressEditText;
+ @InjectView(R.id.username) protected EditText userNameEditText;
+ @InjectView(R.id.owner) protected EditText ownerNameEditText;
+ @InjectView(R.id.geocode) protected EditText geocodeEditText;
+ @InjectView(R.id.trackable) protected EditText trackableEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// search query
- Intent intent = getIntent();
+ final Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
final String query = intent.getStringExtra(SearchManager.QUERY);
final boolean keywordSearch = intent.getBooleanExtra(Intents.EXTRA_KEYWORD_SEARCH, true);
@@ -63,9 +78,12 @@ public class SearchActivity extends AbstractActivity {
}
setTheme();
- setContentView(R.layout.search);
+ setContentView(R.layout.search_activity);
+
+ // set title in code, as the activity needs a hard coded title due to the intent filters
setTitle(res.getString(R.string.search));
+ Views.inject(this);
init();
}
@@ -104,7 +122,7 @@ public class SearchActivity extends AbstractActivity {
// otherwise see if this is a pure geocode
if (StringUtils.isEmpty(geocode)) {
- geocode = StringUtils.trim(query);
+ geocode = StringUtils.upperCase(StringUtils.trim(query));
}
final IConnector connector = ConnectorFactory.getConnector(geocode);
@@ -116,10 +134,10 @@ public class SearchActivity extends AbstractActivity {
}
// Check if the query is a TB code
- final String trackable = BaseUtils.getMatch(query, GCConstants.PATTERN_TB_CODE, true, 0, "", false);
- if (StringUtils.isNotBlank(trackable)) {
+ final TrackableConnector trackableConnector = ConnectorFactory.getTrackableConnector(geocode);
+ if (trackableConnector != ConnectorFactory.UNKNOWN_TRACKABLE_CONNECTOR) {
final Intent trackablesIntent = new Intent(this, TrackableActivity.class);
- trackablesIntent.putExtra(Intents.EXTRA_GEOCODE, trackable.toUpperCase(Locale.US));
+ trackablesIntent.putExtra(Intents.EXTRA_GEOCODE, geocode.toUpperCase(Locale.US));
startActivity(trackablesIntent);
return true;
}
@@ -133,12 +151,10 @@ public class SearchActivity extends AbstractActivity {
}
private void init() {
- Settings.getLogin();
+ Settings.getGcLogin();
- findViewById(R.id.buttonLatitude).setOnClickListener(new FindByCoordsAction());
- findViewById(R.id.buttonLongitude).setOnClickListener(new FindByCoordsAction());
-
- final Button findByCoords = (Button) findViewById(R.id.search_coordinates);
+ buttonLatitude.setOnClickListener(new FindByCoordsAction());
+ buttonLongitude.setOnClickListener(new FindByCoordsAction());
findByCoords.setOnClickListener(new FindByCoordsListener());
EditUtils.setActionListener((EditText) findViewById(R.id.address), new Runnable() {
@@ -148,11 +164,8 @@ public class SearchActivity extends AbstractActivity {
findByAddressFn();
}
});
-
- final Button findByAddress = (Button) findViewById(R.id.search_address);
findByAddress.setOnClickListener(new FindByAddressListener());
- final AutoCompleteTextView geocodeEdit = (AutoCompleteTextView) findViewById(R.id.geocode);
EditUtils.setActionListener(geocodeEdit, new Runnable() {
@Override
@@ -161,8 +174,6 @@ public class SearchActivity extends AbstractActivity {
}
});
addHistoryEntries(geocodeEdit, cgData.getRecentGeocodesForSearch());
-
- final Button displayByGeocode = (Button) findViewById(R.id.display_geocode);
displayByGeocode.setOnClickListener(new FindByGeocodeListener());
EditUtils.setActionListener((EditText) findViewById(R.id.keyword), new Runnable() {
@@ -172,8 +183,6 @@ public class SearchActivity extends AbstractActivity {
findByKeywordFn();
}
});
-
- final Button findByKeyword = (Button) findViewById(R.id.search_keyword);
findByKeyword.setOnClickListener(new FindByKeywordListener());
EditUtils.setActionListener((EditText) findViewById(R.id.username), new Runnable() {
@@ -183,8 +192,6 @@ public class SearchActivity extends AbstractActivity {
findByUsernameFn();
}
});
-
- final Button findByUserName = (Button) findViewById(R.id.search_username);
findByUserName.setOnClickListener(new FindByUsernameListener());
EditUtils.setActionListener((EditText) findViewById(R.id.owner), new Runnable() {
@@ -194,8 +201,6 @@ public class SearchActivity extends AbstractActivity {
findByOwnerFn();
}
});
-
- final Button findByOwner = (Button) findViewById(R.id.search_owner);
findByOwner.setOnClickListener(new OnClickListener() {
@Override
@@ -204,7 +209,6 @@ public class SearchActivity extends AbstractActivity {
}
});
- AutoCompleteTextView trackable = (AutoCompleteTextView) findViewById(R.id.trackable);
EditUtils.setActionListener(trackable, new Runnable() {
@Override
@@ -213,10 +217,7 @@ public class SearchActivity extends AbstractActivity {
}
});
addHistoryEntries(trackable, cgData.getTrackableCodes());
-
disableSuggestions(trackable);
-
- final Button displayTrackable = (Button) findViewById(R.id.display_trackable);
displayTrackable.setOnClickListener(new FindTrackableListener());
}
@@ -231,13 +232,6 @@ public class SearchActivity extends AbstractActivity {
@Override
public void updateGeoData(final IGeoData geo) {
try {
- if (latEdit == null) {
- latEdit = (EditText) findViewById(R.id.latitude);
- }
- if (lonEdit == null) {
- lonEdit = (EditText) findViewById(R.id.longitude);
- }
-
if (geo.getCoords() != null) {
if (latEdit != null) {
latEdit.setHint(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE_RAW));
@@ -246,7 +240,7 @@ public class SearchActivity extends AbstractActivity {
lonEdit.setHint(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE_RAW));
}
}
- } catch (Exception e) {
+ } catch (final Exception e) {
Log.w("Failed to update location.");
}
}
@@ -256,13 +250,13 @@ public class SearchActivity extends AbstractActivity {
@Override
public void onClick(View arg0) {
- CoordinatesInputDialog coordsDialog = new CoordinatesInputDialog(SearchActivity.this, null, null, app.currentGeo());
+ 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) {
- ((Button) findViewById(R.id.buttonLatitude)).setText(gp.format(GeopointFormatter.Format.LAT_DECMINUTE));
- ((Button) findViewById(R.id.buttonLongitude)).setText(gp.format(GeopointFormatter.Format.LON_DECMINUTE));
+ buttonLatitude.setText(gp.format(GeopointFormatter.Format.LAT_DECMINUTE));
+ buttonLongitude.setText(gp.format(GeopointFormatter.Format.LON_DECMINUTE));
}
});
coordsDialog.show();
@@ -278,21 +272,19 @@ public class SearchActivity extends AbstractActivity {
}
private void findByCoordsFn() {
- final Button latView = (Button) findViewById(R.id.buttonLatitude);
- final Button lonView = (Button) findViewById(R.id.buttonLongitude);
- final String latText = latView.getText().toString();
- final String lonText = lonView.getText().toString();
+ final String latText = buttonLatitude.getText().toString();
+ final String lonText = buttonLongitude.getText().toString();
if (StringUtils.isEmpty(latText) || StringUtils.isEmpty(lonText)) {
final IGeoData geo = app.currentGeo();
if (geo.getCoords() != null) {
- latView.setText(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE));
- lonView.setText(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE));
+ buttonLatitude.setText(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE));
+ buttonLongitude.setText(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE));
}
} else {
try {
cgeocaches.startActivityCoordinates(this, new Geopoint(StringUtils.trim(latText), StringUtils.trim(lonText)));
- } catch (Geopoint.ParseException e) {
+ } catch (final Geopoint.ParseException e) {
showToast(res.getString(e.resource));
}
}
@@ -308,7 +300,7 @@ public class SearchActivity extends AbstractActivity {
private void findByKeywordFn() {
// find caches by coordinates
- String keyText = ((EditText) findViewById(R.id.keyword)).getText().toString();
+ final String keyText = keywordEditText.getText().toString();
if (StringUtils.isBlank(keyText)) {
helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_keyword));
@@ -326,7 +318,7 @@ public class SearchActivity extends AbstractActivity {
}
private void findByAddressFn() {
- final String addText = ((EditText) findViewById(R.id.address)).getText().toString();
+ final String addText = addressEditText.getText().toString();
if (StringUtils.isBlank(addText)) {
helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_address));
@@ -347,7 +339,7 @@ public class SearchActivity extends AbstractActivity {
}
public void findByUsernameFn() {
- final String usernameText = ((EditText) findViewById(R.id.username)).getText().toString();
+ final String usernameText = userNameEditText.getText().toString();
if (StringUtils.isBlank(usernameText)) {
helpDialog(res.getString(R.string.warn_search_help_title), res.getString(R.string.warn_search_help_user));
@@ -358,7 +350,7 @@ public class SearchActivity extends AbstractActivity {
}
private void findByOwnerFn() {
- findByOwnerFn(((EditText) findViewById(R.id.owner)).getText().toString());
+ findByOwnerFn(ownerNameEditText.getText().toString());
}
private void findByOwnerFn(String userName) {
@@ -381,7 +373,7 @@ public class SearchActivity extends AbstractActivity {
}
private void findByGeocodeFn() {
- final String geocodeText = ((EditText) findViewById(R.id.geocode)).getText().toString();
+ final String geocodeText = 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));
@@ -400,7 +392,7 @@ public class SearchActivity extends AbstractActivity {
}
private void findTrackableFn() {
- final String trackableText = ((EditText) findViewById(R.id.trackable)).getText().toString();
+ final String trackableText = 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));
@@ -414,13 +406,13 @@ public class SearchActivity extends AbstractActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, MENU_SEARCH_OWN_CACHES, 0, res.getString(R.string.search_own_caches)).setIcon(R.drawable.ic_menu_myplaces);
+ getMenuInflater().inflate(R.menu.search_activity_options, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == MENU_SEARCH_OWN_CACHES) {
+ if (item.getItemId() == R.id.menu_search_own_caches) {
findByOwnerFn(Settings.getUsername());
return true;
}
@@ -432,6 +424,6 @@ public class SearchActivity extends AbstractActivity {
searchIntent.setAction(Intent.ACTION_SEARCH).
putExtra(SearchManager.QUERY, scan).
putExtra(Intents.EXTRA_KEYWORD_SEARCH, false);
- fromActivity.startActivityForResult(searchIntent, cgeo.SEARCH_REQUEST_CODE);
+ fromActivity.startActivityForResult(searchIntent, MainActivity.SEARCH_REQUEST_CODE);
}
}