diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2012-05-05 14:11:26 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2012-05-05 14:11:26 +0200 |
| commit | 20d2bdbc35df4617c4e8c950cb4df93b1a831149 (patch) | |
| tree | 6918b133a7a7505bbb6ea2b5324a5361ad3abbfb /main | |
| parent | 90264f45bd68721666c8e9e5ff4d82611c6fcf7b (diff) | |
| download | cgeo-20d2bdbc35df4617c4e8c950cb4df93b1a831149.zip cgeo-20d2bdbc35df4617c4e8c950cb4df93b1a831149.tar.gz cgeo-20d2bdbc35df4617c4e8c950cb4df93b1a831149.tar.bz2 | |
refactoring: remove more duplicated code from new popups
Diffstat (limited to 'main')
| -rw-r--r-- | main/src/cgeo/geocaching/AbstractPopupActivity.java | 297 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/CacheDetailActivity.java | 135 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/CachePopup.java | 172 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/WaypointPopup.java | 160 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/activity/AbstractActivity.java | 25 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypoint.java | 19 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypointadd.java | 14 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/maps/google/GoogleMapView.java | 9 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/CacheDetailsCreator.java | 132 |
9 files changed, 359 insertions, 604 deletions
diff --git a/main/src/cgeo/geocaching/AbstractPopupActivity.java b/main/src/cgeo/geocaching/AbstractPopupActivity.java index ad3ee80..07ddc2e 100644 --- a/main/src/cgeo/geocaching/AbstractPopupActivity.java +++ b/main/src/cgeo/geocaching/AbstractPopupActivity.java @@ -3,28 +3,33 @@ package cgeo.geocaching; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; import cgeo.geocaching.connector.gc.GCMap; +import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.gcvote.GCVote; import cgeo.geocaching.gcvote.GCVoteRating; +import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.HumanDistance; +import cgeo.geocaching.ui.CacheDetailsCreator; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; +import android.content.Intent; import android.graphics.Rect; +import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; +import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; +import android.widget.Button; import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.RelativeLayout; import android.widget.TextView; import java.util.Collections; @@ -37,162 +42,68 @@ public abstract class AbstractPopupActivity extends AbstractActivity { private static final int MENU_SHOW_IN_BROWSER = 7; protected static final String EXTRA_GEOCODE = "geocode"; - protected class UpdateLocation extends GeoObserver { - - @Override - protected void updateLocation(final IGeoData geo) { - try { - if (geo.getCoords() != null && cache != null && cache.getCoords() != null) { - cacheDistance.setText(HumanDistance.getHumanDistance(geo.getCoords().distanceTo(cache.getCoords()))); - cacheDistance.bringToFront(); - } - } catch (Exception e) { - Log.w("Failed to UpdateLocation location."); - } - } - } - - protected LayoutInflater inflater = null; protected cgCache cache = null; - protected TextView cacheDistance = null; - private GeoObserver geoUpdate = new UpdateLocation(); - protected String geocode = null; + protected CacheDetailsCreator details; + + private TextView cacheDistance = null; + private final GeoObserver geoUpdate = new UpdateLocation(); + private final int layout; - private Handler ratingHandler = new Handler() { + private final Handler ratingHandler = new Handler() { @Override public void handleMessage(Message msg) { try { - final Bundle data = msg.getData(); - - setRating(data.getFloat("rating"), data.getInt("votes")); + details.addRating(cache); } catch (Exception e) { // nothing } } }; - private int layout; - - public AbstractPopupActivity(String helpTopic, int layout) { - super(helpTopic); - this.layout = layout; - } - protected void aquireGCVote() { - if (Settings.isRatingWanted() && cache.supportsGCVote()) { - (new Thread() { + private final class UpdateLocation extends GeoObserver { - @Override - public void run() { - GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode); - - if (rating == null) { - return; - } - - Message msg = Message.obtain(); - Bundle bundle = new Bundle(); - bundle.putFloat("rating", rating.getRating()); - bundle.putInt("votes", rating.getVotes()); - msg.setData(bundle); - - ratingHandler.sendMessage(msg); + @Override + protected void updateLocation(final IGeoData geo) { + try { + if (geo.getCoords() != null && cache != null && cache.getCoords() != null) { + cacheDistance.setText(HumanDistance.getHumanDistance(geo.getCoords().distanceTo(cache.getCoords()))); + cacheDistance.bringToFront(); } - }).start(); + } catch (Exception e) { + Log.w("Failed to UpdateLocation location."); + } } } - protected RelativeLayout createCacheState() { - RelativeLayout itemLayout; - TextView itemName; - TextView itemValue; - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_status)); + public AbstractPopupActivity(String helpTopic, int layout) { + super(helpTopic); + this.layout = layout; + } - final StringBuilder state = new StringBuilder(); - if (cache.isFound()) { - if (state.length() > 0) { - state.append(", "); - } - state.append(res.getString(R.string.cache_status_found)); - } - if (cache.isArchived()) { - if (state.length() > 0) { - state.append(", "); - } - state.append(res.getString(R.string.cache_status_archived)); - } - if (cache.isDisabled()) { - if (state.length() > 0) { - state.append(", "); - } - state.append(res.getString(R.string.cache_status_disabled)); + private void aquireGCVote() { + if (!Settings.isRatingWanted()) { + return; } - if (cache.isPremiumMembersOnly()) { - if (state.length() > 0) { - state.append(", "); - } - state.append(res.getString(R.string.cache_status_premium)); + if (!cache.supportsGCVote()) { + return; } + (new Thread("Load GCVote") { - itemValue.setText(state.toString()); - return itemLayout; - } - - protected RelativeLayout createDifficulty() { - RelativeLayout itemLayout; - TextView itemName; - TextView itemValue; - LinearLayout itemStars; - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_layout, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - itemStars = (LinearLayout) itemLayout.findViewById(R.id.stars); - - itemName.setText(res.getString(R.string.cache_difficulty)); - itemValue.setText(String.format("%.1f", cache.getDifficulty()) + ' ' + res.getString(R.string.cache_rating_of) + " 5"); - for (int i = 0; i <= 4; i++) { - ImageView star = (ImageView) inflater.inflate(R.layout.star, null); - if ((cache.getDifficulty() - i) >= 1.0) { - star.setImageResource(R.drawable.star_on); - } else if ((cache.getDifficulty() - i) > 0.0) { - star.setImageResource(R.drawable.star_half); - } else { - star.setImageResource(R.drawable.star_off); - } - itemStars.addView(star); - } - return itemLayout; - } + @Override + public void run() { + final GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode); - protected RelativeLayout createTerrain() { - RelativeLayout itemLayout; - TextView itemName; - TextView itemValue; - LinearLayout itemStars; - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_layout, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - itemStars = (LinearLayout) itemLayout.findViewById(R.id.stars); - - itemName.setText(res.getString(R.string.cache_terrain)); - itemValue.setText(String.format("%.1f", cache.getTerrain()) + ' ' + res.getString(R.string.cache_rating_of) + " 5"); - for (int i = 0; i <= 4; i++) { - ImageView star = (ImageView) inflater.inflate(R.layout.star, null); - if ((cache.getTerrain() - i) >= 1.0) { - star.setImageResource(R.drawable.star_on); - } else if ((cache.getTerrain() - i) > 0.0) { - star.setImageResource(R.drawable.star_half); - } else { - star.setImageResource(R.drawable.star_off); + if (rating == null) { + return; + } + cache.setRating(rating.getRating()); + cache.setVotes(rating.getVotes()); + final Message msg = Message.obtain(); + ratingHandler.sendMessage(msg); } - itemStars.addView(star); - } - return itemLayout; + }).start(); } @Override @@ -214,19 +125,24 @@ public abstract class AbstractPopupActivity extends AbstractActivity { } if (CacheType.UNKNOWN == cache.getType()) { - SearchResult search = GCMap.searchByGeocodes(Collections.singleton(geocode)); + final SearchResult search = GCMap.searchByGeocodes(Collections.singleton(geocode)); cache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_ONLY); } - inflater = getLayoutInflater(); geocode = cache.getGeocode().toUpperCase(); - } - protected abstract void cachesAround(); + private final void logOffline(int menuItem) { + cache.logOffline(this, LogType.getById(menuItem - MENU_LOG_VISIT_OFFLINE)); + } - protected abstract void logOffline(int menuItem); + private final void logVisit() { + cache.logVisit(this); + finish(); + } - protected abstract void logVisit(); + private void showInBrowser() { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.getGeocode()))); + } protected abstract void navigateTo(); @@ -239,7 +155,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity { setTitle(res.getString(R.string.detail)); // get parameters - Bundle extras = getIntent().getExtras(); + final Bundle extras = getIntent().getExtras(); if (extras != null) { geocode = extras.getString(EXTRA_GEOCODE); } @@ -251,7 +167,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity { return; } - ImageView defaultNavigationImageView = (ImageView) findViewById(R.id.defaultNavigation); + final ImageView defaultNavigationImageView = (ImageView) findViewById(R.id.defaultNavigation); defaultNavigationImageView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { @@ -311,15 +227,10 @@ public abstract class AbstractPopupActivity extends AbstractActivity { super.onPrepareOptionsMenu(menu); try { - if (cache != null && cache.getCoords() != null) { - menu.findItem(MENU_DEFAULT_NAVIGATION).setVisible(true); - menu.findItem(MENU_NAVIGATION).setVisible(true); - menu.findItem(MENU_CACHES_AROUND).setVisible(true); - } else { - menu.findItem(MENU_DEFAULT_NAVIGATION).setVisible(false); - menu.findItem(MENU_NAVIGATION).setVisible(false); - menu.findItem(MENU_CACHES_AROUND).setVisible(false); - } + final boolean visible = getCoordinates() != null; + menu.findItem(MENU_DEFAULT_NAVIGATION).setVisible(visible); + menu.findItem(MENU_NAVIGATION).setVisible(visible); + menu.findItem(MENU_CACHES_AROUND).setVisible(visible); menu.findItem(MENU_LOG_VISIT).setEnabled(Settings.isLogin()); } catch (Exception e) { @@ -329,6 +240,8 @@ public abstract class AbstractPopupActivity extends AbstractActivity { return true; } + protected abstract Geopoint getCoordinates(); + @Override public void onResume() { super.onResume(); @@ -349,38 +262,62 @@ public abstract class AbstractPopupActivity extends AbstractActivity { return super.onTouchEvent(event); } - protected void setRating(float rating, int votes) { - if (rating <= 0) { - return; - } + protected abstract void showNavigationMenu(); - RelativeLayout itemLayout; - TextView itemName; - TextView itemValue; - LinearLayout itemStars; - LinearLayout detailsList = (LinearLayout) findViewById(R.id.details_list); - - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_layout, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - itemStars = (LinearLayout) itemLayout.findViewById(R.id.stars); - - itemName.setText(res.getString(R.string.cache_rating)); - itemValue.setText(String.format("%.1f", rating) + ' ' + res.getString(R.string.cache_rating_of) + " 5"); - itemStars.addView(createStarRating(rating, 5, this), 1); - - if (votes > 0) { - final TextView itemAddition = (TextView) itemLayout.findViewById(R.id.addition); - itemAddition.setText("(" + votes + ")"); - itemAddition.setVisibility(View.VISIBLE); + protected abstract void startDefaultNavigation2(); + + protected final void addCacheDetails() { + // cache type + final String cacheType = cache.getType().getL10n(); + final String cacheSize = cache.getSize() != CacheSize.UNKNOWN ? " (" + cache.getSize().getL10n() + ")" : ""; + details.add(R.string.cache_type, cacheType + cacheSize); + + details.add(R.string.cache_geocode, cache.getGeocode().toUpperCase()); + details.addCacheState(cache); + + // distance + details.add(R.string.cache_distance, "--"); + cacheDistance = details.getValueView(); + + details.addDifficulty(cache); + details.addTerrain(cache); + + // rating + if (cache.getRating() > 0) { + details.addRating(cache); + } else { + aquireGCVote(); } - detailsList.addView(itemLayout); - } - protected abstract void showInBrowser(); + // more details + final Button buttonMore = (Button) findViewById(R.id.more_details); + buttonMore.setOnClickListener(new OnClickListener() { - protected abstract void showNavigationMenu(); + @Override + public void onClick(View arg0) { + CacheDetailActivity.startActivity(AbstractPopupActivity.this, geocode.toUpperCase()); + finish(); + } + }); + } - protected abstract void startDefaultNavigation2(); + private final void cachesAround() { + final Geopoint coords = getCoordinates(); + if (coords == null) { + showToast(res.getString(R.string.err_location_unknown)); + return; + } + cgeocaches.startActivityCoordinates(this, coords); + finish(); + } + + /** + * @param view + * unused here but needed since this method is referenced from XML layout + */ + public final void goDefaultNavigation(View view) { + navigateTo(); + finish(); + } } diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 4fded6a..df16dff 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -19,6 +19,7 @@ import cgeo.geocaching.geopoint.HumanDistance; import cgeo.geocaching.geopoint.IConversion; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.ui.CacheDetailsCreator; import cgeo.geocaching.ui.DecryptTextClickListener; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.utils.BaseUtils; @@ -83,7 +84,6 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; -import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.TextView.BufferType; @@ -476,9 +476,7 @@ public class CacheDetailActivity extends AbstractActivity { 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); + cgeowaypointadd.startActivityEditWaypoint(this, waypoint.getId()); refreshOnResume = true; } break; @@ -1347,45 +1345,19 @@ public class CacheDetailActivity extends AbstractActivity { } detailsList = (LinearLayout) view.findViewById(R.id.details_list); + final CacheDetailsCreator details = new CacheDetailsCreator(CacheDetailActivity.this, detailsList); // cache name (full name) Spannable span = (new Spannable.Factory()).newSpannable(Html.fromHtml(cache.getName()).toString()); if (cache.isDisabled() || cache.isArchived()) { // strike span.setSpan(new StrikethroughSpan(), 0, span.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } - addCacheDetail(R.string.cache_name, span); - // cache type - addCacheDetail(R.string.cache_type, cache.getType().getL10n()); - - // size - if (null != cache.getSize() && cache.showSize()) { - addCacheDetail(R.string.cache_size, cache.getSize().getL10n()); - } - - // gc-code - addCacheDetail(R.string.cache_geocode, cache.getGeocode().toUpperCase()); - - // cache state - if (cache.isLogOffline() || cache.isArchived() || cache.isDisabled() || cache.isPremiumMembersOnly() || cache.isFound()) { - List<String> states = new ArrayList<String>(5); - if (cache.isLogOffline()) { - states.add(res.getString(R.string.cache_status_offline_log)); - } - if (cache.isFound()) { - states.add(res.getString(R.string.cache_status_found)); - } - if (cache.isArchived()) { - states.add(res.getString(R.string.cache_status_archived)); - } - if (cache.isDisabled()) { - states.add(res.getString(R.string.cache_status_disabled)); - } - if (cache.isPremiumMembersOnly()) { - states.add(res.getString(R.string.cache_status_premium)); - } - addCacheDetail(R.string.cache_status, StringUtils.join(states, ", ")); - } + details.add(R.string.cache_name, span); + details.add(R.string.cache_type, cache.getType().getL10n()); + details.addSize(cache); + details.add(R.string.cache_geocode, cache.getGeocode().toUpperCase()); + details.addCacheState(cache); // distance { @@ -1398,41 +1370,25 @@ public class CacheDetailActivity extends AbstractActivity { distance = currentCoords.distanceTo(cache); } } - cacheDistanceView = addCacheDetail(R.string.cache_distance, + cacheDistanceView = details.add(R.string.cache_distance, distance != null ? HumanDistance.getHumanDistance(distance) : "--"); } - // difficulty - if (cache.getDifficulty() > 0) { - addStarRating(R.string.cache_difficulty, cache.getDifficulty()); - } - - // terrain - if (cache.getTerrain() > 0) { - addStarRating(R.string.cache_terrain, cache.getTerrain()); - } - - // rating - if (cache.getRating() > 0) { - final RelativeLayout itemLayout = addStarRating(R.string.cache_rating, cache.getRating()); - if (cache.getVotes() > 0) { - final TextView itemAddition = (TextView) itemLayout.findViewById(R.id.addition); - itemAddition.setText("(" + cache.getVotes() + ")"); - itemAddition.setVisibility(View.VISIBLE); - } - } + details.addDifficulty(cache); + details.addTerrain(cache); + details.addRating(cache); // favourite count - addCacheDetail(R.string.cache_favourite, cache.getFavoritePoints() + "×"); + details.add(R.string.cache_favourite, cache.getFavoritePoints() + "×"); // own rating if (cache.getMyVote() > 0) { - addStarRating(R.string.cache_own_rating, cache.getMyVote()); + details.addStars(R.string.cache_own_rating, cache.getMyVote()); } // cache author if (StringUtils.isNotBlank(cache.getOwner()) || StringUtils.isNotBlank(cache.getOwnerReal())) { - TextView ownerView = addCacheDetail(R.string.cache_owner, ""); + TextView ownerView = details.add(R.string.cache_owner, ""); if (StringUtils.isNotBlank(cache.getOwner())) { ownerView.setText(Html.fromHtml(cache.getOwner()), TextView.BufferType.SPANNABLE); } else { // OwnerReal guaranteed to be not blank based on above @@ -1449,18 +1405,18 @@ public class CacheDetailActivity extends AbstractActivity { if (cache.isEventCache()) { dateString = DateUtils.formatDateTime(cgeoapplication.getInstance().getBaseContext(), time, DateUtils.FORMAT_SHOW_WEEKDAY) + ", " + dateString; } - addCacheDetail(cache.isEventCache() ? R.string.cache_event : R.string.cache_hidden, dateString); + details.add(cache.isEventCache() ? R.string.cache_event : R.string.cache_hidden, dateString); } } // cache location if (StringUtils.isNotBlank(cache.getLocation())) { - addCacheDetail(R.string.cache_location, cache.getLocation()); + details.add(R.string.cache_location, cache.getLocation()); } // cache coordinates if (cache.getCoords() != null) { - TextView valueView = addCacheDetail(R.string.cache_coordinates, cache.getCoords().toString()); + TextView valueView = details.add(R.string.cache_coordinates, cache.getCoords().toString()); valueView.setOnClickListener(new View.OnClickListener() { private int position = 0; private GeopointFormatter.Format[] availableFormats = new GeopointFormatter.Format[] { @@ -1514,29 +1470,6 @@ public class CacheDetailActivity extends AbstractActivity { return view; } - private TextView addCacheDetail(final int nameId, final CharSequence value) { - final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.cache_item, null); - ((TextView) layout.findViewById(R.id.name)).setText(res.getString(nameId)); - final TextView valueView = (TextView) layout.findViewById(R.id.value); - valueView.setText(value); - detailsList.addView(layout); - return valueView; - } - - private RelativeLayout addStarRating(final int nameId, final float value) { - final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.cache_layout, null); - TextView viewName = (TextView) layout.findViewById(R.id.name); - TextView viewValue = (TextView) layout.findViewById(R.id.value); - LinearLayout layoutStars = (LinearLayout) layout.findViewById(R.id.stars); - - viewName.setText(res.getString(nameId)); - viewValue.setText(String.format("%.1f", value) + ' ' + res.getString(R.string.cache_rating_of) + " 5"); - layoutStars.addView(createStarRating(value, 5, CacheDetailActivity.this), 1); - - detailsList.addView(layout); - return layout; - } - private class StoreCacheHandler extends CancellableHandler { @Override public void handleRegularMessage(Message msg) { @@ -2404,32 +2337,28 @@ public class CacheDetailActivity extends AbstractActivity { }); registerForContextMenu(waypointView); - waypointView.setOnClickListener(new WaypointInfoClickListener()); + waypointView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openContextMenu(view); + } + }); waypoints.addView(waypointView); } final Button addWaypoint = (Button) view.findViewById(R.id.add_waypoint); addWaypoint.setClickable(true); - addWaypoint.setOnClickListener(new AddWaypointClickListener()); - - return view; - } - - private class AddWaypointClickListener implements View.OnClickListener { + addWaypoint.setOnClickListener(new View.OnClickListener() { - 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; - } - } + @Override + public void onClick(View v) { + cgeowaypointadd.startActivityAddWaypoint(CacheDetailActivity.this, cache); + refreshOnResume = true; + } + }); - private class WaypointInfoClickListener implements View.OnClickListener { - public void onClick(View view) { - openContextMenu(view); - } + return view; } } diff --git a/main/src/cgeo/geocaching/CachePopup.java b/main/src/cgeo/geocaching/CachePopup.java index 8e563bf..f1eb8ad 100644 --- a/main/src/cgeo/geocaching/CachePopup.java +++ b/main/src/cgeo/geocaching/CachePopup.java @@ -1,8 +1,8 @@ package cgeo.geocaching; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; -import cgeo.geocaching.enumerations.CacheSize; -import cgeo.geocaching.enumerations.LogType; +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.ui.CacheDetailsCreator; import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.Log; @@ -12,20 +12,17 @@ import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; -import android.net.Uri; import android.os.Handler; import android.os.Message; import android.view.View; -import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; -import android.widget.RelativeLayout; import android.widget.TextView; public class CachePopup extends AbstractPopupActivity { private ProgressDialog storeDialog = null; private ProgressDialog dropDialog = null; - private CancellableHandler storeCacheHandler = new CancellableHandler() { + private final CancellableHandler storeCacheHandler = new CancellableHandler() { @Override public void handleRegularMessage(Message msg) { @@ -48,7 +45,7 @@ public class CachePopup extends AbstractPopupActivity { init(); } }; - private Handler dropCacheHandler = new Handler() { + private final Handler dropCacheHandler = new Handler() { @Override public void handleMessage(Message msg) { @@ -77,22 +74,6 @@ public class CachePopup extends AbstractPopupActivity { } @Override - protected void logOffline(final int menuItem) { - cache.logOffline(this, LogType.getById(menuItem - MENU_LOG_VISIT_OFFLINE)); - } - - @Override - protected void showInBrowser() { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.getGeocode()))); - } - - @Override - protected void logVisit() { - cache.logVisit(this); - finish(); - } - - @Override protected void showNavigationMenu() { NavigationAppFactory.showNavigationMenu(app.currentGeo(), this, cache, null, null); } @@ -101,91 +82,18 @@ public class CachePopup extends AbstractPopupActivity { protected void init() { super.init(); try { - RelativeLayout itemLayout; - TextView itemName; - TextView itemValue; - if (StringUtils.isNotBlank(cache.getName())) { setTitle(cache.getName()); } else { setTitle(geocode.toUpperCase()); } - LinearLayout detailsList = (LinearLayout) findViewById(R.id.details_list); - detailsList.removeAllViews(); - // actionbar icon ((TextView) findViewById(R.id.actionbar_title)).setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(cache.getType().markerId), null, null, null); - // cache type - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_type)); - - String cacheType = cache.getType().getL10n(); - String cacheSize = cache.getSize() != CacheSize.UNKNOWN ? " (" + cache.getSize().getL10n() + ")" : ""; - itemValue.setText(cacheType + cacheSize); - detailsList.addView(itemLayout); - - // gc-code - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_geocode)); - itemValue.setText(cache.getGeocode().toUpperCase()); - detailsList.addView(itemLayout); + details = new CacheDetailsCreator(this, (LinearLayout) findViewById(R.id.details_list)); - // cache state - if (cache.isArchived() || cache.isDisabled() || cache.isPremiumMembersOnly() || cache.isFound()) { - itemLayout = createCacheState(); - detailsList.addView(itemLayout); - } - - // distance - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_distance)); - itemValue.setText("--"); - detailsList.addView(itemLayout); - cacheDistance = itemValue; - - // difficulty - if (cache.getDifficulty() > 0f) { - itemLayout = createDifficulty(); - detailsList.addView(itemLayout); - } - - // terrain - if (cache.getTerrain() > 0f) { - itemLayout = createTerrain(); - detailsList.addView(itemLayout); - } - - // rating - if (cache.getRating() > 0) { - setRating(cache.getRating(), cache.getVotes()); - } else { - aquireGCVote(); - } - - // more details - Button buttonMore = (Button) findViewById(R.id.more_details); - buttonMore.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View arg0) { - Intent cachesIntent = new Intent(CachePopup.this, CacheDetailActivity.class); - cachesIntent.putExtra(EXTRA_GEOCODE, geocode.toUpperCase()); - startActivity(cachesIntent); - - finish(); - } - }); + addCacheDetails(); ((LinearLayout) findViewById(R.id.offline_box)).setVisibility(View.VISIBLE); @@ -195,7 +103,7 @@ public class CachePopup extends AbstractPopupActivity { final Button offlineStore = (Button) findViewById(R.id.offline_store); if (cache.getListId() > 0) { - long diff = (System.currentTimeMillis() / (60 * 1000)) - (cache.getDetailedUpdate() / (60 * 1000)); // minutes + final long diff = (System.currentTimeMillis() / (60 * 1000)) - (cache.getDetailedUpdate() / (60 * 1000)); // minutes String ago; if (diff < 15) { @@ -214,11 +122,11 @@ public class CachePopup extends AbstractPopupActivity { offlineRefresh.setVisibility(View.VISIBLE); offlineRefresh.setEnabled(true); - offlineRefresh.setOnClickListener(new storeCache()); + offlineRefresh.setOnClickListener(new StoreCache()); offlineStore.setText(res.getString(R.string.cache_offline_drop)); offlineStore.setEnabled(true); - offlineStore.setOnClickListener(new dropCache()); + offlineStore.setOnClickListener(new DropCache()); } else { offlineText.setText(res.getString(R.string.cache_offline_not_ready)); @@ -229,7 +137,7 @@ public class CachePopup extends AbstractPopupActivity { offlineStore.setText(res.getString(R.string.cache_offline_store)); offlineStore.setEnabled(true); - offlineStore.setOnClickListener(new storeCache()); + offlineStore.setOnClickListener(new StoreCache()); } } catch (Exception e) { Log.e("cgeopopup.init: " + e.toString()); @@ -243,29 +151,7 @@ public class CachePopup extends AbstractPopupActivity { init(); } - @Override - protected void navigateTo() { - if (cache == null || cache.getCoords() == null) { - showToast(res.getString(R.string.err_location_unknown)); - return; - } - - NavigationAppFactory.startDefaultNavigationApplication(app.currentGeo(), this, cache, null, null); - } - - @Override - protected void cachesAround() { - if (cache == null || cache.getCoords() == null) { - showToast(res.getString(R.string.err_location_unknown)); - return; - } - - cgeocaches.startActivityCoordinates(this, cache.getCoords()); - - finish(); - } - - private class storeCache implements View.OnClickListener { + private class StoreCache implements View.OnClickListener { @Override public void onClick(View arg0) { @@ -276,16 +162,16 @@ public class CachePopup extends AbstractPopupActivity { storeDialog = ProgressDialog.show(CachePopup.this, res.getString(R.string.cache_dialog_offline_save_title), res.getString(R.string.cache_dialog_offline_save_message), true); storeDialog.setCancelable(false); - Thread thread = new storeCacheThread(storeCacheHandler); + final Thread thread = new StoreCacheThread(storeCacheHandler); thread.start(); } } - private class storeCacheThread extends Thread { + private class StoreCacheThread extends Thread { final private CancellableHandler handler; - public storeCacheThread(final CancellableHandler handler) { + public StoreCacheThread(final CancellableHandler handler) { this.handler = handler; } @@ -296,7 +182,7 @@ public class CachePopup extends AbstractPopupActivity { } } - private class dropCache implements View.OnClickListener { + private class DropCache implements View.OnClickListener { @Override public void onClick(View arg0) { @@ -307,16 +193,15 @@ public class CachePopup extends AbstractPopupActivity { dropDialog = ProgressDialog.show(CachePopup.this, res.getString(R.string.cache_dialog_offline_drop_title), res.getString(R.string.cache_dialog_offline_drop_message), true); dropDialog.setCancelable(false); - Thread thread = new dropCacheThread(dropCacheHandler); + final Thread thread = new DropCacheThread(dropCacheHandler); thread.start(); } } - private class dropCacheThread extends Thread { + private class DropCacheThread extends Thread { + final private Handler handler; - private Handler handler = null; - - public dropCacheThread(Handler handlerIn) { + public DropCacheThread(Handler handlerIn) { handler = handlerIn; } @@ -326,17 +211,14 @@ public class CachePopup extends AbstractPopupActivity { } } - /** - * @param view - * unused here but needed since this method is referenced from XML layout - */ - public void goDefaultNavigation(View view) { + @Override + protected void navigateTo() { if (cache == null || cache.getCoords() == null) { - showToast(res.getString(R.string.cache_coordinates_no)); + showToast(res.getString(R.string.err_location_unknown)); return; } + NavigationAppFactory.startDefaultNavigationApplication(app.currentGeo(), this, cache, null, null); - finish(); } /** @@ -357,4 +239,12 @@ public class CachePopup extends AbstractPopupActivity { popupIntent.putExtra(EXTRA_GEOCODE, geocode); context.startActivity(popupIntent); } + + @Override + protected Geopoint getCoordinates() { + if (cache == null) { + return null; + } + return cache.getCoords(); + } } diff --git a/main/src/cgeo/geocaching/WaypointPopup.java b/main/src/cgeo/geocaching/WaypointPopup.java index 431dd15..b1e5b08 100644 --- a/main/src/cgeo/geocaching/WaypointPopup.java +++ b/main/src/cgeo/geocaching/WaypointPopup.java @@ -1,21 +1,19 @@ package cgeo.geocaching; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; -import cgeo.geocaching.enumerations.CacheSize; -import cgeo.geocaching.enumerations.LogType; +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.ui.CacheDetailsCreator; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; import android.content.Context; import android.content.Intent; -import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; -import android.widget.RelativeLayout; import android.widget.TextView; public class WaypointPopup extends AbstractPopupActivity { @@ -42,10 +40,6 @@ public class WaypointPopup extends AbstractPopupActivity { super.init(); waypoint = app.loadWaypoint(waypointId); try { - RelativeLayout itemLayout; - TextView itemName; - TextView itemValue; - if (StringUtils.isNotBlank(waypoint.getName())) { setTitle(waypoint.getName()); } else { @@ -56,114 +50,27 @@ public class WaypointPopup extends AbstractPopupActivity { ((TextView) findViewById(R.id.actionbar_title)).setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(waypoint.getWaypointType().markerId), null, null, null); //Start filling waypoint details - LinearLayout waypointDetailsList = (LinearLayout) findViewById(R.id.waypoint_details_list); - waypointDetailsList.removeAllViews(); + details = new CacheDetailsCreator(this, (LinearLayout) findViewById(R.id.waypoint_details_list)); //Waypoint geocode - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_geocode)); - - itemValue.setText(waypoint.getPrefix().toUpperCase() + waypoint.getGeocode().toUpperCase().substring(2)); - waypointDetailsList.addView(itemLayout); + details.add(R.string.cache_geocode, waypoint.getPrefix().toUpperCase() + waypoint.getGeocode().toUpperCase().substring(2)); // Edit Button - Button buttonEdit = (Button) findViewById(R.id.edit); + final Button buttonEdit = (Button) findViewById(R.id.edit); buttonEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { - Intent editIntent = new Intent(WaypointPopup.this, cgeowaypointadd.class); - editIntent.putExtra("waypoint", waypoint.getId()); - startActivity(editIntent); - restartActivity(); + cgeowaypointadd.startActivityEditWaypoint(WaypointPopup.this, waypoint.getId()); + finish(); } }); //Start filling cache details - LinearLayout cacheDetailsList = (LinearLayout) findViewById(R.id.details_list); - cacheDetailsList.removeAllViews(); - - // Cache name - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - itemValue.setLines(1); - itemName.setText(res.getString(R.string.cache_name)); - - itemValue.setText(cache.getName()); - cacheDetailsList.addView(itemLayout); - - // cache type - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - itemName.setText(res.getString(R.string.cache_type)); - - String cacheType = cache.getType().getL10n(); - String cacheSize = cache.getSize() != CacheSize.UNKNOWN ? " (" + cache.getSize().getL10n() + ")" : ""; - itemValue.setText(cacheType + cacheSize); - cacheDetailsList.addView(itemLayout); - - // gc-code - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_geocode)); - itemValue.setText(cache.getGeocode().toUpperCase()); - cacheDetailsList.addView(itemLayout); - - // cache state - if (cache.isArchived() || cache.isDisabled() || cache.isPremiumMembersOnly() || cache.isFound()) { - itemLayout = createCacheState(); - cacheDetailsList.addView(itemLayout); - - } - - // distance - itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null); - itemName = (TextView) itemLayout.findViewById(R.id.name); - itemValue = (TextView) itemLayout.findViewById(R.id.value); - - itemName.setText(res.getString(R.string.cache_distance)); - itemValue.setText("--"); - cacheDistance = itemValue; - cacheDetailsList.addView(itemLayout); - // difficulty - if (cache.getDifficulty() > 0f) { - itemLayout = createDifficulty(); - cacheDetailsList.addView(itemLayout); - } - - // terrain - if (cache.getTerrain() > 0f) { - itemLayout = createTerrain(); - cacheDetailsList.addView(itemLayout); - } - - // rating - if (cache.getRating() > 0) { - setRating(cache.getRating(), cache.getVotes()); - } else { - aquireGCVote(); - } + details = new CacheDetailsCreator(this, (LinearLayout) findViewById(R.id.details_list)); + details.add(R.string.cache_name, cache.getName()); - // more details - Button buttonMore = (Button) findViewById(R.id.more_details); - buttonMore.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View arg0) { - Intent cachesIntent = new Intent(WaypointPopup.this, CacheDetailActivity.class); - cachesIntent.putExtra(EXTRA_GEOCODE, geocode.toUpperCase()); - startActivity(cachesIntent); - - finish(); - } - }); + addCacheDetails(); } catch (Exception e) { Log.e("cgeopopup.init: " + e.toString()); @@ -180,31 +87,6 @@ public class WaypointPopup extends AbstractPopupActivity { NavigationAppFactory.startDefaultNavigationApplication(app.currentGeo(), this, null, waypoint, null); } - @Override - protected void cachesAround() { - if (waypoint == null || waypoint.getCoords() == null) { - showToast(res.getString(R.string.err_location_unknown)); - return; - } - - cgeocaches.startActivityCoordinates(this, waypoint.getCoords()); - - finish(); - } - - /** - * @param view - * unused here but needed since this method is referenced from XML layout - */ - public void goDefaultNavigation(final View view) { - if (waypoint == null || waypoint.getCoords() == null) { - showToast(res.getString(R.string.cache_coordinates_no)); - return; - } - NavigationAppFactory.startDefaultNavigationApplication(app.currentGeo(), this, null, waypoint, null); - finish(); - } - /** * Tries to navigate to the {@link cgCache} of this activity. */ @@ -226,23 +108,15 @@ public class WaypointPopup extends AbstractPopupActivity { } @Override - protected void logOffline(int menuItem) { - cache.logOffline(this, LogType.getById(menuItem - MENU_LOG_VISIT_OFFLINE)); - } - - @Override - protected void logVisit() { - cache.logVisit(this); - finish(); - } - - @Override - protected void showInBrowser() { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.getGeocode()))); + protected void showNavigationMenu() { + NavigationAppFactory.showNavigationMenu(app.currentGeo(), this, null, waypoint, null); } @Override - protected void showNavigationMenu() { - NavigationAppFactory.showNavigationMenu(app.currentGeo(), this, null, waypoint, null); + protected Geopoint getCoordinates() { + if (waypoint == null) { + return null; + } + return waypoint.getCoords(); } } diff --git a/main/src/cgeo/geocaching/activity/AbstractActivity.java b/main/src/cgeo/geocaching/activity/AbstractActivity.java index 8ddd21a..753705d 100644 --- a/main/src/cgeo/geocaching/activity/AbstractActivity.java +++ b/main/src/cgeo/geocaching/activity/AbstractActivity.java @@ -1,6 +1,5 @@ package cgeo.geocaching.activity; -import cgeo.geocaching.R; import cgeo.geocaching.Settings; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgeoapplication; @@ -8,16 +7,12 @@ import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.network.Cookies; import android.app.Activity; -import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.EditText; -import android.widget.ImageView; -import android.widget.LinearLayout; public abstract class AbstractActivity extends Activity implements IAbstractActivity { @@ -106,26 +101,6 @@ public abstract class AbstractActivity extends Activity implements IAbstractActi ActivityMixin.invalidateOptionsMenu(this); } - public static LinearLayout createStarRating(final float value, final int count, final Context context) { - LayoutInflater inflater = LayoutInflater.from(context); - LinearLayout starsContainer = new LinearLayout(context); - starsContainer.setOrientation(LinearLayout.HORIZONTAL); - - for (int i = 0; i < count; i++) { - ImageView star = (ImageView) inflater.inflate(R.layout.star, null); - if (value - i >= 0.75) { - star.setImageResource(R.drawable.star_on); - } else if (value - i >= 0.25) { - star.setImageResource(R.drawable.star_half); - } else { - star.setImageResource(R.drawable.star_off); - } - starsContainer.addView(star); - } - - return starsContainer; - } - /** * insert text into the EditText at the current cursor position * diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java index 61f44b9..4b22ad5 100644 --- a/main/src/cgeo/geocaching/cgeowaypoint.java +++ b/main/src/cgeo/geocaching/cgeowaypoint.java @@ -92,7 +92,13 @@ public class cgeowaypoint extends AbstractActivity implements IObserver<IGeoData } final Button buttonEdit = (Button) findViewById(R.id.edit); - buttonEdit.setOnClickListener(new editWaypointListener()); + buttonEdit.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + cgeowaypointadd.startActivityEditWaypoint(cgeowaypoint.this, id); + } + }); if (waypoint.isUserDefined()) { final Button buttonDelete = (Button) findViewById(R.id.delete); @@ -238,6 +244,7 @@ public class cgeowaypoint extends AbstractActivity implements IObserver<IGeoData private void cachesAround() { if (waypoint == null || waypoint.getCoords() == null) { showToast(res.getString(R.string.err_location_unknown)); + return; } cgeocaches.startActivityCoordinates(this, waypoint.getCoords()); @@ -248,6 +255,7 @@ public class cgeowaypoint extends AbstractActivity implements IObserver<IGeoData private void goToGeocache() { if (waypoint == null || waypoint.getGeocode() == null) { showToast(res.getString(R.string.err_waypoint_open_cache_failed)); + return; } CacheDetailActivity.startActivity(this, waypoint.getGeocode()); @@ -274,15 +282,6 @@ public class cgeowaypoint extends AbstractActivity implements IObserver<IGeoData // nothing } - private class editWaypointListener implements View.OnClickListener { - - public void onClick(View arg0) { - Intent editIntent = new Intent(cgeowaypoint.this, cgeowaypointadd.class); - editIntent.putExtra("waypoint", id); - startActivity(editIntent); - } - } - private class deleteWaypointListener implements View.OnClickListener { public void onClick(View arg0) { diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java index 9c02eef..7255745 100644 --- a/main/src/cgeo/geocaching/cgeowaypointadd.java +++ b/main/src/cgeo/geocaching/cgeowaypointadd.java @@ -15,6 +15,8 @@ import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; import android.app.ProgressDialog; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -423,4 +425,16 @@ public class cgeowaypointadd extends AbstractActivity implements IObserver<IGeoD ActivityMixin.goManual(this, "c:geo-waypoint-new"); } } + + public static void startActivityEditWaypoint(final Context context, final int waypointId) { + final Intent editIntent = new Intent(context, cgeowaypointadd.class); + editIntent.putExtra("waypoint", waypointId); + context.startActivity(editIntent); + } + + public static void startActivityAddWaypoint(final Context context, final cgCache cache) { + final Intent addWptIntent = new Intent(context, cgeowaypointadd.class); + addWptIntent.putExtra("geocode", cache.getGeocode()).putExtra("count", cache.getWaypoints().size()); + context.startActivity(addWptIntent); + } } diff --git a/main/src/cgeo/geocaching/maps/google/GoogleMapView.java b/main/src/cgeo/geocaching/maps/google/GoogleMapView.java index 8bf7deb..7552895 100644 --- a/main/src/cgeo/geocaching/maps/google/GoogleMapView.java +++ b/main/src/cgeo/geocaching/maps/google/GoogleMapView.java @@ -163,8 +163,13 @@ public class GoogleMapView extends MapView implements MapViewImpl { @Override public boolean onTouchEvent(MotionEvent ev) { - gestureDetector.onTouchEvent(ev); - return super.onTouchEvent(ev); + try { + gestureDetector.onTouchEvent(ev); + return super.onTouchEvent(ev); + } catch (Exception e) { + Log.e("GoogleMapView.onTouchEvent", e); + } + return false; } private class GestureListener extends SimpleOnGestureListener { diff --git a/main/src/cgeo/geocaching/ui/CacheDetailsCreator.java b/main/src/cgeo/geocaching/ui/CacheDetailsCreator.java new file mode 100644 index 0000000..e4a0fe5 --- /dev/null +++ b/main/src/cgeo/geocaching/ui/CacheDetailsCreator.java @@ -0,0 +1,132 @@ +package cgeo.geocaching.ui; + +import cgeo.geocaching.R; +import cgeo.geocaching.cgCache; + +import org.apache.commons.lang3.StringUtils; + +import android.app.Activity; +import android.content.res.Resources; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +public final class CacheDetailsCreator { + private final Activity activity; + private final ViewGroup parentView; + private TextView lastValueView; + private final Resources res; + + public CacheDetailsCreator(final Activity activity, final ViewGroup parentView) { + this.activity = activity; + this.res = activity.getResources(); + this.parentView = parentView; + parentView.removeAllViews(); + } + + public TextView add(final int nameId, final CharSequence value) { + final RelativeLayout layout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.cache_item, null); + final TextView nameView = (TextView) layout.findViewById(R.id.name); + nameView.setText(res.getString(nameId)); + lastValueView = (TextView) layout.findViewById(R.id.value); + lastValueView.setText(value); + parentView.addView(layout); + return lastValueView; + } + + public TextView getValueView() { + return lastValueView; + } + + public RelativeLayout addStars(final int nameId, final float value) { + final RelativeLayout layout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.cache_layout, null); + final TextView nameView = (TextView) layout.findViewById(R.id.name); + lastValueView = (TextView) layout.findViewById(R.id.value); + final LinearLayout layoutStars = (LinearLayout) layout.findViewById(R.id.stars); + + nameView.setText(activity.getResources().getString(nameId)); + lastValueView.setText(String.format("%.1f", value) + ' ' + activity.getResources().getString(R.string.cache_rating_of) + " 5"); + layoutStars.addView(createStarImages(value), 1); + + parentView.addView(layout); + return layout; + } + + private LinearLayout createStarImages(final float value) { + final LayoutInflater inflater = LayoutInflater.from(activity); + final LinearLayout starsContainer = new LinearLayout(activity); + starsContainer.setOrientation(LinearLayout.HORIZONTAL); + + for (int i = 0; i < 5; i++) { + ImageView star = (ImageView) inflater.inflate(R.layout.star, null); + if (value - i >= 0.75) { + star.setImageResource(R.drawable.star_on); + } else if (value - i >= 0.25) { + star.setImageResource(R.drawable.star_half); + } else { + star.setImageResource(R.drawable.star_off); + } + starsContainer.addView(star); + } + + return starsContainer; + } + + public void addCacheState(cgCache cache) { + if (cache.isLogOffline() || cache.isArchived() || cache.isDisabled() || cache.isPremiumMembersOnly() || cache.isFound()) { + final List<String> states = new ArrayList<String>(5); + if (cache.isLogOffline()) { + states.add(res.getString(R.string.cache_status_offline_log)); + } + if (cache.isFound()) { + states.add(res.getString(R.string.cache_status_found)); + } + if (cache.isArchived()) { + states.add(res.getString(R.string.cache_status_archived)); + } + if (cache.isDisabled()) { + states.add(res.getString(R.string.cache_status_disabled)); + } + if (cache.isPremiumMembersOnly()) { + states.add(res.getString(R.string.cache_status_premium)); + } + add(R.string.cache_status, StringUtils.join(states, ", ")); + } + } + + public void addRating(cgCache cache) { + if (cache.getRating() > 0) { + final RelativeLayout itemLayout = addStars(R.string.cache_rating, cache.getRating()); + if (cache.getVotes() > 0) { + final TextView itemAddition = (TextView) itemLayout.findViewById(R.id.addition); + itemAddition.setText("(" + cache.getVotes() + ")"); + itemAddition.setVisibility(View.VISIBLE); + } + } + } + + public void addSize(cgCache cache) { + if (null != cache.getSize() && cache.showSize()) { + add(R.string.cache_size, cache.getSize().getL10n()); + } + } + + public void addDifficulty(cgCache cache) { + if (cache.getDifficulty() > 0) { + addStars(R.string.cache_difficulty, cache.getDifficulty()); + } + } + + public void addTerrain(cgCache cache) { + if (cache.getTerrain() > 0) { + addStars(R.string.cache_terrain, cache.getTerrain()); + } + } +} |
