1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
|
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata; |
7 |
|
import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror; |
8 |
|
import static dk.thoerup.traininfo.R.string.timetablelist_nodata; |
9 |
|
|
10 |
import java.util.ArrayList; |
import java.util.ArrayList; |
11 |
import java.util.List; |
import java.util.List; |
12 |
|
|
|
import android.app.Activity; |
|
13 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
14 |
import android.app.Dialog; |
import android.app.Dialog; |
15 |
|
import android.app.ListActivity; |
16 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
17 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
18 |
import android.content.Intent; |
import android.content.Intent; |
19 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
20 |
import android.os.Bundle; |
import android.os.Bundle; |
21 |
|
import android.util.Log; |
22 |
|
import android.view.View; |
23 |
import android.widget.ListView; |
import android.widget.ListView; |
24 |
import android.widget.TextView; |
import android.widget.TextView; |
25 |
|
import android.widget.Toast; |
26 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
27 |
import dk.thoerup.traininfo.provider.TimetableProvider; |
import dk.thoerup.traininfo.provider.TimetableProvider; |
28 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
29 |
|
|
30 |
public class TimetableList extends Activity { |
public class TimetableList extends ListActivity { |
31 |
|
|
32 |
private static final int DLG_PROGRESS = 8000; |
private static final int DLG_PROGRESS = 8000; |
33 |
DepartureBean departure; |
DepartureBean departure; |
45 |
|
|
46 |
provider = ProviderFactory.getTimetableProvider(); |
provider = ProviderFactory.getTimetableProvider(); |
47 |
|
|
48 |
adapter = new TimetableListAdapter(this); |
adapter = new TimetableListAdapter(this); |
49 |
|
setListAdapter(adapter); |
50 |
ListView lv = (ListView) findViewById(R.id.List); |
|
|
lv.setAdapter(adapter); |
|
51 |
|
|
52 |
|
|
53 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
55 |
|
|
56 |
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
57 |
((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); |
((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); |
58 |
|
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
59 |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
60 |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString()); |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); |
61 |
|
|
62 |
|
|
63 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
67 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
68 |
} |
} |
69 |
} |
} |
70 |
|
|
71 |
|
@Override |
72 |
|
protected void onDestroy() { |
73 |
|
super.onDestroy(); |
74 |
|
|
75 |
|
if (fetcher != null) { |
76 |
|
fetcher.cancel(true); |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
@Override |
@Override |
81 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
82 |
|
super.onListItemClick(l, v, position, id); |
83 |
|
|
84 |
|
TimetableBean tt = timetables.get(position); |
85 |
|
|
86 |
|
StationBean station = new StationBean(); |
87 |
|
station.setName( tt.getStation() ); |
88 |
|
station.setId( tt.getStationId() ); |
89 |
|
station.setRegional(true); |
90 |
|
|
91 |
|
Intent intent = new Intent(this, DepartureList.class); |
92 |
|
intent.putExtra("stationbean", station); |
93 |
|
startActivity(intent); |
94 |
|
|
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@Override |
100 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
101 |
{ |
{ |
102 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
103 |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
104 |
} |
} |
105 |
|
|
|
/* 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; |
|
|
|
|
|
*/ |
|
106 |
|
|
107 |
@Override |
@Override |
108 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
119 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
120 |
switch (id) { |
switch (id) { |
121 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
122 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
123 |
dlg.setMessage("Fetch timetable data"); |
dlg.setMessage( getString(timetablelist_fetchdata) ); |
124 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
125 |
return dlg; |
return dlg; |
126 |
default: |
default: |
131 |
void startTimetableFetcher() { |
void startTimetableFetcher() { |
132 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
133 |
fetcher = new TimetableFetcher(); |
fetcher = new TimetableFetcher(); |
134 |
fetcher.execute(departure.getTrainNumber()); |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
135 |
} |
} |
136 |
|
|
137 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
145 |
|
|
146 |
|
|
147 |
if (success) { |
if (success) { |
148 |
|
TimetableList.this.getListView().invalidateViews(); |
149 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
150 |
if (timetables.size() == 0) { |
if (timetables.size() == 0) { |
151 |
MessageBox.showMessage(TimetableList.this, "No timetable found"); |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
152 |
} |
} |
153 |
} else { // communication or parse error |
} else { // communication or parse error |
154 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
155 |
builder.setMessage("Error finding departures"); |
builder.setMessage(getString(timetablelist_fetcherror)); |
156 |
builder.setCancelable(true); |
builder.setCancelable(true); |
157 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
158 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
159 |
dialog.dismiss(); |
dialog.dismiss(); |
160 |
startTimetableFetcher(); |
startTimetableFetcher(); |
161 |
|
|
162 |
} |
} |
163 |
}); |
}); |
164 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
165 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
166 |
dialog.dismiss(); |
dialog.dismiss(); |
167 |
|
TimetableList.this.finish(); |
168 |
} |
} |
169 |
}); |
}); |
170 |
builder.show(); |
|
171 |
|
try { |
172 |
|
builder.show(); |
173 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
174 |
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
175 |
|
} |
176 |
|
|
177 |
} |
} |
178 |
|
|
179 |
} |
} |
180 |
|
|
181 |
@Override |
@Override |
182 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
183 |
String trainID = arg0[0]; |
String type = arg0[0]; |
184 |
success = provider.lookupTimetable(trainID); |
String trainID = arg0[1]; |
185 |
timetables = provider.getTimetable(trainID); |
success = provider.lookupTimetable(type, trainID); |
186 |
|
timetables = provider.getTimetable(type, trainID); |
187 |
|
|
188 |
return null; |
return null; |
189 |
} |
} |