/*******************************************************************************
* Gaggle is Copyright 2010 by Geeksville Industries LLC, a California limited liability corporation.
*
* Gaggle is distributed under a dual license. We've chosen this approach because within Gaggle we've used a number
* of components that Geeksville Industries LLC might reuse for commercial products. Gaggle can be distributed under
* either of the two licenses listed below.
*
* 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.
*
* Commercial Distribution License
* If you would like to distribute Gaggle (or portions thereof) under a license other than
* the "GNU General Public License, version 2", contact Geeksville Industries. Geeksville Industries reserves
* the right to release Gaggle source code under a commercial license of its choice.
*
* GNU Public License, version 2
* All other distribution of Gaggle must conform to the terms of the GNU Public License, version 2. The full
* text of this license is included in the Gaggle source, see assets/manual/gpl-2.0.txt.
******************************************************************************/
package com.geeksville.location;
import java.util.Arrays;
import java.util.Collection;
import com.geeksville.android.InMemoryCursor;
/**
* Lets Cursor users see waypoints sorted a variety of ways
*
* @author kevinh
*
* FIXME - implement deactivate
*/
public class WaypointCursor extends InMemoryCursor {
public static final String KEY_DIST_PILOTX = "distx";
public static final String KEY_DIST_PILOTY = "disty";
private static final String[] colNames = {
LocationLogDbAdapter.KEY_ROWID, // must be first to support updates
LocationLogDbAdapter.KEY_LATITUDE, LocationLogDbAdapter.KEY_LONGITUDE,
LocationLogDbAdapter.KEY_ALTITUDE,
LocationLogDbAdapter.KEY_NAME,
LocationLogDbAdapter.KEY_DESCRIPTION,
LocationLogDbAdapter.KEY_WAYPOINT_TYPE, KEY_DIST_PILOTX, KEY_DIST_PILOTY };
Collection<ExtendedWaypoint> src;
/**
* The currently sorted list of waypoints (will be regenerated by requery())
*/
ExtendedWaypoint[] current;
public WaypointCursor(Collection<ExtendedWaypoint> src) {
super(colNames);
this.src = src;
current = new ExtendedWaypoint[src.size()];
requery();
}
/**
* Get the waypoint this cursor is currently looking at
*
* @return
*/
public ExtendedWaypoint getWaypoint() {
return current[this.getPosition()];
}
/**
*
* @see android.database.AbstractCursor#requery()
*/
@Override
public boolean requery() {
current = src.toArray(current);
// FIXME - is there a faster way to do this?
Arrays.sort(current, 0, getCount());
return super.requery();
}
@Override
protected boolean updateCurRow(int oldPosition, int newPosition, Object[] curRow) {
ExtendedWaypoint w = current[newPosition];
int colNum = 0;
curRow[colNum++] = w.id;
curRow[colNum++] = w.latitude;
curRow[colNum++] = w.longitude;
curRow[colNum++] = w.altitude;
curRow[colNum++] = w.name;
curRow[colNum++] = w.description;
curRow[colNum++] = w.type.ordinal();
curRow[colNum++] = w.distanceFromPilotX;
curRow[colNum++] = w.distanceFromPilotY;
return true;
}
@Override
public int getCount() {
return src.size();
}
}