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;
       rtp.wpt = wptPt;
       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;
       }
       currentPoints.add(rtp);
     }
     sortPoints();
   }
 }
 public void updateCurrentTargetPoint() {
   TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
   String locName = targetPointsHelper.getPointNavigateDescription();
   for (int i = 0; i < currentPoints.size(); i++) {
     RoutePoint rtp = currentPoints.get(i);
     rtp.isNextNavigate =
         rtp.visitedTime == 0 && locName != null && locName.equals(rtp.getName());
     if (rtp.isNextNavigate) {
       locName = null;
     }
   }
   sortPoints();
 }
 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 void reRouteTo(LatLon loc) {
          final OsmandApplication app = activity.getMyApplication();
          final TargetPointsHelper targets = app.getTargetPointsHelper();
          // If we are in following mode then just update target point
          targets.navigateToPoint(loc, true, -1);
          if (!app.getRoutingHelper().isFollowingMode()) {
            // If we are not in following mode then request new route to calculate
            // Use default application mode
            activity.runOnUiThread(
                new Runnable() {

                  @Override
                  public void run() {
                    activity.followRoute(
                        app.getSettings().getApplicationMode(),
                        targets.getPointToNavigate(),
                        targets.getIntermediatePoints(),
                        app.getLastKnownLocation(),
                        null);
                  }
                });
          }
        }
  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();
  }
  @Override
  protected void onResume() {
    super.onResume();
    cancelNotification();
    if (settings.MAP_SCREEN_ORIENTATION.get() != getRequestedOrientation()) {
      setRequestedOrientation(settings.MAP_SCREEN_ORIENTATION.get());
      // can't return from this method we are not sure if activity will be recreated or not
    }

    app.getLocationProvider().checkIfLastKnownLocationIsValid();
    // for voice navigation
    if (settings.AUDIO_STREAM_GUIDANCE.get() != null) {
      setVolumeControlStream(settings.AUDIO_STREAM_GUIDANCE.get());
    } else {
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }

    applicationModeListener =
        new StateChangedListener<ApplicationMode>() {
          @Override
          public void stateChanged(ApplicationMode change) {
            updateApplicationModeSettings();
          }
        };
    settings.APPLICATION_MODE.addListener(applicationModeListener);
    updateApplicationModeSettings();

    String filterId = settings.getPoiFilterForMap();
    PoiFilter poiFilter = app.getPoiFilters().getFilterById(filterId);
    if (poiFilter == null) {
      poiFilter = new PoiFilter(null, app);
    }

    mapLayers.getPoiMapLayer().setFilter(poiFilter);

    // if destination point was changed try to recalculate route
    TargetPointsHelper targets = app.getTargetPointsHelper();
    RoutingHelper routingHelper = app.getRoutingHelper();
    if (routingHelper.isFollowingMode()
        && (!Algorithms.objectEquals(targets.getPointToNavigate(), routingHelper.getFinalLocation())
            || !Algorithms.objectEquals(
                targets.getIntermediatePoints(), routingHelper.getIntermediatePoints()))) {
      routingHelper.setFinalAndCurrentLocation(
          targets.getPointToNavigate(),
          targets.getIntermediatePoints(),
          app.getLocationProvider().getLastKnownLocation(),
          routingHelper.getCurrentGPXRoute());
    }
    app.getLocationProvider().resumeAllUpdates();

    if (settings != null && settings.isLastKnownMapLocation()) {
      LatLon l = settings.getLastKnownMapLocation();
      mapView.setLatLon(l.getLatitude(), l.getLongitude());
      mapView.setZoom(settings.getLastKnownMapZoom());
    }

    settings.MAP_ACTIVITY_ENABLED.set(true);
    checkExternalStorage();
    showAndHideMapPosition();

    LatLon cur = new LatLon(mapView.getLatitude(), mapView.getLongitude());
    LatLon latLonToShow = settings.getAndClearMapLocationToShow();
    String mapLabelToShow = settings.getAndClearMapLabelToShow();
    Object toShow = settings.getAndClearObjectToShow();
    if (settings.isRouteToPointNavigateAndClear()) {
      // always enable and follow and let calculate it (GPS is not accessible in garage)
      mapActions.getDirections(null, null, false);
    }
    if (mapLabelToShow != null && latLonToShow != null) {
      mapLayers.getContextMenuLayer().setSelectedObject(toShow);
      mapLayers.getContextMenuLayer().setLocation(latLonToShow, mapLabelToShow);
    }
    if (latLonToShow != null && !latLonToShow.equals(cur)) {
      mapView
          .getAnimatedDraggingThread()
          .startMoving(
              latLonToShow.getLatitude(),
              latLonToShow.getLongitude(),
              settings.getMapZoomToShow(),
              true);
    }
    if (latLonToShow != null) {
      // remember if map should come back to isMapLinkedToLocation=true
      mapViewTrackingUtilities.setMapLinkedToLocation(false);
    }

    View progress = mapLayers.getMapInfoLayer().getProgressBar();
    if (progress != null) {
      app.getResourceManager().setBusyIndicator(new BusyIndicator(this, progress));
    }

    OsmandPlugin.onMapActivityResume(this);
    mapView.refreshMap(true);
  }
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    OsmandApplication app = (OsmandApplication) getActivity().getApplication();
    boolean light = app.getSettings().isLightActionBar();
    com.actionbarsherlock.view.MenuItem menuItem =
        menu.add(0, NAVIGATE_TO, 0, R.string.get_directions)
            .setShowAsActionFlags(
                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    menuItem =
        menuItem.setIcon(
            light ? R.drawable.ic_action_gdirections_light : R.drawable.ic_action_gdirections_dark);
    menuItem.setOnMenuItemClickListener(
        new OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(com.actionbarsherlock.view.MenuItem item) {
            select(NAVIGATE_TO);
            return true;
          }
        });
    TargetPointsHelper targets = app.getTargetPointsHelper();
    if (targets.getPointToNavigate() != null) {
      menuItem =
          menu.add(0, ADD_WAYPOINT, 0, R.string.context_menu_item_intermediate_point)
              .setShowAsActionFlags(
                  MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
      menuItem =
          menuItem.setIcon(
              light ? R.drawable.ic_action_flage_light : R.drawable.ic_action_flage_dark);
    } else {
      menuItem =
          menu.add(0, ADD_WAYPOINT, 0, R.string.context_menu_item_destination_point)
              .setShowAsActionFlags(
                  MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
      menuItem =
          menuItem.setIcon(
              light ? R.drawable.ic_action_flag_light : R.drawable.ic_action_flag_dark);
    }
    menuItem.setOnMenuItemClickListener(
        new OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(com.actionbarsherlock.view.MenuItem item) {
            select(ADD_WAYPOINT);
            return true;
          }
        });
    // }
    menuItem =
        menu.add(0, SHOW_ON_MAP, 0, R.string.search_shown_on_map)
            .setShowAsActionFlags(
                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    menuItem =
        menuItem.setIcon(
            light ? R.drawable.ic_action_marker_light : R.drawable.ic_action_marker_dark);

    menuItem.setOnMenuItemClickListener(
        new OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(com.actionbarsherlock.view.MenuItem item) {
            select(SHOW_ON_MAP);
            return true;
          }
        });

    menuItem =
        menu.add(0, ADD_TO_FAVORITE, 0, R.string.add_to_favourite)
            .setShowAsActionFlags(
                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    menuItem =
        menuItem.setIcon(light ? R.drawable.ic_action_fav_light : R.drawable.ic_action_fav_dark);

    menuItem.setOnMenuItemClickListener(
        new OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(com.actionbarsherlock.view.MenuItem item) {
            select(ADD_TO_FAVORITE);
            return true;
          }
        });
  }
Exemple #8
0
  private boolean updateCurrentRouteStatus(Location currentLocation, float posTolerance) {
    List<Location> routeNodes = route.getImmutableAllLocations();
    int currentRoute = route.currentRoute;
    // 1. Try to proceed to next point using orthogonal distance (finding minimum orthogonal dist)
    while (currentRoute + 1 < routeNodes.size()) {
      double dist = currentLocation.distanceTo(routeNodes.get(currentRoute));
      if (currentRoute > 0) {
        dist =
            getOrthogonalDistance(
                currentLocation, routeNodes.get(currentRoute - 1), routeNodes.get(currentRoute));
      }
      boolean processed = false;
      // if we are still too far try to proceed many points
      // if not then look ahead only 3 in order to catch sharp turns
      boolean longDistance = dist >= 250;
      int newCurrentRoute =
          lookAheadFindMinOrthogonalDistance(
              currentLocation, routeNodes, currentRoute, longDistance ? 15 : 8);
      double newDist =
          getOrthogonalDistance(
              currentLocation,
              routeNodes.get(newCurrentRoute),
              routeNodes.get(newCurrentRoute + 1));
      if (longDistance) {
        if (newDist < dist) {
          if (log.isDebugEnabled()) {
            log.debug(
                "Processed by distance : (new) "
                    + newDist
                    + " (old) "
                    + dist); //$NON-NLS-1$//$NON-NLS-2$
          }
          processed = true;
        }
      } else if (newDist < dist || newDist < 10) {
        // newDist < 10 (avoid distance 0 till next turn)
        if (dist > posTolerance) {
          processed = true;
          if (log.isDebugEnabled()) {
            log.debug(
                "Processed by distance : " + newDist + " " + dist); // $NON-NLS-1$//$NON-NLS-2$
          }
        } else {
          // case if you are getting close to the next point after turn
          // but you have not yet turned (could be checked bearing)
          if (currentLocation.hasBearing() || lastFixedLocation != null) {
            float bearingToRoute = currentLocation.bearingTo(routeNodes.get(currentRoute));
            float bearingRouteNext =
                routeNodes.get(newCurrentRoute).bearingTo(routeNodes.get(newCurrentRoute + 1));
            float bearingMotion =
                currentLocation.hasBearing()
                    ? currentLocation.getBearing()
                    : lastFixedLocation.bearingTo(currentLocation);
            double diff = Math.abs(MapUtils.degreesDiff(bearingMotion, bearingToRoute));
            double diffToNext = Math.abs(MapUtils.degreesDiff(bearingMotion, bearingRouteNext));
            if (diff > diffToNext) {
              if (log.isDebugEnabled()) {
                log.debug("Processed point bearing deltas : " + diff + " " + diffToNext);
              }
              processed = true;
            }
          }
        }
      }
      if (processed) {
        // that node already passed
        route.updateCurrentRoute(newCurrentRoute + 1);
        currentRoute = newCurrentRoute + 1;
      } else {
        break;
      }
    }

    // 2. check if intermediate found
    if (route.getIntermediatePointsToPass() > 0
        && route.getDistanceToNextIntermediate(lastFixedLocation) < POSITION_TOLERANCE * 2) {
      showMessage(app.getString(R.string.arrived_at_intermediate_point));
      route.passIntermediatePoint();

      TargetPointsHelper targets = app.getTargetPointsHelper();
      List<String> ns = targets.getIntermediatePointNames();
      int toDel = targets.getIntermediatePoints().size() - route.getIntermediatePointsToPass();
      int currentIndex = toDel - 1;
      String name =
          currentIndex < 0 || currentIndex >= ns.size() || ns.get(currentIndex) == null
              ? ""
              : ns.get(currentIndex);
      if (isFollowingMode) {
        voiceRouter.arrivedIntermediatePoint(name);
      }
      while (toDel > 0) {
        targets.removeWayPoint(false, 0);
        toDel--;
      }

      while (intermediatePoints != null
          && route.getIntermediatePointsToPass() < intermediatePoints.size()) {
        intermediatePoints.remove(0);
      }
    }

    // 3. check if destination found
    Location lastPoint = routeNodes.get(routeNodes.size() - 1);
    if (currentRoute > routeNodes.size() - 3
        && currentLocation.distanceTo(lastPoint)
            < (((float) settings.getApplicationMode().getArrivalDistance())
                * settings.ARRIVAL_DISTANCE_FACTOR.get())) {
      showMessage(app.getString(R.string.arrived_at_destination));
      TargetPointsHelper targets = app.getTargetPointsHelper();
      String description = targets.getPointNavigateDescription();
      if (isFollowingMode) {
        voiceRouter.arrivedDestinationPoint(description);
      }
      if (OsmandPlugin.onDestinationReached()) {
        clearCurrentRoute(null, null);
        setRoutePlanningMode(false);
        app.runInUIThread(
            new Runnable() {
              @Override
              public void run() {
                settings.APPLICATION_MODE.set(settings.DEFAULT_APPLICATION_MODE.get());
              }
            });
        // targets.clearPointToNavigate(false);
        return true;
      }
    }
    return false;
  }