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