Back to project page Go2-Rennes.
The source code is released under:
GNU General Public License
If you think the Android project Go2-Rennes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com * /*from ww w. j ava 2 s. c o m*/ * 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.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.util.Log; import fr.gotorennes.domain.Ligne; import fr.gotorennes.domain.LineAlert; public class LineAlertService extends RemoteService<LineAlert> { public static final String COMMAND = "getlinesalerts"; public static final String NODE = "alert"; private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); private static LineAlertService instance; private LineAlertService(Context context) { super(); } protected String getVersion() { return "2.0"; } public static synchronized LineAlertService getInstance(Context context) { if (instance == null) { instance = new LineAlertService(context); } return instance; } private Map<Long, Long> lastUpdate = new HashMap<Long, Long>(); private Map<Long, List<LineAlert>> cache = new HashMap<Long, List<LineAlert>>(); public List<LineAlert> getLineAlerts(Ligne ligne) { List<LineAlert> alerts = cache.get(ligne.id); if (alerts == null || System.currentTimeMillis() - lastUpdate.get(ligne.id) > 3600000) { Map<String, String> parameters = new HashMap<String, String>(); parameters.put("mode", "line"); parameters.put("line", ligne.code); alerts = loadList(COMMAND, NODE, parameters); if (alerts != null) { cache.put(ligne.id, alerts); lastUpdate.put(ligne.id, System.currentTimeMillis()); } } return alerts; } @Override protected LineAlert populate(JSONObject jsonObject) throws JSONException { LineAlert lineAlert = new LineAlert(); lineAlert.titre = jsonObject.getString("title"); lineAlert.debut = parseDate(jsonObject.getString("starttime")); lineAlert.fin = parseDate(jsonObject.getString("endtime")); lineAlert.detail = jsonObject.getString("detail"); lineAlert.perturbationMajeure = "1".equals(jsonObject.getString("majordisturbance")); return lineAlert; } public static Date parseDate(String input) { if (input == null) { return null; } if (input.endsWith("Z")) { input = input.substring(0, input.length() - 1) + "GMT-00:00"; } else { int inset = 6; String s0 = input.substring(0, input.length() - inset); String s1 = input.substring(input.length() - inset, input.length()); input = s0 + "GMT" + s1; } try { return dateFormat.parse(input); } catch (Exception ex) { Log.e("GoToRennes", ex.getMessage()); } return null; } }