~opensatnav-admins/opensatnav/release-1.0

« back to all changes in this revision

Viewing changes to src/org/opensatnav/android/contribute/content/TrackBuffer.java

  • Committer: Kieran Fleming
  • Date: 2010-12-13 13:13:48 UTC
  • Revision ID: kieran.fleming@gmail.com-20101213131348-pixo12i0wjf11jk3
Add all the missing stuff from the failed package rename

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
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
 
6
 * the License at
 
7
 * 
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
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
 
14
 * the License.
 
15
 */
 
16
package org.opensatnav.android.contribute.content;
 
17
 
 
18
 
 
19
 
 
20
import org.opensatnav.android.contribute.util.MyTracksUtils;
 
21
 
 
22
import android.location.Location;
 
23
 
 
24
/**
 
25
 * A buffer of Locations from a track.
 
26
 * This class contains some additional information about the loaded points.
 
27
 *
 
28
 * @author Sandor Dornbush
 
29
 */
 
30
public class TrackBuffer {
 
31
 
 
32
  /**
 
33
   * An array of location objects that are part of a track.
 
34
   */
 
35
  private final Location[] buffer;
 
36
 
 
37
  /**
 
38
   * The id of the last location read from the track.
 
39
   */
 
40
  private long lastLocationRead = 0;
 
41
 
 
42
  /**
 
43
   * The number of locations loaded into the buffer.
 
44
   */
 
45
  private int locationsLoaded = 0;
 
46
 
 
47
  /**
 
48
   * Create a TrackBuffer with size elements.
 
49
   *
 
50
   * @param size The size of the buffer
 
51
   */
 
52
  public TrackBuffer(int size) {
 
53
    buffer = new Location[size];
 
54
  }
 
55
 
 
56
  /**
 
57
   * Reset the track to a state with no locations.
 
58
   */
 
59
  public void reset() {
 
60
    resetAt(0);
 
61
  }
 
62
 
 
63
  /**
 
64
   * Reset the track at the given starting location id.
 
65
   */
 
66
  public void resetAt(long lastLocation) {
 
67
    lastLocationRead = lastLocation;
 
68
    locationsLoaded = 0;
 
69
  }
 
70
 
 
71
  public void setInvalid() {
 
72
    lastLocationRead = Integer.MAX_VALUE;
 
73
  }
 
74
 
 
75
  /**
 
76
   * @return The number of locations that can be stored in this buffer
 
77
   */
 
78
  public int getSize() {
 
79
    return buffer.length;
 
80
  }
 
81
 
 
82
  /**
 
83
   * @param index The index of the location to fetch
 
84
   * @return The location for the given index
 
85
   */
 
86
  public Location get(int index) {
 
87
    return buffer[index];
 
88
  }
 
89
 
 
90
  /**
 
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.
 
94
   */
 
95
  public void add(Location location, long id) {
 
96
    buffer[locationsLoaded++] = location;
 
97
    lastLocationRead = Math.max(lastLocationRead, id);
 
98
  }
 
99
 
 
100
  /**
 
101
   * @return The id of the last location loaded into the buffer
 
102
   */
 
103
  public long getLastLocationRead() {
 
104
    return lastLocationRead;
 
105
  }
 
106
 
 
107
  /**
 
108
   * @return The number of locations loaded into the buffer.
 
109
   */
 
110
  public int getLocationsLoaded() {
 
111
    return locationsLoaded;
 
112
  }
 
113
 
 
114
  /**
 
115
   * Finds the start location, i.e. the one which is the first point of a
 
116
   * segment with at least two points.
 
117
   *
 
118
   * @return the start location
 
119
   */
 
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)) {
 
125
        numValidLocations++;
 
126
        if (numValidLocations == 2) {
 
127
          return buffer[i - 1];
 
128
        }
 
129
      } else {
 
130
        numValidLocations = 0;
 
131
      }
 
132
    }
 
133
    return null;
 
134
  }
 
135
}