Back to project page salaya-tram-map.
The source code is released under:
Unless otherwise noted, this software is Copyright 2011 Pawit Pornkitprasan and is licensed under the Simplified BSD License. Copyright 2011 Pawit Pornkitprasan. All rights reserved. Red...
If you think the Android project salaya-tram-map listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.dyndns.pawitp.salayatrammap.schedule; /* w w w .j a va 2 s. co m*/ import android.text.format.Time; import android.util.Log; public class TramCarSchedule { private static final String TAG = "TramCarSchedule"; private static final long TRAM_ROUND_TIME = 600000; // 10 minutes private static final int NO_MORE_TRAM = -1; private Integer[][] mSchedule; private int mNextTram; // cache, always update before use public TramCarSchedule() { } public void updateSchedule(Integer[][] schedule) { mSchedule = schedule; updateNextTram(); } public Time getLastTram() throws NoTramLeftException, NoMoreTramException { try { int index = locateNextTram() - 1; if (index < 0) { throw new NoTramLeftException(); } return tramTimeToTime(mSchedule[index]); } catch (NoMoreTramException e) { // Check if the last round of tram is still running int indexLast = mSchedule.length - 1; Time last = tramTimeToTime(mSchedule[indexLast]); Time now = new Time(); now.setToNow(); long diff = now.toMillis(false) - last.toMillis(false); if (diff < TRAM_ROUND_TIME) { return last; } else { throw new NoMoreTramException(); } } } public Time getNextTram() throws NoMoreTramException { return tramTimeToTime(mSchedule[locateNextTram()]); } public long getUpdateTime() { Time now = new Time(); now.setToNow(); try { Time tram = tramTimeToTime(mSchedule[locateNextTram()]); return tram.toMillis(false) - now.toMillis(false); } catch (NoMoreTramException e) { // Check last round int indexLast = mSchedule.length - 1; Time last = tramTimeToTime(mSchedule[indexLast]); long timeDiff = now.toMillis(false) - last.toMillis(false); if (timeDiff < TRAM_ROUND_TIME) { return TRAM_ROUND_TIME - timeDiff; } else { // wait till next day Time tomorrow = new Time(); tomorrow.setToNow(); tomorrow.hour = 24; tomorrow.minute = 0; tomorrow.second = 0; return tomorrow.toMillis(false) - now.toMillis(false); } } } // Call before using any of my functions! // (normally called by updateSchedule) public void updateNextTram() { Log.v(TAG, "Calculating next tram"); Time now = new Time(); now.setToNow(); now.second = 0; // Avoid second differences in comparison Time tram = new Time(now); boolean found = false; int len = mSchedule.length; int i; for (i = 0; i < len; i++) { tram.hour = mSchedule[i][0]; tram.minute = mSchedule[i][1]; if (tram.after(now)) { Log.v(TAG, "Found tram: " + i); found = true; break; } } if (found) { mNextTram = i; } else { mNextTram = NO_MORE_TRAM; } } private int locateNextTram() throws NoMoreTramException { if (mNextTram == NO_MORE_TRAM) { throw new NoMoreTramException(); } return mNextTram; } private Time tramTimeToTime(Integer[] time) { Time ret = new Time(); ret.setToNow(); ret.hour = time[0]; ret.minute = time[1]; ret.second = 0; return ret; } }