diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-04-22 16:27:11 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-04-22 16:36:54 +0200 |
| commit | efe2b931240f6c8353608d7d9e4af41f358b24ab (patch) | |
| tree | 5eb8abdf04cae7ee5b5f3f962cf7ed90eb8691ed | |
| parent | 561023f14be9c9dab2cfbd087c91b3c84176fc5a (diff) | |
| download | cgeo-efe2b931240f6c8353608d7d9e4af41f358b24ab.zip cgeo-efe2b931240f6c8353608d7d9e4af41f358b24ab.tar.gz cgeo-efe2b931240f6c8353608d7d9e4af41f358b24ab.tar.bz2 | |
Refactoring: remove isValidIndex() and most uses of hasWaypoints()
Most of the time, a loop on cgCache#getWaypoints() follows. This method
returns an empty collection, so the cgCache#hasWaypoints() test is
useless.
Also, checking for a valid index is useless as cgCache#getWaypoint() will
return null if the index is invalid.
| -rw-r--r-- | main/src/cgeo/geocaching/CacheDetailActivity.java | 160 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/StaticMapsProvider.java | 14 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 18 |
3 files changed, 87 insertions, 105 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 92df3be..6b5199c 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -488,15 +488,16 @@ public class CacheDetailActivity extends AbstractActivity { } break; - case CONTEXT_MENU_WAYPOINT_EDIT: - if (cache.hasWaypoints() && index < cache.getWaypoints().size()) { - final cgWaypoint waypoint = cache.getWaypoints().get(index); + case CONTEXT_MENU_WAYPOINT_EDIT: { + final cgWaypoint waypoint = cache.getWaypoint(index); + if (waypoint != null) { Intent editIntent = new Intent(this, cgeowaypointadd.class); editIntent.putExtra("waypoint", waypoint.getId()); startActivity(editIntent); refreshOnResume = true; } break; + } case CONTEXT_MENU_WAYPOINT_DUPLICATE: if (cache.duplicateWaypoint(index)) { app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); @@ -1079,7 +1080,7 @@ public class CacheDetailActivity extends AbstractActivity { } // show number of waypoints directly in waypoint title if (page == Page.WAYPOINTS) { - int waypointCount = (cache.hasWaypoints() ? cache.getWaypoints().size() : 0); + final int waypointCount = cache.getWaypoints().size(); return res.getQuantityString(R.plurals.waypoints, waypointCount, waypointCount); } return res.getString(page.titleStringId); @@ -2337,92 +2338,88 @@ public class CacheDetailActivity extends AbstractActivity { view = (ScrollView) getLayoutInflater().inflate(R.layout.cacheview_waypoints, null); - LinearLayout waypoints = (LinearLayout) view.findViewById(R.id.waypoints); + final LinearLayout waypoints = (LinearLayout) view.findViewById(R.id.waypoints); - if (cache.hasWaypoints()) { - LinearLayout waypointView; + // sort waypoints: PP, Sx, FI, OWN + final List<cgWaypoint> sortedWaypoints = new ArrayList<cgWaypoint>(cache.getWaypoints()); + Collections.sort(sortedWaypoints); - // sort waypoints: PP, Sx, FI, OWN - List<cgWaypoint> sortedWaypoints = new ArrayList<cgWaypoint>(cache.getWaypoints()); - Collections.sort(sortedWaypoints); + for (final cgWaypoint wpt : sortedWaypoints) { + final LinearLayout waypointView = (LinearLayout) getLayoutInflater().inflate(R.layout.waypoint_item, null); - for (final cgWaypoint wpt : sortedWaypoints) { - waypointView = (LinearLayout) getLayoutInflater().inflate(R.layout.waypoint_item, null); + // coordinates + if (null != wpt.getCoords()) { + final TextView coordinatesView = (TextView) waypointView.findViewById(R.id.coordinates); + coordinatesView.setText(wpt.getCoords().toString()); + coordinatesView.setVisibility(View.VISIBLE); + } - // coordinates - if (null != wpt.getCoords()) { - final TextView coordinatesView = (TextView) waypointView.findViewById(R.id.coordinates); - coordinatesView.setText(wpt.getCoords().toString()); - coordinatesView.setVisibility(View.VISIBLE); + // info + final List<String> infoTextList = new ArrayList<String>(3); + if (WaypointType.ALL_TYPES_EXCEPT_OWN.contains(wpt.getWaypointType())) { + infoTextList.add(wpt.getWaypointType().getL10n()); + } + if (cgWaypoint.PREFIX_OWN.equalsIgnoreCase(wpt.getPrefix())) { + infoTextList.add(res.getString(R.string.waypoint_custom)); + } else { + if (StringUtils.isNotBlank(wpt.getPrefix())) { + infoTextList.add(wpt.getPrefix()); } - - // info - final List<String> infoTextList = new ArrayList<String>(3); - if (WaypointType.ALL_TYPES_EXCEPT_OWN.contains(wpt.getWaypointType())) { - infoTextList.add(wpt.getWaypointType().getL10n()); + if (StringUtils.isNotBlank(wpt.getLookup())) { + infoTextList.add(wpt.getLookup()); } - if (cgWaypoint.PREFIX_OWN.equalsIgnoreCase(wpt.getPrefix())) { - infoTextList.add(res.getString(R.string.waypoint_custom)); - } else { - if (StringUtils.isNotBlank(wpt.getPrefix())) { - infoTextList.add(wpt.getPrefix()); - } - if (StringUtils.isNotBlank(wpt.getLookup())) { - infoTextList.add(wpt.getLookup()); - } + } + if (CollectionUtils.isNotEmpty(infoTextList)) { + final TextView infoView = (TextView) waypointView.findViewById(R.id.info); + infoView.setText(StringUtils.join(infoTextList, Formatter.SEPARATOR)); + infoView.setVisibility(View.VISIBLE); + } + + // title + final TextView nameView = (TextView) waypointView.findViewById(R.id.name); + if (StringUtils.isNotBlank(wpt.getName())) { + nameView.setText(StringEscapeUtils.unescapeHtml4(wpt.getName())); + } else if (null != wpt.getCoords()) { + nameView.setText(wpt.getCoords().toString()); + } else { + nameView.setText(res.getString(R.string.waypoint)); + } + wpt.setIcon(res, nameView); + + // note + if (StringUtils.isNotBlank(wpt.getNote())) { + final TextView noteView = (TextView) waypointView.findViewById(R.id.note); + noteView.setVisibility(View.VISIBLE); + if (BaseUtils.containsHtml(wpt.getNote())) { + noteView.setText(Html.fromHtml(wpt.getNote()), TextView.BufferType.SPANNABLE); } - if (CollectionUtils.isNotEmpty(infoTextList)) { - final TextView infoView = (TextView) waypointView.findViewById(R.id.info); - infoView.setText(StringUtils.join(infoTextList, Formatter.SEPARATOR)); - infoView.setVisibility(View.VISIBLE); + else { + noteView.setText(wpt.getNote()); } + } - // title - TextView nameView = (TextView) waypointView.findViewById(R.id.name); - if (StringUtils.isNotBlank(wpt.getName())) { - nameView.setText(StringEscapeUtils.unescapeHtml4(wpt.getName())); - } else if (null != wpt.getCoords()) { - nameView.setText(wpt.getCoords().toString()); - } else { - nameView.setText(res.getString(R.string.waypoint)); + final View wpNavView = waypointView.findViewById(R.id.wpDefaultNavigation); + wpNavView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + NavigationAppFactory.startDefaultNavigationApplication(geolocation, CacheDetailActivity.this, null, wpt, null); } - wpt.setIcon(res, nameView); - - // note - if (StringUtils.isNotBlank(wpt.getNote())) { - final TextView noteView = (TextView) waypointView.findViewById(R.id.note); - noteView.setVisibility(View.VISIBLE); - if (BaseUtils.containsHtml(wpt.getNote())) { - noteView.setText(Html.fromHtml(wpt.getNote()), TextView.BufferType.SPANNABLE); - } - else { - noteView.setText(wpt.getNote()); - } + }); + wpNavView.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + NavigationAppFactory.startDefaultNavigationApplication2(geolocation, CacheDetailActivity.this, null, wpt, null); + return true; } + }); - View wpNavView = waypointView.findViewById(R.id.wpDefaultNavigation); - wpNavView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - NavigationAppFactory.startDefaultNavigationApplication(geolocation, CacheDetailActivity.this, null, wpt, null); - } - }); - wpNavView.setOnLongClickListener(new View.OnLongClickListener() { - @Override - public boolean onLongClick(View v) { - NavigationAppFactory.startDefaultNavigationApplication2(geolocation, CacheDetailActivity.this, null, wpt, null); - return true; - } - }); - - registerForContextMenu(waypointView); - waypointView.setOnClickListener(new WaypointInfoClickListener()); + registerForContextMenu(waypointView); + waypointView.setOnClickListener(new WaypointInfoClickListener()); - waypoints.addView(waypointView); - } + waypoints.addView(waypointView); } - Button addWaypoint = (Button) view.findViewById(R.id.add_waypoint); + final Button addWaypoint = (Button) view.findViewById(R.id.add_waypoint); addWaypoint.setClickable(true); addWaypoint.setOnClickListener(new AddWaypointClickListener()); @@ -2431,16 +2428,9 @@ public class CacheDetailActivity extends AbstractActivity { private class AddWaypointClickListener implements View.OnClickListener { - public void onClick(View view) { - Intent addWptIntent = new Intent(CacheDetailActivity.this, cgeowaypointadd.class); - - addWptIntent.putExtra("geocode", cache.getGeocode()); - int wpCount = 0; - if (cache.hasWaypoints()) { - wpCount = cache.getWaypoints().size(); - } - addWptIntent.putExtra("count", wpCount); - + public void onClick(final View view) { + final Intent addWptIntent = new Intent(CacheDetailActivity.this, cgeowaypointadd.class); + addWptIntent.putExtra("geocode", cache.getGeocode()).putExtra("count", cache.getWaypoints().size()); startActivity(addWptIntent); refreshOnResume = true; } diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index fc55949..7cdf18e 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -113,17 +113,15 @@ public class StaticMapsProvider { storeCacheStaticMap(cache, edge, waitForResult); } - private static void storeCacheStaticMap(cgCache cache, int edge, final boolean waitForResult) { + private static void storeCacheStaticMap(final cgCache cache, final int edge, final boolean waitForResult) { final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); final Parameters waypoints = new Parameters(); - if (cache.hasWaypoints()) { - for (cgWaypoint waypoint : cache.getWaypoints()) { - if (waypoint.getCoords() == null) { - continue; - } - final String wpMarkerUrl = getWpMarkerUrl(waypoint); - waypoints.put("markers", "icon:" + wpMarkerUrl + "|" + waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA)); + for (final cgWaypoint waypoint : cache.getWaypoints()) { + if (waypoint.getCoords() == null) { + continue; } + final String wpMarkerUrl = getWpMarkerUrl(waypoint); + waypoints.put("markers", "icon:" + wpMarkerUrl + "|" + waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA)); } // download map images in separate background thread for higher performance final String cacheMarkerUrl = getCacheMarkerUrl(cache); diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 6ec1e6b..3dd997d 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -1177,20 +1177,17 @@ public class cgCache implements ICache, IWaypoint { * @return <code>true</code> if the waypoint was duplicated, <code>false</code> otherwise (invalid index) */ public boolean duplicateWaypoint(final int index) { - if (!isValidWaypointIndex(index)) { + final cgWaypoint original = getWaypoint(index); + if (original == null) { return false; } - final cgWaypoint copy = new cgWaypoint(waypoints.get(index)); + final cgWaypoint copy = new cgWaypoint(original); copy.setUserDefined(); copy.setName(cgeoapplication.getInstance().getString(R.string.waypoint_copy_of) + " " + copy.getName()); waypoints.add(index + 1, copy); return cgeoapplication.getInstance().saveOwnWaypoint(-1, geocode, copy); } - private boolean isValidWaypointIndex(final int index) { - return hasWaypoints() && index >= 0 && index < waypoints.size(); - } - /** * delete a user defined waypoint * @@ -1199,10 +1196,10 @@ public class cgCache implements ICache, IWaypoint { * @return <code>true</code>, if the waypoint was deleted */ public boolean deleteWaypoint(final int index) { - if (!isValidWaypointIndex(index)) { + final cgWaypoint waypoint = getWaypoint(index); + if (waypoint == null) { return false; } - final cgWaypoint waypoint = waypoints.get(index); if (waypoint.isUserDefined()) { waypoints.remove(index); cgeoapplication.getInstance().deleteWaypoint(waypoint.getId()); @@ -1256,10 +1253,7 @@ public class cgCache implements ICache, IWaypoint { * @return waypoint or <code>null</code> if index is out of range */ public cgWaypoint getWaypoint(final int index) { - if (!isValidWaypointIndex(index)) { - return null; - } - return waypoints.get(index); + return waypoints != null && index >= 0 && index < waypoints.size() ? waypoints.get(index) : null; } /** |
