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 dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
26 |
import dk.thoerup.traininfo.provider.TimetableProvider; |
import dk.thoerup.traininfo.provider.TimetableProvider; |
27 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
|
import static dk.thoerup.traininfo.R.string.*; |
|
28 |
|
|
29 |
public class TimetableList extends Activity { |
public class TimetableList extends ListActivity { |
30 |
|
|
31 |
private static final int DLG_PROGRESS = 8000; |
private static final int DLG_PROGRESS = 8000; |
32 |
DepartureBean departure; |
DepartureEntry departure; |
33 |
TimetableListAdapter adapter; |
TimetableListAdapter adapter; |
34 |
TimetableFetcher fetcher; |
TimetableFetcher fetcher; |
35 |
List<TimetableBean> timetables; |
List<TimetableBean> timetables; |
36 |
|
int commFailCounter = 0; |
37 |
|
|
38 |
TimetableProvider provider; |
TimetableProvider provider; |
39 |
|
|
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(); |
54 |
departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); |
departure = (DepartureEntry) launchedBy.getSerializableExtra("departure"); |
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()); |
68 |
} |
} |
69 |
} |
} |
70 |
|
|
71 |
@Override |
@Override |
72 |
|
protected void onStart() { |
73 |
|
super.onStart(); |
74 |
|
ProviderFactory.purgeOldEntries(); |
75 |
|
} |
76 |
|
|
77 |
|
@Override |
78 |
|
protected void onDestroy() { |
79 |
|
super.onDestroy(); |
80 |
|
|
81 |
|
if (fetcher != null) { |
82 |
|
fetcher.cancel(true); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
|
@Override |
87 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
88 |
|
super.onListItemClick(l, v, position, id); |
89 |
|
|
90 |
|
TimetableBean tt = timetables.get(position); |
91 |
|
|
92 |
|
StationBean station = new StationBean(); |
93 |
|
station.setName( tt.getStation() ); |
94 |
|
station.setId( tt.getStationId() ); |
95 |
|
station.setRegional(true); |
96 |
|
|
97 |
|
Intent intent = new Intent(this, DepartureList.class); |
98 |
|
intent.putExtra("stationbean", station); |
99 |
|
startActivity(intent); |
100 |
|
|
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
@Override |
106 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
107 |
{ |
{ |
108 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
109 |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
110 |
} |
} |
111 |
|
|
|
/* 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; |
|
|
|
|
|
*/ |
|
112 |
|
|
113 |
@Override |
@Override |
114 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
137 |
void startTimetableFetcher() { |
void startTimetableFetcher() { |
138 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
139 |
fetcher = new TimetableFetcher(); |
fetcher = new TimetableFetcher(); |
140 |
fetcher.execute(departure.getTrainNumber()); |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
141 |
} |
} |
142 |
|
|
143 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
144 |
|
|
|
boolean success; |
|
145 |
|
|
146 |
@Override |
@Override |
147 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
149 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
150 |
|
|
151 |
|
|
152 |
if (success) { |
if (timetables != null) { |
153 |
|
commFailCounter = 0; |
154 |
|
TimetableList.this.getListView().invalidateViews(); |
155 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
156 |
if (timetables.size() == 0) { |
if (timetables.size() == 0) { |
157 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
158 |
} |
} |
159 |
} else { // communication or parse error |
} else { // communication or parse error |
160 |
|
commFailCounter++; |
161 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
162 |
builder.setMessage(getString(timetablelist_fetcherror)); |
builder.setMessage(getString(timetablelist_fetcherror)); |
163 |
builder.setCancelable(true); |
builder.setCancelable(true); |
164 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
165 |
public void onClick(DialogInterface dialog, int id) { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
166 |
dialog.dismiss(); |
public void onClick(DialogInterface dialog, int id) { |
167 |
startTimetableFetcher(); |
dialog.dismiss(); |
168 |
|
startTimetableFetcher(); |
169 |
} |
|
170 |
}); |
} |
171 |
|
}); |
172 |
|
} |
173 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
174 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
175 |
dialog.dismiss(); |
dialog.dismiss(); |
176 |
|
TimetableList.this.finish(); |
177 |
} |
} |
178 |
}); |
}); |
179 |
builder.show(); |
|
180 |
|
try { |
181 |
|
builder.show(); |
182 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
183 |
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
184 |
|
} |
185 |
|
|
186 |
} |
} |
187 |
|
|
188 |
} |
} |
189 |
|
|
190 |
@Override |
@Override |
191 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
192 |
String trainID = arg0[0]; |
String type = arg0[0]; |
193 |
success = provider.lookupTimetable(trainID); |
String trainID = arg0[1]; |
194 |
timetables = provider.getTimetable(trainID); |
timetables = provider.lookupTimetable(type, trainID); |
195 |
|
|
196 |
return null; |
return null; |
197 |
} |
} |