Search in sources :

Example 31 with TargetPoint

use of net.osmand.plus.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class IntermediatePointsDialog method applySortTargets.

private static void applySortTargets(AlertDialog dlg, final Activity activity, final List<TargetPoint> intermediates, final TIntArrayList originalPositions, final ArrayAdapter<TargetPoint> listadapter, final ProgressBar pb, final TextView textInfo) {
    dlg.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    new AsyncTask<Void, Void, int[]>() {

                        protected void onPreExecute() {
                            pb.setVisibility(View.VISIBLE);
                            textInfo.setVisibility(View.VISIBLE);
                        }

                        protected int[] doInBackground(Void[] params) {
                            OsmandApplication app = (OsmandApplication) activity.getApplication();
                            Location cll = app.getLocationProvider().getLastKnownLocation();
                            ArrayList<TargetPoint> lt = new ArrayList<TargetPoint>(intermediates);
                            TargetPoint start;
                            if (cll != null) {
                                LatLon ll = new LatLon(cll.getLatitude(), cll.getLongitude());
                                start = TargetPoint.create(ll, null);
                            } else if (app.getTargetPointsHelper().getPointToStart() != null) {
                                TargetPoint ps = app.getTargetPointsHelper().getPointToStart();
                                LatLon ll = new LatLon(ps.getLatitude(), ps.getLongitude());
                                start = TargetPoint.create(ll, null);
                            // } else if(activity instanceof MapActivity) {
                            // LatLon ll = new LatLon(((MapActivity) activity).getMapView().getLatitude(), ((MapActivity) activity).getMapView().getLongitude());
                            // start = TargetPoint.create(ll, null);
                            } else {
                                start = lt.get(0);
                            }
                            TargetPoint end = lt.remove(lt.size() - 1);
                            ArrayList<LatLon> al = new ArrayList<LatLon>();
                            for (TargetPoint p : lt) {
                                al.add(p.point);
                            }
                            return new TspAnt().readGraph(al, start.point, end.point).solve();
                        }

                        protected void onPostExecute(int[] result) {
                            pb.setVisibility(View.GONE);
                            List<TargetPoint> alocs = new ArrayList<TargetPoint>();
                            TIntArrayList newOriginalPositions = new TIntArrayList();
                            for (int i = 0; i < result.length; i++) {
                                if (result[i] > 0) {
                                    TargetPoint loc = intermediates.get(result[i] - 1);
                                    alocs.add(loc);
                                    newOriginalPositions.add(originalPositions.get(intermediates.indexOf(loc)));
                                }
                            }
                            intermediates.clear();
                            intermediates.addAll(alocs);
                            originalPositions.clear();
                            originalPositions.addAll(newOriginalPositions);
                            listadapter.notifyDataSetChanged();
                        }
                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
                }
            });
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) OsmandApplication(net.osmand.plus.OsmandApplication) DialogInterface(android.content.DialogInterface) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TspAnt(net.osmand.TspAnt) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) OnShowListener(android.content.DialogInterface.OnShowListener) TIntArrayList(gnu.trove.list.array.TIntArrayList) LatLon(net.osmand.data.LatLon) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Location(net.osmand.Location)

Example 32 with TargetPoint

use of net.osmand.plus.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class IntermediatePointsDialog method openIntermediatePointsDialog.

public static void openIntermediatePointsDialog(final Activity activity, final OsmandApplication app, final boolean changeOrder) {
    TargetPointsHelper targets = app.getTargetPointsHelper();
    final List<TargetPoint> intermediates = targets.getIntermediatePointsWithTarget();
    final TIntArrayList originalPositions = new TIntArrayList(intermediates.size());
    for (int j = 1; j <= intermediates.size(); j++) {
        originalPositions.add(j);
    }
    final boolean[] checkedIntermediates = new boolean[intermediates.size()];
    Arrays.fill(checkedIntermediates, true);
    final ArrayAdapter<TargetPoint> listadapter = getListAdapter(app, activity, changeOrder, intermediates, originalPositions, checkedIntermediates);
    ListView lv = new ListView(activity);
    View contentView = lv;
    final ProgressBar pb = new ProgressBar(activity);
    pb.setVisibility(View.GONE);
    final TextView textInfo = new TextView(activity);
    textInfo.setText(R.string.intermediate_items_sort_return);
    textInfo.setVisibility(View.GONE);
    if (changeOrder) {
        LinearLayout ll = new LinearLayout(activity);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView(lv);
        ll.addView(pb);
        ll.addView(textInfo);
        contentView = ll;
    // lv.addFooterView(pb);
    // lv.addFooterView(textInfo);
    }
    lv.setAdapter(listadapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (activity instanceof MapActivity) {
                // AnimateDraggingMapThread thread = mapActivity.getMapView().getAnimatedDraggingThread();
                TargetPoint pointToNavigate = intermediates.get(position);
                int fZoom = ((MapActivity) activity).getMapView().getZoom() < 15 ? 15 : ((MapActivity) activity).getMapView().getZoom();
                // thread.startMoving(pointToNavigate.getLatitude(), pointToNavigate.getLongitude(), fZoom, true);
                ((MapActivity) activity).getMapView().setIntZoom(fZoom);
                ((MapActivity) activity).getMapView().setLatLon(pointToNavigate.getLatitude(), pointToNavigate.getLongitude());
                listadapter.notifyDataSetInvalidated();
            }
        }
    });
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setView(contentView);
    builder.setInverseBackgroundForced(true);
    lv.setBackgroundColor(Color.WHITE);
    builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (changeOrder) {
                commitChangePointsOrder(app, intermediates);
            } else {
                commitPointsRemoval(app, checkedIntermediates);
            }
        }
    });
    if (!changeOrder && intermediates.size() > 1) {
        builder.setNeutralButton(R.string.intermediate_points_change_order, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                openIntermediatePointsDialog(activity, app, true);
            }
        });
    } else if (intermediates.size() > 1) {
        builder.setNeutralButton(R.string.intermediate_items_sort_by_distance, new Dialog.OnClickListener() {

            @Override
            public void onClick(DialogInterface d, int which) {
            // Do nothing here. We override the onclick
            }
        });
    }
    AlertDialog dlg = builder.create();
    if (changeOrder) {
        applySortTargets(dlg, activity, intermediates, originalPositions, listadapter, pb, textInfo);
    }
    dlg.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) DialogInterface(android.content.DialogInterface) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) TIntArrayList(gnu.trove.list.array.TIntArrayList) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) ListView(android.widget.ListView) TextView(android.widget.TextView) TargetPointsHelper(net.osmand.plus.TargetPointsHelper) ProgressBar(android.widget.ProgressBar) LinearLayout(android.widget.LinearLayout)

Example 33 with TargetPoint

use of net.osmand.plus.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class IntermediatePointsDialog method getListAdapter.

private static ArrayAdapter<TargetPoint> getListAdapter(final OsmandApplication app, final Activity activity, final boolean changeOrder, final List<TargetPoint> intermediates, final TIntArrayList originalPositions, final boolean[] checkedIntermediates) {
    final int padding = (int) (12 * activity.getResources().getDisplayMetrics().density + 0.5f);
    final ArrayAdapter<TargetPoint> listadapter = new ArrayAdapter<TargetPoint>(app, changeOrder ? R.layout.change_order_item : R.layout.list_menu_item_native, R.id.title, intermediates) {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // User super class to create the View
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView) v.findViewById(R.id.title);
            String nm = originalPositions.get(position) + ". ";
            TargetPoint tp = intermediates.get(position);
            String distString = "";
            if (activity instanceof MapActivity) {
                double lat = ((MapActivity) activity).getMapView().getLatitude();
                double lon = ((MapActivity) activity).getMapView().getLongitude();
                double meters = MapUtils.getDistance(tp.point, lat, lon);
                distString = OsmAndFormatter.getFormattedDistance((float) meters, app);
            }
            if (position < intermediates.size() - 1) {
                nm += app.getString(R.string.target_point, distString);
            } else {
                nm += app.getString(R.string.destination_point, distString);
            }
            String descr = tp.getOnlyName();
            if (descr != null && descr.trim().length() > 0) {
                nm += "\n" + descr;
            }
            tv.setText(nm);
            if (changeOrder) {
                ((ImageButton) v.findViewById(R.id.up)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (position > 0) {
                            TargetPoint old = intermediates.remove(position - 1);
                            int oldI = originalPositions.removeAt(position - 1);
                            intermediates.add(position, old);
                            originalPositions.insert(position, oldI);
                            notifyDataSetInvalidated();
                        }
                    }
                });
                ((ImageButton) v.findViewById(R.id.down)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (position < intermediates.size() - 1) {
                            TargetPoint old = intermediates.remove(position + 1);
                            int oldI = originalPositions.removeAt(position + 1);
                            intermediates.add(position, old);
                            originalPositions.insert(position, oldI);
                            notifyDataSetInvalidated();
                        }
                    }
                });
            } else {
                int icon = position == intermediates.size() - 1 ? R.drawable.ic_action_target : R.drawable.ic_action_intermediate;
                tv.setCompoundDrawablesWithIntrinsicBounds(app.getIconsCache().getThemedIcon(icon), null, null, null);
                tv.setCompoundDrawablePadding(padding);
                final CheckBox ch = ((CheckBox) v.findViewById(R.id.toggle_item));
                ch.setVisibility(View.VISIBLE);
                ch.setOnCheckedChangeListener(null);
                ch.setChecked(checkedIntermediates[position]);
                ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        checkedIntermediates[position] = isChecked;
                    }
                });
            }
            return v;
        }
    };
    return listadapter;
}
Also used : OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) ViewGroup(android.view.ViewGroup) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) ImageButton(android.widget.ImageButton) CheckBox(android.widget.CheckBox) TextView(android.widget.TextView) ArrayAdapter(android.widget.ArrayAdapter) CompoundButton(android.widget.CompoundButton)

Example 34 with TargetPoint

use of net.osmand.plus.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class RouteProvider method createOsmandRouterGPX.

public GPXFile createOsmandRouterGPX(RouteCalculationResult srcRoute, OsmandApplication ctx) {
    TargetPointsHelper helper = ctx.getTargetPointsHelper();
    int currentRoute = srcRoute.currentRoute;
    List<Location> routeNodes = srcRoute.getImmutableAllLocations();
    List<RouteDirectionInfo> directionInfo = srcRoute.getImmutableAllDirections();
    int currentDirectionInfo = srcRoute.currentDirectionInfo;
    GPXFile gpx = new GPXFile();
    gpx.author = OSMAND_ROUTER;
    Track track = new Track();
    gpx.tracks.add(track);
    TrkSegment trkSegment = new TrkSegment();
    track.segments.add(trkSegment);
    int cRoute = currentRoute;
    int cDirInfo = currentDirectionInfo;
    // Save the start point to gpx file's trkpt section unless already contained
    WptPt startpoint = new WptPt();
    TargetPoint sc = helper.getPointToStart();
    int routePointOffsetAdjusted = 0;
    if (sc != null && ((float) sc.getLatitude() != (float) routeNodes.get(cRoute).getLatitude() || (float) sc.getLongitude() != (float) routeNodes.get(cRoute).getLongitude())) {
        startpoint.lat = sc.getLatitude();
        startpoint.lon = sc.getLongitude();
        trkSegment.points.add(startpoint);
        if (directionInfo != null && !directionInfo.isEmpty()) {
            for (RouteDirectionInfo i : directionInfo) {
                i.routePointOffset++;
            }
        }
        routePointOffsetAdjusted = 1;
    }
    for (int i = cRoute; i < routeNodes.size(); i++) {
        Location loc = routeNodes.get(i);
        WptPt pt = new WptPt();
        pt.lat = loc.getLatitude();
        pt.lon = loc.getLongitude();
        if (loc.hasSpeed()) {
            pt.speed = loc.getSpeed();
        }
        if (loc.hasAltitude()) {
            pt.ele = loc.getAltitude();
        }
        if (loc.hasAccuracy()) {
            pt.hdop = loc.getAccuracy();
        }
        trkSegment.points.add(pt);
    }
    Route route = new Route();
    gpx.routes.add(route);
    for (int i = cDirInfo; i < directionInfo.size(); i++) {
        RouteDirectionInfo dirInfo = directionInfo.get(i);
        if (dirInfo.routePointOffset - routePointOffsetAdjusted >= cRoute) {
            if (dirInfo.getTurnType() != null && !dirInfo.getTurnType().isSkipToSpeak() && dirInfo.routePointOffset - routePointOffsetAdjusted < routeNodes.size()) {
                Location loc = routeNodes.get(dirInfo.routePointOffset - routePointOffsetAdjusted);
                WptPt pt = new WptPt();
                pt.lat = loc.getLatitude();
                pt.lon = loc.getLongitude();
                // Collect distances and times for subsequent suppressed turns
                int collectedDistance = 0;
                int collectedTime = 0;
                for (int j = i + 1; j < directionInfo.size(); j++) {
                    if (directionInfo.get(j).getTurnType() != null && directionInfo.get(j).getTurnType().isSkipToSpeak()) {
                        collectedDistance += directionInfo.get(j).getDistance();
                        collectedTime += directionInfo.get(j).getExpectedTime();
                    } else {
                        break;
                    }
                }
                pt.desc = dirInfo.getDescriptionRoute(ctx, collectedDistance + dirInfo.getDistance());
                Map<String, String> extensions = pt.getExtensionsToWrite();
                extensions.put("time", (collectedTime + dirInfo.getExpectedTime()) + "");
                int turnType = dirInfo.getTurnType().getValue();
                if (TurnType.C != turnType) {
                    extensions.put("turn", dirInfo.getTurnType().toXmlString());
                    extensions.put("turn-angle", dirInfo.getTurnType().getTurnAngle() + "");
                }
                extensions.put("offset", (dirInfo.routePointOffset - cRoute) + "");
                // Issue #2894
                if (dirInfo.getRef() != null && !"null".equals(dirInfo.getRef())) {
                    extensions.put("ref", dirInfo.getRef() + "");
                }
                if (dirInfo.getStreetName() != null && !"null".equals(dirInfo.getStreetName())) {
                    extensions.put("street-name", dirInfo.getStreetName() + "");
                }
                if (dirInfo.getDestinationName() != null && !"null".equals(dirInfo.getDestinationName())) {
                    extensions.put("dest", dirInfo.getDestinationName() + "");
                }
                route.points.add(pt);
            }
        }
    }
    List<TargetPoint> ps = helper.getIntermediatePointsWithTarget();
    for (int k = 0; k < ps.size(); k++) {
        WptPt pt = new WptPt();
        pt.lat = ps.get(k).getLatitude();
        pt.lon = ps.get(k).getLongitude();
        if (k < ps.size()) {
            pt.name = ps.get(k).getOnlyName() + "";
            if (k == ps.size() - 1) {
                String target = ctx.getString(R.string.destination_point, "");
                if (pt.name.startsWith(target)) {
                    pt.name = ctx.getString(R.string.destination_point, pt.name);
                }
            } else {
                String prefix = (k + 1) + ". ";
                if (Algorithms.isEmpty(pt.name)) {
                    pt.name = ctx.getString(R.string.target_point, pt.name);
                }
                if (pt.name.startsWith(prefix)) {
                    pt.name = prefix + pt.name;
                }
            }
            pt.desc = pt.name;
        }
        gpx.addPoint(pt);
    }
    return gpx;
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) LocationPoint(net.osmand.data.LocationPoint) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) TargetPointsHelper(net.osmand.plus.TargetPointsHelper) Track(net.osmand.plus.GPXUtilities.Track) Route(net.osmand.plus.GPXUtilities.Route) Location(net.osmand.Location)

Example 35 with TargetPoint

use of net.osmand.plus.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class MapControlsLayer method startRoutePlanningWithDestination.

private void startRoutePlanningWithDestination(LatLon latLon, PointDescription pointDescription, TargetPointsHelper targets) {
    boolean hasPointToStart = settings.restorePointToStart();
    targets.navigateToPoint(latLon, true, -1, pointDescription);
    if (!hasPointToStart) {
        mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(null, null, null, true, true);
    } else {
        TargetPoint start = targets.getPointToStart();
        if (start != null) {
            mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(null, start.point, start.getOriginalPointDescription(), true, true);
        } else {
            mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(null, null, null, true, true);
        }
    }
}
Also used : TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint)

Aggregations

TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)39 TargetPointsHelper (net.osmand.plus.TargetPointsHelper)18 LatLon (net.osmand.data.LatLon)17 View (android.view.View)13 TextView (android.widget.TextView)13 Location (net.osmand.Location)10 PointDescription (net.osmand.data.PointDescription)10 AdapterView (android.widget.AdapterView)9 ArrayList (java.util.ArrayList)9 ImageView (android.widget.ImageView)8 SuppressLint (android.annotation.SuppressLint)6 LocationPoint (net.osmand.data.LocationPoint)6 FavouritePoint (net.osmand.data.FavouritePoint)5 OsmandApplication (net.osmand.plus.OsmandApplication)5 Paint (android.graphics.Paint)4 ImageButton (android.widget.ImageButton)4 ListView (android.widget.ListView)4 List (java.util.List)4 RoutingHelper (net.osmand.plus.routing.RoutingHelper)4 DialogInterface (android.content.DialogInterface)3