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