diff --git a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyFavoriteFragment.java b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyFavoriteFragment.java new file mode 100644 index 0000000000..3949608194 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyFavoriteFragment.java @@ -0,0 +1,64 @@ +package net.osmand.plus.sherpafy; + +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; + +import net.osmand.plus.R; +import net.osmand.plus.sherpafy.TourInformation.StageFavorite; +import android.app.Activity; +import android.view.View; +import android.webkit.WebView; +import android.widget.ImageView; +import android.widget.TextView; + +public class SherpafyFavoriteFragment extends SherpafyStageInfoFragment { + private static final int SHOW_ON_MAP = 10; + public static final String FAV_PARAM = null; + private StageFavorite fav; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + int k = getArguments().getInt(FAV_PARAM); + if(stage != null) { + fav = (StageFavorite) stage.getFavorites().get(k); + getSherlockActivity().getSupportActionBar().setTitle(fav.getName()); + } + } + + protected void updateView(WebView description, ImageView icon, TextView additional, TextView text, TextView header) { + if (fav.getImage() != null) { + icon.setImageBitmap(fav.getImage()); + } else { + icon.setVisibility(View.GONE); + } + additional.setVisibility(View.GONE); + header.setText(fav.getName()); + text.setText(fav.getShortDescription()); + description.loadData("" + fav.getFullDescription() + " k) { + stage = tour.getStageInformation().get(k); + } + setHasOptionsMenu(true); + } + + @Override + public void onResume() { + super.onResume(); + if(tour != null) { + getSherlockActivity().getSupportActionBar().setTitle(tour.getName()); + } + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + Object item = getListAdapter().getItem(position); + if (item instanceof StageFavorite) { + ((TourViewActivity) getSherlockActivity()).showFavoriteFragment(stage, (StageFavorite) item); + } + } + + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + ((TourViewActivity) getSherlockActivity()).showSelectedItem(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View v = super.onCreateView(inflater, container, savedInstanceState); + imageView = new ImageView(getActivity()); + imageView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); + imageView.setScaleType(ScaleType.CENTER_CROP); + return v; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + favAdapter = new FavoriteAdapter(stage.getFavorites()); + setListAdapter(favAdapter); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + setListAdapter(null); + } + + class FavoriteAdapter extends ArrayAdapter { + + public FavoriteAdapter(List list) { + super(getActivity(), R.layout.sherpafy_stage_list_item, list); + } + + @Override + public View getView(final int position, View convertView, ViewGroup parent) { + View row = convertView; + if (row == null) { + LayoutInflater inflater = getActivity().getLayoutInflater(); + row = inflater.inflate(R.layout.sherpafy_stage_list_item, parent, false); + } + Object ti = getItem(position); + TextView header = (TextView) row.findViewById(R.id.HeaderText); + ImageView img = (ImageView) row.findViewById(R.id.Icon); + TextView text = (TextView) row.findViewById(R.id.Text); + TextView addtext = (TextView) row.findViewById(R.id.AdditionalText); + + if (ti instanceof StageFavoriteGroup) { + addtext.setText(""); + text.setTextColor(((StageFavoriteGroup)ti).getColor()); + text.setText(((StageFavoriteGroup)ti).getName()); + header.setVisibility(View.GONE); + img.setVisibility(View.GONE); + img.setImageDrawable(null); + } else if(ti instanceof StageFavorite){ + StageFavorite sf = ((StageFavorite)ti); + if(stage.startPoint != null && sf.location != null) { + double d = MapUtils.getDistance(stage.startPoint, sf.location); + addtext.setText(OsmAndFormatter.getFormattedDistance((float) d, getMyApplication())); + } else { + addtext.setText(""); + } + header.setVisibility(View.VISIBLE); + header.setText(sf.getName()); + text.setTextColor(StageImageDrawable.MENU_COLOR); + text.setText(sf.getShortDescription()); + img.setVisibility(View.VISIBLE); + img.setImageDrawable(new StageImageDrawable(getActivity(), sf.getGroup().getColor(), sf + .getName().substring(0, 1), 0)); + + } + return row; + } + } + + private OsmandApplication getMyApplication() { + return (OsmandApplication) getActivity().getApplication(); + } + +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageFragment.java b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageFragment.java index 8bfcc6eb2e..ccd296bf6b 100644 --- a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageFragment.java +++ b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.sherpafy; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -9,6 +10,7 @@ import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.sherpafy.TourInformation.StageFavoriteGroup; import net.osmand.plus.sherpafy.TourInformation.StageInformation; import android.app.Activity; import android.content.Context; @@ -18,7 +20,7 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.text.Html; @@ -72,8 +74,8 @@ public class SherpafyStageFragment extends SherlockFragment { int k = getArguments().getInt(STAGE_PARAM); if(tour != null && tour.getStageInformation().size() > k) { stage = tour.getStageInformation().get(k); - getSherlockActivity().getSupportActionBar().setTitle(getString(R.string.tab_stage) + " " + (k+1)); } + getSherlockActivity().getSupportActionBar().setTitle(getString(R.string.tab_stage) + " " + (k+1)); } @@ -111,18 +113,49 @@ public class SherpafyStageFragment extends SherlockFragment { tabHost.setup(); ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager); - mTabsAdapter = new TabsAdapter(getSherlockActivity(), tabHost, mViewPager); - mTabsAdapter.addTab(tabHost.newTabSpec("INFO").setIndicator(getString(R.string.sherpafy_stage_tab_info)), - SherpafyStageInfoFragment.class, null); - mTabsAdapter.addTab(tabHost.newTabSpec("ROUTE").setIndicator(getString(R.string.sherpafy_stage_tab_route)), - SherpafyStageItineraryFragment.class, null); - mTabsAdapter.addTab(tabHost.newTabSpec("FAV").setIndicator(getString(R.string.sherpafy_stage_tab_fav)), - SherpafyStageInfoFragment.class, null); - mTabsAdapter.addTab(tabHost.newTabSpec("TARGET").setIndicator(getString(R.string.sherpafy_stage_tab_target)), - SherpafyStageItineraryFragment.class, null); + + mTabsAdapter = new TabsAdapter(getChildFragmentManager(), getSherlockActivity(), tabHost, mViewPager, stage); + if (stage != null) { + mTabsAdapter.addTab(tabHost.newTabSpec("INFO").setIndicator(getString(R.string.sherpafy_stage_tab_info)), + SherpafyStageInfoFragment.class); + if (!stage.getItinerary().equals("")) { + mTabsAdapter.addTab( + tabHost.newTabSpec("ROUTE").setIndicator(getString(R.string.sherpafy_stage_tab_route)), + SherpafyStageItineraryFragment.class); + } + if (stage.getFavorites().size() > 0) { + mTabsAdapter.addTab(tabHost.newTabSpec("FAV").setIndicator(getString(R.string.sherpafy_stage_tab_fav)), + SherpafyFavoritesListFragment.class); + } + StageFavoriteGroup group = stage.getGroupById("destination"); + if (group != null && group.getFavorites().size() > 0) { + int o = group.getFavorites().get(0).getOrder(); + Bundle bl = new Bundle(); + bl.putInt(SherpafyFavoriteFragment.FAV_PARAM, o); + mTabsAdapter.addTab( + tabHost.newTabSpec("TARGET").setIndicator(getString(R.string.sherpafy_stage_tab_target)), + SherpafyFavoriteFragment.class, bl); + } + } return view; } + @Override + public void onDetach() { + super.onDetach(); + + try { + Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager"); + childFragmentManager.setAccessible(true); + childFragmentManager.set(this, null); + + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + ///////// private ImageGetter getImageGetter(final View v) { return new Html.ImageGetter() { @@ -222,6 +255,7 @@ public class SherpafyStageFragment extends SherlockFragment { private final TabHost mTabHost; private final ViewPager mViewPager; private final ArrayList mTabs = new ArrayList(); + private StageInformation stage; static final class TabInfo { private final String tag; @@ -251,19 +285,27 @@ public class SherpafyStageFragment extends SherlockFragment { } } - public TabsAdapter(FragmentActivity activity, TabHost tabHost,ViewPager pager) { - super(activity.getSupportFragmentManager()); - mContext = activity; + public TabsAdapter(FragmentManager fm, Context ui, TabHost tabHost, ViewPager pager, + StageInformation stage) { + super(fm); + mContext = ui; mTabHost = tabHost; mViewPager = pager; + this.stage = stage; mTabHost.setOnTabChangedListener(this); mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); } + public TabSpec addTab(TabHost.TabSpec tabSpec, Class clss) { + return addTab(tabSpec, clss, new Bundle()); + } + public TabSpec addTab(TabHost.TabSpec tabSpec, Class clss, Bundle args) { tabSpec.setContent(new DummyTabFactory(mContext)); String tag = tabSpec.getTag(); + args.putInt(STAGE_PARAM, stage.getOrder()); + args.putString(TOUR_PARAM, stage.getTour().getId()); TabInfo info = new TabInfo(tag, clss, args); mTabs.add(info); @@ -293,6 +335,7 @@ public class SherpafyStageFragment extends SherlockFragment { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } + @Override public void onPageSelected(int position) { diff --git a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageInfoFragment.java b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageInfoFragment.java index 08162eb503..29d4590fa2 100644 --- a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageInfoFragment.java +++ b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageInfoFragment.java @@ -22,7 +22,7 @@ public class SherpafyStageInfoFragment extends SherlockFragment { OsmandApplication app; private SherpafyCustomization customization; protected StageInformation stage; - private TourInformation tour; + protected TourInformation tour; private View view; public SherpafyStageInfoFragment() { @@ -40,7 +40,6 @@ public class SherpafyStageInfoFragment extends SherlockFragment { for(TourInformation ti : customization.getTourInformations()) { if(ti.getId().equals(id)) { tour = ti; - getSherlockActivity().getSupportActionBar().setTitle(tour.getName()); break; } } @@ -60,7 +59,6 @@ public class SherpafyStageInfoFragment extends SherlockFragment { TextView additional = (TextView) view.findViewById(R.id.AdditionalText); TextView text = (TextView) view.findViewById(R.id.Text); TextView header = (TextView) view.findViewById(R.id.HeaderText); - updateView(description, icon, additional, text, header); return view; } diff --git a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageItineraryFragment.java b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageItineraryFragment.java index e8ef9077a8..6c98e2ac1c 100644 --- a/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageItineraryFragment.java +++ b/OsmAnd/src/net/osmand/plus/sherpafy/SherpafyStageItineraryFragment.java @@ -8,14 +8,14 @@ import android.widget.TextView; public class SherpafyStageItineraryFragment extends SherpafyStageInfoFragment { protected void updateView(WebView description, ImageView icon, TextView additional, TextView text, TextView header) { - if (stage.getItineraryBitmap() != null) { - icon.setImageBitmap(stage.getItineraryBitmap()); - } else { - icon.setVisibility(View.GONE); - } - additional.setVisibility(View.GONE); - header.setVisibility(View.GONE); - description.loadData("" + stage.getItinerary() - + "" + stage.getItinerary() + " stageInformation = new ArrayList(); - public TourInformation(File f) { this.folder = f; this.name = f.getName().replace('_', ' '); @@ -47,6 +48,18 @@ public class TourInformation { return instructions; } + private static WeakHashMap androidBitmaps = new WeakHashMap(); + private static Bitmap decodeImage(File f) { + if(!androidBitmaps.containsKey(f)) { + Bitmap img =null; + if(f != null && f.exists()) { + img = BitmapFactory.decodeFile(f.getAbsolutePath()); + } + androidBitmaps.put(f, img); + } + return androidBitmaps.get(f); + } + private static Reader getUTF8Reader(InputStream f) throws IOException { BufferedInputStream bis = new BufferedInputStream(f); @@ -71,6 +84,8 @@ public class TourInformation { int tok; String text = ""; StageInformation stage = null; + StageFavoriteGroup group = null; + StageFavorite favorite = null; stageInformation.clear(); while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { if (tok == XmlPullParser.START_TAG) { @@ -84,16 +99,39 @@ public class TourInformation { stage.name = name; } else if (tag.equals("itinerary") && stage != null){ String img = getDefAttribute(parser, "image", ""); + stage.distance = Double.parseDouble(getDefAttribute(parser, "distance", "0")); + stage.duration = Integer.parseInt(getDefAttribute(parser, "duration", "0")); + double slat = Double.parseDouble(getDefAttribute(parser, "startLat", "0")); + double slon = Double.parseDouble(getDefAttribute(parser, "startLon", "0")); + if(slat != 0 || slon != 0) { + stage.startPoint = new LatLon(slat, slon); + } if(img.startsWith(FILE_PREFIX)) { stage.itineraryFile = getFile(img); } stage.itinerary = getInnerXml(parser); + } else if(stage != null && tag.equals("group")) { + group = new StageFavoriteGroup(); + group.color = Algorithms.parseColor(getDefAttribute(parser, "color", Algorithms.colorToString(StageImageDrawable.INFO_COLOR))); + group.name = getDefAttribute(parser, "name", ""); + group.id = getDefAttribute(parser, "id", ""); + group.order = stage.favorites.size(); + stage.favorites.add(group); + } else if(group != null && tag.equals("favorite")) { + favorite = new StageFavorite(); + favorite.location = new LatLon(Double.parseDouble(getDefAttribute(parser, "lat", "0")), + Double.parseDouble(getDefAttribute(parser, "lon", "0"))); + favorite.name = getDefAttribute(parser, "name", ""); + favorite.group = group; + favorite.order = stage.favorites.size(); + group.favorites.add(favorite); + stage.favorites.add(favorite); } else if (tag.equals("fullDescription")){ fulldescription = getInnerXml(parser); } else if (tag.equals("instructions")){ instructions = getInnerXml(parser); - } else if (stage != null && tag.equals("interval")){ - stage.distance = Double.parseDouble(getDefAttribute(parser, "distance", "0")); + } else if (favorite != null && tag.equals("description")){ + favorite.fullDescription = getInnerXml(parser); } else if (stage != null && tag.equals("description")){ stage.fullDescription = getInnerXml(parser); } @@ -104,12 +142,18 @@ public class TourInformation { if(tag.equals("stage")) { stageInformation.add(stage); stage = null; - } else if(stage != null && tag.equals("fullDescription")) { - stage.fullDescription = text; + } else if(favorite != null && tag.equals("defaultImage")) { + if(text.startsWith(FILE_PREFIX)) { + favorite.imgFile = getFile(text); + } } else if(stage != null && tag.equals("defaultImage")) { if(text.startsWith(FILE_PREFIX)) { stage.imgFile = getFile(text); } + } else if(stage != null && tag.equals("group")) { + group = null; + } else if(stage != null && tag.equals("favorite")) { + favorite = null; } else if(tag.equals("defaultImage")) { if(text.startsWith(FILE_PREFIX)) { imgFile = getFile(text); @@ -119,7 +163,9 @@ public class TourInformation { stage.gpxFile = getFile(text); } } else if(tag.equals("shortDescription")) { - if(stage != null) { + if(favorite != null) { + favorite.shortDescription = text; + } else if(stage != null) { stage.shortDescription = text; } else { shortDescription = text; @@ -168,10 +214,7 @@ public class TourInformation { public Bitmap getImageBitmap() { - if(defaultImg == null && imgFile != null && imgFile.exists()) { - defaultImg = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); - } - return defaultImg; + return decodeImage(imgFile); } //returns image bitmap from selected relative path @@ -189,21 +232,104 @@ public class TourInformation { } - public static class StageInformation { + public static class StageFavoriteGroup { + String id; + int order; + int color; + String name = ""; + List favorites = new ArrayList(); - String itinerary = ""; - File gpxFile; - public GPXFile gpx; + public int getColor() { + return color; + } + + public String getName() { + return name; + } + + public List getFavorites() { + return favorites; + } + + public int getOrder() { + return order; + } + } + + public static class StageFavorite { + int order; + LatLon location; + String name = ""; + String shortDescription = ""; + String fullDescription = ""; + File imgFile; + StageFavoriteGroup group; + + public StageFavoriteGroup getGroup() { + return group; + } + + public int getOrder() { + return order; + } + + public LatLon getLatLon() { + return location; + } + + public String getName() { + return name; + } + + public String getShortDescription() { + return shortDescription; + } + + public String getFullDescription() { + return fullDescription; + } + + public Bitmap getImage() { + return decodeImage(imgFile); + } + + } + + public static class StageInformation { + int duration; + String itinerary = ""; + File gpxFile; + GPXFile gpx; String name = ""; String shortDescription = ""; String fullDescription = ""; - Bitmap img; File imgFile; - private Bitmap itineraryImg; File itineraryFile; double distance; - private TourInformation tour; - private int order; + LatLon startPoint = null; + List favorites = new ArrayList(); + + TourInformation tour; + int order; + + public List getFavorites() { + return favorites; + } + + public StageFavoriteGroup getGroupById(String id) { + for(Object o : favorites) { + if(o instanceof StageFavoriteGroup) { + if(id.equals(((StageFavoriteGroup)o).id)) { + return (StageFavoriteGroup) o; + } + } + } + return null; + } + + public LatLon getStartPoint() { + return startPoint; + } public String getItinerary() { return itinerary; @@ -244,17 +370,12 @@ public class TourInformation { public Bitmap getImageBitmap() { - if(img == null && imgFile != null && imgFile.exists()) { - img = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); - } - return img; + return decodeImage(imgFile); } + public Bitmap getItineraryBitmap() { - if(itineraryImg == null && itineraryFile != null && itineraryFile.exists()) { - itineraryImg = BitmapFactory.decodeFile(itineraryFile.getAbsolutePath()); - } - return itineraryImg; + return decodeImage(itineraryFile); } @Override diff --git a/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java b/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java index d9e1daf904..41135b5eb6 100644 --- a/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java +++ b/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java @@ -4,6 +4,7 @@ import java.util.WeakHashMap; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.sherpafy.TourInformation.StageFavorite; import net.osmand.plus.sherpafy.TourInformation.StageInformation; import android.content.Intent; import android.content.pm.ActivityInfo; @@ -304,6 +305,19 @@ public class TourViewActivity extends SherlockFragmentActivity { } } + + public void showFavoriteFragment(StageInformation stage, StageFavorite sf) { + FragmentManager fragmentManager = getSupportFragmentManager(); + setDrawerIndicatorVisible(false); + SherpafyFavoriteFragment fragment = new SherpafyFavoriteFragment(); + Bundle bl = new Bundle(); + bl.putInt(SherpafyFavoriteFragment.STAGE_PARAM, stage.getOrder()); + bl.putString(SherpafyFavoriteFragment.TOUR_PARAM, stage.getTour().getId()); + bl.putInt(SherpafyFavoriteFragment.FAV_PARAM, sf.getOrder()); + fragment.setArguments(bl); + fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); + } + public void showHtmlFragment(String title, String cont) { FragmentManager fragmentManager = getSupportFragmentManager(); setDrawerIndicatorVisible(false);