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.contribute.content;
20
import org.opensatnav.contribute.content.OSNProvider;
21
import org.opensatnav.contribute.util.MyTracksUtils;
23
import android.location.Location;
26
* A buffer of Locations from a track.
27
* This class contains some additional information about the loaded points.
29
* @author Sandor Dornbush
31
public class TrackBuffer {
34
* An array of location objects that are part of a track.
36
private final Location[] buffer;
39
* The id of the last location read from the track.
41
private long lastLocationRead = 0;
44
* The number of locations loaded into the buffer.
46
private int locationsLoaded = 0;
49
* Create a TrackBuffer with size elements.
51
* @param size The size of the buffer
53
public TrackBuffer(int size) {
54
buffer = new Location[size];
58
* Reset the track to a state with no locations.
65
* Reset the track at the given starting location id.
67
public void resetAt(long lastLocation) {
68
lastLocationRead = lastLocation;
72
public void setInvalid() {
73
lastLocationRead = Integer.MAX_VALUE;
77
* @return The number of locations that can be stored in this buffer
79
public int getSize() {
84
* @param index The index of the location to fetch
85
* @return The location for the given index
87
public Location get(int index) {
92
* Adds a location to the end of the buffer.
93
* @param location The location to add.
94
* @param id The id of the location to be added.
96
public void add(Location location, long id) {
97
buffer[locationsLoaded++] = location;
98
lastLocationRead = Math.max(lastLocationRead, id);
102
* @return The id of the last location loaded into the buffer
104
public long getLastLocationRead() {
105
return lastLocationRead;
109
* @return The number of locations loaded into the buffer.
111
public int getLocationsLoaded() {
112
return locationsLoaded;
116
* Finds the start location, i.e. the one which is the first point of a
117
* segment with at least two points.
119
* @return the start location
121
public Location findStartLocation() {
122
int numValidLocations = 0;
123
for (int i = 0; i < getLocationsLoaded(); i++) {
124
Location location = buffer[i];
125
if (MyTracksUtils.isValidLocation(location)) {
127
if (numValidLocations == 2) {
128
return buffer[i - 1];
131
numValidLocations = 0;