1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import android.app.ListActivity; |
|
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; |
import android.os.Bundle; |
15 |
|
import android.widget.ListView; |
16 |
import android.widget.TextView; |
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 ListActivity { |
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 |
@Override |
34 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
35 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
36 |
setContentView(R.layout.timetablelist); |
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()); |
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 |
* case DLG_DETAILS: |
@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); |
DepartureBean currentDeparture = departures.get(selectedItemId); |
73 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
74 |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
82 |
break; |
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 |
} |
} |