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