1 |
torben |
362 |
package dk.thoerup.traininfo; |
2 |
|
|
|
3 |
torben |
365 |
|
4 |
torben |
630 |
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 |
torben |
365 |
import java.util.ArrayList; |
11 |
|
|
import java.util.List; |
12 |
|
|
|
13 |
|
|
import android.app.AlertDialog; |
14 |
|
|
import android.app.Dialog; |
15 |
torben |
839 |
import android.app.ListActivity; |
16 |
torben |
365 |
import android.app.ProgressDialog; |
17 |
|
|
import android.content.DialogInterface; |
18 |
torben |
364 |
import android.content.Intent; |
19 |
torben |
365 |
import android.os.AsyncTask; |
20 |
torben |
362 |
import android.os.Bundle; |
21 |
torben |
630 |
import android.util.Log; |
22 |
torben |
362 |
import android.widget.TextView; |
23 |
torben |
365 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
24 |
|
|
import dk.thoerup.traininfo.provider.TimetableProvider; |
25 |
|
|
import dk.thoerup.traininfo.util.MessageBox; |
26 |
torben |
362 |
|
27 |
torben |
839 |
public class TimetableList extends ListActivity { |
28 |
torben |
364 |
|
29 |
torben |
365 |
private static final int DLG_PROGRESS = 8000; |
30 |
torben |
364 |
DepartureBean departure; |
31 |
torben |
365 |
TimetableListAdapter adapter; |
32 |
|
|
TimetableFetcher fetcher; |
33 |
|
|
List<TimetableBean> timetables; |
34 |
torben |
364 |
|
35 |
torben |
365 |
TimetableProvider provider; |
36 |
|
|
|
37 |
|
|
@SuppressWarnings("unchecked") |
38 |
torben |
362 |
@Override |
39 |
|
|
protected void onCreate(Bundle savedInstanceState) { |
40 |
|
|
super.onCreate(savedInstanceState); |
41 |
|
|
setContentView(R.layout.timetablelist); |
42 |
torben |
364 |
|
43 |
torben |
365 |
provider = ProviderFactory.getTimetableProvider(); |
44 |
|
|
|
45 |
torben |
839 |
adapter = new TimetableListAdapter(this); |
46 |
|
|
setListAdapter(adapter); |
47 |
torben |
366 |
|
48 |
torben |
839 |
|
49 |
torben |
366 |
|
50 |
torben |
364 |
Intent launchedBy = getIntent(); |
51 |
|
|
departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); |
52 |
|
|
|
53 |
|
|
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
54 |
|
|
((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); |
55 |
torben |
398 |
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
56 |
torben |
364 |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
57 |
torben |
563 |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); |
58 |
torben |
366 |
|
59 |
torben |
365 |
|
60 |
|
|
if (savedInstanceState == null) { |
61 |
|
|
startTimetableFetcher(); |
62 |
|
|
} else { |
63 |
|
|
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
64 |
|
|
adapter.setTimetable(timetables); |
65 |
|
|
} |
66 |
torben |
362 |
} |
67 |
torben |
365 |
|
68 |
torben |
839 |
|
69 |
torben |
365 |
@Override |
70 |
|
|
public void onSaveInstanceState(Bundle outState) |
71 |
|
|
{ |
72 |
|
|
dismissDialog(DLG_PROGRESS); |
73 |
|
|
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
74 |
|
|
} |
75 |
torben |
364 |
|
76 |
torben |
365 |
|
77 |
|
|
@Override |
78 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) { |
79 |
|
|
super.onPrepareDialog(id, dialog); |
80 |
|
|
|
81 |
|
|
switch (id) { |
82 |
|
|
case DLG_PROGRESS: |
83 |
|
|
//pgDialog = (ProgressDialog) dialog; |
84 |
|
|
break; |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
@Override |
89 |
|
|
protected Dialog onCreateDialog(int id) { |
90 |
|
|
switch (id) { |
91 |
|
|
case DLG_PROGRESS: |
92 |
torben |
561 |
ProgressDialog dlg = new ProgressDialog(this); |
93 |
|
|
dlg.setMessage( getString(timetablelist_fetchdata) ); |
94 |
torben |
365 |
dlg.setCancelable(true); |
95 |
|
|
return dlg; |
96 |
|
|
default: |
97 |
|
|
return super.onCreateDialog(id); |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
void startTimetableFetcher() { |
102 |
|
|
showDialog(DLG_PROGRESS); |
103 |
|
|
fetcher = new TimetableFetcher(); |
104 |
torben |
699 |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
105 |
torben |
365 |
} |
106 |
|
|
|
107 |
|
|
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
108 |
|
|
|
109 |
|
|
boolean success; |
110 |
|
|
|
111 |
|
|
@Override |
112 |
|
|
protected void onPostExecute(Void result) { |
113 |
|
|
super.onPostExecute(result); |
114 |
|
|
dismissDialog(DLG_PROGRESS); |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
if (success) { |
118 |
|
|
adapter.setTimetable(timetables); |
119 |
|
|
if (timetables.size() == 0) { |
120 |
torben |
561 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); |
121 |
torben |
365 |
} |
122 |
|
|
} else { // communication or parse error |
123 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
124 |
torben |
561 |
builder.setMessage(getString(timetablelist_fetcherror)); |
125 |
torben |
365 |
builder.setCancelable(true); |
126 |
torben |
561 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
127 |
torben |
365 |
public void onClick(DialogInterface dialog, int id) { |
128 |
|
|
dialog.dismiss(); |
129 |
|
|
startTimetableFetcher(); |
130 |
|
|
|
131 |
|
|
} |
132 |
|
|
}); |
133 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
134 |
torben |
365 |
public void onClick(DialogInterface dialog, int id) { |
135 |
|
|
dialog.dismiss(); |
136 |
|
|
} |
137 |
torben |
630 |
}); |
138 |
|
|
|
139 |
|
|
try { |
140 |
|
|
builder.show(); |
141 |
|
|
} catch (android.view.WindowManager.BadTokenException e) { |
142 |
|
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
143 |
|
|
} |
144 |
|
|
|
145 |
torben |
365 |
} |
146 |
|
|
|
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
@Override |
150 |
|
|
protected Void doInBackground(String... arg0) { |
151 |
torben |
699 |
String type = arg0[0]; |
152 |
|
|
String trainID = arg0[1]; |
153 |
|
|
success = provider.lookupTimetable(type, trainID); |
154 |
|
|
timetables = provider.getTimetable(type, trainID); |
155 |
torben |
365 |
|
156 |
|
|
return null; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
} |
160 |
torben |
362 |
} |