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