/*******************************************************************************
* 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;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import fr.gotorennes.domain.Arret;
import fr.gotorennes.domain.BikeStation;
import fr.gotorennes.domain.Circuit;
import fr.gotorennes.domain.Ligne;
import fr.gotorennes.domain.MetroStation;
import fr.gotorennes.domain.RelayPark;
import fr.gotorennes.domain.Station;
import fr.gotorennes.util.BackgroundTask;
public class FavorisActivity extends AbstractActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favoris);
}
@Override
protected String getTrackingName() {
return "favoris";
}
@Override
protected int getMapIcon() {
return 0;
}
@Override
protected void init(Intent intent) {
BackgroundTask<List<Ligne>> backgroundTask = new BackgroundTask<List<Ligne>>() {
List<View> arrets = new ArrayList<View>();
List<View> metros = new ArrayList<View>();
List<View> bikes = new ArrayList<View>();
List<View> relayParks = new ArrayList<View>();
@Override
protected List<Ligne> execute() {
SharedPreferences prefs = getSharedPreferences("fr.gotorennes.Application", 0);
Map<String, ?> map = prefs.getAll();
for (Map.Entry<String, ?> entry : map.entrySet()) {
String key = entry.getKey();
String id = key.substring(key.indexOf("-") + 1);
//Avant version 0.8 -> migration du favoris
if (key.startsWith("arret-")) {
String[] ids = id.split(";");
Station station = goToRennes.getBusDao().getStation(Long.parseLong(ids[0]));
Circuit circuit = goToRennes.getBusDao().getCircuit(Long.parseLong(ids[1]));
Editor editor = prefs.edit();
editor.remove(key);
if( circuit != null) {
Ligne ligne = goToRennes.getBusDao().getLigne(circuit.idLigne);
id = station.code + ";" + ligne.code;
key = "arretbus-" + id;
editor.putString(key, "");
}
editor.commit();
}
//Migration 0.8.1 vers les codes au lieu des ids
if (key.startsWith("bus-")) {
String[] ids = id.split(";");
Station station = goToRennes.getBusDao().getStation(Long.parseLong(ids[0]));
Ligne ligne = goToRennes.getBusDao().getLigne(Long.parseLong(ids[1]));
Editor editor = prefs.edit();
editor.remove(key);
if( station != null || ligne != null ) {
id = station.code + ";" + ligne.code;
key = "arretbus-" + id;
editor.putString(key, "");
}
editor.commit();
}
if (key.startsWith("arretbus-")) {
String[] ids = id.split(";");
final Station station = goToRennes.getBusDao().getStation(ids[0]);
final Ligne ligne = goToRennes.getBusDao().getLigne(ids[1]);
if (station == null || ligne == null) {
Editor editor = prefs.edit();
editor.remove(key);
editor.commit();
continue;
}
View view = (RelativeLayout) getLayoutInflater().inflate(R.layout.favoris_listitem, null);
ImageView lineIcon = (ImageView) view.findViewById(R.id.icon);
Bitmap icon = goToRennes.getBusStationService().getBitmapIcon(FavorisActivity.this, ligne.id);
lineIcon.setImageBitmap(icon);
TextView name = (TextView) view.findViewById(R.id.stationName);
name.setText(Html.fromHtml("<u>" + station.nom + "</u>"));
TextView direction = (TextView) view.findViewById(R.id.directionName);
Arret arret = goToRennes.getBusDao().getProchainPassage(station.id, ligne.id, ligne.isLigneDeNuit());
if(arret != null) {
TextView nextStop = (TextView) view.findViewById(R.id.nextStopTime);
nextStop.setText(arret.getDepart());
TextView nextStopMinutes = (TextView) view.findViewById(R.id.nextStopTimeInMinutes);
nextStopMinutes.setText(arret.getDureeAvantDepart(ligne.isLigneDeNuit(), getResources()));
Circuit circuit = goToRennes.getBusDao().getCircuitByIdTrajet(arret.idTrajet);
direction.setText(getString(R.string.vers) + circuit.nom);
}
else {
Circuit circuit = goToRennes.getBusDao().getCircuit(ligne, station);
if(circuit == null) {
Editor editor = prefs.edit();
editor.remove(key);
editor.commit();
continue;
}
direction.setText(getString(R.string.vers) + circuit.nom);
}
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(getMyLocationFormattedDistance(station.latitude, station.longitude));
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), BusArretActivity.class);
intent.putExtra("station", station);
intent.putExtra("ligne", ligne);
startActivity(intent);
}
});
arrets.add(view);
} else if (key.startsWith("metro-")) {
final MetroStation station = goToRennes.getMetroStationService().getMetroStation(id);
if (station != null) {
View view = (RelativeLayout) getLayoutInflater().inflate(R.layout.metrostation_listitem, null);
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(Html.fromHtml("<u>" + station.name + "</u>"));
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(getMyLocationFormattedDistance(station.latitude, station.longitude));
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MetroStationActivity.class);
intent.putExtra("metroStation", station);
startActivity(intent);
}
});
metros.add(view);
}
} else if (key.startsWith("bike-")) {
final BikeStation station = goToRennes.getBikeStationService().getBikeStation(id);
if (station != null) {
View view = (RelativeLayout) getLayoutInflater().inflate(R.layout.bikestation_listitem, null);
TextView name = (TextView) view.findViewById(R.id.bikeStationName);
name.setText(Html.fromHtml("<u>" + station.name + "</u>"));
TextView availableBikes = (TextView) view.findViewById(R.id.availableBikes);
availableBikes.setText(String.valueOf(station.bikesAvailable));
availableBikes.setTextColor(station.getBikeColor());
TextView availableSlots = (TextView) view.findViewById(R.id.availableSlots);
availableSlots.setText(String.valueOf(station.slotsAvailable));
availableSlots.setTextColor(station.getSlotColor());
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(getMyLocationFormattedDistance(station.latitude, station.longitude));
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), BikeStationActivity.class);
intent.putExtra("bikeStation", station);
startActivity(intent);
}
});
bikes.add(view);
}
} else if (key.startsWith("relayPark-")) {
final RelayPark station = goToRennes.getRelayParkService().getRelayPark(id);
if (station != null) {
View view = (RelativeLayout) getLayoutInflater().inflate(R.layout.relaypark_listitem, null);
ProximiteResultActivity.populateRelayPark(view, station, true);
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(getMyLocationFormattedDistance(station.latitude, station.longitude));
TextView name = (TextView) view.findViewById(R.id.relayParkName);
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), RelayParkActivity.class);
intent.putExtra("relayPark", station);
startActivity(intent);
}
});
relayParks.add(view);
}
} else {
// Autres propriétés
continue;
}
}
return null;
}
@Override
protected void callback(List<Ligne> result) {
LinearLayout listViewBus = (LinearLayout) findViewById(R.id.favorisBus);
findViewById(R.id.titreBus).setVisibility(arrets.isEmpty() ? View.GONE : View.VISIBLE);
LinearLayout listViewMetro = (LinearLayout) findViewById(R.id.favorisMetro);
findViewById(R.id.titreMetro).setVisibility(metros.isEmpty() ? View.GONE : View.VISIBLE);
LinearLayout listViewVelo = (LinearLayout) findViewById(R.id.favorisVelo);
findViewById(R.id.titreVelo).setVisibility(bikes.isEmpty() ? View.GONE : View.VISIBLE);
LinearLayout listViewParcRelais = (LinearLayout) findViewById(R.id.favorisParcRelais);
findViewById(R.id.titreParcRelais).setVisibility(relayParks.isEmpty() ? View.GONE : View.VISIBLE);
for (View view : arrets) {
listViewBus.addView(view);
}
for (View view : metros) {
listViewMetro.addView(view);
}
for (View view : bikes) {
listViewVelo.addView(view);
}
for (View view : relayParks) {
listViewParcRelais.addView(view);
}
}
};
backgroundTask.start(this);
}
}