2
* Copyright 2009 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
* use this file except in compliance with the License. You may obtain a copy of
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
* License for the specific language governing permissions and limitations under
16
package org.opensatnav.android.contribute.content;
20
import org.opensatnav.android.contribute.util.MyTracksUtils;
22
import android.location.Location;
25
* A buffer of Locations from a track.
26
* This class contains some additional information about the loaded points.
28
* @author Sandor Dornbush
30
public class TrackBuffer {
33
* An array of location objects that are part of a track.
35
private final Location[] buffer;
38
* The id of the last location read from the track.
40
private long lastLocationRead = 0;
43
* The number of locations loaded into the buffer.
45
private int locationsLoaded = 0;
48
* Create a TrackBuffer with size elements.
50
* @param size The size of the buffer
52
public TrackBuffer(int size) {
53
buffer = new Location[size];
57
* Reset the track to a state with no locations.
64
* Reset the track at the given starting location id.
66
public void resetAt(long lastLocation) {
67
lastLocationRead = lastLocation;
71
public void setInvalid() {
72
lastLocationRead = Integer.MAX_VALUE;
76
* @return The number of locations that can be stored in this buffer
78
public int getSize() {
83
* @param index The index of the location to fetch
84
* @return The location for the given index
86
public Location get(int index) {
91
* Adds a location to the end of the buffer.
92
* @param location The location to add.
93
* @param id The id of the location to be added.
95
public void add(Location location, long id) {
96
buffer[locationsLoaded++] = location;
97
lastLocationRead = Math.max(lastLocationRead, id);
101
* @return The id of the last location loaded into the buffer
103
public long getLastLocationRead() {
104
return lastLocationRead;
108
* @return The number of locations loaded into the buffer.
110
public int getLocationsLoaded() {
111
return locationsLoaded;
115
* Finds the start location, i.e. the one which is the first point of a
116
* segment with at least two points.
118
* @return the start location
120
public Location findStartLocation() {
121
int numValidLocations = 0;
122
for (int i = 0; i < getLocationsLoaded(); i++) {
123
Location location = buffer[i];
124
if (MyTracksUtils.isValidLocation(location)) {
126
if (numValidLocations == 2) {
127
return buffer[i - 1];
130
numValidLocations = 0;