1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
import java.util.ArrayList; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
import java.util.List; |
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata; |
7 |
import android.app.Activity; |
import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror; |
8 |
|
import static dk.thoerup.traininfo.R.string.timetablelist_nodata; |
9 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
10 |
import android.app.Dialog; |
import android.app.Dialog; |
11 |
|
import android.app.ListActivity; |
12 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
13 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
14 |
import android.content.Intent; |
import android.content.Intent; |
15 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
16 |
import android.os.Bundle; |
import android.os.Bundle; |
17 |
|
import android.util.Log; |
18 |
|
import android.view.View; |
19 |
import android.widget.ListView; |
import android.widget.ListView; |
20 |
import android.widget.TextView; |
import android.widget.TextView; |
21 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
22 |
|
import dk.thoerup.android.traininfo.common.TimetableBean; |
23 |
|
import dk.thoerup.android.traininfo.common.TimetableEntry; |
24 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
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; |
TimetableBean timetables; |
36 |
|
int commFailCounter = 0; |
37 |
|
|
38 |
TimetableProvider provider; |
TimetableProvider provider; |
39 |
|
|
40 |
@SuppressWarnings("unchecked") |
|
41 |
@Override |
@Override |
42 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
43 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
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()); |
58 |
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
((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(this)); |
((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() )); |
61 |
|
|
62 |
|
|
63 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
64 |
|
|
65 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
66 |
startTimetableFetcher(); |
startTimetableFetcher(); |
67 |
} else { |
} else { |
68 |
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
timetables = (TimetableBean) savedInstanceState.getSerializable("timetables"); |
69 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
70 |
} |
} |
71 |
} |
} |
72 |
|
|
73 |
|
@Override |
74 |
|
protected void onDestroy() { |
75 |
|
super.onDestroy(); |
76 |
|
|
77 |
|
if (fetcher != null) { |
78 |
|
fetcher.cancel(true); |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
@Override |
@Override |
83 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
84 |
|
super.onListItemClick(l, v, position, id); |
85 |
|
|
86 |
|
TimetableEntry tt = timetables.entries.get(position); |
87 |
|
|
88 |
|
StationEntry station = new StationEntry(); |
89 |
|
station.setName( tt.getStation() ); |
90 |
|
station.setId( tt.getStationId() ); |
91 |
|
station.setIsRegional(true); |
92 |
|
|
93 |
|
|
94 |
|
Intent intent = new Intent(this, DepartureList.class); |
95 |
|
intent.putExtra("stationbean", station); |
96 |
|
startActivity(intent); |
97 |
|
|
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@Override |
103 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
104 |
{ |
{ |
105 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
106 |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
outState.putSerializable("timetables", (TimetableBean) timetables); |
107 |
} |
} |
108 |
|
|
109 |
|
|
134 |
void startTimetableFetcher() { |
void startTimetableFetcher() { |
135 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
136 |
fetcher = new TimetableFetcher(); |
fetcher = new TimetableFetcher(); |
137 |
fetcher.execute(departure.getTrainNumber()); |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
public String getLastUpdateString(int lastUpdate) { |
142 |
|
String minutes = this.getString(R.string.departurebean_minutes); |
143 |
|
String unknown = this.getString(R.string.departurebean_unknown); |
144 |
|
switch (lastUpdate) { |
145 |
|
case 1: |
146 |
|
return "<3 " + minutes; |
147 |
|
case 2: |
148 |
|
return "3-10 " + minutes; |
149 |
|
case 3: |
150 |
|
return ">3 " + minutes; |
151 |
|
case 4: |
152 |
|
return unknown; |
153 |
|
default: |
154 |
|
return ""; |
155 |
|
} |
156 |
} |
} |
157 |
|
|
158 |
|
|
159 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
160 |
|
|
|
boolean success; |
|
161 |
|
|
162 |
@Override |
@Override |
163 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
165 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
166 |
|
|
167 |
|
|
168 |
if (success) { |
if (timetables != null) { |
169 |
|
commFailCounter = 0; |
170 |
|
TimetableList.this.getListView().invalidateViews(); |
171 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
172 |
if (timetables.size() == 0) { |
if (timetables.entries.size() == 0) { |
173 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
174 |
} |
} |
175 |
} else { // communication or parse error |
} else { // communication or parse error |
176 |
|
commFailCounter++; |
177 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
178 |
builder.setMessage(getString(timetablelist_fetcherror)); |
builder.setMessage(getString(timetablelist_fetcherror)); |
179 |
builder.setCancelable(true); |
builder.setCancelable(true); |
180 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
181 |
public void onClick(DialogInterface dialog, int id) { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
182 |
dialog.dismiss(); |
public void onClick(DialogInterface dialog, int id) { |
183 |
startTimetableFetcher(); |
dialog.dismiss(); |
184 |
|
startTimetableFetcher(); |
185 |
} |
|
186 |
}); |
} |
187 |
|
}); |
188 |
|
} |
189 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
190 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
191 |
dialog.dismiss(); |
dialog.dismiss(); |
192 |
|
TimetableList.this.finish(); |
193 |
} |
} |
194 |
}); |
}); |
195 |
builder.show(); |
|
196 |
|
try { |
197 |
|
builder.show(); |
198 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
199 |
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
200 |
|
} |
201 |
|
|
202 |
} |
} |
203 |
|
|
204 |
} |
} |
205 |
|
|
206 |
@Override |
@Override |
207 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
208 |
String trainID = arg0[0]; |
String type = arg0[0]; |
209 |
success = provider.lookupTimetable(trainID); |
String trainID = arg0[1]; |
210 |
timetables = provider.getTimetable(trainID); |
timetables = provider.lookupTimetable(type, trainID); |
211 |
|
|
212 |
return null; |
return null; |
213 |
} |
} |