private static void commitChangePointsOrder(
     OsmandApplication app, List<LatLon> target, List<String> names) {
   TargetPointsHelper targets = app.getTargetPointsHelper();
   List<LatLon> cur = targets.getIntermediatePointsWithTarget();
   boolean eq = true;
   for (int j = 0; j < cur.size() && j < target.size(); j++) {
     if (cur.get(j) != target.get(j)) {
       eq = false;
       break;
     }
   }
   if (!eq) {
     targets.reorderAllTargetPoints(target, names, true);
   }
 }
  public static void openIntermediatePointsDialog(
      final Activity activity, final OsmandApplication app, final boolean changeOrder) {
    TargetPointsHelper targets = app.getTargetPointsHelper();
    final List<LatLon> intermediates = targets.getIntermediatePointsWithTarget();
    final TIntArrayList originalPositions = new TIntArrayList(intermediates.size());
    for (int j = 1; j <= intermediates.size(); j++) {
      originalPositions.add(j);
    }
    final List<String> names = targets.getIntermediatePointNamesWithTarget();
    final boolean[] checkedIntermediates = new boolean[intermediates.size()];
    Arrays.fill(checkedIntermediates, true);
    final ArrayAdapter<LatLon> listadapter =
        getListAdapter(
            app,
            activity,
            changeOrder,
            intermediates,
            originalPositions,
            names,
            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();
              LatLon 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();
            }
          }
        });

    Builder builder = new AccessibleAlertBuilder(activity);
    builder.setView(contentView);
    builder.setInverseBackgroundForced(true);
    lv.setBackgroundColor(Color.WHITE);
    builder.setPositiveButton(
        R.string.default_buttons_ok,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            if (changeOrder) {
              commitChangePointsOrder(app, intermediates, names);
            } 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, names, listadapter, pb, textInfo);
    }
    dlg.show();
  }