aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgeocaches.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/cgeocaches.java')
-rw-r--r--main/src/cgeo/geocaching/cgeocaches.java247
1 files changed, 71 insertions, 176 deletions
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java
index 3be48ca..340122a 100644
--- a/main/src/cgeo/geocaching/cgeocaches.java
+++ b/main/src/cgeo/geocaching/cgeocaches.java
@@ -12,7 +12,6 @@ import cgeo.geocaching.connector.gc.SearchHandler;
import cgeo.geocaching.enumerations.CacheListType;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
-import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.export.ExportFactory;
import cgeo.geocaching.files.GPXImporter;
@@ -24,27 +23,17 @@ import cgeo.geocaching.network.Cookies;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.sorting.CacheComparator;
-import cgeo.geocaching.sorting.DateComparator;
-import cgeo.geocaching.sorting.DifficultyComparator;
+import cgeo.geocaching.sorting.ComparatorUserInterface;
import cgeo.geocaching.sorting.EventDateComparator;
-import cgeo.geocaching.sorting.FindsComparator;
-import cgeo.geocaching.sorting.GeocodeComparator;
-import cgeo.geocaching.sorting.InventoryComparator;
-import cgeo.geocaching.sorting.NameComparator;
-import cgeo.geocaching.sorting.PopularityComparator;
-import cgeo.geocaching.sorting.RatingComparator;
-import cgeo.geocaching.sorting.SizeComparator;
-import cgeo.geocaching.sorting.StateComparator;
-import cgeo.geocaching.sorting.StorageTimeComparator;
-import cgeo.geocaching.sorting.TerrainComparator;
import cgeo.geocaching.sorting.VisitComparator;
-import cgeo.geocaching.sorting.VoteComparator;
import cgeo.geocaching.ui.CacheListAdapter;
+import cgeo.geocaching.ui.LoggingUI;
import cgeo.geocaching.utils.GeoDirHandler;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RunnableWithArgument;
import ch.boye.httpclientandroidlib.HttpResponse;
+
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -71,10 +60,8 @@ import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
public class cgeocaches extends AbstractListActivity {
@@ -93,17 +80,7 @@ public class cgeocaches extends AbstractListActivity {
private static final int MENU_CREATE_LIST = 7;
private static final int MENU_DROP_LIST = 8;
private static final int MENU_INVERT_SELECTION = 9;
- private static final int MENU_SORT_DISTANCE = 10;
- private static final int MENU_SORT_DIFFICULTY = 11;
- private static final int MENU_SORT_TERRAIN = 12;
- private static final int MENU_SORT_SIZE = 13;
- private static final int MENU_SORT_FAVORITES = 14;
- private static final int MENU_SORT_NAME = 15;
- private static final int MENU_SORT_GEOCODE = 16;
private static final int MENU_SWITCH_LIST = 17;
- private static final int MENU_SORT_RATING = 18;
- private static final int MENU_SORT_VOTE = 19;
- private static final int MENU_SORT_INVENTORY = 20;
private static final int MENU_IMPORT_WEB = 21;
private static final int MENU_EXPORT = 22;
private static final int MENU_REMOVE_FROM_HISTORY = 23;
@@ -113,18 +90,14 @@ public class cgeocaches extends AbstractListActivity {
private static final int SUBMENU_SHOW_MAP = 54;
private static final int SUBMENU_MANAGE_LISTS = 55;
private static final int SUBMENU_MANAGE_OFFLINE = 56;
- private static final int SUBMENU_SORT = 57;
+ private static final int MENU_SORT = 57;
private static final int SUBMENU_MANAGE_HISTORY = 60;
- private static final int MENU_SORT_DATE = 61;
- private static final int MENU_SORT_FINDS = 62;
- private static final int MENU_SORT_STATE = 63;
private static final int MENU_RENAME_LIST = 64;
private static final int MENU_DROP_CACHES_AND_LIST = 65;
private static final int MENU_DEFAULT_NAVIGATION = 66;
private static final int MENU_NAVIGATION = 69;
private static final int MENU_STORE_CACHE = 73;
private static final int MENU_FILTER = 74;
- private static final int MENU_SORT_STORAGE = 75;
private static final int MSG_DONE = -1;
private static final int MSG_RESTART_GEO_AND_DIR = -2;
@@ -133,7 +106,6 @@ public class cgeocaches extends AbstractListActivity {
private String action = null;
private CacheListType type = null;
private Geopoint coords = null;
- private CacheType cacheType = Settings.getCacheType();
private SearchResult search = null;
private final List<cgCache> cacheList = new ArrayList<cgCache>();
private CacheListAdapter adapter = null;
@@ -141,7 +113,6 @@ public class cgeocaches extends AbstractListActivity {
private View listFooter = null;
private TextView listFooterText = null;
private final Progress progress = new Progress();
- private Float northHeading = 0f;
private String title = "";
private int detailTotal = 0;
private int detailProgress = 0;
@@ -151,41 +122,31 @@ public class cgeocaches extends AbstractListActivity {
private int listId = StoredList.TEMPORARY_LIST_ID; // Only meaningful for the OFFLINE type
private final GeoDirHandler geoDirHandler = new GeoDirHandler() {
- @Override
+ @Override
public void updateGeoData(final IGeoData geo) {
if (adapter == null) {
return;
}
- try {
- if (geo.getCoords() != null) {
- adapter.setActualCoordinates(geo.getCoords());
- }
-
- if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h
- if (!Settings.isUseCompass()) {
- adapter.setActualHeading(geo.getBearing());
- }
- if (northHeading != null) {
- adapter.setActualHeading(northHeading);
- }
- }
- } catch (Exception e) {
- Log.w("Failed to UpdateLocation location.");
+ if (geo.getCoords() != null) {
+ adapter.setActualCoordinates(geo.getCoords());
+ }
+ if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h
+ adapter.setActualHeading(geo.getBearing());
}
}
- @Override
- public void updateDirection(final float direction) {
- if (!Settings.isLiveList()) {
- return;
- }
+ @Override
+ public void updateDirection(final float direction) {
+ if (adapter == null || !Settings.isLiveList()) {
+ return;
+ }
- northHeading = DirectionProvider.getDirectionNow(cgeocaches.this, direction);
- if (northHeading != null && adapter != null && (app.currentGeo().getSpeed() <= 5)) { // use compass when speed is lower than 18 km/h) {
- adapter.setActualHeading(northHeading);
- }
- }
+ if (app.currentGeo().getSpeed() <= 5) { // use compass when speed is lower than 18 km/h) {
+ final float northHeading = DirectionProvider.getDirectionNow(cgeocaches.this, direction);
+ adapter.setActualHeading(northHeading);
+ }
+ }
};
private ContextMenuInfo lastMenuInfo;
@@ -202,8 +163,6 @@ public class cgeocaches extends AbstractListActivity {
try {
if (search != null) {
setTitle(title + " [" + search.getCount() + ']');
- cacheList.clear();
- cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB));
} else {
setTitle(title);
}
@@ -248,11 +207,7 @@ public class cgeocaches extends AbstractListActivity {
return;
}
- final Geopoint coordsNow = app.currentGeo().getCoords();
- if (coordsNow != null) {
- adapter.setActualCoordinates(coordsNow);
- adapter.setActualHeading(northHeading);
- }
+ setAdapterCurrentCoordinates(false);
} catch (Exception e) {
showToast(res.getString(R.string.err_detail_cache_find_any));
Log.e("cgeocaches.loadCachesHandler: " + e.toString());
@@ -283,8 +238,7 @@ public class cgeocaches extends AbstractListActivity {
try {
if (search != null) {
setTitle(title + " [" + search.getCount() + "]");
- cacheList.clear();
- cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB));
+ replaceCacheListFromSearch();
if (adapter != null) {
adapter.reFilter();
}
@@ -307,11 +261,7 @@ public class cgeocaches extends AbstractListActivity {
return;
}
- final Geopoint coordsNow = app.currentGeo().getCoords();
- if (coordsNow != null) {
- adapter.setActualCoordinates(coordsNow);
- adapter.setActualHeading(northHeading);
- }
+ setAdapterCurrentCoordinates(false);
} catch (Exception e) {
showToast(res.getString(R.string.err_detail_cache_find_next));
Log.e("cgeocaches.loadNextPageHandler: " + e.toString());
@@ -325,6 +275,12 @@ public class cgeocaches extends AbstractListActivity {
}
}
};
+
+ private void replaceCacheListFromSearch() {
+ cacheList.clear();
+ cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB));
+ }
+
private Handler loadDetailsHandler = new Handler() {
@Override
@@ -364,11 +320,7 @@ public class cgeocaches extends AbstractListActivity {
}
}
- final Geopoint coordsNow = app.currentGeo().getCoords();
- if (coordsNow != null) {
- adapter.setActualCoordinates(coordsNow);
- adapter.setActualHeading(northHeading);
- }
+ setAdapterCurrentCoordinates(false);
showProgress(false);
progress.dismiss();
@@ -410,8 +362,7 @@ public class cgeocaches extends AbstractListActivity {
adapter.setSelectMode(false);
}
- cacheList.clear();
- cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB));
+ replaceCacheListFromSearch();
progress.dismiss();
}
@@ -428,8 +379,7 @@ public class cgeocaches extends AbstractListActivity {
refreshCurrentList();
- cacheList.clear();
- cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB));
+ replaceCacheListFromSearch();
progress.dismiss();
}
@@ -487,7 +437,6 @@ public class cgeocaches extends AbstractListActivity {
else {
extras = new Bundle();
}
- cacheType = Settings.getCacheType();
if (isInvokedFromAttachment()) {
type = CacheListType.OFFLINE;
if (coords == null) {
@@ -613,8 +562,8 @@ public class cgeocaches extends AbstractListActivity {
title = res.getString(R.string.map_map);
setTitle(title);
showProgress(true);
- SearchResult result = (SearchResult) extras.get(EXTRAS_SEARCH);
- search = new SearchResult(result);
+ search = (SearchResult) extras.get(EXTRAS_SEARCH);
+ replaceCacheListFromSearch();
loadCachesHandler.sendMessage(Message.obtain());
break;
default:
@@ -668,27 +617,33 @@ public class cgeocaches extends AbstractListActivity {
if (adapter != null) {
adapter.setSelectMode(false);
- final Geopoint coordsNow = app.currentGeo().getCoords();
- if (coordsNow != null) {
- adapter.setActualCoordinates(coordsNow);
- adapter.setActualHeading(northHeading);
- adapter.forceSort();
- }
+ setAdapterCurrentCoordinates(true);
}
if (loadCachesHandler != null && search != null) {
+ replaceCacheListFromSearch();
loadCachesHandler.sendEmptyMessage(0);
}
// refresh standard list if it has changed (new caches downloaded)
if (type == CacheListType.OFFLINE && listId >= StoredList.STANDARD_LIST_ID && search != null) {
- SearchResult newSearch = cgeoapplication.getInstance().getBatchOfStoredCaches(true, coords, cacheType, listId);
+ SearchResult newSearch = cgeoapplication.getInstance().getBatchOfStoredCaches(true, coords, Settings.getCacheType(), listId);
if (newSearch != null && newSearch.getTotal() != search.getTotal()) {
refreshCurrentList();
}
}
}
+ private void setAdapterCurrentCoordinates(final boolean forceSort) {
+ final Geopoint coordsNow = app.currentGeo().getCoords();
+ if (coordsNow != null) {
+ adapter.setActualCoordinates(coordsNow);
+ if (forceSort) {
+ adapter.forceSort();
+ }
+ }
+ }
+
@Override
public void onDestroy() {
if (adapter != null) {
@@ -710,34 +665,7 @@ public class cgeocaches extends AbstractListActivity {
menu.add(0, MENU_FILTER, 0, res.getString(R.string.caches_filter)).setIcon(R.drawable.ic_menu_filter);
if (type != CacheListType.HISTORY) {
- final SubMenu subMenuSort = menu.addSubMenu(0, SUBMENU_SORT, 0, res.getString(R.string.caches_sort)).setIcon(R.drawable.ic_menu_sort_alphabetically);
- subMenuSort.setHeaderTitle(res.getString(R.string.caches_sort_title));
-
- // sort the context menu labels alphabetically for easier reading
- final Map<String, Integer> comparators = new HashMap<String, Integer>();
- comparators.put(res.getString(R.string.caches_sort_distance), MENU_SORT_DISTANCE);
- comparators.put(res.getString(R.string.caches_sort_difficulty), MENU_SORT_DIFFICULTY);
- comparators.put(res.getString(R.string.caches_sort_terrain), MENU_SORT_TERRAIN);
- comparators.put(res.getString(R.string.caches_sort_size), MENU_SORT_SIZE);
- comparators.put(res.getString(R.string.caches_sort_favorites), MENU_SORT_FAVORITES);
- comparators.put(res.getString(R.string.caches_sort_name), MENU_SORT_NAME);
- comparators.put(res.getString(R.string.caches_sort_gccode), MENU_SORT_GEOCODE);
- comparators.put(res.getString(R.string.caches_sort_rating), MENU_SORT_RATING);
- comparators.put(res.getString(R.string.caches_sort_vote), MENU_SORT_VOTE);
- comparators.put(res.getString(R.string.caches_sort_inventory), MENU_SORT_INVENTORY);
- comparators.put(res.getString(R.string.caches_sort_date), MENU_SORT_DATE);
- comparators.put(res.getString(R.string.caches_sort_finds), MENU_SORT_FINDS);
- comparators.put(res.getString(R.string.caches_sort_state), MENU_SORT_STATE);
- comparators.put(res.getString(R.string.caches_sort_storage), MENU_SORT_STORAGE);
-
- final List<String> sortedLabels = new ArrayList<String>(comparators.keySet());
- Collections.sort(sortedLabels);
- for (String label : sortedLabels) {
- Integer id = comparators.get(label);
- subMenuSort.add(1, id, 0, label).setCheckable(true).setChecked(id == MENU_SORT_DISTANCE);
- }
-
- subMenuSort.setGroupCheckable(1, true, true);
+ menu.add(0, MENU_SORT, 0, res.getString(R.string.caches_sort)).setIcon(R.drawable.ic_menu_sort_alphabetically);
}
menu.add(0, MENU_SWITCH_SELECT_MODE, 0, res.getString(R.string.caches_select_mode)).setIcon(R.drawable.ic_menu_agenda);
@@ -806,7 +734,7 @@ public class cgeocaches extends AbstractListActivity {
setVisible(menu, MENU_SWITCH_SELECT_MODE, !isEmpty);
setVisible(menu, SUBMENU_MANAGE_HISTORY, !isEmpty);
setVisible(menu, SUBMENU_SHOW_MAP, !isEmpty);
- setVisible(menu, SUBMENU_SORT, !isEmpty);
+ setVisible(menu, MENU_SORT, !isEmpty);
setVisible(menu, MENU_REFRESH_STORED, !isEmpty && (isConcrete || type != CacheListType.OFFLINE));
setVisible(menu, MENU_DROP_CACHES, !isEmpty);
setVisible(menu, MENU_DROP_CACHES_AND_LIST, isConcrete && !isEmpty);
@@ -916,52 +844,10 @@ public class cgeocaches extends AbstractListActivity {
}
invalidateOptionsMenuCompatible();
return false;
- case MENU_SORT_DISTANCE:
- setComparator(item, null);
- return false;
- case MENU_SORT_DIFFICULTY:
- setComparator(item, new DifficultyComparator());
- return false;
- case MENU_SORT_TERRAIN:
- setComparator(item, new TerrainComparator());
- return false;
- case MENU_SORT_SIZE:
- setComparator(item, new SizeComparator());
- return false;
- case MENU_SORT_FAVORITES:
- setComparator(item, new PopularityComparator());
- return false;
- case MENU_SORT_NAME:
- setComparator(item, new NameComparator());
- return false;
- case MENU_SORT_GEOCODE:
- setComparator(item, new GeocodeComparator());
- return false;
- case MENU_SORT_STORAGE:
- setComparator(item, new StorageTimeComparator());
- return false;
case MENU_SWITCH_LIST:
selectList(null);
invalidateOptionsMenuCompatible();
return false;
- case MENU_SORT_RATING:
- setComparator(item, new RatingComparator());
- return false;
- case MENU_SORT_VOTE:
- setComparator(item, new VoteComparator());
- return false;
- case MENU_SORT_INVENTORY:
- setComparator(item, new InventoryComparator());
- return false;
- case MENU_SORT_DATE:
- setComparator(item, new DateComparator());
- return true;
- case MENU_SORT_FINDS:
- setComparator(item, new FindsComparator(app));
- return true;
- case MENU_SORT_STATE:
- setComparator(item, new StateComparator());
- return true;
case MENU_FILTER:
new FilterUserInterface(this).selectFilter(new RunnableWithArgument<IFilter>() {
@Override
@@ -978,6 +864,14 @@ public class cgeocaches extends AbstractListActivity {
}
});
return true;
+ case MENU_SORT:
+ new ComparatorUserInterface(this).selectComparator(adapter.getCacheComparator(), new RunnableWithArgument<CacheComparator>() {
+ @Override
+ public void run(CacheComparator selectedComparator) {
+ setComparator(selectedComparator);
+ }
+ });
+ return true;
case MENU_IMPORT_WEB:
importWeb();
return false;
@@ -997,12 +891,10 @@ public class cgeocaches extends AbstractListActivity {
return CacheListAppFactory.onMenuItemSelected(item, cacheList, this, search);
}
- private void setComparator(MenuItem item,
- CacheComparator comparator) {
+ private void setComparator(final CacheComparator comparator) {
if (adapter != null) {
adapter.setComparator(comparator);
}
- item.setChecked(true);
}
@Override
@@ -1032,7 +924,7 @@ public class cgeocaches extends AbstractListActivity {
if (cache.getCoords() != null) {
menu.add(0, MENU_DEFAULT_NAVIGATION, 0, NavigationAppFactory.getDefaultNavigationApplication().getName());
menu.add(1, MENU_NAVIGATION, 0, res.getString(R.string.cache_menu_navigate)).setIcon(R.drawable.ic_menu_mapmode);
- addVisitMenu(menu, cache);
+ LoggingUI.addMenuItems(menu, cache);
menu.add(0, MENU_CACHE_DETAILS, 0, res.getString(R.string.cache_menu_details));
}
if (cache.isOffline()) {
@@ -1094,9 +986,6 @@ public class cgeocaches extends AbstractListActivity {
case MENU_NAVIGATION:
NavigationAppFactory.showNavigationMenu(this, cache, null, null);
break;
- case MENU_LOG_VISIT:
- cache.logVisit(this);
- break;
case MENU_CACHE_DETAILS:
final Intent cachesIntent = new Intent(this, CacheDetailActivity.class);
cachesIntent.putExtra("geocode", cache.getGeocode().toUpperCase());
@@ -1107,6 +996,7 @@ public class cgeocaches extends AbstractListActivity {
cache.drop(new Handler() {
@Override
public void handleMessage(Message msg) {
+ adapter.notifyDataSetChanged();
refreshCurrentList();
}
});
@@ -1134,8 +1024,7 @@ public class cgeocaches extends AbstractListActivity {
// in Android:
// https://code.google.com/p/android/issues/detail?id=7139
lastMenuInfo = info;
- // create a search for a single cache (as if in details view)
- cache.logOffline(this, LogType.getById(id - MENU_LOG_VISIT_OFFLINE));
+ LoggingUI.onMenuItemSelected(item, this, cache);
}
return true;
@@ -1381,6 +1270,7 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void run() {
search = cgeoapplication.getInstance().getBatchOfStoredCaches(true, coords, Settings.getCacheType(), listId);
+ replaceCacheListFromSearch();
loadCachesHandler.sendMessage(Message.obtain());
}
}
@@ -1389,6 +1279,7 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void run() {
search = cgeoapplication.getInstance().getHistoryOfCaches(true, coords != null ? Settings.getCacheType() : CacheType.ALL);
+ replaceCacheListFromSearch();
loadCachesHandler.sendMessage(Message.obtain());
}
}
@@ -1414,7 +1305,8 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void runSearch() {
- search = GCParser.searchByCoords(coords, cacheType, Settings.isShowCaptcha());
+ search = GCParser.searchByCoords(coords, Settings.getCacheType(), Settings.isShowCaptcha());
+ replaceCacheListFromSearch();
}
}
@@ -1428,7 +1320,8 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void runSearch() {
- search = GCParser.searchByKeyword(keyword, cacheType, Settings.isShowCaptcha());
+ search = GCParser.searchByKeyword(keyword, Settings.getCacheType(), Settings.isShowCaptcha());
+ replaceCacheListFromSearch();
}
}
@@ -1442,7 +1335,8 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void runSearch() {
- search = GCParser.searchByUsername(username, cacheType, Settings.isShowCaptcha());
+ search = GCParser.searchByUsername(username, Settings.getCacheType(), Settings.isShowCaptcha());
+ replaceCacheListFromSearch();
}
}
@@ -1456,7 +1350,8 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void runSearch() {
- search = GCParser.searchByOwner(username, cacheType, Settings.isShowCaptcha());
+ search = GCParser.searchByOwner(username, Settings.getCacheType(), Settings.isShowCaptcha());
+ replaceCacheListFromSearch();
}
}