Refactoring
This commit is contained in:
parent
cb45a78567
commit
8ddb64f299
5 changed files with 405 additions and 493 deletions
|
@ -1,18 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="map_widget_route_points">Route steps</string>
|
||||
<string name="route_points_no_gpx">No gpx</string>
|
||||
<string name="mark_as_current">Mark as current</string>
|
||||
<string name="marking_as_visited">Marking as visited…</string>
|
||||
<string name="current_route">Current route:</string>
|
||||
<string name="done">Done</string>
|
||||
<string name="marking_as_unvisited">Marking as unvisited…</string>
|
||||
<string name="map_widget_route_steps">Route steps</string>
|
||||
<string name="context_menu_item_show_route_points">Show route points</string>
|
||||
<string name="route_step_menu_mark_as_next">Mark as next</string>
|
||||
<string name="rp_current_route">Current route:</string>
|
||||
<string name="route_points_no_gpx">No route</string>
|
||||
<string name="rp_current_route_not_available">Please select a route</string>
|
||||
<string name="route_plugin_name">Navigate Route Plugin</string>
|
||||
<string name="route_plugin_descr">Navigate predefined GPX route with multiple points</string>
|
||||
<string name="mark_as_visited">Mark as visited</string>
|
||||
<string name="select_gpx">Select gpx</string>
|
||||
<string name="save_gpx">Save gpx</string>
|
||||
<string name="all_route_points">All route points</string>
|
||||
<string name="start_route">Start route</string>
|
||||
<string name="mark_as_not_visited">Mark as not-visited</string>
|
||||
<string name="mark_as_current">Navigate to</string>
|
||||
<string name="map_widget_route_points">Route points</string>
|
||||
|
||||
</resources>
|
|
@ -83,9 +83,10 @@ public abstract class OsmandPlugin {
|
|||
installedPlugins.add(parking);
|
||||
}
|
||||
|
||||
//routePointsPlugin routePointsPlugin = new RoutePointsPlugin(app);
|
||||
//installedPlugins.add(routePointsPlugin);
|
||||
//enablePlugin(app,routePointsPlugin,true);
|
||||
if(Version.isRouteNavPluginInlined(app)) {
|
||||
RoutePointsPlugin routePointsPlugin = new RoutePointsPlugin(app);
|
||||
installedPlugins.add(routePointsPlugin);
|
||||
}
|
||||
|
||||
installPlugin(OSMODROID_PLUGIN_COMPONENT, OsMoDroidPlugin.ID, app, new OsMoDroidPlugin(app));
|
||||
installedPlugins.add(new OsmEditingPlugin(app));
|
||||
|
|
|
@ -27,6 +27,8 @@ public abstract class OsmandListActivity extends SherlockListActivity {
|
|||
protected OsmandApplication getMyApplication() {
|
||||
return (OsmandApplication) getApplication();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
|
||||
|
|
|
@ -1,25 +1,37 @@
|
|||
package net.osmand.plus.routepointsnavigation;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.format.DateFormat;
|
||||
import android.util.Log;
|
||||
import android.view.*;
|
||||
import android.view.ActionMode;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.osmand.CallbackWithObject;
|
||||
import net.osmand.plus.*;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.GPXUtilities;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandPlugin;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.OsmandListActivity;
|
||||
import net.osmand.plus.helpers.GpxUiHelper;
|
||||
import net.osmand.util.MapUtils;
|
||||
import net.osmand.plus.routepointsnavigation.RoutePointsPlugin.RoutePoint;
|
||||
import net.osmand.plus.routepointsnavigation.RoutePointsPlugin.SelectedRouteGpxFile;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.SpannableString;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import com.actionbarsherlock.view.ActionMode;
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
import com.actionbarsherlock.view.Window;
|
||||
|
||||
/**
|
||||
* Created by Bars on 13.06.2014.
|
||||
|
@ -27,198 +39,89 @@ import java.util.*;
|
|||
*/
|
||||
public class RoutePointsActivity extends OsmandListActivity {
|
||||
|
||||
|
||||
private static final String CURRENT_ROUTE_KEY = "CurrentRoute";
|
||||
|
||||
private GPXUtilities.GPXFile gpx;
|
||||
private static final int OK_ID = 5;
|
||||
protected static final int MARK_AS_CURRENT_ID = 6;
|
||||
protected static final int AS_VISITED_ID = 7;
|
||||
protected static final int POI_ON_MAP_ID = 8;
|
||||
protected static final int GPX_SELECT_ID = 9;
|
||||
private RoutePointsPlugin plugin;
|
||||
private OsmandApplication app;
|
||||
|
||||
private List<GPXUtilities.WptPt> sortedPointsList;
|
||||
|
||||
//saves indexed of sorted list
|
||||
private List<Integer> pointsIndex;
|
||||
|
||||
private int selectedItemIndex;
|
||||
private RoutePoint selectedItem;
|
||||
private PointItemAdapter adapter;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.setContentView(R.layout.route_steps_main);
|
||||
this.app = (OsmandApplication) getApplication();
|
||||
getPlugin();
|
||||
getGpx(false);
|
||||
|
||||
if (gpx != null) {
|
||||
plugin = OsmandPlugin.getEnabledPlugin(RoutePointsPlugin.class);
|
||||
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
||||
super.onCreate(savedInstanceState);
|
||||
setSupportProgressBarIndeterminateVisibility(false);
|
||||
|
||||
super.setContentView(R.layout.route_steps_main);
|
||||
if (plugin.getCurrentRoute() == null) {
|
||||
selectGPX();
|
||||
} else {
|
||||
prepareView();
|
||||
}
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
|
||||
private void getPlugin() {
|
||||
List<OsmandPlugin> plugins = OsmandPlugin.getEnabledPlugins();
|
||||
for (OsmandPlugin plugin : plugins) {
|
||||
if (plugin instanceof RoutePointsPlugin) {
|
||||
this.plugin = (RoutePointsPlugin) plugin;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void getGpx(boolean forced) {
|
||||
if (plugin.getGpx() != null && !forced) {
|
||||
this.gpx = plugin.getGpx();
|
||||
return;
|
||||
}
|
||||
|
||||
private void selectGPX() {
|
||||
GpxUiHelper.selectGPXFile(this, false, false, new CallbackWithObject<GPXUtilities.GPXFile[]>() {
|
||||
@Override
|
||||
public boolean processResult(GPXUtilities.GPXFile[] result) {
|
||||
gpx = result[0];
|
||||
plugin.setGpx(gpx);
|
||||
plugin.setCurrentRoute(result[0]);
|
||||
SelectedRouteGpxFile sgpx = plugin.getCurrentRoute();
|
||||
sgpx.setNextPointToNavigate();
|
||||
prepareView();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void prepareView() {
|
||||
TextView gpxName = (TextView) findViewById(R.id.gpx_name);
|
||||
String fileName = getString(R.string.current_route) + " " + gpx.path.substring(gpx.path.lastIndexOf("/") + 1, gpx.path.lastIndexOf("."));
|
||||
gpxName.setText(fileName);
|
||||
gpxName.setOnClickListener(new View.OnClickListener() {
|
||||
SelectedRouteGpxFile route = plugin.getCurrentRoute();
|
||||
String fileName;
|
||||
if(route != null) {
|
||||
fileName = getString(R.string.rp_current_route) + " " + route.getName();
|
||||
} else {
|
||||
fileName = getString(R.string.rp_current_route_not_available);
|
||||
}
|
||||
SpannableString content = new SpannableString(fileName);
|
||||
content.setSpan(new ClickableSpan() {
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
getActivity().startActionMode(mGpxActionModeCallback);
|
||||
public void onClick(View widget) {
|
||||
getSherlock().startActionMode(mGpxActionModeCallback);
|
||||
}
|
||||
});
|
||||
}, 0, content.length(), 0);
|
||||
gpxName.setText(content);
|
||||
gpxName.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
|
||||
TextView visited = (TextView) findViewById(R.id.points_count);
|
||||
visited.setText("(" + plugin.getVisitedAllString() + ")");
|
||||
|
||||
loadCurrentRoute();
|
||||
sortPoints();
|
||||
displayListView();
|
||||
}
|
||||
|
||||
private void displayListView() {
|
||||
ArrayList<PointItem> pointItemsList = new ArrayList<PointItem>();
|
||||
for (int i = 0; i < sortedPointsList.size(); i++) {
|
||||
String pointName = sortedPointsList.get(i).name;
|
||||
long time = plugin.getPointStatus(pointsIndex.get(i));
|
||||
if (time != 0) {
|
||||
String dateString;
|
||||
Date date = new Date(time);
|
||||
if (DateFormat.is24HourFormat(app)) {
|
||||
dateString = DateFormat.format("MM/dd k:mm", date).toString();
|
||||
} else {
|
||||
dateString = DateFormat.format("MM/dd h:mm", date).toString() + DateFormat.format("aa", date).toString();
|
||||
}
|
||||
|
||||
pointItemsList.add(new PointItem(true, pointName, dateString));
|
||||
} else {
|
||||
if (i == 0) {
|
||||
pointItemsList.add(new PointItem(false, pointName, ""));
|
||||
continue;
|
||||
}
|
||||
GPXUtilities.WptPt first = sortedPointsList.get(i - 1);
|
||||
GPXUtilities.WptPt second = sortedPointsList.get(i);
|
||||
|
||||
double d = MapUtils.getDistance(first.lat, first.lon, second.lat, second.lon);
|
||||
String distance = OsmAndFormatter.getFormattedDistance((float) d, app);
|
||||
|
||||
pointItemsList.add(new PointItem(false, pointName, distance));
|
||||
}
|
||||
}
|
||||
|
||||
PointItemAdapter adapter = new PointItemAdapter(this, R.layout.route_point_info, pointItemsList);
|
||||
adapter = new PointItemAdapter(this, R.layout.route_point_info,
|
||||
route == null ? new ArrayList<RoutePoint>() :
|
||||
route.getCurrentPoints());
|
||||
ListView listView = getListView();
|
||||
listView.setAdapter(adapter);
|
||||
|
||||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
selectedItemIndex = i;
|
||||
view.setSelected(true);
|
||||
|
||||
getActivity().startActionMode(mPointActionModeCallback);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||
super.onListItemClick(l, v, position, id);
|
||||
RoutePoint rp = adapter.getItem(position);
|
||||
getSherlock().startActionMode(getPointActionModeCallback(rp));
|
||||
}
|
||||
|
||||
private void loadCurrentRoute() {
|
||||
if (gpx.routes.size() < 1) {
|
||||
return;
|
||||
}
|
||||
private class PointItemAdapter extends ArrayAdapter<RoutePoint> {
|
||||
|
||||
Map<String, String> map = gpx.getExtensionsToRead();
|
||||
if (map.containsKey(CURRENT_ROUTE_KEY)) {
|
||||
String routeName = map.get(CURRENT_ROUTE_KEY);
|
||||
|
||||
for (GPXUtilities.Route route : gpx.routes) {
|
||||
if (route.name.equals(routeName)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sortPoints() {
|
||||
sortedPointsList = plugin.getPoints();
|
||||
List<GPXUtilities.WptPt> listToSort = new ArrayList<GPXUtilities.WptPt>();
|
||||
List<Integer> indexItemsAtTheEnd = new ArrayList<Integer>();
|
||||
pointsIndex = new ArrayList<Integer>();
|
||||
int curPointInd = plugin.getCurrentPointIndex();
|
||||
|
||||
//current item should be first if it's exists
|
||||
if (curPointInd != -1) {
|
||||
pointsIndex.add(curPointInd);
|
||||
listToSort.add(plugin.getCurrentPoint());
|
||||
}
|
||||
|
||||
//all not visited points should be at the top
|
||||
for (int i = 0; i < sortedPointsList.size(); i++) {
|
||||
if (i == curPointInd) {
|
||||
continue;
|
||||
}
|
||||
long status = plugin.getPointStatus(i);
|
||||
if (status == 0L) {
|
||||
listToSort.add(sortedPointsList.get(i));
|
||||
pointsIndex.add(i);
|
||||
} else {
|
||||
indexItemsAtTheEnd.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
List<Long> timeOfVisits = new ArrayList<Long>();
|
||||
|
||||
for (Integer anIndexItemsAtTheEnd : indexItemsAtTheEnd) {
|
||||
timeOfVisits.add(plugin.getPointStatus(anIndexItemsAtTheEnd));
|
||||
}
|
||||
|
||||
//sorting visited point from earliest to latest
|
||||
quickSort(timeOfVisits, indexItemsAtTheEnd, 0, indexItemsAtTheEnd.size());
|
||||
//reverting items so they will be from latest to earliest
|
||||
Collections.reverse(indexItemsAtTheEnd);
|
||||
|
||||
for (int i : indexItemsAtTheEnd) {
|
||||
listToSort.add(sortedPointsList.get(i));
|
||||
pointsIndex.add(i);
|
||||
}
|
||||
|
||||
sortedPointsList = listToSort;
|
||||
}
|
||||
|
||||
private class PointItemAdapter extends ArrayAdapter<PointItem> {
|
||||
private ArrayList<PointItem> pointsList;
|
||||
|
||||
public PointItemAdapter(Context context, int textViewResourceId, ArrayList<PointItem> pointsList) {
|
||||
public PointItemAdapter(Context context, int textViewResourceId, List<RoutePoint> pointsList) {
|
||||
super(context, textViewResourceId, pointsList);
|
||||
this.pointsList = new ArrayList<PointItem>();
|
||||
this.pointsList.addAll(pointsList);
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
|
@ -230,7 +133,6 @@ public class RoutePointsActivity extends OsmandListActivity {
|
|||
@Override
|
||||
public View getView(final int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder holder = null;
|
||||
Log.v("ConvertView", String.valueOf(position));
|
||||
if (convertView == null) {
|
||||
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
convertView = vi.inflate(R.layout.route_point_info, null);
|
||||
|
@ -245,104 +147,64 @@ public class RoutePointsActivity extends OsmandListActivity {
|
|||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
PointItem point = pointsList.get(position);
|
||||
RoutePoint point = getItem(position);
|
||||
holder.name.setText(point.getName());
|
||||
holder.dateOrDistance.setText(String.valueOf(point.getTime()));
|
||||
if (point.isSelected()) {
|
||||
if (selectedItem == point) {
|
||||
convertView.setBackgroundColor(getResources().getColor(R.color.row_selection_color));
|
||||
} else {
|
||||
convertView.setBackgroundColor(Color.TRANSPARENT);
|
||||
}
|
||||
|
||||
if (point.isVisited()) {
|
||||
holder.image.setImageResource(R.drawable.ic_action_ok_dark);
|
||||
holder.name.setTextColor(getResources().getColor(R.color.osmbug_closed));
|
||||
holder.dateOrDistance.setTextColor(getResources().getColor(R.color.color_unknown));
|
||||
holder.dateOrDistance.setText(point.getTime());
|
||||
} else {
|
||||
if (sortedPointsList.get(position).equals(plugin.getCurrentPoint())) {
|
||||
if (point.isNextNavigate()) {
|
||||
holder.image.setImageResource(R.drawable.ic_action_signpost_dark);
|
||||
} else {
|
||||
holder.image.setImageResource(R.drawable.ic_action_marker_dark);
|
||||
}
|
||||
if(position > 0) {
|
||||
holder.dateOrDistance.setText(point.getDistance(getItem(position - 1)));
|
||||
} else {
|
||||
holder.dateOrDistance.setText("");
|
||||
}
|
||||
holder.name.setTextColor(getResources().getColor(R.color.color_update));
|
||||
holder.dateOrDistance.setTextColor(getResources().getColor(R.color.osmbug_not_submitted));
|
||||
holder.dateOrDistance.setTextColor(getResources().getColor(R.color.color_distance));
|
||||
}
|
||||
return convertView;
|
||||
}
|
||||
}
|
||||
|
||||
//this class needed to represent route point in UI
|
||||
private class PointItem {
|
||||
private boolean visited;
|
||||
private String name;
|
||||
private String time;
|
||||
|
||||
public PointItem(boolean visited, String name, String time) {
|
||||
this.visited = visited;
|
||||
this.name = name;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public boolean isSelected() {
|
||||
return visited;
|
||||
}
|
||||
|
||||
public void setSelected(boolean selected) {
|
||||
this.visited = selected;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void revertPointStatusAsync(final GPXUtilities.WptPt point) {
|
||||
new AsyncTask<GPXUtilities.WptPt, Void, Void>() {
|
||||
private ProgressDialog dlg;
|
||||
|
||||
private void saveGPXAsync() {
|
||||
new AsyncTask<SelectedRouteGpxFile, Void, Void>() {
|
||||
protected void onPreExecute() {
|
||||
dlg = new ProgressDialog(getActivity());
|
||||
if (plugin.getPointStatus(point) == 0) {
|
||||
dlg.setTitle(R.string.marking_as_visited);
|
||||
} else {
|
||||
dlg.setTitle(getString(R.string.marking_as_unvisited));
|
||||
}
|
||||
dlg.setMessage(point.name);
|
||||
dlg.show();
|
||||
getSherlock().setProgressBarIndeterminateVisibility(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(GPXUtilities.WptPt... params) {
|
||||
long status = plugin.getPointStatus(point);
|
||||
if (status == 0) {
|
||||
plugin.setPointStatus(point, true);
|
||||
} else {
|
||||
plugin.setPointStatus(point, false);
|
||||
protected Void doInBackground(SelectedRouteGpxFile... params) {
|
||||
if(plugin.getCurrentRoute() != null) {
|
||||
plugin.getCurrentRoute().saveFile();
|
||||
}
|
||||
GPXUtilities.writeGpxFile(new File(gpx.path), gpx, app);
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void onPostExecute(Void result) {
|
||||
//to avoid illegal argument exception when rotating phone during loading
|
||||
try {
|
||||
dlg.dismiss();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
sortPoints();
|
||||
displayListView();
|
||||
getSherlock().setProgressBarIndeterminateVisibility(false);
|
||||
|
||||
}
|
||||
}.execute(point);
|
||||
}.execute(plugin.getCurrentRoute());
|
||||
}
|
||||
|
||||
private ActionMode.Callback mGpxActionModeCallback = new ActionMode.Callback() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
|
||||
MenuItem item = menu.add(getString(R.string.select_gpx));
|
||||
item.setIcon(R.drawable.ic_action_layers_dark);
|
||||
item = menu.add(getString(R.string.start_route));
|
||||
item.setIcon(R.drawable.ic_action_map_marker_dark);
|
||||
createMenuItem(menu, GPX_SELECT_ID, R.string.select_gpx, R.drawable.ic_action_layers_light, R.drawable.ic_action_layers_dark,
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -353,136 +215,81 @@ public class RoutePointsActivity extends OsmandListActivity {
|
|||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
|
||||
if (menuItem.getTitle().equals(getResources().getString(R.string.select_gpx))) {
|
||||
getGpx(true);
|
||||
} else if (menuItem.getTitle().equals(getResources().getString(R.string.start_route))) {
|
||||
GPXUtilities.WptPt point = plugin.getCurrentPoint();
|
||||
if (point == null) {
|
||||
if (plugin.getPointStatus(pointsIndex.get(0)) == 0) {
|
||||
plugin.setCurrentPoint(sortedPointsList.get(0));
|
||||
}
|
||||
if (menuItem.getItemId() == GPX_SELECT_ID) {
|
||||
selectGPX();
|
||||
}
|
||||
actionMode.finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode actionMode) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
private ActionMode.Callback getPointActionModeCallback(final RoutePoint rp) {
|
||||
return new ActionMode.Callback() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
|
||||
selectedItem = rp;
|
||||
createMenuItem(menu, MARK_AS_CURRENT_ID, R.string.mark_as_current, R.drawable.ic_action_signpost_light, R.drawable.ic_action_signpost_dark,
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||
createMenuItem(menu, AS_VISITED_ID, !rp.isVisited() ?
|
||||
R.string.mark_as_visited : R.string.mark_as_not_visited, R.drawable.ic_action_ok_light, R.drawable.ic_action_ok_dark,
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||
createMenuItem(menu, POI_ON_MAP_ID, R.string.show_poi_on_map, R.drawable.ic_action_map_marker_light, R.drawable.ic_action_map_marker_dark,
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
|
||||
if (menuItem.getItemId() == MARK_AS_CURRENT_ID) {
|
||||
plugin.getCurrentRoute().setNextPointToNavigate(rp);
|
||||
saveGPXAsync();
|
||||
} else if (menuItem.getItemId() == POI_ON_MAP_ID) {
|
||||
LatLon point = rp.getPoint();
|
||||
app.getSettings().setMapLocationToShow(point.getLatitude(), point.getLongitude(),
|
||||
app.getSettings().getMapZoomToShow());
|
||||
finish();
|
||||
} else if (menuItem.getItemId() == AS_VISITED_ID) {
|
||||
// inverts selection state of item
|
||||
plugin.getCurrentRoute().markPoint(rp, !rp.isVisited());
|
||||
saveGPXAsync();
|
||||
}
|
||||
actionMode.finish();
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
actionMode.finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode actionMode) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
private ActionMode.Callback mPointActionModeCallback = new ActionMode.Callback() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
|
||||
MenuItem item = menu.add(getString(R.string.mark_as_current));
|
||||
item.setIcon(R.drawable.ic_action_signpost_dark);
|
||||
item = menu.add(getString(R.string.mark_as_visited));
|
||||
item.setIcon(R.drawable.ic_action_ok_dark);
|
||||
item = menu.add(getString(R.string.show_poi_on_map));
|
||||
item.setIcon(R.drawable.ic_action_map_marker_dark);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
|
||||
if (menuItem.getTitle().equals(getResources().getString(R.string.mark_as_current))) {
|
||||
plugin.setCurrentPoint(sortedPointsList.get(selectedItemIndex));
|
||||
sortPoints();
|
||||
displayListView();
|
||||
} else if (menuItem.getTitle().equals(getResources().getString(R.string.show_poi_on_map))) {
|
||||
GPXUtilities.WptPt point = sortedPointsList.get(selectedItemIndex);
|
||||
app.getSettings().setMapLocationToShow(point.lat, point.lon, app.getSettings().getMapZoomToShow());
|
||||
finish();
|
||||
} else {
|
||||
//inverts selection state of item
|
||||
revertPointStatusAsync(sortedPointsList.get(selectedItemIndex));
|
||||
sortPoints();
|
||||
displayListView();
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode actionMode) {
|
||||
selectedItem = null;
|
||||
}
|
||||
actionMode.finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode actionMode) {
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
|
||||
com.actionbarsherlock.view.MenuItem doneItem = menu.add(getString(R.string.done));
|
||||
doneItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||
doneItem.setIcon(R.drawable.ic_action_map_marker_dark);
|
||||
|
||||
createMenuItem(menu, OK_ID, R.string.default_buttons_ok,
|
||||
R.drawable.ic_action_map_marker_light, R.drawable.ic_action_map_marker_dark ,
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
|
||||
if (item.getTitle().equals(getString(R.string.done))) {
|
||||
GPXUtilities.WptPt point = plugin.getCurrentPoint();
|
||||
app.getSettings().setMapLocationToShow(point.lat, point.lon, app.getSettings().getMapZoomToShow());
|
||||
if (item.getItemId() == OK_ID) {
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private void quickSort(List<Long> valuesList, List<Integer> indexList, int beginIdx, int len) {
|
||||
if (len <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int endIdx = beginIdx + len - 1;
|
||||
|
||||
// Pivot selection
|
||||
|
||||
final int pivotPos = beginIdx + len / 2;
|
||||
|
||||
final long pivot = valuesList.get(pivotPos);
|
||||
|
||||
swap(valuesList, indexList, pivotPos, endIdx);
|
||||
|
||||
|
||||
// partitioning
|
||||
int p = beginIdx;
|
||||
|
||||
for (int i = beginIdx; i != endIdx; ++i) {
|
||||
if (valuesList.get(i) <= pivot) {
|
||||
swap(valuesList, indexList, i, p++);
|
||||
}
|
||||
}
|
||||
|
||||
swap(valuesList, indexList, p, endIdx);
|
||||
|
||||
// recursive call
|
||||
quickSort(valuesList, indexList, beginIdx, p - beginIdx);
|
||||
quickSort(valuesList, indexList, p + 1, endIdx - p);
|
||||
|
||||
}
|
||||
|
||||
private void swap(List<Long> valuesList, List<Integer> indexList, int piviotPos, int endIndex) {
|
||||
long value = valuesList.get(piviotPos);
|
||||
valuesList.set(piviotPos, valuesList.get(endIndex));
|
||||
valuesList.set(endIndex, value);
|
||||
|
||||
int index = indexList.get(piviotPos);
|
||||
indexList.set(piviotPos, indexList.get(endIndex));
|
||||
indexList.set(endIndex, index);
|
||||
|
||||
}
|
||||
|
||||
private Activity getActivity() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,32 @@
|
|||
package net.osmand.plus.routepointsnavigation;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Paint;
|
||||
import android.view.View;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.*;
|
||||
import net.osmand.plus.ApplicationMode;
|
||||
import net.osmand.plus.GPXUtilities;
|
||||
import net.osmand.plus.GPXUtilities.GPXFile;
|
||||
import net.osmand.plus.GPXUtilities.Route;
|
||||
import net.osmand.plus.GPXUtilities.WptPt;
|
||||
import net.osmand.plus.OsmAndFormatter;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandPlugin;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.TargetPointsHelper;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.views.MapInfoLayer;
|
||||
import net.osmand.plus.views.OsmandMapLayer;
|
||||
import net.osmand.plus.views.OsmandMapTileView;
|
||||
import net.osmand.plus.views.mapwidgets.TextInfoWidget;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.osmand.util.MapUtils;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Paint;
|
||||
import android.text.format.DateFormat;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* Created by Barsik on 10.06.2014.
|
||||
|
@ -22,42 +35,30 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
|
||||
public static final String ID = "osmand.route.stepsPlugin";
|
||||
|
||||
private static final String VISITED_KEY = "IsVisited";
|
||||
private static final String POINT_KEY = "Point";
|
||||
private static final String VISITED_KEY = "VISITED_KEY";
|
||||
|
||||
private OsmandApplication app;
|
||||
private GPXUtilities.GPXFile gpx;
|
||||
private GPXUtilities.Route currentRoute;
|
||||
private GPXUtilities.WptPt currentPoint;
|
||||
private List<GPXUtilities.WptPt> pointsList;
|
||||
private TextInfoWidget routeStepsControl;
|
||||
|
||||
private int visitedCount;
|
||||
private int currentPointIndex = -1;
|
||||
|
||||
|
||||
private SelectedRouteGpxFile currentRoute;
|
||||
|
||||
public RoutePointsPlugin(OsmandApplication app) {
|
||||
ApplicationMode.regWidget("route_steps", (ApplicationMode[]) null);
|
||||
ApplicationMode.regWidget("route_steps", ApplicationMode.CAR, ApplicationMode.DEFAULT);
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
public void setCurrentPoint(GPXUtilities.WptPt point) {
|
||||
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
|
||||
LatLon latLon = new LatLon(point.lat, point.lon);
|
||||
targetPointsHelper.navigateToPoint(latLon, true, -1,":" + point.name);
|
||||
getCurrentPoint();
|
||||
public SelectedRouteGpxFile getCurrentRoute() {
|
||||
return currentRoute;
|
||||
}
|
||||
|
||||
public void setCurrentPoint(int number) {
|
||||
GPXUtilities.WptPt point = pointsList.get(number);
|
||||
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
|
||||
LatLon latLon = new LatLon(point.lat, point.lon);
|
||||
targetPointsHelper.navigateToPoint(latLon, true, -1, point.name);
|
||||
}
|
||||
|
||||
public List<GPXUtilities.WptPt> getPoints() {
|
||||
return currentRoute.points;
|
||||
|
||||
public void setCurrentRoute(GPXFile gpx) {
|
||||
if(gpx == null) {
|
||||
currentRoute = null;
|
||||
} else {
|
||||
currentRoute = new SelectedRouteGpxFile(gpx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
|
@ -66,12 +67,12 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "This plugin allows you to view key positions of your route...";
|
||||
return app.getString(R.string.route_plugin_descr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Tour Point Plugin";
|
||||
return app.getString(R.string.route_plugin_name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -79,33 +80,6 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
return true;
|
||||
}
|
||||
|
||||
public GPXUtilities.WptPt getCurrentPoint() {
|
||||
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
|
||||
String locName = targetPointsHelper.getPointNavigateDescription();
|
||||
for (int i = 0; i < pointsList.size(); i++) {
|
||||
String pointName = ":" + pointsList.get(i).name;
|
||||
if (pointName.equals(locName)) {
|
||||
currentPoint = pointsList.get(i);
|
||||
currentPointIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return currentPoint;
|
||||
}
|
||||
|
||||
public GPXUtilities.GPXFile getGpx() {
|
||||
return gpx;
|
||||
}
|
||||
|
||||
public void setGpx(GPXUtilities.GPXFile gpx) {
|
||||
this.gpx = gpx;
|
||||
currentRoute = gpx.routes.get(0);
|
||||
pointsList = currentRoute.points;
|
||||
refreshPointsStatus();
|
||||
getCurrentPoint();
|
||||
}
|
||||
|
||||
private void registerWidget(MapActivity activity) {
|
||||
MapInfoLayer mapInfoLayer = activity.getMapLayers().getMapInfoLayer();
|
||||
if (mapInfoLayer != null) {
|
||||
|
@ -116,24 +90,6 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
public GPXUtilities.WptPt getNextPoint() {
|
||||
if (pointsList.size() > currentPointIndex + 1) {
|
||||
return pointsList.get(currentPointIndex + 1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int findPointPosition(GPXUtilities.WptPt point) {
|
||||
int i = 0;
|
||||
for (GPXUtilities.WptPt item : pointsList) {
|
||||
if (item.equals(point)) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateLayers(OsmandMapTileView mapView, MapActivity activity) {
|
||||
|
@ -144,7 +100,12 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
}
|
||||
|
||||
public String getVisitedAllString() {
|
||||
return String.valueOf(visitedCount) + "/" + String.valueOf(pointsList.size());
|
||||
if (currentRoute != null) {
|
||||
return String.valueOf(currentRoute.getVisitedCount()) + "/" + String.valueOf(currentRoute.getCount());
|
||||
} else {
|
||||
return app.getString(R.string.route_points_no_gpx);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private TextInfoWidget createRouteStepsInfoControl(final MapActivity map, Paint paintText, Paint paintSubText) {
|
||||
|
@ -152,12 +113,7 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
|
||||
@Override()
|
||||
public boolean updateInfo(OsmandMapLayer.DrawSettings drawSettings) {
|
||||
if (gpx != null) {
|
||||
setText(String.valueOf(visitedCount) + "/", String.valueOf(pointsList.size()));
|
||||
} else {
|
||||
setText(app.getString(R.string.route_points_no_gpx), "");
|
||||
|
||||
}
|
||||
setText(getVisitedAllString(), "");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -175,49 +131,200 @@ public class RoutePointsPlugin extends OsmandPlugin {
|
|||
return routeStepsControl;
|
||||
}
|
||||
|
||||
public void refreshPointsStatus() {
|
||||
visitedCount = 0;
|
||||
for (int i = 0; i < pointsList.size(); i++) {
|
||||
if (getPointStatus(i) != 0) {
|
||||
visitedCount++;
|
||||
|
||||
public class RoutePoint implements Comparable<RoutePoint> {
|
||||
boolean isNextNavigate;
|
||||
int gpxOrder;
|
||||
long visitedTime; // 0 not visited
|
||||
WptPt wpt;
|
||||
|
||||
public String getName() {
|
||||
return wpt.name;
|
||||
}
|
||||
|
||||
public WptPt getWpt() {
|
||||
return wpt;
|
||||
}
|
||||
|
||||
public boolean isNextNavigate() {
|
||||
return isNextNavigate;
|
||||
}
|
||||
public boolean isVisited() {
|
||||
return visitedTime != 0;
|
||||
}
|
||||
|
||||
public int getGpxOrder() {
|
||||
return gpxOrder;
|
||||
}
|
||||
|
||||
public String getDistance(RoutePoint rp) {
|
||||
double d = MapUtils.getDistance(rp.getPoint(), getPoint());
|
||||
String distance = OsmAndFormatter.getFormattedDistance((float) d, app);
|
||||
return distance;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
if(visitedTime == 0) {
|
||||
return "";
|
||||
}
|
||||
String dateString;
|
||||
Date date = new Date(visitedTime);
|
||||
if (DateFormat.is24HourFormat(app)) {
|
||||
dateString = DateFormat.format("MM/dd k:mm", date).toString();
|
||||
} else {
|
||||
dateString = DateFormat.format("MM/dd h:mm", date).toString() + DateFormat.format("aa", date).toString();
|
||||
}
|
||||
return dateString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(RoutePoint another) {
|
||||
if(isNextNavigate) {
|
||||
return -1;
|
||||
}
|
||||
// this point is not visited
|
||||
if(!isVisited()) {
|
||||
if(another.isNextNavigate) {
|
||||
return 1;
|
||||
}
|
||||
if(another.isVisited()) {
|
||||
return -1;
|
||||
}
|
||||
return Integer.compare(gpxOrder, another.gpxOrder);
|
||||
}
|
||||
// this point is visited
|
||||
if(!another.isVisited()) {
|
||||
return 1;
|
||||
}
|
||||
return -Long.compare(visitedTime, another.visitedTime);
|
||||
}
|
||||
|
||||
public LatLon getPoint() {
|
||||
return new LatLon(wpt.lat, wpt.lon);
|
||||
}
|
||||
|
||||
public void setVisitedTime(long currentTimeMillis) {
|
||||
visitedTime = currentTimeMillis;
|
||||
wpt.getExtensionsToWrite().put(VISITED_KEY, visitedTime+"");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class SelectedRouteGpxFile {
|
||||
private GPXUtilities.GPXFile gpx;
|
||||
private List<RoutePoint> currentPoints = new ArrayList<RoutePointsPlugin.RoutePoint>();
|
||||
|
||||
|
||||
public SelectedRouteGpxFile(GPXUtilities.GPXFile gpx) {
|
||||
this.gpx = gpx;
|
||||
parseGPXFile(gpx);
|
||||
}
|
||||
|
||||
public List<RoutePoint> getCurrentPoints() {
|
||||
return currentPoints;
|
||||
}
|
||||
|
||||
public int getVisitedCount() {
|
||||
int k = 0;
|
||||
for(RoutePoint rp : currentPoints) {
|
||||
if(!rp.isVisited()) {
|
||||
k++;
|
||||
}
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return currentPoints.size();
|
||||
}
|
||||
|
||||
public GPXUtilities.Route getRoute() {
|
||||
if(gpx.routes.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return gpx.routes.get(0);
|
||||
}
|
||||
|
||||
public String saveFile() {
|
||||
return GPXUtilities.writeGpxFile(new File(gpx.path), gpx, app);
|
||||
}
|
||||
|
||||
public void markPoint(RoutePoint point, boolean visited) {
|
||||
if(point.isNextNavigate() && visited) {
|
||||
setNextPointToNavigate();
|
||||
return;
|
||||
}
|
||||
if(visited) {
|
||||
point.setVisitedTime(System.currentTimeMillis());
|
||||
} else {
|
||||
point.setVisitedTime(0);
|
||||
}
|
||||
sortPoints();
|
||||
}
|
||||
|
||||
public void setNextPointToNavigate() {
|
||||
if(!currentPoints.isEmpty()) {
|
||||
RoutePoint rp = currentPoints.get(0);
|
||||
if(rp.isNextNavigate) {
|
||||
rp.setVisitedTime(System.currentTimeMillis());
|
||||
sortPoints();
|
||||
}
|
||||
RoutePoint first = currentPoints.get(0);
|
||||
if(!first.isVisited()) {
|
||||
app.getTargetPointsHelper().navigateToPoint(first.getPoint(), true, -1, first.getName());
|
||||
first.isNextNavigate = true;
|
||||
} else {
|
||||
app.getTargetPointsHelper().clearPointToNavigate(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public long getPointStatus(GPXUtilities.WptPt point) {
|
||||
return getPointStatus(findPointPosition(point));
|
||||
}
|
||||
|
||||
public long getPointStatus(int numberOfPoint) {
|
||||
Map<String, String> map = currentRoute.getExtensionsToRead();
|
||||
|
||||
String mapKey = POINT_KEY + numberOfPoint + VISITED_KEY;
|
||||
if (map.containsKey(mapKey)) {
|
||||
String value = map.get(mapKey);
|
||||
return (Long.valueOf(value));
|
||||
private void sortPoints() {
|
||||
Collections.sort(currentPoints);
|
||||
}
|
||||
|
||||
return 0L;
|
||||
}
|
||||
|
||||
//saves point status value to gpx extention file
|
||||
public void setPointStatus(GPXUtilities.WptPt point, boolean status) {
|
||||
Map<String, String> map = currentRoute.getExtensionsToWrite();
|
||||
int pos = findPointPosition(point);
|
||||
String mapKey = POINT_KEY + pos + VISITED_KEY;
|
||||
if (status) {
|
||||
//value is current time
|
||||
Calendar c = Calendar.getInstance();
|
||||
long number = c.getTimeInMillis();
|
||||
map.put(mapKey, String.valueOf(number));
|
||||
} else if (map.containsKey(mapKey)) {
|
||||
map.remove(mapKey);
|
||||
|
||||
private void parseGPXFile(GPXFile gpx) {
|
||||
this.gpx = gpx;
|
||||
Route rt = getRoute();
|
||||
currentPoints.clear();
|
||||
if(rt != null) {
|
||||
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
|
||||
String locName = targetPointsHelper.getPointNavigateDescription();
|
||||
for(int i = 0; i < rt.points.size(); i++) {
|
||||
WptPt wptPt = rt.points.get(i);
|
||||
RoutePoint rtp = new RoutePoint();
|
||||
rtp.gpxOrder = i;
|
||||
String time = wptPt.getExtensionsToRead().get(VISITED_KEY);
|
||||
try {
|
||||
rtp.visitedTime = Long.parseLong(time);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
rtp.isNextNavigate = rtp.visitedTime == 0 && locName != null && locName.equals(wptPt.name);
|
||||
if(rtp.isNextNavigate) {
|
||||
locName = null;
|
||||
}
|
||||
|
||||
}
|
||||
sortPoints();
|
||||
}
|
||||
}
|
||||
|
||||
refreshPointsStatus();
|
||||
}
|
||||
public String getName() {
|
||||
return gpx.path.substring(gpx.path.lastIndexOf("/") + 1, gpx.path.lastIndexOf("."));
|
||||
}
|
||||
|
||||
public Integer getCurrentPointIndex() {
|
||||
return currentPointIndex;
|
||||
public void setNextPointToNavigate(RoutePoint rp) {
|
||||
if (!currentPoints.isEmpty()) {
|
||||
if (currentPoints.get(0).isNextNavigate()) {
|
||||
currentPoints.get(0).isNextNavigate = false;
|
||||
}
|
||||
}
|
||||
rp.isNextNavigate = true;
|
||||
sortPoints();
|
||||
app.getTargetPointsHelper().navigateToPoint(rp.getPoint(), true, -1, rp.getName());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue