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