diff options
26 files changed, 495 insertions, 429 deletions
diff --git a/src/cgeo/geocaching/cgCacheDifficultyComparator.java b/src/cgeo/geocaching/cgCacheDifficultyComparator.java deleted file mode 100644 index 69971a4..0000000 --- a/src/cgeo/geocaching/cgCacheDifficultyComparator.java +++ /dev/null @@ -1,26 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheDifficultyComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.difficulty == null || cache2.difficulty == null) { - return 0; - } - - if (cache1.difficulty > cache2.difficulty) { - return 1; - } else if (cache2.difficulty > cache1.difficulty) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheDifficultyComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheDistanceComparator.java b/src/cgeo/geocaching/cgCacheDistanceComparator.java deleted file mode 100644 index 2fbdd64..0000000 --- a/src/cgeo/geocaching/cgCacheDistanceComparator.java +++ /dev/null @@ -1,51 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheDistanceComparator implements Comparator<cgCache> { - private Double latitude = null; - private Double longitude = null; - - public cgCacheDistanceComparator() { - // nothing - } - - public cgCacheDistanceComparator(Double latitudeIn, Double longitudeIn) { - latitude = latitudeIn; - longitude = longitudeIn; - } - - public void setCoords(Double latitudeIn, Double longitudeIn) { - latitude = latitudeIn; - longitude = longitudeIn; - } - - public int compare(cgCache cache1, cgCache cache2) { - int result = 0; - try { - if ( - (cache1.latitude == null || cache1.longitude == null || cache2.latitude == null || cache2.longitude == null) && - cache1.distance != null && cache2.distance != null - ) { - if (cache1.distance < cache2.distance) return -1; - else if (cache1.distance > cache2.distance) return 1; - else return 0; - } else { - if (cache1.latitude == null || cache1.longitude == null) return 1; - if (cache2.latitude == null || cache2.longitude == null) return -1; - - Double distance1 = cgBase.getDistance(latitude, longitude, cache1.latitude, cache1.longitude); - Double distance2 = cgBase.getDistance(latitude, longitude, cache2.latitude, cache2.longitude); - - if (distance1 < distance2) result = -1; - else if (distance1 > distance2) result = 1; - else result = 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheDistanceComparator.compare: " + e.toString()); - } - - return result; - } -} diff --git a/src/cgeo/geocaching/cgCacheGeocodeComparator.java b/src/cgeo/geocaching/cgCacheGeocodeComparator.java deleted file mode 100644 index e06d9f3..0000000 --- a/src/cgeo/geocaching/cgCacheGeocodeComparator.java +++ /dev/null @@ -1,26 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheGeocodeComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.geocode == null || cache1.geocode.length() <= 0 || cache2.geocode == null || cache2.geocode.length() <= 0) { - return 0; - } - - if (cache1.geocode.length() > cache2.geocode.length()) { - return 1; - } else if (cache2.geocode.length() > cache1.geocode.length()) { - return -1; - } else { - return cache1.geocode.compareToIgnoreCase(cache2.geocode); - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheGeocodeComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheInventoryComparator.java b/src/cgeo/geocaching/cgCacheInventoryComparator.java deleted file mode 100644 index e58eb43..0000000 --- a/src/cgeo/geocaching/cgCacheInventoryComparator.java +++ /dev/null @@ -1,36 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -/** - * compares by number of items in inventory - * @author bananeweizen - * - */ -public class cgCacheInventoryComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - int itemCount1 = 0; - int itemCount2 = 0; - if (cache1.difficulty != null) { - itemCount1 = cache1.inventoryItems; - } - if (cache2.difficulty != null) { - itemCount2 = cache2.inventoryItems; - } - - if (itemCount1 < itemCount2) { - return 1; - } else if (itemCount2 < itemCount1) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheInventoryComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheListAdapter.java b/src/cgeo/geocaching/cgCacheListAdapter.java index b1eded2..722f650 100644 --- a/src/cgeo/geocaching/cgCacheListAdapter.java +++ b/src/cgeo/geocaching/cgCacheListAdapter.java @@ -1,38 +1,40 @@ package cgeo.geocaching; -import java.util.List; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; +import java.util.Locale; + import android.app.Activity; -import android.text.Spannable; -import android.text.style.StrikethroughSpan; -import android.view.View; -import android.view.ViewGroup; -import android.view.LayoutInflater; -import android.widget.RelativeLayout; -import android.widget.TextView; -import android.widget.ArrayAdapter; import android.content.Intent; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; +import android.text.Spannable; +import android.text.style.StrikethroughSpan; import android.util.DisplayMetrics; import android.util.Log; import android.view.GestureDetector; +import android.view.LayoutInflater; import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; +import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ImageView; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Locale; - +import android.widget.RelativeLayout; +import android.widget.TextView; import cgeo.geocaching.filter.cgFilter; +import cgeo.geocaching.sorting.CacheComparator; +import cgeo.geocaching.sorting.DistanceComparator; +import cgeo.geocaching.sorting.VisitComparator; public class cgCacheListAdapter extends ArrayAdapter<cgCache> { @@ -43,8 +45,8 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> { private LayoutInflater inflater = null; private Activity activity = null; private cgBase base = null; - private cgCacheDistanceComparator dstComparator = null; - private Comparator statComparator = null; + private DistanceComparator dstComparator = null; + private CacheComparator statComparator = null; private boolean historic = false; private Double latitude = null; private Double longitude = null; @@ -73,7 +75,7 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> { settings = settingsIn; list = listIn; base = baseIn; - dstComparator = new cgCacheDistanceComparator(); + dstComparator = new DistanceComparator(); DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); @@ -107,12 +109,12 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> { } } - public void setComparator(Comparator comparator) { + public void setComparator(CacheComparator comparator) { statComparator = comparator; forceSort(latitude, longitude); } - + /** * Called when a new page of caches was loaded. */ @@ -120,7 +122,7 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> { if(currentFilter != null){ // Back up the list again originalList = new ArrayList<cgCache>(list); - + currentFilter.filter(list); } } @@ -133,34 +135,34 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> { if (originalList == null) { originalList = new ArrayList<cgCache>(list); } - + // If there is already a filter in place, this is a request to change or clear the filter, so we have to // replace the original cache list if (currentFilter != null) { list.clear(); list.addAll(originalList); } - + // Do the filtering or clear it if (filter != null) { filter.filter(list); } currentFilter = filter; - + notifyDataSetChanged(); } - + public void clearFilter() { if (originalList != null) { list.clear(); list.addAll(originalList); - + currentFilter = null; } - + notifyDataSetChanged(); } - + public boolean isFilter() { if (currentFilter != null) { return true; @@ -173,7 +175,7 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> { historic = historicIn; if (historic == true) { - statComparator = new cgCacheVisitComparator(); + statComparator = new VisitComparator(); } else { statComparator = null; } diff --git a/src/cgeo/geocaching/cgCacheNameComparator.java b/src/cgeo/geocaching/cgCacheNameComparator.java deleted file mode 100644 index fa247fa..0000000 --- a/src/cgeo/geocaching/cgCacheNameComparator.java +++ /dev/null @@ -1,20 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheNameComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.name == null || cache2.name == null) { - return 0; - } - - return cache1.name.compareToIgnoreCase(cache2.name); - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheNameComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCachePopularityComparator.java b/src/cgeo/geocaching/cgCachePopularityComparator.java deleted file mode 100644 index 10500f9..0000000 --- a/src/cgeo/geocaching/cgCachePopularityComparator.java +++ /dev/null @@ -1,26 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCachePopularityComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.favouriteCnt == null || cache2.favouriteCnt == null) { - return 0; - } - - if (cache1.favouriteCnt < cache2.favouriteCnt) { - return 1; - } else if (cache2.favouriteCnt < cache1.favouriteCnt) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCachePopularityComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheRatingComparator.java b/src/cgeo/geocaching/cgCacheRatingComparator.java deleted file mode 100644 index b7b9720..0000000 --- a/src/cgeo/geocaching/cgCacheRatingComparator.java +++ /dev/null @@ -1,36 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheRatingComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - Float rating1 = cache1.rating; - Float rating2 = cache2.rating; - if (rating1 == null || rating2 == null) { - return 0; - } - - // voting can be disabled for caches, then assume an average rating instead - if (rating1 == 0.0) { - rating1 = 2.5f; - } - if (rating2 == 0.0) { - rating2 = 2.5f; - } - - if (rating1 < rating2) { - return 1; - } else if (rating2 < rating1) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheRatingComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheSizeComparator.java b/src/cgeo/geocaching/cgCacheSizeComparator.java deleted file mode 100644 index a5d0298..0000000 --- a/src/cgeo/geocaching/cgCacheSizeComparator.java +++ /dev/null @@ -1,47 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; -import java.util.ArrayList; - -public class cgCacheSizeComparator implements Comparator<cgCache> { - public static ArrayList<String> cacheSizes = new ArrayList<String>(); - - public cgCacheSizeComparator() { - // list sizes - cacheSizes.add("micro"); - cacheSizes.add("small"); - cacheSizes.add("regular"); - cacheSizes.add("large"); - } - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.size == null || cache1.size.length() == 0 || cache2.size == null || cache2.size.length() == 0) { - return 0; - } - - int size1 = 0; - int size2 = 0; - - int cnt = 1; - for (String size : cacheSizes) { - if (size.equalsIgnoreCase(cache1.size)) size1 = cnt; - if (size.equalsIgnoreCase(cache2.size)) size2 = cnt; - - cnt ++; - } - - if (size1 < size2) { - return 1; - } else if (size2 < size1) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheSizeComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheTerrainComparator.java b/src/cgeo/geocaching/cgCacheTerrainComparator.java deleted file mode 100644 index 3d31a37..0000000 --- a/src/cgeo/geocaching/cgCacheTerrainComparator.java +++ /dev/null @@ -1,26 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheTerrainComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.terrain == null || cache2.terrain == null) { - return 0; - } - - if (cache1.terrain > cache2.terrain) { - return 1; - } else if (cache2.terrain > cache1.terrain) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheTerrainComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheVisitComparator.java b/src/cgeo/geocaching/cgCacheVisitComparator.java deleted file mode 100644 index 19a4b52..0000000 --- a/src/cgeo/geocaching/cgCacheVisitComparator.java +++ /dev/null @@ -1,27 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -public class cgCacheVisitComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - if (cache1.visitedDate == null || cache1.visitedDate <= 0 || cache2.visitedDate == null || cache2.visitedDate <= 0) { - return 0; - } - - if (cache1.visitedDate > cache2.visitedDate) { - return -1; - } else if (cache1.visitedDate < cache2.visitedDate) { - return 1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheVisitComparator.compare: " + e.toString()); - } - - return 0; - } -} diff --git a/src/cgeo/geocaching/cgCacheVoteComparator.java b/src/cgeo/geocaching/cgCacheVoteComparator.java deleted file mode 100644 index 581656c..0000000 --- a/src/cgeo/geocaching/cgCacheVoteComparator.java +++ /dev/null @@ -1,38 +0,0 @@ -package cgeo.geocaching; - -import java.util.Comparator; -import android.util.Log; - -/** - * sorts caches by the users own voting (if available at all) - * @author bananeweizen - * - */ -public class cgCacheVoteComparator implements Comparator<cgCache> { - - public int compare(cgCache cache1, cgCache cache2) { - try { - // if there is no vote available, put that cache at the end of the list - float vote1 = 0; - if (cache1.myVote != null) { - vote1 = cache1.myVote; - } - - float vote2 = 0; - if (cache2.myVote != null) { - vote2 = cache2.myVote; - } - - if (vote1 < vote2) { - return 1; - } else if (vote2 < vote1) { - return -1; - } else { - return 0; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgCacheVoteComparator.compare: " + e.toString()); - } - return 0; - } -} diff --git a/src/cgeo/geocaching/cgeocaches.java b/src/cgeo/geocaching/cgeocaches.java index c73b6c2..bf0fb29 100644 --- a/src/cgeo/geocaching/cgeocaches.java +++ b/src/cgeo/geocaching/cgeocaches.java @@ -13,7 +13,6 @@ import java.io.Writer; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -53,6 +52,16 @@ import cgeo.geocaching.filter.cgFilter; import cgeo.geocaching.filter.cgFilterBySize; import cgeo.geocaching.filter.cgFilterByTrackables; import cgeo.geocaching.filter.cgFilterByType; +import cgeo.geocaching.sorting.CacheComparator; +import cgeo.geocaching.sorting.DifficultyComparator; +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.TerrainComparator; +import cgeo.geocaching.sorting.VoteComparator; import com.google.android.apps.analytics.GoogleAnalyticsTracker; @@ -96,7 +105,7 @@ public class cgeocaches extends ListActivity { private int listId = 0; private ArrayList<cgList> lists = null; private String selectedFilter = null; - private cgCacheGeocodeComparator gcComparator = new cgCacheGeocodeComparator(); + private GeocodeComparator gcComparator = new GeocodeComparator(); private Handler loadCachesHandler = new Handler() { @Override @@ -327,7 +336,7 @@ public class cgeocaches extends ListActivity { @Override public void handleMessage(Message msg) { setAdapter(); - + if (adapter != null) { adapter.notifyDataSetChanged(); } @@ -348,7 +357,7 @@ public class cgeocaches extends ListActivity { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); warning.showToast(res.getString(R.string.gpx_import_no_files)); finish(); - return; + return; } else { if (adapter != null) { adapter.setSelectMode(false, true); @@ -657,7 +666,7 @@ public class cgeocaches extends ListActivity { subMenuFilter.add(0, 22, 0, res.getString(R.string.caches_filter_size)); subMenuFilter.add(0, 23, 0, res.getString(R.string.caches_filter_track)); subMenuFilter.add(0, 24, 0, res.getString(R.string.caches_filter_clear)); - + SubMenu subMenuSort = menu.addSubMenu(0, 104, 0, res.getString(R.string.caches_sort)).setIcon(android.R.drawable.ic_menu_sort_alphabetically); subMenuSort.setHeaderTitle(res.getString(R.string.caches_sort_title)); @@ -751,7 +760,7 @@ public class cgeocaches extends ListActivity { } else { menu.findItem(1).setTitle(res.getString(R.string.caches_refresh_all)); } - + if (adapter != null && adapter.getChecked() > 0) { menu.findItem(26).setTitle(res.getString(R.string.cache_export_fieldnote) + " (" + adapter.getChecked() + ")"); } else { @@ -831,34 +840,34 @@ public class cgeocaches extends ListActivity { setComparator(item, null); return false; case 11: - setComparator(item, new cgCacheDifficultyComparator()); + setComparator(item, new DifficultyComparator()); return false; case 12: - setComparator(item, new cgCacheTerrainComparator()); + setComparator(item, new TerrainComparator()); return false; case 13: - setComparator(item, new cgCacheSizeComparator()); + setComparator(item, new SizeComparator()); return false; case 14: - setComparator(item, new cgCachePopularityComparator()); + setComparator(item, new PopularityComparator()); return false; case 15: - setComparator(item, new cgCacheNameComparator()); + setComparator(item, new NameComparator()); return false; case 16: - setComparator(item, new cgCacheGeocodeComparator()); + setComparator(item, new GeocodeComparator()); return false; case 17: selectList(null); return false; case 18: - setComparator(item, new cgCacheRatingComparator()); + setComparator(item, new RatingComparator()); return false; case 19: - setComparator(item, new cgCacheVoteComparator()); + setComparator(item, new VoteComparator()); return false; case 20: - setComparator(item, new cgCacheInventoryComparator()); + setComparator(item, new InventoryComparator()); return false; case 21: selectedFilter = res.getString(R.string.caches_filter_type); @@ -888,7 +897,7 @@ public class cgeocaches extends ListActivity { } private void setComparator(MenuItem item, - Comparator<cgCache> comparator) { + CacheComparator comparator) { if (adapter != null) { adapter.setComparator(comparator); } @@ -909,7 +918,7 @@ public class cgeocaches extends ListActivity { } catch (Exception e) { Log.w(cgSettings.tag, "cgeocaches.onCreateContextMenu: " + e.toString()); } - + if ((adapterInfo == null || adapterInfo.position < 0) && selectedFilter != null){ // Context menu opened by selecting an option on the filter submenu @@ -919,8 +928,8 @@ public class cgeocaches extends ListActivity { menu.add(0, 9, 0, res.getString(R.string.caches_filter_size_small)); menu.add(0, 10, 0, res.getString(R.string.caches_filter_size_regular)); menu.add(0, 11, 0, res.getString(R.string.caches_filter_size_large)); - menu.add(0, 12, 0, res.getString(R.string.caches_filter_size_other)); - menu.add(0, 13, 0, res.getString(R.string.caches_filter_size_virtual)); + menu.add(0, 12, 0, res.getString(R.string.caches_filter_size_other)); + menu.add(0, 13, 0, res.getString(R.string.caches_filter_size_virtual)); menu.add(0, 14, 0, res.getString(R.string.caches_filter_size_notchosen)); } else if (selectedFilter.equals(res.getString(R.string.caches_filter_type))) { menu.setHeaderTitle(res.getString(R.string.caches_filter_type_title)); @@ -942,13 +951,13 @@ public class cgeocaches extends ListActivity { } } else{ final cgCache cache = adapter.getItem(adapterInfo.position); - + if (cache.name != null && cache.name.length() > 0) { menu.setHeaderTitle(cache.name); } else { menu.setHeaderTitle(cache.geocode); } - + if (cache.latitude != null && cache.longitude != null) { menu.add(0, 1, 0, res.getString(R.string.cache_menu_compass)); menu.add(0, 2, 0, res.getString(R.string.cache_menu_radar)); @@ -959,7 +968,7 @@ public class cgeocaches extends ListActivity { menu.add(0, 7, 0, res.getString(R.string.cache_menu_details)); } } - + ArrayList<cgList> cacheLists = app.getLists(); int listCount = cacheLists.size(); if (listCount > 1) { @@ -1023,7 +1032,7 @@ public class cgeocaches extends ListActivity { } else if (id == 29) { adapter.setFilter(filter = new cgFilterByType("gps")); } - } + } if(filter != null){ return true; } @@ -1151,7 +1160,7 @@ public class cgeocaches extends ListActivity { } return false; } - + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { @@ -1437,7 +1446,7 @@ public class cgeocaches extends ListActivity { threadD = new geocachesLoadDetails(loadDetailsHandler, listId); threadD.start(); } - + public void exportFieldNotes() { if (adapter != null && adapter.getChecked() > 0) @@ -1473,7 +1482,7 @@ public class cgeocaches extends ListActivity { waitDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); waitDialog.setMessage(res.getString(R.string.caches_exporting_fieldnote)); - + waitDialog.setCancelable(true); waitDialog.setMax(detailTotal); waitDialog.show(); @@ -1516,7 +1525,7 @@ public class cgeocaches extends ListActivity { threadW = new geocachesLoadFromWeb(downloadFromWebHandler, listId); threadW.start(); } - + public void dropStored() { AlertDialog.Builder dialog = new AlertDialog.Builder(activity); dialog.setCancelable(true); @@ -1912,7 +1921,7 @@ public class cgeocaches extends ListActivity { handler.sendEmptyMessage(-1); } } - + //tg private class geocachesLoadFromWeb extends Thread { @@ -1941,7 +1950,7 @@ public class cgeocaches extends ListActivity { if (geo != null) { geo = app.removeGeo(); } - + int delay=-1; int times=0; @@ -1952,12 +1961,12 @@ public class cgeocaches extends ListActivity { handler.sendEmptyMessage(-1); break; } - + //download new code String deviceCode = settings.webDeviceCode; if (deviceCode==null) deviceCode=""; cgResponse responseFromWeb = base.request(false, "send2cgeo.carnero.cc", "/readCode.php", "GET", "d=" + cgBase.urlencode_rfc3986(deviceCode), 0, true); - + if ((responseFromWeb.getStatusCode() == 200)&&(responseFromWeb.getData().length()>2)) { String GCcode = responseFromWeb.getData(); @@ -1968,9 +1977,9 @@ public class cgeocaches extends ListActivity { mes.obj=GCcode; handler.sendMessage(mes); yield(); - + base.storeCache(app, activity, null, GCcode, reason, null); - + Message mes1 = new Message(); mes1.what=2; mes1.obj=GCcode; @@ -2065,7 +2074,7 @@ public class cgeocaches extends ListActivity { handler.sendEmptyMessage(-1); } } - + private class geocachesExportFieldNotes extends Thread { private Handler handler = null; @@ -2094,7 +2103,7 @@ public class cgeocaches extends ListActivity { { SimpleDateFormat fieldNoteDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); StringBuffer fieldNoteBuffer = new StringBuffer(500); - + // We need our own HashMap because cgBase.LogTypes1 will give us localized and maybe // different strings than gc.com expects in the field note // We only need such logtypes that are possible to log via c:geo @@ -2104,7 +2113,7 @@ public class cgeocaches extends ListActivity { logTypes.put(cgBase.LOG_NOTE, "Write Note"); logTypes.put(cgBase.LOG_NEEDS_ARCHIVE, "Needs archived"); //logTypes.put(cgBase.LOG_NEEDS_MAINTENANCE, "Needs Maintenance"); // TODO: Strange problems, gc.com aborts with a parse-error - + for (cgCache cache : cacheList) { if (checked > 0 && cache.statusChecked == false) { handler.sendEmptyMessage(0); @@ -2119,11 +2128,11 @@ public class cgeocaches extends ListActivity { Log.i(cgSettings.tag, "Stopped exporting process."); break; } - + if (cache.logOffline) { cgLog log = app.loadLogOffline(cache.geocode); - + if (null != logTypes.get(log.type)) { fieldNoteBuffer.append(cache.geocode) @@ -2138,7 +2147,7 @@ public class cgeocaches extends ListActivity { } detailProgress++; - + handler.sendEmptyMessage(cacheList.indexOf(cache)); yield(); @@ -2146,15 +2155,15 @@ public class cgeocaches extends ListActivity { Log.e(cgSettings.tag, "cgeocaches.geocachesExportFieldNotes: " + e.toString()); } } - + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File exportLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/field-notes"); exportLocation.mkdirs(); - + SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); File exportFile = new File(exportLocation + "/" + fileNameDateFormat.format(new Date()) + ".txt"); - + OutputStream os = null; Writer fw = null; try diff --git a/src/cgeo/geocaching/sorting/AbstractCacheComparator.java b/src/cgeo/geocaching/sorting/AbstractCacheComparator.java new file mode 100644 index 0000000..4630888 --- /dev/null +++ b/src/cgeo/geocaching/sorting/AbstractCacheComparator.java @@ -0,0 +1,42 @@ +package cgeo.geocaching.sorting; + +import android.util.Log; +import cgeo.geocaching.cgCache; +import cgeo.geocaching.cgSettings; + +/** + * abstract super implementation for all cache comparators + * + */ +public abstract class AbstractCacheComparator implements CacheComparator { + + @Override + public final int compare(cgCache cache1, cgCache cache2) { + try { + // first check that we have all necessary data for the comparison + if (!canCompare(cache1, cache2)) { + return 0; + } + return compareCaches(cache1, cache2); + } catch (Exception e) { + Log.e(cgSettings.tag, "AbstractCacheComparator.compare: " + e.toString()); + } + return 0; + } + + /** + * check necessary preconditions (like missing fields) before running the comparison itself + * @param cache1 + * @param cache2 + * @return + */ + protected abstract boolean canCompare(final cgCache cache1, final cgCache cache2); + + /** + * compares two caches. Logging and exception handling is implemented outside this method already. + * @param cache1 + * @param cache2 + * @return an integer < 0 if cache1 is less than cache2, 0 if they are equal, and > 0 if cache1 is greater than cache2. + */ + protected abstract int compareCaches(final cgCache cache1, final cgCache cache2); +} diff --git a/src/cgeo/geocaching/sorting/CacheComparator.java b/src/cgeo/geocaching/sorting/CacheComparator.java new file mode 100644 index 0000000..c9b5150 --- /dev/null +++ b/src/cgeo/geocaching/sorting/CacheComparator.java @@ -0,0 +1,9 @@ +package cgeo.geocaching.sorting; + +import java.util.Comparator; + +import cgeo.geocaching.cgCache; + +public interface CacheComparator extends Comparator<cgCache> { + +} diff --git a/src/cgeo/geocaching/sorting/DifficultyComparator.java b/src/cgeo/geocaching/sorting/DifficultyComparator.java new file mode 100644 index 0000000..cb16b7c --- /dev/null +++ b/src/cgeo/geocaching/sorting/DifficultyComparator.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by difficulty + * + */ +public class DifficultyComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.difficulty != null && cache2.difficulty != null; + } + + @Override + protected int compareCaches(final cgCache cache1, final cgCache cache2) { + if (cache1.difficulty > cache2.difficulty) { + return 1; + } else if (cache2.difficulty > cache1.difficulty) { + return -1; + } + return 0; + } +}
\ No newline at end of file diff --git a/src/cgeo/geocaching/sorting/DistanceComparator.java b/src/cgeo/geocaching/sorting/DistanceComparator.java new file mode 100644 index 0000000..fc7b712 --- /dev/null +++ b/src/cgeo/geocaching/sorting/DistanceComparator.java @@ -0,0 +1,65 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgBase; +import cgeo.geocaching.cgCache; + +/** + * sorts caches by distance to current position + * + */ +public class DistanceComparator extends AbstractCacheComparator { + private Double latitude = null; + private Double longitude = null; + + public DistanceComparator() { + // nothing + } + + public DistanceComparator(Double latitudeIn, Double longitudeIn) { + setCoords(latitudeIn, longitudeIn); + } + + public void setCoords(Double latitudeIn, Double longitudeIn) { + latitude = latitudeIn; + longitude = longitudeIn; + } + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(final cgCache cache1, final cgCache cache2) { + if ((cache1.latitude == null || cache1.longitude == null + || cache2.latitude == null || cache2.longitude == null) + && cache1.distance != null && cache2.distance != null) { + if (cache1.distance < cache2.distance) { + return -1; + } else if (cache1.distance > cache2.distance) { + return 1; + } else { + return 0; + } + } else { + if (cache1.latitude == null || cache1.longitude == null) { + return 1; + } + if (cache2.latitude == null || cache2.longitude == null) { + return -1; + } + + Double distance1 = cgBase.getDistance(latitude, longitude, + cache1.latitude, cache1.longitude); + Double distance2 = cgBase.getDistance(latitude, longitude, + cache2.latitude, cache2.longitude); + + if (distance1 < distance2) { + return -1; + } else if (distance1 > distance2) { + return 1; + } + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/GeocodeComparator.java b/src/cgeo/geocaching/sorting/GeocodeComparator.java new file mode 100644 index 0000000..dd16f08 --- /dev/null +++ b/src/cgeo/geocaching/sorting/GeocodeComparator.java @@ -0,0 +1,26 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by GC code, therefore effectively sorting by cache age + * + */ +public class GeocodeComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.geocode != null && cache1.geocode.length() > 0 + && cache2.geocode != null && cache2.geocode.length() > 0; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.geocode.length() > cache2.geocode.length()) { + return 1; + } else if (cache2.geocode.length() > cache1.geocode.length()) { + return -1; + } + return cache1.geocode.compareToIgnoreCase(cache2.geocode); + } +} diff --git a/src/cgeo/geocaching/sorting/InventoryComparator.java b/src/cgeo/geocaching/sorting/InventoryComparator.java new file mode 100644 index 0000000..c126e41 --- /dev/null +++ b/src/cgeo/geocaching/sorting/InventoryComparator.java @@ -0,0 +1,28 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by number of items in inventory + * @author bananeweizen + * + */ +public class InventoryComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + int itemCount1 = cache1.inventoryItems; + int itemCount2 = cache2.inventoryItems; + if (itemCount1 < itemCount2) { + return 1; + } else if (itemCount2 < itemCount1) { + return -1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/NameComparator.java b/src/cgeo/geocaching/sorting/NameComparator.java new file mode 100644 index 0000000..f1c5ae3 --- /dev/null +++ b/src/cgeo/geocaching/sorting/NameComparator.java @@ -0,0 +1,20 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by name + * + */ +public class NameComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.name != null && cache2.name != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + return cache1.name.compareToIgnoreCase(cache2.name); + } +} diff --git a/src/cgeo/geocaching/sorting/PopularityComparator.java b/src/cgeo/geocaching/sorting/PopularityComparator.java new file mode 100644 index 0000000..cc1a6ea --- /dev/null +++ b/src/cgeo/geocaching/sorting/PopularityComparator.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by popularity (favorite count) + * + */ +public class PopularityComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.favouriteCnt != null && cache2.favouriteCnt != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.favouriteCnt < cache2.favouriteCnt) { + return 1; + } else if (cache2.favouriteCnt < cache1.favouriteCnt) { + return -1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/RatingComparator.java b/src/cgeo/geocaching/sorting/RatingComparator.java new file mode 100644 index 0000000..3cdf474 --- /dev/null +++ b/src/cgeo/geocaching/sorting/RatingComparator.java @@ -0,0 +1,36 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by gcvote.com rating + * + */ +public class RatingComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.rating != null && cache2.rating != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + Float rating1 = cache1.rating; + Float rating2 = cache2.rating; + + // voting can be disabled for caches, then assume an average rating instead + if (rating1 == 0.0) { + rating1 = 2.5f; + } + if (rating2 == 0.0) { + rating2 = 2.5f; + } + + if (rating1 < rating2) { + return 1; + } else if (rating2 < rating1) { + return -1; + } + return 0; + } +}
\ No newline at end of file diff --git a/src/cgeo/geocaching/sorting/SizeComparator.java b/src/cgeo/geocaching/sorting/SizeComparator.java new file mode 100644 index 0000000..f5bd643 --- /dev/null +++ b/src/cgeo/geocaching/sorting/SizeComparator.java @@ -0,0 +1,48 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by size + * + */ +public class SizeComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.size != null && cache1.size.length() > 0 && cache2.size != null && cache2.size.length() > 0; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + int size1 = getSize(cache1); + int size2 = getSize(cache2); + if (size1 < size2) { + return 1; + } else if (size2 < size1) { + return -1; + } + return 0; + } + + /** + * speed optimized comparison of size string + * @param cache + * @return + */ + private int getSize(final cgCache cache) { + char c = cache.size.charAt(0); + switch (c) { + case 'm': // micro + return 1; + case 's': // small + return 2; + case 'r': // regular + return 3; + case 'l': // large + return 4; + default: + return 0; + } + } +}
\ No newline at end of file diff --git a/src/cgeo/geocaching/sorting/TerrainComparator.java b/src/cgeo/geocaching/sorting/TerrainComparator.java new file mode 100644 index 0000000..bf66af9 --- /dev/null +++ b/src/cgeo/geocaching/sorting/TerrainComparator.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by terrain rating + * + */ +public class TerrainComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.terrain != null && cache2.terrain != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.terrain > cache2.terrain) { + return 1; + } else if (cache2.terrain > cache1.terrain) { + return -1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/VisitComparator.java b/src/cgeo/geocaching/sorting/VisitComparator.java new file mode 100644 index 0000000..79a8ac5 --- /dev/null +++ b/src/cgeo/geocaching/sorting/VisitComparator.java @@ -0,0 +1,26 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by last visited date + * + */ +public class VisitComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.visitedDate != null && cache1.visitedDate > 0 + && cache2.visitedDate != null && cache2.visitedDate > 0; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.visitedDate > cache2.visitedDate) { + return -1; + } else if (cache1.visitedDate < cache2.visitedDate) { + return 1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/VoteComparator.java b/src/cgeo/geocaching/sorting/VoteComparator.java new file mode 100644 index 0000000..0039c7c --- /dev/null +++ b/src/cgeo/geocaching/sorting/VoteComparator.java @@ -0,0 +1,39 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by the users own voting (if available at all) + * + * @author bananeweizen + * + */ +public class VoteComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + // if there is no vote available, put that cache at the end of the list + float vote1 = 0; + if (cache1.myVote != null) { + vote1 = cache1.myVote; + } + + float vote2 = 0; + if (cache2.myVote != null) { + vote2 = cache2.myVote; + } + + // compare + if (vote1 < vote2) { + return 1; + } else if (vote2 < vote1) { + return -1; + } + return 0; + } +} |
