1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; |
4 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_cancel; |
6 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
7 |
|
|
8 |
|
|
9 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
10 |
import java.util.ArrayList; |
|
|
import java.util.List; |
|
11 |
|
|
12 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
13 |
import android.app.Dialog; |
import android.app.Dialog; |
18 |
import android.net.Uri; |
import android.net.Uri; |
19 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
20 |
import android.os.Bundle; |
import android.os.Bundle; |
21 |
|
import android.util.Log; |
22 |
import android.view.View; |
import android.view.View; |
23 |
|
import android.view.View.OnClickListener; |
24 |
|
import android.widget.Button; |
25 |
import android.widget.ListView; |
import android.widget.ListView; |
26 |
import android.widget.TextView; |
import android.widget.TextView; |
27 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
35 |
|
|
36 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
37 |
DepartureProvider provider; |
DepartureProvider provider; |
38 |
List<DepartureBean> departures; |
DepartureBean departures; |
39 |
|
|
40 |
int selectedItemId; |
int selectedItemId; |
41 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
46 |
|
|
47 |
StationBean station; |
StationBean station; |
48 |
|
|
49 |
@SuppressWarnings("unchecked") |
boolean arrival = false; |
50 |
|
|
51 |
|
|
52 |
@Override |
@Override |
53 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
54 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
66 |
|
|
67 |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
68 |
|
|
69 |
|
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
70 |
|
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
71 |
|
|
72 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
departureBtn.setOnClickListener( new OnClickListener() { |
73 |
|
@Override |
74 |
|
public void onClick(View arg0) { |
75 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
76 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
77 |
|
arrival = false; |
78 |
|
startDepartureFetcher(); |
79 |
|
} |
80 |
|
}); |
81 |
|
arrivalBtn.setOnClickListener( new OnClickListener() { |
82 |
|
@Override |
83 |
|
public void onClick(View arg0) { |
84 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
85 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button); |
86 |
|
arrival = true; |
87 |
|
startDepartureFetcher(); |
88 |
|
} |
89 |
|
}); |
90 |
|
|
|
NumberFormat format = NumberFormat.getNumberInstance(); |
|
|
format.setMaximumFractionDigits(1); |
|
|
format.setMinimumFractionDigits(1); |
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
95 |
|
|
96 |
int distance = station.getDistance(); |
int distance = station.getDistance(); |
97 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
if (distance != 0) { |
98 |
|
NumberFormat format = NumberFormat.getNumberInstance(); |
99 |
|
format.setMaximumFractionDigits(1); |
100 |
|
format.setMinimumFractionDigits(1); |
101 |
|
|
102 |
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
103 |
|
} else { |
104 |
|
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
if (station.isRegional() == false && station.isSTrain() == false) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
109 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
110 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
111 |
|
departureBtn.setVisibility( View.GONE ); |
112 |
|
arrivalBtn.setVisibility(View.GONE); |
113 |
|
|
114 |
} else { |
} else { |
115 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
117 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
118 |
startDepartureFetcher(); |
startDepartureFetcher(); |
119 |
} else { |
} else { |
120 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
departures = (DepartureBean) savedInstanceState.getSerializable("departures"); |
121 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures.entries); |
122 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
123 |
} |
} |
124 |
} |
} |
132 |
|
|
133 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
134 |
|
|
135 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
outState.putSerializable("departures", departures); |
136 |
} |
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
@Override |
@Override |
141 |
|
protected void onDestroy() { |
142 |
|
super.onDestroy(); |
143 |
|
|
144 |
|
if (fetcher != null) { |
145 |
|
fetcher.cancel(true); |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
@Override |
150 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
151 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
152 |
|
|
153 |
selectedItemId = position; |
selectedItemId = position; |
154 |
|
|
155 |
DepartureBean dep = departures.get(selectedItemId); |
DepartureEntry dep = departures.entries.get(selectedItemId); |
156 |
|
|
157 |
Intent intent = new Intent(this, TimetableList.class); |
Intent intent = new Intent(this, TimetableList.class); |
158 |
intent.putExtra("departure", dep); |
intent.putExtra("departure", dep); |
169 |
switch (id) { |
switch (id) { |
170 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
171 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
172 |
|
int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; |
173 |
|
pgDialog.setMessage( getString(messageId) ); |
174 |
break; |
break; |
175 |
} |
} |
176 |
} |
} |
179 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
180 |
switch (id) { |
switch (id) { |
181 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
182 |
|
|
183 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
|
dlg.setMessage("Fetch departure data"); |
|
184 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
185 |
return dlg; |
return dlg; |
186 |
default: |
default: |
230 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
231 |
|
|
232 |
if (success) { |
if (success) { |
233 |
adapter.setDepartures(departures); |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
234 |
if (departures.size() == 0) { |
adapter.setDepartures(departures.entries); |
235 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
236 |
|
|
237 |
|
if (departures.entries.size() == 0) { |
238 |
|
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
239 |
} |
} |
240 |
} else { // communication or parse error |
} else { // communication or parse error |
241 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
242 |
builder.setMessage("Error finding departures"); |
builder.setMessage("Error finding departures"); |
243 |
builder.setCancelable(true); |
builder.setCancelable(true); |
244 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
245 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
246 |
dialog.dismiss(); |
dialog.dismiss(); |
247 |
startDepartureFetcher(); |
startDepartureFetcher(); |
248 |
|
|
249 |
} |
} |
250 |
}); |
}); |
251 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
252 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
253 |
dialog.dismiss(); |
dialog.dismiss(); |
254 |
|
DepartureList.this.finish(); |
255 |
} |
} |
256 |
}); |
}); |
257 |
builder.show(); |
|
258 |
|
try { |
259 |
|
builder.show(); |
260 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
261 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
262 |
|
} |
263 |
} |
} |
264 |
} |
} |
265 |
|
|
266 |
@Override |
@Override |
267 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
268 |
success = provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
269 |
departures = provider.getDepartures(params[0]); |
departures = provider.getDepartures(params[0], DepartureList.this.arrival); |
270 |
return null; |
return null; |
271 |
} |
} |
272 |
|
|