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 w w w.jav a 2 s. co 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; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.text.Html; import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import fr.gotorennes.domain.Arret; import fr.gotorennes.domain.Circuit; import fr.gotorennes.domain.Ligne; import fr.gotorennes.domain.Station; import fr.gotorennes.util.BackgroundTask; public class BusTripActivity extends AbstractActivity { private long idTrajet; private Station station; private Ligne ligne; private Calendar calendrier; private static final int DATE_DIALOG_ID = 0; private static final int TIME_DIALOG_ID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bustrip); } @Override protected String getTrackingName() { return "trajetBus"; } @Override protected int getMapIcon() { return 0; } @Override protected void init(Intent intent) { ligne = intent.getParcelableExtra("ligne"); station = intent.getParcelableExtra("station"); Arret arret = intent.getParcelableExtra("arret"); calendrier = Calendar.getInstance(); calendrier.setTime(new Date(intent.getLongExtra("date", System.currentTimeMillis()))); idTrajet = arret.idTrajet; TextView lineName = (TextView) findViewById(R.id.name); lineName.setText(Html.fromHtml("<u>" + station.nom + "</u>")); lineName.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), BusArretActivity.class); intent.putExtra("station", station); intent.putExtra("ligne", ligne); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); TextView date = (TextView) findViewById(R.id.date); date.setText(DateFormat.format("dd/MM/yyyy", calendrier)); date.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); TextView time = (TextView) findViewById(R.id.time); time.setText(DateFormat.format("kk:mm", calendrier)); time.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); ImageView lineIcon = (ImageView) findViewById(R.id.icon); Bitmap icon = goToRennes.getBusStationService().getBitmapIcon(this, ligne.id); lineIcon.setImageBitmap(icon); loadStopTimes(); } protected void loadStopTimes() { if (idTrajet == 0) { ListView listView = (ListView) findViewById(R.id.stops); listView.setAdapter(getStopTimesAdapter(new ArrayList<Arret>())); return; } BackgroundTask<List<Arret>> backgroundTask = new BackgroundTask<List<Arret>>() { @Override protected List<Arret> execute() { return goToRennes.getBusDao().getArrets(idTrajet, station.id); } @Override protected void callback(List<Arret> result) { if (result != null) { if (!result.isEmpty()) { Arret arret = getArretStation(result); if (arret != null) { calendrier.set(Calendar.HOUR_OF_DAY, arret.getHeuresDepart(ligne.isLigneDeNuit())); calendrier.set(Calendar.MINUTE, arret.getMinutesDepart()); TextView date = (TextView) findViewById(R.id.time); date.setText(DateFormat.format("kk:mm", calendrier)); Circuit circuit = goToRennes.getBusDao().getCircuitByIdTrajet(arret.idTrajet); TextView direction = (TextView) findViewById(R.id.directionName); direction.setText(getString(R.string.vers) + circuit.nom); } } ListView listView = (ListView) findViewById(R.id.stops); listView.setAdapter(getStopTimesAdapter(result)); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { Arret arret = (Arret) adapter.getItemAtPosition(position); Station station = goToRennes.getBusDao().getStation(arret.idStation); Intent intent = new Intent(getApplicationContext(), BusStationActivity.class); intent.putExtra("station", station); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); } } }; backgroundTask.start(this); } protected Arret getArretStation(List<Arret> arrets) { for (Arret arret : arrets) { if (arret.idStation == station.id) { return arret; } } return null; } protected ListAdapter getStopTimesAdapter(List<Arret> stopTimes) { return new ArrayAdapter<Arret>(this, 0, stopTimes) { @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = vi.inflate(R.layout.bustrip_stop, null); } Arret stopTime = getItem(position); Station station = goToRennes.getBusDao().getStation(stopTime.idStation); TextView time = (TextView) view.findViewById(R.id.stoptime); time.setText(stopTime.getDepart()); TextView name = (TextView) view.findViewById(R.id.stopname); name.setText(station.nom); return view; } }; } public void nextTrip(View view) { long nextTrajet = goToRennes.getBusDao().getTrajetSuivant(ligne.id, station.id, calendrier, ligne.isLigneDeNuit()); if (nextTrajet == 0) { Toast.makeText(getApplicationContext(), getString(R.string.busDernierPassage), Toast.LENGTH_SHORT).show(); } else { idTrajet = nextTrajet; loadStopTimes(); } } public void previousTrip(View view) { long previousTrajet = goToRennes.getBusDao().getTrajetPrecedent(ligne.id, station.id, calendrier, ligne.isLigneDeNuit()); if (previousTrajet == 0) { Toast.makeText(getApplicationContext(), getString(R.string.busPremierPassage), Toast.LENGTH_SHORT).show(); } else { idTrajet = previousTrajet; loadStopTimes(); } } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { calendrier.set(Calendar.YEAR, year); calendrier.set(Calendar.MONTH, monthOfYear); calendrier.set(Calendar.DAY_OF_MONTH, dayOfMonth); TextView date = (TextView) findViewById(R.id.date); date.setText(DateFormat.format("dd/MM/yyyy", calendrier)); idTrajet = goToRennes.getBusDao().getTrajetSuivant(ligne.id, station.id, calendrier, ligne.isLigneDeNuit()); loadStopTimes(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendrier.set(Calendar.HOUR_OF_DAY, hourOfDay); calendrier.set(Calendar.MINUTE, minute); TextView date = (TextView) findViewById(R.id.time); date.setText(DateFormat.format("kk:mm", calendrier)); idTrajet = goToRennes.getBusDao().getTrajetSuivant(ligne.id, station.id, calendrier, ligne.isLigneDeNuit()); loadStopTimes(); } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, calendrier.get(Calendar.YEAR), calendrier.get(Calendar.MONTH), calendrier.get(Calendar.DAY_OF_MONTH)); case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, calendrier.get(Calendar.HOUR_OF_DAY), calendrier.get(Calendar.MINUTE), true); } return null; } }