Android Open Source - keepmoving Geo Point Dao






From Project

Back to project page keepmoving.

License

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.

Java Source Code

package it.rainbowbreeze.keepmoving.data;
/* w w w.j  av a  2s . c  om*/
import android.location.Location;

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.domain.Coord;
import it.rainbowbreeze.keepmoving.domain.GeoPoint;
import it.rainbowbreeze.keepmoving.domain.Weekdays;

/**
 * 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/>
 * 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.
 */

/**
 * DAO for class {@link it.rainbowbreeze.keepmoving.domain.GeoPoint}
 */
@Singleton
public class GeoPointDao {
    private static final String LOG_TAG = GeoPointDao.class.getSimpleName();

    private final ILogManager mLogManager;
    private final Config mConfig;
    private final List<GeoPoint> mPoints;

    @Inject
    public GeoPointDao(ILogManager logManager, Config config, IGeoPointRepository geoPointRepository) {
        mLogManager = logManager;
        mConfig = config;
        mPoints = geoPointRepository.getAllGeoPoints();
    }

    /**
     *
     * @param startingCoord
     * @param timestamp
     * @param day
     * @return
     */
    public List<GeoPoint> getAllPointsAt(Coord startingCoord, int timestamp, Weekdays day) {
        return getPointsAt(startingCoord, timestamp, day, 10);
    }

    /**
     *
     * @param startingCoord
     * @param timestamp
     * @param maxResults
     * @return
     */
    public List<GeoPoint> getPointsAt(Coord startingCoord, int timestamp, Weekdays day, int maxResults) {
        List<GeoPoint> points = new ArrayList<GeoPoint>();
        mLogManager.d(LOG_TAG, "Searching for all points that starts at " + startingCoord + " with timestamp " + timestamp);

        for (GeoPoint point : mPoints) {
            if (samePointWithinOffset(startingCoord, point.getStartCoord())) {
                if (availableAtTime(point, timestamp)) {
                    points.add(point);
                    if (maxResults > 0 && points.size() == maxResults) break;
                }
            }
        }
        return points;
    }

    /**
     * Verifies if two points overlaps each other (the end of the first is the start
     *  of the second)
     * @param first
     * @param second
    public boolean overlap (GeoPoint first, GeoPoint second) {
        if (checkForInvalidPoints(first, second)) return false;
        return samePointWithinOffset(first.getEndCoord(), second.getStartCoord());
    }
     */

    /**
     * Returns true if two points have the same starting coordinate (with tolerance)
     * @param first
     * @param second
     * @return
    public boolean sameStart(GeoPoint first, GeoPoint second) {
        if (checkForInvalidPoints(first, second)) return false;
        return samePointWithinOffset(first.getStartCoord(), second.getStartCoord());
    }
     */

    /**
     * Returns true if two points have the same ending coordinate (with tolerance)
     * @param first
     * @param second
     * @return
    public boolean sameEnd(GeoPoint first, GeoPoint second) {
        if (checkForInvalidPoints(first, second)) return false;
        return samePointWithinOffset(first.getEndCoord(), second.getEndCoord());
    }
     */

    /**
     * Returns distances (in meters) from two coordinates
     *
     * public only for testing purposes
     * @param p1
     * @param p2
     * @return
     */
    public float calcDistance(Coord p1, Coord p2) {
        float[] results = new float[3];
        Location.distanceBetween(p1.getLat(), p1.getLng(), p2.getLat(), p2.getLng(), results);
        return results[0];
    }

    /**
     * Verifies if two points are basically the same (the distance between them are falls type_tube the
     * accepted range).
     * public only for test purpose
     * @param p1
     * @param p2
     * @return
     */
    public boolean samePointWithinOffset(Coord p1, Coord p2) {
        return calcDistance(p1, p2) <= mConfig.getDistanceTolerance();
    }

    public boolean availableAtTime(GeoPoint point, int timestamp) {
        if (point.isValidForAllTime()) return true;
        return point.getValidTime() >= timestamp;
        //TODO implement offset time
    }



    /**
     * Checks if one of the points is null
     * @param points
     * @return true if one of the points is null or has null start/end coordinates, otherwise false
    private boolean checkForInvalidPoints(GeoPoint... points) {
        for (int i=1; i <= points.length; i++) {
            GeoPoint point = points[i-1];
            if (null == point) {
                mLogManager.w(LOG_TAG, String.format("Point %d is null", i));
                return true;
            }
            if (null == point.getStartCoord()) {
                mLogManager.w(LOG_TAG, String.format("Point %d has invalid start coord", i));
                return true;
            }
            if (null == point.getEndCoord()) {
                mLogManager.w(LOG_TAG, String.format("Point %d has invalid end coord", i));
                return true;
            }
        }
        return false;
    }
     */
}




Java Source Code List

it.rainbowbreeze.keepmoving.ApplicationTest.java
it.rainbowbreeze.keepmoving.common.Config.java
it.rainbowbreeze.keepmoving.common.DaggerModule.java
it.rainbowbreeze.keepmoving.common.ILogManager.java
it.rainbowbreeze.keepmoving.common.LogManager.java
it.rainbowbreeze.keepmoving.common.MyApp.java
it.rainbowbreeze.keepmoving.common.Utils.java
it.rainbowbreeze.keepmoving.data.GeoPointDao.java
it.rainbowbreeze.keepmoving.data.GeoPointFixedRepository.java
it.rainbowbreeze.keepmoving.data.IGeoPointRepository.java
it.rainbowbreeze.keepmoving.domain.Coord.java
it.rainbowbreeze.keepmoving.domain.GeoPointTypes.java
it.rainbowbreeze.keepmoving.domain.GeoPoint.java
it.rainbowbreeze.keepmoving.domain.Route.java
it.rainbowbreeze.keepmoving.domain.Weekdays.java
it.rainbowbreeze.keepmoving.logic.PositionManager.java
it.rainbowbreeze.keepmoving.logic.RouteManager.java
it.rainbowbreeze.keepmoving.logic.TimetableController.java
it.rainbowbreeze.keepmoving.logic.TimetableModel.java
it.rainbowbreeze.keepmoving.ui.SpannableHelper.java
it.rainbowbreeze.keepmoving.ui.StepsArrayAdapter.java
it.rainbowbreeze.keepmoving.ui.TimetableActivity.java
it.rainbowbreeze.keepmoving.ui.TimetableActivity.java
it.rainbowbreeze.keepmoving.ui.TimetableFragment.java