add ability to update data with track recording interval

This commit is contained in:
Skalii 2021-02-10 04:31:49 +02:00
parent 0c0d90d42c
commit fb3820f907

View file

@ -1,10 +1,10 @@
package net.osmand.plus.track; package net.osmand.plus.track;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
@ -12,7 +12,6 @@ import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView; import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -24,7 +23,6 @@ import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
@ -41,6 +39,13 @@ public class GpxBlockStatisticsBuilder {
private RecyclerView blocksView; private RecyclerView blocksView;
private final SelectedGpxFile selectedGpxFile; private final SelectedGpxFile selectedGpxFile;
private BlockStatisticsAdapter adapter;
private final List<StatBlock> items = new ArrayList<>();
private final Handler handler = new Handler();
private Runnable updatingItems;
private boolean updateRunning = false;
public GpxBlockStatisticsBuilder(OsmandApplication app, SelectedGpxFile selectedGpxFile) { public GpxBlockStatisticsBuilder(OsmandApplication app, SelectedGpxFile selectedGpxFile) {
this.app = app; this.app = app;
this.selectedGpxFile = selectedGpxFile; this.selectedGpxFile = selectedGpxFile;
@ -59,7 +64,39 @@ public class GpxBlockStatisticsBuilder {
} }
public void initStatBlocks(SegmentActionsListener actionsListener, @ColorInt int activeColor, boolean nightMode) { public void initStatBlocks(SegmentActionsListener actionsListener, @ColorInt int activeColor, boolean nightMode) {
List<StatBlock> items = new ArrayList<>(); initItems();
boolean isNotEmpty = !Algorithms.isEmpty(items);
AndroidUiHelper.updateVisibility(blocksView, isNotEmpty);
if (isNotEmpty) {
adapter = new BlockStatisticsAdapter(getDisplayItem(getGPXFile()), actionsListener, activeColor, nightMode);
adapter.setItems(items);
blocksView.setLayoutManager(new LinearLayoutManager(app, LinearLayoutManager.HORIZONTAL, false));
blocksView.setAdapter(adapter);
}
}
public void stopUpdatingStatBlocks() {
handler.removeCallbacks(updatingItems);
updateRunning = false;
}
public void runUpdatingStatBlocks() {
updatingItems = new Runnable() {
@Override
public void run() {
if (adapter != null) {
initItems();
adapter.setItems(items);
AndroidUiHelper.updateVisibility(blocksView, !Algorithms.isEmpty(items));
}
int interval = app.getSettings().SAVE_GLOBAL_TRACK_INTERVAL.get();
handler.postDelayed(this, Math.max(1000, interval));
}
};
updateRunning = handler.post(updatingItems);
}
public void initItems() {
GPXFile gpxFile = getGPXFile(); GPXFile gpxFile = getGPXFile();
GpxDisplayItem gpxDisplayItem = null; GpxDisplayItem gpxDisplayItem = null;
GPXTrackAnalysis analysis = null; GPXTrackAnalysis analysis = null;
@ -76,72 +113,55 @@ public class GpxBlockStatisticsBuilder {
String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app); String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app);
String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app); String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app);
prepareData(analysis, items, app.getString(R.string.distance), OsmAndFormatter.getFormattedDistance(totalDistance, app), items.clear();
prepareData(analysis, 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, ItemType.ITEM_DISTANCE); R.drawable.ic_action_track_16, R.color.icon_color_default_light, GPXDataSetType.ALTITUDE, GPXDataSetType.SPEED, ItemType.ITEM_DISTANCE);
prepareData(analysis, items, app.getString(R.string.altitude_ascent), asc, prepareData(analysis, app.getString(R.string.altitude_ascent), asc,
R.drawable.ic_action_arrow_up_16, R.color.gpx_chart_red, GPXDataSetType.SLOPE, null, ItemType.ITEM_ALTITUDE); R.drawable.ic_action_arrow_up_16, R.color.gpx_chart_red, GPXDataSetType.SLOPE, null, ItemType.ITEM_ALTITUDE);
prepareData(analysis, items, app.getString(R.string.altitude_descent), desc, prepareData(analysis, app.getString(R.string.altitude_descent), desc,
R.drawable.ic_action_arrow_down_16, R.color.gpx_pale_green, GPXDataSetType.ALTITUDE, GPXDataSetType.SLOPE, ItemType.ITEM_ALTITUDE); R.drawable.ic_action_arrow_down_16, R.color.gpx_pale_green, GPXDataSetType.ALTITUDE, GPXDataSetType.SLOPE, ItemType.ITEM_ALTITUDE);
prepareData(analysis, items, app.getString(R.string.average_speed), avg, prepareData(analysis, app.getString(R.string.average_speed), avg,
R.drawable.ic_action_speed_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null, ItemType.ITEM_SPEED); R.drawable.ic_action_speed_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null, ItemType.ITEM_SPEED);
prepareData(analysis, items, app.getString(R.string.max_speed), max, prepareData(analysis, app.getString(R.string.max_speed), max,
R.drawable.ic_action_max_speed_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null, ItemType.ITEM_SPEED); R.drawable.ic_action_max_speed_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null, ItemType.ITEM_SPEED);
prepareData(analysis, items, app.getString(R.string.shared_string_time_span), prepareData(analysis, app.getString(R.string.shared_string_time_span),
Algorithms.formatDuration((int) (timeSpan / 1000), app.accessibilityEnabled()), Algorithms.formatDuration((int) (timeSpan / 1000), app.accessibilityEnabled()),
R.drawable.ic_action_time_span_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null, ItemType.ITEM_TIME); R.drawable.ic_action_time_span_16, R.color.icon_color_default_light, GPXDataSetType.SPEED, null, ItemType.ITEM_TIME);
} }
boolean isNotEmpty = !Algorithms.isEmpty(items);
AndroidUiHelper.updateVisibility(blocksView, isNotEmpty);
if (isNotEmpty && gpxDisplayItem != null) {
final BlockStatisticsAdapter sbAdapter = new BlockStatisticsAdapter(items, gpxDisplayItem, actionsListener, activeColor, nightMode);
blocksView.setLayoutManager(new LinearLayoutManager(app, LinearLayoutManager.HORIZONTAL, false));
blocksView.setAdapter(sbAdapter);
}
} }
public void prepareData(GPXTrackAnalysis analysis, List<StatBlock> listItems, String title, public void prepareData(GPXTrackAnalysis analysis, String title, String value,
String value, @DrawableRes int imageResId, @ColorRes int imageColorId, @DrawableRes int imageResId, @ColorRes int imageColorId,
GPXDataSetType firstType, GPXDataSetType secondType, ItemType itemType) { GPXDataSetType firstType, GPXDataSetType secondType, ItemType itemType) {
StatBlock statBlock = new StatBlock(title, value, imageResId, imageColorId, firstType, secondType, itemType); StatBlock statBlock = new StatBlock(title, value, imageResId, imageColorId, firstType, secondType, itemType);
switch (statBlock.itemType) { switch (statBlock.itemType) {
case ITEM_DISTANCE: { case ITEM_DISTANCE: {
if (analysis.totalDistance != 0f) { if (analysis.totalDistance != 0f) {
listItems.add(statBlock); items.add(statBlock);
} }
break; break;
} }
case ITEM_ALTITUDE: { case ITEM_ALTITUDE: {
if (analysis.hasElevationData) { if (analysis.hasElevationData) {
listItems.add(statBlock); items.add(statBlock);
} }
break; break;
} }
case ITEM_SPEED: { case ITEM_SPEED: {
if (analysis.isSpeedSpecified()) { if (analysis.isSpeedSpecified()) {
listItems.add(statBlock); items.add(statBlock);
} }
break; break;
} }
case ITEM_TIME: { case ITEM_TIME: {
if (analysis.hasSpeedData) { if (analysis.hasSpeedData) {
listItems.add(statBlock); items.add(statBlock);
} }
break; break;
} }
} }
} }
private void setImageDrawable(ImageView iv, @DrawableRes Integer resId, @ColorRes int color) {
Drawable icon = resId != null ? app.getUIUtilities().getIcon(resId, color)
: UiUtilities.tintDrawable(iv.getDrawable(), getResolvedColor(color));
iv.setImageDrawable(icon);
}
@ColorInt
protected int getResolvedColor(@ColorRes int colorId) {
return ContextCompat.getColor(app, colorId);
}
public class StatBlock { public class StatBlock {
private final String title; private final String title;
private final String value; private final String value;
@ -172,16 +192,15 @@ public class GpxBlockStatisticsBuilder {
private class BlockStatisticsAdapter extends RecyclerView.Adapter<BlockStatisticsViewHolder> { private class BlockStatisticsAdapter extends RecyclerView.Adapter<BlockStatisticsViewHolder> {
private final List<StatBlock> statBlocks; private final List<StatBlock> items = new ArrayList<>();
private final GpxDisplayItem displayItem; private final GpxDisplayItem displayItem;
private final SegmentActionsListener actionsListener; private final SegmentActionsListener actionsListener;
@ColorInt @ColorInt
private final int activeColor; private final int activeColor;
private final boolean nightMode; private final boolean nightMode;
public BlockStatisticsAdapter(List<StatBlock> statBlocks, GpxDisplayItem displayItem, public BlockStatisticsAdapter(GpxDisplayItem displayItem, SegmentActionsListener actionsListener,
SegmentActionsListener actionsListener, @ColorInt int activeColor, boolean nightMode) { @ColorInt int activeColor, boolean nightMode) {
this.statBlocks = statBlocks;
this.displayItem = displayItem; this.displayItem = displayItem;
this.actionsListener = actionsListener; this.actionsListener = actionsListener;
this.activeColor = activeColor; this.activeColor = activeColor;
@ -190,7 +209,7 @@ public class GpxBlockStatisticsBuilder {
@Override @Override
public int getItemCount() { public int getItemCount() {
return statBlocks.size(); return items.size();
} }
@NonNull @NonNull
@ -203,16 +222,19 @@ public class GpxBlockStatisticsBuilder {
@Override @Override
public void onBindViewHolder(BlockStatisticsViewHolder holder, int position) { public void onBindViewHolder(BlockStatisticsViewHolder holder, int position) {
final StatBlock item = statBlocks.get(position); final StatBlock item = items.get(position);
holder.valueText.setText(item.value); holder.valueText.setText(item.value);
holder.titleText.setText(item.title); holder.titleText.setText(item.title);
if (updateRunning) {
holder.titleText.setWidth(app.getResources().getDimensionPixelSize(R.dimen.map_route_buttons_width));
}
holder.valueText.setTextColor(activeColor); holder.valueText.setTextColor(activeColor);
holder.titleText.setTextColor(app.getResources().getColor(R.color.text_color_secondary_light)); holder.titleText.setTextColor(app.getResources().getColor(R.color.text_color_secondary_light));
holder.itemView.setOnClickListener(new View.OnClickListener() { holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
GPXTrackAnalysis analysis = displayItem.analysis; GPXTrackAnalysis analysis = displayItem != null ? displayItem.analysis : null;
if (displayItem != null && analysis != null) { if (analysis != null) {
ArrayList<GPXDataSetType> list = new ArrayList<>(); ArrayList<GPXDataSetType> list = new ArrayList<>();
if (analysis.hasElevationData || analysis.isSpeedSpecified() || analysis.hasSpeedData) { if (analysis.hasElevationData || analysis.isSpeedSpecified() || analysis.hasSpeedData) {
if (item.firstType != null) { if (item.firstType != null) {
@ -228,9 +250,16 @@ public class GpxBlockStatisticsBuilder {
} }
} }
}); });
setImageDrawable(holder.imageView, item.imageResId, item.imageColorId); Drawable icon = app.getUIUtilities().getIcon(item.imageResId, item.imageColorId);
holder.imageView.setImageDrawable(icon);
AndroidUtils.setBackgroundColor(app, holder.divider, nightMode, R.color.divider_color_light, R.color.divider_color_dark); AndroidUtils.setBackgroundColor(app, holder.divider, nightMode, R.color.divider_color_light, R.color.divider_color_dark);
AndroidUiHelper.updateVisibility(holder.divider, position != statBlocks.size() - 1); AndroidUiHelper.updateVisibility(holder.divider, position != items.size() - 1);
}
public void setItems(List<StatBlock> items) {
this.items.clear();
this.items.addAll(items);
notifyDataSetChanged();
} }
} }