4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
|
7 |
|
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
9 |
import android.app.ListActivity; |
import android.app.ListActivity; |
10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
|
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.net.Uri; |
import android.net.Uri; |
14 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
19 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
20 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
21 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
22 |
|
import static dk.thoerup.traininfo.R.string.*; |
23 |
|
|
24 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
25 |
|
|
26 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
27 |
public static final int DLG_DETAILS = 2; |
|
28 |
|
|
29 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
30 |
DepartureProvider provider; |
DepartureProvider provider; |
34 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
35 |
|
|
36 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
|
Dialog detailsDialog; |
|
|
DepartureFetcher fetcher; |
|
37 |
|
|
38 |
double latitude,longitude; |
DepartureFetcher fetcher; |
39 |
|
|
40 |
|
StationBean station; |
41 |
|
|
42 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
43 |
@Override |
@Override |
49 |
setListAdapter(adapter); |
setListAdapter(adapter); |
50 |
|
|
51 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
52 |
|
|
53 |
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
54 |
|
|
55 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
56 |
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
|
57 |
|
|
58 |
String name = launchedBy.getStringExtra("name"); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
|
((TextView) findViewById(R.id.stationName)).setText( name ); |
|
59 |
|
|
|
String addr = launchedBy.getStringExtra("address"); |
|
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
60 |
|
|
61 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
62 |
|
|
63 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
64 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
65 |
format.setMinimumFractionDigits(1); |
format.setMinimumFractionDigits(1); |
|
int distance = launchedBy.getIntExtra("distance", 0); |
|
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
|
|
|
|
66 |
|
|
67 |
provider = ProviderFactory.getDepartureProvider(); |
|
68 |
|
int distance = station.getDistance(); |
69 |
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
70 |
|
|
71 |
fetcher = new DepartureFetcher(); |
|
72 |
if (savedInstanceState == null) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
73 |
showDialog(DLG_PROGRESS); |
getListView().setVisibility( View.GONE ); |
74 |
fetcher.execute(name); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
75 |
|
|
76 |
} else { |
} else { |
77 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
provider = ProviderFactory.getDepartureProvider(); |
78 |
adapter.setDepartures(departures); |
|
79 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
if (savedInstanceState == null) { |
80 |
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
startDepartureFetcher(); |
81 |
if (detailsShowing) |
} else { |
82 |
showDialog(DLG_DETAILS); |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
83 |
|
adapter.setDepartures(departures); |
84 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
85 |
|
} |
86 |
} |
} |
87 |
} |
} |
88 |
|
|
91 |
{ |
{ |
92 |
if (pgDialog != null && pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
93 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
94 |
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
|
|
if (detailsShowing) { |
|
|
dismissDialog(DLG_DETAILS); |
|
|
} |
|
|
outState.putBoolean("detailsShowing", detailsShowing); |
|
95 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
96 |
|
|
97 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
100 |
@Override |
@Override |
101 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
102 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
103 |
|
|
104 |
|
selectedItemId = position; |
105 |
|
|
106 |
|
DepartureBean dep = departures.get(selectedItemId); |
107 |
|
|
108 |
selectedItemId = position; |
Intent intent = new Intent(this, TimetableList.class); |
109 |
showDialog(DLG_DETAILS); |
intent.putExtra("departure", dep); |
110 |
|
|
111 |
|
startActivity(intent); |
112 |
|
|
113 |
} |
} |
114 |
|
|
115 |
|
|
118 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
119 |
|
|
120 |
switch (id) { |
switch (id) { |
|
case DLG_DETAILS: |
|
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
|
|
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
|
|
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
|
|
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
|
|
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
|
|
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
|
|
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
|
|
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
|
|
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
|
|
detailsDialog = dialog; |
|
|
break; |
|
121 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
122 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
123 |
break; |
break; |
129 |
switch (id) { |
switch (id) { |
130 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
131 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
132 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage( getString(departurelist_fetchdata) ); |
133 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
134 |
return dlg; |
return dlg; |
|
case DLG_DETAILS: |
|
|
//Context mContext = getApplicationContext(); |
|
|
Dialog dialog = new Dialog(this); |
|
|
dialog.setCancelable(true); |
|
|
|
|
|
dialog.setContentView(R.layout.departuredetails); |
|
|
dialog.setTitle("Departure details"); |
|
|
|
|
|
View root = dialog.findViewById(R.id.layout_root); |
|
|
root.setOnClickListener( new DialogDismisser(dialog) ); |
|
|
return dialog; |
|
135 |
default: |
default: |
136 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
137 |
} |
} |
138 |
} |
} |
139 |
|
|
140 |
|
void startDepartureFetcher() { |
141 |
|
showDialog(DLG_PROGRESS); |
142 |
|
fetcher = new DepartureFetcher(); |
143 |
|
fetcher.execute(station.getId()); |
144 |
|
} |
145 |
|
|
146 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
147 |
|
|
148 |
Dialog dlg; |
Dialog dlg; |
154 |
public void onClick(View v) { |
public void onClick(View v) { |
155 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
156 |
dlg.dismiss(); |
dlg.dismiss(); |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
161 |
@Override |
@Override |
162 |
public void onClick(View v) { |
public void onClick(View v) { |
163 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
164 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
165 |
} |
} |
166 |
}; |
}; |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
171 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
172 |
|
boolean success; |
173 |
|
|
174 |
@Override |
@Override |
175 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
176 |
super.onPostExecute(result); |
super.onPostExecute(result); |
177 |
|
|
178 |
adapter.setDepartures(departures); |
|
179 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
180 |
|
|
181 |
if (departures.size() == 0) |
if (success) { |
182 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures); |
183 |
|
if (departures.size() == 0) { |
184 |
|
MessageBox.showMessage(DepartureList.this, "No departures found"); |
185 |
|
} |
186 |
|
} else { // communication or parse error |
187 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
188 |
|
builder.setMessage("Error finding departures"); |
189 |
|
builder.setCancelable(true); |
190 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
191 |
|
public void onClick(DialogInterface dialog, int id) { |
192 |
|
dialog.dismiss(); |
193 |
|
startDepartureFetcher(); |
194 |
|
|
195 |
|
} |
196 |
|
}); |
197 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
198 |
|
public void onClick(DialogInterface dialog, int id) { |
199 |
|
dialog.dismiss(); |
200 |
|
} |
201 |
|
}); |
202 |
|
builder.show(); |
203 |
|
} |
204 |
} |
} |
205 |
|
|
206 |
@Override |
@Override |
207 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
208 |
provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0]); |
209 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(params[0]); |
210 |
return null; |
return null; |
211 |
} |
} |
212 |
|
|