/******************************************************************************* * Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package fr.gotorennes.remote; import java.util.ArrayList; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import fr.gotorennes.domain.RelayPark; public class RelayParkService extends RemoteService<RelayPark> { private static RelayParkService instance; private RelayParkService(Context context) { super(); } public static synchronized RelayParkService getInstance(Context context) { if (instance == null) { instance = new RelayParkService(context); } return instance; } private long lastUpdate = 0; private List<RelayPark> cache = null; public List<RelayPark> getRelayParks() { if (cache == null || System.currentTimeMillis() - lastUpdate > 60000) { cache = loadList(); if (cache != null) { lastUpdate = System.currentTimeMillis(); } } return cache != null ? new ArrayList<RelayPark>(cache) : cache; } public List<RelayPark> getRelayParks(double latitudeMin, double latitudeMax, double longitudeMax, double longitudeMin) { List<RelayPark> relayParks = getRelayParks(); if (relayParks == null) { return null; } List<RelayPark> result = new ArrayList<RelayPark>(); for (RelayPark parc : relayParks) { if (parc.latitude >= latitudeMin && parc.latitude <= latitudeMax && parc.longitude >= longitudeMin && parc.longitude <= longitudeMax) { result.add(parc); } } return result; } public List<RelayPark> getProximityRelayParks(double latitude, double longitude) { return loadProximityList(latitude, longitude, 3); } public RelayPark getRelayPark(String name) { List<RelayPark> relayParks = getRelayParks(); if (relayParks == null) { return null; } for (RelayPark relayPark : relayParks) { if (relayPark.name.equals(name)) { return relayPark; } } return null; } @Override protected RelayPark populate(JSONObject jsonObject) throws JSONException { RelayPark relayPark = new RelayPark(); relayPark.name = jsonObject.getString("nom"); String etat = jsonObject.getString("etat"); if ("Fermé".equals(etat)) { relayPark.state = 1; } else if ("Complet".equals(etat)) { relayPark.state = 2; } relayPark.capacity = Integer.parseInt(jsonObject.getString("capaciteactuelle")); relayPark.available = Integer.parseInt(jsonObject.getString("nombreplacesdisponibles")); relayPark.latitude = jsonObject.getJSONArray("coordonnees").getDouble(0); relayPark.longitude = jsonObject.getJSONArray("coordonnees").getDouble(1); return relayPark; } @Override protected String getDataSet() { return "tco-parcsrelais-etat-tr"; } }