From 7673717e3e5a77bd772c9ce1b5e49ea54f7d4b06 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 27 Feb 2017 15:46:19 +0300 Subject: [PATCH] Refactored gpx info classes --- .../osmand/plus/activities/TrackActivity.java | 7 +- .../net/osmand/plus/helpers/GpxUiHelper.java | 2 - .../plus/myplaces/SelectedGPXFragment.java | 305 ------------------ .../plus/myplaces/TrackPointFragment.java | 202 +++++++++++- .../plus/myplaces/TrackSegmentFragment.java | 237 ++++++++++++-- 5 files changed, 410 insertions(+), 343 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java diff --git a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java index 3e0ff7151d..fb7e1f2a9e 100644 --- a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java @@ -25,7 +25,6 @@ import net.osmand.plus.OsmAndAppCustomization; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.myplaces.SelectedGPXFragment; import net.osmand.plus.myplaces.TrackPointFragment; import net.osmand.plus.myplaces.TrackSegmentFragment; import net.osmand.plus.views.controls.PagerSlidingTabStrip; @@ -102,8 +101,10 @@ public class TrackActivity extends TabActivity { for (WeakReference f : fragList) { Fragment frag = f.get(); - if (frag instanceof SelectedGPXFragment) { - ((SelectedGPXFragment) frag).setContent(); + if (frag instanceof TrackSegmentFragment) { + ((TrackSegmentFragment) frag).setContent(); + } else if (frag instanceof TrackPointFragment) { + ((TrackPointFragment) frag).setContent(); } } ((OsmandFragmentPagerAdapter) mViewPager.getAdapter()).addTab( diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index 18e8916418..ad9da0d8bf 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -845,8 +845,6 @@ public class GpxUiHelper { public static void setupGPXChart(OsmandApplication ctx, LineChart mChart, int yLabelsCount) { OsmandSettings settings = ctx.getSettings(); - OsmandSettings.MetricsConstants mc = settings.METRIC_SYSTEM.get(); - boolean useFeet = (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) || (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS); boolean light = settings.isLightContent(); //mChart.setHardwareAccelerationEnabled(true); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java deleted file mode 100644 index c232191640..0000000000 --- a/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java +++ /dev/null @@ -1,305 +0,0 @@ -package net.osmand.plus.myplaces; - - -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.os.Handler; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.view.MenuItemCompat; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ListView; -import android.widget.TextView; - -import net.osmand.data.LatLon; -import net.osmand.data.PointDescription; -import net.osmand.plus.GPXDatabase; -import net.osmand.plus.GPXDatabase.GpxDataItem; -import net.osmand.plus.GPXUtilities.GPXFile; -import net.osmand.plus.GpxSelectionHelper; -import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; -import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; -import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; -import net.osmand.plus.R; -import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.activities.TrackActivity; -import net.osmand.plus.base.OsmAndListFragment; - -import java.io.File; -import java.text.Collator; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - - -public class SelectedGPXFragment extends OsmAndListFragment { - public static final String ARG_TO_FILTER_SHORT_TRACKS = "ARG_TO_FILTER_SHORT_TRACKS"; - protected OsmandApplication app; - protected ArrayAdapter adapter; - private boolean updateEnable; - - @Override - public void onAttach(Context context) { - super.onAttach(context); - final Collator collator = Collator.getInstance(); - collator.setStrength(Collator.SECONDARY); - app = (OsmandApplication) getActivity().getApplication(); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - getListView().setBackgroundColor(getResources().getColor( - getMyApplication().getSettings().isLightContent() ? R.color.ctx_menu_info_view_bg_light - : R.color.ctx_menu_info_view_bg_dark)); - } - - public TrackActivity getMyActivity() { - return (TrackActivity) getActivity(); - } - - @Override - public ArrayAdapter getAdapter() { - return adapter; - } - - - private void startHandler() { - Handler updateCurrentRecordingTrack = new Handler(); - updateCurrentRecordingTrack.postDelayed(new Runnable() { - @Override - public void run() { - if (updateEnable) { - updateContent(); - adapter.notifyDataSetChanged(); - startHandler(); - } - } - }, 2000); - } - - public static boolean isArgumentTrue(@Nullable Bundle args, @NonNull String arg) { - return args != null && args.getBoolean(arg); - } - - - @Override - public void onResume() { - super.onResume(); - updateContent(); - updateEnable = true; - if (getGpx() != null && getGpx().showCurrentTrack && hasFilterType(GpxDisplayItemType.TRACK_POINTS)) { - startHandler(); - } - } - - @Override - public void onPause() { - super.onPause(); - updateEnable = false; - } - - - protected static List filterGroups(GpxDisplayItemType[] types, - @NonNull TrackActivity trackActivity, - @Nullable Bundle args) { - List result = trackActivity.getGpxFile(); - List groups = new ArrayList<>(); - for (GpxDisplayGroup group : result) { - boolean add = types == null || hasFilterType(types, group.getType()); - if (isArgumentTrue(args, ARG_TO_FILTER_SHORT_TRACKS)) { - Iterator item = group.getModifiableList().iterator(); - while (item.hasNext()) { - GpxDisplayItem it2 = item.next(); - if (it2.analysis != null && it2.analysis.totalDistance < 100) { - item.remove(); - } - } - if (group.getModifiableList().isEmpty()) { - add = false; - } - } - if (add) { - groups.add(group); - } - - } - return groups; - } - - public void setContent() { - setContent(getListView()); - } - - public void setContent(ListView listView) { - adapter = createSelectedGPXAdapter(); - updateContent(); - setupListView(listView); - setListAdapter(adapter); - } - - protected void setupListView(ListView listView) { - if (adapter.getCount() > 0) { - listView.addFooterView(getActivity().getLayoutInflater().inflate(R.layout.list_shadow_footer, null, false)); - } - } - - protected void updateContent() { - adapter.clear(); - List groups = filterGroups(filterTypes(), getMyActivity(), getArguments()); - adapter.setNotifyOnChange(false); - for (GpxDisplayItem i : flatten(groups)) { - adapter.add(i); - } - adapter.setNotifyOnChange(true); - adapter.notifyDataSetChanged(); - } - - protected GpxDisplayItemType[] filterTypes() { - return null; - } - - protected boolean hasFilterType(GpxDisplayItemType filterType) { - for (GpxDisplayItemType type : filterTypes()) { - if (type == filterType) { - return true; - } - } - return false; - } - - protected static boolean hasFilterType(GpxDisplayItemType[] filterTypes, GpxDisplayItemType filterType) { - for (GpxDisplayItemType type : filterTypes) { - if (type == filterType) { - return true; - } - } - return false; - } - - protected List flatten(List groups) { - ArrayList list = new ArrayList<>(); - for(GpxDisplayGroup g : groups) { - list.addAll(g.getModifiableList()); - } - return list; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - setHasOptionsMenu(true); - View view = getActivity().getLayoutInflater().inflate(R.layout.update_index, container, false); - view.findViewById(R.id.header_layout).setVisibility(View.GONE); - ListView listView = (ListView) view.findViewById(android.R.id.list); - listView.setDivider(null); - listView.setDividerHeight(0); - TextView tv = new TextView(getActivity()); - tv.setText(R.string.none_selected_gpx); - tv.setTextSize(24); - listView.setEmptyView(tv); - setContent(listView); - return view; - } - - - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - return super.onOptionsItemSelected(item); - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - menu.clear(); - getMyActivity().getClearToolbar(false); - if (getGpx() != null && getGpx().path != null && !getGpx().showCurrentTrack) { - MenuItem item = menu.add(R.string.shared_string_share).setIcon(R.drawable.ic_action_gshare_dark) - .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - final Uri fileUri = Uri.fromFile(new File(getGpx().path)); - final Intent sendIntent = new Intent(Intent.ACTION_SEND); - sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri); - sendIntent.setType("application/gpx+xml"); - startActivity(sendIntent); - return true; - } - }); - MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); - } - } - - protected GPXFile getGpx() { - return getMyActivity().getGpx(); - } - - protected GpxDataItem getGpxDataItem() { - return getMyActivity().getGpxDataItem(); - } - - public ArrayAdapter createSelectedGPXAdapter() { - return null; - } - - @Override - public void onListItemClick(ListView l, View v, int position, long id) { - - GpxDisplayItem child = adapter.getItem(position); - if (child != null) { - if (child.group.getGpx() != null) { - app.getSelectedGpxHelper().setGpxFileToDisplay(child.group.getGpx()); - } - - final OsmandSettings settings = app.getSettings(); - LatLon location = new LatLon(child.locationStart.lat, child.locationStart.lon); - - if (child.group.getType() == GpxDisplayItemType.TRACK_POINTS) { - settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), - settings.getLastKnownMapZoom(), - new PointDescription(PointDescription.POINT_TYPE_WPT, child.locationStart.name), - false, - child.locationStart); - } else if (child.group.getType() == GpxDisplayItemType.TRACK_ROUTE_POINTS) { - settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), - settings.getLastKnownMapZoom(), - new PointDescription(PointDescription.POINT_TYPE_WPT, child.name), - false, - child.locationStart); - } else { - settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), - settings.getLastKnownMapZoom(), - new PointDescription(PointDescription.POINT_TYPE_GPX_ITEM, child.group.getGpxName()), - false, - child); - } - MapActivity.launchMapActivityMoveToTop(getActivity()); - } -/* -// if(child.group.getType() == GpxDisplayItemType.TRACK_POINTS || -// child.group.getType() == GpxDisplayItemType.TRACK_ROUTE_POINTS) { - ContextMenuAdapter qa = new ContextMenuAdapter(v.getContext()); - qa.setAnchor(v); - PointDescription name = new PointDescription(PointDescription.POINT_TYPE_FAVORITE, Html.fromHtml(child.name).toString()); - LatLon location = new LatLon(child.locationStart.lat, child.locationStart.lon); - OsmandSettings settings = app.getSettings(); - final PopupMenu optionsMenu = new PopupMenu(getActivity(), v); - DirectionsDialogs.createDirectionActionsPopUpMenu(optionsMenu, location, child.locationStart, name, settings.getLastKnownMapZoom(), - getActivity(), false, false); - optionsMenu.show(); -// } else { -// child.expanded = !child.expanded; -// adapter.notifyDataSetInvalidated(); -// } -*/ - } -} diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java index 0f0fa39632..dcf3b17911 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java @@ -1,7 +1,9 @@ package net.osmand.plus.myplaces; import android.content.DialogInterface; +import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AlertDialog; import android.text.Html; @@ -21,30 +23,86 @@ import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.plus.FavouritesDbHelper; -import net.osmand.plus.GpxSelectionHelper; +import net.osmand.plus.GPXDatabase; +import net.osmand.plus.GPXUtilities; +import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType; import net.osmand.plus.MapMarkersHelper; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.base.FavoriteImageDrawable; +import net.osmand.plus.base.OsmAndListFragment; import net.osmand.util.Algorithms; import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; -public class TrackPointFragment extends SelectedGPXFragment { +public class TrackPointFragment extends OsmAndListFragment { + + public static final String ARG_TO_FILTER_SHORT_TRACKS = "ARG_TO_FILTER_SHORT_TRACKS"; + + private OsmandApplication app; + private ArrayAdapter adapter; + private GpxDisplayItemType[] filterTypes = { GpxDisplayItemType.TRACK_POINTS, GpxDisplayItemType.TRACK_ROUTE_POINTS }; @Override - protected void setupListView(ListView listView) { - super.setupListView(listView); - if (adapter.getCount() > 0) { - listView.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.list_shadow_header, null, false)); - } + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.app = getMyApplication(); } @Override - protected GpxDisplayItemType[] filterTypes() { - return new GpxDisplayItemType[] { GpxSelectionHelper.GpxDisplayItemType.TRACK_POINTS, GpxSelectionHelper.GpxDisplayItemType.TRACK_ROUTE_POINTS }; + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + getListView().setBackgroundColor(getResources().getColor( + getMyApplication().getSettings().isLightContent() ? R.color.ctx_menu_info_view_bg_light + : R.color.ctx_menu_info_view_bg_dark)); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + setHasOptionsMenu(true); + View view = getActivity().getLayoutInflater().inflate(R.layout.update_index, container, false); + view.findViewById(R.id.header_layout).setVisibility(View.GONE); + ListView listView = (ListView) view.findViewById(android.R.id.list); + listView.setDivider(null); + listView.setDividerHeight(0); + TextView tv = new TextView(getActivity()); + tv.setText(R.string.none_selected_gpx); + tv.setTextSize(24); + listView.setEmptyView(tv); + setContent(listView); + return view; + } + + public TrackActivity getMyActivity() { + return (TrackActivity) getActivity(); + } + + public ArrayAdapter getAdapter() { + return adapter; + } + + private GPXUtilities.GPXFile getGpx() { + return getMyActivity().getGpx(); + } + + private GPXDatabase.GpxDataItem getGpxDataItem() { + return getMyActivity().getGpxDataItem(); + } + + private void setupListView(ListView listView) { + if (adapter.getCount() > 0) { + listView.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.list_shadow_header, null, false)); + listView.addFooterView(getActivity().getLayoutInflater().inflate(R.layout.list_shadow_footer, null, false)); + } } @Override @@ -75,10 +133,78 @@ public class TrackPointFragment extends SelectedGPXFragment { } } + private boolean isArgumentTrue(@NonNull String arg) { + return getArguments() != null && getArguments().getBoolean(arg); + } + + private boolean hasFilterType(GpxDisplayItemType[] filterTypes, GpxDisplayItemType filterType) { + for (GpxDisplayItemType type : filterTypes) { + if (type == filterType) { + return true; + } + } + return false; + } + + private List filterGroups(GpxDisplayItemType[] types) { + List result = getMyActivity().getGpxFile(); + List groups = new ArrayList<>(); + for (GpxDisplayGroup group : result) { + boolean add = types == null || hasFilterType(types, group.getType()); + if (isArgumentTrue(ARG_TO_FILTER_SHORT_TRACKS)) { + Iterator item = group.getModifiableList().iterator(); + while (item.hasNext()) { + GpxDisplayItem it2 = item.next(); + if (it2.analysis != null && it2.analysis.totalDistance < 100) { + item.remove(); + } + } + if (group.getModifiableList().isEmpty()) { + add = false; + } + } + if (add) { + groups.add(group); + } + + } + return groups; + } + + public void setContent() { + setContent(getListView()); + } + + public void setContent(ListView listView) { + adapter = new PointGPXAdapter(new ArrayList()); + updateContent(); + setupListView(listView); + setListAdapter(adapter); + } + + protected void updateContent() { + adapter.clear(); + List groups = filterGroups(filterTypes); + adapter.setNotifyOnChange(false); + for (GpxDisplayItem i : flatten(groups)) { + adapter.add(i); + } + adapter.setNotifyOnChange(true); + adapter.notifyDataSetChanged(); + } + + protected List flatten(List groups) { + ArrayList list = new ArrayList<>(); + for(GpxDisplayGroup g : groups) { + list.addAll(g.getModifiableList()); + } + return list; + } + protected void saveAsFavorites(final GpxDisplayItemType gpxDisplayItemType) { AlertDialog.Builder b = new AlertDialog.Builder(getMyActivity()); final EditText editText = new EditText(getMyActivity()); - final List gs = filterGroups(new GpxDisplayItemType[] { gpxDisplayItemType }, getMyActivity(), getArguments()); + final List gs = filterGroups(new GpxDisplayItemType[] { gpxDisplayItemType }); if (gs.size() == 0) { return; } @@ -103,7 +229,7 @@ public class TrackPointFragment extends SelectedGPXFragment { protected void saveAsMapMarkers(final GpxDisplayItemType gpxDisplayItemType) { AlertDialog.Builder b = new AlertDialog.Builder(getMyActivity()); - final List gs = filterGroups(new GpxDisplayItemType[] { gpxDisplayItemType }, getMyActivity(), getArguments()); + final List gs = filterGroups(new GpxDisplayItemType[] { gpxDisplayItemType }); if (gs.size() == 0) { return; } @@ -147,17 +273,65 @@ public class TrackPointFragment extends SelectedGPXFragment { } @Override - public ArrayAdapter createSelectedGPXAdapter() { - return new PointGPXAdapter(new ArrayList()); - } + public void onListItemClick(ListView l, View v, int position, long id) { + GpxDisplayItem child = adapter.getItem(position); + if (child != null) { + if (child.group.getGpx() != null) { + app.getSelectedGpxHelper().setGpxFileToDisplay(child.group.getGpx()); + } + + final OsmandSettings settings = app.getSettings(); + LatLon location = new LatLon(child.locationStart.lat, child.locationStart.lon); + + if (child.group.getType() == GpxDisplayItemType.TRACK_ROUTE_POINTS) { + settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), + settings.getLastKnownMapZoom(), + new PointDescription(PointDescription.POINT_TYPE_WPT, child.name), + false, + child.locationStart); + } else { + settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), + settings.getLastKnownMapZoom(), + new PointDescription(PointDescription.POINT_TYPE_GPX_ITEM, child.group.getGpxName()), + false, + child); + } + MapActivity.launchMapActivityMoveToTop(getActivity()); + } + } class PointGPXAdapter extends ArrayAdapter { + Map> itemTypes = new LinkedHashMap<>(); + List types = new ArrayList<>(); + PointGPXAdapter(List items) { super(getActivity(), R.layout.gpx_list_item_tab_content, items); } + @Override + public void add(GpxDisplayItem item) { + super.add(item); + GpxDisplayItemType type = item.group.getType(); + List items = itemTypes.get(type); + if (items == null) { + items = new ArrayList<>(); + itemTypes.put(type, items); + } + items.add(item); + if (!types.contains(type)) { + types.add(type); + } + } + + @Override + public void clear() { + super.clear(); + types.clear(); + itemTypes.clear(); + } + @NonNull @Override public View getView(int position, View convertView, @NonNull ViewGroup parent) { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java index ba770323c9..d5aa24287a 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java @@ -1,10 +1,15 @@ package net.osmand.plus.myplaces; import android.content.Context; +import android.content.Intent; import android.graphics.Matrix; +import android.net.Uri; import android.os.AsyncTask; +import android.os.Bundle; +import android.os.Handler; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.view.MenuItemCompat; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewCompat; import android.support.v7.widget.ListPopupWindow; @@ -12,6 +17,9 @@ import android.support.v7.widget.SwitchCompat; import android.util.SparseArray; import android.view.Gravity; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -31,6 +39,7 @@ import com.github.mikephil.charting.listener.OnChartGestureListener; import net.osmand.AndroidUtils; import net.osmand.data.PointDescription; +import net.osmand.plus.GPXDatabase; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXTrackAnalysis; import net.osmand.plus.GpxSelectionHelper; @@ -45,6 +54,7 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TrackActivity; +import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.dialogs.ConfigureMapMenu.AppearanceListItem; import net.osmand.plus.dialogs.ConfigureMapMenu.GpxAppearanceAdapter; import net.osmand.plus.dialogs.ConfigureMapMenu.GpxAppearanceAdapter.GpxAppearanceAdapterType; @@ -57,11 +67,13 @@ import net.osmand.plus.views.controls.WrapContentHeightViewPager.ViewAtPositionI import net.osmand.render.RenderingRulesStorage; import net.osmand.util.Algorithms; +import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -70,19 +82,127 @@ import gnu.trove.list.array.TIntArrayList; import static net.osmand.plus.dialogs.ConfigureMapMenu.CURRENT_TRACK_COLOR_ATTR; -public class TrackSegmentFragment extends SelectedGPXFragment { +public class TrackSegmentFragment extends OsmAndListFragment { + public static final String ARG_TO_FILTER_SHORT_TRACKS = "ARG_TO_FILTER_SHORT_TRACKS"; + + private OsmandApplication app; + private ArrayAdapter adapter; + + private GpxDisplayItemType[] filterTypes = { GpxSelectionHelper.GpxDisplayItemType.TRACK_SEGMENT }; private List options = new ArrayList<>(); private List distanceSplit = new ArrayList<>(); private TIntArrayList timeSplit = new TIntArrayList(); private int selectedSplitInterval; + private boolean updateEnable; @Override - protected void setupListView(ListView listView) { - super.setupListView(listView); + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.app = getMyApplication(); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + getListView().setBackgroundColor(getResources().getColor( + getMyApplication().getSettings().isLightContent() ? R.color.ctx_menu_info_view_bg_light + : R.color.ctx_menu_info_view_bg_dark)); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + setHasOptionsMenu(true); + View view = getActivity().getLayoutInflater().inflate(R.layout.update_index, container, false); + view.findViewById(R.id.header_layout).setVisibility(View.GONE); + ListView listView = (ListView) view.findViewById(android.R.id.list); + listView.setDivider(null); + listView.setDividerHeight(0); + TextView tv = new TextView(getActivity()); + tv.setText(R.string.none_selected_gpx); + tv.setTextSize(24); + listView.setEmptyView(tv); + setContent(listView); + return view; + } + + public TrackActivity getMyActivity() { + return (TrackActivity) getActivity(); + } + + public ArrayAdapter getAdapter() { + return adapter; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + return super.onOptionsItemSelected(item); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + menu.clear(); + getMyActivity().getClearToolbar(false); + if (getGpx() != null && getGpx().path != null && !getGpx().showCurrentTrack) { + MenuItem item = menu.add(R.string.shared_string_share).setIcon(R.drawable.ic_action_gshare_dark) + .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + final Uri fileUri = Uri.fromFile(new File(getGpx().path)); + final Intent sendIntent = new Intent(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri); + sendIntent.setType("application/gpx+xml"); + startActivity(sendIntent); + return true; + } + }); + MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + } + } + + private GPXUtilities.GPXFile getGpx() { + return getMyActivity().getGpx(); + } + + private GPXDatabase.GpxDataItem getGpxDataItem() { + return getMyActivity().getGpxDataItem(); + } + + private void startHandler() { + Handler updateCurrentRecordingTrack = new Handler(); + updateCurrentRecordingTrack.postDelayed(new Runnable() { + @Override + public void run() { + if (updateEnable) { + updateContent(); + adapter.notifyDataSetChanged(); + startHandler(); + } + } + }, 2000); + } + + @Override + public void onResume() { + super.onResume(); + updateContent(); + updateEnable = true; + if (getGpx() != null && getGpx().showCurrentTrack) { + startHandler(); + } + } + + @Override + public void onPause() { + super.onPause(); + updateEnable = false; + } + + private void setupListView(ListView listView) { if (adapter.getCount() > 0) { View view = getActivity().getLayoutInflater().inflate(R.layout.gpx_item_list_header, null, false); listView.addHeaderView(view); + listView.addFooterView(getActivity().getLayoutInflater().inflate(R.layout.list_shadow_footer, null, false)); final ImageView imageView = (ImageView) view.findViewById(R.id.imageView); final View splitIntervalView = view.findViewById(R.id.split_interval_view); final View colorView = view.findViewById(R.id.color_view); @@ -204,8 +324,7 @@ public class TrackSegmentFragment extends SelectedGPXFragment { } private List getGroups() { - return filterGroups(new GpxDisplayItemType[] { GpxDisplayItemType.TRACK_SEGMENT }, - getMyActivity(), getArguments()); + return filterGroups(); } private void setupSplitIntervalView(View view) { @@ -255,9 +374,72 @@ public class TrackSegmentFragment extends SelectedGPXFragment { } } - @Override - protected GpxDisplayItemType[] filterTypes() { - return new GpxDisplayItemType[] { GpxSelectionHelper.GpxDisplayItemType.TRACK_SEGMENT }; + private boolean isArgumentTrue(@NonNull String arg) { + return getArguments() != null && getArguments().getBoolean(arg); + } + + protected boolean hasFilterType(GpxDisplayItemType filterType) { + for (GpxDisplayItemType type : filterTypes) { + if (type == filterType) { + return true; + } + } + return false; + } + + private List filterGroups() { + List result = getMyActivity().getGpxFile(); + List groups = new ArrayList<>(); + for (GpxDisplayGroup group : result) { + boolean add = hasFilterType(group.getType()); + if (isArgumentTrue(ARG_TO_FILTER_SHORT_TRACKS)) { + Iterator item = group.getModifiableList().iterator(); + while (item.hasNext()) { + GpxDisplayItem it2 = item.next(); + if (it2.analysis != null && it2.analysis.totalDistance < 100) { + item.remove(); + } + } + if (group.getModifiableList().isEmpty()) { + add = false; + } + } + if (add) { + groups.add(group); + } + + } + return groups; + } + + public void setContent() { + setContent(getListView()); + } + + public void setContent(ListView listView) { + adapter = new SegmentGPXAdapter(new ArrayList()); + updateContent(); + setupListView(listView); + setListAdapter(adapter); + } + + protected void updateContent() { + adapter.clear(); + List groups = filterGroups(); + adapter.setNotifyOnChange(false); + for (GpxDisplayItem i : flatten(groups)) { + adapter.add(i); + } + adapter.setNotifyOnChange(true); + adapter.notifyDataSetChanged(); + } + + protected List flatten(List groups) { + ArrayList list = new ArrayList<>(); + for(GpxDisplayGroup g : groups) { + list.addAll(g.getModifiableList()); + } + return list; } private void prepareSplitIntervalAdapterData() { @@ -292,7 +474,7 @@ public class TrackSegmentFragment extends SelectedGPXFragment { } private void updateSplit(List groups, SelectedGpxFile sf) { - new SplitTrackAsyncTask(sf, this, getMyActivity(), groups).execute((Void) null); + new SplitTrackAsyncTask(sf, groups).execute((Void) null); } private void addOptionSplit(int value, boolean distance, List model) { @@ -320,10 +502,30 @@ public class TrackSegmentFragment extends SelectedGPXFragment { } } + /* @Override - public ArrayAdapter createSelectedGPXAdapter() { - return new SegmentGPXAdapter(new ArrayList()); + public void onListItemClick(ListView l, View v, int position, long id) { + + GpxDisplayItem child = adapter.getItem(position); + if (child != null) { + if (child.group.getGpx() != null) { + app.getSelectedGpxHelper().setGpxFileToDisplay(child.group.getGpx()); + } + + final OsmandSettings settings = app.getSettings(); + LatLon location = new LatLon(child.locationStart.lat, child.locationStart.lon); + + if (child.group.getType() == GpxDisplayItemType.TRACK_POINTS) { + settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), + settings.getLastKnownMapZoom(), + new PointDescription(PointDescription.POINT_TYPE_WPT, child.locationStart.name), + false, + child.locationStart); + } + MapActivity.launchMapActivityMoveToTop(getActivity()); + } } + */ class SegmentGPXAdapter extends ArrayAdapter { @@ -783,24 +985,21 @@ public class TrackSegmentFragment extends SelectedGPXFragment { class SplitTrackAsyncTask extends AsyncTask { @Nullable private final SelectedGpxFile mSelectedGpxFile; - @NonNull private final SelectedGPXFragment mFragment; + @NonNull private final TrackSegmentFragment mFragment; @NonNull private final TrackActivity mActivity; private final List groups; - SplitTrackAsyncTask(@Nullable SelectedGpxFile selectedGpxFile, - @NonNull SelectedGPXFragment fragment, - @NonNull TrackActivity activity, - List groups) { + SplitTrackAsyncTask(@Nullable SelectedGpxFile selectedGpxFile, List groups) { mSelectedGpxFile = selectedGpxFile; - mFragment = fragment; - mActivity = activity; + mFragment = TrackSegmentFragment.this; + mActivity = getMyActivity(); this.groups = groups; } protected void onPostExecute(Void result) { if (mSelectedGpxFile != null) { - mSelectedGpxFile.setDisplayGroups(filterGroups(null, mActivity, mFragment.getArguments())); + mSelectedGpxFile.setDisplayGroups(filterGroups()); } if (mFragment.isVisible()) { mFragment.updateContent();