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