use of be.brunoparmentier.openbikesharing.app.models.Station in project OpenBikeSharing by bparmentier.
the class StationsListActivity method setNearbyStations.
private void setNearbyStations(List<Station> stations) {
final double radius = 0.01;
nearbyStations = new ArrayList<>();
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
final Location userLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (userLocation != null) {
for (Station station : stations) {
if ((station.getLatitude() > userLocation.getLatitude() - radius) && (station.getLatitude() < userLocation.getLatitude() + radius) && (station.getLongitude() > userLocation.getLongitude() - radius) && (station.getLongitude() < userLocation.getLongitude() + radius)) {
nearbyStations.add(station);
}
}
Collections.sort(nearbyStations, new Comparator<Station>() {
@Override
public int compare(Station station1, Station station2) {
float[] result1 = new float[3];
Location.distanceBetween(userLocation.getLatitude(), userLocation.getLongitude(), station1.getLatitude(), station1.getLongitude(), result1);
Float distance1 = result1[0];
float[] result2 = new float[3];
Location.distanceBetween(userLocation.getLatitude(), userLocation.getLongitude(), station2.getLatitude(), station2.getLongitude(), result2);
Float distance2 = result2[0];
return distance1.compareTo(distance2);
}
});
} else {
// TODO: update location?
}
}
}
use of be.brunoparmentier.openbikesharing.app.models.Station in project OpenBikeSharing by bparmentier.
the class StationsDataSource method storeStations.
public void storeStations(ArrayList<Station> stations) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.beginTransaction();
try {
clearStations();
for (Station station : stations) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.STATIONS_COLUMN_ID, station.getId());
values.put(DatabaseHelper.STATIONS_COLUMN_NAME, station.getName());
values.put(DatabaseHelper.STATIONS_COLUMN_LAST_UPDATE, station.getLastUpdate());
values.put(DatabaseHelper.STATIONS_COLUMN_LATITUDE, String.valueOf(station.getLatitude()));
values.put(DatabaseHelper.STATIONS_COLUMN_LONGITUDE, String.valueOf(station.getLongitude()));
values.put(DatabaseHelper.STATIONS_COLUMN_FREE_BIKES, String.valueOf(station.getFreeBikes()));
values.put(DatabaseHelper.STATIONS_COLUMN_EMPTY_SLOTS, String.valueOf(station.getEmptySlots()));
if (station.getAddress() != null)
values.put(DatabaseHelper.STATIONS_COLUMN_ADDRESS, station.getAddress());
if (station.isBanking() != null)
values.put(DatabaseHelper.STATIONS_COLUMN_BANKING, station.isBanking() ? 1 : 0);
if (station.isBonus() != null)
values.put(DatabaseHelper.STATIONS_COLUMN_BONUS, station.isBonus() ? 1 : 0);
if (station.getStatus() != null)
values.put(DatabaseHelper.STATIONS_COLUMN_STATUS, station.getStatus().name());
db.insert(DatabaseHelper.STATIONS_TABLE_NAME, null, values);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
use of be.brunoparmentier.openbikesharing.app.models.Station in project OpenBikeSharing by bparmentier.
the class StationsDataSource method getStations.
public ArrayList<Station> getStations() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
ArrayList<Station> stations = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT id as _id, name, last_update, latitude, longitude, " + "free_bikes, empty_slots, address, banking, bonus, status " + "FROM " + DatabaseHelper.STATIONS_TABLE_NAME, null);
try {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
Station station = toStation(cursor);
stations.add(station);
cursor.moveToNext();
}
}
Collections.sort(stations);
return stations;
} finally {
cursor.close();
}
}
use of be.brunoparmentier.openbikesharing.app.models.Station in project OpenBikeSharing by bparmentier.
the class StationsListFragment method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
stations = (ArrayList<Station>) getArguments().getSerializable(KEY_STATIONS);
emptyListResourceId = getArguments().getInt(KEY_EMPTY_LIST_RESOURCE_ID);
stationsListAdapter = new StationsListAdapter(getActivity(), R.layout.station_list_item, stations);
}
Aggregations