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; |
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; |
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()); |
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); |
136 |
|
|
137 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
138 |
|
|
|
boolean success; |
|
139 |
|
|
140 |
@Override |
@Override |
141 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
143 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
144 |
|
|
145 |
|
|
146 |
if (success) { |
if (timetables != null) { |
147 |
|
commFailCounter = 0; |
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, getString(timetablelist_nodata)); |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
152 |
} |
} |
153 |
} else { // communication or parse error |
} else { // communication or parse error |
154 |
|
commFailCounter++; |
155 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
156 |
builder.setMessage(getString(timetablelist_fetcherror)); |
builder.setMessage(getString(timetablelist_fetcherror)); |
157 |
builder.setCancelable(true); |
builder.setCancelable(true); |
158 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
159 |
public void onClick(DialogInterface dialog, int id) { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
160 |
dialog.dismiss(); |
public void onClick(DialogInterface dialog, int id) { |
161 |
startTimetableFetcher(); |
dialog.dismiss(); |
162 |
|
startTimetableFetcher(); |
163 |
} |
|
164 |
}); |
} |
165 |
|
}); |
166 |
|
} |
167 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
168 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
169 |
dialog.dismiss(); |
dialog.dismiss(); |
170 |
|
TimetableList.this.finish(); |
171 |
} |
} |
172 |
}); |
}); |
173 |
|
|
185 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
186 |
String type = arg0[0]; |
String type = arg0[0]; |
187 |
String trainID = arg0[1]; |
String trainID = arg0[1]; |
188 |
success = provider.lookupTimetable(type, trainID); |
timetables = provider.lookupTimetable(type, trainID); |
|
timetables = provider.getTimetable(type, trainID); |
|
189 |
|
|
190 |
return null; |
return null; |
191 |
} |
} |