diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2012-05-09 22:12:25 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2012-05-09 22:12:25 +0200 |
| commit | 4bff49b3ba2c3c678795b2e00afd381e72c1d31b (patch) | |
| tree | 8ea3d40b3635a61b99f000058389864f029509c4 | |
| parent | 6cf45296c90d0a8940cb3c172558a55991cbfbf3 (diff) | |
| download | cgeo-4bff49b3ba2c3c678795b2e00afd381e72c1d31b.zip cgeo-4bff49b3ba2c3c678795b2e00afd381e72c1d31b.tar.gz cgeo-4bff49b3ba2c3c678795b2e00afd381e72c1d31b.tar.bz2 | |
#1478: more cleanup of cache lists
| -rw-r--r-- | main/src/cgeo/geocaching/CacheDetailActivity.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 8 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeoapplication.java | 15 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeocaches.java | 316 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/CacheListAdapter.java | 74 |
6 files changed, 168 insertions, 253 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 2daca03..376e282 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -1699,7 +1699,7 @@ public class CacheDetailActivity extends AbstractActivity { final Button offlineRefresh = (Button) view.findViewById(R.id.offline_refresh); final Button offlineStore = (Button) view.findViewById(R.id.offline_store); - if (cache.getListId() >= StoredList.STANDARD_LIST_ID) { + if (cache.isOffline()) { long diff = (System.currentTimeMillis() / (60 * 1000)) - (cache.getDetailedUpdate() / (60 * 1000)); // minutes String ago; diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 75bc38a..957ddb2 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -1437,7 +1437,7 @@ public class cgCache implements ICache, IWaypoint { // get cache details, they may not yet be complete if (origCache != null) { // only reload the cache if it was already stored or doesn't have full details (by checking the description) - if (origCache.getListId() >= StoredList.STANDARD_LIST_ID || StringUtils.isBlank(origCache.getDescription())) { + if (origCache.isOffline() || StringUtils.isBlank(origCache.getDescription())) { final SearchResult search = searchByGeocode(origCache.getGeocode(), null, listId, false, handler); cache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_OR_DB); } else { @@ -1542,4 +1542,8 @@ public class cgCache implements ICache, IWaypoint { return null; } + public boolean isOffline() { + return listId >= StoredList.STANDARD_LIST_ID; + } + } diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 1d545a9..6daeb1f 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -2586,8 +2586,8 @@ public class cgData { return false; } - public void setVisitDate(String geocode, long visitedDate) { - if (StringUtils.isBlank(geocode)) { + public void setVisitDate(List<String> geocodes, long visitedDate) { + if (geocodes.isEmpty()) { return; } @@ -2598,7 +2598,9 @@ public class cgData { ContentValues values = new ContentValues(); values.put("visiteddate", visitedDate); - databaseRW.update(dbTableCaches, values, "geocode = ?", new String[] { geocode }); + for (String geocode : geocodes) { + databaseRW.update(dbTableCaches, values, "geocode = ?", new String[] { geocode }); + } databaseRW.setTransactionSuccessful(); } finally { databaseRW.endTransaction(); diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index bf96575..231bf55 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -21,6 +21,7 @@ import android.os.Handler; import android.os.Message; import java.io.File; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.EnumSet; @@ -351,14 +352,18 @@ public class cgeoapplication extends Application { storage.clearLogOffline(geocode); } - /** {@link cgData#setVisitDate(String, long)} */ + /** {@link cgData#setVisitDate(List, long)} */ public void saveVisitDate(String geocode) { - storage.setVisitDate(geocode, System.currentTimeMillis()); + storage.setVisitDate(Collections.singletonList(geocode), System.currentTimeMillis()); } - /** {@link cgData#setVisitDate(String, long)} */ - public void clearVisitDate(String geocode) { - storage.setVisitDate(geocode, 0); + /** {@link cgData#setVisitDate(List, long)} */ + public void clearVisitDate(List<cgCache> caches) { + ArrayList<String> geocodes = new ArrayList<String>(caches.size()); + for (cgCache cache : caches) { + geocodes.add(cache.getGeocode()); + } + storage.setVisitDate(geocodes, 0); } /** {@link cgData#getLists(Resources)} */ diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index 356dce3..894671f 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -79,9 +79,13 @@ import java.util.Set; public class cgeocaches extends AbstractListActivity { - private static final int MAX_LIST_ITEMS = 1000; + private static final String EXTRAS_USERNAME = "username"; + private static final String EXTRAS_ADDRESS = "address"; + private static final String EXTRAS_SEARCH = "search"; + private static final String EXTRAS_KEYWORD = "keyword"; private static final String EXTRAS_LIST_TYPE = "type"; private static final String EXTRAS_COORDS = "coords"; + private static final int MAX_LIST_ITEMS = 1000; private static final int MENU_REFRESH_STORED = 2; private static final int MENU_CACHE_DETAILS = 4; private static final int MENU_DROP_CACHES = 5; @@ -130,16 +134,13 @@ public class cgeocaches extends AbstractListActivity { private CacheListType type = null; private Geopoint coords = null; private CacheType cacheType = Settings.getCacheType(); - private String keyword = null; - private String address = null; - private String username = null; private SearchResult search = null; - private List<cgCache> cacheList = new ArrayList<cgCache>(); + private final List<cgCache> cacheList = new ArrayList<cgCache>(); private CacheListAdapter adapter = null; private LayoutInflater inflater = null; private View listFooter = null; private TextView listFooterText = null; - private Progress progress = new Progress(); + private final Progress progress = new Progress(); private Float northHeading = 0f; private String title = ""; private int detailTotal = 0; @@ -149,7 +150,14 @@ public class cgeocaches extends AbstractListActivity { private LoadFromWebThread threadWeb = null; private RemoveFromHistoryThread threadH = null; private int listId = StoredList.TEMPORARY_LIST_ID; - private UpdateHandler updateHandler = new UpdateHandler(); + private final UpdateHandler updateHandler = new UpdateHandler(); + private ContextMenuInfo lastMenuInfo; + private String contextMenuGeocode = ""; + /** + * the navigation menu item for the cache list (not the context menu!), or <code>null</code> + */ + private MenuItem navigationMenu; + private Handler loadCachesHandler = new Handler() { @Override @@ -158,11 +166,7 @@ public class cgeocaches extends AbstractListActivity { if (search != null) { setTitle(title + " [" + search.getCount() + ']'); cacheList.clear(); - - final Set<cgCache> caches = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); - if (CollectionUtils.isNotEmpty(caches)) { - cacheList.addAll(caches); - } + cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB)); } else { setTitle(title); } @@ -171,12 +175,9 @@ public class cgeocaches extends AbstractListActivity { setDateComparatorForEventList(); - if (cacheList == null) { - showToast(res.getString(R.string.err_list_load_fail)); - } showFooterMoreCaches(); - if (cacheList != null && search != null && search.getError() == StatusCode.UNAPPROVED_LICENSE) { + if (search != null && search.getError() == StatusCode.UNAPPROVED_LICENSE) { AlertDialog.Builder dialog = new AlertDialog.Builder(cgeocaches.this); dialog.setTitle(res.getString(R.string.license)); dialog.setMessage(res.getString(R.string.err_license)); @@ -244,12 +245,7 @@ public class cgeocaches extends AbstractListActivity { if (search != null) { setTitle(title + " [" + search.getCount() + "]"); cacheList.clear(); - - final Set<cgCache> caches = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); - if (CollectionUtils.isNotEmpty(caches)) { - cacheList.addAll(caches); - caches.clear(); - } + cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB)); if (adapter != null) { adapter.reFilter(); } @@ -259,9 +255,6 @@ public class cgeocaches extends AbstractListActivity { setAdapter(); - if (cacheList == null) { - showToast(res.getString(R.string.err_list_load_fail)); - } showFooterMoreCaches(); if (search != null && search.getError() != null) { @@ -324,12 +317,11 @@ public class cgeocaches extends AbstractListActivity { } else if (msg.what == MSG_RESTART_GEO_AND_DIR) { startGeoAndDir(); } else { - if (cacheList != null && search != null) { + if (search != null) { final Set<cgCache> cacheListTmp = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); if (CollectionUtils.isNotEmpty(cacheListTmp)) { cacheList.clear(); cacheList.addAll(cacheListTmp); - cacheListTmp.clear(); } } @@ -380,12 +372,7 @@ public class cgeocaches extends AbstractListActivity { } cacheList.clear(); - - final Set<cgCache> cacheListTmp = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); - if (CollectionUtils.isNotEmpty(cacheListTmp)) { - cacheList.addAll(cacheListTmp); - cacheListTmp.clear(); - } + cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB)); progress.dismiss(); } @@ -403,12 +390,7 @@ public class cgeocaches extends AbstractListActivity { refreshCurrentList(); cacheList.clear(); - - final Set<cgCache> cacheListTmp = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); - if (CollectionUtils.isNotEmpty(cacheListTmp)) { - cacheList.addAll(cacheListTmp); - cacheListTmp.clear(); - } + cacheList.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB)); progress.dismiss(); } @@ -421,12 +403,7 @@ public class cgeocaches extends AbstractListActivity { setAdapter(); if (msg.what > -1) { - cacheList.get(msg.what).setStatusChecked(false); progress.setProgress(detailProgress); - } else if (msg.what == MSG_CANCEL) { - if (threadH != null) { - threadH.kill(); - } } else { if (adapter != null) { adapter.setSelectMode(false); @@ -447,13 +424,6 @@ public class cgeocaches extends AbstractListActivity { } }; - private ContextMenuInfo lastMenuInfo; - private String contextMenuGeocode = ""; - /** - * the navigation menu item for the cache list (not the context menu!), or <code>null</code> - */ - private MenuItem navigationMenu; - public cgeocaches() { super(true); } @@ -470,15 +440,13 @@ public class cgeocaches extends AbstractListActivity { // get parameters final Bundle extras = getIntent().getExtras(); - if (extras != null) { - Object typeObject = extras.get(EXTRAS_LIST_TYPE); - type = (typeObject instanceof CacheListType) ? (CacheListType) typeObject : CacheListType.OFFLINE; - coords = (Geopoint) extras.getParcelable(EXTRAS_COORDS); - cacheType = Settings.getCacheType(); - keyword = extras.getString("keyword"); - address = extras.getString("address"); - username = extras.getString("username"); + if (extras == null) { + throw new UnsupportedOperationException(); // shall not happen, as we always set the extras when invoking the activity } + Object typeObject = extras.get(EXTRAS_LIST_TYPE); + type = (typeObject instanceof CacheListType) ? (CacheListType) typeObject : CacheListType.OFFLINE; + coords = (Geopoint) extras.getParcelable(EXTRAS_COORDS); + cacheType = Settings.getCacheType(); if (Intent.ACTION_VIEW.equals(getIntent().getAction())) { type = CacheListType.OFFLINE; if (coords == null) { @@ -497,6 +465,7 @@ public class cgeocaches extends AbstractListActivity { Thread threadPure; AbstractSearchThread thread; + final String username = extras.getString(EXTRAS_USERNAME); switch (type) { case OFFLINE: listId = Settings.getLastList(); @@ -550,6 +519,7 @@ public class cgeocaches extends AbstractListActivity { thread.start(); break; case KEYWORD: + final String keyword = extras.getString(EXTRAS_KEYWORD); title = keyword; setTitle(title); showProgress(true); @@ -561,6 +531,7 @@ public class cgeocaches extends AbstractListActivity { break; case ADDRESS: action = "planning"; + final String address = extras.getString(EXTRAS_ADDRESS); if (StringUtils.isNotBlank(address)) { title = address; setTitle(title); @@ -601,7 +572,7 @@ public class cgeocaches extends AbstractListActivity { title = res.getString(R.string.map_map); setTitle(title); showProgress(true); - SearchResult result = extras != null ? (SearchResult) extras.get("search") : null; + SearchResult result = (SearchResult) extras.get(EXTRAS_SEARCH); search = new SearchResult(result); loadCachesHandler.sendMessage(Message.obtain()); break; @@ -676,11 +647,6 @@ public class cgeocaches extends AbstractListActivity { } @Override - public void onStop() { - super.onStop(); - } - - @Override public void onPause() { removeGeoAndDir(); @@ -763,7 +729,7 @@ public class cgeocaches extends AbstractListActivity { super.onPrepareOptionsMenu(menu); try { - if (adapter != null && adapter.getSelectMode()) { + if (adapter != null && adapter.isSelectMode()) { menu.findItem(MENU_SWITCH_SELECT_MODE).setTitle(res.getString(R.string.caches_select_mode_exit)) .setIcon(R.drawable.ic_menu_clear_playlist); menu.findItem(MENU_INVERT_SELECTION).setVisible(true); @@ -961,7 +927,7 @@ public class cgeocaches extends AbstractListActivity { importWeb(); return false; case MENU_EXPORT: - exportCaches(); + ExportFactory.showExportMenu(adapter.getCheckedOrAllCaches(), this); return false; case MENU_REMOVE_FROM_HISTORY: removeFromHistoryCheck(); @@ -1004,11 +970,7 @@ public class cgeocaches extends AbstractListActivity { } final cgCache cache = adapter.getItem(adapterInfo.position); - if (StringUtils.isNotBlank(cache.getName())) { - menu.setHeaderTitle(cache.getName()); - } else { - menu.setHeaderTitle(cache.getGeocode()); - } + menu.setHeaderTitle(StringUtils.defaultIfBlank(cache.getName(), cache.getGeocode())); contextMenuGeocode = cache.getGeocode(); @@ -1018,11 +980,10 @@ public class cgeocaches extends AbstractListActivity { addVisitMenu(menu, cache); menu.add(0, MENU_CACHE_DETAILS, 0, res.getString(R.string.cache_menu_details)); } - if (cache.getListId() >= 1) { + if (cache.isOffline()) { menu.add(0, MENU_DROP_CACHE, 0, res.getString(R.string.cache_offline_drop)); final List<StoredList> cacheLists = app.getLists(); - final int listCount = cacheLists.size(); - if (listCount > 1) { + if (cacheLists.size() > 1) { menu.add(0, MENU_MOVE_TO_LIST, 0, res.getString(R.string.cache_menu_move_list)); } } @@ -1036,14 +997,7 @@ public class cgeocaches extends AbstractListActivity { @Override public void run(Integer newListId) { - List<cgCache> selected; - final boolean moveAll = adapter.getCheckedCount() == 0; - if (moveAll) { - selected = new ArrayList<cgCache>(cacheList); - } else { - selected = adapter.getCheckedCaches(); - } - app.moveToList(selected, newListId); + app.moveToList(adapter.getCheckedOrAllCaches(), newListId); adapter.setSelectMode(false); refreshCurrentList(); @@ -1158,7 +1112,7 @@ public class cgeocaches extends AbstractListActivity { public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (adapter != null) { - if (adapter.getSelectMode()) { + if (adapter.isSelectMode()) { adapter.setSelectMode(false); return true; } @@ -1211,7 +1165,7 @@ public class cgeocaches extends AbstractListActivity { return; } - boolean enableMore = (type != CacheListType.OFFLINE && cacheList != null && cacheList.size() < MAX_LIST_ITEMS); + boolean enableMore = (type != CacheListType.OFFLINE && cacheList.size() < MAX_LIST_ITEMS); if (enableMore && search != null) { final int count = search.getTotal(); enableMore = enableMore && count > 0 && cacheList.size() < count; @@ -1252,13 +1206,7 @@ public class cgeocaches extends AbstractListActivity { } public void refreshStored() { - if (adapter != null && adapter.getCheckedCount() > 0) { - // there are some checked caches - detailTotal = adapter.getCheckedCount(); - } else { - // no checked caches, download everything (when already stored - refresh them) - detailTotal = cacheList.size(); - } + detailTotal = adapter.getCheckedOrAllCount(); detailProgress = 0; showProgress(false); @@ -1305,16 +1253,7 @@ public class cgeocaches extends AbstractListActivity { } public void removeFromHistory() { - if (adapter != null && adapter.getCheckedCount() > 0) - { - // there are some checked caches - detailTotal = adapter.getCheckedCount(); - } - else - { - // no checked caches, remove all - detailTotal = cacheList.size(); - } + detailTotal = adapter.getCheckedOrAllCount(); detailProgress = 0; showProgress(false); @@ -1325,19 +1264,6 @@ public class cgeocaches extends AbstractListActivity { threadH.start(); } - public void exportCaches() { - List<cgCache> caches; - if (adapter != null && adapter.getCheckedCount() > 0) { - // there are some caches checked - caches = adapter.getCheckedCaches(); - } else { - // no caches checked, export all - caches = cacheList; - } - - ExportFactory.showExportMenu(caches, this); - } - public void importWeb() { detailProgress = 0; @@ -1389,9 +1315,9 @@ public class cgeocaches extends AbstractListActivity { @Override public void handleMessage(final Message message) { if (message.obj instanceof IGeoData) { - updateGeoData((IGeoData) message.obj); + updateGeoData((IGeoData) message.obj); } else { - updateDirection((Float) message.obj); + updateDirection((Float) message.obj); } } @@ -1407,7 +1333,7 @@ public class cgeocaches extends AbstractListActivity { } try { - if (cacheList != null && geo.getCoords() != null) { + if (geo.getCoords() != null) { adapter.setActualCoordinates(geo.getCoords()); } @@ -1476,12 +1402,6 @@ public class cgeocaches extends AbstractListActivity { public LoadByCoordsThread(final Geopoint coords) { super(loadCachesHandler); this.coords = coords; - - if (coords == null) { - showToast(res.getString(R.string.warn_no_coordinates)); - - finish(); - } } @Override @@ -1496,12 +1416,6 @@ public class cgeocaches extends AbstractListActivity { public LoadByKeywordThread(final String keyword) { super(loadCachesHandler); this.keyword = keyword; - - if (keyword == null) { - showToast(res.getString(R.string.warn_no_keyword)); - - finish(); - } } @Override @@ -1516,12 +1430,6 @@ public class cgeocaches extends AbstractListActivity { public LoadByUserNameThread(final String username) { super(loadCachesHandler); this.username = username; - - if (StringUtils.isBlank(username)) { - showToast(res.getString(R.string.warn_no_username)); - - finish(); - } } @Override @@ -1536,12 +1444,6 @@ public class cgeocaches extends AbstractListActivity { public LoadByOwnerThread(final String username) { super(loadCachesHandler); this.username = username; - - if (StringUtils.isBlank(username)) { - showToast(res.getString(R.string.warn_no_username)); - - finish(); - } } @Override @@ -1559,8 +1461,6 @@ public class cgeocaches extends AbstractListActivity { private long last = 0L; public LoadDetailsThread(Handler handlerIn, int listId) { - setPriority(Thread.MIN_PRIORITY); - handler = handlerIn; // in case of online lists, set the list id to the standard list @@ -1638,8 +1538,6 @@ public class cgeocaches extends AbstractListActivity { private volatile boolean needToStop = false; public LoadFromWebThread(Handler handlerIn, int listId) { - setPriority(Thread.MIN_PRIORITY); - handler = handlerIn; listIdLFW = listId; } @@ -1720,20 +1618,11 @@ public class cgeocaches extends AbstractListActivity { private class DropDetailsThread extends Thread { final private Handler handler; - private List<cgCache> selected = new ArrayList<cgCache>(); + final private List<cgCache> selected; public DropDetailsThread(Handler handlerIn) { - setPriority(Thread.MIN_PRIORITY); - handler = handlerIn; - - int checked = adapter.getCheckedCount(); - if (checked == 0) { - selected = new ArrayList<cgCache>(cacheList); - } - else { - selected = adapter.getCheckedCaches(); - } + selected = adapter.getCheckedOrAllCaches(); } @Override @@ -1749,50 +1638,16 @@ public class cgeocaches extends AbstractListActivity { private class RemoveFromHistoryThread extends Thread { final private Handler handler; - private volatile boolean needToStop = false; - private int checked = 0; + final private List<cgCache> selected; public RemoveFromHistoryThread(Handler handlerIn) { - setPriority(Thread.MIN_PRIORITY); - handler = handlerIn; - - if (adapter != null) { - checked = adapter.getCheckedCount(); - } - } - - public void kill() { - needToStop = true; + selected = adapter.getCheckedOrAllCaches(); } @Override public void run() { - for (cgCache cache : cacheList) { - if (checked > 0 && !cache.isStatusChecked()) { - handler.sendEmptyMessage(0); - - yield(); - continue; - } - - try { - if (needToStop) { - Log.i("Stopped removing process."); - break; - } - - app.clearVisitDate(cache.getGeocode()); - - detailProgress++; - handler.sendEmptyMessage(cacheList.indexOf(cache)); - - yield(); - } catch (Exception e) { - Log.e("cgeocaches.RemoveFromHistoryThread: " + e.toString()); - } - } - + app.clearVisitDate(selected); handler.sendEmptyMessage(MSG_DONE); } } @@ -1813,10 +1668,9 @@ public class cgeocaches extends AbstractListActivity { private void hideLoading() { final ListView list = getListView(); - final View loading = findViewById(R.id.loading); - if (list.getVisibility() == View.GONE) { list.setVisibility(View.VISIBLE); + final View loading = findViewById(R.id.loading); loading.setVisibility(View.GONE); } } @@ -1896,9 +1750,7 @@ public class cgeocaches extends AbstractListActivity { } private void removeListInternal() { - boolean status = app.removeList(listId); - - if (status) { + if (app.removeList(listId)) { showToast(res.getString(R.string.list_dialog_remove_ok)); switchListById(StoredList.STANDARD_LIST_ID); } else { @@ -1994,33 +1846,42 @@ public class cgeocaches extends AbstractListActivity { } public static void startActivityOwner(final AbstractActivity context, final String userName) { + if (!isValidUsername(context, userName)) { + return; + } final Intent cachesIntent = new Intent(context, cgeocaches.class); - cachesIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.OWNER); - cachesIntent.putExtra("username", userName); - + cachesIntent.putExtra(EXTRAS_USERNAME, userName); context.startActivity(cachesIntent); } + private static boolean isValidUsername(AbstractActivity context, String username) { + if (StringUtils.isBlank(username)) { + context.showToast(cgeoapplication.getInstance().getString(R.string.warn_no_username)); + return false; + } + return true; + } + public static void startActivityUserName(final AbstractActivity context, final String userName) { + if (!isValidUsername(context, userName)) { + return; + } final Intent cachesIntent = new Intent(context, cgeocaches.class); - cachesIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.USERNAME); - cachesIntent.putExtra("username", userName); - + cachesIntent.putExtra(EXTRAS_USERNAME, userName); context.startActivity(cachesIntent); } private void prepareFilterBar() { - if (Settings.getCacheType() != CacheType.ALL || adapter.isFilter()) { - String filter = ""; - String cacheType = Settings.getCacheType().getL10n(); + if (Settings.getCacheType() != CacheType.ALL || adapter.isFiltered()) { + final StringBuilder output = new StringBuilder(Settings.getCacheType().getL10n()); - if (adapter.isFilter()) { - filter = ", " + adapter.getFilterName(); + if (adapter.isFiltered()) { + output.append(", ").append(adapter.getFilterName()); } - ((TextView) findViewById(R.id.filter_text)).setText(cacheType + filter); + ((TextView) findViewById(R.id.filter_text)).setText(output.toString()); findViewById(R.id.filter_bar).setVisibility(View.VISIBLE); } else { @@ -2030,6 +1891,8 @@ public class cgeocaches extends AbstractListActivity { /** * set date comparator for pure event lists + * + * TODO: move this method into the adapter */ private void setDateComparatorForEventList() { if (CollectionUtils.isNotEmpty(cacheList)) { @@ -2052,7 +1915,10 @@ public class cgeocaches extends AbstractListActivity { } } - public static void startActivityNearest(final Context context, final Geopoint coordsNow) { + public static void startActivityNearest(final AbstractActivity context, final Geopoint coordsNow) { + if (!isValidCoords(context, coordsNow)) { + return; + } final Intent cachesIntent = new Intent(context, cgeocaches.class); cachesIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.NEAREST); cachesIntent.putExtra(EXTRAS_COORDS, coordsNow); @@ -2069,29 +1935,43 @@ public class cgeocaches extends AbstractListActivity { final Intent addressIntent = new Intent(context, cgeocaches.class); addressIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.ADDRESS); addressIntent.putExtra(EXTRAS_COORDS, coords); - addressIntent.putExtra("address", address); + addressIntent.putExtra(EXTRAS_ADDRESS, address); context.startActivity(addressIntent); } - public static void startActivityCoordinates(final Context context, final Geopoint coords) { + public static void startActivityCoordinates(final AbstractActivity context, final Geopoint coords) { + if (!isValidCoords(context, coords)) { + return; + } final Intent cachesIntent = new Intent(context, cgeocaches.class); cachesIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.COORDINATE); cachesIntent.putExtra(EXTRAS_COORDS, coords); context.startActivity(cachesIntent); } - public static void startActivityKeyword(final Context context, final String keyword) { + private static boolean isValidCoords(AbstractActivity context, Geopoint coords) { + if (coords == null) { + context.showToast(cgeoapplication.getInstance().getString(R.string.warn_no_coordinates)); + return false; + } + return true; + } + + public static void startActivityKeyword(final AbstractActivity context, final String keyword) { + if (keyword == null) { + context.showToast(cgeoapplication.getInstance().getString(R.string.warn_no_keyword)); + return; + } final Intent cachesIntent = new Intent(context, cgeocaches.class); cachesIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.KEYWORD); - cachesIntent.putExtra("keyword", keyword); + cachesIntent.putExtra(EXTRAS_KEYWORD, keyword); context.startActivity(cachesIntent); } public static void startActivityMap(final Context context, final SearchResult search) { final Intent cachesIntent = new Intent(context, cgeocaches.class); cachesIntent.putExtra(EXTRAS_LIST_TYPE, CacheListType.MAP); - cachesIntent.putExtra("search", search); + cachesIntent.putExtra(EXTRAS_SEARCH, search); context.startActivity(cachesIntent); } - } diff --git a/main/src/cgeo/geocaching/ui/CacheListAdapter.java b/main/src/cgeo/geocaching/ui/CacheListAdapter.java index ea7fc38..69125b4 100644 --- a/main/src/cgeo/geocaching/ui/CacheListAdapter.java +++ b/main/src/cgeo/geocaching/ui/CacheListAdapter.java @@ -1,9 +1,11 @@ package cgeo.geocaching.ui; import cgeo.geocaching.CacheDetailActivity; +import cgeo.geocaching.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.Settings; import cgeo.geocaching.cgCache; +import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.enumerations.CacheListType; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; @@ -49,7 +51,7 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { private LayoutInflater inflater = null; private CacheComparator cacheComparator = null; - private Geopoint coords = null; + private Geopoint coords; private float azimuth = 0; private long lastSort = 0L; private boolean selectMode = false; @@ -58,7 +60,6 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { final private Set<CompassMiniView> compasses = new LinkedHashSet<CompassMiniView>(); final private Set<DistanceView> distances = new LinkedHashSet<DistanceView>(); - final private int[] ratingBcgs = new int[3]; final private CacheListType cacheListType; final private Resources res; /** Resulting list of caches */ @@ -72,9 +73,25 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { */ private static final int PAUSE_BETWEEN_LIST_SORT = 1000; + private static final int[] RATING_BACKGROUND = new int[3]; + static { + if (Settings.isLightSkin()) { + RATING_BACKGROUND[0] = R.drawable.favourite_background_red_light; + RATING_BACKGROUND[1] = R.drawable.favourite_background_orange_light; + RATING_BACKGROUND[2] = R.drawable.favourite_background_green_light; + } else { + RATING_BACKGROUND[0] = R.drawable.favourite_background_red_dark; + RATING_BACKGROUND[1] = R.drawable.favourite_background_orange_dark; + RATING_BACKGROUND[2] = R.drawable.favourite_background_green_dark; + } + } + public CacheListAdapter(final Activity activity, final List<cgCache> list, CacheListType cacheListType) { super(activity, 0, list); - + final IGeoData currentGeo = cgeoapplication.getInstance().currentGeo(); + if (currentGeo != null) { + coords = currentGeo.getCoords(); + } this.res = activity.getResources(); this.list = list; this.cacheListType = cacheListType; @@ -99,16 +116,6 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { 0, 0); gcIconDrawables.put(hashCode, ld); } - - if (Settings.isLightSkin()) { - ratingBcgs[0] = R.drawable.favourite_background_red_light; - ratingBcgs[1] = R.drawable.favourite_background_orange_light; - ratingBcgs[2] = R.drawable.favourite_background_green_light; - } else { - ratingBcgs[0] = R.drawable.favourite_background_red_dark; - ratingBcgs[1] = R.drawable.favourite_background_orange_dark; - ratingBcgs[2] = R.drawable.favourite_background_green_dark; - } } private static int getIconHashCode(final CacheType cacheType, final boolean userModifiedOrFinal) { @@ -178,7 +185,7 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { notifyDataSetChanged(); } - public boolean isFilter() { + public boolean isFiltered() { return currentFilter != null; } @@ -207,12 +214,12 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { notifyDataSetChanged(); } - public boolean getSelectMode() { + public boolean isSelectMode() { return selectMode; } public void switchSelectMode() { - setSelectMode(!getSelectMode()); + setSelectMode(!isSelectMode()); } public void invertSelection() { @@ -267,8 +274,9 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { if (!isSortedByDistance()) { return; } - - Log.w(System.currentTimeMillis() + ": " + coords.toString()); + if (coords == null) { + return; + } Collections.sort(list, new DistanceComparator(coords, list)); notifyDataSetChanged(); lastSort = System.currentTimeMillis(); @@ -451,20 +459,20 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { final float myVote = cache.getMyVote(); if (myVote > 0) { // use my own rating for display, if I have voted if (myVote >= 4) { - favoriteBack = ratingBcgs[2]; + favoriteBack = RATING_BACKGROUND[2]; } else if (myVote >= 3) { - favoriteBack = ratingBcgs[1]; + favoriteBack = RATING_BACKGROUND[1]; } else if (myVote > 0) { - favoriteBack = ratingBcgs[0]; + favoriteBack = RATING_BACKGROUND[0]; } } else { final float rating = cache.getRating(); if (rating >= 3.5) { - favoriteBack = ratingBcgs[2]; + favoriteBack = RATING_BACKGROUND[2]; } else if (rating >= 2.1) { - favoriteBack = ratingBcgs[1]; + favoriteBack = RATING_BACKGROUND[1]; } else if (rating > 0.0) { - favoriteBack = ratingBcgs[0]; + favoriteBack = RATING_BACKGROUND[0]; } } holder.favourite.setBackgroundResource(favoriteBack); @@ -558,7 +566,7 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { return; } - if (getSelectMode()) { + if (isSelectMode()) { cache.setStatusChecked(!cache.isStatusChecked()); notifyDataSetChanged(); return; @@ -641,4 +649,20 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { } return result; } + + public List<cgCache> getCheckedOrAllCaches() { + final List<cgCache> result = getCheckedCaches(); + if (!result.isEmpty()) { + return result; + } + return new ArrayList<cgCache>(list); + } + + public int getCheckedOrAllCount() { + final int checked = getCheckedCount(); + if (checked > 0) { + return checked; + } + return list.size(); + } } |
