Search in sources :

Example 1 with TspAnt

use of net.osmand.TspAnt in project Osmand by osmandapp.

the class WaypointDialogHelper method sortAllTargets.

@SuppressLint("StaticFieldLeak")
public static void sortAllTargets(final OsmandApplication app, final Activity activity, final WaypointDialogHelper helper) {
    new AsyncTask<Void, Void, int[]>() {

        ProgressDialog dlg = null;

        long startDialogTime = 0;

        List<TargetPoint> intermediates;

        protected void onPreExecute() {
            startDialogTime = System.currentTimeMillis();
            dlg = new ProgressDialog(activity);
            dlg.setTitle("");
            dlg.setMessage(activity.getResources().getString(R.string.intermediate_items_sort_by_distance));
            dlg.show();
        }

        protected int[] doInBackground(Void[] params) {
            TargetPointsHelper targets = app.getTargetPointsHelper();
            intermediates = targets.getIntermediatePointsWithTarget();
            Location cll = app.getLocationProvider().getLastKnownLocation();
            ArrayList<TargetPoint> lt = new ArrayList<>(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 {
                start = lt.get(0);
            }
            TargetPoint end = lt.remove(lt.size() - 1);
            ArrayList<LatLon> al = new ArrayList<>();
            for (TargetPoint p : lt) {
                al.add(p.point);
            }
            return new TspAnt().readGraph(al, start.point, end.point).solve();
        }

        protected void onPostExecute(int[] result) {
            if (dlg != null) {
                long t = System.currentTimeMillis();
                if (t - startDialogTime < 500) {
                    app.runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            dlg.dismiss();
                        }
                    }, 500 - (t - startDialogTime));
                } else {
                    dlg.dismiss();
                }
            }
            List<TargetPoint> alocs = new ArrayList<>();
            for (int i : result) {
                if (i > 0) {
                    TargetPoint loc = intermediates.get(i - 1);
                    alocs.add(loc);
                }
            }
            intermediates.clear();
            intermediates.addAll(alocs);
            TargetPointsHelper targets = app.getTargetPointsHelper();
            List<TargetPoint> cur = targets.getIntermediatePointsWithTarget();
            boolean eq = true;
            for (int j = 0; j < cur.size() && j < intermediates.size(); j++) {
                if (cur.get(j) != intermediates.get(j)) {
                    eq = false;
                    break;
                }
            }
            if (!eq) {
                targets.reorderAllTargetPoints(intermediates, true);
            }
            if (helper.helperCallbacks != null) {
                helper.helperCallbacks.reloadAdapter();
            }
            updateRouteInfoMenu(activity);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : ArrayList(java.util.ArrayList) TspAnt(net.osmand.TspAnt) ProgressDialog(android.app.ProgressDialog) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) LatLon(net.osmand.data.LatLon) List(java.util.List) ArrayList(java.util.ArrayList) TargetPointsHelper(net.osmand.plus.TargetPointsHelper) Location(net.osmand.Location) SuppressLint(android.annotation.SuppressLint)

Example 2 with TspAnt

use of net.osmand.TspAnt 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)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 Location (net.osmand.Location)2 TspAnt (net.osmand.TspAnt)2 LatLon (net.osmand.data.LatLon)2 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)2 SuppressLint (android.annotation.SuppressLint)1 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 OnShowListener (android.content.DialogInterface.OnShowListener)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 TargetPointsHelper (net.osmand.plus.TargetPointsHelper)1