Back to project page keepmoving.
The source code is released under:
GNU General Public License
If you think the Android project keepmoving listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * This file is part of KeepMoving. KeepMoving is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public * License as published by the Free Software Foundation, version 2. * <p/>/*from w w w . j a v a 2 s . c o m*/ * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * <p/> * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * <p/> * Copyright Alfredo Morresi * <p/> * Created by Alfredo "Rainbowbreeze" Morresi on 15/06/14. */ package it.rainbowbreeze.keepmoving.logic; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; import javax.inject.Singleton; import it.rainbowbreeze.keepmoving.common.Config; import it.rainbowbreeze.keepmoving.common.ILogManager; import it.rainbowbreeze.keepmoving.common.Utils; import it.rainbowbreeze.keepmoving.data.GeoPointDao; import it.rainbowbreeze.keepmoving.domain.Coord; import it.rainbowbreeze.keepmoving.domain.GeoPoint; import it.rainbowbreeze.keepmoving.domain.Route; import it.rainbowbreeze.keepmoving.domain.Weekdays; /** * Plans different routes between points */ @Singleton public class RouteManager { private static final String LOG_TAG = RouteManager.class.getSimpleName(); private final ILogManager mLogManager; private final GeoPointDao mGeoPointDao; private final Config mConfig; @Inject public RouteManager(ILogManager logManager, Config config, GeoPointDao geoPointDao) { mLogManager = logManager; mGeoPointDao = geoPointDao; mConfig = config; } /** * Find a potential routes between a starting and ending point * * @param startingPoint * @param endingPoint * @param timestamp * @param day * @return */ public Route findRoute(Coord startingPoint, Coord endingPoint, int timestamp, Weekdays day) { Route route = new Route(); List<GeoPoint> steps = route.getSteps(); Coord newStartingCoord = startingPoint; int newTimestamp = timestamp; while (true) { List<GeoPoint> points = mGeoPointDao.getAllPointsAt(newStartingCoord, newTimestamp, day); //no routes if (points.isEmpty()) { steps.clear(); return route; } //checks if one of the points arrives to the ending point boolean arrived = false; for (GeoPoint point : points) { if (mGeoPointDao.samePointWithinOffset(endingPoint, point.getEndCoord())) { mLogManager.d(LOG_TAG, "We're arrived!"); steps.add(point); arrived = true; break; } } if (arrived) { break; } //in any case, add the new point to the final route GeoPoint newStartingPoint = points.get(0); steps.add(newStartingPoint); if (!newStartingPoint.isValidForAllTime()) newTimestamp = newStartingPoint.getValidTime(); newTimestamp += newStartingPoint.getLenght(); newStartingCoord = newStartingPoint.getEndCoord(); //TODO improve checks for infinite loops if (steps.size() > 20) { mLogManager.d(LOG_TAG, "Max hops reached, exiting"); steps.clear(); break; } mLogManager.d(LOG_TAG, "Searching for a new connection starting from " + newStartingCoord); } // add additional information on the routes route.refreshTimestamps(); return route; } /** * Finds the n routes for the current position / time * @param startingPoint * @param endingPoint * @param timestamp * @param day * @param maxRoutes * @return */ public ArrayList<Route> getRoutesStartingAt( Coord startingPoint, Coord endingPoint, int timestamp, Weekdays day, int maxRoutes) { ArrayList<Route> routes = new ArrayList<Route>(); int newTimestamp = timestamp; Weekdays newDay = day; while (routes.size() < maxRoutes) { Route route = findRoute(startingPoint, endingPoint, newTimestamp, newDay); if (route.isInvalid()) break; routes.add(route); //finds new timestamp based on previous route newTimestamp = getNextTimestamp(route); } return routes; } /** * Finds if commute from one point to another is possible * @param first * @param second * @return */ public boolean commutePossible(GeoPoint first, GeoPoint second) { int remainingTime = second.getValidTime() - (first.getValidTime() + first.getLenght()) + mConfig.getTimeTolerance(); return remainingTime >= 0; } /** * Given a route, returns the timestamp just after the */ public int getNextTimestamp(Route route) { if (route.isInvalid()) return Utils.INVALID_TIMESTAMP; int timestamp = Utils.INVALID_TIMESTAMP; int elapsedTime = 0; for (GeoPoint point : route.getSteps()) { if (point.isValidForAllTime()) { elapsedTime += point.getLenght(); //total time required to arrive to the next point continue; //goes to the next point } timestamp = point.getValidTime(); //when the next point is valid timestamp -= elapsedTime; //subtract the time required to arrive to the point timestamp += mConfig.getTimeTolerance(); //subtracts the tolerance timestamp ++; //and adds a minute, enough to require to move to another step break; } return timestamp; } }