Search in sources :

Example 6 with Station

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?
        }
    }
}
Also used : LocationManager(android.location.LocationManager) Station(be.brunoparmentier.openbikesharing.app.models.Station) Location(android.location.Location)

Example 7 with Station

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();
    }
}
Also used : Station(be.brunoparmentier.openbikesharing.app.models.Station) ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 8 with Station

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();
    }
}
Also used : Station(be.brunoparmentier.openbikesharing.app.models.Station) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 9 with Station

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);
}
Also used : Station(be.brunoparmentier.openbikesharing.app.models.Station) StationsListAdapter(be.brunoparmentier.openbikesharing.app.adapters.StationsListAdapter)

Aggregations

Station (be.brunoparmentier.openbikesharing.app.models.Station)9 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 ArrayList (java.util.ArrayList)3 Cursor (android.database.Cursor)2 Location (android.location.Location)2 LocationManager (android.location.LocationManager)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 StationsDataSource (be.brunoparmentier.openbikesharing.app.db.StationsDataSource)2 ActionBar (android.app.ActionBar)1 AlertDialog (android.app.AlertDialog)1 ContentValues (android.content.ContentValues)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 MatrixCursor (android.database.MatrixCursor)1 Bitmap (android.graphics.Bitmap)1 Paint (android.graphics.Paint)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1