/*******************************************************************************
* 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.Collections;
import java.util.Comparator;
import java.util.List;
import android.content.Intent;
import android.content.SharedPreferences;
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.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.PointDeVente;
import fr.gotorennes.domain.RelayPark;
import fr.gotorennes.domain.Station;
import fr.gotorennes.domain.StationGroup;
import fr.gotorennes.util.BackgroundTask;
import fr.gotorennes.view.ExpandableView;
public class ProximiteResultActivity extends AbstractActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.proximite_result);
}
@Override
protected String getTrackingName() {
return "proximitesResult";
}
@Override
protected void init(Intent intent) {
latitude = intent.getDoubleExtra("latitude", 0);
longitude = intent.getDoubleExtra("longitude", 0);
loadProximityData();
}
protected List<MetroStation> getMetroStations() {
return goToRennes.getMetroStationService().getProximityMetroStations(latitude, longitude);
}
protected void hideProximityData() {
ExpandableView expandMetro = (ExpandableView) findViewById(R.id.expandMetro);
expandMetro.setVisibility(View.GONE);
expandMetro.reset();
ExpandableView expandBus = (ExpandableView) findViewById(R.id.expandBus);
expandBus.setVisibility(View.GONE);
expandBus.reset();
ExpandableView expandBike = (ExpandableView) findViewById(R.id.expandBike);
expandBike.setVisibility(View.GONE);
expandBike.reset();
}
protected void loadProximityData() {
SharedPreferences prefs = getSharedPreferences("fr.gotorennes.Application", 0);
ExpandableView expandMetro = (ExpandableView) findViewById(R.id.expandMetro);
expandMetro.setBackgroundTask(getMetroBackgroundTask());
expandMetro.setVisibility(prefs.getBoolean("checkMetro", true) ? View.VISIBLE : View.GONE);
expandMetro.reset();
ExpandableView expandBus = (ExpandableView) findViewById(R.id.expandBus);
expandBus.setBackgroundTask(getBusBackgroundTask());
expandBus.setVisibility(prefs.getBoolean("checkBus", true) ? View.VISIBLE : View.GONE);
expandBus.reset();
ExpandableView expandBike = (ExpandableView) findViewById(R.id.expandBike);
expandBike.setBackgroundTask(getBikeBackgroundTask());
expandBike.setVisibility(prefs.getBoolean("checkBike", true) ? View.VISIBLE : View.GONE);
expandBike.reset();
}
protected List<PointDeVente> getPointsDeVente() {
return goToRennes.getPointDeVenteService().getProximityPointsDeVente(latitude, longitude);
}
private BackgroundTask<?> getMetroBackgroundTask() {
return new BackgroundTask<List<MetroStation>>() {
@Override
protected List<MetroStation> execute() {
return getMetroStations();
}
@Override
protected void callback(List<MetroStation> result) {
if (result == null) {
showError(getString(R.string.erreurChargementMetro));
return;
}
LinearLayout listView = (LinearLayout) findViewById(R.id.metroStations);
listView.removeAllViews();
for (final MetroStation metroStation : result) {
View view = layoutInflater.inflate(R.layout.metrostation_listitem, null);
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(Html.fromHtml("<u>" + metroStation.name + "</u>"));
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(Html.fromHtml("<u>" + getFormattedDistance(metroStation.latitude, metroStation.longitude) + "</u>"));
distance.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ItineraireMapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
intent.putExtra("latitudeDest", metroStation.latitude);
intent.putExtra("longitudeDest", metroStation.longitude);
intent.putExtra("icon", getMapIcon());
intent.putExtra("iconDest", R.drawable.metro);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MetroStationActivity.class);
intent.putExtra("metroStation", metroStation);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
listView.addView(view);
}
}
};
}
protected List<StationGroup> getBusStations() {
return goToRennes.getBusDao().getStationsProches(latitude, longitude);
}
private BackgroundTask<?> getBusBackgroundTask() {
return new BackgroundTask<List<StationGroup>>() {
@Override
protected List<StationGroup> execute() {
List<StationGroup> stations = getBusStations();
Collections.sort(stations, new Comparator<StationGroup>() {
@Override
public int compare(StationGroup object1, StationGroup object2) {
Double distance1 = AbstractMapActivity.getDistance(latitude, longitude, object1.latitude, object1.longitude);
Double distance2 = AbstractMapActivity.getDistance(latitude, longitude, object2.latitude, object2.longitude);
return distance1.compareTo(distance2);
}
});
return stations;
}
@Override
protected void callback(List<StationGroup> result) {
LinearLayout listView = (LinearLayout) findViewById(R.id.busStations);
listView.removeAllViews();
for (final StationGroup stationGroup : result) {
View view = layoutInflater.inflate(R.layout.busstop_listitem, null);
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(Html.fromHtml("<u>" + stationGroup.nom + "</u>"));
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(Html.fromHtml("<u>" + getFormattedDistance(stationGroup.latitude, stationGroup.longitude) + "</u>"));
distance.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ItineraireMapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
intent.putExtra("latitudeDest", stationGroup.latitude);
intent.putExtra("longitudeDest", stationGroup.longitude);
intent.putExtra("icon", getMapIcon());
intent.putExtra("iconDest", R.drawable.bus);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
final LinearLayout details = (LinearLayout) view.findViewById(R.id.details);
BackgroundTask<List<Passage>> backgroundTask = new BackgroundTask<List<Passage>>(true) {
@Override
protected List<Passage> execute() {
List<Passage> passages = new ArrayList<Passage>();
List<Station> stations = goToRennes.getBusDao().getStations(stationGroup);
for(Station station : stations) {
for(Ligne ligne : goToRennes.getBusDao().getLignes(station)) {
Passage passage = new Passage();
passage.station = station;
passage.ligne = ligne;
passage.arret = goToRennes.getBusDao().getProchainPassage(station.id, ligne.id, ligne.isLigneDeNuit());
if(passage.arret != null) {
passage.circuit = goToRennes.getBusDao().getCircuitByIdTrajet(passage.arret.idTrajet);
}
else {
passage.circuit = goToRennes.getBusDao().getCircuit(passage.ligne, passage.station);
}
passages.add(passage);
}
}
Collections.sort(passages);
return passages;
}
@Override
protected void callback(List<Passage> result) {
details.removeAllViews();
for (final Passage passage : result) {
View detailView = getLayoutInflater().inflate(R.layout.busstop_detail_listitem, null);
Ligne ligne = passage.ligne;
Arret arret = passage.arret;
Circuit circuit = passage.circuit;
ImageView lineIcon = (ImageView) detailView.findViewById(R.id.icon);
Bitmap icon = goToRennes.getBusStationService().getBitmapIcon(ProximiteResultActivity.this, ligne.id);
lineIcon.setImageBitmap(icon);
TextView name = (TextView) detailView.findViewById(R.id.name);
name.setText(Html.fromHtml("<u>" + circuit.nom + "</u>"));
if(arret != null) {
TextView nextStop = (TextView) detailView.findViewById(R.id.nextStopTime);
nextStop.setText(arret.getDepart());
TextView nextStopMinutes = (TextView) detailView.findViewById(R.id.nextStopTimeInMinutes);
nextStopMinutes.setText(arret.getDureeAvantDepart(ligne.isLigneDeNuit(), getResources()));
}
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ProximiteResultActivity.this, BusArretActivity.class);
intent.putExtra("station", passage.station);
intent.putExtra("ligne", passage.ligne);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
details.addView(detailView);
}
}
};
backgroundTask.start(ProximiteResultActivity.this);
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), BusStationActivity.class);
intent.putExtra("stationGroup", stationGroup);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
listView.addView(view);
}
}
};
}
class Passage implements Comparable<Passage> {
Station station;
Ligne ligne;
Circuit circuit;
Arret arret;
@Override
public int compareTo(Passage another) {
return ligne.compareTo(another.ligne);
}
}
protected List<RelayPark> getRelayParks() {
return goToRennes.getRelayParkService().getProximityRelayParks(latitude, longitude);
}
protected List<BikeStation> getBikeStations() {
return goToRennes.getBikeStationService().getProximityBikeStations(latitude, longitude);
}
private BackgroundTask<?> getBikeBackgroundTask() {
return new BackgroundTask<List<BikeStation>>() {
@Override
protected List<BikeStation> execute() {
return getBikeStations();
}
@Override
protected void callback(List<BikeStation> result) {
if (result == null) {
showError(getString(R.string.erreurChargementVelo));
return;
}
LinearLayout listView = (LinearLayout) findViewById(R.id.bikeStations);
listView.removeAllViews();
for (final BikeStation bikeStation : result) {
View view = layoutInflater.inflate(R.layout.bikestation_listitem, null);
TextView name = (TextView) view.findViewById(R.id.bikeStationName);
name.setText(Html.fromHtml("<u>" + bikeStation.name + "</u>"));
TextView availableBikes = (TextView) view.findViewById(R.id.availableBikes);
availableBikes.setText(String.valueOf(bikeStation.bikesAvailable));
availableBikes.setTextColor(bikeStation.getBikeColor());
TextView availableSlots = (TextView) view.findViewById(R.id.availableSlots);
availableSlots.setText(String.valueOf(bikeStation.slotsAvailable));
availableSlots.setTextColor(bikeStation.getSlotColor());
TextView distance = (TextView) view.findViewById(R.id.distance);
distance.setText(Html.fromHtml("<u>" + getFormattedDistance(bikeStation.latitude, bikeStation.longitude) + "</u>"));
distance.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ItineraireMapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
intent.putExtra("latitudeDest", bikeStation.latitude);
intent.putExtra("longitudeDest", bikeStation.longitude);
intent.putExtra("icon", getMapIcon());
intent.putExtra("iconDest", R.drawable.bike);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), BikeStationActivity.class);
intent.putExtra("bikeStation", bikeStation);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
listView.addView(view);
}
}
};
}
protected static TextView populateRelayPark(View view, RelayPark relayPark, boolean underline) {
TextView name = (TextView) view.findViewById(R.id.relayParkName);
if (underline) {
name.setText(Html.fromHtml("<u>" + relayPark.name + "</u>"));
} else {
name.setText(relayPark.name);
}
View openedView = view.findViewById(R.id.relayParkOpen);
View closedView = view.findViewById(R.id.relayParkClose);
View fullView = view.findViewById(R.id.relayParkFull);
switch (relayPark.state) {
case 0: // Ouvert
openedView.setVisibility(View.VISIBLE);
closedView.setVisibility(View.GONE);
fullView.setVisibility(View.GONE);
TextView available = (TextView) view.findViewById(R.id.available);
available.setText(String.valueOf(relayPark.available));
available.setTextColor(relayPark.getColor());
TextView capacity = (TextView) view.findViewById(R.id.capacity);
capacity.setText(String.valueOf(relayPark.capacity));
break;
case 2: // Complet
openedView.setVisibility(View.GONE);
closedView.setVisibility(View.GONE);
fullView.setVisibility(View.VISIBLE);
break;
default:
openedView.setVisibility(View.GONE);
closedView.setVisibility(View.VISIBLE);
fullView.setVisibility(View.GONE);
}
return name;
}
@Override
protected int getMapIcon() {
return 0;
}
}