From 36ff1407314839b36ba5dc430c27228d4a733639 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 25 Jan 2021 02:39:46 +0200 Subject: [PATCH] OverviewCard cleanup --- OsmAnd/res/layout/gpx_overview_fragment.xml | 172 +++++++++--------- OsmAnd/res/layout/item_gpx_action.xml | 31 ++-- OsmAnd/res/layout/item_gpx_stat_block.xml | 70 +++---- .../plus/myplaces/GPXItemPagerAdapter.java | 47 ++++- .../plus/myplaces/SegmentActionsListener.java | 6 +- .../plus/myplaces/TrackSegmentFragment.java | 57 +----- .../net/osmand/plus/track/OverviewCard.java | 147 ++++++++------- .../osmand/plus/track/TrackMenuFragment.java | 106 +++++------ 8 files changed, 307 insertions(+), 329 deletions(-) diff --git a/OsmAnd/res/layout/gpx_overview_fragment.xml b/OsmAnd/res/layout/gpx_overview_fragment.xml index cdea20a2be..0acb712fc6 100644 --- a/OsmAnd/res/layout/gpx_overview_fragment.xml +++ b/OsmAnd/res/layout/gpx_overview_fragment.xml @@ -1,104 +1,104 @@ + xmlns:osmand="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + \ No newline at end of file diff --git a/OsmAnd/res/layout/item_gpx_action.xml b/OsmAnd/res/layout/item_gpx_action.xml index 1bc74bf083..dce94e951b 100644 --- a/OsmAnd/res/layout/item_gpx_action.xml +++ b/OsmAnd/res/layout/item_gpx_action.xml @@ -1,22 +1,21 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="wrap_content" + android:layout_height="wrap_content"> - + - + \ No newline at end of file diff --git a/OsmAnd/res/layout/item_gpx_stat_block.xml b/OsmAnd/res/layout/item_gpx_stat_block.xml index 5bd38821ca..fcb3e604cb 100644 --- a/OsmAnd/res/layout/item_gpx_stat_block.xml +++ b/OsmAnd/res/layout/item_gpx_stat_block.xml @@ -1,44 +1,44 @@ + xmlns:osmand="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="vertical"> - + - + - + - + - + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java index 81d9ee9d72..14661fe775 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java @@ -38,6 +38,7 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType; +import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.helpers.GpxUiHelper.LineGraphType; import net.osmand.plus.helpers.GpxUiHelper.OrderedLineDataSet; import net.osmand.plus.track.TrackDisplayHelper; @@ -687,8 +688,50 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid } void openAnalyzeOnMap(GPXTabItemType tabType) { - List ds = getDataSets(null, tabType, null, null); - actionsListener.openAnalyzeOnMap(gpxItem, ds, tabType); + List dataSets = getDataSets(null, tabType, null, null); + prepareGpxItemChartTypes(gpxItem, dataSets); + actionsListener.openAnalyzeOnMap(gpxItem); + } + + public static void prepareGpxItemChartTypes(GpxDisplayItem gpxItem, List dataSets) { + WptPt wpt = null; + gpxItem.chartTypes = null; + if (dataSets != null && dataSets.size() > 0) { + gpxItem.chartTypes = new GPXDataSetType[dataSets.size()]; + for (int i = 0; i < dataSets.size(); i++) { + OrderedLineDataSet orderedDataSet = (OrderedLineDataSet) dataSets.get(i); + gpxItem.chartTypes[i] = orderedDataSet.getDataSetType(); + } + if (gpxItem.chartHighlightPos != -1) { + TrkSegment segment = null; + for (Track t : gpxItem.group.getGpx().tracks) { + for (TrkSegment s : t.segments) { + if (s.points.size() > 0 && s.points.get(0).equals(gpxItem.analysis.locationStart)) { + segment = s; + break; + } + } + if (segment != null) { + break; + } + } + if (segment != null) { + OrderedLineDataSet dataSet = (OrderedLineDataSet) dataSets.get(0); + float distance = gpxItem.chartHighlightPos * dataSet.getDivX(); + for (WptPt p : segment.points) { + if (p.distance >= distance) { + wpt = p; + break; + } + } + } + } + } + if (wpt != null) { + gpxItem.locationOnMap = wpt; + } else { + gpxItem.locationOnMap = gpxItem.locationStart; + } } private void openSplitIntervalScreen() { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SegmentActionsListener.java b/OsmAnd/src/net/osmand/plus/myplaces/SegmentActionsListener.java index 4033dedd7a..473f9bece1 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/SegmentActionsListener.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/SegmentActionsListener.java @@ -2,13 +2,9 @@ package net.osmand.plus.myplaces; import android.view.View; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; - import net.osmand.GPXUtilities.TrkSegment; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; -import java.util.List; - public interface SegmentActionsListener { void updateContent(); @@ -23,5 +19,5 @@ public interface SegmentActionsListener { void showOptionsPopupMenu(View view, TrkSegment trkSegment, boolean confirmDeletion); - void openAnalyzeOnMap(GpxDisplayItem gpxItem, List dataSets, GPXTabItemType tabType); + void openAnalyzeOnMap(GpxDisplayItem gpxItem); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java index c10fa883ad..c19930a334 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java @@ -22,16 +22,11 @@ import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; - import net.osmand.AndroidUtils; import net.osmand.FileUtils; import net.osmand.FileUtils.RenameCallback; import net.osmand.GPXUtilities.GPXFile; -import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.TrkSegment; -import net.osmand.GPXUtilities.WptPt; -import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; @@ -43,8 +38,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.helpers.GpxUiHelper; -import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; -import net.osmand.plus.helpers.GpxUiHelper.OrderedLineDataSet; import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.track.SaveGpxAsyncTask; @@ -299,55 +292,9 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit } @Override - public void openAnalyzeOnMap(GpxDisplayItem gpxItem, List dataSets, GPXTabItemType tabType) { - LatLon location = null; - WptPt wpt = null; - gpxItem.chartTypes = null; - if (dataSets != null && dataSets.size() > 0) { - gpxItem.chartTypes = new GPXDataSetType[dataSets.size()]; - for (int i = 0; i < dataSets.size(); i++) { - OrderedLineDataSet orderedDataSet = (OrderedLineDataSet) dataSets.get(i); - gpxItem.chartTypes[i] = orderedDataSet.getDataSetType(); - } - if (gpxItem.chartHighlightPos != -1) { - TrkSegment segment = null; - for (Track t : gpxItem.group.getGpx().tracks) { - for (TrkSegment s : t.segments) { - if (s.points.size() > 0 && s.points.get(0).equals(gpxItem.analysis.locationStart)) { - segment = s; - break; - } - } - if (segment != null) { - break; - } - } - if (segment != null) { - OrderedLineDataSet dataSet = (OrderedLineDataSet) dataSets.get(0); - float distance = gpxItem.chartHighlightPos * dataSet.getDivX(); - for (WptPt p : segment.points) { - if (p.distance >= distance) { - wpt = p; - break; - } - } - if (wpt != null) { - location = new LatLon(wpt.lat, wpt.lon); - } - } - } - } - if (location == null) { - location = new LatLon(gpxItem.locationStart.lat, gpxItem.locationStart.lon); - } - if (wpt != null) { - gpxItem.locationOnMap = wpt; - } else { - gpxItem.locationOnMap = gpxItem.locationStart; - } - + public void openAnalyzeOnMap(GpxDisplayItem gpxItem) { OsmandSettings settings = app.getSettings(); - settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), + settings.setMapLocationToShow(gpxItem.locationOnMap.lat, gpxItem.locationOnMap.lon, settings.getLastKnownMapZoom(), new PointDescription(PointDescription.POINT_TYPE_WPT, gpxItem.name), false, diff --git a/OsmAnd/src/net/osmand/plus/track/OverviewCard.java b/OsmAnd/src/net/osmand/plus/track/OverviewCard.java index 339fe111e7..d5d8e44ce4 100644 --- a/OsmAnd/src/net/osmand/plus/track/OverviewCard.java +++ b/OsmAnd/src/net/osmand/plus/track/OverviewCard.java @@ -20,25 +20,22 @@ import androidx.appcompat.widget.AppCompatImageView; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; +import androidx.recyclerview.widget.RecyclerView.ItemDecoration; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXTrackAnalysis; -import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.helpers.GpxUiHelper; -import net.osmand.plus.helpers.GpxUiHelper.LineGraphType; +import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.myplaces.SegmentActionsListener; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.widgets.TextViewEx; import net.osmand.util.Algorithms; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -58,7 +55,7 @@ public class OverviewCard extends BaseCard { private final TrackDisplayHelper displayHelper; private final GPXFile gpxFile; - private final GpxDisplayItemType[] filterTypes = new GpxDisplayItemType[]{GpxDisplayItemType.TRACK_SEGMENT}; + private final GpxDisplayItemType[] filterTypes = new GpxDisplayItemType[] {GpxDisplayItemType.TRACK_SEGMENT}; private final SegmentActionsListener listener; public OverviewCard(@NonNull MapActivity mapActivity, @NonNull TrackDisplayHelper displayHelper, @@ -106,19 +103,19 @@ public class OverviewCard extends BaseCard { String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app); String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app); - StatBlock sDistance = new StatBlock(app.getResources().getString(R.string.distance), OsmAndFormatter.getFormattedDistance(totalDistance, app), - R.drawable.ic_action_track_16, R.color.icon_color_default_light, LineGraphType.ALTITUDE, LineGraphType.SPEED); - StatBlock sAscent = new StatBlock(app.getResources().getString(R.string.altitude_ascent), asc, - R.drawable.ic_action_arrow_up_16, R.color.gpx_chart_red, LineGraphType.SLOPE, null); - StatBlock sDescent = new StatBlock(app.getResources().getString(R.string.altitude_descent), desc, - R.drawable.ic_action_arrow_down_16, R.color.gpx_pale_green, LineGraphType.ALTITUDE, LineGraphType.SLOPE); - StatBlock sAvSpeed = new StatBlock(app.getResources().getString(R.string.average_speed), avg, - R.drawable.ic_action_speed_16, R.color.icon_color_default_light, LineGraphType.SPEED, null); - StatBlock sMaxSpeed = new StatBlock(app.getResources().getString(R.string.max_speed), max, - R.drawable.ic_action_max_speed_16, R.color.icon_color_default_light, LineGraphType.SPEED, null); - StatBlock sTimeSpan = new StatBlock(app.getResources().getString(R.string.shared_string_time_span), + StatBlock sDistance = new StatBlock(app.getString(R.string.distance), OsmAndFormatter.getFormattedDistance(totalDistance, app), + R.drawable.ic_action_track_16, R.color.icon_color_default_light, GPXDataSetType.ALTITUDE, GPXDataSetType.SPEED); + StatBlock sAscent = new StatBlock(app.getString(R.string.altitude_ascent), asc, + R.drawable.ic_action_arrow_up_16, R.color.gpx_chart_red, GPXDataSetType.SLOPE, null); + StatBlock sDescent = new StatBlock(app.getString(R.string.altitude_descent), desc, + R.drawable.ic_action_arrow_down_16, R.color.gpx_pale_green, GPXDataSetType.ALTITUDE, GPXDataSetType.SLOPE); + StatBlock sAvSpeed = new StatBlock(app.getString(R.string.average_speed), avg, + R.drawable.ic_action_speed_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null); + StatBlock sMaxSpeed = new StatBlock(app.getString(R.string.max_speed), max, + R.drawable.ic_action_max_speed_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null); + StatBlock sTimeSpan = new StatBlock(app.getString(R.string.shared_string_time_span), Algorithms.formatDuration((int) (timeSpan / 1000), app.accessibilityEnabled()), - R.drawable.ic_action_time_span_16, R.color.icon_color_default_light, LineGraphType.SPEED, null); + R.drawable.ic_action_time_span_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null); LinearLayoutManager llManager = new LinearLayoutManager(app); llManager.setOrientation(LinearLayoutManager.HORIZONTAL); @@ -213,16 +210,17 @@ public class OverviewCard extends BaseCard { }); } - private class StatBlockAdapter extends RecyclerView.Adapter { - private final List StatBlocks; + private class StatBlockAdapter extends RecyclerView.Adapter { + + private final List statBlocks; public StatBlockAdapter(List StatBlocks) { - this.StatBlocks = StatBlocks; + this.statBlocks = StatBlocks; } @Override public int getItemCount() { - return StatBlocks.size(); + return statBlocks.size(); } @NonNull @@ -235,52 +233,65 @@ public class OverviewCard extends BaseCard { @Override public void onBindViewHolder(StatBlockViewHolder holder, int position) { - StatBlock item = StatBlocks.get(position); - holder.bind(item); - } + final StatBlock item = statBlocks.get(position); - class StatBlockViewHolder extends RecyclerView.ViewHolder { - private final TextViewEx valueText; - private final TextView titleText; - private final AppCompatImageView imageView; + holder.valueText.setText(item.value); + holder.titleText.setText(item.title); + holder.valueText.setTextColor(app.getResources().getColor(R.color.active_color_primary_light)); + holder.titleText.setTextColor(app.getResources().getColor(R.color.text_color_secondary_light)); + holder.itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + GpxDisplayItem gpxItem = TrackDisplayHelper.flatten(displayHelper.getOriginalGroups(filterTypes)).get(0); + if (gpxItem != null && gpxItem.analysis != null) { + ArrayList list = new ArrayList<>(); + if (item.firstType != null) { + list.add(item.firstType); + } + if (item.secondType != null) { + list.add(item.secondType); + } + if (list.size() > 0) { + gpxItem.chartTypes = list.toArray(new GPXDataSetType[0]); + } + gpxItem.locationOnMap = gpxItem.locationStart; - StatBlockViewHolder(View view) { - super(view); - valueText = view.findViewById(R.id.value); - titleText = view.findViewById(R.id.title); - imageView = view.findViewById(R.id.image); - } - - public void bind(final StatBlock overviewItem) { - valueText.setText(overviewItem.value); - valueText.setTextColor(app.getResources().getColor(R.color.active_color_primary_light)); - titleText.setText(overviewItem.title); - titleText.setTextColor(app.getResources().getColor(R.color.text_color_secondary_light)); - setImageDrawable(imageView, overviewItem.imageResId, overviewItem.imageColorId); - itemView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - GpxDisplayItem gpxItem = TrackDisplayHelper.flatten(displayHelper.getOriginalGroups(filterTypes)).get(0); - GPXTrackAnalysis analysis = gpxItem.analysis; - GpxDataItem gpxDataItem = displayHelper.getGpxDataItem(); - boolean calcWithoutGaps = gpxItem.isGeneralTrack() && gpxDataItem != null && !gpxDataItem.isJoinSegments(); - List dataSets = GpxUiHelper.getDataSets(new LineChart(app), app, analysis, overviewItem.firstType, overviewItem.secondType, calcWithoutGaps); - listener.openAnalyzeOnMap(gpxItem, dataSets, null); + listener.openAnalyzeOnMap(gpxItem); } - }); - } + } + }); + setImageDrawable(holder.imageView, item.imageResId, item.imageColorId); } } - private class HorizontalDividerDecoration extends RecyclerView.ItemDecoration { + private static class StatBlockViewHolder extends RecyclerView.ViewHolder { + + private final TextViewEx valueText; + private final TextView titleText; + private final AppCompatImageView imageView; + + StatBlockViewHolder(View view) { + super(view); + valueText = view.findViewById(R.id.value); + titleText = view.findViewById(R.id.title); + imageView = view.findViewById(R.id.image); + } + } + + private static class HorizontalDividerDecoration extends ItemDecoration { + private final Drawable divider; + private final int marginV; + private final int marginH; public HorizontalDividerDecoration(Context context) { - int[] ATTRS = new int[]{android.R.attr.listDivider}; + int[] ATTRS = new int[] {android.R.attr.listDivider}; final TypedArray ta = context.obtainStyledAttributes(ATTRS); divider = ta.getDrawable(0); // DrawableCompat.setTint(divider, context.getResources().getColor(R.color.divider_color_light)); //todo change drawable color ta.recycle(); + marginV = context.getResources().getDimensionPixelSize(R.dimen.map_small_button_margin); + marginH = context.getResources().getDimensionPixelSize(R.dimen.content_padding); } @Override @@ -289,10 +300,7 @@ public class OverviewCard extends BaseCard { } public void drawHorizontal(Canvas c, RecyclerView parent) { - final int marginV = parent.getContext().getResources().getDimensionPixelSize(R.dimen.map_small_button_margin); - final int marginH = parent.getContext().getResources().getDimensionPixelSize(R.dimen.content_padding); - final int childCount = parent.getChildCount(); - for (int i = 0; i < childCount; i++) { + for (int i = 0; i < parent.getChildCount(); i++) { final View child = parent.getChildAt(i); final int left = child.getRight() - divider.getIntrinsicWidth() + marginH; final int right = left + divider.getIntrinsicHeight(); @@ -305,21 +313,21 @@ public class OverviewCard extends BaseCard { @Override public void getItemOffsets(Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { - int marginV = parent.getContext().getResources().getDimensionPixelSize(R.dimen.map_small_button_margin); - int marginH = parent.getContext().getResources().getDimensionPixelSize(R.dimen.content_padding); outRect.set(marginH - divider.getIntrinsicWidth(), marginV, marginH + divider.getIntrinsicWidth(), marginV); } } private static class StatBlock { - private String title; - private String value; - private int imageResId; - private int imageColorId; - private LineGraphType firstType; - private LineGraphType secondType; - public StatBlock(String title, String value, @DrawableRes int imageResId, @ColorRes int imageColorId, LineGraphType firstType, LineGraphType secondType) { + private final String title; + private final String value; + private final int imageResId; + private final int imageColorId; + private final GPXDataSetType firstType; + private final GPXDataSetType secondType; + + public StatBlock(String title, String value, @DrawableRes int imageResId, @ColorRes int imageColorId, + GPXDataSetType firstType, GPXDataSetType secondType) { this.title = title; this.value = value; this.imageResId = imageResId; @@ -327,6 +335,5 @@ public class OverviewCard extends BaseCard { this.firstType = firstType; this.secondType = secondType; } - } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java b/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java index 2158894e8f..5f04366e77 100644 --- a/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java @@ -43,6 +43,7 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; +import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -77,6 +78,7 @@ import net.osmand.plus.routepreparationmenu.cards.BaseCard.CardListener; import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener; import net.osmand.plus.widgets.IconPopupMenu; import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -100,7 +102,7 @@ import static net.osmand.plus.track.OptionsCard.SHOW_ON_MAP_BUTTON_INDEX; import static net.osmand.plus.track.OptionsCard.UPLOAD_OSM_BUTTON_INDEX; public class TrackMenuFragment extends ContextMenuScrollFragment implements CardListener, - SegmentActionsListener, RenameCallback, OnTrackFileMoveListener, OsmAndLocationListener { + SegmentActionsListener, RenameCallback, OnTrackFileMoveListener, OsmAndLocationListener, OsmAndCompassListener { public static final String TAG = TrackMenuFragment.class.getName(); private static final Log log = PlatformUtil.getLog(TrackMenuFragment.class); @@ -123,8 +125,9 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card private int menuTitleHeight; private String gpxTitle; private UpdateLocationViewCache updateLocationViewCache; - private MapContextMenu menu; - private Location location = null; + private Location lastLocation = null; + private Float heading; + private boolean locationUpdateStarted; public enum TrackMenuType { OVERVIEW(R.id.action_overview, R.string.shared_string_overview), @@ -217,15 +220,11 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card headerTitle = view.findViewById(R.id.title); headerIcon = view.findViewById(R.id.icon_view); updateLocationViewCache = app.getUIUtilities().getUpdateLocationViewCache(); - menu = ((MapActivity) getActivity()).getContextMenu(); - location = app.getLocationProvider().getLastKnownLocation(); if (isPortrait()) { - View mainView = getMainView(); - View topShadow = getTopShadow(); - FrameLayout bottomContainer = getBottomContainer(); - topShadow.setVisibility(View.VISIBLE); - AndroidUtils.setBackground(mainView.getContext(), bottomContainer, isNightMode(), R.color.list_background_color_light, R.color.list_background_color_dark); + AndroidUiHelper.updateVisibility(getTopShadow(), true); + AndroidUtils.setBackground(view.getContext(), getBottomContainer(), isNightMode(), + R.color.list_background_color_light, R.color.list_background_color_dark); } else { int widthNoShadow = getLandscapeNoShadowWidth(); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(widthNoShadow, ViewGroup.LayoutParams.WRAP_CONTENT); @@ -362,9 +361,28 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } @Override - public void updateLocation(final Location location) { - this.location = location; - getMyApplication().runInUIThread(new Runnable() { + public void updateLocation(Location location) { + if (!MapUtils.areLatLonEqual(lastLocation, location)) { + lastLocation = location; + updateLocationUi(); + } + } + + @Override + public void updateCompassValue(float value) { + // 99 in next line used to one-time initialize arrows (with reference vs. fixed-north direction) + // on non-compass devices + float lastHeading = heading != null ? heading : 99; + heading = value; + if (Math.abs(MapUtils.degreesDiff(lastHeading, heading)) > 5) { + updateLocationUi(); + } else { + heading = lastHeading; + } + } + + private void updateLocationUi() { + app.runInUIThread(new Runnable() { @Override public void run() { updateDistanceDirection(); @@ -373,10 +391,10 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } private void updateDistanceDirection() { - OsmandApplication app = getMyApplication(); - FragmentActivity activity = getActivity(); + MapActivity mapActivity = getMapActivity(); View view = overviewCard.getView(); - if (app != null && activity != null && view != null) { + if (mapActivity != null && view != null) { + MapContextMenu menu = mapActivity.getContextMenu(); TextView distanceText = (TextView) view.findViewById(R.id.distance); ImageView direction = (ImageView) view.findViewById(R.id.direction); app.getUIUtilities().updateLocationView(updateLocationViewCache, direction, distanceText, menu.getLatLon()); @@ -385,14 +403,21 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card private void startLocationUpdate() { OsmandApplication app = getMyApplication(); - app.getLocationProvider().addLocationListener(this); - location = app.getLocationProvider().getLastKnownLocation(); - updateLocation(location); + if (app != null && !locationUpdateStarted) { + locationUpdateStarted = true; + app.getLocationProvider().addCompassListener(this); + app.getLocationProvider().addLocationListener(this); + updateLocationUi(); + } } private void stopLocationUpdate() { OsmandApplication app = getMyApplication(); - app.getLocationProvider().removeLocationListener(this); + if (app != null && locationUpdateStarted) { + locationUpdateStarted = false; + app.getLocationProvider().removeLocationListener(this); + app.getLocationProvider().removeCompassListener(this); + } } @Override @@ -708,46 +733,7 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } @Override - public void openAnalyzeOnMap(GpxDisplayItem gpxItem, List dataSets, GPXTabItemType tabType) { - WptPt wpt = null; - gpxItem.chartTypes = null; - if (dataSets != null && dataSets.size() > 0) { - gpxItem.chartTypes = new GPXDataSetType[dataSets.size()]; - for (int i = 0; i < dataSets.size(); i++) { - OrderedLineDataSet orderedDataSet = (OrderedLineDataSet) dataSets.get(i); - gpxItem.chartTypes[i] = orderedDataSet.getDataSetType(); - } - if (gpxItem.chartHighlightPos != -1) { - TrkSegment segment = null; - for (Track t : gpxItem.group.getGpx().tracks) { - for (TrkSegment s : t.segments) { - if (s.points.size() > 0 && s.points.get(0).equals(gpxItem.analysis.locationStart)) { - segment = s; - break; - } - } - if (segment != null) { - break; - } - } - if (segment != null) { - OrderedLineDataSet dataSet = (OrderedLineDataSet) dataSets.get(0); - float distance = gpxItem.chartHighlightPos * dataSet.getDivX(); - for (WptPt p : segment.points) { - if (p.distance >= distance) { - wpt = p; - break; - } - } - } - } - } - if (wpt != null) { - gpxItem.locationOnMap = wpt; - } else { - gpxItem.locationOnMap = gpxItem.locationStart; - } - + public void openAnalyzeOnMap(GpxDisplayItem gpxItem) { TrackDetailsMenu trackDetailsMenu = getMapActivity().getTrackDetailsMenu(); trackDetailsMenu.setGpxItem(gpxItem); trackDetailsMenu.show();